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