joypad_linux.cpp 19 KB

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