joystick_linux.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /*************************************************************************/
  2. /* joystick_linux.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. //author: Andreas Haas <hondres, [email protected]>
  30. #ifdef JOYDEV_ENABLED
  31. #include "joystick_linux.h"
  32. #include <linux/input.h>
  33. #include <libudev.h>
  34. #include <unistd.h>
  35. #include <fcntl.h>
  36. #include <errno.h>
  37. #define LONG_BITS (sizeof(long) * 8)
  38. #define test_bit(nr, addr) (((1UL << ((nr) % LONG_BITS)) & ((addr)[(nr) / LONG_BITS])) != 0)
  39. #define NBITS(x) ((((x)-1)/LONG_BITS)+1)
  40. static const char* ignore_str = "/dev/input/js";
  41. joystick_linux::Joystick::Joystick() {
  42. fd = -1;
  43. dpad = 0;
  44. devpath = "";
  45. for (int i = 0; i < MAX_ABS; i++) {
  46. abs_info[i] = NULL;
  47. }
  48. }
  49. joystick_linux::Joystick::~Joystick() {
  50. for (int i = 0; i < MAX_ABS; i++) {
  51. if (abs_info[i]) {
  52. memdelete(abs_info[i]);
  53. }
  54. }
  55. }
  56. void joystick_linux::Joystick::reset() {
  57. dpad = 0;
  58. fd = -1;
  59. InputDefault::JoyAxis jx;
  60. jx.min = -1;
  61. jx.value = 0.0f;
  62. for (int i=0; i < MAX_ABS; i++) {
  63. abs_map[i] = -1;
  64. curr_axis[i] = jx;
  65. }
  66. }
  67. joystick_linux::joystick_linux(InputDefault *in)
  68. {
  69. exit_udev = false;
  70. input = in;
  71. joy_mutex = Mutex::create();
  72. joy_thread = Thread::create(joy_thread_func, this);
  73. }
  74. joystick_linux::~joystick_linux() {
  75. exit_udev = true;
  76. Thread::wait_to_finish(joy_thread);
  77. close_joystick();
  78. }
  79. void joystick_linux::joy_thread_func(void *p_user) {
  80. if (p_user) {
  81. joystick_linux* joy = (joystick_linux*) p_user;
  82. joy->run_joystick_thread();
  83. }
  84. return;
  85. }
  86. void joystick_linux::run_joystick_thread() {
  87. udev *_udev = udev_new();
  88. ERR_FAIL_COND(!_udev);
  89. enumerate_joysticks(_udev);
  90. monitor_joysticks(_udev);
  91. udev_unref(_udev);
  92. }
  93. void joystick_linux::enumerate_joysticks(udev *p_udev) {
  94. udev_enumerate *enumerate;
  95. udev_list_entry *devices, *dev_list_entry;
  96. udev_device *dev;
  97. enumerate = udev_enumerate_new(p_udev);
  98. udev_enumerate_add_match_subsystem(enumerate,"input");
  99. udev_enumerate_add_match_property(enumerate, "ID_INPUT_JOYSTICK", "1");
  100. udev_enumerate_scan_devices(enumerate);
  101. devices = udev_enumerate_get_list_entry(enumerate);
  102. udev_list_entry_foreach(dev_list_entry, devices) {
  103. const char* path = udev_list_entry_get_name(dev_list_entry);
  104. dev = udev_device_new_from_syspath(p_udev, path);
  105. const char* devnode = udev_device_get_devnode(dev);
  106. if (devnode) {
  107. String devnode_str = devnode;
  108. if (devnode_str.find(ignore_str) == -1) {
  109. joy_mutex->lock();
  110. open_joystick(devnode);
  111. joy_mutex->unlock();
  112. }
  113. }
  114. udev_device_unref(dev);
  115. }
  116. udev_enumerate_unref(enumerate);
  117. }
  118. void joystick_linux::monitor_joysticks(udev *p_udev) {
  119. udev_device *dev = NULL;
  120. udev_monitor *mon = udev_monitor_new_from_netlink(p_udev, "udev");
  121. udev_monitor_filter_add_match_subsystem_devtype(mon, "input", NULL);
  122. udev_monitor_enable_receiving(mon);
  123. int fd = udev_monitor_get_fd(mon);
  124. while (!exit_udev) {
  125. fd_set fds;
  126. struct timeval tv;
  127. int ret;
  128. FD_ZERO(&fds);
  129. FD_SET(fd, &fds);
  130. tv.tv_sec = 0;
  131. tv.tv_usec = 0;
  132. ret = select(fd+1, &fds, NULL, NULL, &tv);
  133. /* Check if our file descriptor has received data. */
  134. if (ret > 0 && FD_ISSET(fd, &fds)) {
  135. /* Make the call to receive the device.
  136. select() ensured that this will not block. */
  137. dev = udev_monitor_receive_device(mon);
  138. if (dev && udev_device_get_devnode(dev) != 0) {
  139. joy_mutex->lock();
  140. String action = udev_device_get_action(dev);
  141. const char* devnode = udev_device_get_devnode(dev);
  142. if (devnode) {
  143. String devnode_str = devnode;
  144. if (devnode_str.find(ignore_str) == -1) {
  145. if (action == "add")
  146. open_joystick(devnode);
  147. else if (String(action) == "remove")
  148. close_joystick(get_joy_from_path(devnode));
  149. }
  150. }
  151. udev_device_unref(dev);
  152. joy_mutex->unlock();
  153. }
  154. }
  155. usleep(50000);
  156. }
  157. //printf("exit udev\n");
  158. udev_monitor_unref(mon);
  159. }
  160. int joystick_linux::get_free_joy_slot() const {
  161. for (int i = 0; i < JOYSTICKS_MAX; i++) {
  162. if (joysticks[i].fd == -1) return i;
  163. }
  164. return -1;
  165. }
  166. int joystick_linux::get_joy_from_path(String p_path) const {
  167. for (int i = 0; i < JOYSTICKS_MAX; i++) {
  168. if (joysticks[i].devpath == p_path) {
  169. return i;
  170. }
  171. }
  172. return -2;
  173. }
  174. void joystick_linux::close_joystick(int p_id) {
  175. if (p_id == -1) {
  176. for (int i=0; i<JOYSTICKS_MAX; i++) {
  177. close_joystick(i);
  178. };
  179. return;
  180. }
  181. else if (p_id < 0) return;
  182. Joystick &joy = joysticks[p_id];
  183. if (joy.fd != -1) {
  184. close(joy.fd);
  185. joy.fd = -1;
  186. input->joy_connection_changed(p_id, false, "");
  187. };
  188. };
  189. static String _hex_str(uint8_t p_byte) {
  190. static const char* dict = "0123456789abcdef";
  191. char ret[3];
  192. ret[2] = 0;
  193. ret[0] = dict[p_byte>>4];
  194. ret[1] = dict[p_byte & 0xF];
  195. return ret;
  196. };
  197. void joystick_linux::setup_joystick_properties(int p_id) {
  198. Joystick* joy = &joysticks[p_id];
  199. unsigned long keybit[NBITS(KEY_MAX)] = { 0 };
  200. unsigned long absbit[NBITS(ABS_MAX)] = { 0 };
  201. int num_buttons = 0;
  202. int num_axes = 0;
  203. if ((ioctl(joy->fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) < 0) ||
  204. (ioctl(joy->fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) < 0)) {
  205. return;
  206. }
  207. for (int i = BTN_JOYSTICK; i < KEY_MAX; ++i) {
  208. if (test_bit(i, keybit)) {
  209. joy->key_map[i] = num_buttons++;
  210. }
  211. }
  212. for (int i = BTN_MISC; i < BTN_JOYSTICK; ++i) {
  213. if (test_bit(i, keybit)) {
  214. joy->key_map[i] = num_buttons++;
  215. }
  216. }
  217. for (int i = 0; i < ABS_MISC; ++i) {
  218. /* Skip hats */
  219. if (i == ABS_HAT0X) {
  220. i = ABS_HAT3Y;
  221. continue;
  222. }
  223. if (test_bit(i, absbit)) {
  224. joy->abs_map[i] = num_axes++;
  225. joy->abs_info[i] = memnew(input_absinfo);
  226. if (ioctl(joy->fd, EVIOCGABS(i), joy->abs_info[i]) < 0) {
  227. memdelete(joy->abs_info[i]);
  228. joy->abs_info[i] = NULL;
  229. }
  230. }
  231. }
  232. }
  233. void joystick_linux::open_joystick(const char *p_path) {
  234. int joy_num = get_free_joy_slot();
  235. int fd = open(p_path, O_RDONLY | O_NONBLOCK);
  236. if (fd != -1 && joy_num != -1) {
  237. unsigned long evbit[NBITS(EV_MAX)] = { 0 };
  238. unsigned long keybit[NBITS(KEY_MAX)] = { 0 };
  239. unsigned long absbit[NBITS(ABS_MAX)] = { 0 };
  240. if ((ioctl(fd, EVIOCGBIT(0, sizeof(evbit)), evbit) < 0) ||
  241. (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) < 0) ||
  242. (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) < 0)) {
  243. close(fd);
  244. return;
  245. }
  246. //check if the device supports basic gamepad events, prevents certain keyboards from
  247. //being detected as joysticks
  248. if (!(test_bit(EV_KEY, evbit) && test_bit(EV_ABS, evbit) &&
  249. (test_bit(ABS_X, absbit) || test_bit(ABS_Y, absbit) || test_bit(ABS_HAT0X, absbit) ||
  250. test_bit(ABS_GAS, absbit) || test_bit(ABS_RUDDER, absbit)) &&
  251. (test_bit(BTN_A, keybit) || test_bit(BTN_THUMBL, keybit) ||
  252. test_bit(BTN_TRIGGER, keybit) || test_bit(BTN_1, keybit)))) {
  253. close(fd);
  254. return;
  255. }
  256. char uid[128];
  257. char namebuf[128];
  258. String name = "";
  259. input_id inpid;
  260. if (ioctl(fd, EVIOCGNAME(sizeof(namebuf)), namebuf) >= 0) {
  261. name = namebuf;
  262. }
  263. if (ioctl(fd, EVIOCGID, &inpid) < 0) {
  264. close(fd);
  265. return;
  266. }
  267. joysticks[joy_num].reset();
  268. Joystick &joy = joysticks[joy_num];
  269. joy.fd = fd;
  270. joy.devpath = String(p_path);
  271. setup_joystick_properties(joy_num);
  272. sprintf(uid, "%04x%04x", __bswap_16(inpid.bustype), 0);
  273. if (inpid.vendor && inpid.product && inpid.version) {
  274. uint16_t vendor = __bswap_16(inpid.vendor);
  275. uint16_t product = __bswap_16(inpid.product);
  276. uint16_t version = __bswap_16(inpid.version);
  277. sprintf(uid + String(uid).length(), "%04x%04x%04x%04x%04x%04x", vendor,0,product,0,version,0);
  278. input->joy_connection_changed(joy_num, true, name, uid);
  279. }
  280. else {
  281. String uidname = uid;
  282. int uidlen = MIN(name.length(), 11);
  283. for (int i=0; i<uidlen; i++) {
  284. uidname = uidname + _hex_str(name[i]);
  285. }
  286. uidname += "00";
  287. input->joy_connection_changed(joy_num, true, name, uidname);
  288. }
  289. }
  290. }
  291. InputDefault::JoyAxis joystick_linux::axis_correct(const input_absinfo *p_abs, int p_value) const {
  292. int min = p_abs->minimum;
  293. int max = p_abs->maximum;
  294. InputDefault::JoyAxis jx;
  295. if (min < 0) {
  296. jx.min = -1;
  297. if (p_value < 0) {
  298. jx.value = (float) -p_value / min;
  299. }
  300. jx.value = (float) p_value / max;
  301. }
  302. if (min == 0) {
  303. jx.min = 0;
  304. jx.value = 0.0f + (float) p_value / max;
  305. }
  306. return jx;
  307. }
  308. uint32_t joystick_linux::process_joysticks(uint32_t p_event_id) {
  309. if (joy_mutex->try_lock() != OK) {
  310. return p_event_id;
  311. }
  312. for (int i=0; i<JOYSTICKS_MAX; i++) {
  313. if (joysticks[i].fd == -1) continue;
  314. input_event events[32];
  315. Joystick* joy = &joysticks[i];
  316. int len;
  317. while ((len = read(joy->fd, events, (sizeof events))) > 0) {
  318. len /= sizeof(events[0]);
  319. for (int j = 0; j < len; j++) {
  320. input_event &ev = events[j];
  321. switch (ev.type) {
  322. case EV_KEY:
  323. p_event_id = input->joy_button(p_event_id, i, joy->key_map[ev.code], ev.value);
  324. break;
  325. case EV_ABS:
  326. switch (ev.code) {
  327. case ABS_HAT0X:
  328. if (ev.value != 0) {
  329. if (ev.value < 0) joy->dpad |= InputDefault::HAT_MASK_LEFT;
  330. else joy->dpad |= InputDefault::HAT_MASK_RIGHT;
  331. }
  332. else joy->dpad &= ~(InputDefault::HAT_MASK_LEFT | InputDefault::HAT_MASK_RIGHT);
  333. p_event_id = input->joy_hat(p_event_id, i, joy->dpad);
  334. break;
  335. case ABS_HAT0Y:
  336. if (ev.value != 0) {
  337. if (ev.value < 0) joy->dpad |= InputDefault::HAT_MASK_UP;
  338. else joy->dpad |= InputDefault::HAT_MASK_DOWN;
  339. }
  340. else joy->dpad &= ~(InputDefault::HAT_MASK_UP | InputDefault::HAT_MASK_DOWN);
  341. p_event_id = input->joy_hat(p_event_id, i, joy->dpad);
  342. break;
  343. default:
  344. if (joy->abs_map[ev.code] != -1 && joy->abs_info[ev.code]) {
  345. InputDefault::JoyAxis value = axis_correct(joy->abs_info[ev.code], ev.value);
  346. joy->curr_axis[joy->abs_map[ev.code]] = value;
  347. }
  348. break;
  349. }
  350. break;
  351. }
  352. }
  353. }
  354. for (int j = 0; j < MAX_ABS; j++) {
  355. int index = joy->abs_map[j];
  356. if (index != -1) {
  357. p_event_id = input->joy_axis(p_event_id, i, index, joy->curr_axis[index]);
  358. }
  359. }
  360. }
  361. joy_mutex->unlock();
  362. return p_event_id;
  363. }
  364. #endif