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; |
2258f158 |
46 | |
c4334e00 |
47 | #define DRV(id) in_drivers[id] |
da767bd5 |
48 | |
13b692eb |
49 | |
8e77275e |
50 | static 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++) { |
7ceadd99 |
63 | if (defbinds[i].bit == 0 && defbinds[i].btype == 0 |
64 | && defbinds[i].bit == 0) |
215aec76 |
65 | break; |
66 | binds[IN_BIND_OFFS(defbinds[i].code, defbinds[i].btype)] = |
67 | 1 << defbinds[i].bit; |
68 | } |
69 | |
70 | /* always have a copy of defbinds */ |
71 | memcpy(binds + key_count * IN_BINDTYPE_COUNT, binds, |
72 | sizeof(binds[0]) * key_count * IN_BINDTYPE_COUNT); |
73 | } |
da767bd5 |
74 | |
75 | return binds; |
2258f158 |
76 | } |
77 | |
23fb16c8 |
78 | static void in_unprobe(in_dev_t *dev) |
2258f158 |
79 | { |
da767bd5 |
80 | if (dev->probed) |
81 | DRV(dev->drv_id).free(dev->drv_data); |
2258f158 |
82 | dev->probed = 0; |
83 | dev->drv_data = NULL; |
23fb16c8 |
84 | } |
85 | |
86 | static void in_free(in_dev_t *dev) |
87 | { |
88 | in_unprobe(dev); |
2258f158 |
89 | free(dev->name); |
90 | dev->name = NULL; |
91 | free(dev->binds); |
92 | dev->binds = NULL; |
93 | } |
94 | |
13b692eb |
95 | /* to be called by drivers |
96 | * async devices must set drv_fd_hnd to -1 */ |
c4334e00 |
97 | void in_register(const char *nname, int drv_fd_hnd, void *drv_data, |
e9b29264 |
98 | int key_count, const char * const *key_names, int combos) |
2258f158 |
99 | { |
da767bd5 |
100 | int i, ret, dupe_count = 0, *binds; |
2258f158 |
101 | char name[256], *name_end, *tmp; |
102 | |
103 | strncpy(name, nname, sizeof(name)); |
104 | name[sizeof(name)-12] = 0; |
105 | name_end = name + strlen(name); |
106 | |
107 | for (i = 0; i < in_dev_count; i++) |
108 | { |
109 | if (in_devices[i].name == NULL) |
110 | continue; |
111 | if (strcmp(in_devices[i].name, name) == 0) |
112 | { |
113 | if (in_devices[i].probed) { |
114 | dupe_count++; |
115 | sprintf(name_end, " [%d]", dupe_count); |
116 | continue; |
117 | } |
118 | goto update; |
119 | } |
120 | } |
121 | |
122 | if (i >= IN_MAX_DEVS) |
123 | { |
124 | /* try to find unused device */ |
125 | for (i = 0; i < IN_MAX_DEVS; i++) |
126 | if (!in_devices[i].probed) break; |
127 | if (i >= IN_MAX_DEVS) { |
7ee5c389 |
128 | lprintf("input: too many devices, can't add %s\n", name); |
2258f158 |
129 | return; |
130 | } |
131 | in_free(&in_devices[i]); |
132 | } |
133 | |
134 | tmp = strdup(name); |
135 | if (tmp == NULL) |
136 | return; |
137 | |
c4334e00 |
138 | binds = in_alloc_binds(in_probe_dev_id, key_count); |
2258f158 |
139 | if (binds == NULL) { |
140 | free(tmp); |
141 | return; |
142 | } |
143 | |
144 | in_devices[i].name = tmp; |
145 | in_devices[i].binds = binds; |
8e77275e |
146 | in_devices[i].key_count = key_count; |
2258f158 |
147 | if (i + 1 > in_dev_count) |
148 | in_dev_count = i + 1; |
149 | |
7ee5c389 |
150 | lprintf("input: new device #%d \"%s\"\n", i, name); |
2258f158 |
151 | update: |
152 | in_devices[i].probed = 1; |
2c600560 |
153 | in_devices[i].does_combos = combos; |
c4334e00 |
154 | in_devices[i].drv_id = in_probe_dev_id; |
7f022a8d |
155 | in_devices[i].drv_fd_hnd = drv_fd_hnd; |
e9b29264 |
156 | in_devices[i].key_names = key_names; |
2258f158 |
157 | in_devices[i].drv_data = drv_data; |
da767bd5 |
158 | |
159 | if (in_devices[i].binds != NULL) { |
c4334e00 |
160 | ret = DRV(in_probe_dev_id).clean_binds(drv_data, in_devices[i].binds, |
8e77275e |
161 | in_devices[i].binds + key_count * IN_BINDTYPE_COUNT); |
da767bd5 |
162 | if (ret == 0) { |
163 | /* no useable binds */ |
164 | free(in_devices[i].binds); |
165 | in_devices[i].binds = NULL; |
166 | } |
167 | } |
2258f158 |
168 | } |
169 | |
8e77275e |
170 | /* key combo handling, to be called by drivers that support it. |
171 | * Only care about IN_BINDTYPE_EMU */ |
172 | void in_combos_find(const int *binds, int last_key, int *combo_keys, int *combo_acts) |
2c600560 |
173 | { |
174 | int act, u; |
175 | |
176 | *combo_keys = *combo_acts = 0; |
177 | for (act = 0; act < sizeof(binds[0]) * 8; act++) |
178 | { |
179 | int keyc = 0; |
180 | for (u = 0; u <= last_key; u++) |
8e77275e |
181 | if (binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)] & (1 << act)) |
2c600560 |
182 | keyc++; |
183 | |
184 | if (keyc > 1) |
185 | { |
186 | // loop again and mark those keys and actions as combo |
187 | for (u = 0; u <= last_key; u++) |
188 | { |
8e77275e |
189 | if (binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)] & (1 << act)) { |
2c600560 |
190 | *combo_keys |= 1 << u; |
191 | *combo_acts |= 1 << act; |
192 | } |
193 | } |
194 | } |
195 | } |
196 | } |
197 | |
8e77275e |
198 | int in_combos_do(int keys, const int *binds, int last_key, int combo_keys, int combo_acts) |
2c600560 |
199 | { |
200 | int i, ret = 0; |
201 | |
202 | for (i = 0; i <= last_key; i++) |
203 | { |
8e77275e |
204 | int acts, acts_c, u; |
205 | |
2c600560 |
206 | if (!(keys & (1 << i))) |
207 | continue; |
208 | |
8e77275e |
209 | acts = binds[IN_BIND_OFFS(i, IN_BINDTYPE_EMU)]; |
2c600560 |
210 | if (!acts) |
211 | continue; |
212 | |
8e77275e |
213 | if (!(combo_keys & (1 << i))) { |
2c600560 |
214 | ret |= acts; |
8e77275e |
215 | continue; |
216 | } |
217 | |
218 | acts_c = acts & combo_acts; |
219 | u = last_key; |
220 | if (acts_c) { |
221 | // let's try to find the other one |
222 | for (u = i + 1; u <= last_key; u++) |
223 | if ( (keys & (1 << u)) && (binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)] & acts_c) ) { |
224 | ret |= acts_c & binds[IN_BIND_OFFS(u, IN_BINDTYPE_EMU)]; |
225 | keys &= ~((1 << i) | (1 << u)); |
226 | break; |
227 | } |
228 | } |
229 | // add non-combo actions if combo ones were not found |
230 | if (u >= last_key) |
231 | ret |= acts & ~combo_acts; |
2c600560 |
232 | } |
233 | |
234 | return ret; |
235 | } |
236 | |
2258f158 |
237 | void in_probe(void) |
238 | { |
239 | int i; |
13b692eb |
240 | |
241 | in_have_async_devs = 0; |
23fb16c8 |
242 | menu_key_state = 0; |
243 | menu_last_used_dev = 0; |
244 | |
245 | for (i = 0; i < in_dev_count; i++) |
246 | in_unprobe(&in_devices[i]); |
2258f158 |
247 | |
c4334e00 |
248 | for (i = 0; i < in_driver_count; i++) { |
249 | in_probe_dev_id = i; |
da767bd5 |
250 | in_drivers[i].probe(); |
c4334e00 |
251 | } |
2258f158 |
252 | |
253 | /* get rid of devs without binds and probes */ |
254 | for (i = 0; i < in_dev_count; i++) { |
255 | if (!in_devices[i].probed && in_devices[i].binds == NULL) { |
256 | in_dev_count--; |
257 | if (i < in_dev_count) { |
258 | free(in_devices[i].name); |
259 | memmove(&in_devices[i], &in_devices[i+1], |
260 | (in_dev_count - i) * sizeof(in_devices[0])); |
261 | } |
13b692eb |
262 | |
263 | continue; |
2258f158 |
264 | } |
13b692eb |
265 | |
266 | if (in_devices[i].probed && in_devices[i].drv_fd_hnd == -1) |
267 | in_have_async_devs = 1; |
2258f158 |
268 | } |
13b692eb |
269 | |
270 | if (in_have_async_devs) |
7ee5c389 |
271 | lprintf("input: async-only devices detected..\n"); |
e9b29264 |
272 | |
273 | in_debug_dump(); |
2258f158 |
274 | } |
275 | |
7f022a8d |
276 | /* async update */ |
8e77275e |
277 | int in_update(int *result) |
2258f158 |
278 | { |
8e77275e |
279 | int i, ret = 0; |
2258f158 |
280 | |
281 | for (i = 0; i < in_dev_count; i++) { |
906bdc9f |
282 | in_dev_t *dev = &in_devices[i]; |
c4334e00 |
283 | if (dev->probed && dev->binds != NULL) |
284 | ret |= DRV(dev->drv_id).update(dev->drv_data, dev->binds, result); |
2258f158 |
285 | } |
286 | |
8e77275e |
287 | return ret; |
2258f158 |
288 | } |
289 | |
df77913a |
290 | static in_dev_t *get_dev(int dev_id) |
291 | { |
292 | if (dev_id < 0 || dev_id >= IN_MAX_DEVS) |
293 | return NULL; |
294 | |
295 | return &in_devices[dev_id]; |
296 | } |
297 | |
298 | int in_update_analog(int dev_id, int axis_id, int *result) |
299 | { |
300 | in_dev_t *dev = get_dev(dev_id); |
301 | |
302 | if (dev == NULL || !dev->probed) |
303 | return -1; |
304 | |
305 | return DRV(dev->drv_id).update_analog(dev->drv_data, axis_id, result); |
306 | } |
307 | |
13b692eb |
308 | static int in_update_kc_async(int *dev_id_out, int *is_down_out, int timeout_ms) |
309 | { |
13b692eb |
310 | int i, is_down, result; |
4ab30ad4 |
311 | unsigned int ticks; |
13b692eb |
312 | |
4ab30ad4 |
313 | ticks = plat_get_ticks_ms(); |
13b692eb |
314 | |
315 | while (1) |
316 | { |
317 | for (i = 0; i < in_dev_count; i++) { |
318 | in_dev_t *d = &in_devices[i]; |
319 | if (!d->probed) |
320 | continue; |
321 | |
322 | result = DRV(d->drv_id).update_keycode(d->drv_data, &is_down); |
323 | if (result == -1) |
324 | continue; |
325 | |
326 | if (dev_id_out) |
327 | *dev_id_out = i; |
328 | if (is_down_out) |
329 | *is_down_out = is_down; |
330 | return result; |
331 | } |
332 | |
4ab30ad4 |
333 | if (timeout_ms >= 0 && (int)(plat_get_ticks_ms() - ticks) > timeout_ms) |
334 | break; |
13b692eb |
335 | |
4ab30ad4 |
336 | plat_sleep_ms(10); |
13b692eb |
337 | } |
338 | |
339 | return -1; |
340 | } |
7f022a8d |
341 | |
34581c95 |
342 | /* |
13b692eb |
343 | * wait for a press, always return some keycode or -1 on timeout or error |
34581c95 |
344 | */ |
9934732a |
345 | int in_update_keycode(int *dev_id_out, int *is_down_out, char *charcode, int timeout_ms) |
34581c95 |
346 | { |
4ab30ad4 |
347 | int result = -1, dev_id = 0, is_down, result_menu; |
7f022a8d |
348 | int fds_hnds[IN_MAX_DEVS]; |
349 | int i, ret, count = 0; |
7fd581f4 |
350 | in_drv_t *drv = NULL; |
4ab30ad4 |
351 | unsigned int ticks; |
7f022a8d |
352 | |
13b692eb |
353 | if (in_have_async_devs) { |
354 | result = in_update_kc_async(&dev_id, &is_down, timeout_ms); |
355 | if (result == -1) |
356 | return -1; |
357 | drv = &DRV(in_devices[dev_id].drv_id); |
358 | goto finish; |
359 | } |
360 | |
4ab30ad4 |
361 | ticks = plat_get_ticks_ms(); |
362 | |
7f022a8d |
363 | for (i = 0; i < in_dev_count; i++) { |
364 | if (in_devices[i].probed) |
365 | fds_hnds[count++] = in_devices[i].drv_fd_hnd; |
366 | } |
34581c95 |
367 | |
368 | if (count == 0) { |
369 | /* don't deadlock, fail */ |
7ee5c389 |
370 | lprintf("input: failed to find devices to read\n"); |
34581c95 |
371 | exit(1); |
372 | } |
373 | |
4ab30ad4 |
374 | while (1) |
7f022a8d |
375 | { |
4ab30ad4 |
376 | ret = plat_wait_event(fds_hnds, count, timeout_ms); |
377 | if (ret < 0) |
378 | break; |
7f022a8d |
379 | |
4ab30ad4 |
380 | for (i = 0; i < in_dev_count; i++) { |
381 | if (in_devices[i].drv_fd_hnd == ret) { |
382 | dev_id = i; |
383 | break; |
384 | } |
7f022a8d |
385 | } |
386 | |
4ab30ad4 |
387 | drv = &DRV(in_devices[dev_id].drv_id); |
388 | result = drv->update_keycode(in_devices[dev_id].drv_data, &is_down); |
4ab30ad4 |
389 | if (result >= 0) |
906bdc9f |
390 | break; |
4ab30ad4 |
391 | |
23fb16c8 |
392 | if (result == -2) { |
393 | lprintf("input: \"%s\" errored out, removing.\n", in_devices[dev_id].name); |
394 | in_unprobe(&in_devices[dev_id]); |
395 | break; |
396 | } |
397 | |
4ab30ad4 |
398 | if (timeout_ms >= 0) { |
399 | unsigned int ticks2 = plat_get_ticks_ms(); |
400 | timeout_ms -= ticks2 - ticks; |
401 | ticks = ticks2; |
402 | if (timeout_ms <= 0) |
403 | break; |
595491c4 |
404 | } |
405 | } |
7f022a8d |
406 | |
74065be8 |
407 | if (result < 0) |
4ab30ad4 |
408 | return -1; |
13b692eb |
409 | finish: |
906bdc9f |
410 | /* keep track of menu key state, to allow mixing |
fce20e73 |
411 | * in_update_keycode() and in_menu_wait_any() calls */ |
9934732a |
412 | result_menu = drv->menu_translate(in_devices[dev_id].drv_data, result, charcode); |
906bdc9f |
413 | if (result_menu != 0) { |
414 | if (is_down) |
415 | menu_key_state |= result_menu; |
416 | else |
417 | menu_key_state &= ~result_menu; |
418 | } |
419 | |
420 | if (dev_id_out != NULL) |
421 | *dev_id_out = dev_id; |
422 | if (is_down_out != NULL) |
423 | *is_down_out = is_down; |
34581c95 |
424 | return result; |
425 | } |
426 | |
fce20e73 |
427 | /* same as above, only return bitfield of PBTN_* */ |
9934732a |
428 | int in_menu_wait_any(char *charcode, int timeout_ms) |
595491c4 |
429 | { |
906bdc9f |
430 | int keys_old = menu_key_state; |
595491c4 |
431 | |
432 | while (1) |
433 | { |
da767bd5 |
434 | int code, is_down = 0, dev_id = 0; |
595491c4 |
435 | |
9934732a |
436 | code = in_update_keycode(&dev_id, &is_down, charcode, timeout_ms); |
13b692eb |
437 | if (code < 0) |
595491c4 |
438 | break; |
e9b29264 |
439 | |
440 | if (keys_old != menu_key_state) { |
441 | menu_last_used_dev = dev_id; |
442 | break; |
443 | } |
595491c4 |
444 | } |
445 | |
906bdc9f |
446 | return menu_key_state; |
595491c4 |
447 | } |
448 | |
fce20e73 |
449 | /* wait for menu input, do autorepeat */ |
9934732a |
450 | int in_menu_wait(int interesting, char *charcode, int autorep_delay_ms) |
fce20e73 |
451 | { |
452 | static int inp_prev = 0; |
6c263f30 |
453 | static int repeats = 0; |
2c600560 |
454 | int ret, release = 0, wait = 450; |
fce20e73 |
455 | |
6c263f30 |
456 | if (repeats) |
049a6b3e |
457 | wait = autorep_delay_ms; |
fce20e73 |
458 | |
9934732a |
459 | ret = in_menu_wait_any(charcode, wait); |
6c263f30 |
460 | if (ret == inp_prev) |
461 | repeats++; |
fce20e73 |
462 | |
463 | while (!(ret & interesting)) { |
9934732a |
464 | ret = in_menu_wait_any(charcode, -1); |
fce20e73 |
465 | release = 1; |
466 | } |
467 | |
6c263f30 |
468 | if (release || ret != inp_prev) |
fce20e73 |
469 | repeats = 0; |
6c263f30 |
470 | |
fce20e73 |
471 | inp_prev = ret; |
472 | |
6c263f30 |
473 | /* we don't need diagonals in menus */ |
fce20e73 |
474 | if ((ret & PBTN_UP) && (ret & PBTN_LEFT)) ret &= ~PBTN_LEFT; |
475 | if ((ret & PBTN_UP) && (ret & PBTN_RIGHT)) ret &= ~PBTN_RIGHT; |
476 | if ((ret & PBTN_DOWN) && (ret & PBTN_LEFT)) ret &= ~PBTN_LEFT; |
477 | if ((ret & PBTN_DOWN) && (ret & PBTN_RIGHT)) ret &= ~PBTN_RIGHT; |
478 | |
479 | return ret; |
480 | } |
481 | |
da767bd5 |
482 | const int *in_get_dev_binds(int dev_id) |
483 | { |
df77913a |
484 | in_dev_t *dev = get_dev(dev_id); |
da767bd5 |
485 | |
df77913a |
486 | return dev ? dev->binds : NULL; |
da767bd5 |
487 | } |
488 | |
489 | const int *in_get_dev_def_binds(int dev_id) |
490 | { |
df77913a |
491 | in_dev_t *dev = get_dev(dev_id); |
492 | if (dev == NULL) |
da767bd5 |
493 | return NULL; |
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 | menu_key_state = 0; |
544 | |
545 | /* flush events */ |
546 | do { |
9934732a |
547 | ret = in_update_keycode(NULL, NULL, NULL, 0); |
e9b29264 |
548 | } while (ret >= 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) { |
7ee5c389 |
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) { |
7ee5c389 |
797 | lprintf("input: too many devices, can't add %s\n", name); |
da767bd5 |
798 | return -1; |
799 | } |
800 | } |
801 | |
802 | memset(&in_devices[i], 0, sizeof(in_devices[i])); |
803 | |
804 | in_devices[i].name = strdup(name); |
805 | if (in_devices[i].name == NULL) |
806 | return -1; |
807 | |
23fb16c8 |
808 | in_devices[i].key_names = DRV(drv_id).get_key_names(&in_devices[i].key_count); |
8e77275e |
809 | in_devices[i].drv_id = drv_id; |
810 | |
da767bd5 |
811 | if (i + 1 > in_dev_count) |
812 | in_dev_count = i + 1; |
da767bd5 |
813 | |
814 | return i; |
815 | } |
816 | |
8e77275e |
817 | int in_config_bind_key(int dev_id, const char *key, int acts, int bind_type) |
da767bd5 |
818 | { |
906bdc9f |
819 | in_dev_t *dev; |
8e77275e |
820 | int i, offs, kc; |
da767bd5 |
821 | |
df77913a |
822 | dev = get_dev(dev_id); |
823 | if (dev == NULL || bind_type >= IN_BINDTYPE_COUNT) |
da767bd5 |
824 | return -1; |
825 | |
906bdc9f |
826 | /* maybe a raw code? */ |
827 | if (key[0] == '\\' && key[1] == 'x') { |
828 | char *p = NULL; |
829 | kc = (int)strtoul(key + 2, &p, 16); |
830 | if (p == NULL || *p != 0) |
831 | kc = -1; |
832 | } |
833 | else { |
834 | /* device specific key name */ |
835 | if (dev->binds == NULL) { |
8e77275e |
836 | dev->binds = in_alloc_binds(dev->drv_id, dev->key_count); |
906bdc9f |
837 | if (dev->binds == NULL) |
838 | return -1; |
906bdc9f |
839 | } |
840 | |
e9b29264 |
841 | kc = -1; |
842 | if (dev->key_names != NULL) { |
843 | for (i = 0; i < dev->key_count; i++) { |
844 | const char *k = dev->key_names[i]; |
845 | if (k != NULL && strcasecmp(k, key) == 0) { |
846 | kc = i; |
847 | break; |
848 | } |
849 | } |
850 | } |
851 | |
852 | if (kc < 0) |
853 | kc = DRV(dev->drv_id).get_key_code(key); |
906bdc9f |
854 | if (kc < 0 && strlen(key) == 1) { |
855 | /* assume scancode */ |
856 | kc = key[0]; |
857 | } |
da767bd5 |
858 | } |
859 | |
8e77275e |
860 | if (kc < 0 || kc >= dev->key_count) { |
7ee5c389 |
861 | lprintf("input: bad key: %s\n", key); |
da767bd5 |
862 | return -1; |
595491c4 |
863 | } |
864 | |
8e77275e |
865 | if (bind_type == IN_BINDTYPE_NONE) { |
866 | for (i = 0; i < IN_BINDTYPE_COUNT; i++) |
867 | dev->binds[IN_BIND_OFFS(kc, i)] = 0; |
868 | return 0; |
869 | } |
da767bd5 |
870 | |
8e77275e |
871 | offs = IN_BIND_OFFS(kc, bind_type); |
872 | if (dev->binds[offs] == -1) |
873 | dev->binds[offs] = 0; |
874 | dev->binds[offs] |= acts; |
da767bd5 |
875 | return 0; |
876 | } |
877 | |
45a39652 |
878 | void in_clean_binds(void) |
da767bd5 |
879 | { |
880 | int i; |
881 | |
882 | for (i = 0; i < IN_MAX_DEVS; i++) { |
45a39652 |
883 | int ret, count, *binds, *def_binds; |
906bdc9f |
884 | in_dev_t *dev = &in_devices[i]; |
da767bd5 |
885 | |
a4025790 |
886 | if (dev->binds == NULL || !dev->probed) |
da767bd5 |
887 | continue; |
888 | |
8e77275e |
889 | count = dev->key_count; |
906bdc9f |
890 | binds = dev->binds; |
8e77275e |
891 | def_binds = binds + count * IN_BINDTYPE_COUNT; |
da767bd5 |
892 | |
8e77275e |
893 | ret = DRV(dev->drv_id).clean_binds(dev->drv_data, binds, def_binds); |
da767bd5 |
894 | if (ret == 0) { |
895 | /* no useable binds */ |
906bdc9f |
896 | free(dev->binds); |
897 | dev->binds = NULL; |
da767bd5 |
898 | } |
899 | } |
595491c4 |
900 | } |
901 | |
da767bd5 |
902 | void in_debug_dump(void) |
903 | { |
904 | int i; |
905 | |
7ee5c389 |
906 | lprintf("# drv probed binds name\n"); |
da767bd5 |
907 | for (i = 0; i < IN_MAX_DEVS; i++) { |
908 | in_dev_t *d = &in_devices[i]; |
909 | if (!d->probed && d->name == NULL && d->binds == NULL) |
910 | continue; |
7ee5c389 |
911 | lprintf("%d %3d %6c %5c %s\n", i, d->drv_id, d->probed ? 'y' : 'n', |
da767bd5 |
912 | d->binds ? 'y' : 'n', d->name); |
913 | } |
914 | } |
915 | |
c4334e00 |
916 | /* stubs for drivers that choose not to implement something */ |
da767bd5 |
917 | |
da767bd5 |
918 | static void in_def_free(void *drv_data) {} |
c4334e00 |
919 | static int in_def_clean_binds(void *drv_data, int *b, int *db) { return 1; } |
e9b29264 |
920 | static int in_def_get_config(void *drv_data, int what, int *val) { return -1; } |
921 | static int in_def_set_config(void *drv_data, int what, int val) { return -1; } |
df77913a |
922 | static int in_def_update_analog(void *drv_data, int axis_id, int *result) { return -1; } |
7f022a8d |
923 | static int in_def_update_keycode(void *drv_data, int *is_down) { return 0; } |
9934732a |
924 | static int in_def_menu_translate(void *drv_data, int keycode, char *ccode) { return 0; } |
e9b29264 |
925 | static int in_def_get_key_code(const char *key_name) { return -1; } |
da767bd5 |
926 | static const char *in_def_get_key_name(int keycode) { return NULL; } |
927 | |
c4334e00 |
928 | #define CHECK_ADD_STUB(d, f) \ |
929 | if (d.f == NULL) d.f = in_def_##f |
da767bd5 |
930 | |
c4334e00 |
931 | /* to be called by drivers */ |
215aec76 |
932 | int in_register_driver(const in_drv_t *drv, const struct in_default_bind *defbinds) |
c4334e00 |
933 | { |
934 | int count_new = in_driver_count + 1; |
935 | in_drv_t *new_drivers; |
da767bd5 |
936 | |
c4334e00 |
937 | new_drivers = realloc(in_drivers, count_new * sizeof(in_drivers[0])); |
938 | if (new_drivers == NULL) { |
939 | lprintf("input: in_register_driver OOM\n"); |
940 | return -1; |
da767bd5 |
941 | } |
942 | |
c4334e00 |
943 | memcpy(&new_drivers[in_driver_count], drv, sizeof(new_drivers[0])); |
944 | |
945 | CHECK_ADD_STUB(new_drivers[in_driver_count], free); |
c4334e00 |
946 | CHECK_ADD_STUB(new_drivers[in_driver_count], clean_binds); |
947 | CHECK_ADD_STUB(new_drivers[in_driver_count], get_config); |
948 | CHECK_ADD_STUB(new_drivers[in_driver_count], set_config); |
df77913a |
949 | CHECK_ADD_STUB(new_drivers[in_driver_count], update_analog); |
c4334e00 |
950 | CHECK_ADD_STUB(new_drivers[in_driver_count], update_keycode); |
951 | CHECK_ADD_STUB(new_drivers[in_driver_count], menu_translate); |
952 | CHECK_ADD_STUB(new_drivers[in_driver_count], get_key_code); |
953 | CHECK_ADD_STUB(new_drivers[in_driver_count], get_key_name); |
215aec76 |
954 | if (defbinds != NULL) |
955 | new_drivers[in_driver_count].defbinds = defbinds; |
c4334e00 |
956 | in_drivers = new_drivers; |
957 | in_driver_count = count_new; |
958 | |
959 | return 0; |
960 | } |
961 | |
962 | void in_init(void) |
963 | { |
964 | in_drivers = NULL; |
965 | memset(in_devices, 0, sizeof(in_devices)); |
966 | in_driver_count = 0; |
967 | in_dev_count = 0; |
2258f158 |
968 | } |
969 | |
da767bd5 |
970 | #if 0 |
2258f158 |
971 | int main(void) |
972 | { |
34581c95 |
973 | int ret; |
974 | |
2258f158 |
975 | in_init(); |
976 | in_probe(); |
977 | |
595491c4 |
978 | in_set_blocking(1); |
979 | |
980 | #if 1 |
981 | while (1) { |
982 | int dev = 0, down; |
983 | ret = in_update_keycode(&dev, &down); |
7ee5c389 |
984 | lprintf("#%i: %i %i (%s)\n", dev, down, ret, in_get_key_name(dev, ret)); |
595491c4 |
985 | } |
986 | #else |
2258f158 |
987 | while (1) { |
fce20e73 |
988 | ret = in_menu_wait_any(); |
7ee5c389 |
989 | lprintf("%08x\n", ret); |
2258f158 |
990 | } |
595491c4 |
991 | #endif |
2258f158 |
992 | |
993 | return 0; |
994 | } |
da767bd5 |
995 | #endif |