2 * (C) GraÅžvydas "notaz" Ignotas, 2008-2010
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.
8 * See the COPYING file in the top-level directory.
20 #include "../linux/in_evdev.h"
23 #include "../gp2x/in_gp2x.h"
26 #include "../win32/in_vk.h"
36 int *binds; /* total = key_count * bindtypes * 2 */
37 const char * const *key_names;
38 unsigned int probed:1;
39 unsigned int does_combos:1;
42 static in_drv_t in_drivers[IN_DRVID_COUNT];
43 static in_dev_t in_devices[IN_MAX_DEVS];
44 static int in_dev_count = 0; /* probed + bind devices */
45 static int in_have_async_devs = 0;
46 static int menu_key_state = 0;
47 static int menu_last_used_dev = 0;
49 #define DRV(id) in_drivers[(unsigned)(id) < IN_DRVID_COUNT ? (id) : 0]
52 static int *in_alloc_binds(int drv_id, int key_count)
56 binds = calloc(key_count * IN_BINDTYPE_COUNT * 2, sizeof(binds[0]));
60 DRV(drv_id).get_def_binds(binds + key_count * IN_BINDTYPE_COUNT);
61 memcpy(binds, binds + key_count * IN_BINDTYPE_COUNT,
62 sizeof(binds[0]) * key_count * IN_BINDTYPE_COUNT);
67 static void in_unprobe(in_dev_t *dev)
70 DRV(dev->drv_id).free(dev->drv_data);
75 static void in_free(in_dev_t *dev)
84 /* to be called by drivers
85 * async devices must set drv_fd_hnd to -1 */
86 void in_register(const char *nname, int drv_id, int drv_fd_hnd, void *drv_data,
87 int key_count, const char * const *key_names, int combos)
89 int i, ret, dupe_count = 0, *binds;
90 char name[256], *name_end, *tmp;
92 strncpy(name, nname, sizeof(name));
93 name[sizeof(name)-12] = 0;
94 name_end = name + strlen(name);
96 for (i = 0; i < in_dev_count; i++)
98 if (in_devices[i].name == NULL)
100 if (strcmp(in_devices[i].name, name) == 0)
102 if (in_devices[i].probed) {
104 sprintf(name_end, " [%d]", dupe_count);
111 if (i >= IN_MAX_DEVS)
113 /* try to find unused device */
114 for (i = 0; i < IN_MAX_DEVS; i++)
115 if (!in_devices[i].probed) break;
116 if (i >= IN_MAX_DEVS) {
117 lprintf("input: too many devices, can't add %s\n", name);
120 in_free(&in_devices[i]);
127 binds = in_alloc_binds(drv_id, key_count);
133 in_devices[i].name = tmp;
134 in_devices[i].binds = binds;
135 in_devices[i].key_count = key_count;
136 if (i + 1 > in_dev_count)
137 in_dev_count = i + 1;
139 lprintf("input: new device #%d \"%s\"\n", i, name);
141 in_devices[i].probed = 1;
142 in_devices[i].does_combos = combos;
143 in_devices[i].drv_id = drv_id;
144 in_devices[i].drv_fd_hnd = drv_fd_hnd;
145 in_devices[i].key_names = key_names;
146 in_devices[i].drv_data = drv_data;
148 if (in_devices[i].binds != NULL) {
149 ret = DRV(drv_id).clean_binds(drv_data, in_devices[i].binds,
150 in_devices[i].binds + key_count * IN_BINDTYPE_COUNT);
152 /* no useable binds */
153 free(in_devices[i].binds);
154 in_devices[i].binds = NULL;
159 /* key combo handling, to be called by drivers that support it.
160 * Only care about IN_BINDTYPE_EMU */
161 void in_combos_find(const int *binds, int last_key, int *combo_keys, int *combo_acts)
165 *combo_keys = *combo_acts = 0;
166 for (act = 0; act < sizeof(binds[0]) * 8; act++)
169 for (u = 0; u <= last_key; u++)
170 if (binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)] & (1 << act))
175 // loop again and mark those keys and actions as combo
176 for (u = 0; u <= last_key; u++)
178 if (binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)] & (1 << act)) {
179 *combo_keys |= 1 << u;
180 *combo_acts |= 1 << act;
187 int in_combos_do(int keys, const int *binds, int last_key, int combo_keys, int combo_acts)
191 for (i = 0; i <= last_key; i++)
195 if (!(keys & (1 << i)))
198 acts = binds[IN_BIND_OFFS(i, IN_BINDTYPE_EMU)];
202 if (!(combo_keys & (1 << i))) {
207 acts_c = acts & combo_acts;
210 // let's try to find the other one
211 for (u = i + 1; u <= last_key; u++)
212 if ( (keys & (1 << u)) && (binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)] & acts_c) ) {
213 ret |= acts_c & binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)];
214 keys &= ~((1 << i) | (1 << u));
218 // add non-combo actions if combo ones were not found
220 ret |= acts & ~combo_acts;
230 in_have_async_devs = 0;
232 menu_last_used_dev = 0;
234 for (i = 0; i < in_dev_count; i++)
235 in_unprobe(&in_devices[i]);
237 for (i = 1; i < IN_DRVID_COUNT; i++)
238 in_drivers[i].probe();
240 /* get rid of devs without binds and probes */
241 for (i = 0; i < in_dev_count; i++) {
242 if (!in_devices[i].probed && in_devices[i].binds == NULL) {
244 if (i < in_dev_count) {
245 free(in_devices[i].name);
246 memmove(&in_devices[i], &in_devices[i+1],
247 (in_dev_count - i) * sizeof(in_devices[0]));
253 if (in_devices[i].probed && in_devices[i].drv_fd_hnd == -1)
254 in_have_async_devs = 1;
257 if (in_have_async_devs)
258 lprintf("input: async-only devices detected..\n");
264 int in_update(int *result)
268 for (i = 0; i < in_dev_count; i++) {
269 in_dev_t *dev = &in_devices[i];
270 if (dev->probed && dev->binds != NULL) {
271 // FIXME: this is stupid, make it indirect
272 switch (dev->drv_id) {
275 ret |= in_evdev_update(dev->drv_data, dev->binds, result);
280 ret |= in_gp2x_update(dev->drv_data, dev->binds, result);
285 ret |= in_vk_update(dev->drv_data, dev->binds, result);
295 static int in_update_kc_async(int *dev_id_out, int *is_down_out, int timeout_ms)
297 int i, is_down, result;
300 ticks = plat_get_ticks_ms();
304 for (i = 0; i < in_dev_count; i++) {
305 in_dev_t *d = &in_devices[i];
309 result = DRV(d->drv_id).update_keycode(d->drv_data, &is_down);
316 *is_down_out = is_down;
320 if (timeout_ms >= 0 && (int)(plat_get_ticks_ms() - ticks) > timeout_ms)
330 * wait for a press, always return some keycode or -1 on timeout or error
332 int in_update_keycode(int *dev_id_out, int *is_down_out, int timeout_ms)
334 int result = -1, dev_id = 0, is_down, result_menu;
335 int fds_hnds[IN_MAX_DEVS];
336 int i, ret, count = 0;
337 in_drv_t *drv = NULL;
340 if (in_have_async_devs) {
341 result = in_update_kc_async(&dev_id, &is_down, timeout_ms);
344 drv = &DRV(in_devices[dev_id].drv_id);
348 ticks = plat_get_ticks_ms();
350 for (i = 0; i < in_dev_count; i++) {
351 if (in_devices[i].probed)
352 fds_hnds[count++] = in_devices[i].drv_fd_hnd;
356 /* don't deadlock, fail */
357 lprintf("input: failed to find devices to read\n");
363 ret = plat_wait_event(fds_hnds, count, timeout_ms);
367 for (i = 0; i < in_dev_count; i++) {
368 if (in_devices[i].drv_fd_hnd == ret) {
374 drv = &DRV(in_devices[dev_id].drv_id);
375 result = drv->update_keycode(in_devices[dev_id].drv_data, &is_down);
380 lprintf("input: \"%s\" errored out, removing.\n", in_devices[dev_id].name);
381 in_unprobe(&in_devices[dev_id]);
385 if (timeout_ms >= 0) {
386 unsigned int ticks2 = plat_get_ticks_ms();
387 timeout_ms -= ticks2 - ticks;
397 /* keep track of menu key state, to allow mixing
398 * in_update_keycode() and in_menu_wait_any() calls */
399 result_menu = drv->menu_translate(in_devices[dev_id].drv_data, result);
400 if (result_menu != 0) {
402 menu_key_state |= result_menu;
404 menu_key_state &= ~result_menu;
407 if (dev_id_out != NULL)
408 *dev_id_out = dev_id;
409 if (is_down_out != NULL)
410 *is_down_out = is_down;
414 /* same as above, only return bitfield of PBTN_* */
415 int in_menu_wait_any(int timeout_ms)
417 int keys_old = menu_key_state;
421 int code, is_down = 0, dev_id = 0;
423 code = in_update_keycode(&dev_id, &is_down, timeout_ms);
427 if (keys_old != menu_key_state) {
428 menu_last_used_dev = dev_id;
433 return menu_key_state;
436 /* wait for menu input, do autorepeat */
437 int in_menu_wait(int interesting, int autorep_delay_ms)
439 static int inp_prev = 0;
440 static int repeats = 0;
441 int ret, release = 0, wait = 450;
444 wait = autorep_delay_ms;
446 ret = in_menu_wait_any(wait);
450 while (!(ret & interesting)) {
451 ret = in_menu_wait_any(-1);
455 if (release || ret != inp_prev)
460 /* we don't need diagonals in menus */
461 if ((ret & PBTN_UP) && (ret & PBTN_LEFT)) ret &= ~PBTN_LEFT;
462 if ((ret & PBTN_UP) && (ret & PBTN_RIGHT)) ret &= ~PBTN_RIGHT;
463 if ((ret & PBTN_DOWN) && (ret & PBTN_LEFT)) ret &= ~PBTN_LEFT;
464 if ((ret & PBTN_DOWN) && (ret & PBTN_RIGHT)) ret &= ~PBTN_RIGHT;
469 const int *in_get_dev_binds(int dev_id)
471 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
474 return in_devices[dev_id].binds;
477 const int *in_get_dev_def_binds(int dev_id)
479 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
482 return in_devices[dev_id].binds + in_devices[dev_id].key_count * IN_BINDTYPE_COUNT;
485 int in_get_config(int dev_id, int what, void *val)
490 if (dev_id < 0 || dev_id >= IN_MAX_DEVS || val == NULL)
493 dev = &in_devices[dev_id];
495 case IN_CFG_BIND_COUNT:
496 *ival = dev->key_count;
498 case IN_CFG_DOES_COMBOS:
499 *ival = dev->does_combos;
501 case IN_CFG_BLOCKING:
502 case IN_CFG_KEY_NAMES:
503 return -1; /* not implemented */
505 return DRV(dev->drv_id).get_config(dev->drv_data, what, ival);
511 static int in_set_blocking(int is_blocking)
515 /* have_async_devs means we will have to do all reads async anyway.. */
516 if (!in_have_async_devs) {
517 for (i = 0; i < in_dev_count; i++) {
518 if (in_devices[i].probed)
519 DRV(in_devices[i].drv_id).set_config(in_devices[i].drv_data,
520 IN_CFG_BLOCKING, is_blocking);
528 ret = in_update_keycode(NULL, NULL, 0);
534 int in_set_config(int dev_id, int what, const void *val, int size)
536 const int *ival = val;
539 if (what == IN_CFG_BLOCKING)
540 return in_set_blocking(*ival);
542 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
545 dev = &in_devices[dev_id];
546 if (what == IN_CFG_KEY_NAMES) {
547 const char * const *names = val;
548 int count = size / sizeof(names[0]);
550 if (count < dev->key_count) {
551 lprintf("input: set_key_names: not enough keys\n");
555 dev->key_names = names;
559 return DRV(dev->drv_id).set_config(dev->drv_data, what, *ival);
562 const char *in_get_dev_name(int dev_id, int must_be_active, int skip_pfix)
564 const char *name, *tmp;
566 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
569 if (must_be_active && !in_devices[dev_id].probed)
572 name = in_devices[dev_id].name;
573 if (name == NULL || !skip_pfix)
577 tmp = strchr(name, ':');
584 int in_name_to_id(const char *dev_name)
588 for (i = 0; i < in_dev_count; i++)
589 if (strcmp(dev_name, in_devices[i].name) == 0)
592 if (i >= in_dev_count) {
593 lprintf("input: in_name_to_id: no such device: %s\n", dev_name);
600 /* never returns NULL */
601 const char *in_get_key_name(int dev_id, int keycode)
603 const char *name = NULL;
604 static char xname[16];
607 if (dev_id < 0) /* want last used dev? */
608 dev_id = menu_last_used_dev;
610 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
613 dev = &in_devices[dev_id];
614 if (keycode < 0) /* want name for menu key? */
615 keycode = DRV(dev->drv_id).menu_translate(dev->drv_data, keycode);
617 if (dev->key_names != NULL && 0 <= keycode && keycode < dev->key_count)
618 name = dev->key_names[keycode];
622 name = DRV(dev->drv_id).get_key_name(keycode);
626 /* assume scancode */
627 if ((keycode >= '0' && keycode <= '9') || (keycode >= 'a' && keycode <= 'z')
628 || (keycode >= 'A' && keycode <= 'Z'))
629 sprintf(xname, "%c", keycode);
631 sprintf(xname, "\\x%02X", keycode);
635 int in_get_key_code(int dev_id, const char *key_name)
640 if (dev_id < 0) /* want last used dev? */
641 dev_id = menu_last_used_dev;
643 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
646 dev = &in_devices[dev_id];
647 if (dev->key_names == NULL)
650 for (i = 0; i < dev->key_count; i++)
651 if (dev->key_names[i] && strcasecmp(dev->key_names[i], key_name) == 0)
657 int in_bind_key(int dev_id, int keycode, int mask, int bind_type, int force_unbind)
662 if (dev_id < 0 || dev_id >= IN_MAX_DEVS || bind_type >= IN_BINDTYPE_COUNT)
665 dev = &in_devices[dev_id];
666 count = dev->key_count;
668 if (dev->binds == NULL) {
671 dev->binds = in_alloc_binds(dev->drv_id, count);
672 if (dev->binds == NULL)
676 if (keycode < 0 || keycode >= count)
680 dev->binds[IN_BIND_OFFS(keycode, bind_type)] &= ~mask;
682 dev->binds[IN_BIND_OFFS(keycode, bind_type)] ^= mask;
684 ret = DRV(dev->drv_id).clean_binds(dev->drv_data, dev->binds,
685 dev->binds + count * IN_BINDTYPE_COUNT);
695 * Unbind act_mask on binds with type bind_type
696 * - if dev_id_ < 0, affects all devices
697 * else only affects dev_id_
698 * - if act_mask == -1, unbind all keys
699 * else only actions in mask
701 void in_unbind_all(int dev_id_, int act_mask, int bind_type)
703 int dev_id = 0, dev_last = IN_MAX_DEVS - 1;
708 dev_id = dev_last = dev_id_;
710 if (bind_type >= IN_BINDTYPE_COUNT)
713 for (; dev_id <= dev_last; dev_id++) {
714 dev = &in_devices[dev_id];
715 count = dev->key_count;
717 if (dev->binds == NULL)
720 if (act_mask != -1) {
721 for (i = 0; i < count; i++)
722 dev->binds[IN_BIND_OFFS(i, bind_type)] &= ~act_mask;
725 memset(dev->binds, 0, sizeof(dev->binds[0]) * count * IN_BINDTYPE_COUNT);
729 /* returns device id, or -1 on error */
730 int in_config_parse_dev(const char *name)
734 for (i = 0; i < IN_DRVID_COUNT; i++) {
735 int len = strlen(in_drivers[i].prefix);
736 if (strncmp(name, in_drivers[i].prefix, len) == 0) {
743 lprintf("input: missing driver for %s\n", name);
747 for (i = 0; i < in_dev_count; i++)
749 if (in_devices[i].name == NULL)
751 if (strcmp(in_devices[i].name, name) == 0)
755 if (i >= IN_MAX_DEVS)
757 /* try to find unused device */
758 for (i = 0; i < IN_MAX_DEVS; i++)
759 if (in_devices[i].name == NULL) break;
760 if (i >= IN_MAX_DEVS) {
761 lprintf("input: too many devices, can't add %s\n", name);
766 memset(&in_devices[i], 0, sizeof(in_devices[i]));
768 in_devices[i].name = strdup(name);
769 if (in_devices[i].name == NULL)
772 in_devices[i].key_names = DRV(drv_id).get_key_names(&in_devices[i].key_count);
773 in_devices[i].drv_id = drv_id;
775 if (i + 1 > in_dev_count)
776 in_dev_count = i + 1;
781 int in_config_bind_key(int dev_id, const char *key, int acts, int bind_type)
786 if (dev_id < 0 || dev_id >= IN_MAX_DEVS || bind_type >= IN_BINDTYPE_COUNT)
788 dev = &in_devices[dev_id];
790 /* maybe a raw code? */
791 if (key[0] == '\\' && key[1] == 'x') {
793 kc = (int)strtoul(key + 2, &p, 16);
794 if (p == NULL || *p != 0)
798 /* device specific key name */
799 if (dev->binds == NULL) {
800 dev->binds = in_alloc_binds(dev->drv_id, dev->key_count);
801 if (dev->binds == NULL)
806 if (dev->key_names != NULL) {
807 for (i = 0; i < dev->key_count; i++) {
808 const char *k = dev->key_names[i];
809 if (k != NULL && strcasecmp(k, key) == 0) {
817 kc = DRV(dev->drv_id).get_key_code(key);
818 if (kc < 0 && strlen(key) == 1) {
819 /* assume scancode */
824 if (kc < 0 || kc >= dev->key_count) {
825 lprintf("input: bad key: %s\n", key);
829 if (bind_type == IN_BINDTYPE_NONE) {
830 for (i = 0; i < IN_BINDTYPE_COUNT; i++)
831 dev->binds[IN_BIND_OFFS(kc, i)] = 0;
835 offs = IN_BIND_OFFS(kc, bind_type);
836 if (dev->binds[offs] == -1)
837 dev->binds[offs] = 0;
838 dev->binds[offs] |= acts;
842 void in_clean_binds(void)
846 for (i = 0; i < IN_MAX_DEVS; i++) {
847 int ret, count, *binds, *def_binds;
848 in_dev_t *dev = &in_devices[i];
850 if (dev->binds == NULL || dev->drv_data == NULL)
853 count = dev->key_count;
855 def_binds = binds + count * IN_BINDTYPE_COUNT;
857 ret = DRV(dev->drv_id).clean_binds(dev->drv_data, binds, def_binds);
859 /* no useable binds */
866 void in_debug_dump(void)
870 lprintf("# drv probed binds name\n");
871 for (i = 0; i < IN_MAX_DEVS; i++) {
872 in_dev_t *d = &in_devices[i];
873 if (!d->probed && d->name == NULL && d->binds == NULL)
875 lprintf("%d %3d %6c %5c %s\n", i, d->drv_id, d->probed ? 'y' : 'n',
876 d->binds ? 'y' : 'n', d->name);
880 /* handlers for unknown/not_preset drivers */
882 static void in_def_probe(void) {}
883 static void in_def_free(void *drv_data) {}
884 static const char * const *
885 in_def_get_key_names(int *count) { return NULL; }
886 static void in_def_get_def_binds(int *binds) {}
887 static int in_def_clean_binds(void *drv_data, int *b, int *db) { return 0; }
888 static int in_def_get_config(void *drv_data, int what, int *val) { return -1; }
889 static int in_def_set_config(void *drv_data, int what, int val) { return -1; }
890 static int in_def_update_keycode(void *drv_data, int *is_down) { return 0; }
891 static int in_def_menu_translate(void *drv_data, int keycode) { return keycode; }
892 static int in_def_get_key_code(const char *key_name) { return -1; }
893 static const char *in_def_get_key_name(int keycode) { return NULL; }
899 memset(in_drivers, 0, sizeof(in_drivers));
900 memset(in_devices, 0, sizeof(in_devices));
903 for (i = 0; i < IN_DRVID_COUNT; i++) {
904 in_drivers[i].prefix = "none:";
905 in_drivers[i].probe = in_def_probe;
906 in_drivers[i].free = in_def_free;
907 in_drivers[i].get_key_names = in_def_get_key_names;
908 in_drivers[i].get_def_binds = in_def_get_def_binds;
909 in_drivers[i].clean_binds = in_def_clean_binds;
910 in_drivers[i].get_config = in_def_get_config;
911 in_drivers[i].set_config = in_def_set_config;
912 in_drivers[i].update_keycode = in_def_update_keycode;
913 in_drivers[i].menu_translate = in_def_menu_translate;
914 in_drivers[i].get_key_code = in_def_get_key_code;
915 in_drivers[i].get_key_name = in_def_get_key_name;
919 in_gp2x_init(&in_drivers[IN_DRVID_GP2X]);
922 in_evdev_init(&in_drivers[IN_DRVID_EVDEV]);
925 in_vk_init(&in_drivers[IN_DRVID_VK]);
942 ret = in_update_keycode(&dev, &down);
943 lprintf("#%i: %i %i (%s)\n", dev, down, ret, in_get_key_name(dev, ret));
947 ret = in_menu_wait_any();
948 lprintf("%08x\n", ret);