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