2258f158 |
1 | #include <stdio.h> |
2 | #include <stdlib.h> |
3 | #include <string.h> |
4 | |
fce20e73 |
5 | #include "common.h" |
2258f158 |
6 | #include "input.h" |
34581c95 |
7 | #include "../linux/in_evdev.h" |
13b692eb |
8 | #include "../gp2x/in_gp2x.h" |
2258f158 |
9 | |
10 | typedef struct |
11 | { |
12 | int drv_id; |
7f022a8d |
13 | int drv_fd_hnd; |
2258f158 |
14 | void *drv_data; |
2258f158 |
15 | char *name; |
906bdc9f |
16 | int *binds; |
2258f158 |
17 | int probed:1; |
2258f158 |
18 | } in_dev_t; |
19 | |
da767bd5 |
20 | static in_drv_t in_drivers[IN_DRVID_COUNT]; |
2258f158 |
21 | static in_dev_t in_devices[IN_MAX_DEVS]; |
22 | static int in_dev_count = 0; |
13b692eb |
23 | static int in_have_async_devs = 0; |
2258f158 |
24 | |
da767bd5 |
25 | #define DRV(id) in_drivers[(unsigned)(id) < IN_DRVID_COUNT ? (id) : 0] |
26 | |
13b692eb |
27 | |
2258f158 |
28 | static int in_bind_count(int drv_id) |
29 | { |
da767bd5 |
30 | int count = DRV(drv_id).get_bind_count(); |
2258f158 |
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 | { |
da767bd5 |
39 | int count, *binds; |
2258f158 |
40 | |
41 | count = in_bind_count(drv_id); |
da767bd5 |
42 | if (count <= 0) |
2258f158 |
43 | return NULL; |
2258f158 |
44 | |
da767bd5 |
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; |
2258f158 |
53 | } |
54 | |
55 | static void in_free(in_dev_t *dev) |
56 | { |
da767bd5 |
57 | if (dev->probed) |
58 | DRV(dev->drv_id).free(dev->drv_data); |
2258f158 |
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 | |
13b692eb |
67 | /* to be called by drivers |
68 | * async devices must set drv_fd_hnd to -1 */ |
7f022a8d |
69 | void in_register(const char *nname, int drv_id, int drv_fd_hnd, void *drv_data) |
2258f158 |
70 | { |
da767bd5 |
71 | int i, ret, dupe_count = 0, *binds; |
2258f158 |
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; |
7f022a8d |
124 | in_devices[i].drv_fd_hnd = drv_fd_hnd; |
2258f158 |
125 | in_devices[i].drv_data = drv_data; |
da767bd5 |
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 | } |
2258f158 |
135 | } |
136 | |
137 | void in_probe(void) |
138 | { |
139 | int i; |
13b692eb |
140 | |
141 | in_have_async_devs = 0; |
2258f158 |
142 | for (i = 0; i < in_dev_count; i++) |
143 | in_devices[i].probed = 0; |
144 | |
da767bd5 |
145 | for (i = 1; i < IN_DRVID_COUNT; i++) |
146 | in_drivers[i].probe(); |
2258f158 |
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 | } |
13b692eb |
157 | |
158 | continue; |
2258f158 |
159 | } |
13b692eb |
160 | |
161 | if (in_devices[i].probed && in_devices[i].drv_fd_hnd == -1) |
162 | in_have_async_devs = 1; |
2258f158 |
163 | } |
13b692eb |
164 | |
165 | if (in_have_async_devs) |
166 | printf("input: async-only devices detected..\n"); |
2258f158 |
167 | } |
168 | |
7f022a8d |
169 | /* async update */ |
2258f158 |
170 | int in_update(void) |
171 | { |
172 | int i, result = 0; |
173 | |
174 | for (i = 0; i < in_dev_count; i++) { |
906bdc9f |
175 | in_dev_t *dev = &in_devices[i]; |
906bdc9f |
176 | if (dev->probed && dev->binds != NULL) { |
177 | switch (dev->drv_id) { |
da767bd5 |
178 | #ifdef IN_EVDEV |
595491c4 |
179 | case IN_DRVID_EVDEV: |
86883f7f |
180 | result |= in_evdev_update(dev->drv_data, dev->binds); |
595491c4 |
181 | break; |
13b692eb |
182 | #endif |
183 | #ifdef IN_GP2X |
184 | case IN_DRVID_GP2X: |
185 | result |= in_gp2x_update(dev->drv_data, dev->binds); |
186 | break; |
da767bd5 |
187 | #endif |
595491c4 |
188 | } |
2258f158 |
189 | } |
190 | } |
191 | |
192 | return result; |
193 | } |
194 | |
906bdc9f |
195 | static int menu_key_state = 0; |
196 | |
595491c4 |
197 | void in_set_blocking(int is_blocking) |
198 | { |
13b692eb |
199 | int i, ret; |
595491c4 |
200 | |
13b692eb |
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 | } |
595491c4 |
207 | } |
906bdc9f |
208 | |
209 | menu_key_state = 0; |
13b692eb |
210 | |
906bdc9f |
211 | /* flush events */ |
13b692eb |
212 | do { |
213 | ret = in_update_keycode(NULL, NULL, 0); |
214 | } while (ret >= 0); |
595491c4 |
215 | } |
216 | |
7f022a8d |
217 | /* TODO: move.. */ |
218 | #include <unistd.h> |
13b692eb |
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 | } |
7f022a8d |
259 | |
34581c95 |
260 | /* |
13b692eb |
261 | * wait for a press, always return some keycode or -1 on timeout or error |
34581c95 |
262 | */ |
906bdc9f |
263 | int in_update_keycode(int *dev_id_out, int *is_down_out, int timeout_ms) |
34581c95 |
264 | { |
fce20e73 |
265 | int result = 0, dev_id = 0, is_down, result_menu; |
7f022a8d |
266 | int fds_hnds[IN_MAX_DEVS]; |
267 | int i, ret, count = 0; |
268 | in_drv_t *drv; |
269 | |
13b692eb |
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 | |
7f022a8d |
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 | } |
34581c95 |
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 | |
7f022a8d |
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 | } |
595491c4 |
301 | |
7f022a8d |
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); |
13b692eb |
313 | return -1; |
7f022a8d |
314 | } |
315 | |
316 | if (ret == 0) |
13b692eb |
317 | return -1; /* timeout */ |
7f022a8d |
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) { |
906bdc9f |
326 | dev_id = i; |
327 | break; |
595491c4 |
328 | } |
329 | } |
7f022a8d |
330 | |
331 | drv = &DRV(in_devices[dev_id].drv_id); |
332 | result = drv->update_keycode(in_devices[dev_id].drv_data, &is_down); |
333 | |
13b692eb |
334 | /* update_keycode() might return -1 when some not interesting |
7f022a8d |
335 | * event happened, like sync event for evdev. |
336 | * XXX: timeout restarts.. */ |
13b692eb |
337 | if (result == -1) |
7f022a8d |
338 | goto again; |
34581c95 |
339 | |
13b692eb |
340 | finish: |
906bdc9f |
341 | /* keep track of menu key state, to allow mixing |
fce20e73 |
342 | * in_update_keycode() and in_menu_wait_any() calls */ |
7f022a8d |
343 | result_menu = drv->menu_translate(result); |
906bdc9f |
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; |
34581c95 |
355 | return result; |
356 | } |
357 | |
fce20e73 |
358 | /* same as above, only return bitfield of PBTN_* */ |
359 | int in_menu_wait_any(int timeout_ms) |
595491c4 |
360 | { |
906bdc9f |
361 | int keys_old = menu_key_state; |
595491c4 |
362 | |
363 | while (1) |
364 | { |
da767bd5 |
365 | int code, is_down = 0, dev_id = 0; |
595491c4 |
366 | |
906bdc9f |
367 | code = in_update_keycode(&dev_id, &is_down, timeout_ms); |
13b692eb |
368 | if (code >= 0) |
369 | code = DRV(in_devices[dev_id].drv_id).menu_translate(code); |
595491c4 |
370 | |
fce20e73 |
371 | if (timeout_ms >= 0) |
906bdc9f |
372 | break; |
13b692eb |
373 | if (code < 0) |
906bdc9f |
374 | continue; |
375 | if (keys_old != menu_key_state) |
595491c4 |
376 | break; |
377 | } |
378 | |
906bdc9f |
379 | return menu_key_state; |
595491c4 |
380 | } |
381 | |
fce20e73 |
382 | /* wait for menu input, do autorepeat */ |
049a6b3e |
383 | int in_menu_wait(int interesting, int autorep_delay_ms) |
fce20e73 |
384 | { |
385 | static int inp_prev = 0; |
6c263f30 |
386 | static int repeats = 0; |
387 | int ret, release = 0, wait = 666; |
fce20e73 |
388 | |
6c263f30 |
389 | if (repeats) |
049a6b3e |
390 | wait = autorep_delay_ms; |
fce20e73 |
391 | |
6c263f30 |
392 | ret = in_menu_wait_any(wait); |
393 | if (ret == inp_prev) |
394 | repeats++; |
fce20e73 |
395 | |
396 | while (!(ret & interesting)) { |
397 | ret = in_menu_wait_any(-1); |
398 | release = 1; |
399 | } |
400 | |
6c263f30 |
401 | if (release || ret != inp_prev) |
fce20e73 |
402 | repeats = 0; |
6c263f30 |
403 | |
fce20e73 |
404 | inp_prev = ret; |
405 | |
6c263f30 |
406 | /* we don't need diagonals in menus */ |
fce20e73 |
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 | |
da767bd5 |
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 | |
049a6b3e |
442 | const char *in_get_dev_name(int dev_id, int must_be_active, int skip_pfix) |
da767bd5 |
443 | { |
049a6b3e |
444 | const char *name, *tmp; |
445 | |
da767bd5 |
446 | if (dev_id < 0 || dev_id >= IN_MAX_DEVS) |
447 | return NULL; |
448 | |
fce20e73 |
449 | if (must_be_active && !in_devices[dev_id].probed) |
450 | return NULL; |
049a6b3e |
451 | |
452 | name = in_devices[dev_id].name; |
453 | tmp = strchr(name, ':'); |
454 | if (tmp != NULL) |
455 | name = tmp + 1; |
456 | |
457 | return name; |
da767bd5 |
458 | } |
459 | |
906bdc9f |
460 | /* never returns NULL */ |
595491c4 |
461 | const char *in_get_key_name(int dev_id, int keycode) |
462 | { |
906bdc9f |
463 | static char xname[16]; |
464 | const char *name; |
465 | |
595491c4 |
466 | if (dev_id < 0 || dev_id >= IN_MAX_DEVS) |
467 | return "Unkn0"; |
da767bd5 |
468 | |
906bdc9f |
469 | name = DRV(in_devices[dev_id].drv_id).get_key_name(keycode); |
470 | if (name != NULL) |
471 | return name; |
472 | |
473 | /* assume scancode */ |
474 | if ((keycode >= '0' && keycode <= '9') || (keycode >= 'a' && keycode <= 'z') |
475 | || (keycode >= 'A' && keycode <= 'Z')) |
476 | sprintf(xname, "%c", keycode); |
477 | else |
478 | sprintf(xname, "\\x%02X", keycode); |
479 | return xname; |
480 | } |
481 | |
482 | int in_bind_key(int dev_id, int keycode, int mask, int force_unbind) |
483 | { |
484 | int ret, count; |
485 | in_dev_t *dev; |
486 | |
487 | if (dev_id < 0 || dev_id >= IN_MAX_DEVS) |
488 | return -1; |
489 | dev = &in_devices[dev_id]; |
490 | |
491 | if (dev->binds == NULL) { |
492 | if (force_unbind) |
493 | return 0; |
494 | dev->binds = in_alloc_binds(dev->drv_id); |
495 | if (dev->binds == NULL) |
496 | return -1; |
497 | } |
498 | |
499 | count = in_bind_count(dev->drv_id); |
500 | if (keycode < 0 || keycode >= count) |
501 | return -1; |
502 | |
503 | if (force_unbind) |
504 | dev->binds[keycode] &= ~mask; |
505 | else |
506 | dev->binds[keycode] ^= mask; |
507 | |
508 | ret = DRV(dev->drv_id).clean_binds(dev->drv_data, dev->binds); |
509 | if (ret == 0) { |
510 | free(dev->binds); |
511 | dev->binds = NULL; |
512 | } |
513 | |
514 | return 0; |
da767bd5 |
515 | } |
516 | |
517 | /* returns device id, or -1 on error */ |
518 | int in_config_parse_dev(const char *name) |
519 | { |
520 | int drv_id = -1, i; |
521 | |
522 | for (i = 0; i < IN_DRVID_COUNT; i++) { |
523 | int len = strlen(in_drivers[i].prefix); |
524 | if (strncmp(name, in_drivers[i].prefix, len) == 0) { |
525 | drv_id = i; |
526 | break; |
527 | } |
528 | } |
529 | |
530 | if (drv_id < 0) { |
531 | printf("input: missing driver for %s\n", name); |
532 | return -1; |
533 | } |
534 | |
535 | for (i = 0; i < in_dev_count; i++) |
536 | { |
537 | if (in_devices[i].name == NULL) |
538 | continue; |
539 | if (strcmp(in_devices[i].name, name) == 0) |
540 | return i; |
541 | } |
542 | |
543 | if (i >= IN_MAX_DEVS) |
544 | { |
545 | /* try to find unused device */ |
546 | for (i = 0; i < IN_MAX_DEVS; i++) |
547 | if (in_devices[i].name == NULL) break; |
548 | if (i >= IN_MAX_DEVS) { |
549 | printf("input: too many devices, can't add %s\n", name); |
550 | return -1; |
551 | } |
552 | } |
553 | |
554 | memset(&in_devices[i], 0, sizeof(in_devices[i])); |
555 | |
556 | in_devices[i].name = strdup(name); |
557 | if (in_devices[i].name == NULL) |
558 | return -1; |
559 | |
560 | if (i + 1 > in_dev_count) |
561 | in_dev_count = i + 1; |
562 | in_devices[i].drv_id = drv_id; |
563 | |
564 | return i; |
565 | } |
566 | |
6c263f30 |
567 | /* |
568 | * To reduce size of game specific configs, default binds are not saved. |
569 | * So we mark default binds in in_config_start(), override them in in_config_bind_key(), |
570 | * and restore whatever default binds are left in in_config_end(). |
571 | */ |
da767bd5 |
572 | void in_config_start(void) |
573 | { |
574 | int i; |
575 | |
576 | /* mark all default binds, so they get overwritten by func below */ |
577 | for (i = 0; i < IN_MAX_DEVS; i++) { |
578 | int n, count, *binds, *def_binds; |
579 | |
906bdc9f |
580 | binds = in_devices[i].binds; |
581 | if (binds == NULL) |
da767bd5 |
582 | continue; |
583 | |
584 | count = in_bind_count(in_devices[i].drv_id); |
da767bd5 |
585 | def_binds = binds + count; |
586 | |
587 | for (n = 0; n < count; n++) |
588 | if (binds[n] == def_binds[n]) |
589 | binds[n] = -1; |
590 | } |
591 | } |
592 | |
593 | int in_config_bind_key(int dev_id, const char *key, int binds) |
594 | { |
906bdc9f |
595 | int count, kc; |
596 | in_dev_t *dev; |
da767bd5 |
597 | |
598 | if (dev_id < 0 || dev_id >= IN_MAX_DEVS) |
599 | return -1; |
906bdc9f |
600 | dev = &in_devices[dev_id]; |
da767bd5 |
601 | |
906bdc9f |
602 | count = in_bind_count(dev->drv_id); |
603 | |
604 | /* maybe a raw code? */ |
605 | if (key[0] == '\\' && key[1] == 'x') { |
606 | char *p = NULL; |
607 | kc = (int)strtoul(key + 2, &p, 16); |
608 | if (p == NULL || *p != 0) |
609 | kc = -1; |
610 | } |
611 | else { |
612 | /* device specific key name */ |
613 | if (dev->binds == NULL) { |
614 | dev->binds = in_alloc_binds(dev->drv_id); |
615 | if (dev->binds == NULL) |
616 | return -1; |
617 | in_config_start(); |
618 | } |
619 | |
620 | kc = DRV(dev->drv_id).get_key_code(key); |
621 | if (kc < 0 && strlen(key) == 1) { |
622 | /* assume scancode */ |
623 | kc = key[0]; |
624 | } |
da767bd5 |
625 | } |
626 | |
906bdc9f |
627 | if (kc < 0 || kc >= count) { |
da767bd5 |
628 | printf("input: bad key: %s\n", key); |
629 | return -1; |
595491c4 |
630 | } |
631 | |
906bdc9f |
632 | if (dev->binds[kc] == -1) |
633 | dev->binds[kc] = 0; |
634 | dev->binds[kc] |= binds; |
da767bd5 |
635 | |
636 | return 0; |
637 | } |
638 | |
639 | void in_config_end(void) |
640 | { |
641 | int i; |
642 | |
643 | for (i = 0; i < IN_MAX_DEVS; i++) { |
644 | int n, ret, count, *binds, *def_binds; |
906bdc9f |
645 | in_dev_t *dev = &in_devices[i]; |
da767bd5 |
646 | |
906bdc9f |
647 | if (dev->binds == NULL) |
da767bd5 |
648 | continue; |
649 | |
906bdc9f |
650 | count = in_bind_count(dev->drv_id); |
651 | binds = dev->binds; |
da767bd5 |
652 | def_binds = binds + count; |
653 | |
654 | for (n = 0; n < count; n++) |
655 | if (binds[n] == -1) |
656 | binds[n] = def_binds[n]; |
657 | |
906bdc9f |
658 | if (dev->drv_data == NULL) |
da767bd5 |
659 | continue; |
660 | |
906bdc9f |
661 | ret = DRV(dev->drv_id).clean_binds(dev->drv_data, binds); |
da767bd5 |
662 | if (ret == 0) { |
663 | /* no useable binds */ |
906bdc9f |
664 | free(dev->binds); |
665 | dev->binds = NULL; |
da767bd5 |
666 | } |
667 | } |
595491c4 |
668 | } |
669 | |
da767bd5 |
670 | void in_debug_dump(void) |
671 | { |
672 | int i; |
673 | |
674 | printf("# drv probed binds name\n"); |
675 | for (i = 0; i < IN_MAX_DEVS; i++) { |
676 | in_dev_t *d = &in_devices[i]; |
677 | if (!d->probed && d->name == NULL && d->binds == NULL) |
678 | continue; |
679 | printf("%d %3d %6c %5c %s\n", i, d->drv_id, d->probed ? 'y' : 'n', |
680 | d->binds ? 'y' : 'n', d->name); |
681 | } |
682 | } |
683 | |
684 | /* handlers for unknown/not_preset drivers */ |
685 | |
686 | static void in_def_probe(void) {} |
687 | static void in_def_free(void *drv_data) {} |
688 | static int in_def_get_bind_count(void) { return 0; } |
689 | static void in_def_get_def_binds(int *binds) {} |
690 | static int in_def_clean_binds(void *drv_data, int *binds) { return 0; } |
691 | static void in_def_set_blocking(void *data, int y) {} |
7f022a8d |
692 | static int in_def_update_keycode(void *drv_data, int *is_down) { return 0; } |
da767bd5 |
693 | static int in_def_menu_translate(int keycode) { return keycode; } |
694 | static int in_def_get_key_code(const char *key_name) { return 0; } |
695 | static const char *in_def_get_key_name(int keycode) { return NULL; } |
696 | |
2258f158 |
697 | void in_init(void) |
698 | { |
da767bd5 |
699 | int i; |
700 | |
701 | memset(in_drivers, 0, sizeof(in_drivers)); |
2258f158 |
702 | memset(in_devices, 0, sizeof(in_devices)); |
703 | in_dev_count = 0; |
da767bd5 |
704 | |
705 | for (i = 0; i < IN_DRVID_COUNT; i++) { |
706 | in_drivers[i].prefix = "none:"; |
707 | in_drivers[i].probe = in_def_probe; |
708 | in_drivers[i].free = in_def_free; |
709 | in_drivers[i].get_bind_count = in_def_get_bind_count; |
710 | in_drivers[i].get_def_binds = in_def_get_def_binds; |
711 | in_drivers[i].clean_binds = in_def_clean_binds; |
712 | in_drivers[i].set_blocking = in_def_set_blocking; |
7f022a8d |
713 | in_drivers[i].update_keycode = in_def_update_keycode; |
da767bd5 |
714 | in_drivers[i].menu_translate = in_def_menu_translate; |
715 | in_drivers[i].get_key_code = in_def_get_key_code; |
716 | in_drivers[i].get_key_name = in_def_get_key_name; |
717 | } |
718 | |
13b692eb |
719 | #ifdef IN_GP2X |
720 | in_gp2x_init(&in_drivers[IN_DRVID_GP2X]); |
721 | #endif |
da767bd5 |
722 | #ifdef IN_EVDEV |
723 | in_evdev_init(&in_drivers[IN_DRVID_EVDEV]); |
724 | #endif |
2258f158 |
725 | } |
726 | |
da767bd5 |
727 | #if 0 |
2258f158 |
728 | int main(void) |
729 | { |
34581c95 |
730 | int ret; |
731 | |
2258f158 |
732 | in_init(); |
733 | in_probe(); |
734 | |
595491c4 |
735 | in_set_blocking(1); |
736 | |
737 | #if 1 |
738 | while (1) { |
739 | int dev = 0, down; |
740 | ret = in_update_keycode(&dev, &down); |
741 | printf("#%i: %i %i (%s)\n", dev, down, ret, in_get_key_name(dev, ret)); |
742 | } |
743 | #else |
2258f158 |
744 | while (1) { |
fce20e73 |
745 | ret = in_menu_wait_any(); |
34581c95 |
746 | printf("%08x\n", ret); |
2258f158 |
747 | } |
595491c4 |
748 | #endif |
2258f158 |
749 | |
750 | return 0; |
751 | } |
da767bd5 |
752 | #endif |