af8d636cd7cf55713eb473340c09a8ba2f2968ca
[libpicofe.git] / in_sdl.c
1 /*
2  * (C) GraÅžvydas "notaz" Ignotas, 2012
3  *
4  * This work is licensed under the terms of any of these licenses
5  * (at your option):
6  *  - GNU GPL, version 2 or later.
7  *  - GNU LGPL, version 2.1 or later.
8  *  - MAME license.
9  * See the COPYING file in the top-level directory.
10  */
11
12 #include <stdio.h>
13 #include <SDL.h>
14 #include "input.h"
15 #include "in_sdl.h"
16
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)
21
22 struct in_sdl_state {
23         const in_drv_t *drv;
24         SDL_Joystick *joy;
25         int joy_id;
26         int axis_keydown[2];
27         keybits_t keystate[SDLK_LAST / KEYBITS_WORD_BITS + 1];
28 };
29
30 static void (*ext_event_handler)(void *event);
31
32 static const char * const in_sdl_keys[SDLK_LAST] = {
33         [SDLK_BACKSPACE] = "backspace",
34         [SDLK_TAB] = "tab",
35         [SDLK_CLEAR] = "clear",
36         [SDLK_RETURN] = "return",
37         [SDLK_PAUSE] = "pause",
38         [SDLK_ESCAPE] = "escape",
39         [SDLK_SPACE] = "space",
40         [SDLK_EXCLAIM]  = "!",
41         [SDLK_QUOTEDBL]  = "\"",
42         [SDLK_HASH]  = "#",
43         [SDLK_DOLLAR]  = "$",
44         [SDLK_AMPERSAND]  = "&",
45         [SDLK_QUOTE] = "'",
46         [SDLK_LEFTPAREN] = "(",
47         [SDLK_RIGHTPAREN] = ")",
48         [SDLK_ASTERISK] = "*",
49         [SDLK_PLUS] = "+",
50         [SDLK_COMMA] = ",",
51         [SDLK_MINUS] = "-",
52         [SDLK_PERIOD] = ".",
53         [SDLK_SLASH] = "/",
54         [SDLK_0] = "0",
55         [SDLK_1] = "1",
56         [SDLK_2] = "2",
57         [SDLK_3] = "3",
58         [SDLK_4] = "4",
59         [SDLK_5] = "5",
60         [SDLK_6] = "6",
61         [SDLK_7] = "7",
62         [SDLK_8] = "8",
63         [SDLK_9] = "9",
64         [SDLK_COLON] = ":",
65         [SDLK_SEMICOLON] = ";",
66         [SDLK_LESS] = "<",
67         [SDLK_EQUALS] = "=",
68         [SDLK_GREATER] = ">",
69         [SDLK_QUESTION] = "?",
70         [SDLK_AT] = "@",
71         [SDLK_LEFTBRACKET] = "[",
72         [SDLK_BACKSLASH] = "\\",
73         [SDLK_RIGHTBRACKET] = "]",
74         [SDLK_CARET] = "^",
75         [SDLK_UNDERSCORE] = "_",
76         [SDLK_BACKQUOTE] = "`",
77         [SDLK_a] = "a",
78         [SDLK_b] = "b",
79         [SDLK_c] = "c",
80         [SDLK_d] = "d",
81         [SDLK_e] = "e",
82         [SDLK_f] = "f",
83         [SDLK_g] = "g",
84         [SDLK_h] = "h",
85         [SDLK_i] = "i",
86         [SDLK_j] = "j",
87         [SDLK_k] = "k",
88         [SDLK_l] = "l",
89         [SDLK_m] = "m",
90         [SDLK_n] = "n",
91         [SDLK_o] = "o",
92         [SDLK_p] = "p",
93         [SDLK_q] = "q",
94         [SDLK_r] = "r",
95         [SDLK_s] = "s",
96         [SDLK_t] = "t",
97         [SDLK_u] = "u",
98         [SDLK_v] = "v",
99         [SDLK_w] = "w",
100         [SDLK_x] = "x",
101         [SDLK_y] = "y",
102         [SDLK_z] = "z",
103         [SDLK_DELETE] = "delete",
104
105         [SDLK_KP0] = "[0]",
106         [SDLK_KP1] = "[1]",
107         [SDLK_KP2] = "[2]",
108         [SDLK_KP3] = "[3]",
109         [SDLK_KP4] = "[4]",
110         [SDLK_KP5] = "[5]",
111         [SDLK_KP6] = "[6]",
112         [SDLK_KP7] = "[7]",
113         [SDLK_KP8] = "[8]",
114         [SDLK_KP9] = "[9]",
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",
122
123         [SDLK_UP] = "up",
124         [SDLK_DOWN] = "down",
125         [SDLK_RIGHT] = "right",
126         [SDLK_LEFT] = "left",
127         [SDLK_INSERT] = "insert",
128         [SDLK_HOME] = "home",
129         [SDLK_END] = "end",
130         [SDLK_PAGEUP] = "page up",
131         [SDLK_PAGEDOWN] = "page down",
132
133         [SDLK_F1] = "f1",
134         [SDLK_F2] = "f2",
135         [SDLK_F3] = "f3",
136         [SDLK_F4] = "f4",
137         [SDLK_F5] = "f5",
138         [SDLK_F6] = "f6",
139         [SDLK_F7] = "f7",
140         [SDLK_F8] = "f8",
141         [SDLK_F9] = "f9",
142         [SDLK_F10] = "f10",
143         [SDLK_F11] = "f11",
144         [SDLK_F12] = "f12",
145         [SDLK_F13] = "f13",
146         [SDLK_F14] = "f14",
147         [SDLK_F15] = "f15",
148
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",
164 };
165
166 static void in_sdl_probe(const in_drv_t *drv)
167 {
168         struct in_sdl_state *state;
169         SDL_Joystick *joy;
170         int i, joycount;
171         char name[256];
172
173         state = calloc(1, sizeof(*state));
174         if (state == NULL) {
175                 fprintf(stderr, "in_sdl: OOM\n");
176                 return;
177         }
178
179         state->drv = drv;
180         in_register(IN_SDL_PREFIX "keys", -1, state, SDLK_LAST,
181                 in_sdl_keys, 0);
182
183         /* joysticks go here too */
184         SDL_InitSubSystem(SDL_INIT_JOYSTICK);
185
186         joycount = SDL_NumJoysticks();
187         for (i = 0; i < joycount; i++) {
188                 joy = SDL_JoystickOpen(i);
189                 if (joy == NULL)
190                         continue;
191
192                 state = calloc(1, sizeof(*state));
193                 if (state == NULL) {
194                         fprintf(stderr, "in_sdl: OOM\n");
195                         break;
196                 }
197                 state->joy = joy;
198                 state->joy_id = i;
199                 state->drv = drv;
200
201                 snprintf(name, sizeof(name), IN_SDL_PREFIX "%s", SDL_JoystickName(i));
202                 in_register(name, -1, state, SDLK_LAST, in_sdl_keys, 0);
203         }
204
205         if (joycount > 0)
206                 SDL_JoystickEventState(SDL_ENABLE);
207 }
208
209 static void in_sdl_free(void *drv_data)
210 {
211         struct in_sdl_state *state = drv_data;
212
213         if (state != NULL) {
214                 if (state->joy != NULL)
215                         SDL_JoystickClose(state->joy);
216                 free(state);
217         }
218 }
219
220 static const char * const *
221 in_sdl_get_key_names(int *count)
222 {
223         *count = SDLK_LAST;
224         return in_sdl_keys;
225 }
226
227 /* could use SDL_GetKeyState, but this gives better packing */
228 static void update_keystate(keybits_t *keystate, int sym, int is_down)
229 {
230         keybits_t *ks_word, mask;
231
232         mask = 1;
233         mask <<= sym & (KEYBITS_WORD_BITS - 1);
234         ks_word = keystate + sym / KEYBITS_WORD_BITS;
235         if (is_down)
236                 *ks_word |= mask;
237         else
238                 *ks_word &= ~mask;
239 }
240
241 static int handle_event(struct in_sdl_state *state, SDL_Event *event,
242         int *kc_out, int *down_out)
243 {
244         if (event->type != SDL_KEYDOWN && event->type != SDL_KEYUP)
245                 return -1;
246
247         update_keystate(state->keystate, event->key.keysym.sym,
248                 event->type == SDL_KEYDOWN);
249         if (kc_out != NULL)
250                 *kc_out = event->key.keysym.sym;
251         if (down_out != NULL)
252                 *down_out = event->type == SDL_KEYDOWN;
253
254         return 1;
255 }
256
257 static int handle_joy_event(struct in_sdl_state *state, SDL_Event *event,
258         int *kc_out, int *down_out)
259 {
260         int kc = -1, down = 0, ret = 0;
261
262         /* TODO: remaining axis */
263         switch (event->type) {
264         case SDL_JOYAXISMOTION:
265                 if (event->jaxis.which != state->joy_id)
266                         return -2;
267                 if (event->jaxis.axis > 1)
268                         break;
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;
272                         ret = 1;
273                 }
274                 else if (event->jaxis.value < -16384) {
275                         kc = state->axis_keydown[event->jaxis.axis];
276                         if (kc)
277                                 update_keystate(state->keystate, kc, 0);
278                         kc = event->jaxis.axis ? SDLK_UP : SDLK_LEFT;
279                         state->axis_keydown[event->jaxis.axis] = kc;
280                         down = 1;
281                         ret = 1;
282                 }
283                 else if (event->jaxis.value > 16384) {
284                         kc = state->axis_keydown[event->jaxis.axis];
285                         if (kc)
286                                 update_keystate(state->keystate, kc, 0);
287                         kc = event->jaxis.axis ? SDLK_DOWN : SDLK_RIGHT;
288                         state->axis_keydown[event->jaxis.axis] = kc;
289                         down = 1;
290                         ret = 1;
291                 }
292                 break;
293
294         case SDL_JOYBUTTONDOWN:
295         case SDL_JOYBUTTONUP:
296                 if (event->jbutton.which != state->joy_id)
297                         return -2;
298                 kc = (int)event->jbutton.button + SDLK_WORLD_0;
299                 down = event->jbutton.state == SDL_PRESSED;
300                 ret = 1;
301                 break;
302         default:
303                 return -1;
304         }
305
306         if (ret)
307                 update_keystate(state->keystate, kc, down);
308         if (kc_out != NULL)
309                 *kc_out = kc;
310         if (down_out != NULL)
311                 *down_out = down;
312
313         return ret;
314 }
315
316 #define JOY_EVENTS (SDL_JOYAXISMOTIONMASK | SDL_JOYBALLMOTIONMASK | SDL_JOYHATMOTIONMASK \
317                     | SDL_JOYBUTTONDOWNMASK | SDL_JOYBUTTONUPMASK)
318
319 static int collect_events(struct in_sdl_state *state, int *one_kc, int *one_down)
320 {
321         SDL_Event events[4];
322         Uint32 mask = state->joy ? JOY_EVENTS : (SDL_ALLEVENTS & ~JOY_EVENTS);
323         int count, maxcount;
324         int i, ret, retval = 0;
325         int num_events, num_peeped_events;
326         SDL_Event *event;
327
328         maxcount = (one_kc != NULL) ? 1 : sizeof(events) / sizeof(events[0]);
329
330         SDL_PumpEvents();
331
332         num_events = SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, mask);
333
334         for (num_peeped_events = 0; num_peeped_events < num_events; num_peeped_events += count) {
335                 count = SDL_PeepEvents(events, maxcount, SDL_GETEVENT, mask);
336                 if (count <= 0)
337                         break;
338                 for (i = 0; i < count; i++) {
339                         event = &events[i];
340                         if (state->joy)
341                                 ret = handle_joy_event(state,
342                                         event, one_kc, one_down);
343                         else
344                                 ret = handle_event(state,
345                                         event, one_kc, one_down);
346                         if (ret < 0) {
347                                 switch (ret) {
348                                         case -2:
349                                                 SDL_PushEvent(event);
350                                                 break;
351                                         default:
352                                                 if (ext_event_handler != NULL)
353                                                         ext_event_handler(event);
354                                                 break;
355                                 }
356                                 continue;
357                         }
358
359                         retval |= ret;
360                         if (one_kc != NULL && ret)
361                         {
362                                 // don't lose events other devices might want to handle
363                                 for (i++; i < count; i++)
364                                         SDL_PushEvent(&events[i]);
365                                 goto out;
366                         }
367                 }
368         }
369
370 out:
371         return retval;
372 }
373
374 static int in_sdl_update(void *drv_data, const int *binds, int *result)
375 {
376         struct in_sdl_state *state = drv_data;
377         keybits_t mask;
378         int i, sym, bit, b;
379
380         collect_events(state, NULL, NULL);
381
382         for (i = 0; i < SDLK_LAST / KEYBITS_WORD_BITS + 1; i++) {
383                 mask = state->keystate[i];
384                 if (mask == 0)
385                         continue;
386                 for (bit = 0; mask != 0; bit++, mask >>= 1) {
387                         if ((mask & 1) == 0)
388                                 continue;
389                         sym = i * KEYBITS_WORD_BITS + bit;
390
391                         for (b = 0; b < IN_BINDTYPE_COUNT; b++)
392                                 result[b] |= binds[IN_BIND_OFFS(sym, b)];
393                 }
394         }
395
396         return 0;
397 }
398
399 static int in_sdl_update_keycode(void *drv_data, int *is_down)
400 {
401         struct in_sdl_state *state = drv_data;
402         int ret_kc = -1, ret_down = 0;
403
404         collect_events(state, &ret_kc, &ret_down);
405
406         if (is_down != NULL)
407                 *is_down = ret_down;
408
409         return ret_kc;
410 }
411
412 static int in_sdl_menu_translate(void *drv_data, int keycode, char *charcode)
413 {
414         struct in_sdl_state *state = drv_data;
415         const struct in_pdata *pdata = state->drv->pdata;
416         const struct menu_keymap *map;
417         int map_len;
418         int ret = 0;
419         int i;
420
421         if (state->joy) {
422                 map = pdata->joy_map;
423                 map_len = pdata->jmap_size;
424         } else {
425                 map = pdata->key_map;
426                 map_len = pdata->kmap_size;
427         }
428
429         if (keycode < 0)
430         {
431                 /* menu -> kc */
432                 keycode = -keycode;
433                 for (i = 0; i < map_len; i++)
434                         if (map[i].pbtn == keycode)
435                                 return map[i].key;
436         }
437         else
438         {
439                 for (i = 0; i < map_len; i++) {
440                         if (map[i].key == keycode) {
441                                 ret = map[i].pbtn;
442                                 break;
443                         }
444                 }
445
446                 if (charcode != NULL && (unsigned int)keycode < SDLK_LAST &&
447                     in_sdl_keys[keycode] != NULL && in_sdl_keys[keycode][1] == 0)
448                 {
449                         ret |= PBTN_CHAR;
450                         *charcode = in_sdl_keys[keycode][0];
451                 }
452         }
453
454         return ret;
455 }
456
457 static const in_drv_t in_sdl_drv = {
458         .prefix         = IN_SDL_PREFIX,
459         .probe          = in_sdl_probe,
460         .free           = in_sdl_free,
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,
465 };
466
467 int in_sdl_init(const struct in_pdata *pdata, void (*handler)(void *event))
468 {
469         if (!pdata) {
470                 fprintf(stderr, "in_sdl: Missing input platform data\n");
471                 return -1;
472         }
473
474         in_register_driver(&in_sdl_drv, pdata->defbinds, pdata);
475         ext_event_handler = handler;
476         return 0;
477 }