Thu, 30 Dec 2010 00:37:03 +0000
add keyboard event handler; need to do register r/w next.
src/keyboard.c | file | annotate | diff | revisions | |
src/keyboard.h | file | annotate | diff | revisions |
1.1 diff -r 674226015c8a -r ebce87d87808 src/keyboard.c 1.2 --- a/src/keyboard.c Wed Dec 29 09:06:17 2010 +0000 1.3 +++ b/src/keyboard.c Thu Dec 30 00:37:03 2010 +0000 1.4 @@ -1,4 +1,5 @@ 1.5 #include "SDL.h" 1.6 +#include "keyboard.h" 1.7 1.8 /** 1.9 * Key map -- a mapping from SDLK_xxx constants to scancodes and vice versa. 1.10 @@ -73,7 +74,9 @@ 1.11 { SDLK_SEMICOLON, 0, 0x3b }, // ; [Semicolon] 1.12 // Keycode 3C not used 1.13 { SDLK_EQUALS, 0, 0x3d }, // = [Equals] 1.14 -// Keycodes 3E, 3F, 40 not used 1.15 +// Keycode 3E not used 1.16 +// Keycode 3F = BOGUS (Bad Keycode) 1.17 +// Keycode 40 = All Up 1.18 // { SDLK_, 1, 0x41 }, // CMD 1.19 // { SDLK_, 1, 0x42 }, // CLOSE/OPEN 1.20 { SDLK_KP7, 0, 0x43 }, // PRINT 1.21 @@ -87,8 +90,8 @@ 1.22 { SDLK_KP5, 0, 0x4a }, // Home [Keypad 5] 1.23 { SDLK_END, 0, 0x4b }, // End 1.24 { SDLK_KP6, 0, 0x4b }, // End [Keypad 6] 1.25 - { SDLK_LCTRL, 0, 0x4c }, // Left Ctrl? \___ not sure which is left and which is right... 1.26 - { SDLK_RCTRL, 0, 0x4d }, // Right Ctrl? / 1.27 + { SDLK_LCTRL, 0, 0x4c }, // Left Ctrl 1.28 + { SDLK_RCTRL, 0, 0x4d }, // Right Ctrl 1.29 // Keycodes 4E thru 5A not used 1.30 { SDLK_LEFTBRACKET, 0, 0x5b }, // [ 1.31 { SDLK_BACKSLASH, 0, 0x5c }, // \ (backslash) 1.32 @@ -122,23 +125,79 @@ 1.33 { SDLK_y, 0, 0x79 }, // Y 1.34 { SDLK_z, 0, 0x7a }, // Z 1.35 // Keycodes 7B, 7C, 7D not used 1.36 - { SDLK_NUMLOCK, 0, 0x7e } // Numlock 1.37 + { SDLK_NUMLOCK, 0, 0x7e } // Numlock 1.38 // { SDLK_, 1, 0x7f }, // Dlete 1.39 }; 1.40 1.41 /** 1.42 - * List of key states 1.43 + * List of special key codes 1.44 + */ 1.45 +enum { 1.46 + KEY_ALL_UP = 0x40, ///< All keys up 1.47 + KEY_LIST_END = 0x80, ///< End of key code list 1.48 + KEY_BEGIN_MOUSE = 0xCF, ///< Mouse data follows 1.49 + KEY_BEGIN_KEYBOARD = 0xDF, ///< Keyboard data follows 1.50 +}; 1.51 + 1.52 +/** 1.53 + * List of keyboard commands 1.54 */ 1.55 -int keystate[0x80]; 1.56 +enum { 1.57 + KEY_CMD_RESET = 0x92, ///< Reset keyboard 1.58 + KEY_CMD_CAPSLED_OFF = 0xB1, ///< Caps Lock LED off--CHECK! 1.59 + KEY_CMD_CAPSLED_ON = 0xB0, ///< Caps Lock LED on --CHECK! 1.60 + KEY_CMD_NUMLED_OFF = 0xA1, ///< Num Lock LED off --CHECK! 1.61 + KEY_CMD_NUMLED_ON = 0xA0, ///< Num Lock LED on --CHECK! 1.62 + KEY_CMD_MOUSE_ENABLE = 0xD0, ///< Enable mouse 1.63 + KEY_CMD_MOUSE_DISABLE = 0xD1 ///< Disable mouse 1.64 +}; 1.65 1.66 -void keyboard_init(void) 1.67 +void keyboard_init(KEYBOARD_STATE *state) 1.68 { 1.69 // Set all key states to "not pressed" 1.70 - for (int i=0; i<(sizeof(keystate)/sizeof(keystate[0])); i++) { 1.71 - keystate[i] = 0; 1.72 + for (int i=0; i<(sizeof(state->keystate)/sizeof(state->keystate[0])); i++) { 1.73 + state->keystate[i] = 0; 1.74 } 1.75 } 1.76 1.77 -void keyboard_event(SDL_Event *ev) 1.78 +void keyboard_event(KEYBOARD_STATE *state, SDL_Event *ev) 1.79 { 1.80 + int ks; 1.81 + 1.82 + // Figure out the event type 1.83 + if (ev->type == SDL_KEYDOWN) { 1.84 + // Key down event 1.85 + ks = 1; 1.86 + } else if (ev->type == SDL_KEYUP) { 1.87 + // Key up event 1.88 + ks = 0; 1.89 + } else { 1.90 + // Not a keyboard event 1.91 + return; 1.92 + } 1.93 + 1.94 + // Loop over the keyinfo, try and find a match for this key 1.95 + // TODO: handle Extended Keymap 1.96 + for (int i=0; i<(sizeof(keymap)/sizeof(keymap[0])); i++) { 1.97 + if (ev->key.keysym.sym == keymap[i].key) { 1.98 + // Key code match. Is this an Extended Map key? 1.99 + if (keymap[i].extended) { 1.100 + // Key is on the Extended map. Need ALT set when pressing the key. 1.101 + if (ev->key.keysym.mod & KMOD_ALT) { 1.102 + // ALT is down, key matches. 1.103 + state->keystate[keymap[i].scancode] = ks; 1.104 + break; 1.105 + } 1.106 + } else { 1.107 + // Key is on the Standard map. ALT must NOT be set when pressing the key. 1.108 + if (!(ev->key.keysym.mod & KMOD_ALT)) { 1.109 + // ALT is up, key matches 1.110 + state->keystate[keymap[i].scancode] = ks; 1.111 + break; 1.112 + } 1.113 + } 1.114 + } 1.115 + } 1.116 } 1.117 + 1.118 +// TODO: register read and write
2.1 diff -r 674226015c8a -r ebce87d87808 src/keyboard.h 2.2 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.3 +++ b/src/keyboard.h Thu Dec 30 00:37:03 2010 +0000 2.4 @@ -0,0 +1,33 @@ 2.5 +#ifndef _KEYBOARD_H 2.6 +#define _KEYBOARD_H 2.7 + 2.8 +#define KEYBOARD_BUFFER_SIZE 0x100 2.9 + 2.10 +/** 2.11 + * Keyboard controller state 2.12 + */ 2.13 +typedef struct { 2.14 + unsigned char keybuf[KEYBOARD_BUFFER_SIZE]; ///< Keyboard data buffer 2.15 + size_t readptr; ///< Keyboard buffer read pointer 2.16 + size_t writeptr; ///< Keyboard buffer write pointer 2.17 + size_t buflen; ///< Keyboard buffer fill level (buffer length) 2.18 + int keystate[0x80]; ///< List of key up/down states 2.19 +} KEYBOARD_STATE; 2.20 + 2.21 +/** 2.22 + * Initialise the keyboard. 2.23 + * 2.24 + * Initialises a keyboard state block in preparation for keyboard events. 2.25 + * 2.26 + * @param state Keyboard state block 2.27 + */ 2.28 +void keyboard_init(KEYBOARD_STATE *state); 2.29 + 2.30 +/** 2.31 + * Issue a keyboard event. 2.32 + * 2.33 + * Call this function when SDL issues a keyboard event in the event loop. 2.34 + */ 2.35 +void keyboard_event(KEYBOARD_STATE *state, SDL_Event *ev); 2.36 + 2.37 +#endif