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