8 #include "../linux/in_evdev.h"
9 #include "../gp2x/in_gp2x.h"
21 static in_drv_t in_drivers[IN_DRVID_COUNT];
22 static in_dev_t in_devices[IN_MAX_DEVS];
23 static int in_dev_count = 0;
24 static int in_have_async_devs = 0;
26 #define DRV(id) in_drivers[(unsigned)(id) < IN_DRVID_COUNT ? (id) : 0]
29 static int in_bind_count(int drv_id)
31 int count = DRV(drv_id).get_bind_count();
33 printf("input: failed to get bind count for drv %d\n", drv_id);
38 static int *in_alloc_binds(int drv_id)
42 count = in_bind_count(drv_id);
46 binds = calloc(count * 2, sizeof(binds[0]));
50 DRV(drv_id).get_def_binds(binds + count);
51 memcpy(binds, binds + count, count * sizeof(binds[0]));
56 static void in_free(in_dev_t *dev)
59 DRV(dev->drv_id).free(dev->drv_data);
68 /* to be called by drivers
69 * async devices must set drv_fd_hnd to -1 */
70 void in_register(const char *nname, int drv_id, int drv_fd_hnd, void *drv_data)
72 int i, ret, dupe_count = 0, *binds;
73 char name[256], *name_end, *tmp;
75 strncpy(name, nname, sizeof(name));
76 name[sizeof(name)-12] = 0;
77 name_end = name + strlen(name);
79 for (i = 0; i < in_dev_count; i++)
81 if (in_devices[i].name == NULL)
83 if (strcmp(in_devices[i].name, name) == 0)
85 if (in_devices[i].probed) {
87 sprintf(name_end, " [%d]", dupe_count);
96 /* try to find unused device */
97 for (i = 0; i < IN_MAX_DEVS; i++)
98 if (!in_devices[i].probed) break;
99 if (i >= IN_MAX_DEVS) {
100 printf("input: too many devices, can't add %s\n", name);
103 in_free(&in_devices[i]);
110 binds = in_alloc_binds(drv_id);
116 in_devices[i].name = tmp;
117 in_devices[i].binds = binds;
118 if (i + 1 > in_dev_count)
119 in_dev_count = i + 1;
121 printf("input: new device #%d \"%s\"\n", i, name);
123 in_devices[i].probed = 1;
124 in_devices[i].drv_id = drv_id;
125 in_devices[i].drv_fd_hnd = drv_fd_hnd;
126 in_devices[i].drv_data = drv_data;
128 if (in_devices[i].binds != NULL) {
129 ret = DRV(drv_id).clean_binds(drv_data, in_devices[i].binds);
131 /* no useable binds */
132 free(in_devices[i].binds);
133 in_devices[i].binds = NULL;
142 in_have_async_devs = 0;
143 for (i = 0; i < in_dev_count; i++)
144 in_devices[i].probed = 0;
146 for (i = 1; i < IN_DRVID_COUNT; i++)
147 in_drivers[i].probe();
149 /* get rid of devs without binds and probes */
150 for (i = 0; i < in_dev_count; i++) {
151 if (!in_devices[i].probed && in_devices[i].binds == NULL) {
153 if (i < in_dev_count) {
154 free(in_devices[i].name);
155 memmove(&in_devices[i], &in_devices[i+1],
156 (in_dev_count - i) * sizeof(in_devices[0]));
162 if (in_devices[i].probed && in_devices[i].drv_fd_hnd == -1)
163 in_have_async_devs = 1;
166 if (in_have_async_devs)
167 printf("input: async-only devices detected..\n");
175 for (i = 0; i < in_dev_count; i++) {
176 in_dev_t *dev = &in_devices[i];
177 if (dev->probed && dev->binds != NULL) {
178 switch (dev->drv_id) {
181 result |= in_evdev_update(dev->drv_data, dev->binds);
186 result |= in_gp2x_update(dev->drv_data, dev->binds);
196 static int menu_key_state = 0;
198 void in_set_blocking(int is_blocking)
202 /* have_async_devs means we will have to do all reads async anyway.. */
203 if (!in_have_async_devs) {
204 for (i = 0; i < in_dev_count; i++) {
205 if (in_devices[i].probed)
206 DRV(in_devices[i].drv_id).set_blocking(in_devices[i].drv_data, is_blocking);
214 ret = in_update_keycode(NULL, NULL, 0);
218 static int in_update_kc_async(int *dev_id_out, int *is_down_out, int timeout_ms)
220 int i, is_down, result;
223 ticks = plat_get_ticks_ms();
227 for (i = 0; i < in_dev_count; i++) {
228 in_dev_t *d = &in_devices[i];
232 result = DRV(d->drv_id).update_keycode(d->drv_data, &is_down);
239 *is_down_out = is_down;
243 if (timeout_ms >= 0 && (int)(plat_get_ticks_ms() - ticks) > timeout_ms)
253 * wait for a press, always return some keycode or -1 on timeout or error
255 int in_update_keycode(int *dev_id_out, int *is_down_out, int timeout_ms)
257 int result = -1, dev_id = 0, is_down, result_menu;
258 int fds_hnds[IN_MAX_DEVS];
259 int i, ret, count = 0;
263 if (in_have_async_devs) {
264 result = in_update_kc_async(&dev_id, &is_down, timeout_ms);
267 drv = &DRV(in_devices[dev_id].drv_id);
271 ticks = plat_get_ticks_ms();
273 for (i = 0; i < in_dev_count; i++) {
274 if (in_devices[i].probed)
275 fds_hnds[count++] = in_devices[i].drv_fd_hnd;
279 /* don't deadlock, fail */
280 printf("input: failed to find devices to read\n");
286 ret = plat_wait_event(fds_hnds, count, timeout_ms);
290 for (i = 0; i < in_dev_count; i++) {
291 if (in_devices[i].drv_fd_hnd == ret) {
297 drv = &DRV(in_devices[dev_id].drv_id);
298 result = drv->update_keycode(in_devices[dev_id].drv_data, &is_down);
300 /* update_keycode() might return -1 when some not interesting
301 * event happened, like sync event for evdev. */
305 if (timeout_ms >= 0) {
306 unsigned int ticks2 = plat_get_ticks_ms();
307 timeout_ms -= ticks2 - ticks;
317 /* keep track of menu key state, to allow mixing
318 * in_update_keycode() and in_menu_wait_any() calls */
319 result_menu = drv->menu_translate(result);
320 if (result_menu != 0) {
322 menu_key_state |= result_menu;
324 menu_key_state &= ~result_menu;
327 if (dev_id_out != NULL)
328 *dev_id_out = dev_id;
329 if (is_down_out != NULL)
330 *is_down_out = is_down;
334 /* same as above, only return bitfield of PBTN_* */
335 int in_menu_wait_any(int timeout_ms)
337 int keys_old = menu_key_state;
341 int code, is_down = 0, dev_id = 0;
343 code = in_update_keycode(&dev_id, &is_down, timeout_ms);
345 code = DRV(in_devices[dev_id].drv_id).menu_translate(code);
351 if (keys_old != menu_key_state)
355 return menu_key_state;
358 /* wait for menu input, do autorepeat */
359 int in_menu_wait(int interesting, int autorep_delay_ms)
361 static int inp_prev = 0;
362 static int repeats = 0;
363 int ret, release = 0, wait = 666;
366 wait = autorep_delay_ms;
368 ret = in_menu_wait_any(wait);
372 while (!(ret & interesting)) {
373 ret = in_menu_wait_any(-1);
377 if (release || ret != inp_prev)
382 /* we don't need diagonals in menus */
383 if ((ret & PBTN_UP) && (ret & PBTN_LEFT)) ret &= ~PBTN_LEFT;
384 if ((ret & PBTN_UP) && (ret & PBTN_RIGHT)) ret &= ~PBTN_RIGHT;
385 if ((ret & PBTN_DOWN) && (ret & PBTN_LEFT)) ret &= ~PBTN_LEFT;
386 if ((ret & PBTN_DOWN) && (ret & PBTN_RIGHT)) ret &= ~PBTN_RIGHT;
391 const int *in_get_dev_binds(int dev_id)
393 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
396 return in_devices[dev_id].binds;
399 const int *in_get_dev_def_binds(int dev_id)
403 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
406 count = in_bind_count(in_devices[dev_id].drv_id);
407 return in_devices[dev_id].binds + count;
410 int in_get_dev_bind_count(int dev_id)
412 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
415 return in_bind_count(in_devices[dev_id].drv_id);
418 const char *in_get_dev_name(int dev_id, int must_be_active, int skip_pfix)
420 const char *name, *tmp;
422 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
425 if (must_be_active && !in_devices[dev_id].probed)
428 name = in_devices[dev_id].name;
429 if (name == NULL || !skip_pfix)
433 tmp = strchr(name, ':');
440 /* never returns NULL */
441 const char *in_get_key_name(int dev_id, int keycode)
443 static char xname[16];
446 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
449 name = DRV(in_devices[dev_id].drv_id).get_key_name(keycode);
453 /* assume scancode */
454 if ((keycode >= '0' && keycode <= '9') || (keycode >= 'a' && keycode <= 'z')
455 || (keycode >= 'A' && keycode <= 'Z'))
456 sprintf(xname, "%c", keycode);
458 sprintf(xname, "\\x%02X", keycode);
462 int in_bind_key(int dev_id, int keycode, int mask, int force_unbind)
467 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
469 dev = &in_devices[dev_id];
471 if (dev->binds == NULL) {
474 dev->binds = in_alloc_binds(dev->drv_id);
475 if (dev->binds == NULL)
479 count = in_bind_count(dev->drv_id);
480 if (keycode < 0 || keycode >= count)
484 dev->binds[keycode] &= ~mask;
486 dev->binds[keycode] ^= mask;
488 ret = DRV(dev->drv_id).clean_binds(dev->drv_data, dev->binds);
497 /* returns device id, or -1 on error */
498 int in_config_parse_dev(const char *name)
502 for (i = 0; i < IN_DRVID_COUNT; i++) {
503 int len = strlen(in_drivers[i].prefix);
504 if (strncmp(name, in_drivers[i].prefix, len) == 0) {
511 printf("input: missing driver for %s\n", name);
515 for (i = 0; i < in_dev_count; i++)
517 if (in_devices[i].name == NULL)
519 if (strcmp(in_devices[i].name, name) == 0)
523 if (i >= IN_MAX_DEVS)
525 /* try to find unused device */
526 for (i = 0; i < IN_MAX_DEVS; i++)
527 if (in_devices[i].name == NULL) break;
528 if (i >= IN_MAX_DEVS) {
529 printf("input: too many devices, can't add %s\n", name);
534 memset(&in_devices[i], 0, sizeof(in_devices[i]));
536 in_devices[i].name = strdup(name);
537 if (in_devices[i].name == NULL)
540 if (i + 1 > in_dev_count)
541 in_dev_count = i + 1;
542 in_devices[i].drv_id = drv_id;
548 * To reduce size of game specific configs, default binds are not saved.
549 * So we mark default binds in in_config_start(), override them in in_config_bind_key(),
550 * and restore whatever default binds are left in in_config_end().
552 void in_config_start(void)
556 /* mark all default binds, so they get overwritten by func below */
557 for (i = 0; i < IN_MAX_DEVS; i++) {
558 int n, count, *binds, *def_binds;
560 binds = in_devices[i].binds;
564 count = in_bind_count(in_devices[i].drv_id);
565 def_binds = binds + count;
567 for (n = 0; n < count; n++)
568 if (binds[n] == def_binds[n])
573 int in_config_bind_key(int dev_id, const char *key, int binds)
578 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
580 dev = &in_devices[dev_id];
582 count = in_bind_count(dev->drv_id);
584 /* maybe a raw code? */
585 if (key[0] == '\\' && key[1] == 'x') {
587 kc = (int)strtoul(key + 2, &p, 16);
588 if (p == NULL || *p != 0)
592 /* device specific key name */
593 if (dev->binds == NULL) {
594 dev->binds = in_alloc_binds(dev->drv_id);
595 if (dev->binds == NULL)
600 kc = DRV(dev->drv_id).get_key_code(key);
601 if (kc < 0 && strlen(key) == 1) {
602 /* assume scancode */
607 if (kc < 0 || kc >= count) {
608 printf("input: bad key: %s\n", key);
612 if (dev->binds[kc] == -1)
614 dev->binds[kc] |= binds;
619 void in_config_end(void)
623 for (i = 0; i < IN_MAX_DEVS; i++) {
624 int n, ret, count, *binds, *def_binds;
625 in_dev_t *dev = &in_devices[i];
627 if (dev->binds == NULL)
630 count = in_bind_count(dev->drv_id);
632 def_binds = binds + count;
634 for (n = 0; n < count; n++)
636 binds[n] = def_binds[n];
638 if (dev->drv_data == NULL)
641 ret = DRV(dev->drv_id).clean_binds(dev->drv_data, binds);
643 /* no useable binds */
650 void in_debug_dump(void)
654 printf("# drv probed binds name\n");
655 for (i = 0; i < IN_MAX_DEVS; i++) {
656 in_dev_t *d = &in_devices[i];
657 if (!d->probed && d->name == NULL && d->binds == NULL)
659 printf("%d %3d %6c %5c %s\n", i, d->drv_id, d->probed ? 'y' : 'n',
660 d->binds ? 'y' : 'n', d->name);
664 /* handlers for unknown/not_preset drivers */
666 static void in_def_probe(void) {}
667 static void in_def_free(void *drv_data) {}
668 static int in_def_get_bind_count(void) { return 0; }
669 static void in_def_get_def_binds(int *binds) {}
670 static int in_def_clean_binds(void *drv_data, int *binds) { return 0; }
671 static void in_def_set_blocking(void *data, int y) {}
672 static int in_def_update_keycode(void *drv_data, int *is_down) { return 0; }
673 static int in_def_menu_translate(int keycode) { return keycode; }
674 static int in_def_get_key_code(const char *key_name) { return 0; }
675 static const char *in_def_get_key_name(int keycode) { return NULL; }
681 memset(in_drivers, 0, sizeof(in_drivers));
682 memset(in_devices, 0, sizeof(in_devices));
685 for (i = 0; i < IN_DRVID_COUNT; i++) {
686 in_drivers[i].prefix = "none:";
687 in_drivers[i].probe = in_def_probe;
688 in_drivers[i].free = in_def_free;
689 in_drivers[i].get_bind_count = in_def_get_bind_count;
690 in_drivers[i].get_def_binds = in_def_get_def_binds;
691 in_drivers[i].clean_binds = in_def_clean_binds;
692 in_drivers[i].set_blocking = in_def_set_blocking;
693 in_drivers[i].update_keycode = in_def_update_keycode;
694 in_drivers[i].menu_translate = in_def_menu_translate;
695 in_drivers[i].get_key_code = in_def_get_key_code;
696 in_drivers[i].get_key_name = in_def_get_key_name;
700 in_gp2x_init(&in_drivers[IN_DRVID_GP2X]);
703 in_evdev_init(&in_drivers[IN_DRVID_EVDEV]);
720 ret = in_update_keycode(&dev, &down);
721 printf("#%i: %i %i (%s)\n", dev, down, ret, in_get_key_name(dev, ret));
725 ret = in_menu_wait_any();
726 printf("%08x\n", ret);