7 #include "../linux/in_evdev.h"
19 static in_drv_t in_drivers[IN_DRVID_COUNT];
20 static in_dev_t in_devices[IN_MAX_DEVS];
21 static int in_dev_count = 0;
23 #define DRV(id) in_drivers[(unsigned)(id) < IN_DRVID_COUNT ? (id) : 0]
25 static int in_bind_count(int drv_id)
27 int count = DRV(drv_id).get_bind_count();
29 printf("input: failed to get bind count for drv %d\n", drv_id);
34 static int *in_alloc_binds(int drv_id)
38 count = in_bind_count(drv_id);
42 binds = calloc(count * 2, sizeof(binds[0]));
46 DRV(drv_id).get_def_binds(binds + count);
47 memcpy(binds, binds + count, count * sizeof(binds[0]));
52 static void in_free(in_dev_t *dev)
55 DRV(dev->drv_id).free(dev->drv_data);
64 /* to be called by drivers */
65 void in_register(const char *nname, int drv_id, int drv_fd_hnd, void *drv_data)
67 int i, ret, dupe_count = 0, *binds;
68 char name[256], *name_end, *tmp;
70 strncpy(name, nname, sizeof(name));
71 name[sizeof(name)-12] = 0;
72 name_end = name + strlen(name);
74 for (i = 0; i < in_dev_count; i++)
76 if (in_devices[i].name == NULL)
78 if (strcmp(in_devices[i].name, name) == 0)
80 if (in_devices[i].probed) {
82 sprintf(name_end, " [%d]", dupe_count);
91 /* try to find unused device */
92 for (i = 0; i < IN_MAX_DEVS; i++)
93 if (!in_devices[i].probed) break;
94 if (i >= IN_MAX_DEVS) {
95 printf("input: too many devices, can't add %s\n", name);
98 in_free(&in_devices[i]);
105 binds = in_alloc_binds(drv_id);
111 in_devices[i].name = tmp;
112 in_devices[i].binds = binds;
113 if (i + 1 > in_dev_count)
114 in_dev_count = i + 1;
116 printf("input: new device #%d \"%s\"\n", i, name);
118 in_devices[i].probed = 1;
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);
126 /* no useable binds */
127 free(in_devices[i].binds);
128 in_devices[i].binds = NULL;
136 for (i = 0; i < in_dev_count; i++)
137 in_devices[i].probed = 0;
139 for (i = 1; i < IN_DRVID_COUNT; i++)
140 in_drivers[i].probe();
142 /* get rid of devs without binds and probes */
143 for (i = 0; i < in_dev_count; i++) {
144 if (!in_devices[i].probed && in_devices[i].binds == NULL) {
146 if (i < in_dev_count) {
147 free(in_devices[i].name);
148 memmove(&in_devices[i], &in_devices[i+1],
149 (in_dev_count - i) * sizeof(in_devices[0]));
160 for (i = 0; i < in_dev_count; i++) {
161 in_dev_t *dev = &in_devices[i];
162 if (dev->probed && dev->binds != NULL) {
163 switch (dev->drv_id) {
166 result |= in_evdev_update(dev->drv_data, dev->binds);
176 static int menu_key_state = 0;
178 void in_set_blocking(int is_blocking)
182 for (i = 0; i < in_dev_count; i++) {
183 if (in_devices[i].probed)
184 DRV(in_devices[i].drv_id).set_blocking(in_devices[i].drv_data, is_blocking);
189 in_update_keycode(NULL, NULL, 0);
196 * update with wait for a press, always return some keycode
198 int in_update_keycode(int *dev_id_out, int *is_down_out, int timeout_ms)
200 int result = 0, dev_id = 0, is_down, result_menu;
201 int fds_hnds[IN_MAX_DEVS];
202 int i, ret, count = 0;
205 for (i = 0; i < in_dev_count; i++) {
206 if (in_devices[i].probed)
207 fds_hnds[count++] = in_devices[i].drv_fd_hnd;
211 /* don't deadlock, fail */
212 printf("input: failed to find devices to read\n");
217 /* TODO: move this block to platform/linux */
219 struct timeval tv, *timeout = NULL;
223 if (timeout_ms >= 0) {
224 tv.tv_sec = timeout_ms / 1000;
225 tv.tv_usec = (timeout_ms % 1000) * 1000;
230 for (i = 0; i < count; i++) {
231 if (fds_hnds[i] > fdmax) fdmax = fds_hnds[i];
232 FD_SET(fds_hnds[i], &fdset);
235 ret = select(fdmax + 1, &fdset, NULL, NULL, timeout);
238 perror("input: select failed");
244 return 0; /* timeout */
246 for (i = 0; i < count; i++)
247 if (FD_ISSET(fds_hnds[i], &fdset))
251 for (i = 0; i < in_dev_count; i++) {
252 if (in_devices[i].drv_fd_hnd == ret) {
258 drv = &DRV(in_devices[dev_id].drv_id);
259 result = drv->update_keycode(in_devices[dev_id].drv_data, &is_down);
261 /* update_keycode() might return 0 when some not interesting
262 * event happened, like sync event for evdev.
263 * XXX: timeout restarts.. */
267 /* keep track of menu key state, to allow mixing
268 * in_update_keycode() and in_menu_wait_any() calls */
269 result_menu = drv->menu_translate(result);
270 if (result_menu != 0) {
272 menu_key_state |= result_menu;
274 menu_key_state &= ~result_menu;
277 if (dev_id_out != NULL)
278 *dev_id_out = dev_id;
279 if (is_down_out != NULL)
280 *is_down_out = is_down;
284 /* same as above, only return bitfield of PBTN_* */
285 int in_menu_wait_any(int timeout_ms)
287 int keys_old = menu_key_state;
291 int code, is_down = 0, dev_id = 0;
293 code = in_update_keycode(&dev_id, &is_down, timeout_ms);
294 code = DRV(in_devices[dev_id].drv_id).menu_translate(code);
300 if (keys_old != menu_key_state)
304 return menu_key_state;
307 /* wait for menu input, do autorepeat */
308 int in_menu_wait(int interesting)
310 static int inp_prev = 0;
311 static int repeats = 0;
312 int ret, release = 0, wait = 666;
317 ret = in_menu_wait_any(wait);
321 while (!(ret & interesting)) {
322 ret = in_menu_wait_any(-1);
326 if (release || ret != inp_prev)
331 /* we don't need diagonals in menus */
332 if ((ret & PBTN_UP) && (ret & PBTN_LEFT)) ret &= ~PBTN_LEFT;
333 if ((ret & PBTN_UP) && (ret & PBTN_RIGHT)) ret &= ~PBTN_RIGHT;
334 if ((ret & PBTN_DOWN) && (ret & PBTN_LEFT)) ret &= ~PBTN_LEFT;
335 if ((ret & PBTN_DOWN) && (ret & PBTN_RIGHT)) ret &= ~PBTN_RIGHT;
340 const int *in_get_dev_binds(int dev_id)
342 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
345 return in_devices[dev_id].binds;
348 const int *in_get_dev_def_binds(int dev_id)
352 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
355 count = in_bind_count(in_devices[dev_id].drv_id);
356 return in_devices[dev_id].binds + count;
359 int in_get_dev_bind_count(int dev_id)
361 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
364 return in_bind_count(in_devices[dev_id].drv_id);
367 const char *in_get_dev_name(int dev_id, int must_be_active)
369 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
372 if (must_be_active && !in_devices[dev_id].probed)
374 return in_devices[dev_id].name;
377 /* never returns NULL */
378 const char *in_get_key_name(int dev_id, int keycode)
380 static char xname[16];
383 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
386 name = DRV(in_devices[dev_id].drv_id).get_key_name(keycode);
390 /* assume scancode */
391 if ((keycode >= '0' && keycode <= '9') || (keycode >= 'a' && keycode <= 'z')
392 || (keycode >= 'A' && keycode <= 'Z'))
393 sprintf(xname, "%c", keycode);
395 sprintf(xname, "\\x%02X", keycode);
399 int in_bind_key(int dev_id, int keycode, int mask, int force_unbind)
404 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
406 dev = &in_devices[dev_id];
408 if (dev->binds == NULL) {
411 dev->binds = in_alloc_binds(dev->drv_id);
412 if (dev->binds == NULL)
416 count = in_bind_count(dev->drv_id);
417 if (keycode < 0 || keycode >= count)
421 dev->binds[keycode] &= ~mask;
423 dev->binds[keycode] ^= mask;
425 ret = DRV(dev->drv_id).clean_binds(dev->drv_data, dev->binds);
434 /* returns device id, or -1 on error */
435 int in_config_parse_dev(const char *name)
439 for (i = 0; i < IN_DRVID_COUNT; i++) {
440 int len = strlen(in_drivers[i].prefix);
441 if (strncmp(name, in_drivers[i].prefix, len) == 0) {
448 printf("input: missing driver for %s\n", name);
452 for (i = 0; i < in_dev_count; i++)
454 if (in_devices[i].name == NULL)
456 if (strcmp(in_devices[i].name, name) == 0)
460 if (i >= IN_MAX_DEVS)
462 /* try to find unused device */
463 for (i = 0; i < IN_MAX_DEVS; i++)
464 if (in_devices[i].name == NULL) break;
465 if (i >= IN_MAX_DEVS) {
466 printf("input: too many devices, can't add %s\n", name);
471 memset(&in_devices[i], 0, sizeof(in_devices[i]));
473 in_devices[i].name = strdup(name);
474 if (in_devices[i].name == NULL)
477 if (i + 1 > in_dev_count)
478 in_dev_count = i + 1;
479 in_devices[i].drv_id = drv_id;
485 * To reduce size of game specific configs, default binds are not saved.
486 * So we mark default binds in in_config_start(), override them in in_config_bind_key(),
487 * and restore whatever default binds are left in in_config_end().
489 void in_config_start(void)
493 /* mark all default binds, so they get overwritten by func below */
494 for (i = 0; i < IN_MAX_DEVS; i++) {
495 int n, count, *binds, *def_binds;
497 binds = in_devices[i].binds;
501 count = in_bind_count(in_devices[i].drv_id);
502 def_binds = binds + count;
504 for (n = 0; n < count; n++)
505 if (binds[n] == def_binds[n])
510 int in_config_bind_key(int dev_id, const char *key, int binds)
515 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
517 dev = &in_devices[dev_id];
519 count = in_bind_count(dev->drv_id);
521 /* maybe a raw code? */
522 if (key[0] == '\\' && key[1] == 'x') {
524 kc = (int)strtoul(key + 2, &p, 16);
525 if (p == NULL || *p != 0)
529 /* device specific key name */
530 if (dev->binds == NULL) {
531 dev->binds = in_alloc_binds(dev->drv_id);
532 if (dev->binds == NULL)
537 kc = DRV(dev->drv_id).get_key_code(key);
538 if (kc < 0 && strlen(key) == 1) {
539 /* assume scancode */
544 if (kc < 0 || kc >= count) {
545 printf("input: bad key: %s\n", key);
549 if (dev->binds[kc] == -1)
551 dev->binds[kc] |= binds;
556 void in_config_end(void)
560 for (i = 0; i < IN_MAX_DEVS; i++) {
561 int n, ret, count, *binds, *def_binds;
562 in_dev_t *dev = &in_devices[i];
564 if (dev->binds == NULL)
567 count = in_bind_count(dev->drv_id);
569 def_binds = binds + count;
571 for (n = 0; n < count; n++)
573 binds[n] = def_binds[n];
575 if (dev->drv_data == NULL)
578 ret = DRV(dev->drv_id).clean_binds(dev->drv_data, binds);
580 /* no useable binds */
587 void in_debug_dump(void)
591 printf("# drv probed binds name\n");
592 for (i = 0; i < IN_MAX_DEVS; i++) {
593 in_dev_t *d = &in_devices[i];
594 if (!d->probed && d->name == NULL && d->binds == NULL)
596 printf("%d %3d %6c %5c %s\n", i, d->drv_id, d->probed ? 'y' : 'n',
597 d->binds ? 'y' : 'n', d->name);
601 /* handlers for unknown/not_preset drivers */
603 static void in_def_probe(void) {}
604 static void in_def_free(void *drv_data) {}
605 static int in_def_get_bind_count(void) { return 0; }
606 static void in_def_get_def_binds(int *binds) {}
607 static int in_def_clean_binds(void *drv_data, int *binds) { return 0; }
608 static void in_def_set_blocking(void *data, int y) {}
609 static int in_def_update_keycode(void *drv_data, int *is_down) { return 0; }
610 static int in_def_menu_translate(int keycode) { return keycode; }
611 static int in_def_get_key_code(const char *key_name) { return 0; }
612 static const char *in_def_get_key_name(int keycode) { return NULL; }
618 memset(in_drivers, 0, sizeof(in_drivers));
619 memset(in_devices, 0, sizeof(in_devices));
622 for (i = 0; i < IN_DRVID_COUNT; i++) {
623 in_drivers[i].prefix = "none:";
624 in_drivers[i].probe = in_def_probe;
625 in_drivers[i].free = in_def_free;
626 in_drivers[i].get_bind_count = in_def_get_bind_count;
627 in_drivers[i].get_def_binds = in_def_get_def_binds;
628 in_drivers[i].clean_binds = in_def_clean_binds;
629 in_drivers[i].set_blocking = in_def_set_blocking;
630 in_drivers[i].update_keycode = in_def_update_keycode;
631 in_drivers[i].menu_translate = in_def_menu_translate;
632 in_drivers[i].get_key_code = in_def_get_key_code;
633 in_drivers[i].get_key_name = in_def_get_key_name;
637 in_evdev_init(&in_drivers[IN_DRVID_EVDEV]);
654 ret = in_update_keycode(&dev, &down);
655 printf("#%i: %i %i (%s)\n", dev, down, ret, in_get_key_name(dev, ret));
659 ret = in_menu_wait_any();
660 printf("%08x\n", ret);