joypad_linux.cpp 18 KB

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