src/keyboard.c

changeset 86
ebce87d87808
parent 74
51cbc7a44cd9
child 90
934ae2efdd01
     1.1 --- a/src/keyboard.c	Wed Dec 29 09:06:17 2010 +0000
     1.2 +++ b/src/keyboard.c	Thu Dec 30 00:37:03 2010 +0000
     1.3 @@ -1,4 +1,5 @@
     1.4  #include "SDL.h"
     1.5 +#include "keyboard.h"
     1.6  
     1.7  /**
     1.8   * Key map -- a mapping from SDLK_xxx constants to scancodes and vice versa.
     1.9 @@ -73,7 +74,9 @@
    1.10  	{ SDLK_SEMICOLON,		0,	0x3b },	// ;			[Semicolon]
    1.11  // Keycode 3C not used
    1.12  	{ SDLK_EQUALS,			0,	0x3d },	// =			[Equals]
    1.13 -// Keycodes 3E, 3F, 40 not used
    1.14 +// Keycode 3E not used
    1.15 +// Keycode 3F = BOGUS (Bad Keycode)
    1.16 +// Keycode 40 = All Up
    1.17  //	{ SDLK_,				1,	0x41 },	// CMD
    1.18  //	{ SDLK_,				1,	0x42 },	// CLOSE/OPEN
    1.19  	{ SDLK_KP7,				0,	0x43 },	// PRINT
    1.20 @@ -87,8 +90,8 @@
    1.21  	{ SDLK_KP5,				0,	0x4a },	// Home			[Keypad 5]
    1.22  	{ SDLK_END,				0,	0x4b },	// End
    1.23  	{ SDLK_KP6,				0,	0x4b },	// End			[Keypad 6]
    1.24 -	{ SDLK_LCTRL,			0,	0x4c },	// Left Ctrl?	\___ not sure which is left and which is right...
    1.25 -	{ SDLK_RCTRL,			0,	0x4d },	// Right Ctrl?	/
    1.26 +	{ SDLK_LCTRL,			0,	0x4c },	// Left Ctrl
    1.27 +	{ SDLK_RCTRL,			0,	0x4d },	// Right Ctrl
    1.28  // Keycodes 4E thru 5A not used
    1.29  	{ SDLK_LEFTBRACKET,		0,	0x5b },	// [
    1.30  	{ SDLK_BACKSLASH,		0,	0x5c },	// \ (backslash)
    1.31 @@ -122,23 +125,79 @@
    1.32  	{ SDLK_y,				0,	0x79 },	// Y
    1.33  	{ SDLK_z,				0,	0x7a },	// Z
    1.34  // Keycodes 7B, 7C, 7D not used
    1.35 -	{ SDLK_NUMLOCK,			0,	0x7e }		// Numlock
    1.36 +	{ SDLK_NUMLOCK,			0,	0x7e }	// Numlock
    1.37  //	{ SDLK_,				1,	0x7f },	// Dlete
    1.38  };
    1.39  
    1.40  /**
    1.41 - * List of key states
    1.42 + * List of special key codes
    1.43 + */
    1.44 +enum {
    1.45 +	KEY_ALL_UP				= 0x40,		///< All keys up
    1.46 +	KEY_LIST_END			= 0x80,		///< End of key code list
    1.47 +	KEY_BEGIN_MOUSE			= 0xCF,		///< Mouse data follows
    1.48 +	KEY_BEGIN_KEYBOARD		= 0xDF,		///< Keyboard data follows
    1.49 +};
    1.50 +
    1.51 +/**
    1.52 + * List of keyboard commands
    1.53   */
    1.54 -int keystate[0x80];
    1.55 +enum {
    1.56 +	KEY_CMD_RESET			= 0x92,		///< Reset keyboard
    1.57 +	KEY_CMD_CAPSLED_OFF		= 0xB1,		///< Caps Lock LED off--CHECK!
    1.58 +	KEY_CMD_CAPSLED_ON		= 0xB0,		///< Caps Lock LED on --CHECK!
    1.59 +	KEY_CMD_NUMLED_OFF		= 0xA1,		///< Num Lock LED off --CHECK!
    1.60 +	KEY_CMD_NUMLED_ON		= 0xA0,		///< Num Lock LED on  --CHECK!
    1.61 +	KEY_CMD_MOUSE_ENABLE	= 0xD0,		///< Enable mouse
    1.62 +	KEY_CMD_MOUSE_DISABLE	= 0xD1		///< Disable mouse
    1.63 +};
    1.64  
    1.65 -void keyboard_init(void)
    1.66 +void keyboard_init(KEYBOARD_STATE *state)
    1.67  {
    1.68  	// Set all key states to "not pressed"
    1.69 -	for (int i=0; i<(sizeof(keystate)/sizeof(keystate[0])); i++) {
    1.70 -		keystate[i] = 0;
    1.71 +	for (int i=0; i<(sizeof(state->keystate)/sizeof(state->keystate[0])); i++) {
    1.72 +		state->keystate[i] = 0;
    1.73  	}
    1.74  }
    1.75  
    1.76 -void keyboard_event(SDL_Event *ev)
    1.77 +void keyboard_event(KEYBOARD_STATE *state, SDL_Event *ev)
    1.78  {
    1.79 +	int ks;
    1.80 +
    1.81 +	// Figure out the event type
    1.82 +	if (ev->type == SDL_KEYDOWN) {
    1.83 +		// Key down event
    1.84 +		ks = 1;
    1.85 +	} else if (ev->type == SDL_KEYUP) {
    1.86 +		// Key up event
    1.87 +		ks = 0;
    1.88 +	} else {
    1.89 +		// Not a keyboard event
    1.90 +		return;
    1.91 +	}
    1.92 +
    1.93 +	// Loop over the keyinfo, try and find a match for this key
    1.94 +	// TODO: handle Extended Keymap
    1.95 +	for (int i=0; i<(sizeof(keymap)/sizeof(keymap[0])); i++) {
    1.96 +		if (ev->key.keysym.sym == keymap[i].key) {
    1.97 +			// Key code match. Is this an Extended Map key?
    1.98 +			if (keymap[i].extended) {
    1.99 +				// Key is on the Extended map. Need ALT set when pressing the key.
   1.100 +				if (ev->key.keysym.mod & KMOD_ALT) {
   1.101 +					// ALT is down, key matches.
   1.102 +					state->keystate[keymap[i].scancode] = ks;
   1.103 +					break;
   1.104 +				}
   1.105 +			} else {
   1.106 +				// Key is on the Standard map. ALT must NOT be set when pressing the key.
   1.107 +				if (!(ev->key.keysym.mod & KMOD_ALT)) {
   1.108 +					// ALT is up, key matches
   1.109 +					state->keystate[keymap[i].scancode] = ks;
   1.110 +					break;
   1.111 +				}
   1.112 +			}
   1.113 +		}
   1.114 +	}
   1.115  }
   1.116 +
   1.117 +// TODO: register read and write