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