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