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