fix events loss
[libpicofe.git] / common / input.c
CommitLineData
2258f158 1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4
5#include "input.h"
34581c95 6#include "../linux/in_evdev.h"
2258f158 7
8typedef struct
9{
10 int drv_id;
11 void *drv_data;
2258f158 12 char *name;
906bdc9f 13 int *binds;
2258f158 14 int probed:1;
2258f158 15} in_dev_t;
16
da767bd5 17static in_drv_t in_drivers[IN_DRVID_COUNT];
2258f158 18static in_dev_t in_devices[IN_MAX_DEVS];
19static int in_dev_count = 0;
20
da767bd5 21#define DRV(id) in_drivers[(unsigned)(id) < IN_DRVID_COUNT ? (id) : 0]
22
2258f158 23static int in_bind_count(int drv_id)
24{
da767bd5 25 int count = DRV(drv_id).get_bind_count();
2258f158 26 if (count <= 0)
27 printf("input: failed to get bind count for drv %d\n", drv_id);
28
29 return count;
30}
31
32static int *in_alloc_binds(int drv_id)
33{
da767bd5 34 int count, *binds;
2258f158 35
36 count = in_bind_count(drv_id);
da767bd5 37 if (count <= 0)
2258f158 38 return NULL;
2258f158 39
da767bd5 40 binds = calloc(count * 2, sizeof(binds[0]));
41 if (binds == NULL)
42 return NULL;
43
44 DRV(drv_id).get_def_binds(binds + count);
45 memcpy(binds, binds + count, count * sizeof(binds[0]));
46
47 return binds;
2258f158 48}
49
50static void in_free(in_dev_t *dev)
51{
da767bd5 52 if (dev->probed)
53 DRV(dev->drv_id).free(dev->drv_data);
2258f158 54 dev->probed = 0;
55 dev->drv_data = NULL;
56 free(dev->name);
57 dev->name = NULL;
58 free(dev->binds);
59 dev->binds = NULL;
60}
61
62/* to be called by drivers */
63void in_register(const char *nname, int drv_id, void *drv_data)
64{
da767bd5 65 int i, ret, dupe_count = 0, *binds;
2258f158 66 char name[256], *name_end, *tmp;
67
68 strncpy(name, nname, sizeof(name));
69 name[sizeof(name)-12] = 0;
70 name_end = name + strlen(name);
71
72 for (i = 0; i < in_dev_count; i++)
73 {
74 if (in_devices[i].name == NULL)
75 continue;
76 if (strcmp(in_devices[i].name, name) == 0)
77 {
78 if (in_devices[i].probed) {
79 dupe_count++;
80 sprintf(name_end, " [%d]", dupe_count);
81 continue;
82 }
83 goto update;
84 }
85 }
86
87 if (i >= IN_MAX_DEVS)
88 {
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);
94 return;
95 }
96 in_free(&in_devices[i]);
97 }
98
99 tmp = strdup(name);
100 if (tmp == NULL)
101 return;
102
103 binds = in_alloc_binds(drv_id);
104 if (binds == NULL) {
105 free(tmp);
106 return;
107 }
108
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;
113
114 printf("input: new device #%d \"%s\"\n", i, name);
115update:
116 in_devices[i].probed = 1;
117 in_devices[i].drv_id = drv_id;
118 in_devices[i].drv_data = drv_data;
da767bd5 119
120 if (in_devices[i].binds != NULL) {
121 ret = DRV(drv_id).clean_binds(drv_data, in_devices[i].binds);
122 if (ret == 0) {
123 /* no useable binds */
124 free(in_devices[i].binds);
125 in_devices[i].binds = NULL;
126 }
127 }
2258f158 128}
129
130void in_probe(void)
131{
132 int i;
133 for (i = 0; i < in_dev_count; i++)
134 in_devices[i].probed = 0;
135
da767bd5 136 for (i = 1; i < IN_DRVID_COUNT; i++)
137 in_drivers[i].probe();
2258f158 138
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) {
142 in_dev_count--;
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]));
147 }
148 }
149 }
150}
151
2258f158 152int in_update(void)
153{
154 int i, result = 0;
155
156 for (i = 0; i < in_dev_count; i++) {
906bdc9f 157 in_dev_t *dev = &in_devices[i];
906bdc9f 158 if (dev->probed && dev->binds != NULL) {
159 switch (dev->drv_id) {
da767bd5 160#ifdef IN_EVDEV
595491c4 161 case IN_DRVID_EVDEV:
86883f7f 162 result |= in_evdev_update(dev->drv_data, dev->binds);
595491c4 163 break;
da767bd5 164#endif
595491c4 165 }
2258f158 166 }
167 }
168
169 return result;
170}
171
595491c4 172static void **in_collect_drvdata(int drv_id, int *count)
173{
174 static void *data[IN_MAX_DEVS];
175 int i;
176
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;
180 }
181
182 return data;
183}
184
906bdc9f 185static int menu_key_state = 0;
186
595491c4 187void in_set_blocking(int is_blocking)
188{
189 int i;
190
191 for (i = 0; i < in_dev_count; i++) {
da767bd5 192 if (in_devices[i].probed)
193 DRV(in_devices[i].drv_id).set_blocking(in_devices[i].drv_data, is_blocking);
595491c4 194 }
906bdc9f 195
196 menu_key_state = 0;
197 /* flush events */
198 in_update_keycode(NULL, NULL, 1);
595491c4 199}
200
34581c95 201/*
595491c4 202 * update with wait for a press, return keycode
34581c95 203 * only can use 1 drv here..
204 */
906bdc9f 205int in_update_keycode(int *dev_id_out, int *is_down_out, int timeout_ms)
34581c95 206{
906bdc9f 207 int result = 0, dev_id, is_down, result_menu;
34581c95 208#ifdef IN_EVDEV
595491c4 209 void **data;
210 int i, id = 0, count = 0;
34581c95 211
595491c4 212 data = in_collect_drvdata(IN_DRVID_EVDEV, &count);
34581c95 213 if (count == 0) {
214 /* don't deadlock, fail */
215 printf("input: failed to find devices to read\n");
216 exit(1);
217 }
218
906bdc9f 219 result = in_evdev_update_keycode(data, count, &id, &is_down, timeout_ms);
595491c4 220
906bdc9f 221 for (i = id; i < in_dev_count; i++) {
222 if (in_devices[i].drv_data == data[id]) {
223 dev_id = i;
224 break;
595491c4 225 }
226 }
34581c95 227#else
228#error no menu read handlers
229#endif
230
906bdc9f 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) {
235 if (is_down)
236 menu_key_state |= result_menu;
237 else
238 menu_key_state &= ~result_menu;
239 }
240
241 if (dev_id_out != NULL)
242 *dev_id_out = dev_id;
243 if (is_down_out != NULL)
244 *is_down_out = is_down;
34581c95 245 return result;
246}
247
595491c4 248/*
249 * same as above, only return bitfield of BTN_*
250 */
906bdc9f 251int in_update_menu(int timeout_ms)
595491c4 252{
906bdc9f 253 int keys_old = menu_key_state;
595491c4 254
255 while (1)
256 {
da767bd5 257 int code, is_down = 0, dev_id = 0;
595491c4 258
906bdc9f 259 code = in_update_keycode(&dev_id, &is_down, timeout_ms);
260 code = DRV(in_devices[dev_id].drv_id).menu_translate(code);
595491c4 261
906bdc9f 262 if (timeout_ms != 0)
263 break;
264 if (code == 0)
265 continue;
266 if (keys_old != menu_key_state)
595491c4 267 break;
268 }
269
906bdc9f 270 return menu_key_state;
595491c4 271}
272
da767bd5 273const int *in_get_dev_binds(int dev_id)
274{
275 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
276 return NULL;
277
278 return in_devices[dev_id].binds;
279}
280
281const int *in_get_dev_def_binds(int dev_id)
282{
283 int count;
284
285 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
286 return NULL;
287
288 count = in_bind_count(in_devices[dev_id].drv_id);
289 return in_devices[dev_id].binds + count;
290}
291
292int in_get_dev_bind_count(int dev_id)
293{
294 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
295 return 0;
296
297 return in_bind_count(in_devices[dev_id].drv_id);
298}
299
300const char *in_get_dev_name(int dev_id)
301{
302 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
303 return NULL;
304
305 return in_devices[dev_id].name;
306}
307
906bdc9f 308/* never returns NULL */
595491c4 309const char *in_get_key_name(int dev_id, int keycode)
310{
906bdc9f 311 static char xname[16];
312 const char *name;
313
595491c4 314 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
315 return "Unkn0";
da767bd5 316
906bdc9f 317 name = DRV(in_devices[dev_id].drv_id).get_key_name(keycode);
318 if (name != NULL)
319 return name;
320
321 /* assume scancode */
322 if ((keycode >= '0' && keycode <= '9') || (keycode >= 'a' && keycode <= 'z')
323 || (keycode >= 'A' && keycode <= 'Z'))
324 sprintf(xname, "%c", keycode);
325 else
326 sprintf(xname, "\\x%02X", keycode);
327 return xname;
328}
329
330int in_bind_key(int dev_id, int keycode, int mask, int force_unbind)
331{
332 int ret, count;
333 in_dev_t *dev;
334
335 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
336 return -1;
337 dev = &in_devices[dev_id];
338
339 if (dev->binds == NULL) {
340 if (force_unbind)
341 return 0;
342 dev->binds = in_alloc_binds(dev->drv_id);
343 if (dev->binds == NULL)
344 return -1;
345 }
346
347 count = in_bind_count(dev->drv_id);
348 if (keycode < 0 || keycode >= count)
349 return -1;
350
351 if (force_unbind)
352 dev->binds[keycode] &= ~mask;
353 else
354 dev->binds[keycode] ^= mask;
355
356 ret = DRV(dev->drv_id).clean_binds(dev->drv_data, dev->binds);
357 if (ret == 0) {
358 free(dev->binds);
359 dev->binds = NULL;
360 }
361
362 return 0;
da767bd5 363}
364
365/* returns device id, or -1 on error */
366int in_config_parse_dev(const char *name)
367{
368 int drv_id = -1, i;
369
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) {
373 drv_id = i;
374 break;
375 }
376 }
377
378 if (drv_id < 0) {
379 printf("input: missing driver for %s\n", name);
380 return -1;
381 }
382
383 for (i = 0; i < in_dev_count; i++)
384 {
385 if (in_devices[i].name == NULL)
386 continue;
387 if (strcmp(in_devices[i].name, name) == 0)
388 return i;
389 }
390
391 if (i >= IN_MAX_DEVS)
392 {
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);
398 return -1;
399 }
400 }
401
402 memset(&in_devices[i], 0, sizeof(in_devices[i]));
403
404 in_devices[i].name = strdup(name);
405 if (in_devices[i].name == NULL)
406 return -1;
407
408 if (i + 1 > in_dev_count)
409 in_dev_count = i + 1;
410 in_devices[i].drv_id = drv_id;
411
412 return i;
413}
414
415void in_config_start(void)
416{
417 int i;
418
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;
422
906bdc9f 423 binds = in_devices[i].binds;
424 if (binds == NULL)
da767bd5 425 continue;
426
427 count = in_bind_count(in_devices[i].drv_id);
da767bd5 428 def_binds = binds + count;
429
430 for (n = 0; n < count; n++)
431 if (binds[n] == def_binds[n])
432 binds[n] = -1;
433 }
434}
435
436int in_config_bind_key(int dev_id, const char *key, int binds)
437{
906bdc9f 438 int count, kc;
439 in_dev_t *dev;
da767bd5 440
441 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
442 return -1;
906bdc9f 443 dev = &in_devices[dev_id];
da767bd5 444
906bdc9f 445 count = in_bind_count(dev->drv_id);
446
447 /* maybe a raw code? */
448 if (key[0] == '\\' && key[1] == 'x') {
449 char *p = NULL;
450 kc = (int)strtoul(key + 2, &p, 16);
451 if (p == NULL || *p != 0)
452 kc = -1;
453 }
454 else {
455 /* device specific key name */
456 if (dev->binds == NULL) {
457 dev->binds = in_alloc_binds(dev->drv_id);
458 if (dev->binds == NULL)
459 return -1;
460 in_config_start();
461 }
462
463 kc = DRV(dev->drv_id).get_key_code(key);
464 if (kc < 0 && strlen(key) == 1) {
465 /* assume scancode */
466 kc = key[0];
467 }
da767bd5 468 }
469
906bdc9f 470 if (kc < 0 || kc >= count) {
da767bd5 471 printf("input: bad key: %s\n", key);
472 return -1;
595491c4 473 }
474
906bdc9f 475 if (dev->binds[kc] == -1)
476 dev->binds[kc] = 0;
477 dev->binds[kc] |= binds;
da767bd5 478
479 return 0;
480}
481
482void in_config_end(void)
483{
484 int i;
485
486 for (i = 0; i < IN_MAX_DEVS; i++) {
487 int n, ret, count, *binds, *def_binds;
906bdc9f 488 in_dev_t *dev = &in_devices[i];
da767bd5 489
906bdc9f 490 if (dev->binds == NULL)
da767bd5 491 continue;
492
906bdc9f 493 count = in_bind_count(dev->drv_id);
494 binds = dev->binds;
da767bd5 495 def_binds = binds + count;
496
497 for (n = 0; n < count; n++)
498 if (binds[n] == -1)
499 binds[n] = def_binds[n];
500
906bdc9f 501 if (dev->drv_data == NULL)
da767bd5 502 continue;
503
906bdc9f 504 ret = DRV(dev->drv_id).clean_binds(dev->drv_data, binds);
da767bd5 505 if (ret == 0) {
506 /* no useable binds */
906bdc9f 507 free(dev->binds);
508 dev->binds = NULL;
da767bd5 509 }
510 }
595491c4 511}
512
da767bd5 513void in_debug_dump(void)
514{
515 int i;
516
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)
521 continue;
522 printf("%d %3d %6c %5c %s\n", i, d->drv_id, d->probed ? 'y' : 'n',
523 d->binds ? 'y' : 'n', d->name);
524 }
525}
526
527/* handlers for unknown/not_preset drivers */
528
529static void in_def_probe(void) {}
530static void in_def_free(void *drv_data) {}
531static int in_def_get_bind_count(void) { return 0; }
532static void in_def_get_def_binds(int *binds) {}
533static int in_def_clean_binds(void *drv_data, int *binds) { return 0; }
534static void in_def_set_blocking(void *data, int y) {}
535static int in_def_menu_translate(int keycode) { return keycode; }
536static int in_def_get_key_code(const char *key_name) { return 0; }
537static const char *in_def_get_key_name(int keycode) { return NULL; }
538
2258f158 539void in_init(void)
540{
da767bd5 541 int i;
542
543 memset(in_drivers, 0, sizeof(in_drivers));
2258f158 544 memset(in_devices, 0, sizeof(in_devices));
545 in_dev_count = 0;
da767bd5 546
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;
558 }
559
560#ifdef IN_EVDEV
561 in_evdev_init(&in_drivers[IN_DRVID_EVDEV]);
562#endif
2258f158 563}
564
da767bd5 565#if 0
2258f158 566int main(void)
567{
34581c95 568 int ret;
569
2258f158 570 in_init();
571 in_probe();
572
595491c4 573 in_set_blocking(1);
574
575#if 1
576 while (1) {
577 int dev = 0, down;
578 ret = in_update_keycode(&dev, &down);
579 printf("#%i: %i %i (%s)\n", dev, down, ret, in_get_key_name(dev, ret));
580 }
581#else
2258f158 582 while (1) {
34581c95 583 ret = in_update_menu();
584 printf("%08x\n", ret);
2258f158 585 }
595491c4 586#endif
2258f158 587
588 return 0;
589}
da767bd5 590#endif