2 * (C) GraÅžvydas "notaz" Ignotas, 2008-2011
4 * This work is licensed under the terms of any of these licenses
6 * - GNU GPL, version 2 or later.
7 * - GNU LGPL, version 2.1 or later.
9 * See the COPYING file in the top-level directory.
21 #error needs update: in_vk_init in_vk_update
22 #include "../win32/in_vk.h"
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;
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;
47 #define DRV(id) in_drivers[id]
50 static int *in_alloc_binds(int drv_id, int key_count)
52 const struct in_default_bind *defbinds;
56 binds = calloc(key_count * IN_BINDTYPE_COUNT * 2, sizeof(binds[0]));
60 defbinds = DRV(drv_id).defbinds;
61 if (defbinds != NULL) {
63 if (defbinds[i].bit == 0 && defbinds[i].code == 0)
65 binds[IN_BIND_OFFS(defbinds[i].code, defbinds[i].btype)] =
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);
77 static void in_unprobe(in_dev_t *dev)
80 DRV(dev->drv_id).free(dev->drv_data);
85 static void in_free(in_dev_t *dev)
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)
99 int i, ret, dupe_count = 0, *binds;
100 char name[256], *name_end, *tmp;
102 strncpy(name, nname, sizeof(name));
103 name[sizeof(name)-12] = 0;
104 name_end = name + strlen(name);
106 for (i = 0; i < in_dev_count; i++)
108 if (in_devices[i].name == NULL)
110 if (strcmp(in_devices[i].name, name) == 0)
112 if (in_devices[i].probed) {
114 sprintf(name_end, " [%d]", dupe_count);
121 if (i >= IN_MAX_DEVS)
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);
130 in_free(&in_devices[i]);
137 binds = in_alloc_binds(in_probe_dev_id, key_count);
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;
149 lprintf("input: new device #%d \"%s\"\n", i, name);
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;
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);
162 /* no useable binds */
163 free(in_devices[i].binds);
164 in_devices[i].binds = NULL;
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)
175 *combo_keys = *combo_acts = 0;
176 for (act = 0; act < sizeof(binds[0]) * 8; act++)
179 for (u = 0; u <= last_key; u++)
180 if (binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)] & (1 << act))
185 // loop again and mark those keys and actions as combo
186 for (u = 0; u <= last_key; u++)
188 if (binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)] & (1 << act)) {
189 *combo_keys |= 1 << u;
190 *combo_acts |= 1 << act;
197 int in_combos_do(int keys, const int *binds, int last_key, int combo_keys, int combo_acts)
201 for (i = 0; i <= last_key; i++)
205 if (!(keys & (1 << i)))
208 acts = binds[IN_BIND_OFFS(i, IN_BINDTYPE_EMU)];
212 if (!(combo_keys & (1 << i))) {
217 acts_c = acts & combo_acts;
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));
228 // add non-combo actions if combo ones were not found
230 ret |= acts & ~combo_acts;
240 in_have_async_devs = 0;
242 menu_last_used_dev = 0;
244 for (i = 0; i < in_dev_count; i++)
245 in_unprobe(&in_devices[i]);
247 for (i = 0; i < in_driver_count; i++) {
249 in_drivers[i].probe();
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) {
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]));
265 if (in_devices[i].probed && in_devices[i].drv_fd_hnd == -1)
266 in_have_async_devs = 1;
269 if (in_have_async_devs)
270 lprintf("input: async-only devices detected..\n");
276 int in_update(int *result)
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);
289 static in_dev_t *get_dev(int dev_id)
291 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
294 return &in_devices[dev_id];
297 int in_update_analog(int dev_id, int axis_id, int *result)
299 in_dev_t *dev = get_dev(dev_id);
301 if (dev == NULL || !dev->probed)
304 return DRV(dev->drv_id).update_analog(dev->drv_data, axis_id, result);
307 static int in_update_kc_async(int *dev_id_out, int *is_down_out, int timeout_ms)
309 int i, is_down, result;
312 ticks = plat_get_ticks_ms();
316 for (i = 0; i < in_dev_count; i++) {
317 in_dev_t *d = &in_devices[i];
321 result = DRV(d->drv_id).update_keycode(d->drv_data, &is_down);
328 *is_down_out = is_down;
332 if (timeout_ms >= 0 && (int)(plat_get_ticks_ms() - ticks) > timeout_ms)
342 * wait for a press, always return some keycode or -1 on timeout or error
344 int in_update_keycode(int *dev_id_out, int *is_down_out, char *charcode, int timeout_ms)
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;
352 if (in_have_async_devs) {
353 result = in_update_kc_async(&dev_id, &is_down, timeout_ms);
356 drv = &DRV(in_devices[dev_id].drv_id);
360 ticks = plat_get_ticks_ms();
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;
368 /* don't deadlock, fail */
369 lprintf("input: failed to find devices to read\n");
375 ret = plat_wait_event(fds_hnds, count, timeout_ms);
379 for (i = 0; i < in_dev_count; i++) {
380 if (in_devices[i].drv_fd_hnd == ret) {
386 drv = &DRV(in_devices[dev_id].drv_id);
387 result = drv->update_keycode(in_devices[dev_id].drv_data, &is_down);
392 lprintf("input: \"%s\" errored out, removing.\n", in_devices[dev_id].name);
393 in_unprobe(&in_devices[dev_id]);
397 if (timeout_ms >= 0) {
398 unsigned int ticks2 = plat_get_ticks_ms();
399 timeout_ms -= ticks2 - ticks;
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) {
414 menu_key_state |= result_menu;
416 menu_key_state &= ~result_menu;
419 if (dev_id_out != NULL)
420 *dev_id_out = dev_id;
421 if (is_down_out != NULL)
422 *is_down_out = is_down;
426 /* same as above, only return bitfield of PBTN_* */
427 int in_menu_wait_any(char *charcode, int timeout_ms)
429 int keys_old = menu_key_state;
433 int code, is_down = 0, dev_id = 0;
435 code = in_update_keycode(&dev_id, &is_down, charcode, timeout_ms);
439 if (keys_old != menu_key_state) {
440 menu_last_used_dev = dev_id;
445 return menu_key_state;
448 /* wait for menu input, do autorepeat */
449 int in_menu_wait(int interesting, char *charcode, int autorep_delay_ms)
451 static int inp_prev = 0;
452 static int repeats = 0;
453 int ret, release = 0, wait = 450;
456 wait = autorep_delay_ms;
458 ret = in_menu_wait_any(charcode, wait);
462 while (!(ret & interesting)) {
463 ret = in_menu_wait_any(charcode, -1);
467 if (release || ret != inp_prev)
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;
481 const int *in_get_dev_binds(int dev_id)
483 in_dev_t *dev = get_dev(dev_id);
485 return dev ? dev->binds : NULL;
488 const int *in_get_dev_def_binds(int dev_id)
490 in_dev_t *dev = get_dev(dev_id);
494 return dev->binds + dev->key_count * IN_BINDTYPE_COUNT;
497 int in_get_config(int dev_id, int what, void *val)
502 dev = get_dev(dev_id);
503 if (dev == NULL || val == NULL)
507 case IN_CFG_BIND_COUNT:
508 *ival = dev->key_count;
510 case IN_CFG_DOES_COMBOS:
511 *ival = dev->does_combos;
513 case IN_CFG_BLOCKING:
514 case IN_CFG_KEY_NAMES:
515 return -1; /* not implemented */
517 return DRV(dev->drv_id).get_config(dev->drv_data, what, ival);
523 static int in_set_blocking(int is_blocking)
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);
540 ret = in_update_keycode(NULL, NULL, NULL, 0);
546 int in_set_config(int dev_id, int what, const void *val, int size)
548 const char * const *names;
549 const int *ival = val;
553 if (what == IN_CFG_BLOCKING)
554 return in_set_blocking(*ival);
556 dev = get_dev(dev_id);
561 case IN_CFG_KEY_NAMES:
563 count = size / sizeof(names[0]);
565 if (count < dev->key_count) {
566 lprintf("input: set_key_names: not enough keys\n");
570 dev->key_names = names;
572 case IN_CFG_DEFAULT_DEV:
573 /* just set last used dev, for now */
574 menu_last_used_dev = dev_id;
581 return DRV(dev->drv_id).set_config(dev->drv_data, what, *ival);
586 const char *in_get_dev_name(int dev_id, int must_be_active, int skip_pfix)
588 const char *name, *tmp;
591 dev = get_dev(dev_id);
595 if (must_be_active && !dev->probed)
599 if (name == NULL || !skip_pfix)
603 tmp = strchr(name, ':');
610 int in_name_to_id(const char *dev_name)
614 for (i = 0; i < in_dev_count; i++)
615 if (strcmp(dev_name, in_devices[i].name) == 0)
618 if (i >= in_dev_count) {
619 lprintf("input: in_name_to_id: no such device: %s\n", dev_name);
626 /* never returns NULL */
627 const char *in_get_key_name(int dev_id, int keycode)
629 const char *name = NULL;
630 static char xname[16];
634 if (dev_id < 0) /* want last used dev? */
635 dev_id = menu_last_used_dev;
637 dev = get_dev(dev_id);
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);
645 if (dev->key_names != NULL && 0 <= keycode && keycode < dev->key_count)
646 name = dev->key_names[keycode];
650 if (drv->get_key_name != NULL)
651 name = drv->get_key_name(keycode);
655 /* assume scancode */
656 if ((keycode >= '0' && keycode <= '9') || (keycode >= 'a' && keycode <= 'z')
657 || (keycode >= 'A' && keycode <= 'Z'))
658 sprintf(xname, "%c", keycode);
660 sprintf(xname, "\\x%02X", keycode);
664 int in_get_key_code(int dev_id, const char *key_name)
669 if (dev_id < 0) /* want last used dev? */
670 dev_id = menu_last_used_dev;
672 dev = get_dev(dev_id);
676 if (dev->key_names == NULL)
679 for (i = 0; i < dev->key_count; i++)
680 if (dev->key_names[i] && strcasecmp(dev->key_names[i], key_name) == 0)
686 int in_bind_key(int dev_id, int keycode, int mask, int bind_type, int force_unbind)
691 dev = get_dev(dev_id);
692 if (dev == NULL || bind_type >= IN_BINDTYPE_COUNT)
695 count = dev->key_count;
697 if (dev->binds == NULL) {
700 dev->binds = in_alloc_binds(dev->drv_id, count);
701 if (dev->binds == NULL)
705 if (keycode < 0 || keycode >= count)
709 dev->binds[IN_BIND_OFFS(keycode, bind_type)] &= ~mask;
711 dev->binds[IN_BIND_OFFS(keycode, bind_type)] ^= mask;
713 ret = DRV(dev->drv_id).clean_binds(dev->drv_data, dev->binds,
714 dev->binds + count * IN_BINDTYPE_COUNT);
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
730 void in_unbind_all(int dev_id_, int act_mask, int bind_type)
732 int dev_id = 0, dev_last = IN_MAX_DEVS - 1;
737 dev_id = dev_last = dev_id_;
739 if (bind_type >= IN_BINDTYPE_COUNT)
742 for (; dev_id <= dev_last; dev_id++) {
743 dev = &in_devices[dev_id];
744 count = dev->key_count;
746 if (dev->binds == NULL)
749 if (act_mask != -1) {
750 for (i = 0; i < count; i++)
751 dev->binds[IN_BIND_OFFS(i, bind_type)] &= ~act_mask;
754 memset(dev->binds, 0, sizeof(dev->binds[0]) * count * IN_BINDTYPE_COUNT);
758 /* returns device id, or -1 on error */
759 int in_config_parse_dev(const char *name)
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) {
772 lprintf("input: missing driver for %s\n", name);
776 for (i = 0; i < in_dev_count; i++)
778 if (in_devices[i].name == NULL)
780 if (strcmp(in_devices[i].name, name) == 0)
784 if (i >= IN_MAX_DEVS)
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);
795 memset(&in_devices[i], 0, sizeof(in_devices[i]));
797 in_devices[i].name = strdup(name);
798 if (in_devices[i].name == NULL)
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;
804 if (i + 1 > in_dev_count)
805 in_dev_count = i + 1;
810 int in_config_bind_key(int dev_id, const char *key, int acts, int bind_type)
815 dev = get_dev(dev_id);
816 if (dev == NULL || bind_type >= IN_BINDTYPE_COUNT)
819 /* maybe a raw code? */
820 if (key[0] == '\\' && key[1] == 'x') {
822 kc = (int)strtoul(key + 2, &p, 16);
823 if (p == NULL || *p != 0)
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)
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) {
846 kc = DRV(dev->drv_id).get_key_code(key);
847 if (kc < 0 && strlen(key) == 1) {
848 /* assume scancode */
853 if (kc < 0 || kc >= dev->key_count) {
854 lprintf("input: bad key: %s\n", key);
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;
864 offs = IN_BIND_OFFS(kc, bind_type);
865 if (dev->binds[offs] == -1)
866 dev->binds[offs] = 0;
867 dev->binds[offs] |= acts;
871 void in_clean_binds(void)
875 for (i = 0; i < IN_MAX_DEVS; i++) {
876 int ret, count, *binds, *def_binds;
877 in_dev_t *dev = &in_devices[i];
879 if (dev->binds == NULL || dev->drv_data == NULL)
882 count = dev->key_count;
884 def_binds = binds + count * IN_BINDTYPE_COUNT;
886 ret = DRV(dev->drv_id).clean_binds(dev->drv_data, binds, def_binds);
888 /* no useable binds */
895 void in_debug_dump(void)
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)
904 lprintf("%d %3d %6c %5c %s\n", i, d->drv_id, d->probed ? 'y' : 'n',
905 d->binds ? 'y' : 'n', d->name);
909 /* stubs for drivers that choose not to implement something */
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; }
921 #define CHECK_ADD_STUB(d, f) \
922 if (d.f == NULL) d.f = in_def_##f
924 /* to be called by drivers */
925 int in_register_driver(const in_drv_t *drv, const struct in_default_bind *defbinds)
927 int count_new = in_driver_count + 1;
928 in_drv_t *new_drivers;
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");
936 memcpy(&new_drivers[in_driver_count], drv, sizeof(new_drivers[0]));
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;
958 memset(in_devices, 0, sizeof(in_devices));
976 ret = in_update_keycode(&dev, &down);
977 lprintf("#%i: %i %i (%s)\n", dev, down, ret, in_get_key_name(dev, ret));
981 ret = in_menu_wait_any();
982 lprintf("%08x\n", ret);