void keyboard_init(void);
void keyboard_set_callback(keyboard_callback_t callback);
+int keyboard_read_nonblock(char* out, uint32_t max_len);
+
#endif
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);
}
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;
+}
#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"
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;