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;
46 static int menu_key_prev = 0;
47 static int menu_key_mask = 0;
48 static int menu_key_repeat = 0;
50 #define DRV(id) in_drivers[id]
53 static int *in_alloc_binds(int drv_id, int key_count)
55 const struct in_default_bind *defbinds;
59 binds = calloc(key_count * IN_BINDTYPE_COUNT * 2, sizeof(binds[0]));
63 binds_d = binds + key_count * IN_BINDTYPE_COUNT;
65 /* always have a copy of defbinds */
66 defbinds = DRV(drv_id).defbinds;
67 if (defbinds != NULL) {
69 if (defbinds[i].code == 0 && defbinds[i].btype == 0
70 && defbinds[i].bit == 0)
73 binds_d[IN_BIND_OFFS(defbinds[i].code, defbinds[i].btype)] |=
81 static void in_unprobe(in_dev_t *dev)
84 DRV(dev->drv_id).free(dev->drv_data);
89 static void in_free(in_dev_t *dev)
98 /* to be called by drivers
99 * async devices must set drv_fd_hnd to -1 */
100 void in_register(const char *nname, int drv_fd_hnd, void *drv_data,
101 int key_count, const char * const *key_names, int combos)
103 int i, ret, dupe_count = 0, *binds;
104 char name[256], *name_end, *tmp;
106 strncpy(name, nname, sizeof(name));
107 name[sizeof(name)-12] = 0;
108 name_end = name + strlen(name);
110 for (i = 0; i < in_dev_count; i++)
112 if (in_devices[i].name == NULL)
114 if (strcmp(in_devices[i].name, name) == 0)
116 if (in_devices[i].probed) {
118 sprintf(name_end, " [%d]", dupe_count);
125 if (i >= IN_MAX_DEVS)
127 /* try to find unused device */
128 for (i = 0; i < IN_MAX_DEVS; i++)
129 if (!in_devices[i].probed) break;
130 if (i >= IN_MAX_DEVS) {
131 lprintf("input: too many devices, can't add %s\n", name);
134 in_free(&in_devices[i]);
141 binds = in_alloc_binds(in_probe_dev_id, key_count);
147 memcpy(binds, binds + key_count * IN_BINDTYPE_COUNT,
148 sizeof(binds[0]) * key_count * IN_BINDTYPE_COUNT);
150 in_devices[i].name = tmp;
151 in_devices[i].binds = binds;
152 in_devices[i].key_count = key_count;
153 if (i + 1 > in_dev_count)
154 in_dev_count = i + 1;
156 lprintf("input: new device #%d \"%s\"\n", i, name);
158 in_devices[i].probed = 1;
159 in_devices[i].does_combos = combos;
160 in_devices[i].drv_id = in_probe_dev_id;
161 in_devices[i].drv_fd_hnd = drv_fd_hnd;
162 in_devices[i].key_names = key_names;
163 in_devices[i].drv_data = drv_data;
165 if (in_devices[i].binds != NULL) {
166 ret = DRV(in_probe_dev_id).clean_binds(drv_data, in_devices[i].binds,
167 in_devices[i].binds + key_count * IN_BINDTYPE_COUNT);
169 /* no useable binds */
170 free(in_devices[i].binds);
171 in_devices[i].binds = NULL;
176 /* key combo handling, to be called by drivers that support it.
177 * Only care about IN_BINDTYPE_EMU */
178 void in_combos_find(const int *binds, int last_key, int *combo_keys, int *combo_acts)
182 *combo_keys = *combo_acts = 0;
183 for (act = 0; act < sizeof(binds[0]) * 8; act++)
186 for (u = 0; u <= last_key; u++)
187 if (binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)] & (1 << act))
192 // loop again and mark those keys and actions as combo
193 for (u = 0; u <= last_key; u++)
195 if (binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)] & (1 << act)) {
196 *combo_keys |= 1 << u;
197 *combo_acts |= 1 << act;
204 int in_combos_do(int keys, const int *binds, int last_key, int combo_keys, int combo_acts)
208 for (i = 0; i <= last_key; i++)
212 if (!(keys & (1 << i)))
215 acts = binds[IN_BIND_OFFS(i, IN_BINDTYPE_EMU)];
219 if (!(combo_keys & (1 << i))) {
224 acts_c = acts & combo_acts;
227 // let's try to find the other one
228 for (u = i + 1; u <= last_key; u++)
229 if ( (keys & (1 << u)) && (binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)] & acts_c) ) {
230 ret |= acts_c & binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)];
231 keys &= ~((1 << i) | (1 << u));
235 // add non-combo actions if combo ones were not found
237 ret |= acts & ~combo_acts;
247 in_have_async_devs = 0;
249 menu_last_used_dev = 0;
251 for (i = 0; i < in_dev_count; i++)
252 in_unprobe(&in_devices[i]);
254 for (i = 0; i < in_driver_count; i++) {
256 in_drivers[i].probe(&DRV(i));
259 /* get rid of devs without binds and probes */
260 for (i = 0; i < in_dev_count; i++) {
261 if (!in_devices[i].probed && in_devices[i].binds == NULL) {
263 if (i < in_dev_count) {
264 free(in_devices[i].name);
265 memmove(&in_devices[i], &in_devices[i+1],
266 (in_dev_count - i) * sizeof(in_devices[0]));
272 if (in_devices[i].probed && in_devices[i].drv_fd_hnd == -1)
273 in_have_async_devs = 1;
276 if (in_have_async_devs)
277 lprintf("input: async-only devices detected..\n");
283 int in_update(int *result)
287 for (i = 0; i < in_dev_count; i++) {
288 in_dev_t *dev = &in_devices[i];
289 if (dev->probed && dev->binds != NULL)
290 ret |= DRV(dev->drv_id).update(dev->drv_data, dev->binds, result);
296 static in_dev_t *get_dev(int dev_id)
298 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
301 return &in_devices[dev_id];
304 int in_update_analog(int dev_id, int axis_id, int *result)
306 in_dev_t *dev = get_dev(dev_id);
308 if (dev == NULL || !dev->probed)
311 return DRV(dev->drv_id).update_analog(dev->drv_data, axis_id, result);
314 static int in_update_kc_async(int *dev_id_out, int *is_down_out, int timeout_ms)
316 int i, is_down, result;
319 ticks = plat_get_ticks_ms();
323 for (i = 0; i < in_dev_count; i++) {
324 in_dev_t *d = &in_devices[i];
328 result = DRV(d->drv_id).update_keycode(d->drv_data, &is_down);
335 *is_down_out = is_down;
339 if (timeout_ms >= 0 && (int)(plat_get_ticks_ms() - ticks) > timeout_ms)
349 * wait for a press, always return some keycode or -1 on timeout or error
351 int in_update_keycode(int *dev_id_out, int *is_down_out, char *charcode, int timeout_ms)
353 int result = -1, dev_id = 0, is_down, result_menu;
354 int fds_hnds[IN_MAX_DEVS];
355 int i, ret, count = 0;
356 in_drv_t *drv = NULL;
359 if (in_have_async_devs) {
360 result = in_update_kc_async(&dev_id, &is_down, timeout_ms);
363 drv = &DRV(in_devices[dev_id].drv_id);
367 ticks = plat_get_ticks_ms();
369 for (i = 0; i < in_dev_count; i++) {
370 if (in_devices[i].probed)
371 fds_hnds[count++] = in_devices[i].drv_fd_hnd;
375 /* don't deadlock, fail */
376 lprintf("input: failed to find devices to read\n");
382 ret = plat_wait_event(fds_hnds, count, timeout_ms);
386 for (i = 0; i < in_dev_count; i++) {
387 if (in_devices[i].drv_fd_hnd == ret) {
393 drv = &DRV(in_devices[dev_id].drv_id);
394 result = drv->update_keycode(in_devices[dev_id].drv_data, &is_down);
399 lprintf("input: \"%s\" errored out, removing.\n", in_devices[dev_id].name);
400 in_unprobe(&in_devices[dev_id]);
404 if (timeout_ms >= 0) {
405 unsigned int ticks2 = plat_get_ticks_ms();
406 timeout_ms -= ticks2 - ticks;
416 /* keep track of menu key state, to allow mixing
417 * in_update_keycode() and in_menu_wait_any() calls */
418 result_menu = drv->menu_translate(in_devices[dev_id].drv_data, result, charcode);
419 if (result_menu != 0) {
421 menu_key_state |= result_menu;
423 menu_key_state &= ~result_menu;
426 if (dev_id_out != NULL)
427 *dev_id_out = dev_id;
428 if (is_down_out != NULL)
429 *is_down_out = is_down;
433 /* same as above, only return bitfield of PBTN_* */
434 int in_menu_wait_any(char *charcode, int timeout_ms)
436 int keys_old = menu_key_state;
440 menu_key_prev = menu_key_state;
442 in_update_keycode(&dev_id, NULL, charcode, timeout_ms);
444 if (keys_old != menu_key_state)
445 menu_last_used_dev = dev_id;
447 ret = menu_key_state;
449 menu_key_mask = menu_key_prev = 0;
450 else if (ret != menu_key_prev)
451 menu_key_mask = menu_key_prev;
456 /* wait for menu input, do autorepeat */
457 int in_menu_wait(int interesting, char *charcode, int autorep_delay_ms)
462 wait = autorep_delay_ms;
464 /* wait until either key repeat or a new key has been pressed */
466 ret = in_menu_wait_any(charcode, wait);
467 if (ret == 0 || ret != menu_key_prev)
472 /* mask away all old keys if an additional new key is pressed */
473 /* XXX what if old and new keys share bits (PBTN_CHAR)? */
474 ret &= ~menu_key_mask;
475 } while (!(ret & interesting));
477 /* we don't need diagonals in menus */
478 if (ret & (PBTN_UP|PBTN_DOWN)) ret &= ~(PBTN_LEFT|PBTN_RIGHT);
483 const int *in_get_dev_binds(int dev_id)
485 in_dev_t *dev = get_dev(dev_id);
487 return dev ? dev->binds : NULL;
490 const int *in_get_dev_def_binds(int dev_id)
492 in_dev_t *dev = get_dev(dev_id);
495 if (dev->binds == NULL)
498 return dev->binds + dev->key_count * IN_BINDTYPE_COUNT;
501 int in_get_config(int dev_id, int what, void *val)
506 dev = get_dev(dev_id);
507 if (dev == NULL || val == NULL)
511 case IN_CFG_BIND_COUNT:
512 *ival = dev->key_count;
514 case IN_CFG_DOES_COMBOS:
515 *ival = dev->does_combos;
517 case IN_CFG_BLOCKING:
518 case IN_CFG_KEY_NAMES:
519 return -1; /* not implemented */
524 return DRV(dev->drv_id).get_config(dev->drv_data, what, ival);
530 static int in_set_blocking(int is_blocking)
534 /* have_async_devs means we will have to do all reads async anyway.. */
535 if (!in_have_async_devs) {
536 for (i = 0; i < in_dev_count; i++) {
537 if (!in_devices[i].probed)
540 DRV(in_devices[i].drv_id).set_config(
541 in_devices[i].drv_data, IN_CFG_BLOCKING,
548 ret = in_update_keycode(NULL, NULL, NULL, 0);
556 int in_set_config(int dev_id, int what, const void *val, int size)
558 const char * const *names;
559 const int *ival = val;
563 if (what == IN_CFG_BLOCKING)
564 return in_set_blocking(*ival);
566 dev = get_dev(dev_id);
571 case IN_CFG_KEY_NAMES:
573 count = size / sizeof(names[0]);
575 if (count < dev->key_count) {
576 lprintf("input: set_key_names: not enough keys\n");
580 dev->key_names = names;
582 case IN_CFG_DEFAULT_DEV:
583 /* just set last used dev, for now */
584 menu_last_used_dev = dev_id;
591 return DRV(dev->drv_id).set_config(dev->drv_data, what, *ival);
596 const char *in_get_dev_name(int dev_id, int must_be_active, int skip_pfix)
598 const char *name, *tmp;
601 dev = get_dev(dev_id);
605 if (must_be_active && !dev->probed)
609 if (name == NULL || !skip_pfix)
613 tmp = strchr(name, ':');
620 int in_name_to_id(const char *dev_name)
624 for (i = 0; i < in_dev_count; i++)
625 if (strcmp(dev_name, in_devices[i].name) == 0)
628 if (i >= in_dev_count) {
629 lprintf("input: in_name_to_id: no such device: %s\n", dev_name);
636 /* never returns NULL */
637 const char *in_get_key_name(int dev_id, int keycode)
639 const char *name = NULL;
640 static char xname[16];
644 if (dev_id < 0) /* want last used dev? */
645 dev_id = menu_last_used_dev;
647 dev = get_dev(dev_id);
651 drv = &DRV(dev->drv_id);
652 if (keycode < 0) /* want name for menu key? */
653 keycode = drv->menu_translate(dev->drv_data, keycode, NULL);
655 if (dev->key_names != NULL && 0 <= keycode && keycode < dev->key_count)
656 name = dev->key_names[keycode];
660 if (drv->get_key_name != NULL)
661 name = drv->get_key_name(keycode);
665 /* assume scancode */
666 if ((keycode >= '0' && keycode <= '9') || (keycode >= 'a' && keycode <= 'z')
667 || (keycode >= 'A' && keycode <= 'Z'))
668 sprintf(xname, "%c", keycode);
670 sprintf(xname, "\\x%02X", keycode);
674 int in_get_key_code(int dev_id, const char *key_name)
679 if (dev_id < 0) /* want last used dev? */
680 dev_id = menu_last_used_dev;
682 dev = get_dev(dev_id);
686 if (dev->key_names == NULL)
689 for (i = 0; i < dev->key_count; i++)
690 if (dev->key_names[i] && strcasecmp(dev->key_names[i], key_name) == 0)
696 int in_bind_key(int dev_id, int keycode, int mask, int bind_type, int force_unbind)
701 dev = get_dev(dev_id);
702 if (dev == NULL || bind_type >= IN_BINDTYPE_COUNT)
705 count = dev->key_count;
707 if (dev->binds == NULL) {
710 dev->binds = in_alloc_binds(dev->drv_id, count);
711 if (dev->binds == NULL)
715 if (keycode < 0 || keycode >= count)
719 dev->binds[IN_BIND_OFFS(keycode, bind_type)] &= ~mask;
721 dev->binds[IN_BIND_OFFS(keycode, bind_type)] ^= mask;
723 ret = DRV(dev->drv_id).clean_binds(dev->drv_data, dev->binds,
724 dev->binds + count * IN_BINDTYPE_COUNT);
734 * Unbind act_mask on binds with type bind_type
735 * - if dev_id_ < 0, affects all devices
736 * else only affects dev_id_
737 * - if act_mask == -1, unbind all keys
738 * else only actions in mask
740 void in_unbind_all(int dev_id_, int act_mask, int bind_type)
742 int dev_id = 0, dev_last = IN_MAX_DEVS - 1;
747 dev_id = dev_last = dev_id_;
749 if (bind_type >= IN_BINDTYPE_COUNT)
752 for (; dev_id <= dev_last; dev_id++) {
753 dev = &in_devices[dev_id];
754 count = dev->key_count;
756 if (dev->binds == NULL)
759 if (act_mask != -1) {
760 for (i = 0; i < count; i++)
761 dev->binds[IN_BIND_OFFS(i, bind_type)] &= ~act_mask;
764 memset(dev->binds, 0, sizeof(dev->binds[0]) * count * IN_BINDTYPE_COUNT);
768 /* returns device id, or -1 on error */
769 int in_config_parse_dev(const char *name)
773 for (i = 0; i < in_driver_count; i++) {
774 int len = strlen(in_drivers[i].prefix);
775 if (strncmp(name, in_drivers[i].prefix, len) == 0) {
782 lprintf("input: missing driver for '%s'\n", name);
786 for (i = 0; i < in_dev_count; i++)
788 if (in_devices[i].name == NULL)
790 if (strcmp(in_devices[i].name, name) == 0)
794 if (i >= IN_MAX_DEVS)
796 /* try to find unused device */
797 for (i = 0; i < IN_MAX_DEVS; i++)
798 if (in_devices[i].name == NULL) break;
799 if (i >= IN_MAX_DEVS) {
800 lprintf("input: too many devices, can't add '%s'\n",
806 memset(&in_devices[i], 0, sizeof(in_devices[i]));
808 in_devices[i].name = strdup(name);
809 if (in_devices[i].name == NULL)
812 in_devices[i].key_names = DRV(drv_id).get_key_names(&DRV(drv_id),
813 &in_devices[i].key_count);
814 in_devices[i].drv_id = drv_id;
816 if (i + 1 > in_dev_count)
817 in_dev_count = i + 1;
822 int in_config_bind_key(int dev_id, const char *key, int acts, int bind_type)
827 dev = get_dev(dev_id);
828 if (dev == NULL || bind_type >= IN_BINDTYPE_COUNT)
831 /* maybe a raw code? */
832 if (key[0] == '\\' && key[1] == 'x') {
834 kc = (int)strtoul(key + 2, &p, 16);
835 if (p == NULL || *p != 0)
839 /* device specific key name */
840 if (dev->binds == NULL) {
841 dev->binds = in_alloc_binds(dev->drv_id, dev->key_count);
842 if (dev->binds == NULL)
847 if (dev->key_names != NULL) {
848 for (i = 0; i < dev->key_count; i++) {
849 const char *k = dev->key_names[i];
850 if (k != NULL && strcasecmp(k, key) == 0) {
858 kc = DRV(dev->drv_id).get_key_code(key);
859 if (kc < 0 && strlen(key) == 1) {
860 /* assume scancode */
865 if (kc < 0 || kc >= dev->key_count) {
866 lprintf("input: bad key: '%s' for device '%s'\n",
871 if (bind_type == IN_BINDTYPE_NONE) {
872 for (i = 0; i < IN_BINDTYPE_COUNT; i++)
873 dev->binds[IN_BIND_OFFS(kc, i)] = 0;
877 offs = IN_BIND_OFFS(kc, bind_type);
878 if (dev->binds[offs] == -1)
879 dev->binds[offs] = 0;
880 dev->binds[offs] |= acts;
884 void in_clean_binds(void)
888 for (i = 0; i < IN_MAX_DEVS; i++) {
889 int ret, count, *binds, *def_binds;
890 in_dev_t *dev = &in_devices[i];
892 if (dev->binds == NULL || !dev->probed)
895 count = dev->key_count;
897 def_binds = binds + count * IN_BINDTYPE_COUNT;
899 ret = DRV(dev->drv_id).clean_binds(dev->drv_data, binds, def_binds);
901 /* no useable binds */
908 void in_debug_dump(void)
912 lprintf("# drv probed binds name\n");
913 for (i = 0; i < IN_MAX_DEVS; i++) {
914 in_dev_t *d = &in_devices[i];
915 if (!d->probed && d->name == NULL && d->binds == NULL)
917 lprintf("%d %3d %6c %5c %s\n", i, d->drv_id,
918 d->probed ? 'y' : 'n',
919 d->binds ? 'y' : 'n', d->name);
923 for (kc = 0; kc < d->key_count; kc++) {
924 o = IN_BIND_OFFS(kc, 0);
925 for (t = h = 0; t < IN_BINDTYPE_COUNT; t++)
926 h |= d->binds[o + t];
929 lprintf(" [%3d] =", kc);
930 for (t = 0; t < IN_BINDTYPE_COUNT; t++)
931 printf(" %x", d->binds[o + t]);
939 /* stubs for drivers that choose not to implement something */
941 static void in_def_free(void *drv_data) {}
942 static int in_def_clean_binds(void *drv_data, int *b, int *db) { return 1; }
943 static int in_def_get_config(void *drv_data, int what, int *val) { return -1; }
944 static int in_def_set_config(void *drv_data, int what, int val) { return -1; }
945 static int in_def_update_analog(void *drv_data, int axis_id, int *result) { return -1; }
946 static int in_def_update_keycode(void *drv_data, int *is_down) { return 0; }
947 static int in_def_menu_translate(void *drv_data, int keycode, char *ccode) { return 0; }
948 static int in_def_get_key_code(const char *key_name) { return -1; }
949 static const char *in_def_get_key_name(int keycode) { return NULL; }
951 #define CHECK_ADD_STUB(d, f) \
952 if (d.f == NULL) d.f = in_def_##f
954 /* to be called by drivers */
955 int in_register_driver(const in_drv_t *drv,
956 const struct in_default_bind *defbinds, const void *pdata)
958 int count_new = in_driver_count + 1;
959 in_drv_t *new_drivers;
961 new_drivers = realloc(in_drivers, count_new * sizeof(in_drivers[0]));
962 if (new_drivers == NULL) {
963 lprintf("input: in_register_driver OOM\n");
967 memcpy(&new_drivers[in_driver_count], drv, sizeof(new_drivers[0]));
969 CHECK_ADD_STUB(new_drivers[in_driver_count], free);
970 CHECK_ADD_STUB(new_drivers[in_driver_count], clean_binds);
971 CHECK_ADD_STUB(new_drivers[in_driver_count], get_config);
972 CHECK_ADD_STUB(new_drivers[in_driver_count], set_config);
973 CHECK_ADD_STUB(new_drivers[in_driver_count], update_analog);
974 CHECK_ADD_STUB(new_drivers[in_driver_count], update_keycode);
975 CHECK_ADD_STUB(new_drivers[in_driver_count], menu_translate);
976 CHECK_ADD_STUB(new_drivers[in_driver_count], get_key_code);
977 CHECK_ADD_STUB(new_drivers[in_driver_count], get_key_name);
979 new_drivers[in_driver_count].pdata = pdata;
981 new_drivers[in_driver_count].defbinds = defbinds;
982 in_drivers = new_drivers;
983 in_driver_count = count_new;
991 memset(in_devices, 0, sizeof(in_devices));
1009 ret = in_update_keycode(&dev, &down);
1010 lprintf("#%i: %i %i (%s)\n", dev, down, ret, in_get_key_name(dev, ret));
1014 ret = in_menu_wait_any();
1015 lprintf("%08x\n", ret);