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=55533be04569c73dfae0dc7ef7af93aeb0bd9e35;p=AdrOS.git tty: add nonblocking stdin (fd 0) via ring buffer --- diff --git a/include/keyboard.h b/include/keyboard.h index bf61554..9542aeb 100644 --- a/include/keyboard.h +++ b/include/keyboard.h @@ -9,4 +9,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 5d544ef..4011f91 100644 --- a/src/drivers/keyboard.c +++ b/src/drivers/keyboard.c @@ -6,7 +6,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); } @@ -14,9 +29,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 0b54f5f..fc18743 100644 --- a/src/kernel/syscall.c +++ b/src/kernel/syscall.c @@ -2,6 +2,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" @@ -81,6 +83,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;