8 #include "../linux/in_evdev.h"
9 #include "../gp2x/in_gp2x.h"
22 static in_drv_t in_drivers[IN_DRVID_COUNT];
23 static in_dev_t in_devices[IN_MAX_DEVS];
24 static int in_dev_count = 0;
25 static int in_have_async_devs = 0;
27 #define DRV(id) in_drivers[(unsigned)(id) < IN_DRVID_COUNT ? (id) : 0]
30 static int in_bind_count(int drv_id)
32 int count = DRV(drv_id).get_bind_count();
34 printf("input: failed to get bind count for drv %d\n", drv_id);
39 static int *in_alloc_binds(int drv_id)
43 count = in_bind_count(drv_id);
47 binds = calloc(count * 2, sizeof(binds[0]));
51 DRV(drv_id).get_def_binds(binds + count);
52 memcpy(binds, binds + count, count * sizeof(binds[0]));
57 static void in_free(in_dev_t *dev)
60 DRV(dev->drv_id).free(dev->drv_data);
69 /* to be called by drivers
70 * async devices must set drv_fd_hnd to -1 */
71 void in_register(const char *nname, int drv_id, int drv_fd_hnd, void *drv_data, int combos)
73 int i, ret, dupe_count = 0, *binds;
74 char name[256], *name_end, *tmp;
76 strncpy(name, nname, sizeof(name));
77 name[sizeof(name)-12] = 0;
78 name_end = name + strlen(name);
80 for (i = 0; i < in_dev_count; i++)
82 if (in_devices[i].name == NULL)
84 if (strcmp(in_devices[i].name, name) == 0)
86 if (in_devices[i].probed) {
88 sprintf(name_end, " [%d]", dupe_count);
97 /* try to find unused device */
98 for (i = 0; i < IN_MAX_DEVS; i++)
99 if (!in_devices[i].probed) break;
100 if (i >= IN_MAX_DEVS) {
101 printf("input: too many devices, can't add %s\n", name);
104 in_free(&in_devices[i]);
111 binds = in_alloc_binds(drv_id);
117 in_devices[i].name = tmp;
118 in_devices[i].binds = binds;
119 if (i + 1 > in_dev_count)
120 in_dev_count = i + 1;
122 printf("input: new device #%d \"%s\"\n", i, name);
124 in_devices[i].probed = 1;
125 in_devices[i].does_combos = combos;
126 in_devices[i].drv_id = drv_id;
127 in_devices[i].drv_fd_hnd = drv_fd_hnd;
128 in_devices[i].drv_data = drv_data;
130 if (in_devices[i].binds != NULL) {
131 ret = DRV(drv_id).clean_binds(drv_data, in_devices[i].binds);
133 /* no useable binds */
134 free(in_devices[i].binds);
135 in_devices[i].binds = NULL;
140 /* key combo handling, to be called by drivers that support it */
141 void in_combos_find(int *binds, int last_key, int *combo_keys, int *combo_acts)
145 *combo_keys = *combo_acts = 0;
146 for (act = 0; act < sizeof(binds[0]) * 8; act++)
149 for (u = 0; u <= last_key; u++)
150 if (binds[u] & (1 << act))
155 // loop again and mark those keys and actions as combo
156 for (u = 0; u <= last_key; u++)
158 if (binds[u] & (1 << act)) {
159 *combo_keys |= 1 << u;
160 *combo_acts |= 1 << act;
167 int in_combos_do(int keys, int *binds, int last_key, int combo_keys, int combo_acts)
171 for (i = 0; i <= last_key; i++)
174 if (!(keys & (1 << i)))
181 if (combo_keys & (1 << i))
183 int acts_c = acts & combo_acts;
186 // let's try to find the other one
187 for (u = i + 1; u <= last_key; u++)
188 if ( (keys & (1 << u)) && (binds[u] & acts_c) ) {
189 ret |= acts_c & binds[u];
190 keys &= ~((1 << i) | (1 << u));
194 // add non-combo actions if combo ones were not found
196 ret |= acts & ~combo_acts;
208 in_have_async_devs = 0;
209 for (i = 0; i < in_dev_count; i++)
210 in_devices[i].probed = 0;
212 for (i = 1; i < IN_DRVID_COUNT; i++)
213 in_drivers[i].probe();
215 /* get rid of devs without binds and probes */
216 for (i = 0; i < in_dev_count; i++) {
217 if (!in_devices[i].probed && in_devices[i].binds == NULL) {
219 if (i < in_dev_count) {
220 free(in_devices[i].name);
221 memmove(&in_devices[i], &in_devices[i+1],
222 (in_dev_count - i) * sizeof(in_devices[0]));
228 if (in_devices[i].probed && in_devices[i].drv_fd_hnd == -1)
229 in_have_async_devs = 1;
232 if (in_have_async_devs)
233 printf("input: async-only devices detected..\n");
241 for (i = 0; i < in_dev_count; i++) {
242 in_dev_t *dev = &in_devices[i];
243 if (dev->probed && dev->binds != NULL) {
244 switch (dev->drv_id) {
247 result |= in_evdev_update(dev->drv_data, dev->binds);
252 result |= in_gp2x_update(dev->drv_data, dev->binds);
262 static int menu_key_state = 0;
264 void in_set_blocking(int is_blocking)
268 /* have_async_devs means we will have to do all reads async anyway.. */
269 if (!in_have_async_devs) {
270 for (i = 0; i < in_dev_count; i++) {
271 if (in_devices[i].probed)
272 DRV(in_devices[i].drv_id).set_blocking(in_devices[i].drv_data, is_blocking);
280 ret = in_update_keycode(NULL, NULL, 0);
284 static int in_update_kc_async(int *dev_id_out, int *is_down_out, int timeout_ms)
286 int i, is_down, result;
289 ticks = plat_get_ticks_ms();
293 for (i = 0; i < in_dev_count; i++) {
294 in_dev_t *d = &in_devices[i];
298 result = DRV(d->drv_id).update_keycode(d->drv_data, &is_down);
305 *is_down_out = is_down;
309 if (timeout_ms >= 0 && (int)(plat_get_ticks_ms() - ticks) > timeout_ms)
319 * wait for a press, always return some keycode or -1 on timeout or error
321 int in_update_keycode(int *dev_id_out, int *is_down_out, int timeout_ms)
323 int result = -1, dev_id = 0, is_down, result_menu;
324 int fds_hnds[IN_MAX_DEVS];
325 int i, ret, count = 0;
326 in_drv_t *drv = NULL;
329 if (in_have_async_devs) {
330 result = in_update_kc_async(&dev_id, &is_down, timeout_ms);
333 drv = &DRV(in_devices[dev_id].drv_id);
337 ticks = plat_get_ticks_ms();
339 for (i = 0; i < in_dev_count; i++) {
340 if (in_devices[i].probed)
341 fds_hnds[count++] = in_devices[i].drv_fd_hnd;
345 /* don't deadlock, fail */
346 printf("input: failed to find devices to read\n");
352 ret = plat_wait_event(fds_hnds, count, timeout_ms);
356 for (i = 0; i < in_dev_count; i++) {
357 if (in_devices[i].drv_fd_hnd == ret) {
363 drv = &DRV(in_devices[dev_id].drv_id);
364 result = drv->update_keycode(in_devices[dev_id].drv_data, &is_down);
366 /* update_keycode() might return -1 when some not interesting
367 * event happened, like sync event for evdev. */
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(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);
411 code = DRV(in_devices[dev_id].drv_id).menu_translate(code);
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)
469 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
472 count = in_bind_count(in_devices[dev_id].drv_id);
473 return in_devices[dev_id].binds + count;
476 int in_get_dev_info(int dev_id, int what)
478 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
482 case IN_INFO_BIND_COUNT:
483 return in_bind_count(in_devices[dev_id].drv_id);
484 case IN_INFO_DOES_COMBOS:
485 return in_devices[dev_id].does_combos;
491 const char *in_get_dev_name(int dev_id, int must_be_active, int skip_pfix)
493 const char *name, *tmp;
495 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
498 if (must_be_active && !in_devices[dev_id].probed)
501 name = in_devices[dev_id].name;
502 if (name == NULL || !skip_pfix)
506 tmp = strchr(name, ':');
513 /* never returns NULL */
514 const char *in_get_key_name(int dev_id, int keycode)
516 static char xname[16];
519 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
522 name = DRV(in_devices[dev_id].drv_id).get_key_name(keycode);
526 /* assume scancode */
527 if ((keycode >= '0' && keycode <= '9') || (keycode >= 'a' && keycode <= 'z')
528 || (keycode >= 'A' && keycode <= 'Z'))
529 sprintf(xname, "%c", keycode);
531 sprintf(xname, "\\x%02X", keycode);
535 int in_bind_key(int dev_id, int keycode, int mask, int force_unbind)
540 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
542 dev = &in_devices[dev_id];
544 if (dev->binds == NULL) {
547 dev->binds = in_alloc_binds(dev->drv_id);
548 if (dev->binds == NULL)
552 count = in_bind_count(dev->drv_id);
553 if (keycode < 0 || keycode >= count)
557 dev->binds[keycode] &= ~mask;
559 dev->binds[keycode] ^= mask;
561 ret = DRV(dev->drv_id).clean_binds(dev->drv_data, dev->binds);
570 /* returns device id, or -1 on error */
571 int in_config_parse_dev(const char *name)
575 for (i = 0; i < IN_DRVID_COUNT; i++) {
576 int len = strlen(in_drivers[i].prefix);
577 if (strncmp(name, in_drivers[i].prefix, len) == 0) {
584 printf("input: missing driver for %s\n", name);
588 for (i = 0; i < in_dev_count; i++)
590 if (in_devices[i].name == NULL)
592 if (strcmp(in_devices[i].name, name) == 0)
596 if (i >= IN_MAX_DEVS)
598 /* try to find unused device */
599 for (i = 0; i < IN_MAX_DEVS; i++)
600 if (in_devices[i].name == NULL) break;
601 if (i >= IN_MAX_DEVS) {
602 printf("input: too many devices, can't add %s\n", name);
607 memset(&in_devices[i], 0, sizeof(in_devices[i]));
609 in_devices[i].name = strdup(name);
610 if (in_devices[i].name == NULL)
613 if (i + 1 > in_dev_count)
614 in_dev_count = i + 1;
615 in_devices[i].drv_id = drv_id;
621 * To reduce size of game specific configs, default binds are not saved.
622 * So we mark default binds in in_config_start(), override them in in_config_bind_key(),
623 * and restore whatever default binds are left in in_config_end().
625 void in_config_start(void)
629 /* mark all default binds, so they get overwritten by func below */
630 for (i = 0; i < IN_MAX_DEVS; i++) {
631 int n, count, *binds, *def_binds;
633 binds = in_devices[i].binds;
637 count = in_bind_count(in_devices[i].drv_id);
638 def_binds = binds + count;
640 for (n = 0; n < count; n++)
641 if (binds[n] == def_binds[n])
646 int in_config_bind_key(int dev_id, const char *key, int binds)
651 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
653 dev = &in_devices[dev_id];
655 count = in_bind_count(dev->drv_id);
657 /* maybe a raw code? */
658 if (key[0] == '\\' && key[1] == 'x') {
660 kc = (int)strtoul(key + 2, &p, 16);
661 if (p == NULL || *p != 0)
665 /* device specific key name */
666 if (dev->binds == NULL) {
667 dev->binds = in_alloc_binds(dev->drv_id);
668 if (dev->binds == NULL)
673 kc = DRV(dev->drv_id).get_key_code(key);
674 if (kc < 0 && strlen(key) == 1) {
675 /* assume scancode */
680 if (kc < 0 || kc >= count) {
681 printf("input: bad key: %s\n", key);
685 if (dev->binds[kc] == -1)
687 dev->binds[kc] |= binds;
692 void in_config_end(void)
696 for (i = 0; i < IN_MAX_DEVS; i++) {
697 int n, ret, count, *binds, *def_binds;
698 in_dev_t *dev = &in_devices[i];
700 if (dev->binds == NULL)
703 count = in_bind_count(dev->drv_id);
705 def_binds = binds + count;
707 for (n = 0; n < count; n++)
709 binds[n] = def_binds[n];
711 if (dev->drv_data == NULL)
714 ret = DRV(dev->drv_id).clean_binds(dev->drv_data, binds);
716 /* no useable binds */
723 void in_debug_dump(void)
727 printf("# drv probed binds name\n");
728 for (i = 0; i < IN_MAX_DEVS; i++) {
729 in_dev_t *d = &in_devices[i];
730 if (!d->probed && d->name == NULL && d->binds == NULL)
732 printf("%d %3d %6c %5c %s\n", i, d->drv_id, d->probed ? 'y' : 'n',
733 d->binds ? 'y' : 'n', d->name);
737 /* handlers for unknown/not_preset drivers */
739 static void in_def_probe(void) {}
740 static void in_def_free(void *drv_data) {}
741 static int in_def_get_bind_count(void) { return 0; }
742 static void in_def_get_def_binds(int *binds) {}
743 static int in_def_clean_binds(void *drv_data, int *binds) { return 0; }
744 static void in_def_set_blocking(void *data, int y) {}
745 static int in_def_update_keycode(void *drv_data, int *is_down) { return 0; }
746 static int in_def_menu_translate(int keycode) { return keycode; }
747 static int in_def_get_key_code(const char *key_name) { return 0; }
748 static const char *in_def_get_key_name(int keycode) { return NULL; }
754 memset(in_drivers, 0, sizeof(in_drivers));
755 memset(in_devices, 0, sizeof(in_devices));
758 for (i = 0; i < IN_DRVID_COUNT; i++) {
759 in_drivers[i].prefix = "none:";
760 in_drivers[i].probe = in_def_probe;
761 in_drivers[i].free = in_def_free;
762 in_drivers[i].get_bind_count = in_def_get_bind_count;
763 in_drivers[i].get_def_binds = in_def_get_def_binds;
764 in_drivers[i].clean_binds = in_def_clean_binds;
765 in_drivers[i].set_blocking = in_def_set_blocking;
766 in_drivers[i].update_keycode = in_def_update_keycode;
767 in_drivers[i].menu_translate = in_def_menu_translate;
768 in_drivers[i].get_key_code = in_def_get_key_code;
769 in_drivers[i].get_key_name = in_def_get_key_name;
773 in_gp2x_init(&in_drivers[IN_DRVID_GP2X]);
776 in_evdev_init(&in_drivers[IN_DRVID_EVDEV]);
793 ret = in_update_keycode(&dev, &down);
794 printf("#%i: %i %i (%s)\n", dev, down, ret, in_get_key_name(dev, ret));
798 ret = in_menu_wait_any();
799 printf("%08x\n", ret);