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