input.c 18 KB

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