Kernel: Keyboard event callback can now be registered later

This commit is contained in:
Bananymous 2022-12-13 16:14:41 +02:00
parent 941238830e
commit 174daa3e02
2 changed files with 12 additions and 5 deletions

View File

@ -37,9 +37,11 @@ namespace Keyboard
bool pressed; bool pressed;
}; };
bool initialize(void (*callback)(KeyEvent)); bool initialize();
void update_keyboard(); void update_keyboard();
void register_key_event_callback(void(*callback)(KeyEvent));
char key_event_to_ascii(KeyEvent); char key_event_to_ascii(KeyEvent);
void led_disco(); void led_disco();

View File

@ -83,7 +83,7 @@ namespace Keyboard
static uint8_t s_led_states = 0b000; static uint8_t s_led_states = 0b000;
static uint8_t s_modifiers = 0x00; static uint8_t s_modifiers = 0x00;
static void (*s_key_callback)(KeyEvent) = nullptr; static void (*s_key_event_callback)(KeyEvent) = nullptr;
static char s_key_to_char[] static char s_key_to_char[]
{ {
@ -185,7 +185,8 @@ namespace Keyboard
while (!s_key_event_queue.Empty()) while (!s_key_event_queue.Empty())
{ {
s_key_callback(s_key_event_queue.Front()); if (s_key_event_callback)
s_key_event_callback(s_key_event_queue.Front());
s_key_event_queue.Pop(); s_key_event_queue.Pop();
} }
} }
@ -354,7 +355,7 @@ namespace Keyboard
} }
} }
bool initialize(void (*callback)(KeyEvent)) bool initialize()
{ {
// https://wiki.osdev.org/%228042%22_PS/2_Controller // https://wiki.osdev.org/%228042%22_PS/2_Controller
@ -424,13 +425,17 @@ namespace Keyboard
})); }));
// Register callback and IRQ // Register callback and IRQ
s_key_callback = callback;
IDT::register_irq_handler(KEYBOARD_IRQ, keyboard_irq_handler); IDT::register_irq_handler(KEYBOARD_IRQ, keyboard_irq_handler);
PIC::unmask(KEYBOARD_IRQ); PIC::unmask(KEYBOARD_IRQ);
return true; return true;
} }
void register_key_event_callback(void(*callback)(KeyEvent))
{
s_key_event_callback = callback;
}
char key_event_to_ascii(KeyEvent event) char key_event_to_ascii(KeyEvent event)
{ {
char res = s_key_to_char[static_cast<uint8_t>(event.key)]; char res = s_key_to_char[static_cast<uint8_t>(event.key)];