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