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