7 #include "../linux/in_evdev.h"
8 #include "../gp2x/in_gp2x.h"
20 static in_drv_t in_drivers[IN_DRVID_COUNT];
21 static in_dev_t in_devices[IN_MAX_DEVS];
22 static int in_dev_count = 0;
23 static int in_have_async_devs = 0;
25 #define DRV(id) in_drivers[(unsigned)(id) < IN_DRVID_COUNT ? (id) : 0]
28 static int in_bind_count(int drv_id)
30 int count = DRV(drv_id).get_bind_count();
32 printf("input: failed to get bind count for drv %d\n", drv_id);
37 static int *in_alloc_binds(int drv_id)
41 count = in_bind_count(drv_id);
45 binds = calloc(count * 2, sizeof(binds[0]));
49 DRV(drv_id).get_def_binds(binds + count);
50 memcpy(binds, binds + count, count * sizeof(binds[0]));
55 static void in_free(in_dev_t *dev)
58 DRV(dev->drv_id).free(dev->drv_data);
67 /* to be called by drivers
68 * async devices must set drv_fd_hnd to -1 */
69 void in_register(const char *nname, int drv_id, int drv_fd_hnd, void *drv_data)
71 int i, ret, dupe_count = 0, *binds;
72 char name[256], *name_end, *tmp;
74 strncpy(name, nname, sizeof(name));
75 name[sizeof(name)-12] = 0;
76 name_end = name + strlen(name);
78 for (i = 0; i < in_dev_count; i++)
80 if (in_devices[i].name == NULL)
82 if (strcmp(in_devices[i].name, name) == 0)
84 if (in_devices[i].probed) {
86 sprintf(name_end, " [%d]", dupe_count);
95 /* try to find unused device */
96 for (i = 0; i < IN_MAX_DEVS; i++)
97 if (!in_devices[i].probed) break;
98 if (i >= IN_MAX_DEVS) {
99 printf("input: too many devices, can't add %s\n", name);
102 in_free(&in_devices[i]);
109 binds = in_alloc_binds(drv_id);
115 in_devices[i].name = tmp;
116 in_devices[i].binds = binds;
117 if (i + 1 > in_dev_count)
118 in_dev_count = i + 1;
120 printf("input: new device #%d \"%s\"\n", i, name);
122 in_devices[i].probed = 1;
123 in_devices[i].drv_id = drv_id;
124 in_devices[i].drv_fd_hnd = drv_fd_hnd;
125 in_devices[i].drv_data = drv_data;
127 if (in_devices[i].binds != NULL) {
128 ret = DRV(drv_id).clean_binds(drv_data, in_devices[i].binds);
130 /* no useable binds */
131 free(in_devices[i].binds);
132 in_devices[i].binds = NULL;
141 in_have_async_devs = 0;
142 for (i = 0; i < in_dev_count; i++)
143 in_devices[i].probed = 0;
145 for (i = 1; i < IN_DRVID_COUNT; i++)
146 in_drivers[i].probe();
148 /* get rid of devs without binds and probes */
149 for (i = 0; i < in_dev_count; i++) {
150 if (!in_devices[i].probed && in_devices[i].binds == NULL) {
152 if (i < in_dev_count) {
153 free(in_devices[i].name);
154 memmove(&in_devices[i], &in_devices[i+1],
155 (in_dev_count - i) * sizeof(in_devices[0]));
161 if (in_devices[i].probed && in_devices[i].drv_fd_hnd == -1)
162 in_have_async_devs = 1;
165 if (in_have_async_devs)
166 printf("input: async-only devices detected..\n");
174 for (i = 0; i < in_dev_count; i++) {
175 in_dev_t *dev = &in_devices[i];
176 if (dev->probed && dev->binds != NULL) {
177 switch (dev->drv_id) {
180 result |= in_evdev_update(dev->drv_data, dev->binds);
185 result |= in_gp2x_update(dev->drv_data, dev->binds);
195 static int menu_key_state = 0;
197 void in_set_blocking(int is_blocking)
201 /* have_async_devs means we will have to do all reads async anyway.. */
202 if (!in_have_async_devs) {
203 for (i = 0; i < in_dev_count; i++) {
204 if (in_devices[i].probed)
205 DRV(in_devices[i].drv_id).set_blocking(in_devices[i].drv_data, is_blocking);
213 ret = in_update_keycode(NULL, NULL, 0);
219 #include <sys/time.h>
222 static int in_update_kc_async(int *dev_id_out, int *is_down_out, int timeout_ms)
224 struct timeval start, now;
225 int i, is_down, result;
227 gettimeofday(&start, NULL);
231 for (i = 0; i < in_dev_count; i++) {
232 in_dev_t *d = &in_devices[i];
236 result = DRV(d->drv_id).update_keycode(d->drv_data, &is_down);
243 *is_down_out = is_down;
247 if (timeout_ms >= 0) {
248 gettimeofday(&now, NULL);
249 if ((now.tv_sec - start.tv_sec) * 1000 +
250 (now.tv_usec - start.tv_usec) / 1000 > timeout_ms)
261 * wait for a press, always return some keycode or -1 on timeout or error
263 int in_update_keycode(int *dev_id_out, int *is_down_out, int timeout_ms)
265 int result = 0, dev_id = 0, is_down, result_menu;
266 int fds_hnds[IN_MAX_DEVS];
267 int i, ret, count = 0;
270 if (in_have_async_devs) {
271 result = in_update_kc_async(&dev_id, &is_down, timeout_ms);
274 drv = &DRV(in_devices[dev_id].drv_id);
278 for (i = 0; i < in_dev_count; i++) {
279 if (in_devices[i].probed)
280 fds_hnds[count++] = in_devices[i].drv_fd_hnd;
284 /* don't deadlock, fail */
285 printf("input: failed to find devices to read\n");
290 /* TODO: move this block to platform/linux */
292 struct timeval tv, *timeout = NULL;
296 if (timeout_ms >= 0) {
297 tv.tv_sec = timeout_ms / 1000;
298 tv.tv_usec = (timeout_ms % 1000) * 1000;
303 for (i = 0; i < count; i++) {
304 if (fds_hnds[i] > fdmax) fdmax = fds_hnds[i];
305 FD_SET(fds_hnds[i], &fdset);
308 ret = select(fdmax + 1, &fdset, NULL, NULL, timeout);
311 perror("input: select failed");
317 return -1; /* timeout */
319 for (i = 0; i < count; i++)
320 if (FD_ISSET(fds_hnds[i], &fdset))
324 for (i = 0; i < in_dev_count; i++) {
325 if (in_devices[i].drv_fd_hnd == ret) {
331 drv = &DRV(in_devices[dev_id].drv_id);
332 result = drv->update_keycode(in_devices[dev_id].drv_data, &is_down);
334 /* update_keycode() might return -1 when some not interesting
335 * event happened, like sync event for evdev.
336 * XXX: timeout restarts.. */
341 /* keep track of menu key state, to allow mixing
342 * in_update_keycode() and in_menu_wait_any() calls */
343 result_menu = drv->menu_translate(result);
344 if (result_menu != 0) {
346 menu_key_state |= result_menu;
348 menu_key_state &= ~result_menu;
351 if (dev_id_out != NULL)
352 *dev_id_out = dev_id;
353 if (is_down_out != NULL)
354 *is_down_out = is_down;
358 /* same as above, only return bitfield of PBTN_* */
359 int in_menu_wait_any(int timeout_ms)
361 int keys_old = menu_key_state;
365 int code, is_down = 0, dev_id = 0;
367 code = in_update_keycode(&dev_id, &is_down, timeout_ms);
369 code = DRV(in_devices[dev_id].drv_id).menu_translate(code);
375 if (keys_old != menu_key_state)
379 return menu_key_state;
382 /* wait for menu input, do autorepeat */
383 int in_menu_wait(int interesting)
385 static int inp_prev = 0;
386 static int repeats = 0;
387 int ret, release = 0, wait = 666;
392 ret = in_menu_wait_any(wait);
396 while (!(ret & interesting)) {
397 ret = in_menu_wait_any(-1);
401 if (release || ret != inp_prev)
406 /* we don't need diagonals in menus */
407 if ((ret & PBTN_UP) && (ret & PBTN_LEFT)) ret &= ~PBTN_LEFT;
408 if ((ret & PBTN_UP) && (ret & PBTN_RIGHT)) ret &= ~PBTN_RIGHT;
409 if ((ret & PBTN_DOWN) && (ret & PBTN_LEFT)) ret &= ~PBTN_LEFT;
410 if ((ret & PBTN_DOWN) && (ret & PBTN_RIGHT)) ret &= ~PBTN_RIGHT;
415 const int *in_get_dev_binds(int dev_id)
417 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
420 return in_devices[dev_id].binds;
423 const int *in_get_dev_def_binds(int dev_id)
427 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
430 count = in_bind_count(in_devices[dev_id].drv_id);
431 return in_devices[dev_id].binds + count;
434 int in_get_dev_bind_count(int dev_id)
436 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
439 return in_bind_count(in_devices[dev_id].drv_id);
442 const char *in_get_dev_name(int dev_id, int must_be_active)
444 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
447 if (must_be_active && !in_devices[dev_id].probed)
449 return in_devices[dev_id].name;
452 /* never returns NULL */
453 const char *in_get_key_name(int dev_id, int keycode)
455 static char xname[16];
458 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
461 name = DRV(in_devices[dev_id].drv_id).get_key_name(keycode);
465 /* assume scancode */
466 if ((keycode >= '0' && keycode <= '9') || (keycode >= 'a' && keycode <= 'z')
467 || (keycode >= 'A' && keycode <= 'Z'))
468 sprintf(xname, "%c", keycode);
470 sprintf(xname, "\\x%02X", keycode);
474 int in_bind_key(int dev_id, int keycode, int mask, int force_unbind)
479 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
481 dev = &in_devices[dev_id];
483 if (dev->binds == NULL) {
486 dev->binds = in_alloc_binds(dev->drv_id);
487 if (dev->binds == NULL)
491 count = in_bind_count(dev->drv_id);
492 if (keycode < 0 || keycode >= count)
496 dev->binds[keycode] &= ~mask;
498 dev->binds[keycode] ^= mask;
500 ret = DRV(dev->drv_id).clean_binds(dev->drv_data, dev->binds);
509 /* returns device id, or -1 on error */
510 int in_config_parse_dev(const char *name)
514 for (i = 0; i < IN_DRVID_COUNT; i++) {
515 int len = strlen(in_drivers[i].prefix);
516 if (strncmp(name, in_drivers[i].prefix, len) == 0) {
523 printf("input: missing driver for %s\n", name);
527 for (i = 0; i < in_dev_count; i++)
529 if (in_devices[i].name == NULL)
531 if (strcmp(in_devices[i].name, name) == 0)
535 if (i >= IN_MAX_DEVS)
537 /* try to find unused device */
538 for (i = 0; i < IN_MAX_DEVS; i++)
539 if (in_devices[i].name == NULL) break;
540 if (i >= IN_MAX_DEVS) {
541 printf("input: too many devices, can't add %s\n", name);
546 memset(&in_devices[i], 0, sizeof(in_devices[i]));
548 in_devices[i].name = strdup(name);
549 if (in_devices[i].name == NULL)
552 if (i + 1 > in_dev_count)
553 in_dev_count = i + 1;
554 in_devices[i].drv_id = drv_id;
560 * To reduce size of game specific configs, default binds are not saved.
561 * So we mark default binds in in_config_start(), override them in in_config_bind_key(),
562 * and restore whatever default binds are left in in_config_end().
564 void in_config_start(void)
568 /* mark all default binds, so they get overwritten by func below */
569 for (i = 0; i < IN_MAX_DEVS; i++) {
570 int n, count, *binds, *def_binds;
572 binds = in_devices[i].binds;
576 count = in_bind_count(in_devices[i].drv_id);
577 def_binds = binds + count;
579 for (n = 0; n < count; n++)
580 if (binds[n] == def_binds[n])
585 int in_config_bind_key(int dev_id, const char *key, int binds)
590 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
592 dev = &in_devices[dev_id];
594 count = in_bind_count(dev->drv_id);
596 /* maybe a raw code? */
597 if (key[0] == '\\' && key[1] == 'x') {
599 kc = (int)strtoul(key + 2, &p, 16);
600 if (p == NULL || *p != 0)
604 /* device specific key name */
605 if (dev->binds == NULL) {
606 dev->binds = in_alloc_binds(dev->drv_id);
607 if (dev->binds == NULL)
612 kc = DRV(dev->drv_id).get_key_code(key);
613 if (kc < 0 && strlen(key) == 1) {
614 /* assume scancode */
619 if (kc < 0 || kc >= count) {
620 printf("input: bad key: %s\n", key);
624 if (dev->binds[kc] == -1)
626 dev->binds[kc] |= binds;
631 void in_config_end(void)
635 for (i = 0; i < IN_MAX_DEVS; i++) {
636 int n, ret, count, *binds, *def_binds;
637 in_dev_t *dev = &in_devices[i];
639 if (dev->binds == NULL)
642 count = in_bind_count(dev->drv_id);
644 def_binds = binds + count;
646 for (n = 0; n < count; n++)
648 binds[n] = def_binds[n];
650 if (dev->drv_data == NULL)
653 ret = DRV(dev->drv_id).clean_binds(dev->drv_data, binds);
655 /* no useable binds */
662 void in_debug_dump(void)
666 printf("# drv probed binds name\n");
667 for (i = 0; i < IN_MAX_DEVS; i++) {
668 in_dev_t *d = &in_devices[i];
669 if (!d->probed && d->name == NULL && d->binds == NULL)
671 printf("%d %3d %6c %5c %s\n", i, d->drv_id, d->probed ? 'y' : 'n',
672 d->binds ? 'y' : 'n', d->name);
676 /* handlers for unknown/not_preset drivers */
678 static void in_def_probe(void) {}
679 static void in_def_free(void *drv_data) {}
680 static int in_def_get_bind_count(void) { return 0; }
681 static void in_def_get_def_binds(int *binds) {}
682 static int in_def_clean_binds(void *drv_data, int *binds) { return 0; }
683 static void in_def_set_blocking(void *data, int y) {}
684 static int in_def_update_keycode(void *drv_data, int *is_down) { return 0; }
685 static int in_def_menu_translate(int keycode) { return keycode; }
686 static int in_def_get_key_code(const char *key_name) { return 0; }
687 static const char *in_def_get_key_name(int keycode) { return NULL; }
693 memset(in_drivers, 0, sizeof(in_drivers));
694 memset(in_devices, 0, sizeof(in_devices));
697 for (i = 0; i < IN_DRVID_COUNT; i++) {
698 in_drivers[i].prefix = "none:";
699 in_drivers[i].probe = in_def_probe;
700 in_drivers[i].free = in_def_free;
701 in_drivers[i].get_bind_count = in_def_get_bind_count;
702 in_drivers[i].get_def_binds = in_def_get_def_binds;
703 in_drivers[i].clean_binds = in_def_clean_binds;
704 in_drivers[i].set_blocking = in_def_set_blocking;
705 in_drivers[i].update_keycode = in_def_update_keycode;
706 in_drivers[i].menu_translate = in_def_menu_translate;
707 in_drivers[i].get_key_code = in_def_get_key_code;
708 in_drivers[i].get_key_name = in_def_get_key_name;
712 in_gp2x_init(&in_drivers[IN_DRVID_GP2X]);
715 in_evdev_init(&in_drivers[IN_DRVID_EVDEV]);
732 ret = in_update_keycode(&dev, &down);
733 printf("#%i: %i %i (%s)\n", dev, down, ret, in_get_key_name(dev, ret));
737 ret = in_menu_wait_any();
738 printf("%08x\n", ret);