joypad_linux.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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. JoypadLinux::Joypad::~Joypad() {
  52. for (int i = 0; i < MAX_ABS; i++) {
  53. if (abs_info[i]) {
  54. memdelete(abs_info[i]);
  55. }
  56. }
  57. }
  58. void JoypadLinux::Joypad::reset() {
  59. dpad = 0;
  60. fd = -1;
  61. for (int i = 0; i < MAX_ABS; i++) {
  62. abs_map[i] = -1;
  63. curr_axis[i] = 0;
  64. }
  65. events.clear();
  66. }
  67. #ifdef UDEV_ENABLED
  68. // This function is derived from SDL:
  69. // https://github.com/libsdl-org/SDL/blob/main/src/core/linux/SDL_sandbox.c#L28-L45
  70. static bool detect_sandbox() {
  71. if (access("/.flatpak-info", F_OK) == 0) {
  72. return true;
  73. }
  74. // For Snap, we check multiple variables because they might be set for
  75. // unrelated reasons. This is the same thing WebKitGTK does.
  76. if (OS::get_singleton()->has_environment("SNAP") && OS::get_singleton()->has_environment("SNAP_NAME") && OS::get_singleton()->has_environment("SNAP_REVISION")) {
  77. return true;
  78. }
  79. if (access("/run/host/container-manager", F_OK) == 0) {
  80. return true;
  81. }
  82. return false;
  83. }
  84. #endif // UDEV_ENABLED
  85. JoypadLinux::JoypadLinux(Input *in) {
  86. #ifdef UDEV_ENABLED
  87. if (detect_sandbox()) {
  88. // Linux binaries in sandboxes / containers need special handling because
  89. // libudev doesn't work there. So we need to fallback to manual parsing
  90. // of /dev/input in such case.
  91. use_udev = false;
  92. print_verbose("JoypadLinux: udev enabled, but detected incompatible sandboxed mode. Falling back to /dev/input to detect joypads.");
  93. }
  94. #ifdef SOWRAP_ENABLED
  95. else {
  96. #ifdef DEBUG_ENABLED
  97. int dylibloader_verbose = 1;
  98. #else
  99. int dylibloader_verbose = 0;
  100. #endif
  101. use_udev = initialize_libudev(dylibloader_verbose) == 0;
  102. if (use_udev) {
  103. 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) {
  104. // There's no API to check version, check if functions are available instead.
  105. use_udev = false;
  106. print_verbose("JoypadLinux: Unsupported udev library version!");
  107. } else {
  108. print_verbose("JoypadLinux: udev enabled and loaded successfully.");
  109. }
  110. } else {
  111. print_verbose("JoypadLinux: udev enabled, but couldn't be loaded. Falling back to /dev/input to detect joypads.");
  112. }
  113. }
  114. #endif // SOWRAP_ENABLED
  115. #else
  116. print_verbose("JoypadLinux: udev disabled, parsing /dev/input to detect joypads.");
  117. #endif // UDEV_ENABLED
  118. input = in;
  119. monitor_joypads_thread.start(monitor_joypads_thread_func, this);
  120. joypad_events_thread.start(joypad_events_thread_func, this);
  121. }
  122. JoypadLinux::~JoypadLinux() {
  123. monitor_joypads_exit.set();
  124. joypad_events_exit.set();
  125. monitor_joypads_thread.wait_to_finish();
  126. joypad_events_thread.wait_to_finish();
  127. close_joypads();
  128. }
  129. void JoypadLinux::monitor_joypads_thread_func(void *p_user) {
  130. if (p_user) {
  131. JoypadLinux *joy = static_cast<JoypadLinux *>(p_user);
  132. joy->monitor_joypads_thread_run();
  133. }
  134. }
  135. void JoypadLinux::monitor_joypads_thread_run() {
  136. #ifdef UDEV_ENABLED
  137. if (use_udev) {
  138. udev *_udev = udev_new();
  139. if (!_udev) {
  140. use_udev = false;
  141. ERR_PRINT("Failed getting an udev context, falling back to parsing /dev/input.");
  142. monitor_joypads();
  143. } else {
  144. enumerate_joypads(_udev);
  145. monitor_joypads(_udev);
  146. udev_unref(_udev);
  147. }
  148. } else {
  149. monitor_joypads();
  150. }
  151. #else
  152. monitor_joypads();
  153. #endif
  154. }
  155. #ifdef UDEV_ENABLED
  156. void JoypadLinux::enumerate_joypads(udev *p_udev) {
  157. udev_enumerate *enumerate;
  158. udev_list_entry *devices, *dev_list_entry;
  159. udev_device *dev;
  160. enumerate = udev_enumerate_new(p_udev);
  161. udev_enumerate_add_match_subsystem(enumerate, "input");
  162. udev_enumerate_scan_devices(enumerate);
  163. devices = udev_enumerate_get_list_entry(enumerate);
  164. udev_list_entry_foreach(dev_list_entry, devices) {
  165. const char *path = udev_list_entry_get_name(dev_list_entry);
  166. dev = udev_device_new_from_syspath(p_udev, path);
  167. const char *devnode = udev_device_get_devnode(dev);
  168. if (devnode) {
  169. String devnode_str = devnode;
  170. if (devnode_str.find(ignore_str) == -1) {
  171. open_joypad(devnode);
  172. }
  173. }
  174. udev_device_unref(dev);
  175. }
  176. udev_enumerate_unref(enumerate);
  177. }
  178. void JoypadLinux::monitor_joypads(udev *p_udev) {
  179. udev_device *dev = nullptr;
  180. udev_monitor *mon = udev_monitor_new_from_netlink(p_udev, "udev");
  181. udev_monitor_filter_add_match_subsystem_devtype(mon, "input", nullptr);
  182. udev_monitor_enable_receiving(mon);
  183. int fd = udev_monitor_get_fd(mon);
  184. while (!monitor_joypads_exit.is_set()) {
  185. fd_set fds;
  186. struct timeval tv;
  187. int ret;
  188. FD_ZERO(&fds);
  189. FD_SET(fd, &fds);
  190. tv.tv_sec = 0;
  191. tv.tv_usec = 0;
  192. ret = select(fd + 1, &fds, nullptr, nullptr, &tv);
  193. /* Check if our file descriptor has received data. */
  194. if (ret > 0 && FD_ISSET(fd, &fds)) {
  195. /* Make the call to receive the device.
  196. select() ensured that this will not block. */
  197. dev = udev_monitor_receive_device(mon);
  198. if (dev && udev_device_get_devnode(dev) != nullptr) {
  199. String action = udev_device_get_action(dev);
  200. const char *devnode = udev_device_get_devnode(dev);
  201. if (devnode) {
  202. String devnode_str = devnode;
  203. if (devnode_str.find(ignore_str) == -1) {
  204. if (action == "add") {
  205. open_joypad(devnode);
  206. } else if (String(action) == "remove") {
  207. close_joypad(devnode);
  208. }
  209. }
  210. }
  211. udev_device_unref(dev);
  212. }
  213. }
  214. usleep(50000);
  215. }
  216. udev_monitor_unref(mon);
  217. }
  218. #endif
  219. void JoypadLinux::monitor_joypads() {
  220. while (!monitor_joypads_exit.is_set()) {
  221. DIR *input_directory;
  222. input_directory = opendir("/dev/input");
  223. if (input_directory) {
  224. struct dirent *current;
  225. char fname[64];
  226. while ((current = readdir(input_directory)) != nullptr) {
  227. if (strncmp(current->d_name, "event", 5) != 0) {
  228. continue;
  229. }
  230. sprintf(fname, "/dev/input/%.*s", 16, current->d_name);
  231. if (attached_devices.find(fname) == -1) {
  232. open_joypad(fname);
  233. }
  234. }
  235. }
  236. closedir(input_directory);
  237. usleep(1000000); // 1s
  238. }
  239. }
  240. void JoypadLinux::close_joypads() {
  241. for (int i = 0; i < JOYPADS_MAX; i++) {
  242. MutexLock lock(joypads_mutex[i]);
  243. Joypad &joypad = joypads[i];
  244. close_joypad(joypad, i);
  245. }
  246. }
  247. void JoypadLinux::close_joypad(const char *p_devpath) {
  248. for (int i = 0; i < JOYPADS_MAX; i++) {
  249. MutexLock lock(joypads_mutex[i]);
  250. Joypad &joypad = joypads[i];
  251. if (joypads[i].devpath == p_devpath) {
  252. close_joypad(joypad, i);
  253. }
  254. }
  255. }
  256. void JoypadLinux::close_joypad(Joypad &p_joypad, int p_id) {
  257. if (p_joypad.fd != -1) {
  258. close(p_joypad.fd);
  259. p_joypad.fd = -1;
  260. attached_devices.erase(p_joypad.devpath);
  261. input->joy_connection_changed(p_id, false, "");
  262. }
  263. p_joypad.events.clear();
  264. }
  265. static String _hex_str(uint8_t p_byte) {
  266. static const char *dict = "0123456789abcdef";
  267. char ret[3];
  268. ret[2] = 0;
  269. ret[0] = dict[p_byte >> 4];
  270. ret[1] = dict[p_byte & 0xF];
  271. return ret;
  272. }
  273. void JoypadLinux::setup_joypad_properties(Joypad &p_joypad) {
  274. unsigned long keybit[NBITS(KEY_MAX)] = { 0 };
  275. unsigned long absbit[NBITS(ABS_MAX)] = { 0 };
  276. int num_buttons = 0;
  277. int num_axes = 0;
  278. if ((ioctl(p_joypad.fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) < 0) ||
  279. (ioctl(p_joypad.fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) < 0)) {
  280. return;
  281. }
  282. for (int i = BTN_JOYSTICK; i < KEY_MAX; ++i) {
  283. if (test_bit(i, keybit)) {
  284. p_joypad.key_map[i] = num_buttons++;
  285. }
  286. }
  287. for (int i = BTN_MISC; i < BTN_JOYSTICK; ++i) {
  288. if (test_bit(i, keybit)) {
  289. p_joypad.key_map[i] = num_buttons++;
  290. }
  291. }
  292. for (int i = 0; i < ABS_MISC; ++i) {
  293. /* Skip hats */
  294. if (i == ABS_HAT0X) {
  295. i = ABS_HAT3Y;
  296. continue;
  297. }
  298. if (test_bit(i, absbit)) {
  299. p_joypad.abs_map[i] = num_axes++;
  300. p_joypad.abs_info[i] = memnew(input_absinfo);
  301. if (ioctl(p_joypad.fd, EVIOCGABS(i), p_joypad.abs_info[i]) < 0) {
  302. memdelete(p_joypad.abs_info[i]);
  303. p_joypad.abs_info[i] = nullptr;
  304. }
  305. }
  306. }
  307. p_joypad.force_feedback = false;
  308. p_joypad.ff_effect_timestamp = 0;
  309. unsigned long ffbit[NBITS(FF_CNT)];
  310. if (ioctl(p_joypad.fd, EVIOCGBIT(EV_FF, sizeof(ffbit)), ffbit) != -1) {
  311. if (test_bit(FF_RUMBLE, ffbit)) {
  312. p_joypad.force_feedback = true;
  313. }
  314. }
  315. }
  316. void JoypadLinux::open_joypad(const char *p_path) {
  317. int joy_num = input->get_unused_joy_id();
  318. int fd = open(p_path, O_RDWR | O_NONBLOCK);
  319. if (fd != -1 && joy_num != -1) {
  320. unsigned long evbit[NBITS(EV_MAX)] = { 0 };
  321. unsigned long keybit[NBITS(KEY_MAX)] = { 0 };
  322. unsigned long absbit[NBITS(ABS_MAX)] = { 0 };
  323. // add to attached devices so we don't try to open it again
  324. attached_devices.push_back(String(p_path));
  325. if ((ioctl(fd, EVIOCGBIT(0, sizeof(evbit)), evbit) < 0) ||
  326. (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) < 0) ||
  327. (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) < 0)) {
  328. close(fd);
  329. return;
  330. }
  331. // Check if the device supports basic gamepad events
  332. bool has_abs_left = (test_bit(ABS_X, absbit) && test_bit(ABS_Y, absbit));
  333. bool has_abs_right = (test_bit(ABS_RX, absbit) && test_bit(ABS_RY, absbit));
  334. if (!(test_bit(EV_KEY, evbit) && test_bit(EV_ABS, evbit) && (has_abs_left || has_abs_right))) {
  335. close(fd);
  336. return;
  337. }
  338. char uid[128];
  339. char namebuf[128];
  340. String name = "";
  341. input_id inpid;
  342. if (ioctl(fd, EVIOCGNAME(sizeof(namebuf)), namebuf) >= 0) {
  343. name = namebuf;
  344. }
  345. if (ioctl(fd, EVIOCGID, &inpid) < 0) {
  346. close(fd);
  347. return;
  348. }
  349. uint16_t vendor = BSWAP16(inpid.vendor);
  350. uint16_t product = BSWAP16(inpid.product);
  351. uint16_t version = BSWAP16(inpid.version);
  352. if (input->should_ignore_device(vendor, product)) {
  353. // This can be true in cases where Steam is passing information into the game to ignore
  354. // original gamepads when using virtual rebindings (See SteamInput).
  355. return;
  356. }
  357. MutexLock lock(joypads_mutex[joy_num]);
  358. Joypad &joypad = joypads[joy_num];
  359. joypad.reset();
  360. joypad.fd = fd;
  361. joypad.devpath = String(p_path);
  362. setup_joypad_properties(joypad);
  363. sprintf(uid, "%04x%04x", BSWAP16(inpid.bustype), 0);
  364. if (inpid.vendor && inpid.product && inpid.version) {
  365. sprintf(uid + String(uid).length(), "%04x%04x%04x%04x%04x%04x", vendor, 0, product, 0, version, 0);
  366. input->joy_connection_changed(joy_num, true, name, uid);
  367. } else {
  368. String uidname = uid;
  369. int uidlen = MIN(name.length(), 11);
  370. for (int i = 0; i < uidlen; i++) {
  371. uidname = uidname + _hex_str(name[i]);
  372. }
  373. uidname += "00";
  374. input->joy_connection_changed(joy_num, true, name, uidname);
  375. }
  376. }
  377. }
  378. void JoypadLinux::joypad_vibration_start(Joypad &p_joypad, float p_weak_magnitude, float p_strong_magnitude, float p_duration, uint64_t p_timestamp) {
  379. 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) {
  380. return;
  381. }
  382. if (p_joypad.ff_effect_id != -1) {
  383. joypad_vibration_stop(p_joypad, p_timestamp);
  384. }
  385. struct ff_effect effect;
  386. effect.type = FF_RUMBLE;
  387. effect.id = -1;
  388. effect.u.rumble.weak_magnitude = floor(p_weak_magnitude * (float)0xffff);
  389. effect.u.rumble.strong_magnitude = floor(p_strong_magnitude * (float)0xffff);
  390. effect.replay.length = floor(p_duration * 1000);
  391. effect.replay.delay = 0;
  392. if (ioctl(p_joypad.fd, EVIOCSFF, &effect) < 0) {
  393. return;
  394. }
  395. struct input_event play;
  396. play.type = EV_FF;
  397. play.code = effect.id;
  398. play.value = 1;
  399. if (write(p_joypad.fd, (const void *)&play, sizeof(play)) == -1) {
  400. print_verbose("Couldn't write to Joypad device.");
  401. }
  402. p_joypad.ff_effect_id = effect.id;
  403. p_joypad.ff_effect_timestamp = p_timestamp;
  404. }
  405. void JoypadLinux::joypad_vibration_stop(Joypad &p_joypad, uint64_t p_timestamp) {
  406. if (!p_joypad.force_feedback || p_joypad.fd == -1 || p_joypad.ff_effect_id == -1) {
  407. return;
  408. }
  409. if (ioctl(p_joypad.fd, EVIOCRMFF, p_joypad.ff_effect_id) < 0) {
  410. return;
  411. }
  412. p_joypad.ff_effect_id = -1;
  413. p_joypad.ff_effect_timestamp = p_timestamp;
  414. }
  415. float JoypadLinux::axis_correct(const input_absinfo *p_abs, int p_value) const {
  416. int min = p_abs->minimum;
  417. int max = p_abs->maximum;
  418. // Convert to a value between -1.0f and 1.0f.
  419. return 2.0f * (p_value - min) / (max - min) - 1.0f;
  420. }
  421. void JoypadLinux::joypad_events_thread_func(void *p_user) {
  422. if (p_user) {
  423. JoypadLinux *joy = (JoypadLinux *)p_user;
  424. joy->joypad_events_thread_run();
  425. }
  426. }
  427. void JoypadLinux::joypad_events_thread_run() {
  428. while (!joypad_events_exit.is_set()) {
  429. bool no_events = true;
  430. for (int i = 0; i < JOYPADS_MAX; i++) {
  431. MutexLock lock(joypads_mutex[i]);
  432. Joypad &joypad = joypads[i];
  433. if (joypad.fd == -1) {
  434. continue;
  435. }
  436. input_event event;
  437. while (read(joypad.fd, &event, sizeof(event)) > 0) {
  438. no_events = false;
  439. JoypadEvent joypad_event;
  440. joypad_event.type = event.type;
  441. joypad_event.code = event.code;
  442. joypad_event.value = event.value;
  443. joypad.events.push_back(joypad_event);
  444. }
  445. if (errno != EAGAIN) {
  446. close_joypad(joypad, i);
  447. }
  448. }
  449. if (no_events) {
  450. usleep(10000); // 10ms
  451. }
  452. }
  453. }
  454. void JoypadLinux::process_joypads() {
  455. for (int i = 0; i < JOYPADS_MAX; i++) {
  456. MutexLock lock(joypads_mutex[i]);
  457. Joypad &joypad = joypads[i];
  458. if (joypad.fd == -1) {
  459. continue;
  460. }
  461. for (uint32_t j = 0; j < joypad.events.size(); j++) {
  462. const JoypadEvent &joypad_event = joypad.events[j];
  463. // joypad_event may be tainted and out of MAX_KEY range, which will cause
  464. // joypad.key_map[joypad_event.code] to crash
  465. if (joypad_event.code >= MAX_KEY) {
  466. return;
  467. }
  468. switch (joypad_event.type) {
  469. case EV_KEY:
  470. input->joy_button(i, (JoyButton)joypad.key_map[joypad_event.code], joypad_event.value);
  471. break;
  472. case EV_ABS:
  473. switch (joypad_event.code) {
  474. case ABS_HAT0X:
  475. if (joypad_event.value != 0) {
  476. if (joypad_event.value < 0) {
  477. joypad.dpad.set_flag(HatMask::LEFT);
  478. joypad.dpad.clear_flag(HatMask::RIGHT);
  479. } else {
  480. joypad.dpad.set_flag(HatMask::RIGHT);
  481. joypad.dpad.clear_flag(HatMask::LEFT);
  482. }
  483. } else {
  484. joypad.dpad.clear_flag(HatMask::LEFT);
  485. joypad.dpad.clear_flag(HatMask::RIGHT);
  486. }
  487. input->joy_hat(i, joypad.dpad);
  488. break;
  489. case ABS_HAT0Y:
  490. if (joypad_event.value != 0) {
  491. if (joypad_event.value < 0) {
  492. joypad.dpad.set_flag(HatMask::UP);
  493. joypad.dpad.clear_flag(HatMask::DOWN);
  494. } else {
  495. joypad.dpad.set_flag(HatMask::DOWN);
  496. joypad.dpad.clear_flag(HatMask::UP);
  497. }
  498. } else {
  499. joypad.dpad.clear_flag(HatMask::UP);
  500. joypad.dpad.clear_flag(HatMask::DOWN);
  501. }
  502. input->joy_hat(i, joypad.dpad);
  503. break;
  504. default:
  505. if (joypad_event.code >= MAX_ABS) {
  506. return;
  507. }
  508. if (joypad.abs_map[joypad_event.code] != -1 && joypad.abs_info[joypad_event.code]) {
  509. float value = axis_correct(joypad.abs_info[joypad_event.code], joypad_event.value);
  510. joypad.curr_axis[joypad.abs_map[joypad_event.code]] = value;
  511. }
  512. break;
  513. }
  514. break;
  515. }
  516. }
  517. joypad.events.clear();
  518. for (int j = 0; j < MAX_ABS; j++) {
  519. int index = joypad.abs_map[j];
  520. if (index != -1) {
  521. input->joy_axis(i, (JoyAxis)index, joypad.curr_axis[index]);
  522. }
  523. }
  524. if (joypad.force_feedback) {
  525. uint64_t timestamp = input->get_joy_vibration_timestamp(i);
  526. if (timestamp > joypad.ff_effect_timestamp) {
  527. Vector2 strength = input->get_joy_vibration_strength(i);
  528. float duration = input->get_joy_vibration_duration(i);
  529. if (strength.x == 0 && strength.y == 0) {
  530. joypad_vibration_stop(joypad, timestamp);
  531. } else {
  532. joypad_vibration_start(joypad, strength.x, strength.y, duration, timestamp);
  533. }
  534. }
  535. }
  536. }
  537. }
  538. #endif // JOYDEV_ENABLED