keyboard driver
This commit is contained in:
72
src/keyboard.c
Normal file
72
src/keyboard.c
Normal file
@@ -0,0 +1,72 @@
|
||||
#include "keyboard.h"
|
||||
#include "idt.h"
|
||||
#include "kernel.h"
|
||||
#include "vga.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define SCANCODE_LEFT_SHIFT 42
|
||||
#define SCANCODE_RIGHT_SHIFT 54
|
||||
#define SCANCODE_CAPS_LOCK 58
|
||||
|
||||
#define KEYBOARD_HANDLERS_LENGTH 32
|
||||
|
||||
bool shift = false;
|
||||
bool caps_lock = false;
|
||||
|
||||
// todo(nub): make dynamic when a memory allocator is implemented
|
||||
static keyboard_handler_t keyboard_handlers[KEYBOARD_HANDLERS_LENGTH];
|
||||
static int handler_index = 0;
|
||||
|
||||
void handle_keyboard(isr_frame_t* frame)
|
||||
{
|
||||
uint8_t code = inb(0x60);
|
||||
uint8_t scan_code = code & 0x7F;
|
||||
bool pressed = (code & 0x80) == 0;
|
||||
|
||||
switch (scan_code)
|
||||
{
|
||||
case SCANCODE_LEFT_SHIFT:
|
||||
case SCANCODE_RIGHT_SHIFT:
|
||||
{
|
||||
shift = pressed;
|
||||
break;
|
||||
}
|
||||
case SCANCODE_CAPS_LOCK:
|
||||
{
|
||||
if (pressed)
|
||||
{
|
||||
caps_lock = !caps_lock;
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
keyboard_event_t event = { .scan_code = scan_code, .pressed = pressed, .caps_lock = caps_lock, .shift = shift };
|
||||
|
||||
for (int i = 0; i < handler_index; i++)
|
||||
{
|
||||
keyboard_handlers[i](&event);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void register_keypress_handler(keyboard_handler_t handler)
|
||||
{
|
||||
// todo(nub31): remove when a memory allocator is implemented and keyboard_handlers is a dynamic list
|
||||
if (handler_index >= KEYBOARD_HANDLERS_LENGTH)
|
||||
{
|
||||
vga_print_error();
|
||||
vga_print(" Maximum keyboard handlers reached\n");
|
||||
kpanic();
|
||||
}
|
||||
|
||||
keyboard_handlers[handler_index] = handler;
|
||||
handler_index += 1;
|
||||
}
|
||||
|
||||
void init_keyboard()
|
||||
{
|
||||
register_irq_handler(1, handle_keyboard);
|
||||
}
|
||||
Reference in New Issue
Block a user