forked from notaz/libpicofe
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathin_sdl.c
590 lines (523 loc) · 13.2 KB
/
in_sdl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
/*
* (C) Gražvydas "notaz" Ignotas, 2012
*
* This work is licensed under the terms of any of these licenses
* (at your option):
* - GNU GPL, version 2 or later.
* - GNU LGPL, version 2.1 or later.
* - MAME license.
* See the COPYING file in the top-level directory.
*/
#include <stdio.h>
#include <SDL.h>
#include "input.h"
#include "in_sdl.h"
#define IN_SDL_PREFIX "sdl:"
/* should be machine word for best performace */
typedef unsigned long keybits_t;
#define KEYBITS_WORD_BITS (sizeof(keybits_t) * 8)
struct in_sdl_state {
const in_drv_t *drv;
SDL_Joystick *joy;
int joy_id;
int axis_keydown[2];
#ifdef SDL_REDRAW_EVT
int redraw;
SDL_Event revent;
#endif
keybits_t keystate[SDLK_LAST / KEYBITS_WORD_BITS + 1];
// emulator keys should always be processed immediately lest one is lost
keybits_t emu_keys[SDLK_LAST / KEYBITS_WORD_BITS + 1];
};
static void (*ext_event_handler)(void *event);
static const char * const in_sdl_keys[SDLK_LAST] = {
[SDLK_BACKSPACE] = "backspace",
[SDLK_TAB] = "tab",
[SDLK_CLEAR] = "clear",
[SDLK_RETURN] = "return",
[SDLK_PAUSE] = "pause",
[SDLK_ESCAPE] = "escape",
[SDLK_SPACE] = "space",
[SDLK_EXCLAIM] = "!",
[SDLK_QUOTEDBL] = "\"",
[SDLK_HASH] = "#",
[SDLK_DOLLAR] = "$",
[SDLK_AMPERSAND] = "&",
[SDLK_QUOTE] = "'",
[SDLK_LEFTPAREN] = "(",
[SDLK_RIGHTPAREN] = ")",
[SDLK_ASTERISK] = "*",
[SDLK_PLUS] = "+",
[SDLK_COMMA] = ",",
[SDLK_MINUS] = "-",
[SDLK_PERIOD] = ".",
[SDLK_SLASH] = "/",
[SDLK_0] = "0",
[SDLK_1] = "1",
[SDLK_2] = "2",
[SDLK_3] = "3",
[SDLK_4] = "4",
[SDLK_5] = "5",
[SDLK_6] = "6",
[SDLK_7] = "7",
[SDLK_8] = "8",
[SDLK_9] = "9",
[SDLK_COLON] = ":",
[SDLK_SEMICOLON] = ";",
[SDLK_LESS] = "<",
[SDLK_EQUALS] = "=",
[SDLK_GREATER] = ">",
[SDLK_QUESTION] = "?",
[SDLK_AT] = "@",
[SDLK_LEFTBRACKET] = "[",
[SDLK_BACKSLASH] = "\\",
[SDLK_RIGHTBRACKET] = "]",
[SDLK_CARET] = "^",
[SDLK_UNDERSCORE] = "_",
[SDLK_BACKQUOTE] = "`",
[SDLK_a] = "a",
[SDLK_b] = "b",
[SDLK_c] = "c",
[SDLK_d] = "d",
[SDLK_e] = "e",
[SDLK_f] = "f",
[SDLK_g] = "g",
[SDLK_h] = "h",
[SDLK_i] = "i",
[SDLK_j] = "j",
[SDLK_k] = "k",
[SDLK_l] = "l",
[SDLK_m] = "m",
[SDLK_n] = "n",
[SDLK_o] = "o",
[SDLK_p] = "p",
[SDLK_q] = "q",
[SDLK_r] = "r",
[SDLK_s] = "s",
[SDLK_t] = "t",
[SDLK_u] = "u",
[SDLK_v] = "v",
[SDLK_w] = "w",
[SDLK_x] = "x",
[SDLK_y] = "y",
[SDLK_z] = "z",
[SDLK_DELETE] = "delete",
[SDLK_KP0] = "[0]",
[SDLK_KP1] = "[1]",
[SDLK_KP2] = "[2]",
[SDLK_KP3] = "[3]",
[SDLK_KP4] = "[4]",
[SDLK_KP5] = "[5]",
[SDLK_KP6] = "[6]",
[SDLK_KP7] = "[7]",
[SDLK_KP8] = "[8]",
[SDLK_KP9] = "[9]",
[SDLK_KP_PERIOD] = "[.]",
[SDLK_KP_DIVIDE] = "[/]",
[SDLK_KP_MULTIPLY] = "[*]",
[SDLK_KP_MINUS] = "[-]",
[SDLK_KP_PLUS] = "[+]",
[SDLK_KP_ENTER] = "enter",
[SDLK_KP_EQUALS] = "equals",
[SDLK_UP] = "up",
[SDLK_DOWN] = "down",
[SDLK_RIGHT] = "right",
[SDLK_LEFT] = "left",
[SDLK_INSERT] = "insert",
[SDLK_HOME] = "home",
[SDLK_END] = "end",
[SDLK_PAGEUP] = "page up",
[SDLK_PAGEDOWN] = "page down",
[SDLK_F1] = "f1",
[SDLK_F2] = "f2",
[SDLK_F3] = "f3",
[SDLK_F4] = "f4",
[SDLK_F5] = "f5",
[SDLK_F6] = "f6",
[SDLK_F7] = "f7",
[SDLK_F8] = "f8",
[SDLK_F9] = "f9",
[SDLK_F10] = "f10",
[SDLK_F11] = "f11",
[SDLK_F12] = "f12",
[SDLK_F13] = "f13",
[SDLK_F14] = "f14",
[SDLK_F15] = "f15",
[SDLK_NUMLOCK] = "numlock",
[SDLK_CAPSLOCK] = "caps lock",
[SDLK_SCROLLOCK] = "scroll lock",
[SDLK_RSHIFT] = "right shift",
[SDLK_LSHIFT] = "left shift",
[SDLK_RCTRL] = "right ctrl",
[SDLK_LCTRL] = "left ctrl",
[SDLK_RALT] = "right alt",
[SDLK_LALT] = "left alt",
[SDLK_RMETA] = "right meta",
[SDLK_LMETA] = "left meta",
[SDLK_LSUPER] = "left super", /* "Windows" keys */
[SDLK_RSUPER] = "right super",
[SDLK_MODE] = "alt gr",
[SDLK_COMPOSE] = "compose",
};
static void in_sdl_probe(const in_drv_t *drv)
{
const struct in_pdata *pdata = drv->pdata;
const char * const * key_names = in_sdl_keys;
struct in_sdl_state *state;
SDL_Joystick *joy;
int i, joycount;
char name[256];
if (pdata->key_names)
key_names = pdata->key_names;
state = calloc(1, sizeof(*state));
if (state == NULL) {
fprintf(stderr, "in_sdl: OOM\n");
return;
}
state->drv = drv;
in_register(IN_SDL_PREFIX "keys", -1, state, SDLK_LAST,
key_names, 0);
//SDL_EnableUNICODE(1);
/* joysticks go here too */
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
joycount = SDL_NumJoysticks();
for (i = 0; i < joycount; i++) {
joy = SDL_JoystickOpen(i);
if (joy == NULL)
continue;
state = calloc(1, sizeof(*state));
if (state == NULL) {
fprintf(stderr, "in_sdl: OOM\n");
break;
}
state->joy = joy;
state->joy_id = i;
state->drv = drv;
snprintf(name, sizeof(name), IN_SDL_PREFIX "%s", SDL_JoystickName(i));
in_register(name, -1, state, SDLK_LAST, key_names, 0);
}
if (joycount > 0)
SDL_JoystickEventState(SDL_ENABLE);
}
static void in_sdl_free(void *drv_data)
{
struct in_sdl_state *state = drv_data;
if (state != NULL) {
if (state->joy != NULL)
SDL_JoystickClose(state->joy);
free(state);
}
}
static const char * const *
in_sdl_get_key_names(const in_drv_t *drv, int *count)
{
const struct in_pdata *pdata = drv->pdata;
*count = SDLK_LAST;
if (pdata->key_names)
return pdata->key_names;
return in_sdl_keys;
}
/* could use SDL_GetKeyState, but this gives better packing */
static void update_keystate(keybits_t *keystate, int sym, int is_down)
{
keybits_t *ks_word, mask;
mask = 1;
mask <<= sym & (KEYBITS_WORD_BITS - 1);
ks_word = keystate + sym / KEYBITS_WORD_BITS;
if (is_down)
*ks_word |= mask;
else
*ks_word &= ~mask;
}
static int get_keystate(keybits_t *keystate, int sym)
{
keybits_t *ks_word, mask;
mask = 1;
mask <<= sym & (KEYBITS_WORD_BITS - 1);
ks_word = keystate + sym / KEYBITS_WORD_BITS;
return !!(*ks_word & mask);
}
static int handle_event(struct in_sdl_state *state, SDL_Event *event,
int *kc_out, int *down_out, int *emu_out)
{
int emu;
if (event->type != SDL_KEYDOWN && event->type != SDL_KEYUP)
return -1;
emu = get_keystate(state->emu_keys, event->key.keysym.sym);
update_keystate(state->keystate, event->key.keysym.sym,
event->type == SDL_KEYDOWN);
if (kc_out != NULL)
*kc_out = event->key.keysym.sym;
if (down_out != NULL)
*down_out = event->type == SDL_KEYDOWN;
if (emu_out != NULL)
*emu_out = emu;
return 1;
}
static int handle_joy_event(struct in_sdl_state *state, SDL_Event *event,
int *kc_out, int *down_out, int *emu_out)
{
int kc = -1, down = 0, emu = 0, ret = 0;
/* TODO: remaining axis */
switch (event->type) {
case SDL_JOYAXISMOTION:
if (event->jaxis.which != state->joy_id)
return -2;
if (event->jaxis.axis > 1)
break;
if (-16384 <= event->jaxis.value && event->jaxis.value <= 16384) {
kc = state->axis_keydown[event->jaxis.axis];
state->axis_keydown[event->jaxis.axis] = 0;
ret = 1;
}
else if (event->jaxis.value < -16384) {
kc = state->axis_keydown[event->jaxis.axis];
if (kc) {
emu |= get_keystate(state->emu_keys, kc);
update_keystate(state->keystate, kc, 0);
}
kc = event->jaxis.axis ? SDLK_UP : SDLK_LEFT;
state->axis_keydown[event->jaxis.axis] = kc;
down = 1;
ret = 1;
}
else if (event->jaxis.value > 16384) {
kc = state->axis_keydown[event->jaxis.axis];
if (kc) {
emu |= get_keystate(state->emu_keys, kc);
update_keystate(state->keystate, kc, 0);
}
kc = event->jaxis.axis ? SDLK_DOWN : SDLK_RIGHT;
state->axis_keydown[event->jaxis.axis] = kc;
down = 1;
ret = 1;
}
break;
case SDL_JOYBUTTONDOWN:
case SDL_JOYBUTTONUP:
if (event->jbutton.which != state->joy_id)
return -2;
kc = (int)event->jbutton.button + SDLK_WORLD_0;
down = event->jbutton.state == SDL_PRESSED;
ret = 1;
break;
default:
return -1;
}
if (ret) {
emu |= get_keystate(state->emu_keys, kc);
update_keystate(state->keystate, kc, down);
}
if (kc_out != NULL)
*kc_out = kc;
if (down_out != NULL)
*down_out = down;
if (emu_out != 0)
*emu_out = emu;
return ret;
}
#define JOY_EVENTS (SDL_JOYAXISMOTIONMASK | SDL_JOYBALLMOTIONMASK | SDL_JOYHATMOTIONMASK \
| SDL_JOYBUTTONDOWNMASK | SDL_JOYBUTTONUPMASK)
static int collect_events(struct in_sdl_state *state, int *one_kc, int *one_down)
{
SDL_Event events[8];
Uint32 mask = state->joy ? JOY_EVENTS : (SDL_ALLEVENTS & ~JOY_EVENTS);
int count, maxcount, is_emukey = 0;
int i = 0, ret = 0, retval = 0;
SDL_Event *event;
SDL_PumpEvents();
maxcount = sizeof(events) / sizeof(events[0]);
if ((count = SDL_PeepEvents(events, maxcount, SDL_GETEVENT, mask)) > 0) {
for (i = 0; i < count; i++) {
event = &events[i];
if (state->joy) {
ret = handle_joy_event(state,
event, one_kc, one_down, &is_emukey);
} else {
ret = handle_event(state,
event, one_kc, one_down, &is_emukey);
}
if (ret < 0) {
switch (ret) {
case -2:
SDL_PushEvent(event);
break;
default:
#ifdef SDL_REDRAW_EVT
if (event->type == SDL_VIDEORESIZE) {
state->redraw = 1;
state->revent = *event;
} else if (event->type == SDL_VIDEOEXPOSE) {
if (state->revent.type == SDL_NOEVENT) {
state->redraw = 1;
state->revent = *event;
}
} else
#endif
if (ext_event_handler != NULL)
ext_event_handler(event);
break;
}
continue;
}
retval |= ret;
if ((is_emukey || one_kc != NULL) && retval)
{
break;
}
}
}
#ifdef SDL_REDRAW_EVT
// if the event queue has been emptied and resize/expose events were in it
if (state->redraw && count == 0) {
if (ext_event_handler != NULL)
ext_event_handler(&state->revent);
state->redraw = 0;
state->revent.type = SDL_NOEVENT;
// dummy key event to force returning from the key loop,
// so the application has a chance to redraw the window
if (one_kc != NULL) {
*one_kc = SDLK_UNKNOWN;
retval |= 1;
}
if (one_down != NULL)
*one_down = 1;
} else
#endif
i++;
// don't lose events other devices might want to handle
if (i < count)
SDL_PeepEvents(events+i, count-i, SDL_ADDEVENT, mask);
return retval;
}
static int in_sdl_update(void *drv_data, const int *binds, int *result)
{
struct in_sdl_state *state = drv_data;
keybits_t mask;
int i, sym, bit, b;
collect_events(state, NULL, NULL);
for (i = 0; i < SDLK_LAST / KEYBITS_WORD_BITS + 1; i++) {
mask = state->keystate[i];
if (mask == 0)
continue;
for (bit = 0; mask != 0; bit++, mask >>= 1) {
if ((mask & 1) == 0)
continue;
sym = i * KEYBITS_WORD_BITS + bit;
for (b = 0; b < IN_BINDTYPE_COUNT; b++)
result[b] |= binds[IN_BIND_OFFS(sym, b)];
}
}
return 0;
}
static int in_sdl_update_kbd(void *drv_data, const int *binds, int *result)
{
struct in_sdl_state *state = drv_data;
keybits_t mask;
int i, sym, bit, b = 0;
collect_events(state, NULL, NULL);
for (i = 0; i < SDLK_LAST / KEYBITS_WORD_BITS + 1; i++) {
mask = state->keystate[i];
if (mask == 0)
continue;
for (bit = 0; mask != 0; bit++, mask >>= 1) {
if ((mask & 1) == 0)
continue;
sym = i * KEYBITS_WORD_BITS + bit;
result[b++] = binds[sym];
}
}
return b;
}
static int in_sdl_update_keycode(void *drv_data, int *is_down)
{
struct in_sdl_state *state = drv_data;
int ret_kc = -1, ret_down = 0;
collect_events(state, &ret_kc, &ret_down);
if (is_down != NULL)
*is_down = ret_down;
return ret_kc;
}
static int in_sdl_menu_translate(void *drv_data, int keycode, char *charcode)
{
struct in_sdl_state *state = drv_data;
const struct in_pdata *pdata = state->drv->pdata;
const char * const * key_names = in_sdl_keys;
const struct menu_keymap *map;
int map_len;
int ret = 0;
int i;
if (pdata->key_names)
key_names = pdata->key_names;
if (state->joy) {
map = pdata->joy_map;
map_len = pdata->jmap_size;
} else {
map = pdata->key_map;
map_len = pdata->kmap_size;
}
if (keycode < 0)
{
/* menu -> kc */
keycode = -keycode;
for (i = 0; i < map_len; i++)
if (map[i].pbtn == keycode)
return map[i].key;
}
else
{
#ifdef SDL_REDRAW_EVT
if (keycode == SDLK_UNKNOWN)
ret = PBTN_RDRAW;
else
#endif
for (i = 0; i < map_len; i++) {
if (map[i].key == keycode) {
ret = map[i].pbtn;
break;
}
}
if (charcode != NULL && (unsigned int)keycode < SDLK_LAST &&
key_names[keycode] != NULL && key_names[keycode][1] == 0)
{
ret |= PBTN_CHAR;
*charcode = key_names[keycode][0];
}
}
return ret;
}
static int in_sdl_clean_binds(void *drv_data, int *binds, int *def_finds)
{
struct in_sdl_state *state = drv_data;
int i, t, cnt = 0;
memset(state->emu_keys, 0, sizeof(state->emu_keys));
for (t = 0; t < IN_BINDTYPE_COUNT; t++)
for (i = 0; i < SDLK_LAST; i++)
if (binds[IN_BIND_OFFS(i, t)]) {
if (t == IN_BINDTYPE_EMU)
update_keystate(state->emu_keys, i, 1);
cnt ++;
}
return cnt;
}
static const in_drv_t in_sdl_drv = {
.prefix = IN_SDL_PREFIX,
.probe = in_sdl_probe,
.free = in_sdl_free,
.get_key_names = in_sdl_get_key_names,
.update = in_sdl_update,
.update_kbd = in_sdl_update_kbd,
.update_keycode = in_sdl_update_keycode,
.menu_translate = in_sdl_menu_translate,
.clean_binds = in_sdl_clean_binds,
};
int in_sdl_init(const struct in_pdata *pdata, void (*handler)(void *event))
{
if (!pdata) {
fprintf(stderr, "in_sdl: Missing input platform data\n");
return -1;
}
in_register_driver(&in_sdl_drv, pdata->defbinds, pdata->kbd_map, pdata);
ext_event_handler = handler;
return 0;
}