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 if (in_devices[i].probed && in_devices[i].binds != NULL) {
158 switch (in_devices[i].drv_id) {
161 result |= in_evdev_update(in_devices[i].drv_data, in_devices[i].binds);
171 static void **in_collect_drvdata(int drv_id, int *count)
173 static void *data[IN_MAX_DEVS];
176 for (*count = i = 0; i < in_dev_count; i++) {
177 if (in_devices[i].drv_id == drv_id && in_devices[i].probed)
178 data[(*count)++] = in_devices[i].drv_data;
184 void in_set_blocking(int is_blocking)
188 for (i = 0; i < in_dev_count; i++) {
189 if (in_devices[i].probed)
190 DRV(in_devices[i].drv_id).set_blocking(in_devices[i].drv_data, is_blocking);
195 * update with wait for a press, return keycode
196 * only can use 1 drv here..
198 int in_update_keycode(int *dev_id, int *is_down)
203 int i, id = 0, count = 0;
205 data = in_collect_drvdata(IN_DRVID_EVDEV, &count);
207 /* don't deadlock, fail */
208 printf("input: failed to find devices to read\n");
212 result = in_evdev_update_keycode(data, count, &id, is_down);
214 if (dev_id != NULL) {
215 for (i = id; i < in_dev_count; i++) {
216 if (in_devices[i].drv_data == data[id]) {
223 #error no menu read handlers
230 * same as above, only return bitfield of BTN_*
232 int in_update_menu(void)
234 static int keys_active = 0;
235 int keys_old = keys_active;
239 int code, is_down = 0, dev_id = 0;
240 code = in_update_keycode(&dev_id, &is_down);
241 code = DRV(in_devices[dev_id].drv_id).menu_translate(code);
242 if (code == 0) continue;
247 keys_active &= ~code;
249 if (keys_old != keys_active)
256 const int *in_get_dev_binds(int dev_id)
258 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
261 return in_devices[dev_id].binds;
264 const int *in_get_dev_def_binds(int dev_id)
268 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
271 count = in_bind_count(in_devices[dev_id].drv_id);
272 return in_devices[dev_id].binds + count;
275 int in_get_dev_bind_count(int dev_id)
277 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
280 return in_bind_count(in_devices[dev_id].drv_id);
283 const char *in_get_dev_name(int dev_id)
285 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
288 return in_devices[dev_id].name;
291 const char *in_get_key_name(int dev_id, int keycode)
293 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
296 return DRV(in_devices[dev_id].drv_id).get_key_name(keycode);
299 /* returns device id, or -1 on error */
300 int in_config_parse_dev(const char *name)
304 for (i = 0; i < IN_DRVID_COUNT; i++) {
305 int len = strlen(in_drivers[i].prefix);
306 if (strncmp(name, in_drivers[i].prefix, len) == 0) {
313 printf("input: missing driver for %s\n", name);
317 for (i = 0; i < in_dev_count; i++)
319 if (in_devices[i].name == NULL)
321 if (strcmp(in_devices[i].name, name) == 0)
325 if (i >= IN_MAX_DEVS)
327 /* try to find unused device */
328 for (i = 0; i < IN_MAX_DEVS; i++)
329 if (in_devices[i].name == NULL) break;
330 if (i >= IN_MAX_DEVS) {
331 printf("input: too many devices, can't add %s\n", name);
336 memset(&in_devices[i], 0, sizeof(in_devices[i]));
338 in_devices[i].name = strdup(name);
339 if (in_devices[i].name == NULL)
342 if (i + 1 > in_dev_count)
343 in_dev_count = i + 1;
344 in_devices[i].drv_id = drv_id;
349 void in_config_start(void)
353 /* mark all default binds, so they get overwritten by func below */
354 for (i = 0; i < IN_MAX_DEVS; i++) {
355 int n, count, *binds, *def_binds;
357 if (in_devices[i].binds == NULL)
360 count = in_bind_count(in_devices[i].drv_id);
361 binds = in_devices[i].binds;
362 def_binds = binds + count;
364 for (n = 0; n < count; n++)
365 if (binds[n] == def_binds[n])
370 int in_config_bind_key(int dev_id, const char *key, int binds)
374 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
377 if (in_devices[dev_id].binds == NULL) {
378 in_devices[dev_id].binds = in_alloc_binds(in_devices[dev_id].drv_id);
379 if (in_devices[dev_id].binds == NULL)
384 kc = DRV(in_devices[dev_id].drv_id).get_key_code(key);
386 printf("input: bad key: %s\n", key);
390 if (in_devices[dev_id].binds[kc] == -1)
391 in_devices[dev_id].binds[kc] = 0;
392 in_devices[dev_id].binds[kc] |= binds;
397 void in_config_end(void)
401 for (i = 0; i < IN_MAX_DEVS; i++) {
402 int n, ret, count, *binds, *def_binds;
404 if (in_devices[i].binds == NULL)
407 count = in_bind_count(in_devices[i].drv_id);
408 binds = in_devices[i].binds;
409 def_binds = binds + count;
411 for (n = 0; n < count; n++)
413 binds[n] = def_binds[n];
415 if (in_devices[i].drv_data == NULL)
418 ret = DRV(in_devices[i].drv_id).clean_binds(in_devices[i].drv_data, binds);
420 /* no useable binds */
422 in_devices[i].binds = NULL;
427 void in_debug_dump(void)
431 printf("# drv probed binds name\n");
432 for (i = 0; i < IN_MAX_DEVS; i++) {
433 in_dev_t *d = &in_devices[i];
434 if (!d->probed && d->name == NULL && d->binds == NULL)
436 printf("%d %3d %6c %5c %s\n", i, d->drv_id, d->probed ? 'y' : 'n',
437 d->binds ? 'y' : 'n', d->name);
441 /* handlers for unknown/not_preset drivers */
443 static void in_def_probe(void) {}
444 static void in_def_free(void *drv_data) {}
445 static int in_def_get_bind_count(void) { return 0; }
446 static void in_def_get_def_binds(int *binds) {}
447 static int in_def_clean_binds(void *drv_data, int *binds) { return 0; }
448 static void in_def_set_blocking(void *data, int y) {}
449 static int in_def_menu_translate(int keycode) { return keycode; }
450 static int in_def_get_key_code(const char *key_name) { return 0; }
451 static const char *in_def_get_key_name(int keycode) { return NULL; }
457 memset(in_drivers, 0, sizeof(in_drivers));
458 memset(in_devices, 0, sizeof(in_devices));
461 for (i = 0; i < IN_DRVID_COUNT; i++) {
462 in_drivers[i].prefix = "none:";
463 in_drivers[i].probe = in_def_probe;
464 in_drivers[i].free = in_def_free;
465 in_drivers[i].get_bind_count = in_def_get_bind_count;
466 in_drivers[i].get_def_binds = in_def_get_def_binds;
467 in_drivers[i].clean_binds = in_def_clean_binds;
468 in_drivers[i].set_blocking = in_def_set_blocking;
469 in_drivers[i].menu_translate = in_def_menu_translate;
470 in_drivers[i].get_key_code = in_def_get_key_code;
471 in_drivers[i].get_key_name = in_def_get_key_name;
475 in_evdev_init(&in_drivers[IN_DRVID_EVDEV]);
492 ret = in_update_keycode(&dev, &down);
493 printf("#%i: %i %i (%s)\n", dev, down, ret, in_get_key_name(dev, ret));
497 ret = in_update_menu();
498 printf("%08x\n", ret);