input: support analog read
[libpicofe.git] / common / input.c
CommitLineData
b7c7cd5d 1/*
c4334e00 2 * (C) GraÅžvydas "notaz" Ignotas, 2008-2011
b7c7cd5d 3 *
4 * This work is licensed under the terms of any of these licenses
5 * (at your option):
6 * - GNU GPL, version 2 or later.
7 * - GNU LGPL, version 2.1 or later.
8 * See the COPYING file in the top-level directory.
9 */
10
2258f158 11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14
15#include "input.h"
4ab30ad4 16#include "plat.h"
7ee5c389 17#include "lprintf.h"
972151fc 18
972151fc 19#ifdef IN_GP2X
c4334e00 20#error needs update: in_gp2x_init in_gp2x_update
13b692eb 21#include "../gp2x/in_gp2x.h"
972151fc 22#endif
23#ifdef IN_VK
c4334e00 24#error needs update: in_vk_init in_vk_update
8ced8d2b 25#include "../win32/in_vk.h"
972151fc 26#endif
2258f158 27
28typedef struct
29{
30 int drv_id;
7f022a8d 31 int drv_fd_hnd;
2258f158 32 void *drv_data;
2258f158 33 char *name;
8e77275e 34 int key_count;
35 int *binds; /* total = key_count * bindtypes * 2 */
e9b29264 36 const char * const *key_names;
37 unsigned int probed:1;
38 unsigned int does_combos:1;
2258f158 39} in_dev_t;
40
c4334e00 41static in_drv_t *in_drivers;
2258f158 42static in_dev_t in_devices[IN_MAX_DEVS];
c4334e00 43static int in_driver_count = 0;
23fb16c8 44static int in_dev_count = 0; /* probed + bind devices */
13b692eb 45static int in_have_async_devs = 0;
c4334e00 46static int in_probe_dev_id;
82abf46f 47static int menu_key_state = 0;
48static int menu_last_used_dev = 0;
2258f158 49
c4334e00 50#define DRV(id) in_drivers[id]
da767bd5 51
13b692eb 52
8e77275e 53static int *in_alloc_binds(int drv_id, int key_count)
2258f158 54{
8e77275e 55 int *binds;
2258f158 56
8e77275e 57 binds = calloc(key_count * IN_BINDTYPE_COUNT * 2, sizeof(binds[0]));
da767bd5 58 if (binds == NULL)
59 return NULL;
60
8e77275e 61 DRV(drv_id).get_def_binds(binds + key_count * IN_BINDTYPE_COUNT);
62 memcpy(binds, binds + key_count * IN_BINDTYPE_COUNT,
63 sizeof(binds[0]) * key_count * IN_BINDTYPE_COUNT);
da767bd5 64
65 return binds;
2258f158 66}
67
23fb16c8 68static void in_unprobe(in_dev_t *dev)
2258f158 69{
da767bd5 70 if (dev->probed)
71 DRV(dev->drv_id).free(dev->drv_data);
2258f158 72 dev->probed = 0;
73 dev->drv_data = NULL;
23fb16c8 74}
75
76static void in_free(in_dev_t *dev)
77{
78 in_unprobe(dev);
2258f158 79 free(dev->name);
80 dev->name = NULL;
81 free(dev->binds);
82 dev->binds = NULL;
83}
84
13b692eb 85/* to be called by drivers
86 * async devices must set drv_fd_hnd to -1 */
c4334e00 87void in_register(const char *nname, int drv_fd_hnd, void *drv_data,
e9b29264 88 int key_count, const char * const *key_names, int combos)
2258f158 89{
da767bd5 90 int i, ret, dupe_count = 0, *binds;
2258f158 91 char name[256], *name_end, *tmp;
92
93 strncpy(name, nname, sizeof(name));
94 name[sizeof(name)-12] = 0;
95 name_end = name + strlen(name);
96
97 for (i = 0; i < in_dev_count; i++)
98 {
99 if (in_devices[i].name == NULL)
100 continue;
101 if (strcmp(in_devices[i].name, name) == 0)
102 {
103 if (in_devices[i].probed) {
104 dupe_count++;
105 sprintf(name_end, " [%d]", dupe_count);
106 continue;
107 }
108 goto update;
109 }
110 }
111
112 if (i >= IN_MAX_DEVS)
113 {
114 /* try to find unused device */
115 for (i = 0; i < IN_MAX_DEVS; i++)
116 if (!in_devices[i].probed) break;
117 if (i >= IN_MAX_DEVS) {
7ee5c389 118 lprintf("input: too many devices, can't add %s\n", name);
2258f158 119 return;
120 }
121 in_free(&in_devices[i]);
122 }
123
124 tmp = strdup(name);
125 if (tmp == NULL)
126 return;
127
c4334e00 128 binds = in_alloc_binds(in_probe_dev_id, key_count);
2258f158 129 if (binds == NULL) {
130 free(tmp);
131 return;
132 }
133
134 in_devices[i].name = tmp;
135 in_devices[i].binds = binds;
8e77275e 136 in_devices[i].key_count = key_count;
2258f158 137 if (i + 1 > in_dev_count)
138 in_dev_count = i + 1;
139
7ee5c389 140 lprintf("input: new device #%d \"%s\"\n", i, name);
2258f158 141update:
142 in_devices[i].probed = 1;
2c600560 143 in_devices[i].does_combos = combos;
c4334e00 144 in_devices[i].drv_id = in_probe_dev_id;
7f022a8d 145 in_devices[i].drv_fd_hnd = drv_fd_hnd;
e9b29264 146 in_devices[i].key_names = key_names;
2258f158 147 in_devices[i].drv_data = drv_data;
da767bd5 148
149 if (in_devices[i].binds != NULL) {
c4334e00 150 ret = DRV(in_probe_dev_id).clean_binds(drv_data, in_devices[i].binds,
8e77275e 151 in_devices[i].binds + key_count * IN_BINDTYPE_COUNT);
da767bd5 152 if (ret == 0) {
153 /* no useable binds */
154 free(in_devices[i].binds);
155 in_devices[i].binds = NULL;
156 }
157 }
2258f158 158}
159
8e77275e 160/* key combo handling, to be called by drivers that support it.
161 * Only care about IN_BINDTYPE_EMU */
162void in_combos_find(const int *binds, int last_key, int *combo_keys, int *combo_acts)
2c600560 163{
164 int act, u;
165
166 *combo_keys = *combo_acts = 0;
167 for (act = 0; act < sizeof(binds[0]) * 8; act++)
168 {
169 int keyc = 0;
170 for (u = 0; u <= last_key; u++)
8e77275e 171 if (binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)] & (1 << act))
2c600560 172 keyc++;
173
174 if (keyc > 1)
175 {
176 // loop again and mark those keys and actions as combo
177 for (u = 0; u <= last_key; u++)
178 {
8e77275e 179 if (binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)] & (1 << act)) {
2c600560 180 *combo_keys |= 1 << u;
181 *combo_acts |= 1 << act;
182 }
183 }
184 }
185 }
186}
187
8e77275e 188int in_combos_do(int keys, const int *binds, int last_key, int combo_keys, int combo_acts)
2c600560 189{
190 int i, ret = 0;
191
192 for (i = 0; i <= last_key; i++)
193 {
8e77275e 194 int acts, acts_c, u;
195
2c600560 196 if (!(keys & (1 << i)))
197 continue;
198
8e77275e 199 acts = binds[IN_BIND_OFFS(i, IN_BINDTYPE_EMU)];
2c600560 200 if (!acts)
201 continue;
202
8e77275e 203 if (!(combo_keys & (1 << i))) {
2c600560 204 ret |= acts;
8e77275e 205 continue;
206 }
207
208 acts_c = acts & combo_acts;
209 u = last_key;
210 if (acts_c) {
211 // let's try to find the other one
212 for (u = i + 1; u <= last_key; u++)
213 if ( (keys & (1 << u)) && (binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)] & acts_c) ) {
214 ret |= acts_c & binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)];
215 keys &= ~((1 << i) | (1 << u));
216 break;
217 }
218 }
219 // add non-combo actions if combo ones were not found
220 if (u >= last_key)
221 ret |= acts & ~combo_acts;
2c600560 222 }
223
224 return ret;
225}
226
2258f158 227void in_probe(void)
228{
229 int i;
13b692eb 230
231 in_have_async_devs = 0;
23fb16c8 232 menu_key_state = 0;
233 menu_last_used_dev = 0;
234
235 for (i = 0; i < in_dev_count; i++)
236 in_unprobe(&in_devices[i]);
2258f158 237
c4334e00 238 for (i = 0; i < in_driver_count; i++) {
239 in_probe_dev_id = i;
da767bd5 240 in_drivers[i].probe();
c4334e00 241 }
2258f158 242
243 /* get rid of devs without binds and probes */
244 for (i = 0; i < in_dev_count; i++) {
245 if (!in_devices[i].probed && in_devices[i].binds == NULL) {
246 in_dev_count--;
247 if (i < in_dev_count) {
248 free(in_devices[i].name);
249 memmove(&in_devices[i], &in_devices[i+1],
250 (in_dev_count - i) * sizeof(in_devices[0]));
251 }
13b692eb 252
253 continue;
2258f158 254 }
13b692eb 255
256 if (in_devices[i].probed && in_devices[i].drv_fd_hnd == -1)
257 in_have_async_devs = 1;
2258f158 258 }
13b692eb 259
260 if (in_have_async_devs)
7ee5c389 261 lprintf("input: async-only devices detected..\n");
e9b29264 262
263 in_debug_dump();
2258f158 264}
265
7f022a8d 266/* async update */
8e77275e 267int in_update(int *result)
2258f158 268{
8e77275e 269 int i, ret = 0;
2258f158 270
271 for (i = 0; i < in_dev_count; i++) {
906bdc9f 272 in_dev_t *dev = &in_devices[i];
c4334e00 273 if (dev->probed && dev->binds != NULL)
274 ret |= DRV(dev->drv_id).update(dev->drv_data, dev->binds, result);
2258f158 275 }
276
8e77275e 277 return ret;
2258f158 278}
279
13b692eb 280static int in_update_kc_async(int *dev_id_out, int *is_down_out, int timeout_ms)
281{
13b692eb 282 int i, is_down, result;
4ab30ad4 283 unsigned int ticks;
13b692eb 284
4ab30ad4 285 ticks = plat_get_ticks_ms();
13b692eb 286
287 while (1)
288 {
289 for (i = 0; i < in_dev_count; i++) {
290 in_dev_t *d = &in_devices[i];
291 if (!d->probed)
292 continue;
293
294 result = DRV(d->drv_id).update_keycode(d->drv_data, &is_down);
295 if (result == -1)
296 continue;
297
298 if (dev_id_out)
299 *dev_id_out = i;
300 if (is_down_out)
301 *is_down_out = is_down;
302 return result;
303 }
304
4ab30ad4 305 if (timeout_ms >= 0 && (int)(plat_get_ticks_ms() - ticks) > timeout_ms)
306 break;
13b692eb 307
4ab30ad4 308 plat_sleep_ms(10);
13b692eb 309 }
310
311 return -1;
312}
7f022a8d 313
34581c95 314/*
13b692eb 315 * wait for a press, always return some keycode or -1 on timeout or error
34581c95 316 */
906bdc9f 317int in_update_keycode(int *dev_id_out, int *is_down_out, int timeout_ms)
34581c95 318{
4ab30ad4 319 int result = -1, dev_id = 0, is_down, result_menu;
7f022a8d 320 int fds_hnds[IN_MAX_DEVS];
321 int i, ret, count = 0;
7fd581f4 322 in_drv_t *drv = NULL;
4ab30ad4 323 unsigned int ticks;
7f022a8d 324
13b692eb 325 if (in_have_async_devs) {
326 result = in_update_kc_async(&dev_id, &is_down, timeout_ms);
327 if (result == -1)
328 return -1;
329 drv = &DRV(in_devices[dev_id].drv_id);
330 goto finish;
331 }
332
4ab30ad4 333 ticks = plat_get_ticks_ms();
334
7f022a8d 335 for (i = 0; i < in_dev_count; i++) {
336 if (in_devices[i].probed)
337 fds_hnds[count++] = in_devices[i].drv_fd_hnd;
338 }
34581c95 339
340 if (count == 0) {
341 /* don't deadlock, fail */
7ee5c389 342 lprintf("input: failed to find devices to read\n");
34581c95 343 exit(1);
344 }
345
4ab30ad4 346 while (1)
7f022a8d 347 {
4ab30ad4 348 ret = plat_wait_event(fds_hnds, count, timeout_ms);
349 if (ret < 0)
350 break;
7f022a8d 351
4ab30ad4 352 for (i = 0; i < in_dev_count; i++) {
353 if (in_devices[i].drv_fd_hnd == ret) {
354 dev_id = i;
355 break;
356 }
7f022a8d 357 }
358
4ab30ad4 359 drv = &DRV(in_devices[dev_id].drv_id);
360 result = drv->update_keycode(in_devices[dev_id].drv_data, &is_down);
4ab30ad4 361 if (result >= 0)
906bdc9f 362 break;
4ab30ad4 363
23fb16c8 364 if (result == -2) {
365 lprintf("input: \"%s\" errored out, removing.\n", in_devices[dev_id].name);
366 in_unprobe(&in_devices[dev_id]);
367 break;
368 }
369
4ab30ad4 370 if (timeout_ms >= 0) {
371 unsigned int ticks2 = plat_get_ticks_ms();
372 timeout_ms -= ticks2 - ticks;
373 ticks = ticks2;
374 if (timeout_ms <= 0)
375 break;
595491c4 376 }
377 }
7f022a8d 378
74065be8 379 if (result < 0)
4ab30ad4 380 return -1;
13b692eb 381finish:
906bdc9f 382 /* keep track of menu key state, to allow mixing
fce20e73 383 * in_update_keycode() and in_menu_wait_any() calls */
e9b29264 384 result_menu = drv->menu_translate(in_devices[dev_id].drv_data, result);
906bdc9f 385 if (result_menu != 0) {
386 if (is_down)
387 menu_key_state |= result_menu;
388 else
389 menu_key_state &= ~result_menu;
390 }
391
392 if (dev_id_out != NULL)
393 *dev_id_out = dev_id;
394 if (is_down_out != NULL)
395 *is_down_out = is_down;
34581c95 396 return result;
397}
398
fce20e73 399/* same as above, only return bitfield of PBTN_* */
400int in_menu_wait_any(int timeout_ms)
595491c4 401{
906bdc9f 402 int keys_old = menu_key_state;
595491c4 403
404 while (1)
405 {
da767bd5 406 int code, is_down = 0, dev_id = 0;
595491c4 407
906bdc9f 408 code = in_update_keycode(&dev_id, &is_down, timeout_ms);
13b692eb 409 if (code < 0)
595491c4 410 break;
e9b29264 411
412 if (keys_old != menu_key_state) {
413 menu_last_used_dev = dev_id;
414 break;
415 }
595491c4 416 }
417
906bdc9f 418 return menu_key_state;
595491c4 419}
420
fce20e73 421/* wait for menu input, do autorepeat */
049a6b3e 422int in_menu_wait(int interesting, int autorep_delay_ms)
fce20e73 423{
424 static int inp_prev = 0;
6c263f30 425 static int repeats = 0;
2c600560 426 int ret, release = 0, wait = 450;
fce20e73 427
6c263f30 428 if (repeats)
049a6b3e 429 wait = autorep_delay_ms;
fce20e73 430
6c263f30 431 ret = in_menu_wait_any(wait);
432 if (ret == inp_prev)
433 repeats++;
fce20e73 434
435 while (!(ret & interesting)) {
436 ret = in_menu_wait_any(-1);
437 release = 1;
438 }
439
6c263f30 440 if (release || ret != inp_prev)
fce20e73 441 repeats = 0;
6c263f30 442
fce20e73 443 inp_prev = ret;
444
6c263f30 445 /* we don't need diagonals in menus */
fce20e73 446 if ((ret & PBTN_UP) && (ret & PBTN_LEFT)) ret &= ~PBTN_LEFT;
447 if ((ret & PBTN_UP) && (ret & PBTN_RIGHT)) ret &= ~PBTN_RIGHT;
448 if ((ret & PBTN_DOWN) && (ret & PBTN_LEFT)) ret &= ~PBTN_LEFT;
449 if ((ret & PBTN_DOWN) && (ret & PBTN_RIGHT)) ret &= ~PBTN_RIGHT;
450
451 return ret;
452}
453
da767bd5 454const int *in_get_dev_binds(int dev_id)
455{
456 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
457 return NULL;
458
459 return in_devices[dev_id].binds;
460}
461
462const int *in_get_dev_def_binds(int dev_id)
463{
da767bd5 464 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
465 return NULL;
466
8e77275e 467 return in_devices[dev_id].binds + in_devices[dev_id].key_count * IN_BINDTYPE_COUNT;
da767bd5 468}
469
e9b29264 470int in_get_config(int dev_id, int what, void *val)
da767bd5 471{
e9b29264 472 int *ival = val;
473 in_dev_t *dev;
da767bd5 474
e9b29264 475 if (dev_id < 0 || dev_id >= IN_MAX_DEVS || val == NULL)
476 return -1;
477
478 dev = &in_devices[dev_id];
2c600560 479 switch (what) {
e9b29264 480 case IN_CFG_BIND_COUNT:
481 *ival = dev->key_count;
482 break;
483 case IN_CFG_DOES_COMBOS:
484 *ival = dev->does_combos;
485 break;
486 case IN_CFG_BLOCKING:
487 case IN_CFG_KEY_NAMES:
488 return -1; /* not implemented */
489 default:
490 return DRV(dev->drv_id).get_config(dev->drv_data, what, ival);
491 }
492
493 return 0;
494}
495
496static int in_set_blocking(int is_blocking)
497{
498 int i, ret;
499
500 /* have_async_devs means we will have to do all reads async anyway.. */
501 if (!in_have_async_devs) {
502 for (i = 0; i < in_dev_count; i++) {
503 if (in_devices[i].probed)
504 DRV(in_devices[i].drv_id).set_config(in_devices[i].drv_data,
505 IN_CFG_BLOCKING, is_blocking);
506 }
2c600560 507 }
508
e9b29264 509 menu_key_state = 0;
510
511 /* flush events */
512 do {
513 ret = in_update_keycode(NULL, NULL, 0);
514 } while (ret >= 0);
515
2c600560 516 return 0;
da767bd5 517}
518
e9b29264 519int in_set_config(int dev_id, int what, const void *val, int size)
520{
521 const int *ival = val;
522 in_dev_t *dev;
523
524 if (what == IN_CFG_BLOCKING)
525 return in_set_blocking(*ival);
526
527 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
528 return -1;
529
530 dev = &in_devices[dev_id];
531 if (what == IN_CFG_KEY_NAMES) {
532 const char * const *names = val;
533 int count = size / sizeof(names[0]);
534
535 if (count < dev->key_count) {
536 lprintf("input: set_key_names: not enough keys\n");
537 return -1;
538 }
539
540 dev->key_names = names;
541 return 0;
542 }
543
8754b84c 544 if (dev->probed)
545 return DRV(dev->drv_id).set_config(dev->drv_data, what, *ival);
546
547 return -1;
e9b29264 548}
549
049a6b3e 550const char *in_get_dev_name(int dev_id, int must_be_active, int skip_pfix)
da767bd5 551{
049a6b3e 552 const char *name, *tmp;
553
da767bd5 554 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
555 return NULL;
556
fce20e73 557 if (must_be_active && !in_devices[dev_id].probed)
558 return NULL;
049a6b3e 559
560 name = in_devices[dev_id].name;
bf3c44f4 561 if (name == NULL || !skip_pfix)
562 return name;
f3b6f133 563
bf3c44f4 564 /* skip prefix */
049a6b3e 565 tmp = strchr(name, ':');
566 if (tmp != NULL)
567 name = tmp + 1;
568
569 return name;
da767bd5 570}
571
e9b29264 572int in_name_to_id(const char *dev_name)
573{
574 int i;
575
576 for (i = 0; i < in_dev_count; i++)
577 if (strcmp(dev_name, in_devices[i].name) == 0)
578 break;
579
580 if (i >= in_dev_count) {
581 lprintf("input: in_name_to_id: no such device: %s\n", dev_name);
582 return -1;
583 }
584
585 return i;
586}
587
906bdc9f 588/* never returns NULL */
595491c4 589const char *in_get_key_name(int dev_id, int keycode)
590{
e9b29264 591 const char *name = NULL;
906bdc9f 592 static char xname[16];
c4334e00 593 in_drv_t *drv;
e9b29264 594 in_dev_t *dev;
906bdc9f 595
82abf46f 596 if (dev_id < 0) /* want last used dev? */
597 dev_id = menu_last_used_dev;
598
595491c4 599 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
600 return "Unkn0";
da767bd5 601
e9b29264 602 dev = &in_devices[dev_id];
c4334e00 603 drv = &DRV(dev->drv_id);
82abf46f 604 if (keycode < 0) /* want name for menu key? */
c4334e00 605 keycode = drv->menu_translate(dev->drv_data, keycode);
e9b29264 606
607 if (dev->key_names != NULL && 0 <= keycode && keycode < dev->key_count)
608 name = dev->key_names[keycode];
609 if (name != NULL)
610 return name;
82abf46f 611
c4334e00 612 if (drv->get_key_name != NULL)
613 name = drv->get_key_name(keycode);
906bdc9f 614 if (name != NULL)
615 return name;
616
617 /* assume scancode */
618 if ((keycode >= '0' && keycode <= '9') || (keycode >= 'a' && keycode <= 'z')
619 || (keycode >= 'A' && keycode <= 'Z'))
620 sprintf(xname, "%c", keycode);
621 else
622 sprintf(xname, "\\x%02X", keycode);
623 return xname;
624}
625
d166ceb6 626int in_get_key_code(int dev_id, const char *key_name)
627{
628 in_dev_t *dev;
629 int i;
630
631 if (dev_id < 0) /* want last used dev? */
632 dev_id = menu_last_used_dev;
633
634 if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
635 return -1;
636
637 dev = &in_devices[dev_id];
638 if (dev->key_names == NULL)
639 return -1;
640
641 for (i = 0; i < dev->key_count; i++)
642 if (dev->key_names[i] && strcasecmp(dev->key_names[i], key_name) == 0)
643 return i;
644
645 return -1;
646}
647
92068389 648int in_bind_key(int dev_id, int keycode, int mask, int bind_type, int force_unbind)
906bdc9f 649{
650 int ret, count;
651 in_dev_t *dev;
652
8e77275e 653 if (dev_id < 0 || dev_id >= IN_MAX_DEVS || bind_type >= IN_BINDTYPE_COUNT)
906bdc9f 654 return -1;
8e77275e 655
906bdc9f 656 dev = &in_devices[dev_id];
8e77275e 657 count = dev->key_count;
906bdc9f 658
659 if (dev->binds == NULL) {
660 if (force_unbind)
661 return 0;
8e77275e 662 dev->binds = in_alloc_binds(dev->drv_id, count);
906bdc9f 663 if (dev->binds == NULL)
664 return -1;
665 }
666
906bdc9f 667 if (keycode < 0 || keycode >= count)
668 return -1;
669
670 if (force_unbind)
8e77275e 671 dev->binds[IN_BIND_OFFS(keycode, bind_type)] &= ~mask;
906bdc9f 672 else
8e77275e 673 dev->binds[IN_BIND_OFFS(keycode, bind_type)] ^= mask;
906bdc9f 674
8e77275e 675 ret = DRV(dev->drv_id).clean_binds(dev->drv_data, dev->binds,
676 dev->binds + count * IN_BINDTYPE_COUNT);
906bdc9f 677 if (ret == 0) {
678 free(dev->binds);
679 dev->binds = NULL;
680 }
681
682 return 0;
da767bd5 683}
684
2e307a07 685/*
686 * Unbind act_mask on binds with type bind_type
687 * - if dev_id_ < 0, affects all devices
688 * else only affects dev_id_
689 * - if act_mask == -1, unbind all keys
690 * else only actions in mask
691 */
692void in_unbind_all(int dev_id_, int act_mask, int bind_type)
92068389 693{
2e307a07 694 int dev_id = 0, dev_last = IN_MAX_DEVS - 1;
92068389 695 int i, count;
696 in_dev_t *dev;
697
2e307a07 698 if (dev_id_ >= 0)
699 dev_id = dev_last = dev_id_;
700
701 if (bind_type >= IN_BINDTYPE_COUNT)
92068389 702 return;
703
2e307a07 704 for (; dev_id <= dev_last; dev_id++) {
705 dev = &in_devices[dev_id];
706 count = dev->key_count;
92068389 707
2e307a07 708 if (dev->binds == NULL)
709 continue;
92068389 710
2e307a07 711 if (act_mask != -1) {
712 for (i = 0; i < count; i++)
713 dev->binds[IN_BIND_OFFS(i, bind_type)] &= ~act_mask;
714 }
715 else
716 memset(dev->binds, 0, sizeof(dev->binds[0]) * count * IN_BINDTYPE_COUNT);
717 }
92068389 718}
719
da767bd5 720/* returns device id, or -1 on error */
721int in_config_parse_dev(const char *name)
722{
723 int drv_id = -1, i;
724
c4334e00 725 for (i = 0; i < in_driver_count; i++) {
da767bd5 726 int len = strlen(in_drivers[i].prefix);
727 if (strncmp(name, in_drivers[i].prefix, len) == 0) {
728 drv_id = i;
729 break;
730 }
731 }
732
733 if (drv_id < 0) {
7ee5c389 734 lprintf("input: missing driver for %s\n", name);
da767bd5 735 return -1;
736 }
737
738 for (i = 0; i < in_dev_count; i++)
739 {
740 if (in_devices[i].name == NULL)
741 continue;
742 if (strcmp(in_devices[i].name, name) == 0)
743 return i;
744 }
745
746 if (i >= IN_MAX_DEVS)
747 {
748 /* try to find unused device */
749 for (i = 0; i < IN_MAX_DEVS; i++)
750 if (in_devices[i].name == NULL) break;
751 if (i >= IN_MAX_DEVS) {
7ee5c389 752 lprintf("input: too many devices, can't add %s\n", name);
da767bd5 753 return -1;
754 }
755 }
756
757 memset(&in_devices[i], 0, sizeof(in_devices[i]));
758
759 in_devices[i].name = strdup(name);
760 if (in_devices[i].name == NULL)
761 return -1;
762
23fb16c8 763 in_devices[i].key_names = DRV(drv_id).get_key_names(&in_devices[i].key_count);
8e77275e 764 in_devices[i].drv_id = drv_id;
765
da767bd5 766 if (i + 1 > in_dev_count)
767 in_dev_count = i + 1;
da767bd5 768
769 return i;
770}
771
8e77275e 772int in_config_bind_key(int dev_id, const char *key, int acts, int bind_type)
da767bd5 773{
906bdc9f 774 in_dev_t *dev;
8e77275e 775 int i, offs, kc;
da767bd5 776
8e77275e 777 if (dev_id < 0 || dev_id >= IN_MAX_DEVS || bind_type >= IN_BINDTYPE_COUNT)
da767bd5 778 return -1;
906bdc9f 779 dev = &in_devices[dev_id];
da767bd5 780
906bdc9f 781 /* maybe a raw code? */
782 if (key[0] == '\\' && key[1] == 'x') {
783 char *p = NULL;
784 kc = (int)strtoul(key + 2, &p, 16);
785 if (p == NULL || *p != 0)
786 kc = -1;
787 }
788 else {
789 /* device specific key name */
790 if (dev->binds == NULL) {
8e77275e 791 dev->binds = in_alloc_binds(dev->drv_id, dev->key_count);
906bdc9f 792 if (dev->binds == NULL)
793 return -1;
906bdc9f 794 }
795
e9b29264 796 kc = -1;
797 if (dev->key_names != NULL) {
798 for (i = 0; i < dev->key_count; i++) {
799 const char *k = dev->key_names[i];
800 if (k != NULL && strcasecmp(k, key) == 0) {
801 kc = i;
802 break;
803 }
804 }
805 }
806
807 if (kc < 0)
808 kc = DRV(dev->drv_id).get_key_code(key);
906bdc9f 809 if (kc < 0 && strlen(key) == 1) {
810 /* assume scancode */
811 kc = key[0];
812 }
da767bd5 813 }
814
8e77275e 815 if (kc < 0 || kc >= dev->key_count) {
7ee5c389 816 lprintf("input: bad key: %s\n", key);
da767bd5 817 return -1;
595491c4 818 }
819
8e77275e 820 if (bind_type == IN_BINDTYPE_NONE) {
821 for (i = 0; i < IN_BINDTYPE_COUNT; i++)
822 dev->binds[IN_BIND_OFFS(kc, i)] = 0;
823 return 0;
824 }
da767bd5 825
8e77275e 826 offs = IN_BIND_OFFS(kc, bind_type);
827 if (dev->binds[offs] == -1)
828 dev->binds[offs] = 0;
829 dev->binds[offs] |= acts;
da767bd5 830 return 0;
831}
832
45a39652 833void in_clean_binds(void)
da767bd5 834{
835 int i;
836
837 for (i = 0; i < IN_MAX_DEVS; i++) {
45a39652 838 int ret, count, *binds, *def_binds;
906bdc9f 839 in_dev_t *dev = &in_devices[i];
da767bd5 840
45a39652 841 if (dev->binds == NULL || dev->drv_data == NULL)
da767bd5 842 continue;
843
8e77275e 844 count = dev->key_count;
906bdc9f 845 binds = dev->binds;
8e77275e 846 def_binds = binds + count * IN_BINDTYPE_COUNT;
da767bd5 847
8e77275e 848 ret = DRV(dev->drv_id).clean_binds(dev->drv_data, binds, def_binds);
da767bd5 849 if (ret == 0) {
850 /* no useable binds */
906bdc9f 851 free(dev->binds);
852 dev->binds = NULL;
da767bd5 853 }
854 }
595491c4 855}
856
da767bd5 857void in_debug_dump(void)
858{
859 int i;
860
7ee5c389 861 lprintf("# drv probed binds name\n");
da767bd5 862 for (i = 0; i < IN_MAX_DEVS; i++) {
863 in_dev_t *d = &in_devices[i];
864 if (!d->probed && d->name == NULL && d->binds == NULL)
865 continue;
7ee5c389 866 lprintf("%d %3d %6c %5c %s\n", i, d->drv_id, d->probed ? 'y' : 'n',
da767bd5 867 d->binds ? 'y' : 'n', d->name);
868 }
869}
870
c4334e00 871/* stubs for drivers that choose not to implement something */
da767bd5 872
da767bd5 873static void in_def_free(void *drv_data) {}
da767bd5 874static void in_def_get_def_binds(int *binds) {}
c4334e00 875static int in_def_clean_binds(void *drv_data, int *b, int *db) { return 1; }
e9b29264 876static int in_def_get_config(void *drv_data, int what, int *val) { return -1; }
877static int in_def_set_config(void *drv_data, int what, int val) { return -1; }
7f022a8d 878static int in_def_update_keycode(void *drv_data, int *is_down) { return 0; }
c4334e00 879static int in_def_menu_translate(void *drv_data, int keycode) { return 0; }
e9b29264 880static int in_def_get_key_code(const char *key_name) { return -1; }
da767bd5 881static const char *in_def_get_key_name(int keycode) { return NULL; }
882
c4334e00 883#define CHECK_ADD_STUB(d, f) \
884 if (d.f == NULL) d.f = in_def_##f
da767bd5 885
c4334e00 886/* to be called by drivers */
887int in_register_driver(const in_drv_t *drv)
888{
889 int count_new = in_driver_count + 1;
890 in_drv_t *new_drivers;
da767bd5 891
c4334e00 892 new_drivers = realloc(in_drivers, count_new * sizeof(in_drivers[0]));
893 if (new_drivers == NULL) {
894 lprintf("input: in_register_driver OOM\n");
895 return -1;
da767bd5 896 }
897
c4334e00 898 memcpy(&new_drivers[in_driver_count], drv, sizeof(new_drivers[0]));
899
900 CHECK_ADD_STUB(new_drivers[in_driver_count], free);
901 CHECK_ADD_STUB(new_drivers[in_driver_count], get_def_binds);
902 CHECK_ADD_STUB(new_drivers[in_driver_count], clean_binds);
903 CHECK_ADD_STUB(new_drivers[in_driver_count], get_config);
904 CHECK_ADD_STUB(new_drivers[in_driver_count], set_config);
905 CHECK_ADD_STUB(new_drivers[in_driver_count], update_keycode);
906 CHECK_ADD_STUB(new_drivers[in_driver_count], menu_translate);
907 CHECK_ADD_STUB(new_drivers[in_driver_count], get_key_code);
908 CHECK_ADD_STUB(new_drivers[in_driver_count], get_key_name);
909 in_drivers = new_drivers;
910 in_driver_count = count_new;
911
912 return 0;
913}
914
915void in_init(void)
916{
917 in_drivers = NULL;
918 memset(in_devices, 0, sizeof(in_devices));
919 in_driver_count = 0;
920 in_dev_count = 0;
2258f158 921}
922
da767bd5 923#if 0
2258f158 924int main(void)
925{
34581c95 926 int ret;
927
2258f158 928 in_init();
929 in_probe();
930
595491c4 931 in_set_blocking(1);
932
933#if 1
934 while (1) {
935 int dev = 0, down;
936 ret = in_update_keycode(&dev, &down);
7ee5c389 937 lprintf("#%i: %i %i (%s)\n", dev, down, ret, in_get_key_name(dev, ret));
595491c4 938 }
939#else
2258f158 940 while (1) {
fce20e73 941 ret = in_menu_wait_any();
7ee5c389 942 lprintf("%08x\n", ret);
2258f158 943 }
595491c4 944#endif
2258f158 945
946 return 0;
947}
da767bd5 948#endif