fix events loss
[picodrive.git] / platform / common / input.c
... / ...
CommitLineData
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4
5#include "input.h"
6#include "../linux/in_evdev.h"
7
8typedef struct
9{
10 int drv_id;
11 void *drv_data;
12 char *name;
13 int *binds;
14 int probed:1;
15} in_dev_t;
16
17static in_drv_t in_drivers[IN_DRVID_COUNT];
18static in_dev_t in_devices[IN_MAX_DEVS];
19static int in_dev_count = 0;
20
21#define DRV(id) in_drivers[(unsigned)(id) < IN_DRVID_COUNT ? (id) : 0]
22
23static int in_bind_count(int drv_id)
24{
25 int count = DRV(drv_id).get_bind_count();
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{
34 int count, *binds;
35
36 count = in_bind_count(drv_id);
37 if (count <= 0)
38 return NULL;
39
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;
48}
49
50static void in_free(in_dev_t *dev)
51{
52 if (dev->probed)
53 DRV(dev->drv_id).free(dev->drv_data);
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{
65 int i, ret, dupe_count = 0, *binds;
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;
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 }
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
136 for (i = 1; i < IN_DRVID_COUNT; i++)
137 in_drivers[i].probe();
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
152int in_update(void)
153{
154 int i, result = 0;
155
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) {
160#ifdef IN_EVDEV
161 case IN_DRVID_EVDEV:
162 result |= in_evdev_update(dev->drv_data, dev->binds);
163 break;
164#endif
165 }
166 }
167 }
168
169 return result;
170}
171
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
185static int menu_key_state = 0;
186
187void in_set_blocking(int is_blocking)
188{
189 int i;
190
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);
194 }
195
196 menu_key_state = 0;
197 /* flush events */
198 in_update_keycode(NULL, NULL, 1);
199}
200
201/*
202 * update with wait for a press, return keycode
203 * only can use 1 drv here..
204 */
205int in_update_keycode(int *dev_id_out, int *is_down_out, int timeout_ms)
206{
207 int result = 0, dev_id, is_down, result_menu;
208#ifdef IN_EVDEV
209 void **data;
210 int i, id = 0, count = 0;
211
212 data = in_collect_drvdata(IN_DRVID_EVDEV, &count);
213 if (count == 0) {
214 /* don't deadlock, fail */
215 printf("input: failed to find devices to read\n");
216 exit(1);
217 }
218
219 result = in_evdev_update_keycode(data, count, &id, &is_down, timeout_ms);
220
221 for (i = id; i < in_dev_count; i++) {
222 if (in_devices[i].drv_data == data[id]) {
223 dev_id = i;
224 break;
225 }
226 }
227#else
228#error no menu read handlers
229#endif
230
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;
245 return result;
246}
247
248/*
249 * same as above, only return bitfield of BTN_*
250 */
251int in_update_menu(int timeout_ms)
252{
253 int keys_old = menu_key_state;
254
255 while (1)
256 {
257 int code, is_down = 0, dev_id = 0;
258
259 code = in_update_keycode(&dev_id, &is_down, timeout_ms);
260 code = DRV(in_devices[dev_id].drv_id).menu_translate(code);
261
262 if (timeout_ms != 0)
263 break;
264 if (code == 0)
265 continue;
266 if (keys_old != menu_key_state)
267 break;
268 }
269
270 return menu_key_state;
271}
272
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
308/* never returns NULL */
309const char *in_get_key_name(int dev_id, int keycode)
310{
311 static char xname[16];
312 const char *name;
313
314 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
315 return "Unkn0";
316
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;
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
423 binds = in_devices[i].binds;
424 if (binds == NULL)
425 continue;
426
427 count = in_bind_count(in_devices[i].drv_id);
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{
438 int count, kc;
439 in_dev_t *dev;
440
441 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
442 return -1;
443 dev = &in_devices[dev_id];
444
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 }
468 }
469
470 if (kc < 0 || kc >= count) {
471 printf("input: bad key: %s\n", key);
472 return -1;
473 }
474
475 if (dev->binds[kc] == -1)
476 dev->binds[kc] = 0;
477 dev->binds[kc] |= binds;
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;
488 in_dev_t *dev = &in_devices[i];
489
490 if (dev->binds == NULL)
491 continue;
492
493 count = in_bind_count(dev->drv_id);
494 binds = dev->binds;
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
501 if (dev->drv_data == NULL)
502 continue;
503
504 ret = DRV(dev->drv_id).clean_binds(dev->drv_data, binds);
505 if (ret == 0) {
506 /* no useable binds */
507 free(dev->binds);
508 dev->binds = NULL;
509 }
510 }
511}
512
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
539void in_init(void)
540{
541 int i;
542
543 memset(in_drivers, 0, sizeof(in_drivers));
544 memset(in_devices, 0, sizeof(in_devices));
545 in_dev_count = 0;
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
563}
564
565#if 0
566int main(void)
567{
568 int ret;
569
570 in_init();
571 in_probe();
572
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
582 while (1) {
583 ret = in_update_menu();
584 printf("%08x\n", ret);
585 }
586#endif
587
588 return 0;
589}
590#endif