2 * (C) GraÅžvydas "notaz" Ignotas, 2012
4 * This work is licensed under the terms of any of these licenses
6 * - GNU GPL, version 2 or later.
7 * - GNU LGPL, version 2.1 or later.
9 * See the COPYING file in the top-level directory.
17 #define IN_SDL_PREFIX "sdl:"
18 /* should be machine word for best performace */
19 typedef unsigned long keybits_t;
20 #define KEYBITS_WORD_BITS (sizeof(keybits_t) * 8)
26 keybits_t keystate[SDLK_LAST / KEYBITS_WORD_BITS + 1];
29 static void (*ext_event_handler)(void *event);
31 static const char * const in_sdl_keys[SDLK_LAST] = {
32 [SDLK_BACKSPACE] = "backspace",
34 [SDLK_CLEAR] = "clear",
35 [SDLK_RETURN] = "return",
36 [SDLK_PAUSE] = "pause",
37 [SDLK_ESCAPE] = "escape",
38 [SDLK_SPACE] = "space",
40 [SDLK_QUOTEDBL] = "\"",
43 [SDLK_AMPERSAND] = "&",
45 [SDLK_LEFTPAREN] = "(",
46 [SDLK_RIGHTPAREN] = ")",
47 [SDLK_ASTERISK] = "*",
64 [SDLK_SEMICOLON] = ";",
68 [SDLK_QUESTION] = "?",
70 [SDLK_LEFTBRACKET] = "[",
71 [SDLK_BACKSLASH] = "\\",
72 [SDLK_RIGHTBRACKET] = "]",
74 [SDLK_UNDERSCORE] = "_",
75 [SDLK_BACKQUOTE] = "`",
102 [SDLK_DELETE] = "delete",
114 [SDLK_KP_PERIOD] = "[.]",
115 [SDLK_KP_DIVIDE] = "[/]",
116 [SDLK_KP_MULTIPLY] = "[*]",
117 [SDLK_KP_MINUS] = "[-]",
118 [SDLK_KP_PLUS] = "[+]",
119 [SDLK_KP_ENTER] = "enter",
120 [SDLK_KP_EQUALS] = "equals",
123 [SDLK_DOWN] = "down",
124 [SDLK_RIGHT] = "right",
125 [SDLK_LEFT] = "left",
126 [SDLK_INSERT] = "insert",
127 [SDLK_HOME] = "home",
129 [SDLK_PAGEUP] = "page up",
130 [SDLK_PAGEDOWN] = "page down",
148 [SDLK_NUMLOCK] = "numlock",
149 [SDLK_CAPSLOCK] = "caps lock",
150 [SDLK_SCROLLOCK] = "scroll lock",
151 [SDLK_RSHIFT] = "right shift",
152 [SDLK_LSHIFT] = "left shift",
153 [SDLK_RCTRL] = "right ctrl",
154 [SDLK_LCTRL] = "left ctrl",
155 [SDLK_RALT] = "right alt",
156 [SDLK_LALT] = "left alt",
157 [SDLK_RMETA] = "right meta",
158 [SDLK_LMETA] = "left meta",
159 [SDLK_LSUPER] = "left super", /* "Windows" keys */
160 [SDLK_RSUPER] = "right super",
161 [SDLK_MODE] = "alt gr",
162 [SDLK_COMPOSE] = "compose",
165 static void in_sdl_probe(void)
167 struct in_sdl_state *state;
172 state = calloc(1, sizeof(*state));
174 fprintf(stderr, "in_sdl: OOM\n");
178 in_register(IN_SDL_PREFIX "keys", -1, state, SDLK_LAST,
181 /* joysticks go here too */
182 SDL_InitSubSystem(SDL_INIT_JOYSTICK);
184 joycount = SDL_NumJoysticks();
185 for (i = 0; i < joycount; i++) {
186 joy = SDL_JoystickOpen(i);
190 state = calloc(1, sizeof(*state));
192 fprintf(stderr, "in_sdl: OOM\n");
198 snprintf(name, sizeof(name), IN_SDL_PREFIX "%s", SDL_JoystickName(i));
199 in_register(name, -1, state, SDLK_LAST, in_sdl_keys, 0);
203 SDL_JoystickEventState(SDL_ENABLE);
206 static void in_sdl_free(void *drv_data)
208 struct in_sdl_state *state = drv_data;
211 if (state->joy != NULL)
212 SDL_JoystickClose(state->joy);
217 static const char * const *
218 in_sdl_get_key_names(int *count)
224 /* could use SDL_GetKeyState, but this gives better packing */
225 static void update_keystate(keybits_t *keystate, int sym, int is_down)
227 keybits_t *ks_word, mask;
230 mask <<= sym & (KEYBITS_WORD_BITS - 1);
231 ks_word = keystate + sym / KEYBITS_WORD_BITS;
238 static int handle_event(struct in_sdl_state *state, SDL_Event *event,
239 int *kc_out, int *down_out)
241 if (event->type != SDL_KEYDOWN && event->type != SDL_KEYUP)
244 update_keystate(state->keystate, event->key.keysym.sym,
245 event->type == SDL_KEYDOWN);
247 *kc_out = event->key.keysym.sym;
248 if (down_out != NULL)
249 *down_out = event->type == SDL_KEYDOWN;
254 static int handle_joy_event(struct in_sdl_state *state, SDL_Event *event,
255 int *kc_out, int *down_out)
257 int kc = -1, down = 0, ret = 0;
259 /* TODO: remaining axis */
260 switch (event->type) {
261 case SDL_JOYAXISMOTION:
262 if (event->jaxis.which != state->joy_id)
264 if (event->jaxis.axis > 1)
266 if (-16384 <= event->jaxis.value && event->jaxis.value <= 16384) {
267 kc = state->axis_keydown[event->jaxis.axis];
268 state->axis_keydown[event->jaxis.axis] = 0;
271 else if (event->jaxis.value < -16384) {
272 kc = state->axis_keydown[event->jaxis.axis];
274 update_keystate(state->keystate, kc, 0);
275 kc = event->jaxis.axis ? SDLK_UP : SDLK_LEFT;
276 state->axis_keydown[event->jaxis.axis] = kc;
280 else if (event->jaxis.value > 16384) {
281 kc = state->axis_keydown[event->jaxis.axis];
283 update_keystate(state->keystate, kc, 0);
284 kc = event->jaxis.axis ? SDLK_DOWN : SDLK_RIGHT;
285 state->axis_keydown[event->jaxis.axis] = kc;
291 case SDL_JOYBUTTONDOWN:
292 case SDL_JOYBUTTONUP:
293 if (event->jbutton.which != state->joy_id)
295 kc = (int)event->jbutton.button + SDLK_WORLD_0;
296 down = event->jbutton.state == SDL_PRESSED;
304 update_keystate(state->keystate, kc, down);
307 if (down_out != NULL)
313 #define JOY_EVENTS (SDL_JOYAXISMOTIONMASK | SDL_JOYBALLMOTIONMASK | SDL_JOYHATMOTIONMASK \
314 | SDL_JOYBUTTONDOWNMASK | SDL_JOYBUTTONUPMASK)
316 static int collect_events(struct in_sdl_state *state, int *one_kc, int *one_down)
319 Uint32 mask = state->joy ? JOY_EVENTS : (SDL_ALLEVENTS & ~JOY_EVENTS);
321 int i, ret, retval = 0;
322 int num_events, num_peeped_events;
325 maxcount = (one_kc != NULL) ? 1 : sizeof(events) / sizeof(events[0]);
329 num_events = SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, mask);
331 for (num_peeped_events = 0; num_peeped_events < num_events; num_peeped_events += count) {
332 count = SDL_PeepEvents(events, maxcount, SDL_GETEVENT, mask);
335 for (i = 0; i < count; i++) {
338 ret = handle_joy_event(state,
339 event, one_kc, one_down);
341 ret = handle_event(state,
342 event, one_kc, one_down);
346 SDL_PushEvent(event);
349 if (ext_event_handler != NULL)
350 ext_event_handler(event);
357 if (one_kc != NULL && ret)
359 // don't lose events other devices might want to handle
360 for (i++; i < count; i++)
361 SDL_PushEvent(&events[i]);
371 static int in_sdl_update(void *drv_data, const int *binds, int *result)
373 struct in_sdl_state *state = drv_data;
377 collect_events(state, NULL, NULL);
379 for (i = 0; i < SDLK_LAST / KEYBITS_WORD_BITS + 1; i++) {
380 mask = state->keystate[i];
383 for (bit = 0; mask != 0; bit++, mask >>= 1) {
386 sym = i * KEYBITS_WORD_BITS + bit;
388 for (b = 0; b < IN_BINDTYPE_COUNT; b++)
389 result[b] |= binds[IN_BIND_OFFS(sym, b)];
396 static int in_sdl_update_keycode(void *drv_data, int *is_down)
398 struct in_sdl_state *state = drv_data;
399 int ret_kc = -1, ret_down = 0;
401 collect_events(state, &ret_kc, &ret_down);
414 static const struct menu_keymap key_pbtn_map[] =
416 { SDLK_UP, PBTN_UP },
417 { SDLK_DOWN, PBTN_DOWN },
418 { SDLK_LEFT, PBTN_LEFT },
419 { SDLK_RIGHT, PBTN_RIGHT },
420 /* XXX: maybe better set this from it's plat code somehow */
421 { SDLK_RETURN, PBTN_MOK },
422 { SDLK_ESCAPE, PBTN_MBACK },
423 { SDLK_SEMICOLON, PBTN_MA2 },
424 { SDLK_QUOTE, PBTN_MA3 },
425 { SDLK_BACKSLASH, PBTN_MENU },
426 { SDLK_LEFTBRACKET, PBTN_L },
427 { SDLK_RIGHTBRACKET, PBTN_R },
429 #define KEY_PBTN_MAP_SIZE (sizeof(key_pbtn_map) / sizeof(key_pbtn_map[0]))
431 static const struct menu_keymap joybtn_pbtn_map[] =
433 { SDLK_UP, PBTN_UP },
434 { SDLK_DOWN, PBTN_DOWN },
435 { SDLK_LEFT, PBTN_LEFT },
436 { SDLK_RIGHT, PBTN_RIGHT },
438 { SDLK_WORLD_0, PBTN_MOK },
439 { SDLK_WORLD_1, PBTN_MBACK },
440 { SDLK_WORLD_2, PBTN_MA2 },
441 { SDLK_WORLD_3, PBTN_MA3 },
443 #define JOYBTN_PBTN_MAP_SIZE (sizeof(joybtn_pbtn_map) / sizeof(joybtn_pbtn_map[0]))
445 static int in_sdl_menu_translate(void *drv_data, int keycode, char *charcode)
447 struct in_sdl_state *state = drv_data;
448 const struct menu_keymap *map;
453 map = state->joy ? joybtn_pbtn_map : key_pbtn_map;
454 map_len = state->joy ? JOYBTN_PBTN_MAP_SIZE : KEY_PBTN_MAP_SIZE;
460 for (i = 0; i < map_len; i++)
461 if (map[i].pbtn == keycode)
466 for (i = 0; i < map_len; i++) {
467 if (map[i].key == keycode) {
473 if (charcode != NULL && (unsigned int)keycode < SDLK_LAST &&
474 in_sdl_keys[keycode] != NULL && in_sdl_keys[keycode][1] == 0)
477 *charcode = in_sdl_keys[keycode][0];
484 static const in_drv_t in_sdl_drv = {
485 .prefix = IN_SDL_PREFIX,
486 .probe = in_sdl_probe,
488 .get_key_names = in_sdl_get_key_names,
489 .update = in_sdl_update,
490 .update_keycode = in_sdl_update_keycode,
491 .menu_translate = in_sdl_menu_translate,
494 void in_sdl_init(const struct in_default_bind *defbinds,
495 void (*handler)(void *event))
497 in_register_driver(&in_sdl_drv, defbinds);
498 ext_event_handler = handler;