gl: clear w, h on reinit
[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, *binds_d;
54         int i;
55
56         binds = calloc(key_count * IN_BINDTYPE_COUNT * 2, sizeof(binds[0]));
57         if (binds == NULL)
58                 return NULL;
59
60         binds_d = binds + key_count * IN_BINDTYPE_COUNT;
61
62         /* always have a copy of defbinds */
63         defbinds = DRV(drv_id).defbinds;
64         if (defbinds != NULL) {
65                 for (i = 0; ; i++) {
66                         if (defbinds[i].code == 0 && defbinds[i].btype == 0
67                             && defbinds[i].bit == 0)
68                                 break;
69
70                         binds_d[IN_BIND_OFFS(defbinds[i].code, defbinds[i].btype)] |=
71                                 1 << defbinds[i].bit;
72                 }
73         }
74
75         return binds;
76 }
77
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         memcpy(binds, binds + key_count * IN_BINDTYPE_COUNT,
145                 sizeof(binds[0]) * key_count * IN_BINDTYPE_COUNT);
146
147         in_devices[i].name = tmp;
148         in_devices[i].binds = binds;
149         in_devices[i].key_count = key_count;
150         if (i + 1 > in_dev_count)
151                 in_dev_count = i + 1;
152
153         lprintf("input: new device #%d \"%s\"\n", i, name);
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(&DRV(i));
254         }
255
256         /* get rid of devs without binds and probes */
257         for (i = 0; i < in_dev_count; i++) {
258                 if (!in_devices[i].probed && in_devices[i].binds == NULL) {
259                         in_dev_count--;
260                         if (i < in_dev_count) {
261                                 free(in_devices[i].name);
262                                 memmove(&in_devices[i], &in_devices[i+1],
263                                         (in_dev_count - i) * sizeof(in_devices[0]));
264                         }
265
266                         continue;
267                 }
268
269                 if (in_devices[i].probed && in_devices[i].drv_fd_hnd == -1)
270                         in_have_async_devs = 1;
271         }
272
273         if (in_have_async_devs)
274                 lprintf("input: async-only devices detected..\n");
275
276         in_debug_dump();
277 }
278
279 /* async update */
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         int ret;
435
436         while (1)
437         {
438                 int code, is_down = 0, dev_id = 0;
439
440                 code = in_update_keycode(&dev_id, &is_down, charcode, timeout_ms);
441                 if (code < 0)
442                         break;
443
444                 if (keys_old != menu_key_state) {
445                         menu_last_used_dev = dev_id;
446                         break;
447                 }
448         }
449
450         ret = menu_key_state;
451         menu_key_state &= ~PBTN_CHAR;
452         return ret;
453 }
454
455 /* wait for menu input, do autorepeat */
456 int in_menu_wait(int interesting, char *charcode, int autorep_delay_ms)
457 {
458         static int inp_prev = 0;
459         static int repeats = 0;
460         int ret, release = 0, wait = 450;
461
462         if (repeats)
463                 wait = autorep_delay_ms;
464
465         ret = in_menu_wait_any(charcode, wait);
466         if (ret == inp_prev)
467                 repeats++;
468
469         while (!(ret & interesting)) {
470                 ret = in_menu_wait_any(charcode, -1);
471                 release = 1;
472         }
473
474         if (release || ret != inp_prev)
475                 repeats = 0;
476
477         inp_prev = ret;
478
479         /* we don't need diagonals in menus */
480         if ((ret & PBTN_UP)   && (ret & PBTN_LEFT))  ret &= ~PBTN_LEFT;
481         if ((ret & PBTN_UP)   && (ret & PBTN_RIGHT)) ret &= ~PBTN_RIGHT;
482         if ((ret & PBTN_DOWN) && (ret & PBTN_LEFT))  ret &= ~PBTN_LEFT;
483         if ((ret & PBTN_DOWN) && (ret & PBTN_RIGHT)) ret &= ~PBTN_RIGHT;
484
485         return ret;
486 }
487
488 const int *in_get_dev_binds(int dev_id)
489 {
490         in_dev_t *dev = get_dev(dev_id);
491
492         return dev ? dev->binds : NULL;
493 }
494
495 const int *in_get_dev_def_binds(int dev_id)
496 {
497         in_dev_t *dev = get_dev(dev_id);
498         if (dev == NULL)
499                 return NULL;
500         if (dev->binds == NULL)
501                 return NULL;
502
503         return dev->binds + dev->key_count * IN_BINDTYPE_COUNT;
504 }
505
506 int in_get_config(int dev_id, int what, void *val)
507 {
508         int *ival = val;
509         in_dev_t *dev;
510
511         dev = get_dev(dev_id);
512         if (dev == NULL || val == NULL)
513                 return -1;
514
515         switch (what) {
516         case IN_CFG_BIND_COUNT:
517                 *ival = dev->key_count;
518                 break;
519         case IN_CFG_DOES_COMBOS:
520                 *ival = dev->does_combos;
521                 break;
522         case IN_CFG_BLOCKING:
523         case IN_CFG_KEY_NAMES:
524                 return -1; /* not implemented */
525         default:
526                 if (!dev->probed)
527                         return -1;
528
529                 return DRV(dev->drv_id).get_config(dev->drv_data, what, ival);
530         }
531
532         return 0;
533 }
534
535 static int in_set_blocking(int is_blocking)
536 {
537         int i, ret;
538
539         /* have_async_devs means we will have to do all reads async anyway.. */
540         if (!in_have_async_devs) {
541                 for (i = 0; i < in_dev_count; i++) {
542                         if (!in_devices[i].probed)
543                                 continue;
544
545                         DRV(in_devices[i].drv_id).set_config(
546                                 in_devices[i].drv_data, IN_CFG_BLOCKING,
547                                 is_blocking);
548                 }
549         }
550
551         menu_key_state = 0;
552
553         /* flush events */
554         do {
555                 ret = in_update_keycode(NULL, NULL, NULL, 0);
556         } while (ret >= 0);
557
558         return 0;
559 }
560
561 int in_set_config(int dev_id, int what, const void *val, int size)
562 {
563         const char * const *names;
564         const int *ival = val;
565         in_dev_t *dev;
566         int count;
567
568         if (what == IN_CFG_BLOCKING)
569                 return in_set_blocking(*ival);
570
571         dev = get_dev(dev_id);
572         if (dev == NULL)
573                 return -1;
574
575         switch (what) {
576         case IN_CFG_KEY_NAMES:
577                 names = val;
578                 count = size / sizeof(names[0]);
579
580                 if (count < dev->key_count) {
581                         lprintf("input: set_key_names: not enough keys\n");
582                         return -1;
583                 }
584
585                 dev->key_names = names;
586                 return 0;
587         case IN_CFG_DEFAULT_DEV:
588                 /* just set last used dev, for now */
589                 menu_last_used_dev = dev_id;
590                 return 0;
591         default:
592                 break;
593         }
594
595         if (dev->probed)
596                 return DRV(dev->drv_id).set_config(dev->drv_data, what, *ival);
597
598         return -1;
599 }
600
601 const char *in_get_dev_name(int dev_id, int must_be_active, int skip_pfix)
602 {
603         const char *name, *tmp;
604         in_dev_t *dev;
605
606         dev = get_dev(dev_id);
607         if (dev == NULL)
608                 return NULL;
609
610         if (must_be_active && !dev->probed)
611                 return NULL;
612
613         name = dev->name;
614         if (name == NULL || !skip_pfix)
615                 return name;
616
617         /* skip prefix */
618         tmp = strchr(name, ':');
619         if (tmp != NULL)
620                 name = tmp + 1;
621
622         return name;
623 }
624
625 int in_name_to_id(const char *dev_name)
626 {
627         int i;
628
629         for (i = 0; i < in_dev_count; i++)
630                 if (strcmp(dev_name, in_devices[i].name) == 0)
631                         break;
632
633         if (i >= in_dev_count) {
634                 lprintf("input: in_name_to_id: no such device: %s\n", dev_name);
635                 return -1;
636         }
637
638         return i;
639 }
640
641 /* never returns NULL */
642 const char *in_get_key_name(int dev_id, int keycode)
643 {
644         const char *name = NULL;
645         static char xname[16];
646         in_drv_t *drv;
647         in_dev_t *dev;
648
649         if (dev_id < 0)         /* want last used dev? */
650                 dev_id = menu_last_used_dev;
651
652         dev = get_dev(dev_id);
653         if (dev == NULL)
654                 return "Unkn0";
655
656         drv = &DRV(dev->drv_id);
657         if (keycode < 0)        /* want name for menu key? */
658                 keycode = drv->menu_translate(dev->drv_data, keycode, NULL);
659
660         if (dev->key_names != NULL && 0 <= keycode && keycode < dev->key_count)
661                 name = dev->key_names[keycode];
662         if (name != NULL)
663                 return name;
664
665         if (drv->get_key_name != NULL)
666                 name = drv->get_key_name(keycode);
667         if (name != NULL)
668                 return name;
669
670         /* assume scancode */
671         if ((keycode >= '0' && keycode <= '9') || (keycode >= 'a' && keycode <= 'z')
672                         || (keycode >= 'A' && keycode <= 'Z'))
673                 sprintf(xname, "%c", keycode);
674         else
675                 sprintf(xname, "\\x%02X", keycode);
676         return xname;
677 }
678
679 int in_get_key_code(int dev_id, const char *key_name)
680 {
681         in_dev_t *dev;
682         int i;
683
684         if (dev_id < 0)         /* want last used dev? */
685                 dev_id = menu_last_used_dev;
686
687         dev = get_dev(dev_id);
688         if (dev == NULL)
689                 return -1;
690
691         if (dev->key_names == NULL)
692                 return -1;
693
694         for (i = 0; i < dev->key_count; i++)
695                 if (dev->key_names[i] && strcasecmp(dev->key_names[i], key_name) == 0)
696                         return i;
697
698         return -1;
699 }
700
701 int in_bind_key(int dev_id, int keycode, int mask, int bind_type, int force_unbind)
702 {
703         int ret, count;
704         in_dev_t *dev;
705
706         dev = get_dev(dev_id);
707         if (dev == NULL || bind_type >= IN_BINDTYPE_COUNT)
708                 return -1;
709
710         count = dev->key_count;
711
712         if (dev->binds == NULL) {
713                 if (force_unbind)
714                         return 0;
715                 dev->binds = in_alloc_binds(dev->drv_id, count);
716                 if (dev->binds == NULL)
717                         return -1;
718         }
719
720         if (keycode < 0 || keycode >= count)
721                 return -1;
722         
723         if (force_unbind)
724                 dev->binds[IN_BIND_OFFS(keycode, bind_type)] &= ~mask;
725         else
726                 dev->binds[IN_BIND_OFFS(keycode, bind_type)] ^=  mask;
727         
728         ret = DRV(dev->drv_id).clean_binds(dev->drv_data, dev->binds,
729                                 dev->binds + count * IN_BINDTYPE_COUNT);
730         if (ret == 0) {
731                 free(dev->binds);
732                 dev->binds = NULL;
733         }
734
735         return 0;
736 }
737
738 /*
739  * Unbind act_mask on binds with type bind_type
740  * - if dev_id_ < 0, affects all devices
741  *   else only affects dev_id_
742  * - if act_mask == -1, unbind all keys
743  *   else only actions in mask
744  */
745 void in_unbind_all(int dev_id_, int act_mask, int bind_type)
746 {
747         int dev_id = 0, dev_last = IN_MAX_DEVS - 1;
748         int i, count;
749         in_dev_t *dev;
750
751         if (dev_id_ >= 0)
752                 dev_id = dev_last = dev_id_;
753
754         if (bind_type >= IN_BINDTYPE_COUNT)
755                 return;
756
757         for (; dev_id <= dev_last; dev_id++) {
758                 dev = &in_devices[dev_id];
759                 count = dev->key_count;
760
761                 if (dev->binds == NULL)
762                         continue;
763
764                 if (act_mask != -1) {
765                         for (i = 0; i < count; i++)
766                                 dev->binds[IN_BIND_OFFS(i, bind_type)] &= ~act_mask;
767                 }
768                 else
769                         memset(dev->binds, 0, sizeof(dev->binds[0]) * count * IN_BINDTYPE_COUNT);
770         }
771 }
772
773 /* returns device id, or -1 on error */
774 int in_config_parse_dev(const char *name)
775 {
776         int drv_id = -1, i;
777
778         for (i = 0; i < in_driver_count; i++) {
779                 int len = strlen(in_drivers[i].prefix);
780                 if (strncmp(name, in_drivers[i].prefix, len) == 0) {
781                         drv_id = i;
782                         break;
783                 }
784         }
785
786         if (drv_id < 0) {
787                 lprintf("input: missing driver for '%s'\n", name);
788                 return -1;
789         }
790
791         for (i = 0; i < in_dev_count; i++)
792         {
793                 if (in_devices[i].name == NULL)
794                         continue;
795                 if (strcmp(in_devices[i].name, name) == 0)
796                         return i;
797         }
798
799         if (i >= IN_MAX_DEVS)
800         {
801                 /* try to find unused device */
802                 for (i = 0; i < IN_MAX_DEVS; i++)
803                         if (in_devices[i].name == NULL) break;
804                 if (i >= IN_MAX_DEVS) {
805                         lprintf("input: too many devices, can't add '%s'\n",
806                                 name);
807                         return -1;
808                 }
809         }
810
811         memset(&in_devices[i], 0, sizeof(in_devices[i]));
812
813         in_devices[i].name = strdup(name);
814         if (in_devices[i].name == NULL)
815                 return -1;
816
817         in_devices[i].key_names = DRV(drv_id).get_key_names(&DRV(drv_id),
818                                 &in_devices[i].key_count);
819         in_devices[i].drv_id = drv_id;
820
821         if (i + 1 > in_dev_count)
822                 in_dev_count = i + 1;
823
824         return i;
825 }
826
827 int in_config_bind_key(int dev_id, const char *key, int acts, int bind_type)
828 {
829         in_dev_t *dev;
830         int i, offs, kc;
831
832         dev = get_dev(dev_id);
833         if (dev == NULL || bind_type >= IN_BINDTYPE_COUNT)
834                 return -1;
835
836         /* maybe a raw code? */
837         if (key[0] == '\\' && key[1] == 'x') {
838                 char *p = NULL;
839                 kc = (int)strtoul(key + 2, &p, 16);
840                 if (p == NULL || *p != 0)
841                         kc = -1;
842         }
843         else {
844                 /* device specific key name */
845                 if (dev->binds == NULL) {
846                         dev->binds = in_alloc_binds(dev->drv_id, dev->key_count);
847                         if (dev->binds == NULL)
848                                 return -1;
849                 }
850
851                 kc = -1;
852                 if (dev->key_names != NULL) {
853                         for (i = 0; i < dev->key_count; i++) {
854                                 const char *k = dev->key_names[i];
855                                 if (k != NULL && strcasecmp(k, key) == 0) {
856                                         kc = i;
857                                         break;
858                                 }
859                         }
860                 }
861
862                 if (kc < 0)
863                         kc = DRV(dev->drv_id).get_key_code(key);
864                 if (kc < 0 && strlen(key) == 1) {
865                         /* assume scancode */
866                         kc = key[0];
867                 }
868         }
869
870         if (kc < 0 || kc >= dev->key_count) {
871                 lprintf("input: bad key: '%s' for device '%s'\n",
872                         key, dev->name);
873                 return -1;
874         }
875
876         if (bind_type == IN_BINDTYPE_NONE) {
877                 for (i = 0; i < IN_BINDTYPE_COUNT; i++)
878                         dev->binds[IN_BIND_OFFS(kc, i)] = 0;
879                 return 0;
880         }
881
882         offs = IN_BIND_OFFS(kc, bind_type);
883         if (dev->binds[offs] == -1)
884                 dev->binds[offs] = 0;
885         dev->binds[offs] |= acts;
886         return 0;
887 }
888
889 void in_clean_binds(void)
890 {
891         int i;
892
893         for (i = 0; i < IN_MAX_DEVS; i++) {
894                 int ret, count, *binds, *def_binds;
895                 in_dev_t *dev = &in_devices[i];
896
897                 if (dev->binds == NULL || !dev->probed)
898                         continue;
899
900                 count = dev->key_count;
901                 binds = dev->binds;
902                 def_binds = binds + count * IN_BINDTYPE_COUNT;
903
904                 ret = DRV(dev->drv_id).clean_binds(dev->drv_data, binds, def_binds);
905                 if (ret == 0) {
906                         /* no useable binds */
907                         free(dev->binds);
908                         dev->binds = NULL;
909                 }
910         }
911 }
912
913 void in_debug_dump(void)
914 {
915         int i;
916
917         lprintf("# drv probed binds name\n");
918         for (i = 0; i < IN_MAX_DEVS; i++) {
919                 in_dev_t *d = &in_devices[i];
920                 if (!d->probed && d->name == NULL && d->binds == NULL)
921                         continue;
922                 lprintf("%d %3d %6c %5c %s\n", i, d->drv_id,
923                         d->probed ? 'y' : 'n',
924                         d->binds ? 'y' : 'n', d->name);
925 #if 0
926                 if (d->binds) {
927                         int kc, o, t, h;
928                         for (kc = 0; kc < d->key_count; kc++) {
929                                 o = IN_BIND_OFFS(kc, 0);
930                                 for (t = h = 0; t < IN_BINDTYPE_COUNT; t++)
931                                         h |= d->binds[o + t];
932                                 if (h == 0)
933                                         continue;
934                                 lprintf("  [%3d] =", kc);
935                                 for (t = 0; t < IN_BINDTYPE_COUNT; t++)
936                                         printf(" %x", d->binds[o + t]);
937                                 printf("\n");
938                         }
939                 }
940 #endif
941         }
942 }
943
944 /* stubs for drivers that choose not to implement something */
945
946 static void in_def_free(void *drv_data) {}
947 static int  in_def_clean_binds(void *drv_data, int *b, int *db) { return 1; }
948 static int  in_def_get_config(void *drv_data, int what, int *val) { return -1; }
949 static int  in_def_set_config(void *drv_data, int what, int val) { return -1; }
950 static int  in_def_update_analog(void *drv_data, int axis_id, int *result) { return -1; }
951 static int  in_def_update_keycode(void *drv_data, int *is_down) { return 0; }
952 static int  in_def_menu_translate(void *drv_data, int keycode, char *ccode) { return 0; }
953 static int  in_def_get_key_code(const char *key_name) { return -1; }
954 static const char *in_def_get_key_name(int keycode) { return NULL; }
955
956 #define CHECK_ADD_STUB(d, f) \
957         if (d.f == NULL) d.f = in_def_##f
958
959 /* to be called by drivers */
960 int in_register_driver(const in_drv_t *drv,
961                         const struct in_default_bind *defbinds, const void *pdata)
962 {
963         int count_new = in_driver_count + 1;
964         in_drv_t *new_drivers;
965
966         new_drivers = realloc(in_drivers, count_new * sizeof(in_drivers[0]));
967         if (new_drivers == NULL) {
968                 lprintf("input: in_register_driver OOM\n");
969                 return -1;
970         }
971
972         memcpy(&new_drivers[in_driver_count], drv, sizeof(new_drivers[0]));
973
974         CHECK_ADD_STUB(new_drivers[in_driver_count], free);
975         CHECK_ADD_STUB(new_drivers[in_driver_count], clean_binds);
976         CHECK_ADD_STUB(new_drivers[in_driver_count], get_config);
977         CHECK_ADD_STUB(new_drivers[in_driver_count], set_config);
978         CHECK_ADD_STUB(new_drivers[in_driver_count], update_analog);
979         CHECK_ADD_STUB(new_drivers[in_driver_count], update_keycode);
980         CHECK_ADD_STUB(new_drivers[in_driver_count], menu_translate);
981         CHECK_ADD_STUB(new_drivers[in_driver_count], get_key_code);
982         CHECK_ADD_STUB(new_drivers[in_driver_count], get_key_name);
983         if (pdata)
984                 new_drivers[in_driver_count].pdata = pdata;
985         if (defbinds)
986                 new_drivers[in_driver_count].defbinds = defbinds;
987         in_drivers = new_drivers;
988         in_driver_count = count_new;
989
990         return 0;
991 }
992
993 void in_init(void)
994 {
995         in_drivers = NULL;
996         memset(in_devices, 0, sizeof(in_devices));
997         in_driver_count = 0;
998         in_dev_count = 0;
999 }
1000
1001 #if 0
1002 int main(void)
1003 {
1004         int ret;
1005
1006         in_init();
1007         in_probe();
1008
1009         in_set_blocking(1);
1010
1011 #if 1
1012         while (1) {
1013                 int dev = 0, down;
1014                 ret = in_update_keycode(&dev, &down);
1015                 lprintf("#%i: %i %i (%s)\n", dev, down, ret, in_get_key_name(dev, ret));
1016         }
1017 #else
1018         while (1) {
1019                 ret = in_menu_wait_any();
1020                 lprintf("%08x\n", ret);
1021         }
1022 #endif
1023
1024         return 0;
1025 }
1026 #endif