HhhOS/arch/x86_64/drivers/keyboard/keyboard.c
2021-08-24 20:56:50 +03:00

102 lines
2.7 KiB
C

#include <stdbool.h>
#include <drivers/pic/pic.h>
#include <drivers/keyboard/keyboard.h>
#include <drivers/device/device.h>
unsigned char kbdbuf[KBD_BUF_SIZE];
unsigned char kbdus[128] = {
0, 27, '1', '2', '3', '4', '5', '6', '7', '8', /* 9 */
'9', '0', '-', '=', '\b', /* Backspace */
'\t', /* Tab */
'q', 'w', 'e', 'r', /* 19 */
't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n', /* Enter key */
0, /* 29 - Control */
'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', /* 39 */
'\'', '`', 0, /* Left shift */
'\\', 'z', 'x', 'c', 'v', 'b', 'n', /* 49 */
'm', ',', '.', '/', 0, /* Right shift */
'*',
0, /* Alt */
' ', /* Space bar */
0, /* Caps lock */
0, /* 59 - F1 key ... > */
0, 0, 0, 0, 0, 0, 0, 0,
0, /* < ... F10 */
0, /* 69 - Num lock*/
0, /* Scroll Lock */
0, /* Home key */
0, /* Up Arrow */
0, /* Page Up */
'-',
0, /* Left Arrow */
0,
0, /* Right Arrow */
'+',
0, /* 79 - End key*/
0, /* Down Arrow */
0, /* Page Down */
0, /* Insert Key */
0, /* Delete Key */
0, 0, 0,
0, /* F11 Key */
0, /* F12 Key */
0, /* All other keys are undefined */
};
u8 lastkey = 0;
u8 __kbd_enabled = 0;
void keyboard_buffer_push_key(unsigned char scancode) {
kbdbuf[lastkey++] = scancode;
}
unsigned char keyboard_buffer_pop_key() {
u8 index = lastkey--;
unsigned char scancode = kbdbuf[index];
kbdbuf[index] = 0;
return scancode;
}
unsigned char keyboard_get_key_from_scancode(unsigned char scancode) {
return kbdus[scancode];
}
__attribute__((interrupt)) static void keyboard_irq(interrupt_frame_t *frame) {
unsigned char scancode = inportb(0x60);
if (scancode & 128) {
// This is a release scancode, just ignore it
pic_send_eoi(1);
return;
}
keyboard_buffer_push_key(scancode);
pic_send_eoi(1);
}
u8 keyboard_enabled(void) {
return __kbd_enabled;
}
void* keyboard_read() {
return (void*) keyboard_buffer_pop_key();
}
void* keyboard_write() {
return -1;
}
int keyboard_init(void) {
pic_irq_enable(1);
idt_set_entry(33, (void*)keyboard_irq, INTERRUPT_GATE);
__kbd_enabled = 1;
struct device keyboard_device = {
write: &keyboard_write,
read: &keyboard_read
};
return push_device(keyboard_device);
}