keyboard driver

This commit is contained in:
nub31
2025-08-29 15:16:49 +02:00
parent 573398bb0f
commit 9c1b4a2c94
10 changed files with 143 additions and 28 deletions

72
src/keyboard.c Normal file
View 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);
}