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