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_DOWN] = "down",
127 [SDLK_INSERT] = "insert",
128 [SDLK_HOME] = "home",
130 [SDLK_PAGEUP] = "page up",
131 [SDLK_PAGEDOWN] = "page down",
149 [SDLK_NUMLOCK] = "numlock",
150 [SDLK_CAPSLOCK] = "caps lock",
151 [SDLK_SCROLLOCK] = "scroll lock",
152 [SDLK_RSHIFT] = "right shift",
153 [SDLK_LSHIFT] = "left shift",
154 [SDLK_RCTRL] = "right ctrl",
155 [SDLK_LCTRL] = "left ctrl",
156 [SDLK_RALT] = "right alt",
157 [SDLK_LALT] = "left alt",
158 [SDLK_RMETA] = "right meta",
159 [SDLK_LMETA] = "left meta",
160 [SDLK_LSUPER] = "left super", /* "Windows" keys */
161 [SDLK_RSUPER] = "right super",
162 [SDLK_MODE] = "alt gr",
163 [SDLK_COMPOSE] = "compose",
166 static void in_sdl_probe(void)
168 struct in_sdl_state *state;
173 state = calloc(1, sizeof(*state));
175 fprintf(stderr, "in_sdl: OOM\n");
179 in_register(IN_SDL_PREFIX "keys", -1, state, SDLK_LAST,
182 /* joysticks go here too */
183 SDL_InitSubSystem(SDL_INIT_JOYSTICK);
185 joycount = SDL_NumJoysticks();
186 for (i = 0; i < joycount; i++) {
187 joy = SDL_JoystickOpen(i);
191 state = calloc(1, sizeof(*state));
193 fprintf(stderr, "in_sdl: OOM\n");
199 snprintf(name, sizeof(name), IN_SDL_PREFIX "%s", SDL_JoystickName(i));
200 in_register(name, -1, state, SDLK_LAST, in_sdl_keys, 0);
204 SDL_JoystickEventState(SDL_ENABLE);
207 static void in_sdl_free(void *drv_data)
209 struct in_sdl_state *state = drv_data;
212 if (state->joy != NULL)
213 SDL_JoystickClose(state->joy);
218 static const char * const *
219 in_sdl_get_key_names(int *count)
225 /* could use SDL_GetKeyState, but this gives better packing */
226 static void update_keystate(keybits_t *keystate, int sym, int is_down)
228 keybits_t *ks_word, mask;
231 mask <<= sym & (KEYBITS_WORD_BITS - 1);
232 ks_word = keystate + sym / KEYBITS_WORD_BITS;
239 static int handle_event(struct in_sdl_state *state, SDL_Event *event,
240 int *kc_out, int *down_out)
242 if (event->type != SDL_KEYDOWN && event->type != SDL_KEYUP)
245 update_keystate(state->keystate, event->key.keysym.sym,
246 event->type == SDL_KEYDOWN);
248 *kc_out = event->key.keysym.sym;
249 if (down_out != NULL)
250 *down_out = event->type == SDL_KEYDOWN;
255 static int handle_joy_event(struct in_sdl_state *state, SDL_Event *event,
256 int *kc_out, int *down_out)
258 int kc = -1, down = 0, ret = 0;
260 /* FIXME: should ckeck .which */
261 /* TODO: remaining axis */
262 switch (event->type) {
263 case SDL_JOYAXISMOTION:
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 kc = (int)event->jbutton.button + SDLK_WORLD_0;
294 down = event->jbutton.state == SDL_PRESSED;
302 update_keystate(state->keystate, kc, down);
305 if (down_out != NULL)
311 #define JOY_EVENTS (SDL_JOYAXISMOTIONMASK | SDL_JOYBALLMOTIONMASK | SDL_JOYHATMOTIONMASK \
312 | SDL_JOYBUTTONDOWNMASK | SDL_JOYBUTTONUPMASK)
314 static int collect_events(struct in_sdl_state *state, int *one_kc, int *one_down)
317 Uint32 mask = state->joy ? JOY_EVENTS : (SDL_ALLEVENTS & ~JOY_EVENTS);
319 int i, ret, retval = 0;
321 maxcount = (one_kc != NULL) ? 1 : sizeof(events) / sizeof(events[0]);
325 count = SDL_PeepEvents(events, maxcount, SDL_GETEVENT, mask);
328 for (i = 0; i < count; i++) {
330 ret = handle_joy_event(state,
331 &events[i], one_kc, one_down);
333 ret = handle_event(state,
334 &events[i], one_kc, one_down);
336 if (ext_event_handler != NULL)
337 ext_event_handler(&events[i]);
342 if (one_kc != NULL && ret)
351 static int in_sdl_update(void *drv_data, const int *binds, int *result)
353 struct in_sdl_state *state = drv_data;
357 collect_events(state, NULL, NULL);
359 for (i = 0; i < SDLK_LAST / KEYBITS_WORD_BITS + 1; i++) {
360 mask = state->keystate[i];
363 for (bit = 0; mask != 0; bit++, mask >>= 1) {
366 sym = i * KEYBITS_WORD_BITS + bit;
368 for (b = 0; b < IN_BINDTYPE_COUNT; b++)
369 result[b] |= binds[IN_BIND_OFFS(sym, b)];
376 static int in_sdl_update_keycode(void *drv_data, int *is_down)
378 struct in_sdl_state *state = drv_data;
379 int ret_kc = -1, ret_down = 0;
381 collect_events(state, &ret_kc, &ret_down);
394 static const struct menu_keymap key_pbtn_map[] =
396 { SDLK_UP, PBTN_UP },
397 { SDLK_DOWN, PBTN_DOWN },
398 { SDLK_LEFT, PBTN_LEFT },
399 { SDLK_RIGHT, PBTN_RIGHT },
400 /* XXX: maybe better set this from it's plat code somehow */
401 { SDLK_RETURN, PBTN_MOK },
402 { SDLK_ESCAPE, PBTN_MBACK },
403 { SDLK_a, PBTN_MA2 },
404 { SDLK_s, PBTN_MA3 },
405 { SDLK_BACKSLASH, PBTN_MENU },
406 { SDLK_LEFTBRACKET, PBTN_L },
407 { SDLK_RIGHTBRACKET, PBTN_R },
409 #define KEY_PBTN_MAP_SIZE (sizeof(key_pbtn_map) / sizeof(key_pbtn_map[0]))
411 static const struct menu_keymap joybtn_pbtn_map[] =
413 { SDLK_UP, PBTN_UP },
414 { SDLK_DOWN, PBTN_DOWN },
415 { SDLK_LEFT, PBTN_LEFT },
416 { SDLK_RIGHT, PBTN_RIGHT },
418 { SDLK_WORLD_0, PBTN_MOK },
419 { SDLK_WORLD_1, PBTN_MBACK },
420 { SDLK_WORLD_2, PBTN_MA2 },
421 { SDLK_WORLD_3, PBTN_MA3 },
423 #define JOYBTN_PBTN_MAP_SIZE (sizeof(joybtn_pbtn_map) / sizeof(joybtn_pbtn_map[0]))
425 static int in_sdl_menu_translate(void *drv_data, int keycode, char *charcode)
427 struct in_sdl_state *state = drv_data;
428 const struct menu_keymap *map;
433 map = state->joy ? joybtn_pbtn_map : key_pbtn_map;
434 map_len = state->joy ? JOYBTN_PBTN_MAP_SIZE : KEY_PBTN_MAP_SIZE;
440 for (i = 0; i < map_len; i++)
441 if (map[i].pbtn == keycode)
446 for (i = 0; i < map_len; i++) {
447 if (map[i].key == keycode) {
453 if (charcode != NULL && (unsigned int)keycode < SDLK_LAST &&
454 in_sdl_keys[keycode] != NULL && in_sdl_keys[keycode][1] == 0)
457 *charcode = in_sdl_keys[keycode][0];
464 static const in_drv_t in_sdl_drv = {
465 .prefix = IN_SDL_PREFIX,
466 .probe = in_sdl_probe,
468 .get_key_names = in_sdl_get_key_names,
469 .update = in_sdl_update,
470 .update_keycode = in_sdl_update_keycode,
471 .menu_translate = in_sdl_menu_translate,
474 void in_sdl_init(const struct in_default_bind *defbinds,
475 void (*handler)(void *event))
477 in_register_driver(&in_sdl_drv, defbinds);
478 ext_event_handler = handler;