7 #include "../linux/in_evdev.h"
8 #include "../gp2x/in_gp2x.h"
9 #include "../win32/in_vk.h"
18 int *binds; /* total = key_count * bindtypes * 2 */
23 static in_drv_t in_drivers[IN_DRVID_COUNT];
24 static in_dev_t in_devices[IN_MAX_DEVS];
25 static int in_dev_count = 0;
26 static int in_have_async_devs = 0;
27 static int menu_key_state = 0;
28 static int menu_last_used_dev = 0;
30 #define DRV(id) in_drivers[(unsigned)(id) < IN_DRVID_COUNT ? (id) : 0]
33 static int *in_alloc_binds(int drv_id, int key_count)
37 binds = calloc(key_count * IN_BINDTYPE_COUNT * 2, sizeof(binds[0]));
41 DRV(drv_id).get_def_binds(binds + key_count * IN_BINDTYPE_COUNT);
42 memcpy(binds, binds + key_count * IN_BINDTYPE_COUNT,
43 sizeof(binds[0]) * key_count * IN_BINDTYPE_COUNT);
48 static void in_free(in_dev_t *dev)
51 DRV(dev->drv_id).free(dev->drv_data);
60 /* to be called by drivers
61 * async devices must set drv_fd_hnd to -1 */
62 void in_register(const char *nname, int drv_id, int drv_fd_hnd, void *drv_data,
63 int key_count, int combos)
65 int i, ret, dupe_count = 0, *binds;
66 char name[256], *name_end, *tmp;
68 strncpy(name, nname, sizeof(name));
69 name[sizeof(name)-12] = 0;
70 name_end = name + strlen(name);
72 for (i = 0; i < in_dev_count; i++)
74 if (in_devices[i].name == NULL)
76 if (strcmp(in_devices[i].name, name) == 0)
78 if (in_devices[i].probed) {
80 sprintf(name_end, " [%d]", dupe_count);
89 /* try to find unused device */
90 for (i = 0; i < IN_MAX_DEVS; i++)
91 if (!in_devices[i].probed) break;
92 if (i >= IN_MAX_DEVS) {
93 printf("input: too many devices, can't add %s\n", name);
96 in_free(&in_devices[i]);
103 binds = in_alloc_binds(drv_id, key_count);
109 in_devices[i].name = tmp;
110 in_devices[i].binds = binds;
111 in_devices[i].key_count = key_count;
112 if (i + 1 > in_dev_count)
113 in_dev_count = i + 1;
115 printf("input: new device #%d \"%s\"\n", i, name);
117 in_devices[i].probed = 1;
118 in_devices[i].does_combos = combos;
119 in_devices[i].drv_id = drv_id;
120 in_devices[i].drv_fd_hnd = drv_fd_hnd;
121 in_devices[i].drv_data = drv_data;
123 if (in_devices[i].binds != NULL) {
124 ret = DRV(drv_id).clean_binds(drv_data, in_devices[i].binds,
125 in_devices[i].binds + key_count * IN_BINDTYPE_COUNT);
127 /* no useable binds */
128 free(in_devices[i].binds);
129 in_devices[i].binds = NULL;
134 /* key combo handling, to be called by drivers that support it.
135 * Only care about IN_BINDTYPE_EMU */
136 void in_combos_find(const int *binds, int last_key, int *combo_keys, int *combo_acts)
140 *combo_keys = *combo_acts = 0;
141 for (act = 0; act < sizeof(binds[0]) * 8; act++)
144 for (u = 0; u <= last_key; u++)
145 if (binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)] & (1 << act))
150 // loop again and mark those keys and actions as combo
151 for (u = 0; u <= last_key; u++)
153 if (binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)] & (1 << act)) {
154 *combo_keys |= 1 << u;
155 *combo_acts |= 1 << act;
162 int in_combos_do(int keys, const int *binds, int last_key, int combo_keys, int combo_acts)
166 for (i = 0; i <= last_key; i++)
170 if (!(keys & (1 << i)))
173 acts = binds[IN_BIND_OFFS(i, IN_BINDTYPE_EMU)];
177 if (!(combo_keys & (1 << i))) {
182 acts_c = acts & combo_acts;
185 // let's try to find the other one
186 for (u = i + 1; u <= last_key; u++)
187 if ( (keys & (1 << u)) && (binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)] & acts_c) ) {
188 ret |= acts_c & binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)];
189 keys &= ~((1 << i) | (1 << u));
193 // add non-combo actions if combo ones were not found
195 ret |= acts & ~combo_acts;
205 in_have_async_devs = 0;
206 for (i = 0; i < in_dev_count; i++)
207 in_devices[i].probed = 0;
209 for (i = 1; i < IN_DRVID_COUNT; i++)
210 in_drivers[i].probe();
212 /* get rid of devs without binds and probes */
213 for (i = 0; i < in_dev_count; i++) {
214 if (!in_devices[i].probed && in_devices[i].binds == NULL) {
216 if (i < in_dev_count) {
217 free(in_devices[i].name);
218 memmove(&in_devices[i], &in_devices[i+1],
219 (in_dev_count - i) * sizeof(in_devices[0]));
225 if (in_devices[i].probed && in_devices[i].drv_fd_hnd == -1)
226 in_have_async_devs = 1;
229 if (in_have_async_devs)
230 printf("input: async-only devices detected..\n");
234 int in_update(int *result)
238 for (i = 0; i < in_dev_count; i++) {
239 in_dev_t *dev = &in_devices[i];
240 if (dev->probed && dev->binds != NULL) {
241 // FIXME: this is stupid, make it indirect
242 switch (dev->drv_id) {
245 ret |= in_evdev_update(dev->drv_data, dev->binds, result);
250 ret |= in_gp2x_update(dev->drv_data, dev->binds, result);
254 ret |= in_vk_update(dev->drv_data, dev->binds, result);
263 void in_set_blocking(int is_blocking)
267 /* have_async_devs means we will have to do all reads async anyway.. */
268 if (!in_have_async_devs) {
269 for (i = 0; i < in_dev_count; i++) {
270 if (in_devices[i].probed)
271 DRV(in_devices[i].drv_id).set_blocking(in_devices[i].drv_data, is_blocking);
279 ret = in_update_keycode(NULL, NULL, 0);
283 static int in_update_kc_async(int *dev_id_out, int *is_down_out, int timeout_ms)
285 int i, is_down, result;
288 ticks = plat_get_ticks_ms();
292 for (i = 0; i < in_dev_count; i++) {
293 in_dev_t *d = &in_devices[i];
297 result = DRV(d->drv_id).update_keycode(d->drv_data, &is_down);
304 *is_down_out = is_down;
308 if (timeout_ms >= 0 && (int)(plat_get_ticks_ms() - ticks) > timeout_ms)
318 * wait for a press, always return some keycode or -1 on timeout or error
320 int in_update_keycode(int *dev_id_out, int *is_down_out, int timeout_ms)
322 int result = -1, dev_id = 0, is_down, result_menu;
323 int fds_hnds[IN_MAX_DEVS];
324 int i, ret, count = 0;
325 in_drv_t *drv = NULL;
328 if (in_have_async_devs) {
329 result = in_update_kc_async(&dev_id, &is_down, timeout_ms);
332 drv = &DRV(in_devices[dev_id].drv_id);
336 ticks = plat_get_ticks_ms();
338 for (i = 0; i < in_dev_count; i++) {
339 if (in_devices[i].probed)
340 fds_hnds[count++] = in_devices[i].drv_fd_hnd;
344 /* don't deadlock, fail */
345 printf("input: failed to find devices to read\n");
351 ret = plat_wait_event(fds_hnds, count, timeout_ms);
355 for (i = 0; i < in_dev_count; i++) {
356 if (in_devices[i].drv_fd_hnd == ret) {
362 drv = &DRV(in_devices[dev_id].drv_id);
363 result = drv->update_keycode(in_devices[dev_id].drv_data, &is_down);
365 /* update_keycode() might return -1 when some not interesting
366 * event happened, like sync event for evdev. */
370 if (timeout_ms >= 0) {
371 unsigned int ticks2 = plat_get_ticks_ms();
372 timeout_ms -= ticks2 - ticks;
382 /* keep track of menu key state, to allow mixing
383 * in_update_keycode() and in_menu_wait_any() calls */
384 result_menu = drv->menu_translate(result);
385 if (result_menu != 0) {
387 menu_key_state |= result_menu;
389 menu_key_state &= ~result_menu;
392 if (dev_id_out != NULL)
393 *dev_id_out = dev_id;
394 if (is_down_out != NULL)
395 *is_down_out = is_down;
399 /* same as above, only return bitfield of PBTN_* */
400 int in_menu_wait_any(int timeout_ms)
402 int keys_old = menu_key_state;
406 int code, is_down = 0, dev_id = 0;
408 code = in_update_keycode(&dev_id, &is_down, timeout_ms);
410 code = DRV(in_devices[dev_id].drv_id).menu_translate(code);
416 menu_last_used_dev = dev_id;
417 if (keys_old != menu_key_state)
421 return menu_key_state;
424 /* wait for menu input, do autorepeat */
425 int in_menu_wait(int interesting, int autorep_delay_ms)
427 static int inp_prev = 0;
428 static int repeats = 0;
429 int ret, release = 0, wait = 450;
432 wait = autorep_delay_ms;
434 ret = in_menu_wait_any(wait);
438 while (!(ret & interesting)) {
439 ret = in_menu_wait_any(-1);
443 if (release || ret != inp_prev)
448 /* we don't need diagonals in menus */
449 if ((ret & PBTN_UP) && (ret & PBTN_LEFT)) ret &= ~PBTN_LEFT;
450 if ((ret & PBTN_UP) && (ret & PBTN_RIGHT)) ret &= ~PBTN_RIGHT;
451 if ((ret & PBTN_DOWN) && (ret & PBTN_LEFT)) ret &= ~PBTN_LEFT;
452 if ((ret & PBTN_DOWN) && (ret & PBTN_RIGHT)) ret &= ~PBTN_RIGHT;
457 const int *in_get_dev_binds(int dev_id)
459 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
462 return in_devices[dev_id].binds;
465 const int *in_get_dev_def_binds(int dev_id)
467 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
470 return in_devices[dev_id].binds + in_devices[dev_id].key_count * IN_BINDTYPE_COUNT;
473 int in_get_dev_info(int dev_id, int what)
475 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
479 case IN_INFO_BIND_COUNT:
480 return in_devices[dev_id].key_count;
481 case IN_INFO_DOES_COMBOS:
482 return in_devices[dev_id].does_combos;
488 const char *in_get_dev_name(int dev_id, int must_be_active, int skip_pfix)
490 const char *name, *tmp;
492 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
495 if (must_be_active && !in_devices[dev_id].probed)
498 name = in_devices[dev_id].name;
499 if (name == NULL || !skip_pfix)
503 tmp = strchr(name, ':');
510 /* never returns NULL */
511 const char *in_get_key_name(int dev_id, int keycode)
513 static char xname[16];
516 if (dev_id < 0) /* want last used dev? */
517 dev_id = menu_last_used_dev;
519 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
522 if (keycode < 0) /* want name for menu key? */
523 keycode = DRV(in_devices[dev_id].drv_id).menu_translate(keycode);
525 name = DRV(in_devices[dev_id].drv_id).get_key_name(keycode);
529 /* assume scancode */
530 if ((keycode >= '0' && keycode <= '9') || (keycode >= 'a' && keycode <= 'z')
531 || (keycode >= 'A' && keycode <= 'Z'))
532 sprintf(xname, "%c", keycode);
534 sprintf(xname, "\\x%02X", keycode);
538 int in_bind_key(int dev_id, int keycode, int mask, int bind_type, int force_unbind)
543 if (dev_id < 0 || dev_id >= IN_MAX_DEVS || bind_type >= IN_BINDTYPE_COUNT)
546 dev = &in_devices[dev_id];
547 count = dev->key_count;
549 if (dev->binds == NULL) {
552 dev->binds = in_alloc_binds(dev->drv_id, count);
553 if (dev->binds == NULL)
557 if (keycode < 0 || keycode >= count)
561 dev->binds[IN_BIND_OFFS(keycode, bind_type)] &= ~mask;
563 dev->binds[IN_BIND_OFFS(keycode, bind_type)] ^= mask;
565 ret = DRV(dev->drv_id).clean_binds(dev->drv_data, dev->binds,
566 dev->binds + count * IN_BINDTYPE_COUNT);
575 void in_unbind_all(int dev_id, int act_mask, int bind_type)
580 if (dev_id < 0 || dev_id >= IN_MAX_DEVS || bind_type >= IN_BINDTYPE_COUNT)
583 dev = &in_devices[dev_id];
584 count = dev->key_count;
586 if (dev->binds == NULL)
589 for (i = 0; i < count; i++)
590 dev->binds[IN_BIND_OFFS(i, bind_type)] &= ~act_mask;
593 /* returns device id, or -1 on error */
594 int in_config_parse_dev(const char *name)
598 for (i = 0; i < IN_DRVID_COUNT; i++) {
599 int len = strlen(in_drivers[i].prefix);
600 if (strncmp(name, in_drivers[i].prefix, len) == 0) {
607 printf("input: missing driver for %s\n", name);
611 for (i = 0; i < in_dev_count; i++)
613 if (in_devices[i].name == NULL)
615 if (strcmp(in_devices[i].name, name) == 0)
619 if (i >= IN_MAX_DEVS)
621 /* try to find unused device */
622 for (i = 0; i < IN_MAX_DEVS; i++)
623 if (in_devices[i].name == NULL) break;
624 if (i >= IN_MAX_DEVS) {
625 printf("input: too many devices, can't add %s\n", name);
630 memset(&in_devices[i], 0, sizeof(in_devices[i]));
632 in_devices[i].name = strdup(name);
633 if (in_devices[i].name == NULL)
636 in_devices[i].key_count = DRV(drv_id).get_bind_count();
637 in_devices[i].drv_id = drv_id;
639 if (i + 1 > in_dev_count)
640 in_dev_count = i + 1;
646 * To reduce size of game specific configs, default binds are not saved.
647 * So we mark default binds in in_config_start(), override them in in_config_bind_key(),
648 * and restore whatever default binds are left in in_config_end().
650 void in_config_start(void)
654 /* mark all default binds, so they get overwritten by func below */
655 for (i = 0; i < IN_MAX_DEVS; i++) {
656 int n, count, *binds, *def_binds;
658 binds = in_devices[i].binds;
662 count = in_devices[i].key_count;
663 def_binds = binds + count * IN_BINDTYPE_COUNT;
665 for (n = 0; n < count * IN_BINDTYPE_COUNT; n++)
666 if (binds[n] == def_binds[n])
671 int in_config_bind_key(int dev_id, const char *key, int acts, int bind_type)
676 if (dev_id < 0 || dev_id >= IN_MAX_DEVS || bind_type >= IN_BINDTYPE_COUNT)
678 dev = &in_devices[dev_id];
680 /* maybe a raw code? */
681 if (key[0] == '\\' && key[1] == 'x') {
683 kc = (int)strtoul(key + 2, &p, 16);
684 if (p == NULL || *p != 0)
688 /* device specific key name */
689 if (dev->binds == NULL) {
690 dev->binds = in_alloc_binds(dev->drv_id, dev->key_count);
691 if (dev->binds == NULL)
696 kc = DRV(dev->drv_id).get_key_code(key);
697 if (kc < 0 && strlen(key) == 1) {
698 /* assume scancode */
703 if (kc < 0 || kc >= dev->key_count) {
704 printf("input: bad key: %s\n", key);
708 if (bind_type == IN_BINDTYPE_NONE) {
709 for (i = 0; i < IN_BINDTYPE_COUNT; i++)
710 dev->binds[IN_BIND_OFFS(kc, i)] = 0;
714 offs = IN_BIND_OFFS(kc, bind_type);
715 if (dev->binds[offs] == -1)
716 dev->binds[offs] = 0;
717 dev->binds[offs] |= acts;
721 void in_config_end(void)
725 for (i = 0; i < IN_MAX_DEVS; i++) {
726 int n, t, ret, count, *binds, *def_binds;
727 in_dev_t *dev = &in_devices[i];
729 if (dev->binds == NULL)
732 count = dev->key_count;
734 def_binds = binds + count * IN_BINDTYPE_COUNT;
736 for (n = 0; n < count; n++) {
738 for (t = 0; t < IN_BINDTYPE_COUNT; t++)
739 if (binds[IN_BIND_OFFS(n, t)] == -1)
740 binds[IN_BIND_OFFS(n, t)] = 0;
745 for (t = 0; t < IN_BINDTYPE_COUNT; t++)
746 binds[IN_BIND_OFFS(n, t)] = def_binds[IN_BIND_OFFS(n, t)];
749 if (dev->drv_data == NULL)
752 ret = DRV(dev->drv_id).clean_binds(dev->drv_data, binds, def_binds);
754 /* no useable binds */
761 void in_debug_dump(void)
765 printf("# drv probed binds name\n");
766 for (i = 0; i < IN_MAX_DEVS; i++) {
767 in_dev_t *d = &in_devices[i];
768 if (!d->probed && d->name == NULL && d->binds == NULL)
770 printf("%d %3d %6c %5c %s\n", i, d->drv_id, d->probed ? 'y' : 'n',
771 d->binds ? 'y' : 'n', d->name);
775 /* handlers for unknown/not_preset drivers */
777 static void in_def_probe(void) {}
778 static void in_def_free(void *drv_data) {}
779 static int in_def_get_bind_count(void) { return 0; }
780 static void in_def_get_def_binds(int *binds) {}
781 static int in_def_clean_binds(void *drv_data, int *b, int *db) { return 0; }
782 static void in_def_set_blocking(void *data, int y) {}
783 static int in_def_update_keycode(void *drv_data, int *is_down) { return 0; }
784 static int in_def_menu_translate(int keycode) { return keycode; }
785 static int in_def_get_key_code(const char *key_name) { return 0; }
786 static const char *in_def_get_key_name(int keycode) { return NULL; }
792 memset(in_drivers, 0, sizeof(in_drivers));
793 memset(in_devices, 0, sizeof(in_devices));
796 for (i = 0; i < IN_DRVID_COUNT; i++) {
797 in_drivers[i].prefix = "none:";
798 in_drivers[i].probe = in_def_probe;
799 in_drivers[i].free = in_def_free;
800 in_drivers[i].get_bind_count = in_def_get_bind_count;
801 in_drivers[i].get_def_binds = in_def_get_def_binds;
802 in_drivers[i].clean_binds = in_def_clean_binds;
803 in_drivers[i].set_blocking = in_def_set_blocking;
804 in_drivers[i].update_keycode = in_def_update_keycode;
805 in_drivers[i].menu_translate = in_def_menu_translate;
806 in_drivers[i].get_key_code = in_def_get_key_code;
807 in_drivers[i].get_key_name = in_def_get_key_name;
811 in_gp2x_init(&in_drivers[IN_DRVID_GP2X]);
814 in_evdev_init(&in_drivers[IN_DRVID_EVDEV]);
816 in_vk_init(&in_drivers[IN_DRVID_VK]);
832 ret = in_update_keycode(&dev, &down);
833 printf("#%i: %i %i (%s)\n", dev, down, ret, in_get_key_name(dev, ret));
837 ret = in_menu_wait_any();
838 printf("%08x\n", ret);