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