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