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