input: rework abs handling, change API, allow custom key names
[picodrive.git] / platform / common / input.c
CommitLineData
e1f99f2b 1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4
5#include "input.h"
0403eead 6#include "plat.h"
ba86b61e 7#include "lprintf.h"
8a011923 8
9#ifdef IN_EVDEV
3b13ec65 10#include "../linux/in_evdev.h"
8a011923 11#endif
12#ifdef IN_GP2X
217d08bc 13#include "../gp2x/in_gp2x.h"
8a011923 14#endif
15#ifdef IN_VK
823b9004 16#include "../win32/in_vk.h"
8a011923 17#endif
e1f99f2b 18
19typedef struct
20{
21 int drv_id;
7201dc73 22 int drv_fd_hnd;
e1f99f2b 23 void *drv_data;
e1f99f2b 24 char *name;
093b8a42 25 int key_count;
26 int *binds; /* total = key_count * bindtypes * 2 */
603c28b3 27 const char * const *key_names;
28 unsigned int probed:1;
29 unsigned int does_combos:1;
e1f99f2b 30} in_dev_t;
31
819ef025 32static in_drv_t in_drivers[IN_DRVID_COUNT];
e1f99f2b 33static in_dev_t in_devices[IN_MAX_DEVS];
34static int in_dev_count = 0;
217d08bc 35static int in_have_async_devs = 0;
991473ad 36static int menu_key_state = 0;
37static int menu_last_used_dev = 0;
e1f99f2b 38
819ef025 39#define DRV(id) in_drivers[(unsigned)(id) < IN_DRVID_COUNT ? (id) : 0]
40
217d08bc 41
093b8a42 42static int *in_alloc_binds(int drv_id, int key_count)
e1f99f2b 43{
093b8a42 44 int *binds;
e1f99f2b 45
093b8a42 46 binds = calloc(key_count * IN_BINDTYPE_COUNT * 2, sizeof(binds[0]));
819ef025 47 if (binds == NULL)
48 return NULL;
49
093b8a42 50 DRV(drv_id).get_def_binds(binds + key_count * IN_BINDTYPE_COUNT);
51 memcpy(binds, binds + key_count * IN_BINDTYPE_COUNT,
52 sizeof(binds[0]) * key_count * IN_BINDTYPE_COUNT);
819ef025 53
54 return binds;
e1f99f2b 55}
56
57static void in_free(in_dev_t *dev)
58{
819ef025 59 if (dev->probed)
60 DRV(dev->drv_id).free(dev->drv_data);
e1f99f2b 61 dev->probed = 0;
62 dev->drv_data = NULL;
63 free(dev->name);
64 dev->name = NULL;
65 free(dev->binds);
66 dev->binds = NULL;
67}
68
217d08bc 69/* to be called by drivers
70 * async devices must set drv_fd_hnd to -1 */
093b8a42 71void in_register(const char *nname, int drv_id, int drv_fd_hnd, void *drv_data,
603c28b3 72 int key_count, const char * const *key_names, int combos)
e1f99f2b 73{
819ef025 74 int i, ret, dupe_count = 0, *binds;
e1f99f2b 75 char name[256], *name_end, *tmp;
76
77 strncpy(name, nname, sizeof(name));
78 name[sizeof(name)-12] = 0;
79 name_end = name + strlen(name);
80
81 for (i = 0; i < in_dev_count; i++)
82 {
83 if (in_devices[i].name == NULL)
84 continue;
85 if (strcmp(in_devices[i].name, name) == 0)
86 {
87 if (in_devices[i].probed) {
88 dupe_count++;
89 sprintf(name_end, " [%d]", dupe_count);
90 continue;
91 }
92 goto update;
93 }
94 }
95
96 if (i >= IN_MAX_DEVS)
97 {
98 /* try to find unused device */
99 for (i = 0; i < IN_MAX_DEVS; i++)
100 if (!in_devices[i].probed) break;
101 if (i >= IN_MAX_DEVS) {
ba86b61e 102 lprintf("input: too many devices, can't add %s\n", name);
e1f99f2b 103 return;
104 }
105 in_free(&in_devices[i]);
106 }
107
108 tmp = strdup(name);
109 if (tmp == NULL)
110 return;
111
093b8a42 112 binds = in_alloc_binds(drv_id, key_count);
e1f99f2b 113 if (binds == NULL) {
114 free(tmp);
115 return;
116 }
117
118 in_devices[i].name = tmp;
119 in_devices[i].binds = binds;
093b8a42 120 in_devices[i].key_count = key_count;
e1f99f2b 121 if (i + 1 > in_dev_count)
122 in_dev_count = i + 1;
123
ba86b61e 124 lprintf("input: new device #%d \"%s\"\n", i, name);
e1f99f2b 125update:
126 in_devices[i].probed = 1;
9025b931 127 in_devices[i].does_combos = combos;
e1f99f2b 128 in_devices[i].drv_id = drv_id;
7201dc73 129 in_devices[i].drv_fd_hnd = drv_fd_hnd;
603c28b3 130 in_devices[i].key_names = key_names;
e1f99f2b 131 in_devices[i].drv_data = drv_data;
819ef025 132
133 if (in_devices[i].binds != NULL) {
093b8a42 134 ret = DRV(drv_id).clean_binds(drv_data, in_devices[i].binds,
135 in_devices[i].binds + key_count * IN_BINDTYPE_COUNT);
819ef025 136 if (ret == 0) {
137 /* no useable binds */
138 free(in_devices[i].binds);
139 in_devices[i].binds = NULL;
140 }
141 }
e1f99f2b 142}
143
093b8a42 144/* key combo handling, to be called by drivers that support it.
145 * Only care about IN_BINDTYPE_EMU */
146void in_combos_find(const int *binds, int last_key, int *combo_keys, int *combo_acts)
9025b931 147{
148 int act, u;
149
150 *combo_keys = *combo_acts = 0;
151 for (act = 0; act < sizeof(binds[0]) * 8; act++)
152 {
153 int keyc = 0;
154 for (u = 0; u <= last_key; u++)
093b8a42 155 if (binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)] & (1 << act))
9025b931 156 keyc++;
157
158 if (keyc > 1)
159 {
160 // loop again and mark those keys and actions as combo
161 for (u = 0; u <= last_key; u++)
162 {
093b8a42 163 if (binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)] & (1 << act)) {
9025b931 164 *combo_keys |= 1 << u;
165 *combo_acts |= 1 << act;
166 }
167 }
168 }
169 }
170}
171
093b8a42 172int in_combos_do(int keys, const int *binds, int last_key, int combo_keys, int combo_acts)
9025b931 173{
174 int i, ret = 0;
175
176 for (i = 0; i <= last_key; i++)
177 {
093b8a42 178 int acts, acts_c, u;
179
9025b931 180 if (!(keys & (1 << i)))
181 continue;
182
093b8a42 183 acts = binds[IN_BIND_OFFS(i, IN_BINDTYPE_EMU)];
9025b931 184 if (!acts)
185 continue;
186
093b8a42 187 if (!(combo_keys & (1 << i))) {
9025b931 188 ret |= acts;
093b8a42 189 continue;
190 }
191
192 acts_c = acts & combo_acts;
193 u = last_key;
194 if (acts_c) {
195 // let's try to find the other one
196 for (u = i + 1; u <= last_key; u++)
197 if ( (keys & (1 << u)) && (binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)] & acts_c) ) {
198 ret |= acts_c & binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)];
199 keys &= ~((1 << i) | (1 << u));
200 break;
201 }
202 }
203 // add non-combo actions if combo ones were not found
204 if (u >= last_key)
205 ret |= acts & ~combo_acts;
9025b931 206 }
207
208 return ret;
209}
210
e1f99f2b 211void in_probe(void)
212{
213 int i;
217d08bc 214
215 in_have_async_devs = 0;
e1f99f2b 216 for (i = 0; i < in_dev_count; i++)
217 in_devices[i].probed = 0;
218
819ef025 219 for (i = 1; i < IN_DRVID_COUNT; i++)
220 in_drivers[i].probe();
e1f99f2b 221
222 /* get rid of devs without binds and probes */
223 for (i = 0; i < in_dev_count; i++) {
224 if (!in_devices[i].probed && in_devices[i].binds == NULL) {
225 in_dev_count--;
226 if (i < in_dev_count) {
227 free(in_devices[i].name);
228 memmove(&in_devices[i], &in_devices[i+1],
229 (in_dev_count - i) * sizeof(in_devices[0]));
230 }
217d08bc 231
232 continue;
e1f99f2b 233 }
217d08bc 234
235 if (in_devices[i].probed && in_devices[i].drv_fd_hnd == -1)
236 in_have_async_devs = 1;
e1f99f2b 237 }
217d08bc 238
239 if (in_have_async_devs)
ba86b61e 240 lprintf("input: async-only devices detected..\n");
603c28b3 241
242 in_debug_dump();
e1f99f2b 243}
244
7201dc73 245/* async update */
093b8a42 246int in_update(int *result)
e1f99f2b 247{
093b8a42 248 int i, ret = 0;
e1f99f2b 249
250 for (i = 0; i < in_dev_count; i++) {
f484a9fe 251 in_dev_t *dev = &in_devices[i];
f484a9fe 252 if (dev->probed && dev->binds != NULL) {
823b9004 253 // FIXME: this is stupid, make it indirect
f484a9fe 254 switch (dev->drv_id) {
819ef025 255#ifdef IN_EVDEV
3427e573 256 case IN_DRVID_EVDEV:
093b8a42 257 ret |= in_evdev_update(dev->drv_data, dev->binds, result);
3427e573 258 break;
217d08bc 259#endif
260#ifdef IN_GP2X
261 case IN_DRVID_GP2X:
093b8a42 262 ret |= in_gp2x_update(dev->drv_data, dev->binds, result);
217d08bc 263 break;
819ef025 264#endif
8a011923 265#ifdef IN_VK
823b9004 266 case IN_DRVID_VK:
267 ret |= in_vk_update(dev->drv_data, dev->binds, result);
268 break;
8a011923 269#endif
3427e573 270 }
e1f99f2b 271 }
272 }
273
093b8a42 274 return ret;
e1f99f2b 275}
276
217d08bc 277static int in_update_kc_async(int *dev_id_out, int *is_down_out, int timeout_ms)
278{
217d08bc 279 int i, is_down, result;
0403eead 280 unsigned int ticks;
217d08bc 281
0403eead 282 ticks = plat_get_ticks_ms();
217d08bc 283
284 while (1)
285 {
286 for (i = 0; i < in_dev_count; i++) {
287 in_dev_t *d = &in_devices[i];
288 if (!d->probed)
289 continue;
290
291 result = DRV(d->drv_id).update_keycode(d->drv_data, &is_down);
292 if (result == -1)
293 continue;
294
295 if (dev_id_out)
296 *dev_id_out = i;
297 if (is_down_out)
298 *is_down_out = is_down;
299 return result;
300 }
301
0403eead 302 if (timeout_ms >= 0 && (int)(plat_get_ticks_ms() - ticks) > timeout_ms)
303 break;
217d08bc 304
0403eead 305 plat_sleep_ms(10);
217d08bc 306 }
307
308 return -1;
309}
7201dc73 310
3b13ec65 311/*
217d08bc 312 * wait for a press, always return some keycode or -1 on timeout or error
3b13ec65 313 */
f484a9fe 314int in_update_keycode(int *dev_id_out, int *is_down_out, int timeout_ms)
3b13ec65 315{
0403eead 316 int result = -1, dev_id = 0, is_down, result_menu;
7201dc73 317 int fds_hnds[IN_MAX_DEVS];
318 int i, ret, count = 0;
74f5e726 319 in_drv_t *drv = NULL;
0403eead 320 unsigned int ticks;
7201dc73 321
217d08bc 322 if (in_have_async_devs) {
323 result = in_update_kc_async(&dev_id, &is_down, timeout_ms);
324 if (result == -1)
325 return -1;
326 drv = &DRV(in_devices[dev_id].drv_id);
327 goto finish;
328 }
329
0403eead 330 ticks = plat_get_ticks_ms();
331
7201dc73 332 for (i = 0; i < in_dev_count; i++) {
333 if (in_devices[i].probed)
334 fds_hnds[count++] = in_devices[i].drv_fd_hnd;
335 }
3b13ec65 336
337 if (count == 0) {
338 /* don't deadlock, fail */
ba86b61e 339 lprintf("input: failed to find devices to read\n");
3b13ec65 340 exit(1);
341 }
342
0403eead 343 while (1)
7201dc73 344 {
0403eead 345 ret = plat_wait_event(fds_hnds, count, timeout_ms);
346 if (ret < 0)
347 break;
7201dc73 348
0403eead 349 for (i = 0; i < in_dev_count; i++) {
350 if (in_devices[i].drv_fd_hnd == ret) {
351 dev_id = i;
352 break;
353 }
7201dc73 354 }
355
0403eead 356 drv = &DRV(in_devices[dev_id].drv_id);
357 result = drv->update_keycode(in_devices[dev_id].drv_data, &is_down);
7201dc73 358
0403eead 359 /* update_keycode() might return -1 when some not interesting
360 * event happened, like sync event for evdev. */
361 if (result >= 0)
f484a9fe 362 break;
0403eead 363
364 if (timeout_ms >= 0) {
365 unsigned int ticks2 = plat_get_ticks_ms();
366 timeout_ms -= ticks2 - ticks;
367 ticks = ticks2;
368 if (timeout_ms <= 0)
369 break;
3427e573 370 }
371 }
7201dc73 372
217d08bc 373 if (result == -1)
0403eead 374 return -1;
217d08bc 375finish:
f484a9fe 376 /* keep track of menu key state, to allow mixing
b6820926 377 * in_update_keycode() and in_menu_wait_any() calls */
603c28b3 378 result_menu = drv->menu_translate(in_devices[dev_id].drv_data, result);
f484a9fe 379 if (result_menu != 0) {
380 if (is_down)
381 menu_key_state |= result_menu;
382 else
383 menu_key_state &= ~result_menu;
384 }
385
386 if (dev_id_out != NULL)
387 *dev_id_out = dev_id;
388 if (is_down_out != NULL)
389 *is_down_out = is_down;
3b13ec65 390 return result;
391}
392
b6820926 393/* same as above, only return bitfield of PBTN_* */
394int in_menu_wait_any(int timeout_ms)
3427e573 395{
f484a9fe 396 int keys_old = menu_key_state;
3427e573 397
398 while (1)
399 {
819ef025 400 int code, is_down = 0, dev_id = 0;
3427e573 401
f484a9fe 402 code = in_update_keycode(&dev_id, &is_down, timeout_ms);
217d08bc 403 if (code < 0)
3427e573 404 break;
603c28b3 405
406 if (keys_old != menu_key_state) {
407 menu_last_used_dev = dev_id;
408 break;
409 }
3427e573 410 }
411
f484a9fe 412 return menu_key_state;
3427e573 413}
414
b6820926 415/* wait for menu input, do autorepeat */
713c9224 416int in_menu_wait(int interesting, int autorep_delay_ms)
b6820926 417{
418 static int inp_prev = 0;
299acadc 419 static int repeats = 0;
9025b931 420 int ret, release = 0, wait = 450;
b6820926 421
299acadc 422 if (repeats)
713c9224 423 wait = autorep_delay_ms;
b6820926 424
299acadc 425 ret = in_menu_wait_any(wait);
426 if (ret == inp_prev)
427 repeats++;
b6820926 428
429 while (!(ret & interesting)) {
430 ret = in_menu_wait_any(-1);
431 release = 1;
432 }
433
299acadc 434 if (release || ret != inp_prev)
b6820926 435 repeats = 0;
299acadc 436
b6820926 437 inp_prev = ret;
438
299acadc 439 /* we don't need diagonals in menus */
b6820926 440 if ((ret & PBTN_UP) && (ret & PBTN_LEFT)) ret &= ~PBTN_LEFT;
441 if ((ret & PBTN_UP) && (ret & PBTN_RIGHT)) ret &= ~PBTN_RIGHT;
442 if ((ret & PBTN_DOWN) && (ret & PBTN_LEFT)) ret &= ~PBTN_LEFT;
443 if ((ret & PBTN_DOWN) && (ret & PBTN_RIGHT)) ret &= ~PBTN_RIGHT;
444
445 return ret;
446}
447
819ef025 448const int *in_get_dev_binds(int dev_id)
449{
450 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
451 return NULL;
452
453 return in_devices[dev_id].binds;
454}
455
456const int *in_get_dev_def_binds(int dev_id)
457{
819ef025 458 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
459 return NULL;
460
093b8a42 461 return in_devices[dev_id].binds + in_devices[dev_id].key_count * IN_BINDTYPE_COUNT;
819ef025 462}
463
603c28b3 464int in_get_config(int dev_id, int what, void *val)
819ef025 465{
603c28b3 466 int *ival = val;
467 in_dev_t *dev;
819ef025 468
603c28b3 469 if (dev_id < 0 || dev_id >= IN_MAX_DEVS || val == NULL)
470 return -1;
471
472 dev = &in_devices[dev_id];
9025b931 473 switch (what) {
603c28b3 474 case IN_CFG_BIND_COUNT:
475 *ival = dev->key_count;
476 break;
477 case IN_CFG_DOES_COMBOS:
478 *ival = dev->does_combos;
479 break;
480 case IN_CFG_BLOCKING:
481 case IN_CFG_KEY_NAMES:
482 return -1; /* not implemented */
483 default:
484 return DRV(dev->drv_id).get_config(dev->drv_data, what, ival);
485 }
486
487 return 0;
488}
489
490static int in_set_blocking(int is_blocking)
491{
492 int i, ret;
493
494 /* have_async_devs means we will have to do all reads async anyway.. */
495 if (!in_have_async_devs) {
496 for (i = 0; i < in_dev_count; i++) {
497 if (in_devices[i].probed)
498 DRV(in_devices[i].drv_id).set_config(in_devices[i].drv_data,
499 IN_CFG_BLOCKING, is_blocking);
500 }
9025b931 501 }
502
603c28b3 503 menu_key_state = 0;
504
505 /* flush events */
506 do {
507 ret = in_update_keycode(NULL, NULL, 0);
508 } while (ret >= 0);
509
9025b931 510 return 0;
819ef025 511}
512
603c28b3 513int in_set_config(int dev_id, int what, const void *val, int size)
514{
515 const int *ival = val;
516 in_dev_t *dev;
517
518 if (what == IN_CFG_BLOCKING)
519 return in_set_blocking(*ival);
520
521 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
522 return -1;
523
524 dev = &in_devices[dev_id];
525 if (what == IN_CFG_KEY_NAMES) {
526 const char * const *names = val;
527 int count = size / sizeof(names[0]);
528
529 if (count < dev->key_count) {
530 lprintf("input: set_key_names: not enough keys\n");
531 return -1;
532 }
533
534 dev->key_names = names;
535 return 0;
536 }
537
538 return DRV(dev->drv_id).set_config(dev->drv_data, what, *ival);
539}
540
713c9224 541const char *in_get_dev_name(int dev_id, int must_be_active, int skip_pfix)
819ef025 542{
713c9224 543 const char *name, *tmp;
544
819ef025 545 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
546 return NULL;
547
b6820926 548 if (must_be_active && !in_devices[dev_id].probed)
549 return NULL;
713c9224 550
551 name = in_devices[dev_id].name;
8c2a3661 552 if (name == NULL || !skip_pfix)
553 return name;
027c898c 554
8c2a3661 555 /* skip prefix */
713c9224 556 tmp = strchr(name, ':');
557 if (tmp != NULL)
558 name = tmp + 1;
559
560 return name;
819ef025 561}
562
603c28b3 563int in_name_to_id(const char *dev_name)
564{
565 int i;
566
567 for (i = 0; i < in_dev_count; i++)
568 if (strcmp(dev_name, in_devices[i].name) == 0)
569 break;
570
571 if (i >= in_dev_count) {
572 lprintf("input: in_name_to_id: no such device: %s\n", dev_name);
573 return -1;
574 }
575
576 return i;
577}
578
f484a9fe 579/* never returns NULL */
3427e573 580const char *in_get_key_name(int dev_id, int keycode)
581{
603c28b3 582 const char *name = NULL;
f484a9fe 583 static char xname[16];
603c28b3 584 in_dev_t *dev;
f484a9fe 585
991473ad 586 if (dev_id < 0) /* want last used dev? */
587 dev_id = menu_last_used_dev;
588
3427e573 589 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
590 return "Unkn0";
819ef025 591
603c28b3 592 dev = &in_devices[dev_id];
991473ad 593 if (keycode < 0) /* want name for menu key? */
603c28b3 594 keycode = DRV(dev->drv_id).menu_translate(dev->drv_data, keycode);
595
596 if (dev->key_names != NULL && 0 <= keycode && keycode < dev->key_count)
597 name = dev->key_names[keycode];
598 if (name != NULL)
599 return name;
991473ad 600
603c28b3 601 name = DRV(dev->drv_id).get_key_name(keycode);
f484a9fe 602 if (name != NULL)
603 return name;
604
605 /* assume scancode */
606 if ((keycode >= '0' && keycode <= '9') || (keycode >= 'a' && keycode <= 'z')
607 || (keycode >= 'A' && keycode <= 'Z'))
608 sprintf(xname, "%c", keycode);
609 else
610 sprintf(xname, "\\x%02X", keycode);
611 return xname;
612}
613
37807164 614int in_bind_key(int dev_id, int keycode, int mask, int bind_type, int force_unbind)
f484a9fe 615{
616 int ret, count;
617 in_dev_t *dev;
618
093b8a42 619 if (dev_id < 0 || dev_id >= IN_MAX_DEVS || bind_type >= IN_BINDTYPE_COUNT)
f484a9fe 620 return -1;
093b8a42 621
f484a9fe 622 dev = &in_devices[dev_id];
093b8a42 623 count = dev->key_count;
f484a9fe 624
625 if (dev->binds == NULL) {
626 if (force_unbind)
627 return 0;
093b8a42 628 dev->binds = in_alloc_binds(dev->drv_id, count);
f484a9fe 629 if (dev->binds == NULL)
630 return -1;
631 }
632
f484a9fe 633 if (keycode < 0 || keycode >= count)
634 return -1;
635
636 if (force_unbind)
093b8a42 637 dev->binds[IN_BIND_OFFS(keycode, bind_type)] &= ~mask;
f484a9fe 638 else
093b8a42 639 dev->binds[IN_BIND_OFFS(keycode, bind_type)] ^= mask;
f484a9fe 640
093b8a42 641 ret = DRV(dev->drv_id).clean_binds(dev->drv_data, dev->binds,
642 dev->binds + count * IN_BINDTYPE_COUNT);
f484a9fe 643 if (ret == 0) {
644 free(dev->binds);
645 dev->binds = NULL;
646 }
647
648 return 0;
819ef025 649}
650
37807164 651void in_unbind_all(int dev_id, int act_mask, int bind_type)
652{
653 int i, count;
654 in_dev_t *dev;
655
656 if (dev_id < 0 || dev_id >= IN_MAX_DEVS || bind_type >= IN_BINDTYPE_COUNT)
657 return;
658
659 dev = &in_devices[dev_id];
660 count = dev->key_count;
661
662 if (dev->binds == NULL)
663 return;
664
665 for (i = 0; i < count; i++)
666 dev->binds[IN_BIND_OFFS(i, bind_type)] &= ~act_mask;
667}
668
819ef025 669/* returns device id, or -1 on error */
670int in_config_parse_dev(const char *name)
671{
672 int drv_id = -1, i;
673
674 for (i = 0; i < IN_DRVID_COUNT; i++) {
675 int len = strlen(in_drivers[i].prefix);
676 if (strncmp(name, in_drivers[i].prefix, len) == 0) {
677 drv_id = i;
678 break;
679 }
680 }
681
682 if (drv_id < 0) {
ba86b61e 683 lprintf("input: missing driver for %s\n", name);
819ef025 684 return -1;
685 }
686
687 for (i = 0; i < in_dev_count; i++)
688 {
689 if (in_devices[i].name == NULL)
690 continue;
691 if (strcmp(in_devices[i].name, name) == 0)
692 return i;
693 }
694
695 if (i >= IN_MAX_DEVS)
696 {
697 /* try to find unused device */
698 for (i = 0; i < IN_MAX_DEVS; i++)
699 if (in_devices[i].name == NULL) break;
700 if (i >= IN_MAX_DEVS) {
ba86b61e 701 lprintf("input: too many devices, can't add %s\n", name);
819ef025 702 return -1;
703 }
704 }
705
706 memset(&in_devices[i], 0, sizeof(in_devices[i]));
707
708 in_devices[i].name = strdup(name);
709 if (in_devices[i].name == NULL)
710 return -1;
711
093b8a42 712 in_devices[i].key_count = DRV(drv_id).get_bind_count();
713 in_devices[i].drv_id = drv_id;
714
819ef025 715 if (i + 1 > in_dev_count)
716 in_dev_count = i + 1;
819ef025 717
718 return i;
719}
720
299acadc 721/*
722 * To reduce size of game specific configs, default binds are not saved.
723 * So we mark default binds in in_config_start(), override them in in_config_bind_key(),
724 * and restore whatever default binds are left in in_config_end().
725 */
819ef025 726void in_config_start(void)
727{
728 int i;
729
730 /* mark all default binds, so they get overwritten by func below */
731 for (i = 0; i < IN_MAX_DEVS; i++) {
732 int n, count, *binds, *def_binds;
733
f484a9fe 734 binds = in_devices[i].binds;
735 if (binds == NULL)
819ef025 736 continue;
737
093b8a42 738 count = in_devices[i].key_count;
739 def_binds = binds + count * IN_BINDTYPE_COUNT;
819ef025 740
093b8a42 741 for (n = 0; n < count * IN_BINDTYPE_COUNT; n++)
819ef025 742 if (binds[n] == def_binds[n])
743 binds[n] = -1;
744 }
745}
746
093b8a42 747int in_config_bind_key(int dev_id, const char *key, int acts, int bind_type)
819ef025 748{
f484a9fe 749 in_dev_t *dev;
093b8a42 750 int i, offs, kc;
819ef025 751
093b8a42 752 if (dev_id < 0 || dev_id >= IN_MAX_DEVS || bind_type >= IN_BINDTYPE_COUNT)
819ef025 753 return -1;
f484a9fe 754 dev = &in_devices[dev_id];
819ef025 755
f484a9fe 756 /* maybe a raw code? */
757 if (key[0] == '\\' && key[1] == 'x') {
758 char *p = NULL;
759 kc = (int)strtoul(key + 2, &p, 16);
760 if (p == NULL || *p != 0)
761 kc = -1;
762 }
763 else {
764 /* device specific key name */
765 if (dev->binds == NULL) {
093b8a42 766 dev->binds = in_alloc_binds(dev->drv_id, dev->key_count);
f484a9fe 767 if (dev->binds == NULL)
768 return -1;
769 in_config_start();
770 }
771
603c28b3 772 kc = -1;
773 if (dev->key_names != NULL) {
774 for (i = 0; i < dev->key_count; i++) {
775 const char *k = dev->key_names[i];
776 if (k != NULL && strcasecmp(k, key) == 0) {
777 kc = i;
778 break;
779 }
780 }
781 }
782
783 if (kc < 0)
784 kc = DRV(dev->drv_id).get_key_code(key);
f484a9fe 785 if (kc < 0 && strlen(key) == 1) {
786 /* assume scancode */
787 kc = key[0];
788 }
819ef025 789 }
790
093b8a42 791 if (kc < 0 || kc >= dev->key_count) {
ba86b61e 792 lprintf("input: bad key: %s\n", key);
819ef025 793 return -1;
3427e573 794 }
795
093b8a42 796 if (bind_type == IN_BINDTYPE_NONE) {
797 for (i = 0; i < IN_BINDTYPE_COUNT; i++)
798 dev->binds[IN_BIND_OFFS(kc, i)] = 0;
799 return 0;
800 }
819ef025 801
093b8a42 802 offs = IN_BIND_OFFS(kc, bind_type);
803 if (dev->binds[offs] == -1)
804 dev->binds[offs] = 0;
805 dev->binds[offs] |= acts;
819ef025 806 return 0;
807}
808
809void in_config_end(void)
810{
811 int i;
812
813 for (i = 0; i < IN_MAX_DEVS; i++) {
c5c73e2f 814 int n, t, ret, count, *binds, *def_binds;
f484a9fe 815 in_dev_t *dev = &in_devices[i];
819ef025 816
f484a9fe 817 if (dev->binds == NULL)
819ef025 818 continue;
819
093b8a42 820 count = dev->key_count;
f484a9fe 821 binds = dev->binds;
093b8a42 822 def_binds = binds + count * IN_BINDTYPE_COUNT;
819ef025 823
c5c73e2f 824 for (n = 0; n < count; n++) {
825 int is_default = 1;
826 for (t = 0; t < IN_BINDTYPE_COUNT; t++)
827 if (binds[IN_BIND_OFFS(n, t)] == -1)
828 binds[IN_BIND_OFFS(n, t)] = 0;
829 else
830 is_default = 0;
831
832 if (is_default)
833 for (t = 0; t < IN_BINDTYPE_COUNT; t++)
834 binds[IN_BIND_OFFS(n, t)] = def_binds[IN_BIND_OFFS(n, t)];
835 }
819ef025 836
f484a9fe 837 if (dev->drv_data == NULL)
819ef025 838 continue;
839
093b8a42 840 ret = DRV(dev->drv_id).clean_binds(dev->drv_data, binds, def_binds);
819ef025 841 if (ret == 0) {
842 /* no useable binds */
f484a9fe 843 free(dev->binds);
844 dev->binds = NULL;
819ef025 845 }
846 }
3427e573 847}
848
819ef025 849void in_debug_dump(void)
850{
851 int i;
852
ba86b61e 853 lprintf("# drv probed binds name\n");
819ef025 854 for (i = 0; i < IN_MAX_DEVS; i++) {
855 in_dev_t *d = &in_devices[i];
856 if (!d->probed && d->name == NULL && d->binds == NULL)
857 continue;
ba86b61e 858 lprintf("%d %3d %6c %5c %s\n", i, d->drv_id, d->probed ? 'y' : 'n',
819ef025 859 d->binds ? 'y' : 'n', d->name);
860 }
861}
862
863/* handlers for unknown/not_preset drivers */
864
865static void in_def_probe(void) {}
866static void in_def_free(void *drv_data) {}
867static int in_def_get_bind_count(void) { return 0; }
868static void in_def_get_def_binds(int *binds) {}
093b8a42 869static int in_def_clean_binds(void *drv_data, int *b, int *db) { return 0; }
603c28b3 870static int in_def_get_config(void *drv_data, int what, int *val) { return -1; }
871static int in_def_set_config(void *drv_data, int what, int val) { return -1; }
7201dc73 872static int in_def_update_keycode(void *drv_data, int *is_down) { return 0; }
603c28b3 873static int in_def_menu_translate(void *drv_data, int keycode) { return keycode; }
874static int in_def_get_key_code(const char *key_name) { return -1; }
819ef025 875static const char *in_def_get_key_name(int keycode) { return NULL; }
876
e1f99f2b 877void in_init(void)
878{
819ef025 879 int i;
880
881 memset(in_drivers, 0, sizeof(in_drivers));
e1f99f2b 882 memset(in_devices, 0, sizeof(in_devices));
883 in_dev_count = 0;
819ef025 884
885 for (i = 0; i < IN_DRVID_COUNT; i++) {
886 in_drivers[i].prefix = "none:";
887 in_drivers[i].probe = in_def_probe;
888 in_drivers[i].free = in_def_free;
889 in_drivers[i].get_bind_count = in_def_get_bind_count;
890 in_drivers[i].get_def_binds = in_def_get_def_binds;
891 in_drivers[i].clean_binds = in_def_clean_binds;
603c28b3 892 in_drivers[i].get_config = in_def_get_config;
893 in_drivers[i].set_config = in_def_set_config;
7201dc73 894 in_drivers[i].update_keycode = in_def_update_keycode;
819ef025 895 in_drivers[i].menu_translate = in_def_menu_translate;
896 in_drivers[i].get_key_code = in_def_get_key_code;
897 in_drivers[i].get_key_name = in_def_get_key_name;
898 }
899
217d08bc 900#ifdef IN_GP2X
901 in_gp2x_init(&in_drivers[IN_DRVID_GP2X]);
902#endif
819ef025 903#ifdef IN_EVDEV
904 in_evdev_init(&in_drivers[IN_DRVID_EVDEV]);
905#endif
8a011923 906#ifdef IN_VK
823b9004 907 in_vk_init(&in_drivers[IN_DRVID_VK]);
8a011923 908#endif
e1f99f2b 909}
910
819ef025 911#if 0
e1f99f2b 912int main(void)
913{
3b13ec65 914 int ret;
915
e1f99f2b 916 in_init();
917 in_probe();
918
3427e573 919 in_set_blocking(1);
920
921#if 1
922 while (1) {
923 int dev = 0, down;
924 ret = in_update_keycode(&dev, &down);
ba86b61e 925 lprintf("#%i: %i %i (%s)\n", dev, down, ret, in_get_key_name(dev, ret));
3427e573 926 }
927#else
e1f99f2b 928 while (1) {
b6820926 929 ret = in_menu_wait_any();
ba86b61e 930 lprintf("%08x\n", ret);
e1f99f2b 931 }
3427e573 932#endif
e1f99f2b 933
934 return 0;
935}
819ef025 936#endif