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