GP2X updates
[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].btype == 0
64                             && defbinds[i].bit == 0)
65                                 break;
66                         binds[IN_BIND_OFFS(defbinds[i].code, defbinds[i].btype)] =
67                                 1 << defbinds[i].bit;
68                 }
69
70                 /* always have a copy of defbinds */
71                 memcpy(binds + key_count * IN_BINDTYPE_COUNT, binds,
72                         sizeof(binds[0]) * key_count * IN_BINDTYPE_COUNT);
73         }
74
75         return binds;
76 }
77
78 static 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
86 static 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 */
97 void 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         in_devices[i].name = tmp;
145         in_devices[i].binds = binds;
146         in_devices[i].key_count = key_count;
147         if (i + 1 > in_dev_count)
148                 in_dev_count = i + 1;
149
150         lprintf("input: new device #%d \"%s\"\n", i, name);
151 update:
152         in_devices[i].probed = 1;
153         in_devices[i].does_combos = combos;
154         in_devices[i].drv_id = in_probe_dev_id;
155         in_devices[i].drv_fd_hnd = drv_fd_hnd;
156         in_devices[i].key_names = key_names;
157         in_devices[i].drv_data = drv_data;
158
159         if (in_devices[i].binds != NULL) {
160                 ret = DRV(in_probe_dev_id).clean_binds(drv_data, in_devices[i].binds,
161                         in_devices[i].binds + key_count * IN_BINDTYPE_COUNT);
162                 if (ret == 0) {
163                         /* no useable binds */
164                         free(in_devices[i].binds);
165                         in_devices[i].binds = NULL;
166                 }
167         }
168 }
169
170 /* key combo handling, to be called by drivers that support it.
171  * Only care about IN_BINDTYPE_EMU */
172 void in_combos_find(const int *binds, int last_key, int *combo_keys, int *combo_acts)
173 {
174         int act, u;
175
176         *combo_keys = *combo_acts = 0;
177         for (act = 0; act < sizeof(binds[0]) * 8; act++)
178         {
179                 int keyc = 0;
180                 for (u = 0; u <= last_key; u++)
181                         if (binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)] & (1 << act))
182                                 keyc++;
183
184                 if (keyc > 1)
185                 {
186                         // loop again and mark those keys and actions as combo
187                         for (u = 0; u <= last_key; u++)
188                         {
189                                 if (binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)] & (1 << act)) {
190                                         *combo_keys |= 1 << u;
191                                         *combo_acts |= 1 << act;
192                                 }
193                         }
194                 }
195         }
196 }
197
198 int in_combos_do(int keys, const int *binds, int last_key, int combo_keys, int combo_acts)
199 {
200         int i, ret = 0;
201
202         for (i = 0; i <= last_key; i++)
203         {
204                 int acts, acts_c, u;
205
206                 if (!(keys & (1 << i)))
207                         continue;
208
209                 acts = binds[IN_BIND_OFFS(i, IN_BINDTYPE_EMU)];
210                 if (!acts)
211                         continue;
212
213                 if (!(combo_keys & (1 << i))) {
214                         ret |= acts;
215                         continue;
216                 }
217
218                 acts_c = acts & combo_acts;
219                 u = last_key;
220                 if (acts_c) {
221                         // let's try to find the other one
222                         for (u = i + 1; u <= last_key; u++)
223                                 if ( (keys & (1 << u)) && (binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)] & acts_c) ) {
224                                         ret |= acts_c & binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)];
225                                         keys &= ~((1 << i) | (1 << u));
226                                         break;
227                                 }
228                 }
229                 // add non-combo actions if combo ones were not found
230                 if (u >= last_key)
231                         ret |= acts & ~combo_acts;
232         }
233
234         return ret;
235 }
236
237 void in_probe(void)
238 {
239         int i;
240
241         in_have_async_devs = 0;
242         menu_key_state = 0;
243         menu_last_used_dev = 0;
244
245         for (i = 0; i < in_dev_count; i++)
246                 in_unprobe(&in_devices[i]);
247
248         for (i = 0; i < in_driver_count; i++) {
249                 in_probe_dev_id = i;
250                 in_drivers[i].probe();
251         }
252
253         /* get rid of devs without binds and probes */
254         for (i = 0; i < in_dev_count; i++) {
255                 if (!in_devices[i].probed && in_devices[i].binds == NULL) {
256                         in_dev_count--;
257                         if (i < in_dev_count) {
258                                 free(in_devices[i].name);
259                                 memmove(&in_devices[i], &in_devices[i+1],
260                                         (in_dev_count - i) * sizeof(in_devices[0]));
261                         }
262
263                         continue;
264                 }
265
266                 if (in_devices[i].probed && in_devices[i].drv_fd_hnd == -1)
267                         in_have_async_devs = 1;
268         }
269
270         if (in_have_async_devs)
271                 lprintf("input: async-only devices detected..\n");
272
273         in_debug_dump();
274 }
275
276 /* async update */
277 int in_update(int *result)
278 {
279         int i, ret = 0;
280
281         for (i = 0; i < in_dev_count; i++) {
282                 in_dev_t *dev = &in_devices[i];
283                 if (dev->probed && dev->binds != NULL)
284                         ret |= DRV(dev->drv_id).update(dev->drv_data, dev->binds, result);
285         }
286
287         return ret;
288 }
289
290 static in_dev_t *get_dev(int dev_id)
291 {
292         if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
293                 return NULL;
294
295         return &in_devices[dev_id];
296 }
297
298 int in_update_analog(int dev_id, int axis_id, int *result)
299 {
300         in_dev_t *dev = get_dev(dev_id);
301
302         if (dev == NULL || !dev->probed)
303                 return -1;
304
305         return DRV(dev->drv_id).update_analog(dev->drv_data, axis_id, result);
306 }
307
308 static int in_update_kc_async(int *dev_id_out, int *is_down_out, int timeout_ms)
309 {
310         int i, is_down, result;
311         unsigned int ticks;
312
313         ticks = plat_get_ticks_ms();
314
315         while (1)
316         {
317                 for (i = 0; i < in_dev_count; i++) {
318                         in_dev_t *d = &in_devices[i];
319                         if (!d->probed)
320                                 continue;
321
322                         result = DRV(d->drv_id).update_keycode(d->drv_data, &is_down);
323                         if (result == -1)
324                                 continue;
325
326                         if (dev_id_out)
327                                 *dev_id_out = i;
328                         if (is_down_out)
329                                 *is_down_out = is_down;
330                         return result;
331                 }
332
333                 if (timeout_ms >= 0 && (int)(plat_get_ticks_ms() - ticks) > timeout_ms)
334                         break;
335
336                 plat_sleep_ms(10);
337         }
338
339         return -1;
340 }
341
342 /* 
343  * wait for a press, always return some keycode or -1 on timeout or error
344  */
345 int in_update_keycode(int *dev_id_out, int *is_down_out, char *charcode, int timeout_ms)
346 {
347         int result = -1, dev_id = 0, is_down, result_menu;
348         int fds_hnds[IN_MAX_DEVS];
349         int i, ret, count = 0;
350         in_drv_t *drv = NULL;
351         unsigned int ticks;
352
353         if (in_have_async_devs) {
354                 result = in_update_kc_async(&dev_id, &is_down, timeout_ms);
355                 if (result == -1)
356                         return -1;
357                 drv = &DRV(in_devices[dev_id].drv_id);
358                 goto finish;
359         }
360
361         ticks = plat_get_ticks_ms();
362
363         for (i = 0; i < in_dev_count; i++) {
364                 if (in_devices[i].probed)
365                         fds_hnds[count++] = in_devices[i].drv_fd_hnd;
366         }
367
368         if (count == 0) {
369                 /* don't deadlock, fail */
370                 lprintf("input: failed to find devices to read\n");
371                 exit(1);
372         }
373
374         while (1)
375         {
376                 ret = plat_wait_event(fds_hnds, count, timeout_ms);
377                 if (ret < 0)
378                         break;
379
380                 for (i = 0; i < in_dev_count; i++) {
381                         if (in_devices[i].drv_fd_hnd == ret) {
382                                 dev_id = i;
383                                 break;
384                         }
385                 }
386
387                 drv = &DRV(in_devices[dev_id].drv_id);
388                 result = drv->update_keycode(in_devices[dev_id].drv_data, &is_down);
389                 if (result >= 0)
390                         break;
391
392                 if (result == -2) {
393                         lprintf("input: \"%s\" errored out, removing.\n", in_devices[dev_id].name);
394                         in_unprobe(&in_devices[dev_id]);
395                         break;
396                 }
397
398                 if (timeout_ms >= 0) {
399                         unsigned int ticks2 = plat_get_ticks_ms();
400                         timeout_ms -= ticks2 - ticks;
401                         ticks = ticks2;
402                         if (timeout_ms <= 0)
403                                 break;
404                 }
405         }
406
407         if (result < 0)
408                 return -1;
409 finish:
410         /* keep track of menu key state, to allow mixing
411          * in_update_keycode() and in_menu_wait_any() calls */
412         result_menu = drv->menu_translate(in_devices[dev_id].drv_data, result, charcode);
413         if (result_menu != 0) {
414                 if (is_down)
415                         menu_key_state |=  result_menu;
416                 else
417                         menu_key_state &= ~result_menu;
418         }
419
420         if (dev_id_out != NULL)
421                 *dev_id_out = dev_id;
422         if (is_down_out != NULL)
423                 *is_down_out = is_down;
424         return result;
425 }
426
427 /* same as above, only return bitfield of PBTN_*  */
428 int in_menu_wait_any(char *charcode, int timeout_ms)
429 {
430         int keys_old = menu_key_state;
431
432         while (1)
433         {
434                 int code, is_down = 0, dev_id = 0;
435
436                 code = in_update_keycode(&dev_id, &is_down, charcode, timeout_ms);
437                 if (code < 0)
438                         break;
439
440                 if (keys_old != menu_key_state) {
441                         menu_last_used_dev = dev_id;
442                         break;
443                 }
444         }
445
446         return menu_key_state;
447 }
448
449 /* wait for menu input, do autorepeat */
450 int in_menu_wait(int interesting, char *charcode, int autorep_delay_ms)
451 {
452         static int inp_prev = 0;
453         static int repeats = 0;
454         int ret, release = 0, wait = 450;
455
456         if (repeats)
457                 wait = autorep_delay_ms;
458
459         ret = in_menu_wait_any(charcode, wait);
460         if (ret == inp_prev)
461                 repeats++;
462
463         while (!(ret & interesting)) {
464                 ret = in_menu_wait_any(charcode, -1);
465                 release = 1;
466         }
467
468         if (release || ret != inp_prev)
469                 repeats = 0;
470
471         inp_prev = ret;
472
473         /* we don't need diagonals in menus */
474         if ((ret & PBTN_UP)   && (ret & PBTN_LEFT))  ret &= ~PBTN_LEFT;
475         if ((ret & PBTN_UP)   && (ret & PBTN_RIGHT)) ret &= ~PBTN_RIGHT;
476         if ((ret & PBTN_DOWN) && (ret & PBTN_LEFT))  ret &= ~PBTN_LEFT;
477         if ((ret & PBTN_DOWN) && (ret & PBTN_RIGHT)) ret &= ~PBTN_RIGHT;
478
479         return ret;
480 }
481
482 const int *in_get_dev_binds(int dev_id)
483 {
484         in_dev_t *dev = get_dev(dev_id);
485
486         return dev ? dev->binds : NULL;
487 }
488
489 const int *in_get_dev_def_binds(int dev_id)
490 {
491         in_dev_t *dev = get_dev(dev_id);
492         if (dev == NULL)
493                 return NULL;
494
495         return dev->binds + dev->key_count * IN_BINDTYPE_COUNT;
496 }
497
498 int 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
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                                 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         menu_key_state = 0;
544
545         /* flush events */
546         do {
547                 ret = in_update_keycode(NULL, NULL, NULL, 0);
548         } while (ret >= 0);
549
550         return 0;
551 }
552
553 int 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
593 const 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
617 int 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 */
634 const 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
671 int 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
693 int 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  */
737 void 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 */
766 int 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", name);
798                         return -1;
799                 }
800         }
801
802         memset(&in_devices[i], 0, sizeof(in_devices[i]));
803
804         in_devices[i].name = strdup(name);
805         if (in_devices[i].name == NULL)
806                 return -1;
807
808         in_devices[i].key_names = DRV(drv_id).get_key_names(&in_devices[i].key_count);
809         in_devices[i].drv_id = drv_id;
810
811         if (i + 1 > in_dev_count)
812                 in_dev_count = i + 1;
813
814         return i;
815 }
816
817 int in_config_bind_key(int dev_id, const char *key, int acts, int bind_type)
818 {
819         in_dev_t *dev;
820         int i, offs, kc;
821
822         dev = get_dev(dev_id);
823         if (dev == NULL || bind_type >= IN_BINDTYPE_COUNT)
824                 return -1;
825
826         /* maybe a raw code? */
827         if (key[0] == '\\' && key[1] == 'x') {
828                 char *p = NULL;
829                 kc = (int)strtoul(key + 2, &p, 16);
830                 if (p == NULL || *p != 0)
831                         kc = -1;
832         }
833         else {
834                 /* device specific key name */
835                 if (dev->binds == NULL) {
836                         dev->binds = in_alloc_binds(dev->drv_id, dev->key_count);
837                         if (dev->binds == NULL)
838                                 return -1;
839                 }
840
841                 kc = -1;
842                 if (dev->key_names != NULL) {
843                         for (i = 0; i < dev->key_count; i++) {
844                                 const char *k = dev->key_names[i];
845                                 if (k != NULL && strcasecmp(k, key) == 0) {
846                                         kc = i;
847                                         break;
848                                 }
849                         }
850                 }
851
852                 if (kc < 0)
853                         kc = DRV(dev->drv_id).get_key_code(key);
854                 if (kc < 0 && strlen(key) == 1) {
855                         /* assume scancode */
856                         kc = key[0];
857                 }
858         }
859
860         if (kc < 0 || kc >= dev->key_count) {
861                 lprintf("input: bad key: %s\n", key);
862                 return -1;
863         }
864
865         if (bind_type == IN_BINDTYPE_NONE) {
866                 for (i = 0; i < IN_BINDTYPE_COUNT; i++)
867                         dev->binds[IN_BIND_OFFS(kc, i)] = 0;
868                 return 0;
869         }
870
871         offs = IN_BIND_OFFS(kc, bind_type);
872         if (dev->binds[offs] == -1)
873                 dev->binds[offs] = 0;
874         dev->binds[offs] |= acts;
875         return 0;
876 }
877
878 void in_clean_binds(void)
879 {
880         int i;
881
882         for (i = 0; i < IN_MAX_DEVS; i++) {
883                 int ret, count, *binds, *def_binds;
884                 in_dev_t *dev = &in_devices[i];
885
886                 if (dev->binds == NULL || !dev->probed)
887                         continue;
888
889                 count = dev->key_count;
890                 binds = dev->binds;
891                 def_binds = binds + count * IN_BINDTYPE_COUNT;
892
893                 ret = DRV(dev->drv_id).clean_binds(dev->drv_data, binds, def_binds);
894                 if (ret == 0) {
895                         /* no useable binds */
896                         free(dev->binds);
897                         dev->binds = NULL;
898                 }
899         }
900 }
901
902 void in_debug_dump(void)
903 {
904         int i;
905
906         lprintf("# drv probed binds name\n");
907         for (i = 0; i < IN_MAX_DEVS; i++) {
908                 in_dev_t *d = &in_devices[i];
909                 if (!d->probed && d->name == NULL && d->binds == NULL)
910                         continue;
911                 lprintf("%d %3d %6c %5c %s\n", i, d->drv_id, d->probed ? 'y' : 'n',
912                         d->binds ? 'y' : 'n', d->name);
913         }
914 }
915
916 /* stubs for drivers that choose not to implement something */
917
918 static void in_def_free(void *drv_data) {}
919 static int  in_def_clean_binds(void *drv_data, int *b, int *db) { return 1; }
920 static int  in_def_get_config(void *drv_data, int what, int *val) { return -1; }
921 static int  in_def_set_config(void *drv_data, int what, int val) { return -1; }
922 static int  in_def_update_analog(void *drv_data, int axis_id, int *result) { return -1; }
923 static int  in_def_update_keycode(void *drv_data, int *is_down) { return 0; }
924 static int  in_def_menu_translate(void *drv_data, int keycode, char *ccode) { return 0; }
925 static int  in_def_get_key_code(const char *key_name) { return -1; }
926 static const char *in_def_get_key_name(int keycode) { return NULL; }
927
928 #define CHECK_ADD_STUB(d, f) \
929         if (d.f == NULL) d.f = in_def_##f
930
931 /* to be called by drivers */
932 int in_register_driver(const in_drv_t *drv, const struct in_default_bind *defbinds)
933 {
934         int count_new = in_driver_count + 1;
935         in_drv_t *new_drivers;
936
937         new_drivers = realloc(in_drivers, count_new * sizeof(in_drivers[0]));
938         if (new_drivers == NULL) {
939                 lprintf("input: in_register_driver OOM\n");
940                 return -1;
941         }
942
943         memcpy(&new_drivers[in_driver_count], drv, sizeof(new_drivers[0]));
944
945         CHECK_ADD_STUB(new_drivers[in_driver_count], free);
946         CHECK_ADD_STUB(new_drivers[in_driver_count], clean_binds);
947         CHECK_ADD_STUB(new_drivers[in_driver_count], get_config);
948         CHECK_ADD_STUB(new_drivers[in_driver_count], set_config);
949         CHECK_ADD_STUB(new_drivers[in_driver_count], update_analog);
950         CHECK_ADD_STUB(new_drivers[in_driver_count], update_keycode);
951         CHECK_ADD_STUB(new_drivers[in_driver_count], menu_translate);
952         CHECK_ADD_STUB(new_drivers[in_driver_count], get_key_code);
953         CHECK_ADD_STUB(new_drivers[in_driver_count], get_key_name);
954         if (defbinds != NULL)
955                 new_drivers[in_driver_count].defbinds = defbinds;
956         in_drivers = new_drivers;
957         in_driver_count = count_new;
958
959         return 0;
960 }
961
962 void in_init(void)
963 {
964         in_drivers = NULL;
965         memset(in_devices, 0, sizeof(in_devices));
966         in_driver_count = 0;
967         in_dev_count = 0;
968 }
969
970 #if 0
971 int main(void)
972 {
973         int ret;
974
975         in_init();
976         in_probe();
977
978         in_set_blocking(1);
979
980 #if 1
981         while (1) {
982                 int dev = 0, down;
983                 ret = in_update_keycode(&dev, &down);
984                 lprintf("#%i: %i %i (%s)\n", dev, down, ret, in_get_key_name(dev, ret));
985         }
986 #else
987         while (1) {
988                 ret = in_menu_wait_any();
989                 lprintf("%08x\n", ret);
990         }
991 #endif
992
993         return 0;
994 }
995 #endif