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.
8 * See the COPYING file in the top-level directory.
20 #error needs update: in_gp2x_init in_gp2x_update
21 #include "../gp2x/in_gp2x.h"
24 #error needs update: in_vk_init in_vk_update
25 #include "../win32/in_vk.h"
35 int *binds; /* total = key_count * bindtypes * 2 */
36 const char * const *key_names;
37 unsigned int probed:1;
38 unsigned int does_combos:1;
41 static in_drv_t *in_drivers;
42 static in_dev_t in_devices[IN_MAX_DEVS];
43 static int in_driver_count = 0;
44 static int in_dev_count = 0; /* probed + bind devices */
45 static int in_have_async_devs = 0;
46 static int in_probe_dev_id;
47 static int menu_key_state = 0;
48 static int menu_last_used_dev = 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 defbinds = DRV(drv_id).defbinds;
64 if (defbinds != NULL) {
66 if (defbinds[i].bit == 0 && defbinds[i].code == 0)
68 binds[IN_BIND_OFFS(defbinds[i].code, defbinds[i].btype)] =
72 /* always have a copy of defbinds */
73 memcpy(binds + key_count * IN_BINDTYPE_COUNT, binds,
74 sizeof(binds[0]) * key_count * IN_BINDTYPE_COUNT);
80 static void in_unprobe(in_dev_t *dev)
83 DRV(dev->drv_id).free(dev->drv_data);
88 static void in_free(in_dev_t *dev)
97 /* to be called by drivers
98 * async devices must set drv_fd_hnd to -1 */
99 void in_register(const char *nname, int drv_fd_hnd, void *drv_data,
100 int key_count, const char * const *key_names, int combos)
102 int i, ret, dupe_count = 0, *binds;
103 char name[256], *name_end, *tmp;
105 strncpy(name, nname, sizeof(name));
106 name[sizeof(name)-12] = 0;
107 name_end = name + strlen(name);
109 for (i = 0; i < in_dev_count; i++)
111 if (in_devices[i].name == NULL)
113 if (strcmp(in_devices[i].name, name) == 0)
115 if (in_devices[i].probed) {
117 sprintf(name_end, " [%d]", dupe_count);
124 if (i >= IN_MAX_DEVS)
126 /* try to find unused device */
127 for (i = 0; i < IN_MAX_DEVS; i++)
128 if (!in_devices[i].probed) break;
129 if (i >= IN_MAX_DEVS) {
130 lprintf("input: too many devices, can't add %s\n", name);
133 in_free(&in_devices[i]);
140 binds = in_alloc_binds(in_probe_dev_id, key_count);
146 in_devices[i].name = tmp;
147 in_devices[i].binds = binds;
148 in_devices[i].key_count = key_count;
149 if (i + 1 > in_dev_count)
150 in_dev_count = i + 1;
152 lprintf("input: new device #%d \"%s\"\n", i, name);
154 in_devices[i].probed = 1;
155 in_devices[i].does_combos = combos;
156 in_devices[i].drv_id = in_probe_dev_id;
157 in_devices[i].drv_fd_hnd = drv_fd_hnd;
158 in_devices[i].key_names = key_names;
159 in_devices[i].drv_data = drv_data;
161 if (in_devices[i].binds != NULL) {
162 ret = DRV(in_probe_dev_id).clean_binds(drv_data, in_devices[i].binds,
163 in_devices[i].binds + key_count * IN_BINDTYPE_COUNT);
165 /* no useable binds */
166 free(in_devices[i].binds);
167 in_devices[i].binds = NULL;
172 /* key combo handling, to be called by drivers that support it.
173 * Only care about IN_BINDTYPE_EMU */
174 void in_combos_find(const int *binds, int last_key, int *combo_keys, int *combo_acts)
178 *combo_keys = *combo_acts = 0;
179 for (act = 0; act < sizeof(binds[0]) * 8; act++)
182 for (u = 0; u <= last_key; u++)
183 if (binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)] & (1 << act))
188 // loop again and mark those keys and actions as combo
189 for (u = 0; u <= last_key; u++)
191 if (binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)] & (1 << act)) {
192 *combo_keys |= 1 << u;
193 *combo_acts |= 1 << act;
200 int in_combos_do(int keys, const int *binds, int last_key, int combo_keys, int combo_acts)
204 for (i = 0; i <= last_key; i++)
208 if (!(keys & (1 << i)))
211 acts = binds[IN_BIND_OFFS(i, IN_BINDTYPE_EMU)];
215 if (!(combo_keys & (1 << i))) {
220 acts_c = acts & combo_acts;
223 // let's try to find the other one
224 for (u = i + 1; u <= last_key; u++)
225 if ( (keys & (1 << u)) && (binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)] & acts_c) ) {
226 ret |= acts_c & binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)];
227 keys &= ~((1 << i) | (1 << u));
231 // add non-combo actions if combo ones were not found
233 ret |= acts & ~combo_acts;
243 in_have_async_devs = 0;
245 menu_last_used_dev = 0;
247 for (i = 0; i < in_dev_count; i++)
248 in_unprobe(&in_devices[i]);
250 for (i = 0; i < in_driver_count; i++) {
252 in_drivers[i].probe();
255 /* get rid of devs without binds and probes */
256 for (i = 0; i < in_dev_count; i++) {
257 if (!in_devices[i].probed && in_devices[i].binds == NULL) {
259 if (i < in_dev_count) {
260 free(in_devices[i].name);
261 memmove(&in_devices[i], &in_devices[i+1],
262 (in_dev_count - i) * sizeof(in_devices[0]));
268 if (in_devices[i].probed && in_devices[i].drv_fd_hnd == -1)
269 in_have_async_devs = 1;
272 if (in_have_async_devs)
273 lprintf("input: async-only devices detected..\n");
279 int in_update(int *result)
283 for (i = 0; i < in_dev_count; i++) {
284 in_dev_t *dev = &in_devices[i];
285 if (dev->probed && dev->binds != NULL)
286 ret |= DRV(dev->drv_id).update(dev->drv_data, dev->binds, result);
292 static in_dev_t *get_dev(int dev_id)
294 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
297 return &in_devices[dev_id];
300 int in_update_analog(int dev_id, int axis_id, int *result)
302 in_dev_t *dev = get_dev(dev_id);
304 if (dev == NULL || !dev->probed)
307 return DRV(dev->drv_id).update_analog(dev->drv_data, axis_id, result);
310 static int in_update_kc_async(int *dev_id_out, int *is_down_out, int timeout_ms)
312 int i, is_down, result;
315 ticks = plat_get_ticks_ms();
319 for (i = 0; i < in_dev_count; i++) {
320 in_dev_t *d = &in_devices[i];
324 result = DRV(d->drv_id).update_keycode(d->drv_data, &is_down);
331 *is_down_out = is_down;
335 if (timeout_ms >= 0 && (int)(plat_get_ticks_ms() - ticks) > timeout_ms)
345 * wait for a press, always return some keycode or -1 on timeout or error
347 int in_update_keycode(int *dev_id_out, int *is_down_out, int timeout_ms)
349 int result = -1, dev_id = 0, is_down, result_menu;
350 int fds_hnds[IN_MAX_DEVS];
351 int i, ret, count = 0;
352 in_drv_t *drv = NULL;
355 if (in_have_async_devs) {
356 result = in_update_kc_async(&dev_id, &is_down, timeout_ms);
359 drv = &DRV(in_devices[dev_id].drv_id);
363 ticks = plat_get_ticks_ms();
365 for (i = 0; i < in_dev_count; i++) {
366 if (in_devices[i].probed)
367 fds_hnds[count++] = in_devices[i].drv_fd_hnd;
371 /* don't deadlock, fail */
372 lprintf("input: failed to find devices to read\n");
378 ret = plat_wait_event(fds_hnds, count, timeout_ms);
382 for (i = 0; i < in_dev_count; i++) {
383 if (in_devices[i].drv_fd_hnd == ret) {
389 drv = &DRV(in_devices[dev_id].drv_id);
390 result = drv->update_keycode(in_devices[dev_id].drv_data, &is_down);
395 lprintf("input: \"%s\" errored out, removing.\n", in_devices[dev_id].name);
396 in_unprobe(&in_devices[dev_id]);
400 if (timeout_ms >= 0) {
401 unsigned int ticks2 = plat_get_ticks_ms();
402 timeout_ms -= ticks2 - ticks;
412 /* keep track of menu key state, to allow mixing
413 * in_update_keycode() and in_menu_wait_any() calls */
414 result_menu = drv->menu_translate(in_devices[dev_id].drv_data, result);
415 if (result_menu != 0) {
417 menu_key_state |= result_menu;
419 menu_key_state &= ~result_menu;
422 if (dev_id_out != NULL)
423 *dev_id_out = dev_id;
424 if (is_down_out != NULL)
425 *is_down_out = is_down;
429 /* same as above, only return bitfield of PBTN_* */
430 int in_menu_wait_any(int timeout_ms)
432 int keys_old = menu_key_state;
436 int code, is_down = 0, dev_id = 0;
438 code = in_update_keycode(&dev_id, &is_down, timeout_ms);
442 if (keys_old != menu_key_state) {
443 menu_last_used_dev = dev_id;
448 return menu_key_state;
451 /* wait for menu input, do autorepeat */
452 int in_menu_wait(int interesting, int autorep_delay_ms)
454 static int inp_prev = 0;
455 static int repeats = 0;
456 int ret, release = 0, wait = 450;
459 wait = autorep_delay_ms;
461 ret = in_menu_wait_any(wait);
465 while (!(ret & interesting)) {
466 ret = in_menu_wait_any(-1);
470 if (release || ret != inp_prev)
475 /* we don't need diagonals in menus */
476 if ((ret & PBTN_UP) && (ret & PBTN_LEFT)) ret &= ~PBTN_LEFT;
477 if ((ret & PBTN_UP) && (ret & PBTN_RIGHT)) ret &= ~PBTN_RIGHT;
478 if ((ret & PBTN_DOWN) && (ret & PBTN_LEFT)) ret &= ~PBTN_LEFT;
479 if ((ret & PBTN_DOWN) && (ret & PBTN_RIGHT)) ret &= ~PBTN_RIGHT;
484 const int *in_get_dev_binds(int dev_id)
486 in_dev_t *dev = get_dev(dev_id);
488 return dev ? dev->binds : NULL;
491 const int *in_get_dev_def_binds(int dev_id)
493 in_dev_t *dev = get_dev(dev_id);
497 return dev->binds + dev->key_count * IN_BINDTYPE_COUNT;
500 int in_get_config(int dev_id, int what, void *val)
505 dev = get_dev(dev_id);
506 if (dev == NULL || val == NULL)
510 case IN_CFG_BIND_COUNT:
511 *ival = dev->key_count;
513 case IN_CFG_DOES_COMBOS:
514 *ival = dev->does_combos;
516 case IN_CFG_BLOCKING:
517 case IN_CFG_KEY_NAMES:
518 return -1; /* not implemented */
520 return DRV(dev->drv_id).get_config(dev->drv_data, what, ival);
526 static int in_set_blocking(int is_blocking)
530 /* have_async_devs means we will have to do all reads async anyway.. */
531 if (!in_have_async_devs) {
532 for (i = 0; i < in_dev_count; i++) {
533 if (in_devices[i].probed)
534 DRV(in_devices[i].drv_id).set_config(in_devices[i].drv_data,
535 IN_CFG_BLOCKING, is_blocking);
543 ret = in_update_keycode(NULL, NULL, 0);
549 int in_set_config(int dev_id, int what, const void *val, int size)
551 const int *ival = val;
554 if (what == IN_CFG_BLOCKING)
555 return in_set_blocking(*ival);
557 dev = get_dev(dev_id);
561 if (what == IN_CFG_KEY_NAMES) {
562 const char * const *names = val;
563 int 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;
575 return DRV(dev->drv_id).set_config(dev->drv_data, what, *ival);
580 const char *in_get_dev_name(int dev_id, int must_be_active, int skip_pfix)
582 const char *name, *tmp;
585 dev = get_dev(dev_id);
589 if (must_be_active && !dev->probed)
593 if (name == NULL || !skip_pfix)
597 tmp = strchr(name, ':');
604 int in_name_to_id(const char *dev_name)
608 for (i = 0; i < in_dev_count; i++)
609 if (strcmp(dev_name, in_devices[i].name) == 0)
612 if (i >= in_dev_count) {
613 lprintf("input: in_name_to_id: no such device: %s\n", dev_name);
620 /* never returns NULL */
621 const char *in_get_key_name(int dev_id, int keycode)
623 const char *name = NULL;
624 static char xname[16];
628 if (dev_id < 0) /* want last used dev? */
629 dev_id = menu_last_used_dev;
631 dev = get_dev(dev_id);
635 drv = &DRV(dev->drv_id);
636 if (keycode < 0) /* want name for menu key? */
637 keycode = drv->menu_translate(dev->drv_data, keycode);
639 if (dev->key_names != NULL && 0 <= keycode && keycode < dev->key_count)
640 name = dev->key_names[keycode];
644 if (drv->get_key_name != NULL)
645 name = drv->get_key_name(keycode);
649 /* assume scancode */
650 if ((keycode >= '0' && keycode <= '9') || (keycode >= 'a' && keycode <= 'z')
651 || (keycode >= 'A' && keycode <= 'Z'))
652 sprintf(xname, "%c", keycode);
654 sprintf(xname, "\\x%02X", keycode);
658 int in_get_key_code(int dev_id, const char *key_name)
663 if (dev_id < 0) /* want last used dev? */
664 dev_id = menu_last_used_dev;
666 dev = get_dev(dev_id);
670 if (dev->key_names == NULL)
673 for (i = 0; i < dev->key_count; i++)
674 if (dev->key_names[i] && strcasecmp(dev->key_names[i], key_name) == 0)
680 int in_bind_key(int dev_id, int keycode, int mask, int bind_type, int force_unbind)
685 dev = get_dev(dev_id);
686 if (dev == NULL || bind_type >= IN_BINDTYPE_COUNT)
689 count = dev->key_count;
691 if (dev->binds == NULL) {
694 dev->binds = in_alloc_binds(dev->drv_id, count);
695 if (dev->binds == NULL)
699 if (keycode < 0 || keycode >= count)
703 dev->binds[IN_BIND_OFFS(keycode, bind_type)] &= ~mask;
705 dev->binds[IN_BIND_OFFS(keycode, bind_type)] ^= mask;
707 ret = DRV(dev->drv_id).clean_binds(dev->drv_data, dev->binds,
708 dev->binds + count * IN_BINDTYPE_COUNT);
718 * Unbind act_mask on binds with type bind_type
719 * - if dev_id_ < 0, affects all devices
720 * else only affects dev_id_
721 * - if act_mask == -1, unbind all keys
722 * else only actions in mask
724 void in_unbind_all(int dev_id_, int act_mask, int bind_type)
726 int dev_id = 0, dev_last = IN_MAX_DEVS - 1;
731 dev_id = dev_last = dev_id_;
733 if (bind_type >= IN_BINDTYPE_COUNT)
736 for (; dev_id <= dev_last; dev_id++) {
737 dev = &in_devices[dev_id];
738 count = dev->key_count;
740 if (dev->binds == NULL)
743 if (act_mask != -1) {
744 for (i = 0; i < count; i++)
745 dev->binds[IN_BIND_OFFS(i, bind_type)] &= ~act_mask;
748 memset(dev->binds, 0, sizeof(dev->binds[0]) * count * IN_BINDTYPE_COUNT);
752 /* returns device id, or -1 on error */
753 int in_config_parse_dev(const char *name)
757 for (i = 0; i < in_driver_count; i++) {
758 int len = strlen(in_drivers[i].prefix);
759 if (strncmp(name, in_drivers[i].prefix, len) == 0) {
766 lprintf("input: missing driver for %s\n", name);
770 for (i = 0; i < in_dev_count; i++)
772 if (in_devices[i].name == NULL)
774 if (strcmp(in_devices[i].name, name) == 0)
778 if (i >= IN_MAX_DEVS)
780 /* try to find unused device */
781 for (i = 0; i < IN_MAX_DEVS; i++)
782 if (in_devices[i].name == NULL) break;
783 if (i >= IN_MAX_DEVS) {
784 lprintf("input: too many devices, can't add %s\n", name);
789 memset(&in_devices[i], 0, sizeof(in_devices[i]));
791 in_devices[i].name = strdup(name);
792 if (in_devices[i].name == NULL)
795 in_devices[i].key_names = DRV(drv_id).get_key_names(&in_devices[i].key_count);
796 in_devices[i].drv_id = drv_id;
798 if (i + 1 > in_dev_count)
799 in_dev_count = i + 1;
804 int in_config_bind_key(int dev_id, const char *key, int acts, int bind_type)
809 dev = get_dev(dev_id);
810 if (dev == NULL || bind_type >= IN_BINDTYPE_COUNT)
813 /* maybe a raw code? */
814 if (key[0] == '\\' && key[1] == 'x') {
816 kc = (int)strtoul(key + 2, &p, 16);
817 if (p == NULL || *p != 0)
821 /* device specific key name */
822 if (dev->binds == NULL) {
823 dev->binds = in_alloc_binds(dev->drv_id, dev->key_count);
824 if (dev->binds == NULL)
829 if (dev->key_names != NULL) {
830 for (i = 0; i < dev->key_count; i++) {
831 const char *k = dev->key_names[i];
832 if (k != NULL && strcasecmp(k, key) == 0) {
840 kc = DRV(dev->drv_id).get_key_code(key);
841 if (kc < 0 && strlen(key) == 1) {
842 /* assume scancode */
847 if (kc < 0 || kc >= dev->key_count) {
848 lprintf("input: bad key: %s\n", key);
852 if (bind_type == IN_BINDTYPE_NONE) {
853 for (i = 0; i < IN_BINDTYPE_COUNT; i++)
854 dev->binds[IN_BIND_OFFS(kc, i)] = 0;
858 offs = IN_BIND_OFFS(kc, bind_type);
859 if (dev->binds[offs] == -1)
860 dev->binds[offs] = 0;
861 dev->binds[offs] |= acts;
865 void in_clean_binds(void)
869 for (i = 0; i < IN_MAX_DEVS; i++) {
870 int ret, count, *binds, *def_binds;
871 in_dev_t *dev = &in_devices[i];
873 if (dev->binds == NULL || dev->drv_data == NULL)
876 count = dev->key_count;
878 def_binds = binds + count * IN_BINDTYPE_COUNT;
880 ret = DRV(dev->drv_id).clean_binds(dev->drv_data, binds, def_binds);
882 /* no useable binds */
889 void in_debug_dump(void)
893 lprintf("# drv probed binds name\n");
894 for (i = 0; i < IN_MAX_DEVS; i++) {
895 in_dev_t *d = &in_devices[i];
896 if (!d->probed && d->name == NULL && d->binds == NULL)
898 lprintf("%d %3d %6c %5c %s\n", i, d->drv_id, d->probed ? 'y' : 'n',
899 d->binds ? 'y' : 'n', d->name);
903 /* stubs for drivers that choose not to implement something */
905 static void in_def_free(void *drv_data) {}
906 static int in_def_clean_binds(void *drv_data, int *b, int *db) { return 1; }
907 static int in_def_get_config(void *drv_data, int what, int *val) { return -1; }
908 static int in_def_set_config(void *drv_data, int what, int val) { return -1; }
909 static int in_def_update_analog(void *drv_data, int axis_id, int *result) { return -1; }
910 static int in_def_update_keycode(void *drv_data, int *is_down) { return 0; }
911 static int in_def_menu_translate(void *drv_data, int keycode) { return 0; }
912 static int in_def_get_key_code(const char *key_name) { return -1; }
913 static const char *in_def_get_key_name(int keycode) { return NULL; }
915 #define CHECK_ADD_STUB(d, f) \
916 if (d.f == NULL) d.f = in_def_##f
918 /* to be called by drivers */
919 int in_register_driver(const in_drv_t *drv, const struct in_default_bind *defbinds)
921 int count_new = in_driver_count + 1;
922 in_drv_t *new_drivers;
924 new_drivers = realloc(in_drivers, count_new * sizeof(in_drivers[0]));
925 if (new_drivers == NULL) {
926 lprintf("input: in_register_driver OOM\n");
930 memcpy(&new_drivers[in_driver_count], drv, sizeof(new_drivers[0]));
932 CHECK_ADD_STUB(new_drivers[in_driver_count], free);
933 CHECK_ADD_STUB(new_drivers[in_driver_count], clean_binds);
934 CHECK_ADD_STUB(new_drivers[in_driver_count], get_config);
935 CHECK_ADD_STUB(new_drivers[in_driver_count], set_config);
936 CHECK_ADD_STUB(new_drivers[in_driver_count], update_analog);
937 CHECK_ADD_STUB(new_drivers[in_driver_count], update_keycode);
938 CHECK_ADD_STUB(new_drivers[in_driver_count], menu_translate);
939 CHECK_ADD_STUB(new_drivers[in_driver_count], get_key_code);
940 CHECK_ADD_STUB(new_drivers[in_driver_count], get_key_name);
941 if (defbinds != NULL)
942 new_drivers[in_driver_count].defbinds = defbinds;
943 in_drivers = new_drivers;
944 in_driver_count = count_new;
952 memset(in_devices, 0, sizeof(in_devices));
970 ret = in_update_keycode(&dev, &down);
971 lprintf("#%i: %i %i (%s)\n", dev, down, ret, in_get_key_name(dev, ret));
975 ret = in_menu_wait_any();
976 lprintf("%08x\n", ret);