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