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