2022-06-20 18:05:28 +02:00
|
|
|
#include <stdbool.h>
|
2022-06-19 19:29:10 +02:00
|
|
|
#include "../lib/string.h"
|
2022-06-19 19:08:16 +02:00
|
|
|
#include "keyboard.h"
|
|
|
|
#include "../arch/i686/isr.h"
|
2022-06-19 19:17:15 +02:00
|
|
|
#include "../lib/stdio.h"
|
2022-06-19 19:08:16 +02:00
|
|
|
#include "../arch/i686/io.h"
|
2022-06-19 19:17:15 +02:00
|
|
|
#include "../lib/stdio.h"
|
2022-06-19 19:52:46 +02:00
|
|
|
#include "../shell.h"
|
2022-08-07 21:58:36 +02:00
|
|
|
#include "../lib/debug.h"
|
2022-06-19 19:08:16 +02:00
|
|
|
|
2022-06-20 18:05:28 +02:00
|
|
|
static bool g_caps_lock = false;
|
|
|
|
static bool g_shift = false;
|
|
|
|
|
2022-06-19 19:08:16 +02:00
|
|
|
static char g_key_buffer[256];
|
|
|
|
|
|
|
|
static const char g_sc_ascii[128] = {
|
|
|
|
'?', '?', '1', '2', '3', '4', '5', '6',
|
|
|
|
'7', '8', '9', '0', '-', '=', '?', '?',
|
2022-06-20 18:05:28 +02:00
|
|
|
'q', 'w', 'e', 'r', 't', 'y', 'u', 'i',
|
|
|
|
'o', 'p', '[', ']', '?', '?', 'a', 's',
|
|
|
|
'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',
|
|
|
|
'\'', '`', '?', '\\', 'z', 'x', 'c', 'v',
|
|
|
|
'b', 'n', 'm', ',', '.', '/', '?', '?',
|
2022-06-19 19:08:16 +02:00
|
|
|
'?', ' '
|
|
|
|
};
|
|
|
|
|
|
|
|
static void user_input(char *str){
|
2022-06-19 19:52:46 +02:00
|
|
|
shell_run(str);
|
2022-06-19 19:08:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void keyboard_callback(){
|
|
|
|
uint8_t scancode = i686_inb(0x60);
|
2022-06-20 18:05:28 +02:00
|
|
|
if(scancode == 0x2A) g_shift = true; // Shift pressed
|
|
|
|
else if(scancode == 0x2A + 128) g_shift = false; // Shift released
|
|
|
|
else if(scancode == 0x3A) g_caps_lock = !g_caps_lock; // Caps lock pressed
|
|
|
|
else if(scancode < 128){ // If it's a "pressed" event, not "released"
|
2022-06-19 19:08:16 +02:00
|
|
|
if(scancode == 0x0E){ // Backspace
|
|
|
|
putc('\b');
|
|
|
|
g_key_buffer[strlen(g_key_buffer)-1] = '\0';
|
|
|
|
}
|
|
|
|
else if(scancode == 0x1C){ // Enter
|
|
|
|
putc('\n');
|
|
|
|
user_input(g_key_buffer);
|
|
|
|
g_key_buffer[0] = '\0';
|
|
|
|
}
|
|
|
|
else{
|
2022-06-20 18:05:28 +02:00
|
|
|
char c = g_sc_ascii[scancode];
|
|
|
|
if(c >= 'a' && c <= 'z' && g_shift != g_caps_lock) c += 'A' - 'a';
|
|
|
|
putc(c);
|
2022-06-19 19:08:16 +02:00
|
|
|
|
|
|
|
// strlen() works here because char[] is the same as
|
|
|
|
// char*, and both are null-terminated, so they are
|
|
|
|
// exactly the same
|
2022-06-19 19:52:46 +02:00
|
|
|
int len = strlen(g_key_buffer);
|
2022-06-20 18:05:28 +02:00
|
|
|
g_key_buffer[len] = c;
|
2022-06-19 19:52:46 +02:00
|
|
|
g_key_buffer[len+1] = '\0';
|
2022-06-19 19:08:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void keyboard_initialize(){
|
2022-08-07 21:58:36 +02:00
|
|
|
debug_printf("[KEYBOARD] Initializing keyboard\n");
|
2022-06-19 19:08:16 +02:00
|
|
|
i686_isr_register_handler(IRQ(1), keyboard_callback);
|
|
|
|
}
|