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