From 3c0af135b94579bc7cb8f263b44d56b646ef5dee Mon Sep 17 00:00:00 2001 From: Furkan Mudanyali Date: Fri, 22 Jul 2022 00:24:26 +0300 Subject: [PATCH] Fix keyboard driver --- kernel/rockos/kernel.c | 10 ++---- kernel/rockos/keyboard.c | 77 +++++++++++++++++----------------------- 2 files changed, 35 insertions(+), 52 deletions(-) diff --git a/kernel/rockos/kernel.c b/kernel/rockos/kernel.c index 5bd5828..ff34bd5 100644 --- a/kernel/rockos/kernel.c +++ b/kernel/rockos/kernel.c @@ -23,15 +23,11 @@ void kernel_main(void) { uint8_t hour = inb(0x71); printf("Time: %x:%x:%x\n", hour, min, sec); - unsigned char oldkey; - unsigned char key = readkey(); - for(;;){ - oldkey = key; + unsigned char key; + for(;;) { key = readkey(); - if (key != '\0') { + if(key) putchar(key); - } - } asm("cli; hlt"); diff --git a/kernel/rockos/keyboard.c b/kernel/rockos/keyboard.c index 27b001b..ae70caf 100644 --- a/kernel/rockos/keyboard.c +++ b/kernel/rockos/keyboard.c @@ -8,76 +8,63 @@ #define BACKSPACE_P 0x0F #define ENTER_P 0x1C #define LEFT_C_P 0x1D -#define LEFT_SHIFT_P 0x2B +#define LEFT_SHIFT_P 0x2A #define RIGHT_SHIFT_P 0x36 #define LEFT_ALT_P 0x38 -#define CAPS_LCK_P 0x3A + +#define CAPS_LOCK_P 0x3A +#define CAPS_LOCK_R 0xBA static unsigned char interrupt_key; static uint8_t keyset[89]; static uint64_t keytimes[89]; static unsigned char keymap[89] = { - 0, ESC_P, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', BACKSPACE_P, - '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', ENTER_P, LEFT_C_P, - 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`', LEFT_SHIFT_P, '\\', - 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', RIGHT_SHIFT_P, '*', LEFT_ALT_P, ' ', CAPS_LCK_P + 0, 0, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', 0, + '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', 0, 0, + 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`', 0, '\\', + 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 0, '*', 0, ' ', 0 }; +static unsigned char keymapshift[89] = { + 0, 0, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', 0, + '\t', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', 0, 0, + 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '\"', '`', 0, '|', + 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?', 0, '*', 0, ' ', 0 +}; + + void initialize_keyset() { memset(keyset, 0, 89); memset(keytimes, 0, 89); + memset(&keymap[59], 0, 29); + memset(&keymapshift[59], 0, 29); } +uint8_t caps_lock = 0; + __attribute__((interrupt)) void keyboard_handler(void* frame) { interrupt_key = inb(0x60); pic_eoi(1); - if((interrupt_key & 128) == 128){ + if((interrupt_key & 128) == 128) { + if(interrupt_key == CAPS_LOCK_R) + caps_lock = !caps_lock; keyset[interrupt_key - 0x80] = 0; - keytimes[interrupt_key - 0x80] = 0; } else { - if(keyset[interrupt_key] == 2) - return; keyset[interrupt_key] = 1; } } -unsigned char readkey() { - uint8_t shift_pressed = 0; - if (keyset[LEFT_SHIFT_P] || keyset[RIGHT_SHIFT_P]) - shift_pressed = 1; - for(uint8_t i = 0; i < 89; ++i) { - switch(i) { - case ESC_P: - case LEFT_C_P: - case LEFT_SHIFT_P: - case RIGHT_SHIFT_P: - case LEFT_ALT_P: - case CAPS_LCK_P: - continue; - } - if (keyset[i]){ - uint64_t temptime = get_ticks(); - if(keytimes[i]) { - if(keyset[i] == 1) { - if(temptime - keytimes[i] < 20) { - return 0; - } else { - keyset[i] = 2; - } - } else { - if(temptime - keytimes[i] < 5) { - return 0; - } - } - } - keytimes[i] = temptime; - if (shift_pressed && (keymap[i] > 0x60 && keymap[i] < 0x7A)) { - return keymap[i] - 0x20; - } else { - return keymap[i]; - } +unsigned char readkey() { + if((interrupt_key & 128) != 128) { + unsigned char tempkey = interrupt_key; + interrupt_key = 0; + if ((!caps_lock && (keyset[LEFT_SHIFT_P] || keyset[RIGHT_SHIFT_P])) || + (caps_lock && !keyset[LEFT_SHIFT_P] && !keyset[RIGHT_SHIFT_P])) { + return keymapshift[tempkey]; + } else { + return keymap[tempkey]; } } return 0;