joystick_linux.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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-2015 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 __linux__
  31. #include "joystick_linux.h"
  32. #include "print_string.h"
  33. #include <libevdev-1.0/libevdev/libevdev.h>
  34. #include <libudev.h>
  35. #include <unistd.h>
  36. #include <fcntl.h>
  37. #include <errno.h>
  38. #include <cstring>
  39. static const char* ignore_str = "/dev/input/js";
  40. joystick_linux::Joystick::Joystick() {
  41. fd = -1;
  42. dpad = 0;
  43. }
  44. void joystick_linux::Joystick::reset() {
  45. num_buttons = 0;
  46. num_axes = 0;
  47. dpad = 0;
  48. fd = -1;
  49. for (int i=0; i < MAX_ABS; i++) {
  50. abs_map[i] = -1;
  51. }
  52. }
  53. joystick_linux::joystick_linux(InputDefault *in)
  54. {
  55. exit_udev = false;
  56. input = in;
  57. joy_mutex = Mutex::create();
  58. joy_thread = Thread::create(joy_thread_func, this);
  59. }
  60. joystick_linux::~joystick_linux() {
  61. exit_udev = true;
  62. Thread::wait_to_finish(joy_thread);
  63. close_joystick();
  64. }
  65. void joystick_linux::joy_thread_func(void *p_user) {
  66. if (p_user) {
  67. joystick_linux* joy = (joystick_linux*) p_user;
  68. joy->run_joystick_thread();
  69. }
  70. return;
  71. }
  72. void joystick_linux::run_joystick_thread() {
  73. udev *_udev = udev_new();
  74. ERR_FAIL_COND(!_udev);
  75. enumerate_joysticks(_udev);
  76. monitor_joysticks(_udev);
  77. udev_unref(_udev);
  78. }
  79. void joystick_linux::enumerate_joysticks(udev *p_udev) {
  80. udev_enumerate *enumerate;
  81. udev_list_entry *devices, *dev_list_entry;
  82. udev_device *dev;
  83. enumerate = udev_enumerate_new(p_udev);
  84. udev_enumerate_add_match_subsystem(enumerate,"input");
  85. udev_enumerate_add_match_property(enumerate, "ID_INPUT_JOYSTICK", "1");
  86. udev_enumerate_scan_devices(enumerate);
  87. devices = udev_enumerate_get_list_entry(enumerate);
  88. udev_list_entry_foreach(dev_list_entry, devices) {
  89. const char* path = udev_list_entry_get_name(dev_list_entry);
  90. dev = udev_device_new_from_syspath(p_udev, path);
  91. const char* devnode = udev_device_get_devnode(dev);
  92. if (devnode != NULL && strstr(devnode, ignore_str) == NULL) {
  93. joy_mutex->lock();
  94. open_joystick(devnode);
  95. joy_mutex->unlock();
  96. }
  97. udev_device_unref(dev);
  98. }
  99. udev_enumerate_unref(enumerate);
  100. }
  101. void joystick_linux::monitor_joysticks(udev *p_udev) {
  102. udev_device *dev = NULL;
  103. udev_monitor *mon = udev_monitor_new_from_netlink(p_udev, "udev");
  104. udev_monitor_filter_add_match_subsystem_devtype(mon, "input", NULL);
  105. udev_monitor_enable_receiving(mon);
  106. int fd = udev_monitor_get_fd(mon);
  107. while (!exit_udev) {
  108. fd_set fds;
  109. struct timeval tv;
  110. int ret;
  111. FD_ZERO(&fds);
  112. FD_SET(fd, &fds);
  113. tv.tv_sec = 0;
  114. tv.tv_usec = 0;
  115. ret = select(fd+1, &fds, NULL, NULL, &tv);
  116. /* Check if our file descriptor has received data. */
  117. if (ret > 0 && FD_ISSET(fd, &fds)) {
  118. /* Make the call to receive the device.
  119. select() ensured that this will not block. */
  120. dev = udev_monitor_receive_device(mon);
  121. if (dev && udev_device_get_devnode(dev) != 0) {
  122. joy_mutex->lock();
  123. const char* action = udev_device_get_action(dev);
  124. const char* devnode = udev_device_get_devnode(dev);
  125. if (strstr(devnode, ignore_str) == NULL) {
  126. if (strcmp(action, "add") == 0)
  127. open_joystick(devnode);
  128. else if (strcmp(action, "remove") == 0)
  129. close_joystick(get_joy_from_path(devnode));
  130. }
  131. udev_device_unref(dev);
  132. joy_mutex->unlock();
  133. }
  134. }
  135. usleep(50000);
  136. }
  137. //printf("exit udev\n");
  138. udev_monitor_unref(mon);
  139. }
  140. int joystick_linux::get_free_joy_slot() const {
  141. for (int i = 0; i < JOYSTICKS_MAX; i++) {
  142. if (joysticks[i].fd == -1) return i;
  143. }
  144. return -1;
  145. }
  146. int joystick_linux::get_joy_from_path(String p_path) const {
  147. for (int i = 0; i < JOYSTICKS_MAX; i++) {
  148. if (joysticks[i].devpath == p_path) {
  149. return i;
  150. }
  151. }
  152. return -2;
  153. }
  154. void joystick_linux::close_joystick(int p_id) {
  155. if (p_id == -1) {
  156. for (int i=0; i<JOYSTICKS_MAX; i++) {
  157. close_joystick(i);
  158. };
  159. return;
  160. }
  161. else if (p_id < 0) return;
  162. Joystick &joy = joysticks[p_id];
  163. if (joy.fd != -1) {
  164. libevdev_free(joy.dev);
  165. close(joy.fd);
  166. joy.fd = -1;
  167. input->joy_connection_changed(p_id, false, "");
  168. };
  169. };
  170. static String _hex_str(uint8_t p_byte) {
  171. static const char* dict = "0123456789abcdef";
  172. char ret[3];
  173. ret[2] = 0;
  174. ret[0] = dict[p_byte>>4];
  175. ret[1] = dict[p_byte & 0xF];
  176. return ret;
  177. };
  178. void joystick_linux::setup_joystick_properties(int p_id) {
  179. Joystick* joy = &joysticks[p_id];
  180. libevdev* dev = joy->dev;
  181. for (int i = BTN_JOYSTICK; i < KEY_MAX; ++i) {
  182. if (libevdev_has_event_code(dev, EV_KEY, i)) {
  183. joy->key_map[i] = joy->num_buttons++;
  184. }
  185. }
  186. for (int i = BTN_MISC; i < BTN_JOYSTICK; ++i) {
  187. if (libevdev_has_event_code(dev, EV_KEY, i)) {
  188. joy->key_map[i] = joy->num_buttons++;
  189. }
  190. }
  191. for (int i = 0; i < ABS_MISC; ++i) {
  192. /* Skip hats */
  193. if (i == ABS_HAT0X) {
  194. i = ABS_HAT3Y;
  195. continue;
  196. }
  197. if (libevdev_has_event_code(dev, EV_ABS, i)) {
  198. joy->abs_map[i] = joy->num_axes++;
  199. }
  200. }
  201. }
  202. void joystick_linux::open_joystick(const char *p_path) {
  203. int joy_num = get_free_joy_slot();
  204. int fd = open(p_path, O_RDONLY | O_NONBLOCK);
  205. if (fd != -1 && joy_num != -1) {
  206. int rc = libevdev_new_from_fd(fd, &joysticks[joy_num].dev);
  207. if (rc < 0) {
  208. fprintf(stderr, "Failed to init libevdev (%s)\n", strerror(-rc));
  209. return;
  210. }
  211. libevdev *dev = joysticks[joy_num].dev;
  212. //check if the device supports basic gamepad events, prevents certain keyboards from
  213. //being detected as joysticks
  214. if (libevdev_has_event_type(dev, EV_ABS) && libevdev_has_event_type(dev, EV_KEY) &&
  215. (libevdev_has_event_code(dev, EV_KEY, BTN_A) || libevdev_has_event_code(dev, EV_KEY, BTN_THUMBL) || libevdev_has_event_code(dev, EV_KEY, BTN_TOP))) {
  216. char uid[128];
  217. String name = libevdev_get_name(dev);
  218. uint16_t bus = __bswap_16(libevdev_get_id_bustype(dev));
  219. uint16_t vendor = __bswap_16(libevdev_get_id_vendor(dev));
  220. uint16_t product = __bswap_16(libevdev_get_id_product(dev));
  221. uint16_t version = __bswap_16(libevdev_get_id_version(dev));
  222. joysticks[joy_num].reset();
  223. Joystick &joy = joysticks[joy_num];
  224. joy.fd = fd;
  225. joy.devpath = String(p_path);
  226. setup_joystick_properties(joy_num);
  227. sprintf(uid, "%04x%04x", bus, 0);
  228. if (vendor && product && version) {
  229. sprintf(uid + String(uid).length(), "%04x%04x%04x%04x%04x%04x", vendor,0,product,0,version,0);
  230. input->joy_connection_changed(joy_num, true, name, uid);
  231. }
  232. else {
  233. String uidname = uid;
  234. int uidlen = MIN(name.length(), 11);
  235. for (int i=0; i<uidlen; i++) {
  236. uidname = uidname + _hex_str(name[i]);
  237. }
  238. uidname += "00";
  239. input->joy_connection_changed(joy_num, true, name, uidname);
  240. }
  241. }
  242. else {
  243. //device is not a gamepad, clean up
  244. libevdev_free(dev);
  245. close(fd);
  246. }
  247. }
  248. }
  249. InputDefault::JoyAxis joystick_linux::axis_correct(const input_absinfo *p_abs, int p_value) const {
  250. int min = p_abs->minimum;
  251. int max = p_abs->maximum;
  252. InputDefault::JoyAxis jx;
  253. if (min < 0) {
  254. jx.min = -1;
  255. if (p_value < 0) {
  256. jx.value = (float) -p_value / min;
  257. }
  258. jx.value = (float) p_value / max;
  259. }
  260. if (min == 0) {
  261. jx.min = 0;
  262. jx.value = 0.0f + (float) p_value / max;
  263. }
  264. return jx;
  265. }
  266. uint32_t joystick_linux::process_joysticks(uint32_t p_event_id) {
  267. if (joy_mutex->try_lock() != OK) {
  268. return p_event_id;
  269. }
  270. for (int i=0; i<JOYSTICKS_MAX; i++) {
  271. if (joysticks[i].fd == -1) continue;
  272. input_event ev;
  273. Joystick* joy = &joysticks[i];
  274. libevdev* dev = joy->dev;
  275. int rc = 1;
  276. rc = libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, &ev);
  277. if (rc < 0 && rc != -EAGAIN) {
  278. continue;
  279. }
  280. while (rc == LIBEVDEV_READ_STATUS_SYNC || rc == LIBEVDEV_READ_STATUS_SUCCESS) {
  281. switch (ev.type) {
  282. case EV_KEY:
  283. p_event_id = input->joy_button(p_event_id, i, joy->key_map[ev.code], ev.value);
  284. break;
  285. case EV_ABS:
  286. switch (ev.code) {
  287. case ABS_HAT0X:
  288. if (ev.value != 0) {
  289. if (ev.value < 0) joy->dpad |= InputDefault::HAT_MASK_LEFT;
  290. else joy->dpad |= InputDefault::HAT_MASK_RIGHT;
  291. }
  292. else joy->dpad &= ~(InputDefault::HAT_MASK_LEFT | InputDefault::HAT_MASK_RIGHT);
  293. p_event_id = input->joy_hat(p_event_id, i, joy->dpad);
  294. break;
  295. case ABS_HAT0Y:
  296. if (ev.value != 0) {
  297. if (ev.value < 0) joy->dpad |= InputDefault::HAT_MASK_UP;
  298. else joy->dpad |= InputDefault::HAT_MASK_DOWN;
  299. }
  300. else joy->dpad &= ~(InputDefault::HAT_MASK_UP | InputDefault::HAT_MASK_DOWN);
  301. p_event_id = input->joy_hat(p_event_id, i, joy->dpad);
  302. break;
  303. default:
  304. if (joy->abs_map[ev.code] != -1) {
  305. InputDefault::JoyAxis value = axis_correct(libevdev_get_abs_info(dev, ev.code), ev.value);
  306. p_event_id = input->joy_axis(p_event_id, i, joy->abs_map[ev.code], value);
  307. }
  308. break;
  309. }
  310. break;
  311. }
  312. rc = libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, &ev);
  313. }
  314. }
  315. joy_mutex->unlock();
  316. return p_event_id;
  317. }
  318. #endif