joypad_linux.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. /**************************************************************************/
  2. /* joypad_linux.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifdef JOYDEV_ENABLED
  31. #include "joypad_linux.h"
  32. #include "core/os/os.h"
  33. #include <dirent.h>
  34. #include <errno.h>
  35. #include <fcntl.h>
  36. #include <linux/input.h>
  37. #include <unistd.h>
  38. #ifdef UDEV_ENABLED
  39. #ifdef SOWRAP_ENABLED
  40. #include "libudev-so_wrap.h"
  41. #else
  42. #include <libudev.h>
  43. #endif
  44. #endif
  45. #define LONG_BITS (sizeof(long) * 8)
  46. #define test_bit(nr, addr) (((1UL << ((nr) % LONG_BITS)) & ((addr)[(nr) / LONG_BITS])) != 0)
  47. #define NBITS(x) ((((x) - 1) / LONG_BITS) + 1)
  48. #ifdef UDEV_ENABLED
  49. static const char *ignore_str = "/dev/input/js";
  50. #endif
  51. // On Linux with Steam Input Xbox 360 devices have an index appended to their device name, this index is
  52. // the Steam Input gamepad index
  53. #define VALVE_GAMEPAD_NAME_PREFIX "Microsoft X-Box 360 pad "
  54. // IDs used by Steam Input virtual controllers.
  55. // See https://partner.steamgames.com/doc/features/steam_controller/steam_input_gamepad_emulation_bestpractices
  56. #define VALVE_GAMEPAD_VID 0x28DE
  57. #define VALVE_GAMEPAD_PID 0x11FF
  58. JoypadLinux::Joypad::~Joypad() {
  59. for (int i = 0; i < MAX_ABS; i++) {
  60. if (abs_info[i]) {
  61. memdelete(abs_info[i]);
  62. }
  63. }
  64. }
  65. void JoypadLinux::Joypad::reset() {
  66. dpad = 0;
  67. fd = -1;
  68. for (int i = 0; i < MAX_ABS; i++) {
  69. abs_map[i] = -1;
  70. curr_axis[i] = 0;
  71. }
  72. events.clear();
  73. }
  74. JoypadLinux::JoypadLinux(Input *in) {
  75. #ifdef UDEV_ENABLED
  76. if (OS::get_singleton()->is_sandboxed()) {
  77. // Linux binaries in sandboxes / containers need special handling because
  78. // libudev doesn't work there. So we need to fallback to manual parsing
  79. // of /dev/input in such case.
  80. use_udev = false;
  81. print_verbose("JoypadLinux: udev enabled, but detected incompatible sandboxed mode. Falling back to /dev/input to detect joypads.");
  82. }
  83. #ifdef SOWRAP_ENABLED
  84. else {
  85. #ifdef DEBUG_ENABLED
  86. int dylibloader_verbose = 1;
  87. #else
  88. int dylibloader_verbose = 0;
  89. #endif
  90. use_udev = initialize_libudev(dylibloader_verbose) == 0;
  91. if (use_udev) {
  92. if (!udev_new || !udev_unref || !udev_enumerate_new || !udev_enumerate_add_match_subsystem || !udev_enumerate_scan_devices || !udev_enumerate_get_list_entry || !udev_list_entry_get_next || !udev_list_entry_get_name || !udev_device_new_from_syspath || !udev_device_get_devnode || !udev_device_get_action || !udev_device_unref || !udev_enumerate_unref || !udev_monitor_new_from_netlink || !udev_monitor_filter_add_match_subsystem_devtype || !udev_monitor_enable_receiving || !udev_monitor_get_fd || !udev_monitor_receive_device || !udev_monitor_unref) {
  93. // There's no API to check version, check if functions are available instead.
  94. use_udev = false;
  95. print_verbose("JoypadLinux: Unsupported udev library version!");
  96. } else {
  97. print_verbose("JoypadLinux: udev enabled and loaded successfully.");
  98. }
  99. } else {
  100. print_verbose("JoypadLinux: udev enabled, but couldn't be loaded. Falling back to /dev/input to detect joypads.");
  101. }
  102. }
  103. #endif // SOWRAP_ENABLED
  104. #else
  105. print_verbose("JoypadLinux: udev disabled, parsing /dev/input to detect joypads.");
  106. #endif // UDEV_ENABLED
  107. input = in;
  108. monitor_joypads_thread.start(monitor_joypads_thread_func, this);
  109. joypad_events_thread.start(joypad_events_thread_func, this);
  110. }
  111. JoypadLinux::~JoypadLinux() {
  112. monitor_joypads_exit.set();
  113. joypad_events_exit.set();
  114. monitor_joypads_thread.wait_to_finish();
  115. joypad_events_thread.wait_to_finish();
  116. close_joypads();
  117. }
  118. void JoypadLinux::monitor_joypads_thread_func(void *p_user) {
  119. if (p_user) {
  120. JoypadLinux *joy = static_cast<JoypadLinux *>(p_user);
  121. joy->monitor_joypads_thread_run();
  122. }
  123. }
  124. void JoypadLinux::monitor_joypads_thread_run() {
  125. #ifdef UDEV_ENABLED
  126. if (use_udev) {
  127. udev *_udev = udev_new();
  128. if (!_udev) {
  129. use_udev = false;
  130. ERR_PRINT("Failed getting an udev context, falling back to parsing /dev/input.");
  131. monitor_joypads();
  132. } else {
  133. enumerate_joypads(_udev);
  134. monitor_joypads(_udev);
  135. udev_unref(_udev);
  136. }
  137. } else {
  138. monitor_joypads();
  139. }
  140. #else
  141. monitor_joypads();
  142. #endif
  143. }
  144. #ifdef UDEV_ENABLED
  145. void JoypadLinux::enumerate_joypads(udev *p_udev) {
  146. udev_enumerate *enumerate;
  147. udev_list_entry *devices, *dev_list_entry;
  148. udev_device *dev;
  149. enumerate = udev_enumerate_new(p_udev);
  150. udev_enumerate_add_match_subsystem(enumerate, "input");
  151. udev_enumerate_scan_devices(enumerate);
  152. devices = udev_enumerate_get_list_entry(enumerate);
  153. udev_list_entry_foreach(dev_list_entry, devices) {
  154. const char *path = udev_list_entry_get_name(dev_list_entry);
  155. dev = udev_device_new_from_syspath(p_udev, path);
  156. const char *devnode = udev_device_get_devnode(dev);
  157. if (devnode) {
  158. String devnode_str = devnode;
  159. if (!devnode_str.contains(ignore_str)) {
  160. open_joypad(devnode);
  161. }
  162. }
  163. udev_device_unref(dev);
  164. }
  165. udev_enumerate_unref(enumerate);
  166. }
  167. void JoypadLinux::monitor_joypads(udev *p_udev) {
  168. udev_device *dev = nullptr;
  169. udev_monitor *mon = udev_monitor_new_from_netlink(p_udev, "udev");
  170. udev_monitor_filter_add_match_subsystem_devtype(mon, "input", nullptr);
  171. udev_monitor_enable_receiving(mon);
  172. int fd = udev_monitor_get_fd(mon);
  173. while (!monitor_joypads_exit.is_set()) {
  174. fd_set fds;
  175. struct timeval tv;
  176. int ret;
  177. FD_ZERO(&fds);
  178. FD_SET(fd, &fds);
  179. tv.tv_sec = 0;
  180. tv.tv_usec = 0;
  181. ret = select(fd + 1, &fds, nullptr, nullptr, &tv);
  182. /* Check if our file descriptor has received data. */
  183. if (ret > 0 && FD_ISSET(fd, &fds)) {
  184. /* Make the call to receive the device.
  185. select() ensured that this will not block. */
  186. dev = udev_monitor_receive_device(mon);
  187. if (dev && udev_device_get_devnode(dev) != nullptr) {
  188. String action = udev_device_get_action(dev);
  189. const char *devnode = udev_device_get_devnode(dev);
  190. if (devnode) {
  191. String devnode_str = devnode;
  192. if (!devnode_str.contains(ignore_str)) {
  193. if (action == "add") {
  194. open_joypad(devnode);
  195. } else if (String(action) == "remove") {
  196. close_joypad(devnode);
  197. }
  198. }
  199. }
  200. udev_device_unref(dev);
  201. }
  202. }
  203. OS::get_singleton()->delay_usec(50'000);
  204. }
  205. udev_monitor_unref(mon);
  206. }
  207. #endif
  208. void JoypadLinux::monitor_joypads() {
  209. while (!monitor_joypads_exit.is_set()) {
  210. DIR *input_directory;
  211. input_directory = opendir("/dev/input");
  212. if (input_directory) {
  213. struct dirent *current;
  214. char fname[64];
  215. while ((current = readdir(input_directory)) != nullptr) {
  216. if (strncmp(current->d_name, "event", 5) != 0) {
  217. continue;
  218. }
  219. sprintf(fname, "/dev/input/%.*s", 16, current->d_name);
  220. if (!attached_devices.has(fname)) {
  221. open_joypad(fname);
  222. }
  223. }
  224. }
  225. closedir(input_directory);
  226. OS::get_singleton()->delay_usec(1'000'000);
  227. }
  228. }
  229. void JoypadLinux::close_joypads() {
  230. for (int i = 0; i < JOYPADS_MAX; i++) {
  231. MutexLock lock(joypads_mutex[i]);
  232. Joypad &joypad = joypads[i];
  233. close_joypad(joypad, i);
  234. }
  235. }
  236. void JoypadLinux::close_joypad(const char *p_devpath) {
  237. for (int i = 0; i < JOYPADS_MAX; i++) {
  238. MutexLock lock(joypads_mutex[i]);
  239. Joypad &joypad = joypads[i];
  240. if (joypads[i].devpath == p_devpath) {
  241. close_joypad(joypad, i);
  242. }
  243. }
  244. }
  245. void JoypadLinux::close_joypad(Joypad &p_joypad, int p_id) {
  246. if (p_joypad.fd != -1) {
  247. close(p_joypad.fd);
  248. p_joypad.fd = -1;
  249. attached_devices.erase(p_joypad.devpath);
  250. input->joy_connection_changed(p_id, false, "");
  251. }
  252. p_joypad.events.clear();
  253. }
  254. static String _hex_str(uint8_t p_byte) {
  255. static const char *dict = "0123456789abcdef";
  256. char ret[3];
  257. ret[2] = 0;
  258. ret[0] = dict[p_byte >> 4];
  259. ret[1] = dict[p_byte & 0xF];
  260. return ret;
  261. }
  262. void JoypadLinux::setup_joypad_properties(Joypad &p_joypad) {
  263. unsigned long keybit[NBITS(KEY_MAX)] = { 0 };
  264. unsigned long absbit[NBITS(ABS_MAX)] = { 0 };
  265. int num_buttons = 0;
  266. int num_axes = 0;
  267. if ((ioctl(p_joypad.fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) < 0) ||
  268. (ioctl(p_joypad.fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) < 0)) {
  269. return;
  270. }
  271. for (int i = BTN_JOYSTICK; i < KEY_MAX; ++i) {
  272. if (test_bit(i, keybit)) {
  273. p_joypad.key_map[i] = num_buttons++;
  274. }
  275. }
  276. for (int i = BTN_MISC; i < BTN_JOYSTICK; ++i) {
  277. if (test_bit(i, keybit)) {
  278. p_joypad.key_map[i] = num_buttons++;
  279. }
  280. }
  281. for (int i = 0; i < ABS_MISC; ++i) {
  282. /* Skip hats */
  283. if (i == ABS_HAT0X) {
  284. i = ABS_HAT3Y;
  285. continue;
  286. }
  287. if (test_bit(i, absbit)) {
  288. p_joypad.abs_map[i] = num_axes++;
  289. p_joypad.abs_info[i] = memnew(input_absinfo);
  290. if (ioctl(p_joypad.fd, EVIOCGABS(i), p_joypad.abs_info[i]) < 0) {
  291. memdelete(p_joypad.abs_info[i]);
  292. p_joypad.abs_info[i] = nullptr;
  293. }
  294. }
  295. }
  296. p_joypad.force_feedback = false;
  297. p_joypad.ff_effect_timestamp = 0;
  298. unsigned long ffbit[NBITS(FF_CNT)];
  299. if (ioctl(p_joypad.fd, EVIOCGBIT(EV_FF, sizeof(ffbit)), ffbit) != -1) {
  300. if (test_bit(FF_RUMBLE, ffbit)) {
  301. p_joypad.force_feedback = true;
  302. }
  303. }
  304. }
  305. void JoypadLinux::open_joypad(const char *p_path) {
  306. int joy_num = input->get_unused_joy_id();
  307. int fd = open(p_path, O_RDWR | O_NONBLOCK);
  308. if (fd != -1 && joy_num != -1) {
  309. unsigned long evbit[NBITS(EV_MAX)] = { 0 };
  310. unsigned long keybit[NBITS(KEY_MAX)] = { 0 };
  311. unsigned long absbit[NBITS(ABS_MAX)] = { 0 };
  312. // add to attached devices so we don't try to open it again
  313. attached_devices.push_back(String(p_path));
  314. if ((ioctl(fd, EVIOCGBIT(0, sizeof(evbit)), evbit) < 0) ||
  315. (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) < 0) ||
  316. (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) < 0)) {
  317. close(fd);
  318. return;
  319. }
  320. // Check if the device supports basic gamepad events
  321. bool has_abs_left = (test_bit(ABS_X, absbit) && test_bit(ABS_Y, absbit));
  322. bool has_abs_right = (test_bit(ABS_RX, absbit) && test_bit(ABS_RY, absbit));
  323. if (!(test_bit(EV_KEY, evbit) && test_bit(EV_ABS, evbit) && (has_abs_left || has_abs_right))) {
  324. close(fd);
  325. return;
  326. }
  327. char uid[128];
  328. char namebuf[128];
  329. String name = "";
  330. input_id inpid;
  331. if (ioctl(fd, EVIOCGNAME(sizeof(namebuf)), namebuf) >= 0) {
  332. name = namebuf;
  333. }
  334. for (const String &word : name.to_lower().split(" ")) {
  335. if (banned_words.has(word)) {
  336. return;
  337. }
  338. }
  339. if (ioctl(fd, EVIOCGID, &inpid) < 0) {
  340. close(fd);
  341. return;
  342. }
  343. uint16_t vendor = BSWAP16(inpid.vendor);
  344. uint16_t product = BSWAP16(inpid.product);
  345. uint16_t version = BSWAP16(inpid.version);
  346. if (input->should_ignore_device(vendor, product)) {
  347. // This can be true in cases where Steam is passing information into the game to ignore
  348. // original gamepads when using virtual rebindings (See SteamInput).
  349. return;
  350. }
  351. MutexLock lock(joypads_mutex[joy_num]);
  352. Joypad &joypad = joypads[joy_num];
  353. joypad.reset();
  354. joypad.fd = fd;
  355. joypad.devpath = String(p_path);
  356. setup_joypad_properties(joypad);
  357. sprintf(uid, "%04x%04x", BSWAP16(inpid.bustype), 0);
  358. if (inpid.vendor && inpid.product && inpid.version) {
  359. Dictionary joypad_info;
  360. joypad_info["vendor_id"] = inpid.vendor;
  361. joypad_info["product_id"] = inpid.product;
  362. joypad_info["raw_name"] = name;
  363. sprintf(uid + String(uid).length(), "%04x%04x%04x%04x%04x%04x", vendor, 0, product, 0, version, 0);
  364. if (inpid.vendor == VALVE_GAMEPAD_VID && inpid.product == VALVE_GAMEPAD_PID) {
  365. if (name.begins_with(VALVE_GAMEPAD_NAME_PREFIX)) {
  366. String idx_str = name.substr(strlen(VALVE_GAMEPAD_NAME_PREFIX));
  367. if (idx_str.is_valid_int()) {
  368. joypad_info["steam_input_index"] = idx_str.to_int();
  369. }
  370. }
  371. }
  372. input->joy_connection_changed(joy_num, true, name, uid, joypad_info);
  373. } else {
  374. String uidname = uid;
  375. int uidlen = MIN(name.length(), 11);
  376. for (int i = 0; i < uidlen; i++) {
  377. uidname = uidname + _hex_str(name[i]);
  378. }
  379. uidname += "00";
  380. input->joy_connection_changed(joy_num, true, name, uidname);
  381. }
  382. }
  383. }
  384. void JoypadLinux::joypad_vibration_start(Joypad &p_joypad, float p_weak_magnitude, float p_strong_magnitude, float p_duration, uint64_t p_timestamp) {
  385. if (!p_joypad.force_feedback || p_joypad.fd == -1 || p_weak_magnitude < 0.f || p_weak_magnitude > 1.f || p_strong_magnitude < 0.f || p_strong_magnitude > 1.f) {
  386. return;
  387. }
  388. if (p_joypad.ff_effect_id != -1) {
  389. joypad_vibration_stop(p_joypad, p_timestamp);
  390. }
  391. struct ff_effect effect;
  392. effect.type = FF_RUMBLE;
  393. effect.id = -1;
  394. effect.u.rumble.weak_magnitude = floor(p_weak_magnitude * (float)0xffff);
  395. effect.u.rumble.strong_magnitude = floor(p_strong_magnitude * (float)0xffff);
  396. effect.replay.length = floor(p_duration * 1000);
  397. effect.replay.delay = 0;
  398. if (ioctl(p_joypad.fd, EVIOCSFF, &effect) < 0) {
  399. return;
  400. }
  401. struct input_event play;
  402. play.type = EV_FF;
  403. play.code = effect.id;
  404. play.value = 1;
  405. if (write(p_joypad.fd, (const void *)&play, sizeof(play)) == -1) {
  406. print_verbose("Couldn't write to Joypad device.");
  407. }
  408. p_joypad.ff_effect_id = effect.id;
  409. p_joypad.ff_effect_timestamp = p_timestamp;
  410. }
  411. void JoypadLinux::joypad_vibration_stop(Joypad &p_joypad, uint64_t p_timestamp) {
  412. if (!p_joypad.force_feedback || p_joypad.fd == -1 || p_joypad.ff_effect_id == -1) {
  413. return;
  414. }
  415. if (ioctl(p_joypad.fd, EVIOCRMFF, p_joypad.ff_effect_id) < 0) {
  416. return;
  417. }
  418. p_joypad.ff_effect_id = -1;
  419. p_joypad.ff_effect_timestamp = p_timestamp;
  420. }
  421. float JoypadLinux::axis_correct(const input_absinfo *p_abs, int p_value) const {
  422. int min = p_abs->minimum;
  423. int max = p_abs->maximum;
  424. // Convert to a value between -1.0f and 1.0f.
  425. return 2.0f * (p_value - min) / (max - min) - 1.0f;
  426. }
  427. void JoypadLinux::joypad_events_thread_func(void *p_user) {
  428. if (p_user) {
  429. JoypadLinux *joy = (JoypadLinux *)p_user;
  430. joy->joypad_events_thread_run();
  431. }
  432. }
  433. void JoypadLinux::joypad_events_thread_run() {
  434. while (!joypad_events_exit.is_set()) {
  435. bool no_events = true;
  436. for (int i = 0; i < JOYPADS_MAX; i++) {
  437. MutexLock lock(joypads_mutex[i]);
  438. Joypad &joypad = joypads[i];
  439. if (joypad.fd == -1) {
  440. continue;
  441. }
  442. input_event event;
  443. while (read(joypad.fd, &event, sizeof(event)) > 0) {
  444. no_events = false;
  445. JoypadEvent joypad_event;
  446. joypad_event.type = event.type;
  447. joypad_event.code = event.code;
  448. joypad_event.value = event.value;
  449. joypad.events.push_back(joypad_event);
  450. }
  451. if (errno != EAGAIN) {
  452. close_joypad(joypad, i);
  453. }
  454. }
  455. if (no_events) {
  456. OS::get_singleton()->delay_usec(10'000);
  457. }
  458. }
  459. }
  460. void JoypadLinux::process_joypads() {
  461. for (int i = 0; i < JOYPADS_MAX; i++) {
  462. MutexLock lock(joypads_mutex[i]);
  463. Joypad &joypad = joypads[i];
  464. if (joypad.fd == -1) {
  465. continue;
  466. }
  467. for (uint32_t j = 0; j < joypad.events.size(); j++) {
  468. const JoypadEvent &joypad_event = joypad.events[j];
  469. // joypad_event may be tainted and out of MAX_KEY range, which will cause
  470. // joypad.key_map[joypad_event.code] to crash
  471. if (joypad_event.code >= MAX_KEY) {
  472. return;
  473. }
  474. switch (joypad_event.type) {
  475. case EV_KEY:
  476. input->joy_button(i, (JoyButton)joypad.key_map[joypad_event.code], joypad_event.value);
  477. break;
  478. case EV_ABS:
  479. switch (joypad_event.code) {
  480. case ABS_HAT0X:
  481. if (joypad_event.value != 0) {
  482. if (joypad_event.value < 0) {
  483. joypad.dpad.set_flag(HatMask::LEFT);
  484. joypad.dpad.clear_flag(HatMask::RIGHT);
  485. } else {
  486. joypad.dpad.set_flag(HatMask::RIGHT);
  487. joypad.dpad.clear_flag(HatMask::LEFT);
  488. }
  489. } else {
  490. joypad.dpad.clear_flag(HatMask::LEFT);
  491. joypad.dpad.clear_flag(HatMask::RIGHT);
  492. }
  493. input->joy_hat(i, joypad.dpad);
  494. break;
  495. case ABS_HAT0Y:
  496. if (joypad_event.value != 0) {
  497. if (joypad_event.value < 0) {
  498. joypad.dpad.set_flag(HatMask::UP);
  499. joypad.dpad.clear_flag(HatMask::DOWN);
  500. } else {
  501. joypad.dpad.set_flag(HatMask::DOWN);
  502. joypad.dpad.clear_flag(HatMask::UP);
  503. }
  504. } else {
  505. joypad.dpad.clear_flag(HatMask::UP);
  506. joypad.dpad.clear_flag(HatMask::DOWN);
  507. }
  508. input->joy_hat(i, joypad.dpad);
  509. break;
  510. default:
  511. if (joypad_event.code >= MAX_ABS) {
  512. return;
  513. }
  514. if (joypad.abs_map[joypad_event.code] != -1 && joypad.abs_info[joypad_event.code]) {
  515. float value = axis_correct(joypad.abs_info[joypad_event.code], joypad_event.value);
  516. joypad.curr_axis[joypad.abs_map[joypad_event.code]] = value;
  517. }
  518. break;
  519. }
  520. break;
  521. }
  522. }
  523. joypad.events.clear();
  524. for (int j = 0; j < MAX_ABS; j++) {
  525. int index = joypad.abs_map[j];
  526. if (index != -1) {
  527. input->joy_axis(i, (JoyAxis)index, joypad.curr_axis[index]);
  528. }
  529. }
  530. if (joypad.force_feedback) {
  531. uint64_t timestamp = input->get_joy_vibration_timestamp(i);
  532. if (timestamp > joypad.ff_effect_timestamp) {
  533. Vector2 strength = input->get_joy_vibration_strength(i);
  534. float duration = input->get_joy_vibration_duration(i);
  535. if (strength.x == 0 && strength.y == 0) {
  536. joypad_vibration_stop(joypad, timestamp);
  537. } else {
  538. joypad_vibration_start(joypad, strength.x, strength.y, duration, timestamp);
  539. }
  540. }
  541. }
  542. }
  543. }
  544. #endif // JOYDEV_ENABLED