src/keyboard.c

Thu, 30 Dec 2010 00:37:03 +0000

author
Philip Pemberton <philpem@philpem.me.uk>
date
Thu, 30 Dec 2010 00:37:03 +0000
changeset 86
ebce87d87808
parent 74
51cbc7a44cd9
child 90
934ae2efdd01
permissions
-rw-r--r--

add keyboard event handler; need to do register r/w next.

     1 #include "SDL.h"
     2 #include "keyboard.h"
     4 /**
     5  * Key map -- a mapping from SDLK_xxx constants to scancodes and vice versa.
     6  */
     7 struct {
     8 	SDLKey			key;			///< SDLK_xxx key code constant
     9 	int				extended;		///< 1 if this is an extended keycode
    10 	unsigned char	scancode;		///< Keyboard scan code
    11 } keymap[] = {
    12 	{ SDLK_UP,				0,	0x01 },	// ROLL/Up		[UpArrow]
    13 	{ SDLK_KP2,				0,	0x01 },	// ROLL/Up		[Keypad 2]
    14 //	{ SDLK_,				1,	0x02 },	// Clear Line
    15 //	{ SDLK_,				1,	0x03 },	// Rstrt / Ref
    16 //	{ SDLK_,				1,	0x04 },	// Exit
    17 	{ SDLK_KP1,				0,	0x05 },	// PREV			[Keypad 1]
    18 //	{ SDLK_,				1,	0x06 },	// Msg
    19 //	{ SDLK_,				1,	0x07 },	// Cancl
    20 	{ SDLK_BACKSPACE,		0,	0x08 },	// Backspace
    21 	{ SDLK_TAB,				0,	0x09 },	// Tab
    22 //	{ SDLK_RETURN,			1,	0x0a },	// ENTER
    23 	{ SDLK_DOWN,			0,	0x0b },	// ROLL/Down	[DownArrow]
    24 	{ SDLK_KP0,				0,	0x0b },	// ROLL/Down	[Keypad 0]
    25 	{ SDLK_KP3,				0,	0x0c },	// NEXT			[Keypad 3]
    26 	{ SDLK_RETURN,			0,	0x0d },	// RETURN		[Return]
    27 	{ SDLK_LEFT,			0,	0x0e },	// <--			[LeftArrow]
    28 	{ SDLK_KP_MINUS,		0,	0x0e },	// <--			[Keypad -]
    29 	{ SDLK_RIGHT,			0,	0x0f },	// -->			[RightArrow]
    30 	{ SDLK_KP_PERIOD,		0,	0x0f },	// -->			[Keypad .]
    31 //	{ SDLK_,				1,	0x10 },	// Creat
    32 //	{ SDLK_,				1,	0x11 },	// Save
    33 //	{ SDLK_,				1,	0x12 },	// Move
    34 //	{ SDLK_,				1,	0x13 },	// Ops
    35 //	{ SDLK_,				1,	0x14 },	// Copy
    36 	{ SDLK_F1,				0,	0x15 },	// F1
    37 	{ SDLK_F2,				0,	0x16 },	// F2
    38 	{ SDLK_F3,				0,	0x17 },	// F3
    39 	{ SDLK_F4,				0,	0x18 },	// F4
    40 	{ SDLK_F5,				0,	0x19 },	// F5
    41 	{ SDLK_F6,				0,	0x1a },	// F6
    42 	{ SDLK_ESCAPE,			0,	0x1b },	// ESC/DEL		[Escape]
    43 	{ SDLK_F7,				0,	0x1c },	// F7
    44 	{ SDLK_F8,				0,	0x1d },	// F8
    45 //	{ SDLK_,				1,	0x1e },	// Suspd
    46 //	{ SDLK_,				1,	0x1f },	// Rsume
    47 	{ SDLK_SPACE,			0,	0x20 },	// SPACE		[Spacebar]
    48 //	{ SDLK_,				1,	0x21 },	// Undo
    49 //	{ SDLK_,				1,	0x22 },	// Redo
    50 //	{ SDLK_,				1,	0x23 },	// FIND
    51 //	{ SDLK_,				1,	0x24 },	// RPLAC
    52 	{ SDLK_BREAK,			0,	0x25 },	// RESET/BREAK	[Pause/Break]
    53 //	{ SDLK_,				1,	0x26 },	// DleteChar
    54 	{ SDLK_QUOTE,			0,	0x27 },	// ' (single-quote)
    55 //	{ SDLK_,				1,	0x28 },	// SLCT/MARK
    56 //	{ SDLK_,				1,	0x29 },	// INPUT/MODE
    57 //	{ SDLK_,				1,	0x2a },	// HELP
    58 //	Keycode 2B not used
    59 	{ SDLK_COMMA,			0,	0x2c },	// ,			[Comma]
    60 	{ SDLK_MINUS,			0,	0x2d },	// -			[Dash]
    61 	{ SDLK_PERIOD,			0,	0x2e },	// .			[Period]
    62 	{ SDLK_SLASH,			0,	0x2f },	// /			[Forward-slash]
    63 	{ SDLK_0,				0,	0x30 },	// 0
    64 	{ SDLK_1,				0,	0x31 },	// 1
    65 	{ SDLK_2,				0,	0x32 },	// 2
    66 	{ SDLK_3,				0,	0x33 },	// 3
    67 	{ SDLK_4,				0,	0x34 },	// 4
    68 	{ SDLK_5,				0,	0x35 },	// 5
    69 	{ SDLK_6,				0,	0x36 },	// 6
    70 	{ SDLK_7,				0,	0x37 },	// 7
    71 	{ SDLK_8,				0,	0x38 },	// 8
    72 	{ SDLK_9,				0,	0x39 },	// 9
    73 // Keycode 3A not used
    74 	{ SDLK_SEMICOLON,		0,	0x3b },	// ;			[Semicolon]
    75 // Keycode 3C not used
    76 	{ SDLK_EQUALS,			0,	0x3d },	// =			[Equals]
    77 // Keycode 3E not used
    78 // Keycode 3F = BOGUS (Bad Keycode)
    79 // Keycode 40 = All Up
    80 //	{ SDLK_,				1,	0x41 },	// CMD
    81 //	{ SDLK_,				1,	0x42 },	// CLOSE/OPEN
    82 	{ SDLK_KP7,				0,	0x43 },	// PRINT
    83 	{ SDLK_KP8,				0,	0x44 },	// CLEAR/RFRSH
    84 	{ SDLK_CAPSLOCK,		0,	0x45 },	// Caps Lock
    85 	{ SDLK_KP9,				0,	0x46 },	// PAGE
    86 	{ SDLK_KP4,				0,	0x47 },	// BEG
    87 	{ SDLK_LSHIFT,			0,	0x48 },	// Left Shift
    88 	{ SDLK_RSHIFT,			0,	0x49 },	// Right Shift
    89 	{ SDLK_HOME,			0,	0x4a },	// Home
    90 	{ SDLK_KP5,				0,	0x4a },	// Home			[Keypad 5]
    91 	{ SDLK_END,				0,	0x4b },	// End
    92 	{ SDLK_KP6,				0,	0x4b },	// End			[Keypad 6]
    93 	{ SDLK_LCTRL,			0,	0x4c },	// Left Ctrl
    94 	{ SDLK_RCTRL,			0,	0x4d },	// Right Ctrl
    95 // Keycodes 4E thru 5A not used
    96 	{ SDLK_LEFTBRACKET,		0,	0x5b },	// [
    97 	{ SDLK_BACKSLASH,		0,	0x5c },	// \ (backslash)
    98 	{ SDLK_RIGHTBRACKET,	0,	0x5d },	// ]
    99 // Keycodes 5E, 5F not used
   100 	{ SDLK_BACKQUOTE,		0,	0x60 },	// `
   101 	{ SDLK_a,				0,	0x61 },	// A
   102 	{ SDLK_b,				0,	0x62 },	// B
   103 	{ SDLK_c,				0,	0x63 },	// C
   104 	{ SDLK_d,				0,	0x64 },	// D
   105 	{ SDLK_e,				0,	0x65 },	// E
   106 	{ SDLK_f,				0,	0x66 },	// F
   107 	{ SDLK_g,				0,	0x67 },	// G
   108 	{ SDLK_h,				0,	0x68 },	// H
   109 	{ SDLK_i,				0,	0x69 },	// I
   110 	{ SDLK_j,				0,	0x6a },	// J
   111 	{ SDLK_k,				0,	0x6b },	// K
   112 	{ SDLK_l,				0,	0x6c },	// L
   113 	{ SDLK_m,				0,	0x6d },	// M
   114 	{ SDLK_n,				0,	0x6e },	// N
   115 	{ SDLK_o,				0,	0x6f },	// O
   116 	{ SDLK_p,				0,	0x70 },	// P
   117 	{ SDLK_q,				0,	0x71 },	// Q
   118 	{ SDLK_r,				0,	0x72 },	// R
   119 	{ SDLK_s,				0,	0x73 },	// S
   120 	{ SDLK_t,				0,	0x74 },	// T
   121 	{ SDLK_u,				0,	0x75 },	// U
   122 	{ SDLK_v,				0,	0x76 },	// V
   123 	{ SDLK_w,				0,	0x77 },	// W
   124 	{ SDLK_x,				0,	0x78 },	// X
   125 	{ SDLK_y,				0,	0x79 },	// Y
   126 	{ SDLK_z,				0,	0x7a },	// Z
   127 // Keycodes 7B, 7C, 7D not used
   128 	{ SDLK_NUMLOCK,			0,	0x7e }	// Numlock
   129 //	{ SDLK_,				1,	0x7f },	// Dlete
   130 };
   132 /**
   133  * List of special key codes
   134  */
   135 enum {
   136 	KEY_ALL_UP				= 0x40,		///< All keys up
   137 	KEY_LIST_END			= 0x80,		///< End of key code list
   138 	KEY_BEGIN_MOUSE			= 0xCF,		///< Mouse data follows
   139 	KEY_BEGIN_KEYBOARD		= 0xDF,		///< Keyboard data follows
   140 };
   142 /**
   143  * List of keyboard commands
   144  */
   145 enum {
   146 	KEY_CMD_RESET			= 0x92,		///< Reset keyboard
   147 	KEY_CMD_CAPSLED_OFF		= 0xB1,		///< Caps Lock LED off--CHECK!
   148 	KEY_CMD_CAPSLED_ON		= 0xB0,		///< Caps Lock LED on --CHECK!
   149 	KEY_CMD_NUMLED_OFF		= 0xA1,		///< Num Lock LED off --CHECK!
   150 	KEY_CMD_NUMLED_ON		= 0xA0,		///< Num Lock LED on  --CHECK!
   151 	KEY_CMD_MOUSE_ENABLE	= 0xD0,		///< Enable mouse
   152 	KEY_CMD_MOUSE_DISABLE	= 0xD1		///< Disable mouse
   153 };
   155 void keyboard_init(KEYBOARD_STATE *state)
   156 {
   157 	// Set all key states to "not pressed"
   158 	for (int i=0; i<(sizeof(state->keystate)/sizeof(state->keystate[0])); i++) {
   159 		state->keystate[i] = 0;
   160 	}
   161 }
   163 void keyboard_event(KEYBOARD_STATE *state, SDL_Event *ev)
   164 {
   165 	int ks;
   167 	// Figure out the event type
   168 	if (ev->type == SDL_KEYDOWN) {
   169 		// Key down event
   170 		ks = 1;
   171 	} else if (ev->type == SDL_KEYUP) {
   172 		// Key up event
   173 		ks = 0;
   174 	} else {
   175 		// Not a keyboard event
   176 		return;
   177 	}
   179 	// Loop over the keyinfo, try and find a match for this key
   180 	// TODO: handle Extended Keymap
   181 	for (int i=0; i<(sizeof(keymap)/sizeof(keymap[0])); i++) {
   182 		if (ev->key.keysym.sym == keymap[i].key) {
   183 			// Key code match. Is this an Extended Map key?
   184 			if (keymap[i].extended) {
   185 				// Key is on the Extended map. Need ALT set when pressing the key.
   186 				if (ev->key.keysym.mod & KMOD_ALT) {
   187 					// ALT is down, key matches.
   188 					state->keystate[keymap[i].scancode] = ks;
   189 					break;
   190 				}
   191 			} else {
   192 				// Key is on the Standard map. ALT must NOT be set when pressing the key.
   193 				if (!(ev->key.keysym.mod & KMOD_ALT)) {
   194 					// ALT is up, key matches
   195 					state->keystate[keymap[i].scancode] = ks;
   196 					break;
   197 				}
   198 			}
   199 		}
   200 	}
   201 }
   203 // TODO: register read and write