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