110 lines
3.0 KiB
C
110 lines
3.0 KiB
C
#include "keyboard.h"
|
|
#include "interrupts.h"
|
|
#include "kernel.h"
|
|
#include "vga.h"
|
|
#include "util.h"
|
|
|
|
#define SCANCODE_LEFT_SHIFT 42
|
|
#define SCANCODE_RIGHT_SHIFT 54
|
|
#define SCANCODE_CAPS_LOCK 58
|
|
|
|
#define KEYBOARD_HANDLERS_LENGTH 32
|
|
|
|
unsigned const char us_keymap[128] = {
|
|
0, 27, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\b', '\t', 'q', 'w',
|
|
'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n', 0, 'a', 's', 'd', 'f', 'g', 'h',
|
|
'j', 'k', 'l', ';', '\'', '`', 0, '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/',
|
|
0, '*', 0, ' ', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
0, 0, '-', 0, 0, 0, '+', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
};
|
|
|
|
unsigned const char us_keymap_shift[128] = {
|
|
0, 27, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '\b', '\t', 'Q', 'W',
|
|
'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n', 0, 'A', 'S', 'D', 'F', 'G', 'H',
|
|
'J', 'K', 'L', ':', '"', '~', 0, '|', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?',
|
|
0, '*', 0, ' ', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
0, 0, '-', 0, 0, 0, '+', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
};
|
|
|
|
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;
|
|
|
|
char scan_code_to_ascii(uint8_t scan_code)
|
|
{
|
|
if (scan_code >= 128)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
if ((!caps_lock && shift) || (caps_lock && !shift))
|
|
{
|
|
return us_keymap_shift[scan_code];
|
|
}
|
|
else
|
|
{
|
|
return us_keymap[scan_code];
|
|
}
|
|
}
|
|
|
|
void handle_keyboard(const 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,
|
|
.ascii = scan_code_to_ascii(scan_code),
|
|
};
|
|
|
|
for (size_t i = 0; i < handler_index; i++)
|
|
{
|
|
keyboard_handlers[i](&event);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void register_keypress_handler(const 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)
|
|
{
|
|
kprintf("Maximum keyboard handlers reached\n");
|
|
kpanic();
|
|
}
|
|
|
|
keyboard_handlers[handler_index] = handler;
|
|
handler_index += 1;
|
|
}
|
|
|
|
void init_keyboard()
|
|
{
|
|
register_irq_handler(1, handle_keyboard);
|
|
} |