asyn-only dev support + in_gp2x driver
[libpicofe.git] / common / input.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include "common.h"
6 #include "input.h"
7 #include "../linux/in_evdev.h"
8 #include "../gp2x/in_gp2x.h"
9
10 typedef struct
11 {
12         int drv_id;
13         int drv_fd_hnd;
14         void *drv_data;
15         char *name;
16         int *binds;
17         int probed:1;
18 } in_dev_t;
19
20 static in_drv_t in_drivers[IN_DRVID_COUNT];
21 static in_dev_t in_devices[IN_MAX_DEVS];
22 static int in_dev_count = 0;
23 static int in_have_async_devs = 0;
24
25 #define DRV(id) in_drivers[(unsigned)(id) < IN_DRVID_COUNT ? (id) : 0]
26
27
28 static int in_bind_count(int drv_id)
29 {
30         int count = DRV(drv_id).get_bind_count();
31         if (count <= 0)
32                 printf("input: failed to get bind count for drv %d\n", drv_id);
33
34         return count;
35 }
36
37 static int *in_alloc_binds(int drv_id)
38 {
39         int count, *binds;
40
41         count = in_bind_count(drv_id);
42         if (count <= 0)
43                 return NULL;
44
45         binds = calloc(count * 2, sizeof(binds[0]));
46         if (binds == NULL)
47                 return NULL;
48
49         DRV(drv_id).get_def_binds(binds + count);
50         memcpy(binds, binds + count, count * sizeof(binds[0]));
51
52         return binds;
53 }
54
55 static void in_free(in_dev_t *dev)
56 {
57         if (dev->probed)
58                 DRV(dev->drv_id).free(dev->drv_data);
59         dev->probed = 0;
60         dev->drv_data = NULL;
61         free(dev->name);
62         dev->name = NULL;
63         free(dev->binds);
64         dev->binds = NULL;
65 }
66
67 /* to be called by drivers
68  * async devices must set drv_fd_hnd to -1 */
69 void in_register(const char *nname, int drv_id, int drv_fd_hnd, void *drv_data)
70 {
71         int i, ret, dupe_count = 0, *binds;
72         char name[256], *name_end, *tmp;
73
74         strncpy(name, nname, sizeof(name));
75         name[sizeof(name)-12] = 0;
76         name_end = name + strlen(name);
77
78         for (i = 0; i < in_dev_count; i++)
79         {
80                 if (in_devices[i].name == NULL)
81                         continue;
82                 if (strcmp(in_devices[i].name, name) == 0)
83                 {
84                         if (in_devices[i].probed) {
85                                 dupe_count++;
86                                 sprintf(name_end, " [%d]", dupe_count);
87                                 continue;
88                         }
89                         goto update;
90                 }
91         }
92
93         if (i >= IN_MAX_DEVS)
94         {
95                 /* try to find unused device */
96                 for (i = 0; i < IN_MAX_DEVS; i++)
97                         if (!in_devices[i].probed) break;
98                 if (i >= IN_MAX_DEVS) {
99                         printf("input: too many devices, can't add %s\n", name);
100                         return;
101                 }
102                 in_free(&in_devices[i]);
103         }
104
105         tmp = strdup(name);
106         if (tmp == NULL)
107                 return;
108
109         binds = in_alloc_binds(drv_id);
110         if (binds == NULL) {
111                 free(tmp);
112                 return;
113         }
114
115         in_devices[i].name = tmp;
116         in_devices[i].binds = binds;
117         if (i + 1 > in_dev_count)
118                 in_dev_count = i + 1;
119
120         printf("input: new device #%d \"%s\"\n", i, name);
121 update:
122         in_devices[i].probed = 1;
123         in_devices[i].drv_id = drv_id;
124         in_devices[i].drv_fd_hnd = drv_fd_hnd;
125         in_devices[i].drv_data = drv_data;
126
127         if (in_devices[i].binds != NULL) {
128                 ret = DRV(drv_id).clean_binds(drv_data, in_devices[i].binds);
129                 if (ret == 0) {
130                         /* no useable binds */
131                         free(in_devices[i].binds);
132                         in_devices[i].binds = NULL;
133                 }
134         }
135 }
136
137 void in_probe(void)
138 {
139         int i;
140
141         in_have_async_devs = 0;
142         for (i = 0; i < in_dev_count; i++)
143                 in_devices[i].probed = 0;
144
145         for (i = 1; i < IN_DRVID_COUNT; i++)
146                 in_drivers[i].probe();
147
148         /* get rid of devs without binds and probes */
149         for (i = 0; i < in_dev_count; i++) {
150                 if (!in_devices[i].probed && in_devices[i].binds == NULL) {
151                         in_dev_count--;
152                         if (i < in_dev_count) {
153                                 free(in_devices[i].name);
154                                 memmove(&in_devices[i], &in_devices[i+1],
155                                         (in_dev_count - i) * sizeof(in_devices[0]));
156                         }
157
158                         continue;
159                 }
160
161                 if (in_devices[i].probed && in_devices[i].drv_fd_hnd == -1)
162                         in_have_async_devs = 1;
163         }
164
165         if (in_have_async_devs)
166                 printf("input: async-only devices detected..\n");
167 }
168
169 /* async update */
170 int in_update(void)
171 {
172         int i, result = 0;
173
174         for (i = 0; i < in_dev_count; i++) {
175                 in_dev_t *dev = &in_devices[i];
176                 if (dev->probed && dev->binds != NULL) {
177                         switch (dev->drv_id) {
178 #ifdef IN_EVDEV
179                         case IN_DRVID_EVDEV:
180                                 result |= in_evdev_update(dev->drv_data, dev->binds);
181                                 break;
182 #endif
183 #ifdef IN_GP2X
184                         case IN_DRVID_GP2X:
185                                 result |= in_gp2x_update(dev->drv_data, dev->binds);
186                                 break;
187 #endif
188                         }
189                 }
190         }
191
192         return result;
193 }
194
195 static int menu_key_state = 0;
196
197 void in_set_blocking(int is_blocking)
198 {
199         int i, ret;
200
201         /* have_async_devs means we will have to do all reads async anyway.. */
202         if (!in_have_async_devs) {
203                 for (i = 0; i < in_dev_count; i++) {
204                         if (in_devices[i].probed)
205                                 DRV(in_devices[i].drv_id).set_blocking(in_devices[i].drv_data, is_blocking);
206                 }
207         }
208
209         menu_key_state = 0;
210
211         /* flush events */
212         do {
213                 ret = in_update_keycode(NULL, NULL, 0);
214         } while (ret >= 0);
215 }
216
217 /* TODO: move.. */
218 #include <unistd.h>
219 #include <sys/time.h>
220 #include <time.h>
221
222 static int in_update_kc_async(int *dev_id_out, int *is_down_out, int timeout_ms)
223 {
224         struct timeval start, now;
225         int i, is_down, result;
226
227         gettimeofday(&start, NULL);
228
229         while (1)
230         {
231                 for (i = 0; i < in_dev_count; i++) {
232                         in_dev_t *d = &in_devices[i];
233                         if (!d->probed)
234                                 continue;
235
236                         result = DRV(d->drv_id).update_keycode(d->drv_data, &is_down);
237                         if (result == -1)
238                                 continue;
239
240                         if (dev_id_out)
241                                 *dev_id_out = i;
242                         if (is_down_out)
243                                 *is_down_out = is_down;
244                         return result;
245                 }
246
247                 if (timeout_ms >= 0) {
248                         gettimeofday(&now, NULL);
249                         if ((now.tv_sec - start.tv_sec) * 1000 +
250                                         (now.tv_usec - start.tv_usec) / 1000 > timeout_ms)
251                                 break;
252                 }
253
254                 usleep(10000);
255         }
256
257         return -1;
258 }
259
260 /* 
261  * wait for a press, always return some keycode or -1 on timeout or error
262  */
263 int in_update_keycode(int *dev_id_out, int *is_down_out, int timeout_ms)
264 {
265         int result = 0, dev_id = 0, is_down, result_menu;
266         int fds_hnds[IN_MAX_DEVS];
267         int i, ret, count = 0;
268         in_drv_t *drv;
269
270         if (in_have_async_devs) {
271                 result = in_update_kc_async(&dev_id, &is_down, timeout_ms);
272                 if (result == -1)
273                         return -1;
274                 drv = &DRV(in_devices[dev_id].drv_id);
275                 goto finish;
276         }
277
278         for (i = 0; i < in_dev_count; i++) {
279                 if (in_devices[i].probed)
280                         fds_hnds[count++] = in_devices[i].drv_fd_hnd;
281         }
282
283         if (count == 0) {
284                 /* don't deadlock, fail */
285                 printf("input: failed to find devices to read\n");
286                 exit(1);
287         }
288
289 again:
290         /* TODO: move this block to platform/linux */
291         {
292                 struct timeval tv, *timeout = NULL;
293                 int fdmax = -1;
294                 fd_set fdset;
295
296                 if (timeout_ms >= 0) {
297                         tv.tv_sec = timeout_ms / 1000;
298                         tv.tv_usec = (timeout_ms % 1000) * 1000;
299                         timeout = &tv;
300                 }
301
302                 FD_ZERO(&fdset);
303                 for (i = 0; i < count; i++) {
304                         if (fds_hnds[i] > fdmax) fdmax = fds_hnds[i];
305                         FD_SET(fds_hnds[i], &fdset);
306                 }
307
308                 ret = select(fdmax + 1, &fdset, NULL, NULL, timeout);
309                 if (ret == -1)
310                 {
311                         perror("input: select failed");
312                         sleep(1);
313                         return -1;
314                 }
315
316                 if (ret == 0)
317                         return -1; /* timeout */
318
319                 for (i = 0; i < count; i++)
320                         if (FD_ISSET(fds_hnds[i], &fdset))
321                                 ret = fds_hnds[i];
322         }
323
324         for (i = 0; i < in_dev_count; i++) {
325                 if (in_devices[i].drv_fd_hnd == ret) {
326                         dev_id = i;
327                         break;
328                 }
329         }
330
331         drv = &DRV(in_devices[dev_id].drv_id);
332         result = drv->update_keycode(in_devices[dev_id].drv_data, &is_down);
333
334         /* update_keycode() might return -1 when some not interesting
335          * event happened, like sync event for evdev.
336          * XXX: timeout restarts.. */
337         if (result == -1)
338                 goto again;
339
340 finish:
341         /* keep track of menu key state, to allow mixing
342          * in_update_keycode() and in_menu_wait_any() calls */
343         result_menu = drv->menu_translate(result);
344         if (result_menu != 0) {
345                 if (is_down)
346                         menu_key_state |=  result_menu;
347                 else
348                         menu_key_state &= ~result_menu;
349         }
350
351         if (dev_id_out != NULL)
352                 *dev_id_out = dev_id;
353         if (is_down_out != NULL)
354                 *is_down_out = is_down;
355         return result;
356 }
357
358 /* same as above, only return bitfield of PBTN_*  */
359 int in_menu_wait_any(int timeout_ms)
360 {
361         int keys_old = menu_key_state;
362
363         while (1)
364         {
365                 int code, is_down = 0, dev_id = 0;
366
367                 code = in_update_keycode(&dev_id, &is_down, timeout_ms);
368                 if (code >= 0)
369                         code = DRV(in_devices[dev_id].drv_id).menu_translate(code);
370
371                 if (timeout_ms >= 0)
372                         break;
373                 if (code < 0)
374                         continue;
375                 if (keys_old != menu_key_state)
376                         break;
377         }
378
379         return menu_key_state;
380 }
381
382 /* wait for menu input, do autorepeat */
383 int in_menu_wait(int interesting)
384 {
385         static int inp_prev = 0;
386         static int repeats = 0;
387         int ret, release = 0, wait = 666;
388
389         if (repeats)
390                 wait = 33;
391
392         ret = in_menu_wait_any(wait);
393         if (ret == inp_prev)
394                 repeats++;
395
396         while (!(ret & interesting)) {
397                 ret = in_menu_wait_any(-1);
398                 release = 1;
399         }
400
401         if (release || ret != inp_prev)
402                 repeats = 0;
403
404         inp_prev = ret;
405
406         /* we don't need diagonals in menus */
407         if ((ret & PBTN_UP)   && (ret & PBTN_LEFT))  ret &= ~PBTN_LEFT;
408         if ((ret & PBTN_UP)   && (ret & PBTN_RIGHT)) ret &= ~PBTN_RIGHT;
409         if ((ret & PBTN_DOWN) && (ret & PBTN_LEFT))  ret &= ~PBTN_LEFT;
410         if ((ret & PBTN_DOWN) && (ret & PBTN_RIGHT)) ret &= ~PBTN_RIGHT;
411
412         return ret;
413 }
414
415 const int *in_get_dev_binds(int dev_id)
416 {
417         if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
418                 return NULL;
419
420         return in_devices[dev_id].binds;
421 }
422
423 const int *in_get_dev_def_binds(int dev_id)
424 {
425         int count;
426
427         if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
428                 return NULL;
429
430         count = in_bind_count(in_devices[dev_id].drv_id);
431         return in_devices[dev_id].binds + count;
432 }
433
434 int in_get_dev_bind_count(int dev_id)
435 {
436         if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
437                 return 0;
438
439         return in_bind_count(in_devices[dev_id].drv_id);
440 }
441
442 const char *in_get_dev_name(int dev_id, int must_be_active)
443 {
444         if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
445                 return NULL;
446
447         if (must_be_active && !in_devices[dev_id].probed)
448                 return NULL;
449         return in_devices[dev_id].name;
450 }
451
452 /* never returns NULL */
453 const char *in_get_key_name(int dev_id, int keycode)
454 {
455         static char xname[16];
456         const char *name;
457
458         if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
459                 return "Unkn0";
460
461         name = DRV(in_devices[dev_id].drv_id).get_key_name(keycode);
462         if (name != NULL)
463                 return name;
464
465         /* assume scancode */
466         if ((keycode >= '0' && keycode <= '9') || (keycode >= 'a' && keycode <= 'z')
467                         || (keycode >= 'A' && keycode <= 'Z'))
468                 sprintf(xname, "%c", keycode);
469         else
470                 sprintf(xname, "\\x%02X", keycode);
471         return xname;
472 }
473
474 int in_bind_key(int dev_id, int keycode, int mask, int force_unbind)
475 {
476         int ret, count;
477         in_dev_t *dev;
478
479         if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
480                 return -1;
481         dev = &in_devices[dev_id];
482
483         if (dev->binds == NULL) {
484                 if (force_unbind)
485                         return 0;
486                 dev->binds = in_alloc_binds(dev->drv_id);
487                 if (dev->binds == NULL)
488                         return -1;
489         }
490
491         count = in_bind_count(dev->drv_id);
492         if (keycode < 0 || keycode >= count)
493                 return -1;
494         
495         if (force_unbind)
496                 dev->binds[keycode] &= ~mask;
497         else
498                 dev->binds[keycode] ^=  mask;
499         
500         ret = DRV(dev->drv_id).clean_binds(dev->drv_data, dev->binds);
501         if (ret == 0) {
502                 free(dev->binds);
503                 dev->binds = NULL;
504         }
505
506         return 0;
507 }
508
509 /* returns device id, or -1 on error */
510 int in_config_parse_dev(const char *name)
511 {
512         int drv_id = -1, i;
513
514         for (i = 0; i < IN_DRVID_COUNT; i++) {
515                 int len = strlen(in_drivers[i].prefix);
516                 if (strncmp(name, in_drivers[i].prefix, len) == 0) {
517                         drv_id = i;
518                         break;
519                 }
520         }
521
522         if (drv_id < 0) {
523                 printf("input: missing driver for %s\n", name);
524                 return -1;
525         }
526
527         for (i = 0; i < in_dev_count; i++)
528         {
529                 if (in_devices[i].name == NULL)
530                         continue;
531                 if (strcmp(in_devices[i].name, name) == 0)
532                         return i;
533         }
534
535         if (i >= IN_MAX_DEVS)
536         {
537                 /* try to find unused device */
538                 for (i = 0; i < IN_MAX_DEVS; i++)
539                         if (in_devices[i].name == NULL) break;
540                 if (i >= IN_MAX_DEVS) {
541                         printf("input: too many devices, can't add %s\n", name);
542                         return -1;
543                 }
544         }
545
546         memset(&in_devices[i], 0, sizeof(in_devices[i]));
547
548         in_devices[i].name = strdup(name);
549         if (in_devices[i].name == NULL)
550                 return -1;
551
552         if (i + 1 > in_dev_count)
553                 in_dev_count = i + 1;
554         in_devices[i].drv_id = drv_id;
555
556         return i;
557 }
558
559 /*
560  * To reduce size of game specific configs, default binds are not saved.
561  * So we mark default binds in in_config_start(), override them in in_config_bind_key(),
562  * and restore whatever default binds are left in in_config_end().
563  */
564 void in_config_start(void)
565 {
566         int i;
567
568         /* mark all default binds, so they get overwritten by func below */
569         for (i = 0; i < IN_MAX_DEVS; i++) {
570                 int n, count, *binds, *def_binds;
571
572                 binds = in_devices[i].binds;
573                 if (binds == NULL)
574                         continue;
575
576                 count = in_bind_count(in_devices[i].drv_id);
577                 def_binds = binds + count;
578
579                 for (n = 0; n < count; n++)
580                         if (binds[n] == def_binds[n])
581                                 binds[n] = -1;
582         }
583 }
584
585 int in_config_bind_key(int dev_id, const char *key, int binds)
586 {
587         int count, kc;
588         in_dev_t *dev;
589
590         if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
591                 return -1;
592         dev = &in_devices[dev_id];
593
594         count = in_bind_count(dev->drv_id);
595
596         /* maybe a raw code? */
597         if (key[0] == '\\' && key[1] == 'x') {
598                 char *p = NULL;
599                 kc = (int)strtoul(key + 2, &p, 16);
600                 if (p == NULL || *p != 0)
601                         kc = -1;
602         }
603         else {
604                 /* device specific key name */
605                 if (dev->binds == NULL) {
606                         dev->binds = in_alloc_binds(dev->drv_id);
607                         if (dev->binds == NULL)
608                                 return -1;
609                         in_config_start();
610                 }
611
612                 kc = DRV(dev->drv_id).get_key_code(key);
613                 if (kc < 0 && strlen(key) == 1) {
614                         /* assume scancode */
615                         kc = key[0];
616                 }
617         }
618
619         if (kc < 0 || kc >= count) {
620                 printf("input: bad key: %s\n", key);
621                 return -1;
622         }
623
624         if (dev->binds[kc] == -1)
625                 dev->binds[kc] = 0;
626         dev->binds[kc] |= binds;
627
628         return 0;
629 }
630
631 void in_config_end(void)
632 {
633         int i;
634
635         for (i = 0; i < IN_MAX_DEVS; i++) {
636                 int n, ret, count, *binds, *def_binds;
637                 in_dev_t *dev = &in_devices[i];
638
639                 if (dev->binds == NULL)
640                         continue;
641
642                 count = in_bind_count(dev->drv_id);
643                 binds = dev->binds;
644                 def_binds = binds + count;
645
646                 for (n = 0; n < count; n++)
647                         if (binds[n] == -1)
648                                 binds[n] = def_binds[n];
649
650                 if (dev->drv_data == NULL)
651                         continue;
652
653                 ret = DRV(dev->drv_id).clean_binds(dev->drv_data, binds);
654                 if (ret == 0) {
655                         /* no useable binds */
656                         free(dev->binds);
657                         dev->binds = NULL;
658                 }
659         }
660 }
661
662 void in_debug_dump(void)
663 {
664         int i;
665
666         printf("# drv probed binds name\n");
667         for (i = 0; i < IN_MAX_DEVS; i++) {
668                 in_dev_t *d = &in_devices[i];
669                 if (!d->probed && d->name == NULL && d->binds == NULL)
670                         continue;
671                 printf("%d %3d %6c %5c %s\n", i, d->drv_id, d->probed ? 'y' : 'n',
672                         d->binds ? 'y' : 'n', d->name);
673         }
674 }
675
676 /* handlers for unknown/not_preset drivers */
677
678 static void in_def_probe(void) {}
679 static void in_def_free(void *drv_data) {}
680 static int  in_def_get_bind_count(void) { return 0; }
681 static void in_def_get_def_binds(int *binds) {}
682 static int  in_def_clean_binds(void *drv_data, int *binds) { return 0; }
683 static void in_def_set_blocking(void *data, int y) {}
684 static int  in_def_update_keycode(void *drv_data, int *is_down) { return 0; }
685 static int  in_def_menu_translate(int keycode) { return keycode; }
686 static int  in_def_get_key_code(const char *key_name) { return 0; }
687 static const char *in_def_get_key_name(int keycode) { return NULL; }
688
689 void in_init(void)
690 {
691         int i;
692
693         memset(in_drivers, 0, sizeof(in_drivers));
694         memset(in_devices, 0, sizeof(in_devices));
695         in_dev_count = 0;
696
697         for (i = 0; i < IN_DRVID_COUNT; i++) {
698                 in_drivers[i].prefix = "none:";
699                 in_drivers[i].probe = in_def_probe;
700                 in_drivers[i].free = in_def_free;
701                 in_drivers[i].get_bind_count = in_def_get_bind_count;
702                 in_drivers[i].get_def_binds = in_def_get_def_binds;
703                 in_drivers[i].clean_binds = in_def_clean_binds;
704                 in_drivers[i].set_blocking = in_def_set_blocking;
705                 in_drivers[i].update_keycode = in_def_update_keycode;
706                 in_drivers[i].menu_translate = in_def_menu_translate;
707                 in_drivers[i].get_key_code = in_def_get_key_code;
708                 in_drivers[i].get_key_name = in_def_get_key_name;
709         }
710
711 #ifdef IN_GP2X
712         in_gp2x_init(&in_drivers[IN_DRVID_GP2X]);
713 #endif
714 #ifdef IN_EVDEV
715         in_evdev_init(&in_drivers[IN_DRVID_EVDEV]);
716 #endif
717 }
718
719 #if 0
720 int main(void)
721 {
722         int ret;
723
724         in_init();
725         in_probe();
726
727         in_set_blocking(1);
728
729 #if 1
730         while (1) {
731                 int dev = 0, down;
732                 ret = in_update_keycode(&dev, &down);
733                 printf("#%i: %i %i (%s)\n", dev, down, ret, in_get_key_name(dev, ret));
734         }
735 #else
736         while (1) {
737                 ret = in_menu_wait_any();
738                 printf("%08x\n", ret);
739         }
740 #endif
741
742         return 0;
743 }
744 #endif