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)
27 keybits_t keystate[SDLK_LAST / KEYBITS_WORD_BITS + 1];
30 static void (*ext_event_handler)(void *event);
32 static const char * const in_sdl_keys[SDLK_LAST] = {
33 [SDLK_BACKSPACE] = "backspace",
35 [SDLK_CLEAR] = "clear",
36 [SDLK_RETURN] = "return",
37 [SDLK_PAUSE] = "pause",
38 [SDLK_ESCAPE] = "escape",
39 [SDLK_SPACE] = "space",
41 [SDLK_QUOTEDBL] = "\"",
44 [SDLK_AMPERSAND] = "&",
46 [SDLK_LEFTPAREN] = "(",
47 [SDLK_RIGHTPAREN] = ")",
48 [SDLK_ASTERISK] = "*",
65 [SDLK_SEMICOLON] = ";",
69 [SDLK_QUESTION] = "?",
71 [SDLK_LEFTBRACKET] = "[",
72 [SDLK_BACKSLASH] = "\\",
73 [SDLK_RIGHTBRACKET] = "]",
75 [SDLK_UNDERSCORE] = "_",
76 [SDLK_BACKQUOTE] = "`",
103 [SDLK_DELETE] = "delete",
115 [SDLK_KP_PERIOD] = "[.]",
116 [SDLK_KP_DIVIDE] = "[/]",
117 [SDLK_KP_MULTIPLY] = "[*]",
118 [SDLK_KP_MINUS] = "[-]",
119 [SDLK_KP_PLUS] = "[+]",
120 [SDLK_KP_ENTER] = "enter",
121 [SDLK_KP_EQUALS] = "equals",
124 [SDLK_DOWN] = "down",
125 [SDLK_RIGHT] = "right",
126 [SDLK_LEFT] = "left",
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(const in_drv_t *drv)
168 struct in_sdl_state *state;
173 state = calloc(1, sizeof(*state));
175 fprintf(stderr, "in_sdl: OOM\n");
180 in_register(IN_SDL_PREFIX "keys", -1, state, SDLK_LAST,
183 /* joysticks go here too */
184 SDL_InitSubSystem(SDL_INIT_JOYSTICK);
186 joycount = SDL_NumJoysticks();
187 for (i = 0; i < joycount; i++) {
188 joy = SDL_JoystickOpen(i);
192 state = calloc(1, sizeof(*state));
194 fprintf(stderr, "in_sdl: OOM\n");
201 snprintf(name, sizeof(name), IN_SDL_PREFIX "%s", SDL_JoystickName(i));
202 in_register(name, -1, state, SDLK_LAST, in_sdl_keys, 0);
206 SDL_JoystickEventState(SDL_ENABLE);
209 static void in_sdl_free(void *drv_data)
211 struct in_sdl_state *state = drv_data;
214 if (state->joy != NULL)
215 SDL_JoystickClose(state->joy);
220 static const char * const *
221 in_sdl_get_key_names(int *count)
227 /* could use SDL_GetKeyState, but this gives better packing */
228 static void update_keystate(keybits_t *keystate, int sym, int is_down)
230 keybits_t *ks_word, mask;
233 mask <<= sym & (KEYBITS_WORD_BITS - 1);
234 ks_word = keystate + sym / KEYBITS_WORD_BITS;
241 static int handle_event(struct in_sdl_state *state, SDL_Event *event,
242 int *kc_out, int *down_out)
244 if (event->type != SDL_KEYDOWN && event->type != SDL_KEYUP)
247 update_keystate(state->keystate, event->key.keysym.sym,
248 event->type == SDL_KEYDOWN);
250 *kc_out = event->key.keysym.sym;
251 if (down_out != NULL)
252 *down_out = event->type == SDL_KEYDOWN;
257 static int handle_joy_event(struct in_sdl_state *state, SDL_Event *event,
258 int *kc_out, int *down_out)
260 int kc = -1, down = 0, ret = 0;
262 /* TODO: remaining axis */
263 switch (event->type) {
264 case SDL_JOYAXISMOTION:
265 if (event->jaxis.which != state->joy_id)
267 if (event->jaxis.axis > 1)
269 if (-16384 <= event->jaxis.value && event->jaxis.value <= 16384) {
270 kc = state->axis_keydown[event->jaxis.axis];
271 state->axis_keydown[event->jaxis.axis] = 0;
274 else if (event->jaxis.value < -16384) {
275 kc = state->axis_keydown[event->jaxis.axis];
277 update_keystate(state->keystate, kc, 0);
278 kc = event->jaxis.axis ? SDLK_UP : SDLK_LEFT;
279 state->axis_keydown[event->jaxis.axis] = kc;
283 else if (event->jaxis.value > 16384) {
284 kc = state->axis_keydown[event->jaxis.axis];
286 update_keystate(state->keystate, kc, 0);
287 kc = event->jaxis.axis ? SDLK_DOWN : SDLK_RIGHT;
288 state->axis_keydown[event->jaxis.axis] = kc;
294 case SDL_JOYBUTTONDOWN:
295 case SDL_JOYBUTTONUP:
296 if (event->jbutton.which != state->joy_id)
298 kc = (int)event->jbutton.button + SDLK_WORLD_0;
299 down = event->jbutton.state == SDL_PRESSED;
307 update_keystate(state->keystate, kc, down);
310 if (down_out != NULL)
316 #define JOY_EVENTS (SDL_JOYAXISMOTIONMASK | SDL_JOYBALLMOTIONMASK | SDL_JOYHATMOTIONMASK \
317 | SDL_JOYBUTTONDOWNMASK | SDL_JOYBUTTONUPMASK)
319 static int collect_events(struct in_sdl_state *state, int *one_kc, int *one_down)
322 Uint32 mask = state->joy ? JOY_EVENTS : (SDL_ALLEVENTS & ~JOY_EVENTS);
324 int i, ret, retval = 0;
325 int num_events, num_peeped_events;
328 maxcount = (one_kc != NULL) ? 1 : sizeof(events) / sizeof(events[0]);
332 num_events = SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, mask);
334 for (num_peeped_events = 0; num_peeped_events < num_events; num_peeped_events += count) {
335 count = SDL_PeepEvents(events, maxcount, SDL_GETEVENT, mask);
338 for (i = 0; i < count; i++) {
341 ret = handle_joy_event(state,
342 event, one_kc, one_down);
344 ret = handle_event(state,
345 event, one_kc, one_down);
349 SDL_PushEvent(event);
352 if (ext_event_handler != NULL)
353 ext_event_handler(event);
360 if (one_kc != NULL && ret)
362 // don't lose events other devices might want to handle
363 for (i++; i < count; i++)
364 SDL_PushEvent(&events[i]);
374 static int in_sdl_update(void *drv_data, const int *binds, int *result)
376 struct in_sdl_state *state = drv_data;
380 collect_events(state, NULL, NULL);
382 for (i = 0; i < SDLK_LAST / KEYBITS_WORD_BITS + 1; i++) {
383 mask = state->keystate[i];
386 for (bit = 0; mask != 0; bit++, mask >>= 1) {
389 sym = i * KEYBITS_WORD_BITS + bit;
391 for (b = 0; b < IN_BINDTYPE_COUNT; b++)
392 result[b] |= binds[IN_BIND_OFFS(sym, b)];
399 static int in_sdl_update_keycode(void *drv_data, int *is_down)
401 struct in_sdl_state *state = drv_data;
402 int ret_kc = -1, ret_down = 0;
404 collect_events(state, &ret_kc, &ret_down);
412 static int in_sdl_menu_translate(void *drv_data, int keycode, char *charcode)
414 struct in_sdl_state *state = drv_data;
415 const struct in_pdata *pdata = state->drv->pdata;
416 const struct menu_keymap *map;
422 map = pdata->joy_map;
423 map_len = pdata->jmap_size;
425 map = pdata->key_map;
426 map_len = pdata->kmap_size;
433 for (i = 0; i < map_len; i++)
434 if (map[i].pbtn == keycode)
439 for (i = 0; i < map_len; i++) {
440 if (map[i].key == keycode) {
446 if (charcode != NULL && (unsigned int)keycode < SDLK_LAST &&
447 in_sdl_keys[keycode] != NULL && in_sdl_keys[keycode][1] == 0)
450 *charcode = in_sdl_keys[keycode][0];
457 static const in_drv_t in_sdl_drv = {
458 .prefix = IN_SDL_PREFIX,
459 .probe = in_sdl_probe,
461 .get_key_names = in_sdl_get_key_names,
462 .update = in_sdl_update,
463 .update_keycode = in_sdl_update_keycode,
464 .menu_translate = in_sdl_menu_translate,
467 int in_sdl_init(const struct in_pdata *pdata, void (*handler)(void *event))
470 fprintf(stderr, "in_sdl: Missing input platform data\n");
474 in_register_driver(&in_sdl_drv, pdata->defbinds, pdata);
475 ext_event_handler = handler;