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.
8 * See the COPYING file in the top-level directory.
16 #define IN_SDL_PREFIX "sdl:"
17 /* should be machine word for best performace */
18 typedef unsigned long keybits_t;
19 #define KEYBITS_WORD_BITS (sizeof(keybits_t) * 8)
25 keybits_t keystate[SDLK_LAST / KEYBITS_WORD_BITS + 1];
28 static const char * const in_sdl_keys[SDLK_LAST] = {
29 [SDLK_BACKSPACE] = "backspace",
31 [SDLK_CLEAR] = "clear",
32 [SDLK_RETURN] = "return",
33 [SDLK_PAUSE] = "pause",
34 [SDLK_ESCAPE] = "escape",
35 [SDLK_SPACE] = "space",
37 [SDLK_QUOTEDBL] = "\"",
40 [SDLK_AMPERSAND] = "&",
42 [SDLK_LEFTPAREN] = "(",
43 [SDLK_RIGHTPAREN] = ")",
44 [SDLK_ASTERISK] = "*",
61 [SDLK_SEMICOLON] = ",",
65 [SDLK_QUESTION] = "?",
67 [SDLK_LEFTBRACKET] = "[",
68 [SDLK_BACKSLASH] = "\\",
69 [SDLK_RIGHTBRACKET] = "]",
71 [SDLK_UNDERSCORE] = "_",
72 [SDLK_BACKQUOTE] = "`",
99 [SDLK_DELETE] = "delete",
111 [SDLK_KP_PERIOD] = "[.]",
112 [SDLK_KP_DIVIDE] = "[/]",
113 [SDLK_KP_MULTIPLY] = "[*]",
114 [SDLK_KP_MINUS] = "[-]",
115 [SDLK_KP_PLUS] = "[+]",
116 [SDLK_KP_ENTER] = "enter",
117 [SDLK_KP_EQUALS] = "equals",
120 [SDLK_DOWN] = "down",
121 [SDLK_RIGHT] = "right",
122 [SDLK_LEFT] = "left",
123 [SDLK_DOWN] = "down",
124 [SDLK_INSERT] = "insert",
125 [SDLK_HOME] = "home",
127 [SDLK_PAGEUP] = "page up",
128 [SDLK_PAGEDOWN] = "page down",
146 [SDLK_NUMLOCK] = "numlock",
147 [SDLK_CAPSLOCK] = "caps lock",
148 [SDLK_SCROLLOCK] = "scroll lock",
149 [SDLK_RSHIFT] = "right shift",
150 [SDLK_LSHIFT] = "left shift",
151 [SDLK_RCTRL] = "right ctrl",
152 [SDLK_LCTRL] = "left ctrl",
153 [SDLK_RALT] = "right alt",
154 [SDLK_LALT] = "left alt",
155 [SDLK_RMETA] = "right meta",
156 [SDLK_LMETA] = "left meta",
157 [SDLK_LSUPER] = "left super", /* "Windows" keys */
158 [SDLK_RSUPER] = "right super",
159 [SDLK_MODE] = "alt gr",
160 [SDLK_COMPOSE] = "compose",
163 static void in_sdl_probe(void)
165 struct in_sdl_state *state;
170 state = calloc(1, sizeof(*state));
172 fprintf(stderr, "in_sdl: OOM\n");
176 in_register(IN_SDL_PREFIX "keys", -1, state, SDLK_LAST,
179 /* joysticks go here too */
180 SDL_InitSubSystem(SDL_INIT_JOYSTICK);
182 joycount = SDL_NumJoysticks();
183 for (i = 0; i < joycount; i++) {
184 joy = SDL_JoystickOpen(i);
188 state = calloc(1, sizeof(*state));
190 fprintf(stderr, "in_sdl: OOM\n");
196 snprintf(name, sizeof(name), IN_SDL_PREFIX "%s", SDL_JoystickName(i));
197 in_register(name, -1, state, SDLK_LAST, in_sdl_keys, 0);
201 SDL_JoystickEventState(SDL_ENABLE);
204 static void in_sdl_free(void *drv_data)
206 struct in_sdl_state *state = drv_data;
209 if (state->joy != NULL)
210 SDL_JoystickClose(state->joy);
215 static const char * const *
216 in_sdl_get_key_names(int *count)
222 /* could use SDL_GetKeyState, but this gives better packing */
223 static void update_keystate(keybits_t *keystate, int sym, int is_down)
225 keybits_t *ks_word, mask;
228 mask <<= sym & (KEYBITS_WORD_BITS - 1);
229 ks_word = keystate + sym / KEYBITS_WORD_BITS;
236 static int handle_event(struct in_sdl_state *state, SDL_Event *event,
237 int *kc_out, int *down_out)
239 if (event->type != SDL_KEYDOWN && event->type != SDL_KEYUP)
242 update_keystate(state->keystate, event->key.keysym.sym,
243 event->type == SDL_KEYDOWN);
245 *kc_out = event->key.keysym.sym;
246 if (down_out != NULL)
247 *down_out = event->type == SDL_KEYDOWN;
252 static int handle_joy_event(struct in_sdl_state *state, SDL_Event *event,
253 int *kc_out, int *down_out)
255 int kc = -1, down = 0, ret = 0;
257 /* FIXME: should ckeck .which */
258 /* TODO: remaining axis */
259 switch (event->type) {
260 case SDL_JOYAXISMOTION:
261 if (event->jaxis.axis > 1)
263 if (-16384 <= event->jaxis.value && event->jaxis.value <= 16384) {
264 kc = state->axis_keydown[event->jaxis.axis];
265 state->axis_keydown[event->jaxis.axis] = 0;
268 else if (event->jaxis.value < -16384) {
269 kc = state->axis_keydown[event->jaxis.axis];
271 update_keystate(state->keystate, kc, 0);
272 kc = event->jaxis.axis ? SDLK_UP : SDLK_LEFT;
273 state->axis_keydown[event->jaxis.axis] = kc;
277 else if (event->jaxis.value > 16384) {
278 kc = state->axis_keydown[event->jaxis.axis];
280 update_keystate(state->keystate, kc, 0);
281 kc = event->jaxis.axis ? SDLK_DOWN : SDLK_RIGHT;
282 state->axis_keydown[event->jaxis.axis] = kc;
288 case SDL_JOYBUTTONDOWN:
289 case SDL_JOYBUTTONUP:
290 kc = (int)event->jbutton.button + SDLK_WORLD_0;
291 down = event->jbutton.state == SDL_PRESSED;
297 update_keystate(state->keystate, kc, down);
300 if (down_out != NULL)
306 #define JOY_EVENTS (SDL_JOYAXISMOTIONMASK | SDL_JOYBALLMOTIONMASK | SDL_JOYHATMOTIONMASK \
307 | SDL_JOYBUTTONDOWNMASK | SDL_JOYBUTTONUPMASK)
309 static int collect_events(struct in_sdl_state *state, int *one_kc, int *one_down)
312 Uint32 mask = state->joy ? JOY_EVENTS : (SDL_ALLEVENTS & ~JOY_EVENTS);
314 int i, ret, retval = 0;
316 maxcount = (one_kc != NULL) ? 1 : sizeof(events) / sizeof(events[0]);
320 count = SDL_PeepEvents(events, maxcount, SDL_GETEVENT, mask);
323 for (i = 0; i < count; i++) {
325 ret = handle_joy_event(state,
326 &events[i], one_kc, one_down);
328 ret = handle_event(state,
329 &events[i], one_kc, one_down);
331 if (one_kc != NULL && ret)
340 static int in_sdl_update(void *drv_data, const int *binds, int *result)
342 struct in_sdl_state *state = drv_data;
346 collect_events(state, NULL, NULL);
348 for (i = 0; i < SDLK_LAST / KEYBITS_WORD_BITS + 1; i++) {
349 mask = state->keystate[i];
352 for (bit = 0; mask != 0; bit++, mask >>= 1) {
355 sym = i * KEYBITS_WORD_BITS + bit;
357 for (b = 0; b < IN_BINDTYPE_COUNT; b++)
358 result[b] |= binds[IN_BIND_OFFS(sym, b)];
365 static int in_sdl_update_keycode(void *drv_data, int *is_down)
367 struct in_sdl_state *state = drv_data;
368 int ret_kc = -1, ret_down = 0;
370 collect_events(state, &ret_kc, &ret_down);
383 static const struct menu_keymap key_pbtn_map[] =
385 { SDLK_UP, PBTN_UP },
386 { SDLK_DOWN, PBTN_DOWN },
387 { SDLK_LEFT, PBTN_LEFT },
388 { SDLK_RIGHT, PBTN_RIGHT },
389 /* XXX: maybe better set this from it's plat code somehow */
390 { SDLK_RETURN, PBTN_MOK },
391 { SDLK_ESCAPE, PBTN_MBACK },
392 { SDLK_a, PBTN_MA2 },
393 { SDLK_s, PBTN_MA3 },
394 { SDLK_BACKSLASH, PBTN_MENU },
395 { SDLK_LEFTBRACKET, PBTN_L },
396 { SDLK_RIGHTBRACKET, PBTN_R },
398 #define KEY_PBTN_MAP_SIZE (sizeof(key_pbtn_map) / sizeof(key_pbtn_map[0]))
400 static const struct menu_keymap joybtn_pbtn_map[] =
402 { SDLK_UP, PBTN_UP },
403 { SDLK_DOWN, PBTN_DOWN },
404 { SDLK_LEFT, PBTN_LEFT },
405 { SDLK_RIGHT, PBTN_RIGHT },
407 { SDLK_WORLD_0, PBTN_MOK },
408 { SDLK_WORLD_1, PBTN_MBACK },
409 { SDLK_WORLD_2, PBTN_MA2 },
410 { SDLK_WORLD_3, PBTN_MA3 },
412 #define JOYBTN_PBTN_MAP_SIZE (sizeof(joybtn_pbtn_map) / sizeof(joybtn_pbtn_map[0]))
414 static int in_sdl_menu_translate(void *drv_data, int keycode, char *charcode)
416 struct in_sdl_state *state = drv_data;
417 const struct menu_keymap *map;
422 map = state->joy ? joybtn_pbtn_map : key_pbtn_map;
423 map_len = state->joy ? JOYBTN_PBTN_MAP_SIZE : KEY_PBTN_MAP_SIZE;
429 for (i = 0; i < map_len; i++)
430 if (map[i].pbtn == keycode)
435 for (i = 0; i < map_len; i++) {
436 if (map[i].key == keycode) {
442 if (charcode != NULL && (unsigned int)keycode < SDLK_LAST &&
443 in_sdl_keys[keycode] != NULL && in_sdl_keys[keycode][1] == 0)
446 *charcode = in_sdl_keys[keycode][0];
453 static const in_drv_t in_sdl_drv = {
454 .prefix = IN_SDL_PREFIX,
455 .probe = in_sdl_probe,
457 .get_key_names = in_sdl_get_key_names,
458 .update = in_sdl_update,
459 .update_keycode = in_sdl_update_keycode,
460 .menu_translate = in_sdl_menu_translate,
463 void in_sdl_init(const struct in_default_bind *defbinds)
465 in_register_driver(&in_sdl_drv, defbinds);