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