input.c 21 KB

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