From: Tulio A M Mendes Date: Sat, 7 Feb 2026 18:54:15 +0000 (-0300) Subject: tty: add nonblocking stdin (fd 0) via ring buffer X-Git-Url: https://projects.tadryanom.me/?a=commitdiff_plain;h=669c77673fe7f1ade9c2ad2a732a1e1c14240cf8;p=AdrOS.git tty: add nonblocking stdin (fd 0) via ring buffer --- diff --git a/include/keyboard.h b/include/keyboard.h index 9d0f3f5c..ec990059 100644 --- a/include/keyboard.h +++ b/include/keyboard.h @@ -18,4 +18,6 @@ typedef void (*keyboard_callback_t)(char); void keyboard_init(void); void keyboard_set_callback(keyboard_callback_t callback); +int keyboard_read_nonblock(char* out, uint32_t max_len); + #endif diff --git a/src/drivers/keyboard.c b/src/drivers/keyboard.c index da6fed9b..47f3ce4e 100644 --- a/src/drivers/keyboard.c +++ b/src/drivers/keyboard.c @@ -15,7 +15,22 @@ static keyboard_callback_t active_callback = NULL; +#define KBD_BUF_SIZE 256 +static volatile uint32_t kbd_head = 0; +static volatile uint32_t kbd_tail = 0; +static char kbd_buf[KBD_BUF_SIZE]; + +static void kbd_push_char(char c) { + uint32_t next = (kbd_head + 1U) % KBD_BUF_SIZE; + if (next == kbd_tail) { + kbd_tail = (kbd_tail + 1U) % KBD_BUF_SIZE; + } + kbd_buf[kbd_head] = c; + kbd_head = next; +} + static void hal_kbd_bridge(char c) { + kbd_push_char(c); if (active_callback) { active_callback(c); } @@ -23,9 +38,23 @@ static void hal_kbd_bridge(char c) { void keyboard_init(void) { uart_print("[KBD] Initializing Keyboard Driver...\n"); + kbd_head = 0; + kbd_tail = 0; hal_keyboard_init(hal_kbd_bridge); } void keyboard_set_callback(keyboard_callback_t callback) { active_callback = callback; } + +int keyboard_read_nonblock(char* out, uint32_t max_len) { + if (!out || max_len == 0) return 0; + + uint32_t count = 0; + while (count < max_len) { + if (kbd_tail == kbd_head) break; + out[count++] = kbd_buf[kbd_tail]; + kbd_tail = (kbd_tail + 1U) % KBD_BUF_SIZE; + } + return (int)count; +} diff --git a/src/kernel/syscall.c b/src/kernel/syscall.c index b4270d08..b9deb644 100644 --- a/src/kernel/syscall.c +++ b/src/kernel/syscall.c @@ -11,6 +11,8 @@ #include "idt.h" #include "fs.h" #include "heap.h" +#include "keyboard.h" +#include "keyboard.h" #include "process.h" #include "uart_console.h" #include "uaccess.h" @@ -90,6 +92,18 @@ static int syscall_read_impl(int fd, void* user_buf, uint32_t len) { if (len > 1024 * 1024) return -1; if (user_range_ok(user_buf, (size_t)len) == 0) return -1; + if (fd == 0) { + char kbuf[256]; + uint32_t chunk = len; + if (chunk > sizeof(kbuf)) chunk = (uint32_t)sizeof(kbuf); + int rd = keyboard_read_nonblock(kbuf, chunk); + if (rd <= 0) return 0; + if (copy_to_user(user_buf, kbuf, (size_t)rd) < 0) return -1; + return rd; + } + + if (fd == 1 || fd == 2) return -1; + struct file* f = fd_get(fd); if (!f || !f->node) return -1;