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