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