6 #include "../linux/in_evdev.h"
18 static in_drv_t in_drivers[IN_DRVID_COUNT];
19 static in_dev_t in_devices[IN_MAX_DEVS];
20 static int in_dev_count = 0;
22 #define DRV(id) in_drivers[(unsigned)(id) < IN_DRVID_COUNT ? (id) : 0]
24 static int in_bind_count(int drv_id)
26 int count = DRV(drv_id).get_bind_count();
28 printf("input: failed to get bind count for drv %d\n", drv_id);
33 static int *in_alloc_binds(int drv_id)
37 count = in_bind_count(drv_id);
41 binds = calloc(count * 2, sizeof(binds[0]));
45 DRV(drv_id).get_def_binds(binds + count);
46 memcpy(binds, binds + count, count * sizeof(binds[0]));
51 static void in_free(in_dev_t *dev)
54 DRV(dev->drv_id).free(dev->drv_data);
63 /* to be called by drivers */
64 void in_register(const char *nname, int drv_id, void *drv_data)
66 int i, ret, dupe_count = 0, *binds;
67 char name[256], *name_end, *tmp;
69 strncpy(name, nname, sizeof(name));
70 name[sizeof(name)-12] = 0;
71 name_end = name + strlen(name);
73 for (i = 0; i < in_dev_count; i++)
75 if (in_devices[i].name == NULL)
77 if (strcmp(in_devices[i].name, name) == 0)
79 if (in_devices[i].probed) {
81 sprintf(name_end, " [%d]", dupe_count);
90 /* try to find unused device */
91 for (i = 0; i < IN_MAX_DEVS; i++)
92 if (!in_devices[i].probed) break;
93 if (i >= IN_MAX_DEVS) {
94 printf("input: too many devices, can't add %s\n", name);
97 in_free(&in_devices[i]);
104 binds = in_alloc_binds(drv_id);
110 in_devices[i].name = tmp;
111 in_devices[i].binds = binds;
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].drv_id = drv_id;
119 in_devices[i].drv_data = drv_data;
121 if (in_devices[i].binds != NULL) {
122 ret = DRV(drv_id).clean_binds(drv_data, in_devices[i].binds);
124 /* no useable binds */
125 free(in_devices[i].binds);
126 in_devices[i].binds = NULL;
134 for (i = 0; i < in_dev_count; i++)
135 in_devices[i].probed = 0;
137 for (i = 1; i < IN_DRVID_COUNT; i++)
138 in_drivers[i].probe();
140 /* get rid of devs without binds and probes */
141 for (i = 0; i < in_dev_count; i++) {
142 if (!in_devices[i].probed && in_devices[i].binds == NULL) {
144 if (i < in_dev_count) {
145 free(in_devices[i].name);
146 memmove(&in_devices[i], &in_devices[i+1],
147 (in_dev_count - i) * sizeof(in_devices[0]));
157 for (i = 0; i < in_dev_count; i++) {
158 in_dev_t *dev = &in_devices[i];
160 if (dev->probed && dev->binds != NULL) {
161 switch (dev->drv_id) {
164 ret = in_evdev_update(dev->drv_data, dev->binds);
165 if (ret == -1) /* no change */
166 result |= dev->prev_result;
168 dev->prev_result = ret;
180 static void **in_collect_drvdata(int drv_id, int *count)
182 static void *data[IN_MAX_DEVS];
185 for (*count = i = 0; i < in_dev_count; i++) {
186 if (in_devices[i].drv_id == drv_id && in_devices[i].probed)
187 data[(*count)++] = in_devices[i].drv_data;
193 static int menu_key_state = 0;
195 void in_set_blocking(int is_blocking)
199 for (i = 0; i < in_dev_count; i++) {
200 if (in_devices[i].probed)
201 DRV(in_devices[i].drv_id).set_blocking(in_devices[i].drv_data, is_blocking);
202 in_devices[i].prev_result = 0;
207 in_update_keycode(NULL, NULL, 1);
211 * update with wait for a press, return keycode
212 * only can use 1 drv here..
214 int in_update_keycode(int *dev_id_out, int *is_down_out, int timeout_ms)
216 int result = 0, dev_id, is_down, result_menu;
219 int i, id = 0, count = 0;
221 data = in_collect_drvdata(IN_DRVID_EVDEV, &count);
223 /* don't deadlock, fail */
224 printf("input: failed to find devices to read\n");
228 result = in_evdev_update_keycode(data, count, &id, &is_down, timeout_ms);
230 for (i = id; i < in_dev_count; i++) {
231 if (in_devices[i].drv_data == data[id]) {
237 #error no menu read handlers
240 /* keep track of menu key state, to allow mixing
241 * in_update_keycode() and in_update_menu() calls */
242 result_menu = DRV(in_devices[dev_id].drv_id).menu_translate(result);
243 if (result_menu != 0) {
245 menu_key_state |= result_menu;
247 menu_key_state &= ~result_menu;
250 if (dev_id_out != NULL)
251 *dev_id_out = dev_id;
252 if (is_down_out != NULL)
253 *is_down_out = is_down;
258 * same as above, only return bitfield of BTN_*
260 int in_update_menu(int timeout_ms)
262 int keys_old = menu_key_state;
266 int code, is_down = 0, dev_id = 0;
268 code = in_update_keycode(&dev_id, &is_down, timeout_ms);
269 code = DRV(in_devices[dev_id].drv_id).menu_translate(code);
275 if (keys_old != menu_key_state)
279 return menu_key_state;
282 const int *in_get_dev_binds(int dev_id)
284 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
287 return in_devices[dev_id].binds;
290 const int *in_get_dev_def_binds(int dev_id)
294 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
297 count = in_bind_count(in_devices[dev_id].drv_id);
298 return in_devices[dev_id].binds + count;
301 int in_get_dev_bind_count(int dev_id)
303 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
306 return in_bind_count(in_devices[dev_id].drv_id);
309 const char *in_get_dev_name(int dev_id)
311 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
314 return in_devices[dev_id].name;
317 /* never returns NULL */
318 const char *in_get_key_name(int dev_id, int keycode)
320 static char xname[16];
323 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
326 name = DRV(in_devices[dev_id].drv_id).get_key_name(keycode);
330 /* assume scancode */
331 if ((keycode >= '0' && keycode <= '9') || (keycode >= 'a' && keycode <= 'z')
332 || (keycode >= 'A' && keycode <= 'Z'))
333 sprintf(xname, "%c", keycode);
335 sprintf(xname, "\\x%02X", keycode);
339 int in_bind_key(int dev_id, int keycode, int mask, int force_unbind)
344 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
346 dev = &in_devices[dev_id];
348 if (dev->binds == NULL) {
351 dev->binds = in_alloc_binds(dev->drv_id);
352 if (dev->binds == NULL)
356 count = in_bind_count(dev->drv_id);
357 if (keycode < 0 || keycode >= count)
361 dev->binds[keycode] &= ~mask;
363 dev->binds[keycode] ^= mask;
365 ret = DRV(dev->drv_id).clean_binds(dev->drv_data, dev->binds);
374 /* returns device id, or -1 on error */
375 int in_config_parse_dev(const char *name)
379 for (i = 0; i < IN_DRVID_COUNT; i++) {
380 int len = strlen(in_drivers[i].prefix);
381 if (strncmp(name, in_drivers[i].prefix, len) == 0) {
388 printf("input: missing driver for %s\n", name);
392 for (i = 0; i < in_dev_count; i++)
394 if (in_devices[i].name == NULL)
396 if (strcmp(in_devices[i].name, name) == 0)
400 if (i >= IN_MAX_DEVS)
402 /* try to find unused device */
403 for (i = 0; i < IN_MAX_DEVS; i++)
404 if (in_devices[i].name == NULL) break;
405 if (i >= IN_MAX_DEVS) {
406 printf("input: too many devices, can't add %s\n", name);
411 memset(&in_devices[i], 0, sizeof(in_devices[i]));
413 in_devices[i].name = strdup(name);
414 if (in_devices[i].name == NULL)
417 if (i + 1 > in_dev_count)
418 in_dev_count = i + 1;
419 in_devices[i].drv_id = drv_id;
424 void in_config_start(void)
428 /* mark all default binds, so they get overwritten by func below */
429 for (i = 0; i < IN_MAX_DEVS; i++) {
430 int n, count, *binds, *def_binds;
432 binds = in_devices[i].binds;
436 count = in_bind_count(in_devices[i].drv_id);
437 def_binds = binds + count;
439 for (n = 0; n < count; n++)
440 if (binds[n] == def_binds[n])
445 int in_config_bind_key(int dev_id, const char *key, int binds)
450 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
452 dev = &in_devices[dev_id];
454 count = in_bind_count(dev->drv_id);
456 /* maybe a raw code? */
457 if (key[0] == '\\' && key[1] == 'x') {
459 kc = (int)strtoul(key + 2, &p, 16);
460 if (p == NULL || *p != 0)
464 /* device specific key name */
465 if (dev->binds == NULL) {
466 dev->binds = in_alloc_binds(dev->drv_id);
467 if (dev->binds == NULL)
472 kc = DRV(dev->drv_id).get_key_code(key);
473 if (kc < 0 && strlen(key) == 1) {
474 /* assume scancode */
479 if (kc < 0 || kc >= count) {
480 printf("input: bad key: %s\n", key);
484 if (dev->binds[kc] == -1)
486 dev->binds[kc] |= binds;
491 void in_config_end(void)
495 for (i = 0; i < IN_MAX_DEVS; i++) {
496 int n, ret, count, *binds, *def_binds;
497 in_dev_t *dev = &in_devices[i];
499 if (dev->binds == NULL)
502 count = in_bind_count(dev->drv_id);
504 def_binds = binds + count;
506 for (n = 0; n < count; n++)
508 binds[n] = def_binds[n];
510 if (dev->drv_data == NULL)
513 ret = DRV(dev->drv_id).clean_binds(dev->drv_data, binds);
515 /* no useable binds */
522 void in_debug_dump(void)
526 printf("# drv probed binds name\n");
527 for (i = 0; i < IN_MAX_DEVS; i++) {
528 in_dev_t *d = &in_devices[i];
529 if (!d->probed && d->name == NULL && d->binds == NULL)
531 printf("%d %3d %6c %5c %s\n", i, d->drv_id, d->probed ? 'y' : 'n',
532 d->binds ? 'y' : 'n', d->name);
536 /* handlers for unknown/not_preset drivers */
538 static void in_def_probe(void) {}
539 static void in_def_free(void *drv_data) {}
540 static int in_def_get_bind_count(void) { return 0; }
541 static void in_def_get_def_binds(int *binds) {}
542 static int in_def_clean_binds(void *drv_data, int *binds) { return 0; }
543 static void in_def_set_blocking(void *data, int y) {}
544 static int in_def_menu_translate(int keycode) { return keycode; }
545 static int in_def_get_key_code(const char *key_name) { return 0; }
546 static const char *in_def_get_key_name(int keycode) { return NULL; }
552 memset(in_drivers, 0, sizeof(in_drivers));
553 memset(in_devices, 0, sizeof(in_devices));
556 for (i = 0; i < IN_DRVID_COUNT; i++) {
557 in_drivers[i].prefix = "none:";
558 in_drivers[i].probe = in_def_probe;
559 in_drivers[i].free = in_def_free;
560 in_drivers[i].get_bind_count = in_def_get_bind_count;
561 in_drivers[i].get_def_binds = in_def_get_def_binds;
562 in_drivers[i].clean_binds = in_def_clean_binds;
563 in_drivers[i].set_blocking = in_def_set_blocking;
564 in_drivers[i].menu_translate = in_def_menu_translate;
565 in_drivers[i].get_key_code = in_def_get_key_code;
566 in_drivers[i].get_key_name = in_def_get_key_name;
570 in_evdev_init(&in_drivers[IN_DRVID_EVDEV]);
587 ret = in_update_keycode(&dev, &down);
588 printf("#%i: %i %i (%s)\n", dev, down, ret, in_get_key_name(dev, ret));
592 ret = in_update_menu();
593 printf("%08x\n", ret);