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