update license in source code itself
[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_GP2X
21 #error needs update: in_gp2x_init in_gp2x_update
22 #include "../gp2x/in_gp2x.h"
23 #endif
24 #ifdef IN_VK
25 #error needs update: in_vk_init in_vk_update
26 #include "../win32/in_vk.h"
27 #endif
28
29 typedef struct
30 {
31         int drv_id;
32         int drv_fd_hnd;
33         void *drv_data;
34         char *name;
35         int key_count;
36         int *binds;     /* total = key_count * bindtypes * 2 */
37         const char * const *key_names;
38         unsigned int probed:1;
39         unsigned int does_combos:1;
40 } in_dev_t;
41
42 static in_drv_t *in_drivers;
43 static in_dev_t in_devices[IN_MAX_DEVS];
44 static int in_driver_count = 0;
45 static int in_dev_count = 0;            /* probed + bind devices */
46 static int in_have_async_devs = 0;
47 static int in_probe_dev_id;
48 static int menu_key_state = 0;
49 static int menu_last_used_dev = 0;
50
51 #define DRV(id) in_drivers[id]
52
53
54 static int *in_alloc_binds(int drv_id, int key_count)
55 {
56         const struct in_default_bind *defbinds;
57         int *binds;
58         int i;
59
60         binds = calloc(key_count * IN_BINDTYPE_COUNT * 2, sizeof(binds[0]));
61         if (binds == NULL)
62                 return NULL;
63
64         defbinds = DRV(drv_id).defbinds;
65         if (defbinds != NULL) {
66                 for (i = 0; ; i++) {
67                         if (defbinds[i].bit == 0 && defbinds[i].code == 0)
68                                 break;
69                         binds[IN_BIND_OFFS(defbinds[i].code, defbinds[i].btype)] =
70                                 1 << defbinds[i].bit;
71                 }
72
73                 /* always have a copy of defbinds */
74                 memcpy(binds + key_count * IN_BINDTYPE_COUNT, binds,
75                         sizeof(binds[0]) * key_count * IN_BINDTYPE_COUNT);
76         }
77
78         return binds;
79 }
80
81 static void in_unprobe(in_dev_t *dev)
82 {
83         if (dev->probed)
84                 DRV(dev->drv_id).free(dev->drv_data);
85         dev->probed = 0;
86         dev->drv_data = NULL;
87 }
88
89 static void in_free(in_dev_t *dev)
90 {
91         in_unprobe(dev);
92         free(dev->name);
93         dev->name = NULL;
94         free(dev->binds);
95         dev->binds = NULL;
96 }
97
98 /* to be called by drivers
99  * async devices must set drv_fd_hnd to -1 */
100 void in_register(const char *nname, int drv_fd_hnd, void *drv_data,
101                 int key_count, const char * const *key_names, int combos)
102 {
103         int i, ret, dupe_count = 0, *binds;
104         char name[256], *name_end, *tmp;
105
106         strncpy(name, nname, sizeof(name));
107         name[sizeof(name)-12] = 0;
108         name_end = name + strlen(name);
109
110         for (i = 0; i < in_dev_count; i++)
111         {
112                 if (in_devices[i].name == NULL)
113                         continue;
114                 if (strcmp(in_devices[i].name, name) == 0)
115                 {
116                         if (in_devices[i].probed) {
117                                 dupe_count++;
118                                 sprintf(name_end, " [%d]", dupe_count);
119                                 continue;
120                         }
121                         goto update;
122                 }
123         }
124
125         if (i >= IN_MAX_DEVS)
126         {
127                 /* try to find unused device */
128                 for (i = 0; i < IN_MAX_DEVS; i++)
129                         if (!in_devices[i].probed) break;
130                 if (i >= IN_MAX_DEVS) {
131                         lprintf("input: too many devices, can't add %s\n", name);
132                         return;
133                 }
134                 in_free(&in_devices[i]);
135         }
136
137         tmp = strdup(name);
138         if (tmp == NULL)
139                 return;
140
141         binds = in_alloc_binds(in_probe_dev_id, key_count);
142         if (binds == NULL) {
143                 free(tmp);
144                 return;
145         }
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);
154 update:
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 */
175 void 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
201 int 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
240 void 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();
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 */
280 int 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
293 static 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
301 int 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
311 static 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  */
348 int 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;
412 finish:
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_*  */
431 int in_menu_wait_any(char *charcode, int timeout_ms)
432 {
433         int keys_old = menu_key_state;
434
435         while (1)
436         {
437                 int code, is_down = 0, dev_id = 0;
438
439                 code = in_update_keycode(&dev_id, &is_down, charcode, timeout_ms);
440                 if (code < 0)
441                         break;
442
443                 if (keys_old != menu_key_state) {
444                         menu_last_used_dev = dev_id;
445                         break;
446                 }
447         }
448
449         return menu_key_state;
450 }
451
452 /* wait for menu input, do autorepeat */
453 int in_menu_wait(int interesting, char *charcode, int autorep_delay_ms)
454 {
455         static int inp_prev = 0;
456         static int repeats = 0;
457         int ret, release = 0, wait = 450;
458
459         if (repeats)
460                 wait = autorep_delay_ms;
461
462         ret = in_menu_wait_any(charcode, wait);
463         if (ret == inp_prev)
464                 repeats++;
465
466         while (!(ret & interesting)) {
467                 ret = in_menu_wait_any(charcode, -1);
468                 release = 1;
469         }
470
471         if (release || ret != inp_prev)
472                 repeats = 0;
473
474         inp_prev = ret;
475
476         /* we don't need diagonals in menus */
477         if ((ret & PBTN_UP)   && (ret & PBTN_LEFT))  ret &= ~PBTN_LEFT;
478         if ((ret & PBTN_UP)   && (ret & PBTN_RIGHT)) ret &= ~PBTN_RIGHT;
479         if ((ret & PBTN_DOWN) && (ret & PBTN_LEFT))  ret &= ~PBTN_LEFT;
480         if ((ret & PBTN_DOWN) && (ret & PBTN_RIGHT)) ret &= ~PBTN_RIGHT;
481
482         return ret;
483 }
484
485 const int *in_get_dev_binds(int dev_id)
486 {
487         in_dev_t *dev = get_dev(dev_id);
488
489         return dev ? dev->binds : NULL;
490 }
491
492 const int *in_get_dev_def_binds(int dev_id)
493 {
494         in_dev_t *dev = get_dev(dev_id);
495         if (dev == NULL)
496                 return NULL;
497
498         return dev->binds + dev->key_count * IN_BINDTYPE_COUNT;
499 }
500
501 int in_get_config(int dev_id, int what, void *val)
502 {
503         int *ival = val;
504         in_dev_t *dev;
505
506         dev = get_dev(dev_id);
507         if (dev == NULL || val == NULL)
508                 return -1;
509
510         switch (what) {
511         case IN_CFG_BIND_COUNT:
512                 *ival = dev->key_count;
513                 break;
514         case IN_CFG_DOES_COMBOS:
515                 *ival = dev->does_combos;
516                 break;
517         case IN_CFG_BLOCKING:
518         case IN_CFG_KEY_NAMES:
519                 return -1; /* not implemented */
520         default:
521                 return DRV(dev->drv_id).get_config(dev->drv_data, what, ival);
522         }
523
524         return 0;
525 }
526
527 static 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                                 DRV(in_devices[i].drv_id).set_config(in_devices[i].drv_data,
536                                                                      IN_CFG_BLOCKING, is_blocking);
537                 }
538         }
539
540         menu_key_state = 0;
541
542         /* flush events */
543         do {
544                 ret = in_update_keycode(NULL, NULL, NULL, 0);
545         } while (ret >= 0);
546
547         return 0;
548 }
549
550 int in_set_config(int dev_id, int what, const void *val, int size)
551 {
552         const char * const *names;
553         const int *ival = val;
554         in_dev_t *dev;
555         int count;
556
557         if (what == IN_CFG_BLOCKING)
558                 return in_set_blocking(*ival);
559
560         dev = get_dev(dev_id);
561         if (dev == NULL)
562                 return -1;
563
564         switch (what) {
565         case IN_CFG_KEY_NAMES:
566                 names = val;
567                 count = size / sizeof(names[0]);
568
569                 if (count < dev->key_count) {
570                         lprintf("input: set_key_names: not enough keys\n");
571                         return -1;
572                 }
573
574                 dev->key_names = names;
575                 return 0;
576         case IN_CFG_DEFAULT_DEV:
577                 /* just set last used dev, for now */
578                 menu_last_used_dev = dev_id;
579                 return 0;
580         default:
581                 break;
582         }
583
584         if (dev->probed)
585                 return DRV(dev->drv_id).set_config(dev->drv_data, what, *ival);
586
587         return -1;
588 }
589
590 const char *in_get_dev_name(int dev_id, int must_be_active, int skip_pfix)
591 {
592         const char *name, *tmp;
593         in_dev_t *dev;
594
595         dev = get_dev(dev_id);
596         if (dev == NULL)
597                 return NULL;
598
599         if (must_be_active && !dev->probed)
600                 return NULL;
601
602         name = dev->name;
603         if (name == NULL || !skip_pfix)
604                 return name;
605
606         /* skip prefix */
607         tmp = strchr(name, ':');
608         if (tmp != NULL)
609                 name = tmp + 1;
610
611         return name;
612 }
613
614 int in_name_to_id(const char *dev_name)
615 {
616         int i;
617
618         for (i = 0; i < in_dev_count; i++)
619                 if (strcmp(dev_name, in_devices[i].name) == 0)
620                         break;
621
622         if (i >= in_dev_count) {
623                 lprintf("input: in_name_to_id: no such device: %s\n", dev_name);
624                 return -1;
625         }
626
627         return i;
628 }
629
630 /* never returns NULL */
631 const char *in_get_key_name(int dev_id, int keycode)
632 {
633         const char *name = NULL;
634         static char xname[16];
635         in_drv_t *drv;
636         in_dev_t *dev;
637
638         if (dev_id < 0)         /* want last used dev? */
639                 dev_id = menu_last_used_dev;
640
641         dev = get_dev(dev_id);
642         if (dev == NULL)
643                 return "Unkn0";
644
645         drv = &DRV(dev->drv_id);
646         if (keycode < 0)        /* want name for menu key? */
647                 keycode = drv->menu_translate(dev->drv_data, keycode, NULL);
648
649         if (dev->key_names != NULL && 0 <= keycode && keycode < dev->key_count)
650                 name = dev->key_names[keycode];
651         if (name != NULL)
652                 return name;
653
654         if (drv->get_key_name != NULL)
655                 name = drv->get_key_name(keycode);
656         if (name != NULL)
657                 return name;
658
659         /* assume scancode */
660         if ((keycode >= '0' && keycode <= '9') || (keycode >= 'a' && keycode <= 'z')
661                         || (keycode >= 'A' && keycode <= 'Z'))
662                 sprintf(xname, "%c", keycode);
663         else
664                 sprintf(xname, "\\x%02X", keycode);
665         return xname;
666 }
667
668 int in_get_key_code(int dev_id, const char *key_name)
669 {
670         in_dev_t *dev;
671         int i;
672
673         if (dev_id < 0)         /* want last used dev? */
674                 dev_id = menu_last_used_dev;
675
676         dev = get_dev(dev_id);
677         if (dev == NULL)
678                 return -1;
679
680         if (dev->key_names == NULL)
681                 return -1;
682
683         for (i = 0; i < dev->key_count; i++)
684                 if (dev->key_names[i] && strcasecmp(dev->key_names[i], key_name) == 0)
685                         return i;
686
687         return -1;
688 }
689
690 int in_bind_key(int dev_id, int keycode, int mask, int bind_type, int force_unbind)
691 {
692         int ret, count;
693         in_dev_t *dev;
694
695         dev = get_dev(dev_id);
696         if (dev == NULL || bind_type >= IN_BINDTYPE_COUNT)
697                 return -1;
698
699         count = dev->key_count;
700
701         if (dev->binds == NULL) {
702                 if (force_unbind)
703                         return 0;
704                 dev->binds = in_alloc_binds(dev->drv_id, count);
705                 if (dev->binds == NULL)
706                         return -1;
707         }
708
709         if (keycode < 0 || keycode >= count)
710                 return -1;
711         
712         if (force_unbind)
713                 dev->binds[IN_BIND_OFFS(keycode, bind_type)] &= ~mask;
714         else
715                 dev->binds[IN_BIND_OFFS(keycode, bind_type)] ^=  mask;
716         
717         ret = DRV(dev->drv_id).clean_binds(dev->drv_data, dev->binds,
718                                 dev->binds + count * IN_BINDTYPE_COUNT);
719         if (ret == 0) {
720                 free(dev->binds);
721                 dev->binds = NULL;
722         }
723
724         return 0;
725 }
726
727 /*
728  * Unbind act_mask on binds with type bind_type
729  * - if dev_id_ < 0, affects all devices
730  *   else only affects dev_id_
731  * - if act_mask == -1, unbind all keys
732  *   else only actions in mask
733  */
734 void in_unbind_all(int dev_id_, int act_mask, int bind_type)
735 {
736         int dev_id = 0, dev_last = IN_MAX_DEVS - 1;
737         int i, count;
738         in_dev_t *dev;
739
740         if (dev_id_ >= 0)
741                 dev_id = dev_last = dev_id_;
742
743         if (bind_type >= IN_BINDTYPE_COUNT)
744                 return;
745
746         for (; dev_id <= dev_last; dev_id++) {
747                 dev = &in_devices[dev_id];
748                 count = dev->key_count;
749
750                 if (dev->binds == NULL)
751                         continue;
752
753                 if (act_mask != -1) {
754                         for (i = 0; i < count; i++)
755                                 dev->binds[IN_BIND_OFFS(i, bind_type)] &= ~act_mask;
756                 }
757                 else
758                         memset(dev->binds, 0, sizeof(dev->binds[0]) * count * IN_BINDTYPE_COUNT);
759         }
760 }
761
762 /* returns device id, or -1 on error */
763 int in_config_parse_dev(const char *name)
764 {
765         int drv_id = -1, i;
766
767         for (i = 0; i < in_driver_count; i++) {
768                 int len = strlen(in_drivers[i].prefix);
769                 if (strncmp(name, in_drivers[i].prefix, len) == 0) {
770                         drv_id = i;
771                         break;
772                 }
773         }
774
775         if (drv_id < 0) {
776                 lprintf("input: missing driver for %s\n", name);
777                 return -1;
778         }
779
780         for (i = 0; i < in_dev_count; i++)
781         {
782                 if (in_devices[i].name == NULL)
783                         continue;
784                 if (strcmp(in_devices[i].name, name) == 0)
785                         return i;
786         }
787
788         if (i >= IN_MAX_DEVS)
789         {
790                 /* try to find unused device */
791                 for (i = 0; i < IN_MAX_DEVS; i++)
792                         if (in_devices[i].name == NULL) break;
793                 if (i >= IN_MAX_DEVS) {
794                         lprintf("input: too many devices, can't add %s\n", name);
795                         return -1;
796                 }
797         }
798
799         memset(&in_devices[i], 0, sizeof(in_devices[i]));
800
801         in_devices[i].name = strdup(name);
802         if (in_devices[i].name == NULL)
803                 return -1;
804
805         in_devices[i].key_names = DRV(drv_id).get_key_names(&in_devices[i].key_count);
806         in_devices[i].drv_id = drv_id;
807
808         if (i + 1 > in_dev_count)
809                 in_dev_count = i + 1;
810
811         return i;
812 }
813
814 int in_config_bind_key(int dev_id, const char *key, int acts, int bind_type)
815 {
816         in_dev_t *dev;
817         int i, offs, kc;
818
819         dev = get_dev(dev_id);
820         if (dev == NULL || bind_type >= IN_BINDTYPE_COUNT)
821                 return -1;
822
823         /* maybe a raw code? */
824         if (key[0] == '\\' && key[1] == 'x') {
825                 char *p = NULL;
826                 kc = (int)strtoul(key + 2, &p, 16);
827                 if (p == NULL || *p != 0)
828                         kc = -1;
829         }
830         else {
831                 /* device specific key name */
832                 if (dev->binds == NULL) {
833                         dev->binds = in_alloc_binds(dev->drv_id, dev->key_count);
834                         if (dev->binds == NULL)
835                                 return -1;
836                 }
837
838                 kc = -1;
839                 if (dev->key_names != NULL) {
840                         for (i = 0; i < dev->key_count; i++) {
841                                 const char *k = dev->key_names[i];
842                                 if (k != NULL && strcasecmp(k, key) == 0) {
843                                         kc = i;
844                                         break;
845                                 }
846                         }
847                 }
848
849                 if (kc < 0)
850                         kc = DRV(dev->drv_id).get_key_code(key);
851                 if (kc < 0 && strlen(key) == 1) {
852                         /* assume scancode */
853                         kc = key[0];
854                 }
855         }
856
857         if (kc < 0 || kc >= dev->key_count) {
858                 lprintf("input: bad key: %s\n", key);
859                 return -1;
860         }
861
862         if (bind_type == IN_BINDTYPE_NONE) {
863                 for (i = 0; i < IN_BINDTYPE_COUNT; i++)
864                         dev->binds[IN_BIND_OFFS(kc, i)] = 0;
865                 return 0;
866         }
867
868         offs = IN_BIND_OFFS(kc, bind_type);
869         if (dev->binds[offs] == -1)
870                 dev->binds[offs] = 0;
871         dev->binds[offs] |= acts;
872         return 0;
873 }
874
875 void in_clean_binds(void)
876 {
877         int i;
878
879         for (i = 0; i < IN_MAX_DEVS; i++) {
880                 int ret, count, *binds, *def_binds;
881                 in_dev_t *dev = &in_devices[i];
882
883                 if (dev->binds == NULL || dev->drv_data == NULL)
884                         continue;
885
886                 count = dev->key_count;
887                 binds = dev->binds;
888                 def_binds = binds + count * IN_BINDTYPE_COUNT;
889
890                 ret = DRV(dev->drv_id).clean_binds(dev->drv_data, binds, def_binds);
891                 if (ret == 0) {
892                         /* no useable binds */
893                         free(dev->binds);
894                         dev->binds = NULL;
895                 }
896         }
897 }
898
899 void in_debug_dump(void)
900 {
901         int i;
902
903         lprintf("# drv probed binds name\n");
904         for (i = 0; i < IN_MAX_DEVS; i++) {
905                 in_dev_t *d = &in_devices[i];
906                 if (!d->probed && d->name == NULL && d->binds == NULL)
907                         continue;
908                 lprintf("%d %3d %6c %5c %s\n", i, d->drv_id, d->probed ? 'y' : 'n',
909                         d->binds ? 'y' : 'n', d->name);
910         }
911 }
912
913 /* stubs for drivers that choose not to implement something */
914
915 static void in_def_free(void *drv_data) {}
916 static int  in_def_clean_binds(void *drv_data, int *b, int *db) { return 1; }
917 static int  in_def_get_config(void *drv_data, int what, int *val) { return -1; }
918 static int  in_def_set_config(void *drv_data, int what, int val) { return -1; }
919 static int  in_def_update_analog(void *drv_data, int axis_id, int *result) { return -1; }
920 static int  in_def_update_keycode(void *drv_data, int *is_down) { return 0; }
921 static int  in_def_menu_translate(void *drv_data, int keycode, char *ccode) { return 0; }
922 static int  in_def_get_key_code(const char *key_name) { return -1; }
923 static const char *in_def_get_key_name(int keycode) { return NULL; }
924
925 #define CHECK_ADD_STUB(d, f) \
926         if (d.f == NULL) d.f = in_def_##f
927
928 /* to be called by drivers */
929 int in_register_driver(const in_drv_t *drv, const struct in_default_bind *defbinds)
930 {
931         int count_new = in_driver_count + 1;
932         in_drv_t *new_drivers;
933
934         new_drivers = realloc(in_drivers, count_new * sizeof(in_drivers[0]));
935         if (new_drivers == NULL) {
936                 lprintf("input: in_register_driver OOM\n");
937                 return -1;
938         }
939
940         memcpy(&new_drivers[in_driver_count], drv, sizeof(new_drivers[0]));
941
942         CHECK_ADD_STUB(new_drivers[in_driver_count], free);
943         CHECK_ADD_STUB(new_drivers[in_driver_count], clean_binds);
944         CHECK_ADD_STUB(new_drivers[in_driver_count], get_config);
945         CHECK_ADD_STUB(new_drivers[in_driver_count], set_config);
946         CHECK_ADD_STUB(new_drivers[in_driver_count], update_analog);
947         CHECK_ADD_STUB(new_drivers[in_driver_count], update_keycode);
948         CHECK_ADD_STUB(new_drivers[in_driver_count], menu_translate);
949         CHECK_ADD_STUB(new_drivers[in_driver_count], get_key_code);
950         CHECK_ADD_STUB(new_drivers[in_driver_count], get_key_name);
951         if (defbinds != NULL)
952                 new_drivers[in_driver_count].defbinds = defbinds;
953         in_drivers = new_drivers;
954         in_driver_count = count_new;
955
956         return 0;
957 }
958
959 void in_init(void)
960 {
961         in_drivers = NULL;
962         memset(in_devices, 0, sizeof(in_devices));
963         in_driver_count = 0;
964         in_dev_count = 0;
965 }
966
967 #if 0
968 int main(void)
969 {
970         int ret;
971
972         in_init();
973         in_probe();
974
975         in_set_blocking(1);
976
977 #if 1
978         while (1) {
979                 int dev = 0, down;
980                 ret = in_update_keycode(&dev, &down);
981                 lprintf("#%i: %i %i (%s)\n", dev, down, ret, in_get_key_name(dev, ret));
982         }
983 #else
984         while (1) {
985                 ret = in_menu_wait_any();
986                 lprintf("%08x\n", ret);
987         }
988 #endif
989
990         return 0;
991 }
992 #endif