6 #include "../linux/in_evdev.h"
17 static in_drv_t in_drivers[IN_DRVID_COUNT];
18 static in_dev_t in_devices[IN_MAX_DEVS];
19 static int in_dev_count = 0;
21 #define DRV(id) in_drivers[(unsigned)(id) < IN_DRVID_COUNT ? (id) : 0]
23 static int in_bind_count(int drv_id)
25 int count = DRV(drv_id).get_bind_count();
27 printf("input: failed to get bind count for drv %d\n", drv_id);
32 static int *in_alloc_binds(int drv_id)
36 count = in_bind_count(drv_id);
40 binds = calloc(count * 2, sizeof(binds[0]));
44 DRV(drv_id).get_def_binds(binds + count);
45 memcpy(binds, binds + count, count * sizeof(binds[0]));
50 static void in_free(in_dev_t *dev)
53 DRV(dev->drv_id).free(dev->drv_data);
62 /* to be called by drivers */
63 void in_register(const char *nname, int drv_id, void *drv_data)
65 int i, ret, dupe_count = 0, *binds;
66 char name[256], *name_end, *tmp;
68 strncpy(name, nname, sizeof(name));
69 name[sizeof(name)-12] = 0;
70 name_end = name + strlen(name);
72 for (i = 0; i < in_dev_count; i++)
74 if (in_devices[i].name == NULL)
76 if (strcmp(in_devices[i].name, name) == 0)
78 if (in_devices[i].probed) {
80 sprintf(name_end, " [%d]", dupe_count);
89 /* try to find unused device */
90 for (i = 0; i < IN_MAX_DEVS; i++)
91 if (!in_devices[i].probed) break;
92 if (i >= IN_MAX_DEVS) {
93 printf("input: too many devices, can't add %s\n", name);
96 in_free(&in_devices[i]);
103 binds = in_alloc_binds(drv_id);
109 in_devices[i].name = tmp;
110 in_devices[i].binds = binds;
111 if (i + 1 > in_dev_count)
112 in_dev_count = i + 1;
114 printf("input: new device #%d \"%s\"\n", i, name);
116 in_devices[i].probed = 1;
117 in_devices[i].drv_id = drv_id;
118 in_devices[i].drv_data = drv_data;
120 if (in_devices[i].binds != NULL) {
121 ret = DRV(drv_id).clean_binds(drv_data, in_devices[i].binds);
123 /* no useable binds */
124 free(in_devices[i].binds);
125 in_devices[i].binds = NULL;
133 for (i = 0; i < in_dev_count; i++)
134 in_devices[i].probed = 0;
136 for (i = 1; i < IN_DRVID_COUNT; i++)
137 in_drivers[i].probe();
139 /* get rid of devs without binds and probes */
140 for (i = 0; i < in_dev_count; i++) {
141 if (!in_devices[i].probed && in_devices[i].binds == NULL) {
143 if (i < in_dev_count) {
144 free(in_devices[i].name);
145 memmove(&in_devices[i], &in_devices[i+1],
146 (in_dev_count - i) * sizeof(in_devices[0]));
156 for (i = 0; i < in_dev_count; i++) {
157 in_dev_t *dev = &in_devices[i];
158 if (dev->probed && dev->binds != NULL) {
159 switch (dev->drv_id) {
162 result |= in_evdev_update(dev->drv_data, dev->binds);
172 static void **in_collect_drvdata(int drv_id, int *count)
174 static void *data[IN_MAX_DEVS];
177 for (*count = i = 0; i < in_dev_count; i++) {
178 if (in_devices[i].drv_id == drv_id && in_devices[i].probed)
179 data[(*count)++] = in_devices[i].drv_data;
185 static int menu_key_state = 0;
187 void in_set_blocking(int is_blocking)
191 for (i = 0; i < in_dev_count; i++) {
192 if (in_devices[i].probed)
193 DRV(in_devices[i].drv_id).set_blocking(in_devices[i].drv_data, is_blocking);
198 in_update_keycode(NULL, NULL, 1);
202 * update with wait for a press, return keycode
203 * only can use 1 drv here..
205 int in_update_keycode(int *dev_id_out, int *is_down_out, int timeout_ms)
207 int result = 0, dev_id, is_down, result_menu;
210 int i, id = 0, count = 0;
212 data = in_collect_drvdata(IN_DRVID_EVDEV, &count);
214 /* don't deadlock, fail */
215 printf("input: failed to find devices to read\n");
219 result = in_evdev_update_keycode(data, count, &id, &is_down, timeout_ms);
221 for (i = id; i < in_dev_count; i++) {
222 if (in_devices[i].drv_data == data[id]) {
228 #error no menu read handlers
231 /* keep track of menu key state, to allow mixing
232 * in_update_keycode() and in_update_menu() calls */
233 result_menu = DRV(in_devices[dev_id].drv_id).menu_translate(result);
234 if (result_menu != 0) {
236 menu_key_state |= result_menu;
238 menu_key_state &= ~result_menu;
241 if (dev_id_out != NULL)
242 *dev_id_out = dev_id;
243 if (is_down_out != NULL)
244 *is_down_out = is_down;
249 * same as above, only return bitfield of BTN_*
251 int in_update_menu(int timeout_ms)
253 int keys_old = menu_key_state;
257 int code, is_down = 0, dev_id = 0;
259 code = in_update_keycode(&dev_id, &is_down, timeout_ms);
260 code = DRV(in_devices[dev_id].drv_id).menu_translate(code);
266 if (keys_old != menu_key_state)
270 return menu_key_state;
273 const int *in_get_dev_binds(int dev_id)
275 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
278 return in_devices[dev_id].binds;
281 const int *in_get_dev_def_binds(int dev_id)
285 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
288 count = in_bind_count(in_devices[dev_id].drv_id);
289 return in_devices[dev_id].binds + count;
292 int in_get_dev_bind_count(int dev_id)
294 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
297 return in_bind_count(in_devices[dev_id].drv_id);
300 const char *in_get_dev_name(int dev_id)
302 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
305 return in_devices[dev_id].name;
308 /* never returns NULL */
309 const char *in_get_key_name(int dev_id, int keycode)
311 static char xname[16];
314 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
317 name = DRV(in_devices[dev_id].drv_id).get_key_name(keycode);
321 /* assume scancode */
322 if ((keycode >= '0' && keycode <= '9') || (keycode >= 'a' && keycode <= 'z')
323 || (keycode >= 'A' && keycode <= 'Z'))
324 sprintf(xname, "%c", keycode);
326 sprintf(xname, "\\x%02X", keycode);
330 int in_bind_key(int dev_id, int keycode, int mask, int force_unbind)
335 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
337 dev = &in_devices[dev_id];
339 if (dev->binds == NULL) {
342 dev->binds = in_alloc_binds(dev->drv_id);
343 if (dev->binds == NULL)
347 count = in_bind_count(dev->drv_id);
348 if (keycode < 0 || keycode >= count)
352 dev->binds[keycode] &= ~mask;
354 dev->binds[keycode] ^= mask;
356 ret = DRV(dev->drv_id).clean_binds(dev->drv_data, dev->binds);
365 /* returns device id, or -1 on error */
366 int in_config_parse_dev(const char *name)
370 for (i = 0; i < IN_DRVID_COUNT; i++) {
371 int len = strlen(in_drivers[i].prefix);
372 if (strncmp(name, in_drivers[i].prefix, len) == 0) {
379 printf("input: missing driver for %s\n", name);
383 for (i = 0; i < in_dev_count; i++)
385 if (in_devices[i].name == NULL)
387 if (strcmp(in_devices[i].name, name) == 0)
391 if (i >= IN_MAX_DEVS)
393 /* try to find unused device */
394 for (i = 0; i < IN_MAX_DEVS; i++)
395 if (in_devices[i].name == NULL) break;
396 if (i >= IN_MAX_DEVS) {
397 printf("input: too many devices, can't add %s\n", name);
402 memset(&in_devices[i], 0, sizeof(in_devices[i]));
404 in_devices[i].name = strdup(name);
405 if (in_devices[i].name == NULL)
408 if (i + 1 > in_dev_count)
409 in_dev_count = i + 1;
410 in_devices[i].drv_id = drv_id;
415 void in_config_start(void)
419 /* mark all default binds, so they get overwritten by func below */
420 for (i = 0; i < IN_MAX_DEVS; i++) {
421 int n, count, *binds, *def_binds;
423 binds = in_devices[i].binds;
427 count = in_bind_count(in_devices[i].drv_id);
428 def_binds = binds + count;
430 for (n = 0; n < count; n++)
431 if (binds[n] == def_binds[n])
436 int in_config_bind_key(int dev_id, const char *key, int binds)
441 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
443 dev = &in_devices[dev_id];
445 count = in_bind_count(dev->drv_id);
447 /* maybe a raw code? */
448 if (key[0] == '\\' && key[1] == 'x') {
450 kc = (int)strtoul(key + 2, &p, 16);
451 if (p == NULL || *p != 0)
455 /* device specific key name */
456 if (dev->binds == NULL) {
457 dev->binds = in_alloc_binds(dev->drv_id);
458 if (dev->binds == NULL)
463 kc = DRV(dev->drv_id).get_key_code(key);
464 if (kc < 0 && strlen(key) == 1) {
465 /* assume scancode */
470 if (kc < 0 || kc >= count) {
471 printf("input: bad key: %s\n", key);
475 if (dev->binds[kc] == -1)
477 dev->binds[kc] |= binds;
482 void in_config_end(void)
486 for (i = 0; i < IN_MAX_DEVS; i++) {
487 int n, ret, count, *binds, *def_binds;
488 in_dev_t *dev = &in_devices[i];
490 if (dev->binds == NULL)
493 count = in_bind_count(dev->drv_id);
495 def_binds = binds + count;
497 for (n = 0; n < count; n++)
499 binds[n] = def_binds[n];
501 if (dev->drv_data == NULL)
504 ret = DRV(dev->drv_id).clean_binds(dev->drv_data, binds);
506 /* no useable binds */
513 void in_debug_dump(void)
517 printf("# drv probed binds name\n");
518 for (i = 0; i < IN_MAX_DEVS; i++) {
519 in_dev_t *d = &in_devices[i];
520 if (!d->probed && d->name == NULL && d->binds == NULL)
522 printf("%d %3d %6c %5c %s\n", i, d->drv_id, d->probed ? 'y' : 'n',
523 d->binds ? 'y' : 'n', d->name);
527 /* handlers for unknown/not_preset drivers */
529 static void in_def_probe(void) {}
530 static void in_def_free(void *drv_data) {}
531 static int in_def_get_bind_count(void) { return 0; }
532 static void in_def_get_def_binds(int *binds) {}
533 static int in_def_clean_binds(void *drv_data, int *binds) { return 0; }
534 static void in_def_set_blocking(void *data, int y) {}
535 static int in_def_menu_translate(int keycode) { return keycode; }
536 static int in_def_get_key_code(const char *key_name) { return 0; }
537 static const char *in_def_get_key_name(int keycode) { return NULL; }
543 memset(in_drivers, 0, sizeof(in_drivers));
544 memset(in_devices, 0, sizeof(in_devices));
547 for (i = 0; i < IN_DRVID_COUNT; i++) {
548 in_drivers[i].prefix = "none:";
549 in_drivers[i].probe = in_def_probe;
550 in_drivers[i].free = in_def_free;
551 in_drivers[i].get_bind_count = in_def_get_bind_count;
552 in_drivers[i].get_def_binds = in_def_get_def_binds;
553 in_drivers[i].clean_binds = in_def_clean_binds;
554 in_drivers[i].set_blocking = in_def_set_blocking;
555 in_drivers[i].menu_translate = in_def_menu_translate;
556 in_drivers[i].get_key_code = in_def_get_key_code;
557 in_drivers[i].get_key_name = in_def_get_key_name;
561 in_evdev_init(&in_drivers[IN_DRVID_EVDEV]);
578 ret = in_update_keycode(&dev, &down);
579 printf("#%i: %i %i (%s)\n", dev, down, ret, in_get_key_name(dev, ret));
583 ret = in_update_menu();
584 printf("%08x\n", ret);