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 binds_d = binds + key_count * IN_BINDTYPE_COUNT;
62 /* always have a copy of defbinds */
63 defbinds = DRV(drv_id).defbinds;
64 if (defbinds != NULL) {
66 if (defbinds[i].code == 0 && defbinds[i].btype == 0
67 && defbinds[i].bit == 0)
70 binds_d[IN_BIND_OFFS(defbinds[i].code, defbinds[i].btype)] |=
78 static void in_unprobe(in_dev_t *dev)
81 DRV(dev->drv_id).free(dev->drv_data);
86 static void in_free(in_dev_t *dev)
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)
100 int i, ret, dupe_count = 0, *binds;
101 char name[256], *name_end, *tmp;
103 strncpy(name, nname, sizeof(name));
104 name[sizeof(name)-12] = 0;
105 name_end = name + strlen(name);
107 for (i = 0; i < in_dev_count; i++)
109 if (in_devices[i].name == NULL)
111 if (strcmp(in_devices[i].name, name) == 0)
113 if (in_devices[i].probed) {
115 sprintf(name_end, " [%d]", dupe_count);
122 if (i >= IN_MAX_DEVS)
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);
131 in_free(&in_devices[i]);
138 binds = in_alloc_binds(in_probe_dev_id, key_count);
144 memcpy(binds, binds + key_count * IN_BINDTYPE_COUNT,
145 sizeof(binds[0]) * key_count * IN_BINDTYPE_COUNT);
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;
153 lprintf("input: new device #%d \"%s\"\n", i, name);
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;
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);
166 /* no useable binds */
167 free(in_devices[i].binds);
168 in_devices[i].binds = NULL;
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)
179 *combo_keys = *combo_acts = 0;
180 for (act = 0; act < sizeof(binds[0]) * 8; act++)
183 for (u = 0; u <= last_key; u++)
184 if (binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)] & (1 << act))
189 // loop again and mark those keys and actions as combo
190 for (u = 0; u <= last_key; u++)
192 if (binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)] & (1 << act)) {
193 *combo_keys |= 1 << u;
194 *combo_acts |= 1 << act;
201 int in_combos_do(int keys, const int *binds, int last_key, int combo_keys, int combo_acts)
205 for (i = 0; i <= last_key; i++)
209 if (!(keys & (1 << i)))
212 acts = binds[IN_BIND_OFFS(i, IN_BINDTYPE_EMU)];
216 if (!(combo_keys & (1 << i))) {
221 acts_c = acts & combo_acts;
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));
232 // add non-combo actions if combo ones were not found
234 ret |= acts & ~combo_acts;
244 in_have_async_devs = 0;
246 menu_last_used_dev = 0;
248 for (i = 0; i < in_dev_count; i++)
249 in_unprobe(&in_devices[i]);
251 for (i = 0; i < in_driver_count; i++) {
253 in_drivers[i].probe(&DRV(i));
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) {
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]));
269 if (in_devices[i].probed && in_devices[i].drv_fd_hnd == -1)
270 in_have_async_devs = 1;
273 if (in_have_async_devs)
274 lprintf("input: async-only devices detected..\n");
280 int in_update(int *result)
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);
293 static in_dev_t *get_dev(int dev_id)
295 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
298 return &in_devices[dev_id];
301 int in_update_analog(int dev_id, int axis_id, int *result)
303 in_dev_t *dev = get_dev(dev_id);
305 if (dev == NULL || !dev->probed)
308 return DRV(dev->drv_id).update_analog(dev->drv_data, axis_id, result);
311 static int in_update_kc_async(int *dev_id_out, int *is_down_out, int timeout_ms)
313 int i, is_down, result;
316 ticks = plat_get_ticks_ms();
320 for (i = 0; i < in_dev_count; i++) {
321 in_dev_t *d = &in_devices[i];
325 result = DRV(d->drv_id).update_keycode(d->drv_data, &is_down);
332 *is_down_out = is_down;
336 if (timeout_ms >= 0 && (int)(plat_get_ticks_ms() - ticks) > timeout_ms)
346 * wait for a press, always return some keycode or -1 on timeout or error
348 int in_update_keycode(int *dev_id_out, int *is_down_out, char *charcode, int timeout_ms)
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;
356 if (in_have_async_devs) {
357 result = in_update_kc_async(&dev_id, &is_down, timeout_ms);
360 drv = &DRV(in_devices[dev_id].drv_id);
364 ticks = plat_get_ticks_ms();
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;
372 /* don't deadlock, fail */
373 lprintf("input: failed to find devices to read\n");
379 ret = plat_wait_event(fds_hnds, count, timeout_ms);
383 for (i = 0; i < in_dev_count; i++) {
384 if (in_devices[i].drv_fd_hnd == ret) {
390 drv = &DRV(in_devices[dev_id].drv_id);
391 result = drv->update_keycode(in_devices[dev_id].drv_data, &is_down);
396 lprintf("input: \"%s\" errored out, removing.\n", in_devices[dev_id].name);
397 in_unprobe(&in_devices[dev_id]);
401 if (timeout_ms >= 0) {
402 unsigned int ticks2 = plat_get_ticks_ms();
403 timeout_ms -= ticks2 - ticks;
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) {
418 menu_key_state |= result_menu;
420 menu_key_state &= ~result_menu;
423 if (dev_id_out != NULL)
424 *dev_id_out = dev_id;
425 if (is_down_out != NULL)
426 *is_down_out = is_down;
430 /* same as above, only return bitfield of PBTN_* */
431 int in_menu_wait_any(char *charcode, int timeout_ms)
433 int keys_old = menu_key_state;
438 int code, is_down = 0, dev_id = 0;
440 code = in_update_keycode(&dev_id, &is_down, charcode, timeout_ms);
444 if (keys_old != menu_key_state) {
445 menu_last_used_dev = dev_id;
450 ret = menu_key_state;
451 menu_key_state &= ~PBTN_CHAR;
455 /* wait for menu input, do autorepeat */
456 int in_menu_wait(int interesting, char *charcode, int autorep_delay_ms)
458 static int inp_prev = 0;
459 static int repeats = 0;
460 int ret, release = 0, wait = 450;
463 wait = autorep_delay_ms;
465 ret = in_menu_wait_any(charcode, wait);
469 while (!(ret & interesting)) {
470 ret = in_menu_wait_any(charcode, -1);
474 if (release || ret != inp_prev)
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;
488 const int *in_get_dev_binds(int dev_id)
490 in_dev_t *dev = get_dev(dev_id);
492 return dev ? dev->binds : NULL;
495 const int *in_get_dev_def_binds(int dev_id)
497 in_dev_t *dev = get_dev(dev_id);
500 if (dev->binds == NULL)
503 return dev->binds + dev->key_count * IN_BINDTYPE_COUNT;
506 int in_get_config(int dev_id, int what, void *val)
511 dev = get_dev(dev_id);
512 if (dev == NULL || val == NULL)
516 case IN_CFG_BIND_COUNT:
517 *ival = dev->key_count;
519 case IN_CFG_DOES_COMBOS:
520 *ival = dev->does_combos;
522 case IN_CFG_BLOCKING:
523 case IN_CFG_KEY_NAMES:
524 return -1; /* not implemented */
529 return DRV(dev->drv_id).get_config(dev->drv_data, what, ival);
535 static int in_set_blocking(int is_blocking)
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)
545 DRV(in_devices[i].drv_id).set_config(
546 in_devices[i].drv_data, IN_CFG_BLOCKING,
555 ret = in_update_keycode(NULL, NULL, NULL, 0);
561 int in_set_config(int dev_id, int what, const void *val, int size)
563 const char * const *names;
564 const int *ival = val;
568 if (what == IN_CFG_BLOCKING)
569 return in_set_blocking(*ival);
571 dev = get_dev(dev_id);
576 case IN_CFG_KEY_NAMES:
578 count = size / sizeof(names[0]);
580 if (count < dev->key_count) {
581 lprintf("input: set_key_names: not enough keys\n");
585 dev->key_names = names;
587 case IN_CFG_DEFAULT_DEV:
588 /* just set last used dev, for now */
589 menu_last_used_dev = dev_id;
596 return DRV(dev->drv_id).set_config(dev->drv_data, what, *ival);
601 const char *in_get_dev_name(int dev_id, int must_be_active, int skip_pfix)
603 const char *name, *tmp;
606 dev = get_dev(dev_id);
610 if (must_be_active && !dev->probed)
614 if (name == NULL || !skip_pfix)
618 tmp = strchr(name, ':');
625 int in_name_to_id(const char *dev_name)
629 for (i = 0; i < in_dev_count; i++)
630 if (strcmp(dev_name, in_devices[i].name) == 0)
633 if (i >= in_dev_count) {
634 lprintf("input: in_name_to_id: no such device: %s\n", dev_name);
641 /* never returns NULL */
642 const char *in_get_key_name(int dev_id, int keycode)
644 const char *name = NULL;
645 static char xname[16];
649 if (dev_id < 0) /* want last used dev? */
650 dev_id = menu_last_used_dev;
652 dev = get_dev(dev_id);
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);
660 if (dev->key_names != NULL && 0 <= keycode && keycode < dev->key_count)
661 name = dev->key_names[keycode];
665 if (drv->get_key_name != NULL)
666 name = drv->get_key_name(keycode);
670 /* assume scancode */
671 if ((keycode >= '0' && keycode <= '9') || (keycode >= 'a' && keycode <= 'z')
672 || (keycode >= 'A' && keycode <= 'Z'))
673 sprintf(xname, "%c", keycode);
675 sprintf(xname, "\\x%02X", keycode);
679 int in_get_key_code(int dev_id, const char *key_name)
684 if (dev_id < 0) /* want last used dev? */
685 dev_id = menu_last_used_dev;
687 dev = get_dev(dev_id);
691 if (dev->key_names == NULL)
694 for (i = 0; i < dev->key_count; i++)
695 if (dev->key_names[i] && strcasecmp(dev->key_names[i], key_name) == 0)
701 int in_bind_key(int dev_id, int keycode, int mask, int bind_type, int force_unbind)
706 dev = get_dev(dev_id);
707 if (dev == NULL || bind_type >= IN_BINDTYPE_COUNT)
710 count = dev->key_count;
712 if (dev->binds == NULL) {
715 dev->binds = in_alloc_binds(dev->drv_id, count);
716 if (dev->binds == NULL)
720 if (keycode < 0 || keycode >= count)
724 dev->binds[IN_BIND_OFFS(keycode, bind_type)] &= ~mask;
726 dev->binds[IN_BIND_OFFS(keycode, bind_type)] ^= mask;
728 ret = DRV(dev->drv_id).clean_binds(dev->drv_data, dev->binds,
729 dev->binds + count * IN_BINDTYPE_COUNT);
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
745 void in_unbind_all(int dev_id_, int act_mask, int bind_type)
747 int dev_id = 0, dev_last = IN_MAX_DEVS - 1;
752 dev_id = dev_last = dev_id_;
754 if (bind_type >= IN_BINDTYPE_COUNT)
757 for (; dev_id <= dev_last; dev_id++) {
758 dev = &in_devices[dev_id];
759 count = dev->key_count;
761 if (dev->binds == NULL)
764 if (act_mask != -1) {
765 for (i = 0; i < count; i++)
766 dev->binds[IN_BIND_OFFS(i, bind_type)] &= ~act_mask;
769 memset(dev->binds, 0, sizeof(dev->binds[0]) * count * IN_BINDTYPE_COUNT);
773 /* returns device id, or -1 on error */
774 int in_config_parse_dev(const char *name)
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) {
787 lprintf("input: missing driver for '%s'\n", name);
791 for (i = 0; i < in_dev_count; i++)
793 if (in_devices[i].name == NULL)
795 if (strcmp(in_devices[i].name, name) == 0)
799 if (i >= IN_MAX_DEVS)
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",
811 memset(&in_devices[i], 0, sizeof(in_devices[i]));
813 in_devices[i].name = strdup(name);
814 if (in_devices[i].name == NULL)
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;
821 if (i + 1 > in_dev_count)
822 in_dev_count = i + 1;
827 int in_config_bind_key(int dev_id, const char *key, int acts, int bind_type)
832 dev = get_dev(dev_id);
833 if (dev == NULL || bind_type >= IN_BINDTYPE_COUNT)
836 /* maybe a raw code? */
837 if (key[0] == '\\' && key[1] == 'x') {
839 kc = (int)strtoul(key + 2, &p, 16);
840 if (p == NULL || *p != 0)
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)
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) {
863 kc = DRV(dev->drv_id).get_key_code(key);
864 if (kc < 0 && strlen(key) == 1) {
865 /* assume scancode */
870 if (kc < 0 || kc >= dev->key_count) {
871 lprintf("input: bad key: '%s' for device '%s'\n",
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;
882 offs = IN_BIND_OFFS(kc, bind_type);
883 if (dev->binds[offs] == -1)
884 dev->binds[offs] = 0;
885 dev->binds[offs] |= acts;
889 void in_clean_binds(void)
893 for (i = 0; i < IN_MAX_DEVS; i++) {
894 int ret, count, *binds, *def_binds;
895 in_dev_t *dev = &in_devices[i];
897 if (dev->binds == NULL || !dev->probed)
900 count = dev->key_count;
902 def_binds = binds + count * IN_BINDTYPE_COUNT;
904 ret = DRV(dev->drv_id).clean_binds(dev->drv_data, binds, def_binds);
906 /* no useable binds */
913 void in_debug_dump(void)
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)
922 lprintf("%d %3d %6c %5c %s\n", i, d->drv_id,
923 d->probed ? 'y' : 'n',
924 d->binds ? 'y' : 'n', d->name);
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];
934 lprintf(" [%3d] =", kc);
935 for (t = 0; t < IN_BINDTYPE_COUNT; t++)
936 printf(" %x", d->binds[o + t]);
944 /* stubs for drivers that choose not to implement something */
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; }
956 #define CHECK_ADD_STUB(d, f) \
957 if (d.f == NULL) d.f = in_def_##f
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)
963 int count_new = in_driver_count + 1;
964 in_drv_t *new_drivers;
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");
972 memcpy(&new_drivers[in_driver_count], drv, sizeof(new_drivers[0]));
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);
984 new_drivers[in_driver_count].pdata = pdata;
986 new_drivers[in_driver_count].defbinds = defbinds;
987 in_drivers = new_drivers;
988 in_driver_count = count_new;
996 memset(in_devices, 0, sizeof(in_devices));
1014 ret = in_update_keycode(&dev, &down);
1015 lprintf("#%i: %i %i (%s)\n", dev, down, ret, in_get_key_name(dev, ret));
1019 ret = in_menu_wait_any();
1020 lprintf("%08x\n", ret);