diff --git a/lib/metalkit/keyboard.c b/lib/metalkit/keyboard.c index dd1f6ac..7047c99 100644 --- a/lib/metalkit/keyboard.c +++ b/lib/metalkit/keyboard.c @@ -53,12 +53,7 @@ * Global keyboard state */ -static struct { - Bool escape; - KeyboardIRQHandler handler; - uint32 keyDown[roundup(KEY_MAX, 32)]; -} gKeyboard; - +KeyboardPrivate gKeyboard; /* * KeyboardWrite -- @@ -122,24 +117,6 @@ KeyboardReadCB(void) } -/* - * KeyboardSetKeyPressed -- - * - * Set a key's up/down state. - */ - -static void -KeyboardSetKeyPressed(Keycode k, Bool down) -{ - uint32 mask = 1 << (k & 0x1F); - if (down) { - gKeyboard.keyDown[k >> 5] |= mask; - } else { - gKeyboard.keyDown[k >> 5] &= ~mask; - } -} - - /* * KeyboardTranslate -- * @@ -287,7 +264,7 @@ KeyboardTranslate(KeyEvent *event) } } - KeyboardSetKeyPressed(event->rawKey, event->pressed); + gKeyboard.keyDown[event->rawKey] = event->pressed; } @@ -334,39 +311,3 @@ Keyboard_Init(void) Intr_SetMask(KB_IRQ, TRUE); Intr_SetHandler(IRQ_VECTOR(KB_IRQ), KeyboardHandlerInternal); } - - -/* - * Keyboard_IsKeyPressed -- - * - * Check whether a key, identified by Keycode, is down. - */ - -fastcall Bool -Keyboard_IsKeyPressed(Keycode k) -{ - if (k < KEY_MAX) { - return (gKeyboard.keyDown[k >> 5] >> (k & 0x1F)) & 1; - } - return FALSE; -} - - -/* - * Keyboard_SetHandler -- - * - * Set a handler that will receive translated keys and scancodes. - * This handler is run within the IRQ handler, so it must complete - * quickly and use minimal stack space. - * - * The handler will be called once per scancode byte, regardless of - * whether that byte ended a key event or not. If event->key is - * zero, the event can be ignored unless you're interested in - * seeing the raw scancodes. - */ - -fastcall void -Keyboard_SetHandler(KeyboardIRQHandler handler) -{ - gKeyboard.handler = handler; -} diff --git a/lib/metalkit/keyboard.h b/lib/metalkit/keyboard.h index 73d2841..0a3f060 100644 --- a/lib/metalkit/keyboard.h +++ b/lib/metalkit/keyboard.h @@ -91,8 +91,60 @@ typedef struct KeyEvent { typedef fastcall void (*KeyboardIRQHandler)(KeyEvent *event); -fastcall void Keyboard_Init(void); -fastcall Bool Keyboard_IsKeyPressed(Keycode key); -fastcall void Keyboard_SetHandler(KeyboardIRQHandler handler); +/* + * Private data, used by the inline functions below. + */ -#endif /* __VGA_TEXT_H__ */ +typedef struct KeyboardPrivate { + Bool escape; + KeyboardIRQHandler handler; + Bool keyDown[KEY_MAX]; +} KeyboardPrivate; + +extern KeyboardPrivate gKeyboard; + + +/* + * Public Functions + */ + +fastcall void Keyboard_Init(void); + + +/* + * Keyboard_IsKeyPressed -- + * + * Check whether a key, identified by Keycode, is down. + */ + +static inline Bool +Keyboard_IsKeyPressed(Keycode k) +{ + if (k < KEY_MAX) { + return gKeyboard.keyDown[k]; + } + return FALSE; +} + + +/* + * Keyboard_SetHandler -- + * + * Set a handler that will receive translated keys and scancodes. + * This handler is run within the IRQ handler, so it must complete + * quickly and use minimal stack space. + * + * The handler will be called once per scancode byte, regardless of + * whether that byte ended a key event or not. If event->key is + * zero, the event can be ignored unless you're interested in + * seeing the raw scancodes. + */ + +static inline void +Keyboard_SetHandler(KeyboardIRQHandler handler) +{ + gKeyboard.handler = handler; +} + + +#endif /* __KEYBOARD_H__ */