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;
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_free(in_dev_t *dev)
70 DRV(dev->drv_id).free(dev->drv_data);
79 /* to be called by drivers
80 * async devices must set drv_fd_hnd to -1 */
81 void in_register(const char *nname, int drv_id, int drv_fd_hnd, void *drv_data,
82 int key_count, const char * const *key_names, int combos)
84 int i, ret, dupe_count = 0, *binds;
85 char name[256], *name_end, *tmp;
87 strncpy(name, nname, sizeof(name));
88 name[sizeof(name)-12] = 0;
89 name_end = name + strlen(name);
91 for (i = 0; i < in_dev_count; i++)
93 if (in_devices[i].name == NULL)
95 if (strcmp(in_devices[i].name, name) == 0)
97 if (in_devices[i].probed) {
99 sprintf(name_end, " [%d]", dupe_count);
106 if (i >= IN_MAX_DEVS)
108 /* try to find unused device */
109 for (i = 0; i < IN_MAX_DEVS; i++)
110 if (!in_devices[i].probed) break;
111 if (i >= IN_MAX_DEVS) {
112 lprintf("input: too many devices, can't add %s\n", name);
115 in_free(&in_devices[i]);
122 binds = in_alloc_binds(drv_id, key_count);
128 in_devices[i].name = tmp;
129 in_devices[i].binds = binds;
130 in_devices[i].key_count = key_count;
131 if (i + 1 > in_dev_count)
132 in_dev_count = i + 1;
134 lprintf("input: new device #%d \"%s\"\n", i, name);
136 in_devices[i].probed = 1;
137 in_devices[i].does_combos = combos;
138 in_devices[i].drv_id = drv_id;
139 in_devices[i].drv_fd_hnd = drv_fd_hnd;
140 in_devices[i].key_names = key_names;
141 in_devices[i].drv_data = drv_data;
143 if (in_devices[i].binds != NULL) {
144 ret = DRV(drv_id).clean_binds(drv_data, in_devices[i].binds,
145 in_devices[i].binds + key_count * IN_BINDTYPE_COUNT);
147 /* no useable binds */
148 free(in_devices[i].binds);
149 in_devices[i].binds = NULL;
154 /* key combo handling, to be called by drivers that support it.
155 * Only care about IN_BINDTYPE_EMU */
156 void in_combos_find(const int *binds, int last_key, int *combo_keys, int *combo_acts)
160 *combo_keys = *combo_acts = 0;
161 for (act = 0; act < sizeof(binds[0]) * 8; act++)
164 for (u = 0; u <= last_key; u++)
165 if (binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)] & (1 << act))
170 // loop again and mark those keys and actions as combo
171 for (u = 0; u <= last_key; u++)
173 if (binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)] & (1 << act)) {
174 *combo_keys |= 1 << u;
175 *combo_acts |= 1 << act;
182 int in_combos_do(int keys, const int *binds, int last_key, int combo_keys, int combo_acts)
186 for (i = 0; i <= last_key; i++)
190 if (!(keys & (1 << i)))
193 acts = binds[IN_BIND_OFFS(i, IN_BINDTYPE_EMU)];
197 if (!(combo_keys & (1 << i))) {
202 acts_c = acts & combo_acts;
205 // let's try to find the other one
206 for (u = i + 1; u <= last_key; u++)
207 if ( (keys & (1 << u)) && (binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)] & acts_c) ) {
208 ret |= acts_c & binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)];
209 keys &= ~((1 << i) | (1 << u));
213 // add non-combo actions if combo ones were not found
215 ret |= acts & ~combo_acts;
225 in_have_async_devs = 0;
226 for (i = 0; i < in_dev_count; i++)
227 in_devices[i].probed = 0;
229 for (i = 1; i < IN_DRVID_COUNT; i++)
230 in_drivers[i].probe();
232 /* get rid of devs without binds and probes */
233 for (i = 0; i < in_dev_count; i++) {
234 if (!in_devices[i].probed && in_devices[i].binds == NULL) {
236 if (i < in_dev_count) {
237 free(in_devices[i].name);
238 memmove(&in_devices[i], &in_devices[i+1],
239 (in_dev_count - i) * sizeof(in_devices[0]));
245 if (in_devices[i].probed && in_devices[i].drv_fd_hnd == -1)
246 in_have_async_devs = 1;
249 if (in_have_async_devs)
250 lprintf("input: async-only devices detected..\n");
256 int in_update(int *result)
260 for (i = 0; i < in_dev_count; i++) {
261 in_dev_t *dev = &in_devices[i];
262 if (dev->probed && dev->binds != NULL) {
263 // FIXME: this is stupid, make it indirect
264 switch (dev->drv_id) {
267 ret |= in_evdev_update(dev->drv_data, dev->binds, result);
272 ret |= in_gp2x_update(dev->drv_data, dev->binds, result);
277 ret |= in_vk_update(dev->drv_data, dev->binds, result);
287 static int in_update_kc_async(int *dev_id_out, int *is_down_out, int timeout_ms)
289 int i, is_down, result;
292 ticks = plat_get_ticks_ms();
296 for (i = 0; i < in_dev_count; i++) {
297 in_dev_t *d = &in_devices[i];
301 result = DRV(d->drv_id).update_keycode(d->drv_data, &is_down);
308 *is_down_out = is_down;
312 if (timeout_ms >= 0 && (int)(plat_get_ticks_ms() - ticks) > timeout_ms)
322 * wait for a press, always return some keycode or -1 on timeout or error
324 int in_update_keycode(int *dev_id_out, int *is_down_out, int timeout_ms)
326 int result = -1, dev_id = 0, is_down, result_menu;
327 int fds_hnds[IN_MAX_DEVS];
328 int i, ret, count = 0;
329 in_drv_t *drv = NULL;
332 if (in_have_async_devs) {
333 result = in_update_kc_async(&dev_id, &is_down, timeout_ms);
336 drv = &DRV(in_devices[dev_id].drv_id);
340 ticks = plat_get_ticks_ms();
342 for (i = 0; i < in_dev_count; i++) {
343 if (in_devices[i].probed)
344 fds_hnds[count++] = in_devices[i].drv_fd_hnd;
348 /* don't deadlock, fail */
349 lprintf("input: failed to find devices to read\n");
355 ret = plat_wait_event(fds_hnds, count, timeout_ms);
359 for (i = 0; i < in_dev_count; i++) {
360 if (in_devices[i].drv_fd_hnd == ret) {
366 drv = &DRV(in_devices[dev_id].drv_id);
367 result = drv->update_keycode(in_devices[dev_id].drv_data, &is_down);
371 if (timeout_ms >= 0) {
372 unsigned int ticks2 = plat_get_ticks_ms();
373 timeout_ms -= ticks2 - ticks;
383 /* keep track of menu key state, to allow mixing
384 * in_update_keycode() and in_menu_wait_any() calls */
385 result_menu = drv->menu_translate(in_devices[dev_id].drv_data, result);
386 if (result_menu != 0) {
388 menu_key_state |= result_menu;
390 menu_key_state &= ~result_menu;
393 if (dev_id_out != NULL)
394 *dev_id_out = dev_id;
395 if (is_down_out != NULL)
396 *is_down_out = is_down;
400 /* same as above, only return bitfield of PBTN_* */
401 int in_menu_wait_any(int timeout_ms)
403 int keys_old = menu_key_state;
407 int code, is_down = 0, dev_id = 0;
409 code = in_update_keycode(&dev_id, &is_down, timeout_ms);
413 if (keys_old != menu_key_state) {
414 menu_last_used_dev = dev_id;
419 return menu_key_state;
422 /* wait for menu input, do autorepeat */
423 int in_menu_wait(int interesting, int autorep_delay_ms)
425 static int inp_prev = 0;
426 static int repeats = 0;
427 int ret, release = 0, wait = 450;
430 wait = autorep_delay_ms;
432 ret = in_menu_wait_any(wait);
436 while (!(ret & interesting)) {
437 ret = in_menu_wait_any(-1);
441 if (release || ret != inp_prev)
446 /* we don't need diagonals in menus */
447 if ((ret & PBTN_UP) && (ret & PBTN_LEFT)) ret &= ~PBTN_LEFT;
448 if ((ret & PBTN_UP) && (ret & PBTN_RIGHT)) ret &= ~PBTN_RIGHT;
449 if ((ret & PBTN_DOWN) && (ret & PBTN_LEFT)) ret &= ~PBTN_LEFT;
450 if ((ret & PBTN_DOWN) && (ret & PBTN_RIGHT)) ret &= ~PBTN_RIGHT;
455 const int *in_get_dev_binds(int dev_id)
457 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
460 return in_devices[dev_id].binds;
463 const int *in_get_dev_def_binds(int dev_id)
465 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
468 return in_devices[dev_id].binds + in_devices[dev_id].key_count * IN_BINDTYPE_COUNT;
471 int in_get_config(int dev_id, int what, void *val)
476 if (dev_id < 0 || dev_id >= IN_MAX_DEVS || val == NULL)
479 dev = &in_devices[dev_id];
481 case IN_CFG_BIND_COUNT:
482 *ival = dev->key_count;
484 case IN_CFG_DOES_COMBOS:
485 *ival = dev->does_combos;
487 case IN_CFG_BLOCKING:
488 case IN_CFG_KEY_NAMES:
489 return -1; /* not implemented */
491 return DRV(dev->drv_id).get_config(dev->drv_data, what, ival);
497 static int in_set_blocking(int is_blocking)
501 /* have_async_devs means we will have to do all reads async anyway.. */
502 if (!in_have_async_devs) {
503 for (i = 0; i < in_dev_count; i++) {
504 if (in_devices[i].probed)
505 DRV(in_devices[i].drv_id).set_config(in_devices[i].drv_data,
506 IN_CFG_BLOCKING, is_blocking);
514 ret = in_update_keycode(NULL, NULL, 0);
520 int in_set_config(int dev_id, int what, const void *val, int size)
522 const int *ival = val;
525 if (what == IN_CFG_BLOCKING)
526 return in_set_blocking(*ival);
528 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
531 dev = &in_devices[dev_id];
532 if (what == IN_CFG_KEY_NAMES) {
533 const char * const *names = val;
534 int count = size / sizeof(names[0]);
536 if (count < dev->key_count) {
537 lprintf("input: set_key_names: not enough keys\n");
541 dev->key_names = names;
545 return DRV(dev->drv_id).set_config(dev->drv_data, what, *ival);
548 const char *in_get_dev_name(int dev_id, int must_be_active, int skip_pfix)
550 const char *name, *tmp;
552 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
555 if (must_be_active && !in_devices[dev_id].probed)
558 name = in_devices[dev_id].name;
559 if (name == NULL || !skip_pfix)
563 tmp = strchr(name, ':');
570 int in_name_to_id(const char *dev_name)
574 for (i = 0; i < in_dev_count; i++)
575 if (strcmp(dev_name, in_devices[i].name) == 0)
578 if (i >= in_dev_count) {
579 lprintf("input: in_name_to_id: no such device: %s\n", dev_name);
586 /* never returns NULL */
587 const char *in_get_key_name(int dev_id, int keycode)
589 const char *name = NULL;
590 static char xname[16];
593 if (dev_id < 0) /* want last used dev? */
594 dev_id = menu_last_used_dev;
596 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
599 dev = &in_devices[dev_id];
600 if (keycode < 0) /* want name for menu key? */
601 keycode = DRV(dev->drv_id).menu_translate(dev->drv_data, keycode);
603 if (dev->key_names != NULL && 0 <= keycode && keycode < dev->key_count)
604 name = dev->key_names[keycode];
608 name = DRV(dev->drv_id).get_key_name(keycode);
612 /* assume scancode */
613 if ((keycode >= '0' && keycode <= '9') || (keycode >= 'a' && keycode <= 'z')
614 || (keycode >= 'A' && keycode <= 'Z'))
615 sprintf(xname, "%c", keycode);
617 sprintf(xname, "\\x%02X", keycode);
621 int in_get_key_code(int dev_id, const char *key_name)
626 if (dev_id < 0) /* want last used dev? */
627 dev_id = menu_last_used_dev;
629 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
632 dev = &in_devices[dev_id];
633 if (dev->key_names == NULL)
636 for (i = 0; i < dev->key_count; i++)
637 if (dev->key_names[i] && strcasecmp(dev->key_names[i], key_name) == 0)
643 int in_bind_key(int dev_id, int keycode, int mask, int bind_type, int force_unbind)
648 if (dev_id < 0 || dev_id >= IN_MAX_DEVS || bind_type >= IN_BINDTYPE_COUNT)
651 dev = &in_devices[dev_id];
652 count = dev->key_count;
654 if (dev->binds == NULL) {
657 dev->binds = in_alloc_binds(dev->drv_id, count);
658 if (dev->binds == NULL)
662 if (keycode < 0 || keycode >= count)
666 dev->binds[IN_BIND_OFFS(keycode, bind_type)] &= ~mask;
668 dev->binds[IN_BIND_OFFS(keycode, bind_type)] ^= mask;
670 ret = DRV(dev->drv_id).clean_binds(dev->drv_data, dev->binds,
671 dev->binds + count * IN_BINDTYPE_COUNT);
680 void in_unbind_all(int dev_id, int act_mask, int bind_type)
685 if (dev_id < 0 || dev_id >= IN_MAX_DEVS || bind_type >= IN_BINDTYPE_COUNT)
688 dev = &in_devices[dev_id];
689 count = dev->key_count;
691 if (dev->binds == NULL)
694 for (i = 0; i < count; i++)
695 dev->binds[IN_BIND_OFFS(i, bind_type)] &= ~act_mask;
698 /* returns device id, or -1 on error */
699 int in_config_parse_dev(const char *name)
703 for (i = 0; i < IN_DRVID_COUNT; i++) {
704 int len = strlen(in_drivers[i].prefix);
705 if (strncmp(name, in_drivers[i].prefix, len) == 0) {
712 lprintf("input: missing driver for %s\n", name);
716 for (i = 0; i < in_dev_count; i++)
718 if (in_devices[i].name == NULL)
720 if (strcmp(in_devices[i].name, name) == 0)
724 if (i >= IN_MAX_DEVS)
726 /* try to find unused device */
727 for (i = 0; i < IN_MAX_DEVS; i++)
728 if (in_devices[i].name == NULL) break;
729 if (i >= IN_MAX_DEVS) {
730 lprintf("input: too many devices, can't add %s\n", name);
735 memset(&in_devices[i], 0, sizeof(in_devices[i]));
737 in_devices[i].name = strdup(name);
738 if (in_devices[i].name == NULL)
741 in_devices[i].key_count = DRV(drv_id).get_bind_count();
742 in_devices[i].drv_id = drv_id;
744 if (i + 1 > in_dev_count)
745 in_dev_count = i + 1;
751 * To reduce size of game specific configs, default binds are not saved.
752 * So we mark default binds in in_config_start(), override them in in_config_bind_key(),
753 * and restore whatever default binds are left in in_config_end().
755 void in_config_start(void)
759 /* mark all default binds, so they get overwritten by func below */
760 for (i = 0; i < IN_MAX_DEVS; i++) {
761 int n, count, *binds, *def_binds;
763 binds = in_devices[i].binds;
767 count = in_devices[i].key_count;
768 def_binds = binds + count * IN_BINDTYPE_COUNT;
770 for (n = 0; n < count * IN_BINDTYPE_COUNT; n++)
771 if (binds[n] == def_binds[n])
776 int in_config_bind_key(int dev_id, const char *key, int acts, int bind_type)
781 if (dev_id < 0 || dev_id >= IN_MAX_DEVS || bind_type >= IN_BINDTYPE_COUNT)
783 dev = &in_devices[dev_id];
785 /* maybe a raw code? */
786 if (key[0] == '\\' && key[1] == 'x') {
788 kc = (int)strtoul(key + 2, &p, 16);
789 if (p == NULL || *p != 0)
793 /* device specific key name */
794 if (dev->binds == NULL) {
795 dev->binds = in_alloc_binds(dev->drv_id, dev->key_count);
796 if (dev->binds == NULL)
802 if (dev->key_names != NULL) {
803 for (i = 0; i < dev->key_count; i++) {
804 const char *k = dev->key_names[i];
805 if (k != NULL && strcasecmp(k, key) == 0) {
813 kc = DRV(dev->drv_id).get_key_code(key);
814 if (kc < 0 && strlen(key) == 1) {
815 /* assume scancode */
820 if (kc < 0 || kc >= dev->key_count) {
821 lprintf("input: bad key: %s\n", key);
825 if (bind_type == IN_BINDTYPE_NONE) {
826 for (i = 0; i < IN_BINDTYPE_COUNT; i++)
827 dev->binds[IN_BIND_OFFS(kc, i)] = 0;
831 offs = IN_BIND_OFFS(kc, bind_type);
832 if (dev->binds[offs] == -1)
833 dev->binds[offs] = 0;
834 dev->binds[offs] |= acts;
838 void in_config_end(void)
842 for (i = 0; i < IN_MAX_DEVS; i++) {
843 int n, t, ret, count, *binds, *def_binds;
844 in_dev_t *dev = &in_devices[i];
846 if (dev->binds == NULL)
849 count = dev->key_count;
851 def_binds = binds + count * IN_BINDTYPE_COUNT;
853 for (n = 0; n < count; n++) {
855 for (t = 0; t < IN_BINDTYPE_COUNT; t++)
856 if (binds[IN_BIND_OFFS(n, t)] == -1)
857 binds[IN_BIND_OFFS(n, t)] = 0;
862 for (t = 0; t < IN_BINDTYPE_COUNT; t++)
863 binds[IN_BIND_OFFS(n, t)] = def_binds[IN_BIND_OFFS(n, t)];
866 if (dev->drv_data == NULL)
869 ret = DRV(dev->drv_id).clean_binds(dev->drv_data, binds, def_binds);
871 /* no useable binds */
878 void in_debug_dump(void)
882 lprintf("# drv probed binds name\n");
883 for (i = 0; i < IN_MAX_DEVS; i++) {
884 in_dev_t *d = &in_devices[i];
885 if (!d->probed && d->name == NULL && d->binds == NULL)
887 lprintf("%d %3d %6c %5c %s\n", i, d->drv_id, d->probed ? 'y' : 'n',
888 d->binds ? 'y' : 'n', d->name);
892 /* handlers for unknown/not_preset drivers */
894 static void in_def_probe(void) {}
895 static void in_def_free(void *drv_data) {}
896 static int in_def_get_bind_count(void) { return 0; }
897 static void in_def_get_def_binds(int *binds) {}
898 static int in_def_clean_binds(void *drv_data, int *b, int *db) { return 0; }
899 static int in_def_get_config(void *drv_data, int what, int *val) { return -1; }
900 static int in_def_set_config(void *drv_data, int what, int val) { return -1; }
901 static int in_def_update_keycode(void *drv_data, int *is_down) { return 0; }
902 static int in_def_menu_translate(void *drv_data, int keycode) { return keycode; }
903 static int in_def_get_key_code(const char *key_name) { return -1; }
904 static const char *in_def_get_key_name(int keycode) { return NULL; }
910 memset(in_drivers, 0, sizeof(in_drivers));
911 memset(in_devices, 0, sizeof(in_devices));
914 for (i = 0; i < IN_DRVID_COUNT; i++) {
915 in_drivers[i].prefix = "none:";
916 in_drivers[i].probe = in_def_probe;
917 in_drivers[i].free = in_def_free;
918 in_drivers[i].get_bind_count = in_def_get_bind_count;
919 in_drivers[i].get_def_binds = in_def_get_def_binds;
920 in_drivers[i].clean_binds = in_def_clean_binds;
921 in_drivers[i].get_config = in_def_get_config;
922 in_drivers[i].set_config = in_def_set_config;
923 in_drivers[i].update_keycode = in_def_update_keycode;
924 in_drivers[i].menu_translate = in_def_menu_translate;
925 in_drivers[i].get_key_code = in_def_get_key_code;
926 in_drivers[i].get_key_name = in_def_get_key_name;
930 in_gp2x_init(&in_drivers[IN_DRVID_GP2X]);
933 in_evdev_init(&in_drivers[IN_DRVID_EVDEV]);
936 in_vk_init(&in_drivers[IN_DRVID_VK]);
953 ret = in_update_keycode(&dev, &down);
954 lprintf("#%i: %i %i (%s)\n", dev, down, ret, in_get_key_name(dev, ret));
958 ret = in_menu_wait_any();
959 lprintf("%08x\n", ret);