Fix keyboard driver

This commit is contained in:
Furkan Mudanyali 2022-07-22 00:24:26 +03:00
parent 1f35b80e2e
commit 3c0af135b9
2 changed files with 35 additions and 52 deletions

View File

@ -23,16 +23,12 @@ void kernel_main(void) {
uint8_t hour = inb(0x71); uint8_t hour = inb(0x71);
printf("Time: %x:%x:%x\n", hour, min, sec); printf("Time: %x:%x:%x\n", hour, min, sec);
unsigned char oldkey; unsigned char key;
unsigned char key = readkey(); for(;;) {
for(;;){
oldkey = key;
key = readkey(); key = readkey();
if (key != '\0') { if(key)
putchar(key); putchar(key);
} }
}
asm("cli; hlt"); asm("cli; hlt");
} }

View File

@ -8,76 +8,63 @@
#define BACKSPACE_P 0x0F #define BACKSPACE_P 0x0F
#define ENTER_P 0x1C #define ENTER_P 0x1C
#define LEFT_C_P 0x1D #define LEFT_C_P 0x1D
#define LEFT_SHIFT_P 0x2B #define LEFT_SHIFT_P 0x2A
#define RIGHT_SHIFT_P 0x36 #define RIGHT_SHIFT_P 0x36
#define LEFT_ALT_P 0x38 #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 unsigned char interrupt_key;
static uint8_t keyset[89]; static uint8_t keyset[89];
static uint64_t keytimes[89]; static uint64_t keytimes[89];
static unsigned char keymap[89] = { static unsigned char keymap[89] = {
0, ESC_P, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', BACKSPACE_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', '[', ']', ENTER_P, LEFT_C_P, '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', 0, 0,
'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`', LEFT_SHIFT_P, '\\', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`', 0, '\\',
'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', RIGHT_SHIFT_P, '*', LEFT_ALT_P, ' ', CAPS_LCK_P '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() { void initialize_keyset() {
memset(keyset, 0, 89); memset(keyset, 0, 89);
memset(keytimes, 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) { __attribute__((interrupt)) void keyboard_handler(void* frame) {
interrupt_key = inb(0x60); interrupt_key = inb(0x60);
pic_eoi(1); 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; keyset[interrupt_key - 0x80] = 0;
keytimes[interrupt_key - 0x80] = 0;
} else { } else {
if(keyset[interrupt_key] == 2)
return;
keyset[interrupt_key] = 1; 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) { unsigned char readkey() {
switch(i) { if((interrupt_key & 128) != 128) {
case ESC_P: unsigned char tempkey = interrupt_key;
case LEFT_C_P: interrupt_key = 0;
case LEFT_SHIFT_P: if ((!caps_lock && (keyset[LEFT_SHIFT_P] || keyset[RIGHT_SHIFT_P])) ||
case RIGHT_SHIFT_P: (caps_lock && !keyset[LEFT_SHIFT_P] && !keyset[RIGHT_SHIFT_P])) {
case LEFT_ALT_P: return keymapshift[tempkey];
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 { } else {
keyset[i] = 2; return keymap[tempkey];
}
} 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];
}
} }
} }
return 0; return 0;