From 06ade25a46e39ec28795afbc83465a6b002e60b8 Mon Sep 17 00:00:00 2001 From: Starnakin Date: Wed, 9 Oct 2024 22:58:06 +0200 Subject: [PATCH] add: hold down key && core: create get_key to simplify keyboard usage --- headers/keyboard.h | 2 ++ headers/terminal.h | 3 +-- src/drivers/keyboard.c | 46 ++++++++++-------------------------------- src/shell/exec.c | 42 +++++++++++++++++++++++++++++++++----- src/terminal/get.c | 9 +++------ 5 files changed, 54 insertions(+), 48 deletions(-) diff --git a/headers/keyboard.h b/headers/keyboard.h index 2c8810f..611cd23 100644 --- a/headers/keyboard.h +++ b/headers/keyboard.h @@ -103,3 +103,5 @@ struct key_event { uint8_t c; uint8_t scan_code; }; + +struct key_event get_key(void); \ No newline at end of file diff --git a/headers/terminal.h b/headers/terminal.h index 0a80268..b7aed9c 100644 --- a/headers/terminal.h +++ b/headers/terminal.h @@ -58,5 +58,4 @@ void terminal_set_default_fg_color(uint8_t fg_color); void terminal_set_default_bg_color(uint8_t fg_color); void terminal_change_default_fg_color(uint8_t color); uint8_t terminal_get_default_color(void); -uint8_t terminal_get_char(int column, int row); -char *get_line(void); \ No newline at end of file +uint8_t terminal_get_char(int column, int row); \ No newline at end of file diff --git a/src/drivers/keyboard.c b/src/drivers/keyboard.c index ffceee4..1453bcd 100644 --- a/src/drivers/keyboard.c +++ b/src/drivers/keyboard.c @@ -1,53 +1,29 @@ +#include "keyboard.h" #include "drivers.h" #include "interrupts.h" #include "kprintf.h" #include "shell.h" #include "string.h" #include "terminal.h" +#include #include extern struct screen *screen; -int line_status = READING; +static bool new_input_indicator = false; +static struct key_event new_input = {}; void keyboard_handler(struct registers *regs) { - size_t i = 0; - struct key_event ev; - - (void)regs; - ev = terminal_getkey(); - char *buf = screen->line; - i = strlen(screen->line); - if (ev.c == '\n') { - kprintf("\n"); - screen->line[i] = '\0'; - line_status = NEWLINE; - return; - } - const size_t size = sizeof(screen->line); - if (ev.scan_code == KEY_BACKSPACE && i) { - buf[--i] = '\0'; - move_cursor(LEFT); - terminal_putchar(' '); - move_cursor(LEFT); - } else if (ev.scan_code == KEY_TAB && i) { - auto_complete(); - } else if (ev.c && i < size - 1) { - kprintf("%c", ev.c); - buf[i++] = ev.c; - } - if (i >= size) { - kprintf("\n"); - screen->line[i] = '\0'; - line_status = NEWLINE; - } + new_input_indicator = true; + new_input = terminal_getkey(); } -char *get_line(void) +struct key_event get_key(void) { - while (line_status != NEWLINE) + + while (!new_input_indicator) ; - line_status = READING; - return screen->line; + new_input_indicator = false; + return new_input; } \ No newline at end of file diff --git a/src/shell/exec.c b/src/shell/exec.c index ff16bd3..bc91aa1 100644 --- a/src/shell/exec.c +++ b/src/shell/exec.c @@ -75,11 +75,44 @@ void auto_complete(void) } } -void shell_init(void) +static char *get_line(void) { - char *line; - kprintf(PROMPT); - while ((line = get_line())) { + size_t i = 0; + struct key_event ev; + + while (1) { + ev = get_key(); + // kprintf("%d %d\n", ev.scan_code, ev.c); + if (ev.c == '\n') + break; + char *buf = screen->line; + i = strlen(screen->line); + const size_t size = sizeof(screen->line); + if (ev.scan_code) { + if (ev.scan_code == KEY_BACKSPACE && i) { + buf[--i] = '\0'; + move_cursor(LEFT); + terminal_putchar(' '); + move_cursor(LEFT); + } else if (ev.scan_code == KEY_TAB && i) { + auto_complete(); + } else if (ev.c && i < size - 1) { + kprintf("%c", ev.c); + buf[i++] = ev.c; + } + if (i >= size) + break; + } + } + kprintf("\n"); + screen->line[i] = '\0'; +} + +void shell_init() +{ + while (1) { + kprintf(PROMPT); + get_line(); bool invalid = true; for (unsigned i = 0; i < NB_CMDS; i++) { if (!strncmp(cmds[i].name, screen->line, @@ -96,6 +129,5 @@ void shell_init(void) kprintf(KERN_WARNING "invalid command: %s\n", screen->line); memset(screen->line, '\0', sizeof(screen->line)); - kprintf(PROMPT); } } diff --git a/src/terminal/get.c b/src/terminal/get.c index 8eea7fc..11529ee 100644 --- a/src/terminal/get.c +++ b/src/terminal/get.c @@ -7,7 +7,6 @@ struct key_event terminal_getkey(void) { static bool caps_mode = false; - static uint8_t prev_scan_code = 0; struct key_event ev = {0}; uint8_t scan_code; @@ -20,13 +19,11 @@ struct key_event terminal_getkey(void) else if (scan_code == KEY_RIGHT_SHIFT + 128 || scan_code == KEY_LEFT_SHIFT + 128) caps_mode = false; - else if (scan_code != prev_scan_code && prev_scan_code != 0) { - if (scan_code < 128 && keymap[scan_code]) - ev.c = keymap[scan_code][caps_mode]; + if (scan_code < 128) { + ev.c = keymap[scan_code][caps_mode]; if (scan_code >= KEY_F1 && scan_code <= KEY_F10) terminal_set_screen(scan_code - KEY_F1); + ev.scan_code = scan_code; } - prev_scan_code = scan_code; - ev.scan_code = scan_code; return ev; }