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