SDL_sysjoystick.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2021 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../../SDL_internal.h"
  19. #ifdef SDL_JOYSTICK_LINUX
  20. #ifndef SDL_INPUT_LINUXEV
  21. #error SDL now requires a Linux 2.4+ kernel with /dev/input/event support.
  22. #endif
  23. /* This is the Linux implementation of the SDL joystick API */
  24. #include <sys/stat.h>
  25. #include <errno.h> /* errno, strerror */
  26. #include <fcntl.h>
  27. #include <limits.h> /* For the definition of PATH_MAX */
  28. #ifdef HAVE_INOTIFY
  29. #include <sys/inotify.h>
  30. #endif
  31. #include <sys/ioctl.h>
  32. #include <unistd.h>
  33. #include <dirent.h>
  34. #include <linux/joystick.h>
  35. #include "SDL_hints.h"
  36. #include "SDL_joystick.h"
  37. #include "SDL_log.h"
  38. #include "SDL_endian.h"
  39. #include "SDL_timer.h"
  40. #include "../../events/SDL_events_c.h"
  41. #include "../SDL_sysjoystick.h"
  42. #include "../SDL_joystick_c.h"
  43. #include "../steam/SDL_steamcontroller.h"
  44. #include "SDL_sysjoystick_c.h"
  45. #include "../hidapi/SDL_hidapijoystick_c.h"
  46. /* This isn't defined in older Linux kernel headers */
  47. #ifndef SYN_DROPPED
  48. #define SYN_DROPPED 3
  49. #endif
  50. #ifndef BTN_DPAD_UP
  51. #define BTN_DPAD_UP 0x220
  52. #endif
  53. #ifndef BTN_DPAD_DOWN
  54. #define BTN_DPAD_DOWN 0x221
  55. #endif
  56. #ifndef BTN_DPAD_LEFT
  57. #define BTN_DPAD_LEFT 0x222
  58. #endif
  59. #ifndef BTN_DPAD_RIGHT
  60. #define BTN_DPAD_RIGHT 0x223
  61. #endif
  62. #include "../../core/linux/SDL_evdev_capabilities.h"
  63. #include "../../core/linux/SDL_udev.h"
  64. #if 0
  65. #define DEBUG_INPUT_EVENTS 1
  66. #endif
  67. typedef enum
  68. {
  69. ENUMERATION_UNSET,
  70. ENUMERATION_LIBUDEV,
  71. ENUMERATION_FALLBACK
  72. } EnumerationMethod;
  73. static EnumerationMethod enumeration_method = ENUMERATION_UNSET;
  74. static int MaybeAddDevice(const char *path);
  75. static int MaybeRemoveDevice(const char *path);
  76. /* A linked list of available joysticks */
  77. typedef struct SDL_joylist_item
  78. {
  79. int device_instance;
  80. char *path; /* "/dev/input/event2" or whatever */
  81. char *name; /* "SideWinder 3D Pro" or whatever */
  82. SDL_JoystickGUID guid;
  83. dev_t devnum;
  84. struct joystick_hwdata *hwdata;
  85. struct SDL_joylist_item *next;
  86. /* Steam Controller support */
  87. SDL_bool m_bSteamController;
  88. SDL_GamepadMapping *mapping;
  89. } SDL_joylist_item;
  90. static SDL_joylist_item *SDL_joylist = NULL;
  91. static SDL_joylist_item *SDL_joylist_tail = NULL;
  92. static int numjoysticks = 0;
  93. static int inotify_fd = -1;
  94. static Uint32 last_joy_detect_time;
  95. static time_t last_input_dir_mtime;
  96. static void
  97. FixupDeviceInfoForMapping(int fd, struct input_id *inpid)
  98. {
  99. if (inpid->vendor == 0x045e && inpid->product == 0x0b05 && inpid->version == 0x0903) {
  100. /* This is a Microsoft Xbox One Elite Series 2 controller */
  101. unsigned long keybit[NBITS(KEY_MAX)] = { 0 };
  102. /* The first version of the firmware duplicated all the inputs */
  103. if ((ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) >= 0) &&
  104. test_bit(0x2c0, keybit)) {
  105. /* Change the version to 0x0902, so we can map it differently */
  106. inpid->version = 0x0902;
  107. }
  108. }
  109. /* For Atari vcs modern and classic controllers have the version reflecting
  110. * firmware version, but the mapping stays stable so ignore
  111. * version information */
  112. if (inpid->vendor == 0x3250
  113. && (inpid->product == 0x1001 || inpid->product == 0x1002)) {
  114. inpid->version = 0;
  115. }
  116. }
  117. #ifdef SDL_JOYSTICK_HIDAPI
  118. static SDL_bool
  119. IsVirtualJoystick(Uint16 vendor, Uint16 product, Uint16 version, const char *name)
  120. {
  121. if (vendor == USB_VENDOR_MICROSOFT && product == USB_PRODUCT_XBOX_ONE_S && version == 0 &&
  122. SDL_strcmp(name, "Xbox One S Controller") == 0) {
  123. /* This is the virtual device created by the xow driver */
  124. return SDL_TRUE;
  125. }
  126. return SDL_FALSE;
  127. }
  128. #endif /* SDL_JOYSTICK_HIDAPI */
  129. static int
  130. GuessIsJoystick(int fd)
  131. {
  132. unsigned long evbit[NBITS(EV_MAX)] = { 0 };
  133. unsigned long keybit[NBITS(KEY_MAX)] = { 0 };
  134. unsigned long absbit[NBITS(ABS_MAX)] = { 0 };
  135. unsigned long relbit[NBITS(REL_MAX)] = { 0 };
  136. int devclass;
  137. if ((ioctl(fd, EVIOCGBIT(0, sizeof(evbit)), evbit) < 0) ||
  138. (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) < 0) ||
  139. (ioctl(fd, EVIOCGBIT(EV_REL, sizeof(relbit)), relbit) < 0) ||
  140. (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) < 0)) {
  141. return (0);
  142. }
  143. devclass = SDL_EVDEV_GuessDeviceClass(evbit, absbit, keybit, relbit);
  144. if (devclass & SDL_UDEV_DEVICE_JOYSTICK) {
  145. return 1;
  146. }
  147. return 0;
  148. }
  149. static int
  150. IsJoystick(int fd, char **name_return, SDL_JoystickGUID *guid)
  151. {
  152. struct input_id inpid;
  153. Uint16 *guid16 = (Uint16 *)guid->data;
  154. char *name;
  155. char product_string[128];
  156. /* When udev is enabled we only get joystick devices here, so there's no need to test them */
  157. if (enumeration_method != ENUMERATION_LIBUDEV && !GuessIsJoystick(fd)) {
  158. return 0;
  159. }
  160. if (ioctl(fd, EVIOCGID, &inpid) < 0) {
  161. return 0;
  162. }
  163. if (ioctl(fd, EVIOCGNAME(sizeof(product_string)), product_string) < 0) {
  164. return 0;
  165. }
  166. name = SDL_CreateJoystickName(inpid.vendor, inpid.product, NULL, product_string);
  167. if (!name) {
  168. return 0;
  169. }
  170. #ifdef SDL_JOYSTICK_HIDAPI
  171. if (!IsVirtualJoystick(inpid.vendor, inpid.product, inpid.version, name) &&
  172. HIDAPI_IsDevicePresent(inpid.vendor, inpid.product, inpid.version, name)) {
  173. /* The HIDAPI driver is taking care of this device */
  174. SDL_free(name);
  175. return 0;
  176. }
  177. #endif
  178. FixupDeviceInfoForMapping(fd, &inpid);
  179. #ifdef DEBUG_JOYSTICK
  180. printf("Joystick: %s, bustype = %d, vendor = 0x%.4x, product = 0x%.4x, version = %d\n", name, inpid.bustype, inpid.vendor, inpid.product, inpid.version);
  181. #endif
  182. SDL_memset(guid->data, 0, sizeof(guid->data));
  183. /* We only need 16 bits for each of these; space them out to fill 128. */
  184. /* Byteswap so devices get same GUID on little/big endian platforms. */
  185. *guid16++ = SDL_SwapLE16(inpid.bustype);
  186. *guid16++ = 0;
  187. if (inpid.vendor && inpid.product) {
  188. *guid16++ = SDL_SwapLE16(inpid.vendor);
  189. *guid16++ = 0;
  190. *guid16++ = SDL_SwapLE16(inpid.product);
  191. *guid16++ = 0;
  192. *guid16++ = SDL_SwapLE16(inpid.version);
  193. *guid16++ = 0;
  194. } else {
  195. SDL_strlcpy((char*)guid16, name, sizeof(guid->data) - 4);
  196. }
  197. if (SDL_ShouldIgnoreJoystick(name, *guid)) {
  198. SDL_free(name);
  199. return 0;
  200. }
  201. *name_return = name;
  202. return 1;
  203. }
  204. #if SDL_USE_LIBUDEV
  205. static void joystick_udev_callback(SDL_UDEV_deviceevent udev_type, int udev_class, const char *devpath)
  206. {
  207. if (devpath == NULL) {
  208. return;
  209. }
  210. switch (udev_type) {
  211. case SDL_UDEV_DEVICEADDED:
  212. if (!(udev_class & SDL_UDEV_DEVICE_JOYSTICK)) {
  213. return;
  214. }
  215. MaybeAddDevice(devpath);
  216. break;
  217. case SDL_UDEV_DEVICEREMOVED:
  218. MaybeRemoveDevice(devpath);
  219. break;
  220. default:
  221. break;
  222. }
  223. }
  224. #endif /* SDL_USE_LIBUDEV */
  225. static void
  226. FreeJoylistItem(SDL_joylist_item *item)
  227. {
  228. SDL_free(item->mapping);
  229. SDL_free(item->path);
  230. SDL_free(item->name);
  231. SDL_free(item);
  232. }
  233. static int
  234. MaybeAddDevice(const char *path)
  235. {
  236. struct stat sb;
  237. int fd = -1;
  238. int isstick = 0;
  239. char *name = NULL;
  240. SDL_JoystickGUID guid;
  241. SDL_joylist_item *item;
  242. if (path == NULL) {
  243. return -1;
  244. }
  245. if (stat(path, &sb) == -1) {
  246. return -1;
  247. }
  248. /* Check to make sure it's not already in list. */
  249. for (item = SDL_joylist; item != NULL; item = item->next) {
  250. if (sb.st_rdev == item->devnum) {
  251. return -1; /* already have this one */
  252. }
  253. }
  254. fd = open(path, O_RDONLY, 0);
  255. if (fd < 0) {
  256. return -1;
  257. }
  258. #ifdef DEBUG_INPUT_EVENTS
  259. printf("Checking %s\n", path);
  260. #endif
  261. isstick = IsJoystick(fd, &name, &guid);
  262. close(fd);
  263. if (!isstick) {
  264. return -1;
  265. }
  266. item = (SDL_joylist_item *) SDL_calloc(1, sizeof (SDL_joylist_item));
  267. if (item == NULL) {
  268. return -1;
  269. }
  270. item->devnum = sb.st_rdev;
  271. item->path = SDL_strdup(path);
  272. item->name = name;
  273. item->guid = guid;
  274. if ((item->path == NULL) || (item->name == NULL)) {
  275. FreeJoylistItem(item);
  276. return -1;
  277. }
  278. item->device_instance = SDL_GetNextJoystickInstanceID();
  279. if (SDL_joylist_tail == NULL) {
  280. SDL_joylist = SDL_joylist_tail = item;
  281. } else {
  282. SDL_joylist_tail->next = item;
  283. SDL_joylist_tail = item;
  284. }
  285. /* Need to increment the joystick count before we post the event */
  286. ++numjoysticks;
  287. SDL_PrivateJoystickAdded(item->device_instance);
  288. return numjoysticks;
  289. }
  290. static void
  291. RemoveJoylistItem(SDL_joylist_item *item, SDL_joylist_item *prev)
  292. {
  293. if (item->hwdata) {
  294. item->hwdata->item = NULL;
  295. }
  296. if (prev != NULL) {
  297. prev->next = item->next;
  298. } else {
  299. SDL_assert(SDL_joylist == item);
  300. SDL_joylist = item->next;
  301. }
  302. if (item == SDL_joylist_tail) {
  303. SDL_joylist_tail = prev;
  304. }
  305. /* Need to decrement the joystick count before we post the event */
  306. --numjoysticks;
  307. SDL_PrivateJoystickRemoved(item->device_instance);
  308. FreeJoylistItem(item);
  309. }
  310. static int
  311. MaybeRemoveDevice(const char *path)
  312. {
  313. SDL_joylist_item *item;
  314. SDL_joylist_item *prev = NULL;
  315. if (path == NULL) {
  316. return -1;
  317. }
  318. for (item = SDL_joylist; item != NULL; item = item->next) {
  319. /* found it, remove it. */
  320. if (SDL_strcmp(path, item->path) == 0) {
  321. const int retval = item->device_instance;
  322. RemoveJoylistItem(item, prev);
  323. return retval;
  324. }
  325. prev = item;
  326. }
  327. return -1;
  328. }
  329. static void
  330. HandlePendingRemovals(void)
  331. {
  332. SDL_joylist_item *prev = NULL;
  333. SDL_joylist_item *item = SDL_joylist;
  334. while (item != NULL) {
  335. if (item->hwdata && item->hwdata->gone) {
  336. RemoveJoylistItem(item, prev);
  337. if (prev != NULL) {
  338. item = prev->next;
  339. } else {
  340. item = SDL_joylist;
  341. }
  342. } else {
  343. prev = item;
  344. item = item->next;
  345. }
  346. }
  347. }
  348. static SDL_bool SteamControllerConnectedCallback(const char *name, SDL_JoystickGUID guid, int *device_instance)
  349. {
  350. SDL_joylist_item *item;
  351. item = (SDL_joylist_item *) SDL_calloc(1, sizeof (SDL_joylist_item));
  352. if (item == NULL) {
  353. return SDL_FALSE;
  354. }
  355. item->path = SDL_strdup("");
  356. item->name = SDL_strdup(name);
  357. item->guid = guid;
  358. item->m_bSteamController = SDL_TRUE;
  359. if ((item->path == NULL) || (item->name == NULL)) {
  360. FreeJoylistItem(item);
  361. return SDL_FALSE;
  362. }
  363. *device_instance = item->device_instance = SDL_GetNextJoystickInstanceID();
  364. if (SDL_joylist_tail == NULL) {
  365. SDL_joylist = SDL_joylist_tail = item;
  366. } else {
  367. SDL_joylist_tail->next = item;
  368. SDL_joylist_tail = item;
  369. }
  370. /* Need to increment the joystick count before we post the event */
  371. ++numjoysticks;
  372. SDL_PrivateJoystickAdded(item->device_instance);
  373. return SDL_TRUE;
  374. }
  375. static void SteamControllerDisconnectedCallback(int device_instance)
  376. {
  377. SDL_joylist_item *item;
  378. SDL_joylist_item *prev = NULL;
  379. for (item = SDL_joylist; item != NULL; item = item->next) {
  380. /* found it, remove it. */
  381. if (item->device_instance == device_instance) {
  382. RemoveJoylistItem(item, prev);
  383. return;
  384. }
  385. prev = item;
  386. }
  387. }
  388. #ifdef HAVE_INOTIFY
  389. #ifdef HAVE_INOTIFY_INIT1
  390. static int SDL_inotify_init1(void) {
  391. return inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
  392. }
  393. #else
  394. static int SDL_inotify_init1(void) {
  395. int fd = inotify_init();
  396. if (fd < 0) return -1;
  397. fcntl(fd, F_SETFL, O_NONBLOCK);
  398. fcntl(fd, F_SETFD, FD_CLOEXEC);
  399. return fd;
  400. }
  401. #endif
  402. static int
  403. StrHasPrefix(const char *string, const char *prefix)
  404. {
  405. return (SDL_strncmp(string, prefix, SDL_strlen(prefix)) == 0);
  406. }
  407. static int
  408. StrIsInteger(const char *string)
  409. {
  410. const char *p;
  411. if (*string == '\0') {
  412. return 0;
  413. }
  414. for (p = string; *p != '\0'; p++) {
  415. if (*p < '0' || *p > '9') {
  416. return 0;
  417. }
  418. }
  419. return 1;
  420. }
  421. static void
  422. LINUX_InotifyJoystickDetect(void)
  423. {
  424. union
  425. {
  426. struct inotify_event event;
  427. char storage[4096];
  428. char enough_for_inotify[sizeof (struct inotify_event) + NAME_MAX + 1];
  429. } buf;
  430. ssize_t bytes;
  431. size_t remain = 0;
  432. size_t len;
  433. bytes = read(inotify_fd, &buf, sizeof (buf));
  434. if (bytes > 0) {
  435. remain = (size_t) bytes;
  436. }
  437. while (remain > 0) {
  438. if (buf.event.len > 0) {
  439. if (StrHasPrefix(buf.event.name, "event") &&
  440. StrIsInteger(buf.event.name + strlen ("event"))) {
  441. char path[PATH_MAX];
  442. SDL_snprintf(path, SDL_arraysize(path), "/dev/input/%s", buf.event.name);
  443. if (buf.event.mask & (IN_CREATE | IN_MOVED_TO | IN_ATTRIB)) {
  444. MaybeAddDevice(path);
  445. }
  446. else if (buf.event.mask & (IN_DELETE | IN_MOVED_FROM)) {
  447. MaybeRemoveDevice(path);
  448. }
  449. }
  450. }
  451. len = sizeof (struct inotify_event) + buf.event.len;
  452. remain -= len;
  453. if (remain != 0) {
  454. memmove (&buf.storage[0], &buf.storage[len], remain);
  455. }
  456. }
  457. }
  458. #endif /* HAVE_INOTIFY */
  459. /* Detect devices by reading /dev/input. In the inotify code path we
  460. * have to do this the first time, to detect devices that already existed
  461. * before we started; in the non-inotify code path we do this repeatedly
  462. * (polling). */
  463. static int
  464. filter_entries(const struct dirent *entry)
  465. {
  466. return (SDL_strlen(entry->d_name) > 5 && SDL_strncmp(entry->d_name, "event", 5) == 0);
  467. }
  468. static int
  469. sort_entries(const struct dirent **a, const struct dirent **b)
  470. {
  471. int numA = SDL_atoi((*a)->d_name+5);
  472. int numB = SDL_atoi((*b)->d_name+5);
  473. return (numA - numB);
  474. }
  475. static void
  476. LINUX_FallbackJoystickDetect(void)
  477. {
  478. const Uint32 SDL_JOY_DETECT_INTERVAL_MS = 3000; /* Update every 3 seconds */
  479. Uint32 now = SDL_GetTicks();
  480. if (!last_joy_detect_time || SDL_TICKS_PASSED(now, last_joy_detect_time + SDL_JOY_DETECT_INTERVAL_MS)) {
  481. struct stat sb;
  482. /* Opening input devices can generate synchronous device I/O, so avoid it if we can */
  483. if (stat("/dev/input", &sb) == 0 && sb.st_mtime != last_input_dir_mtime) {
  484. int i, count;
  485. struct dirent **entries;
  486. char path[PATH_MAX];
  487. count = scandir("/dev/input", &entries, filter_entries, sort_entries);
  488. for (i = 0; i < count; ++i) {
  489. SDL_snprintf(path, SDL_arraysize(path), "/dev/input/%s", entries[i]->d_name);
  490. MaybeAddDevice(path);
  491. free(entries[i]); /* This should NOT be SDL_free() */
  492. }
  493. free(entries); /* This should NOT be SDL_free() */
  494. last_input_dir_mtime = sb.st_mtime;
  495. }
  496. last_joy_detect_time = now;
  497. }
  498. }
  499. static void
  500. LINUX_JoystickDetect(void)
  501. {
  502. #if SDL_USE_LIBUDEV
  503. if (enumeration_method == ENUMERATION_LIBUDEV) {
  504. SDL_UDEV_Poll();
  505. }
  506. else
  507. #endif
  508. #ifdef HAVE_INOTIFY
  509. if (inotify_fd >= 0 && last_joy_detect_time != 0) {
  510. LINUX_InotifyJoystickDetect();
  511. }
  512. else
  513. #endif
  514. {
  515. LINUX_FallbackJoystickDetect();
  516. }
  517. HandlePendingRemovals();
  518. SDL_UpdateSteamControllers();
  519. }
  520. static int
  521. LINUX_JoystickInit(void)
  522. {
  523. const char *devices = SDL_GetHint("SDL_JOYSTICK_DEVICE");
  524. #if SDL_USE_LIBUDEV
  525. if (enumeration_method == ENUMERATION_UNSET) {
  526. if (!SDL_GetHintBoolean("SDL_JOYSTICK_DISABLE_UDEV", SDL_FALSE)) {
  527. SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
  528. "udev disabled by SDL_JOYSTICK_DISABLE_UDEV");
  529. enumeration_method = ENUMERATION_FALLBACK;
  530. } else if (access("/.flatpak-info", F_OK) == 0
  531. || access("/run/host/container-manager", F_OK) == 0) {
  532. /* Explicitly check `/.flatpak-info` because, for old versions of
  533. * Flatpak, this was the only available way to tell if we were in
  534. * a Flatpak container. */
  535. SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
  536. "Container detected, disabling udev integration");
  537. enumeration_method = ENUMERATION_FALLBACK;
  538. } else {
  539. SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
  540. "Using udev for joystick device discovery");
  541. enumeration_method = ENUMERATION_LIBUDEV;
  542. }
  543. }
  544. #endif
  545. /* First see if the user specified one or more joysticks to use */
  546. if (devices != NULL) {
  547. char *envcopy, *envpath, *delim;
  548. envcopy = SDL_strdup(devices);
  549. envpath = envcopy;
  550. while (envpath != NULL) {
  551. delim = SDL_strchr(envpath, ':');
  552. if (delim != NULL) {
  553. *delim++ = '\0';
  554. }
  555. MaybeAddDevice(envpath);
  556. envpath = delim;
  557. }
  558. SDL_free(envcopy);
  559. }
  560. SDL_InitSteamControllers(SteamControllerConnectedCallback,
  561. SteamControllerDisconnectedCallback);
  562. /* Force immediate joystick detection if using fallback */
  563. last_joy_detect_time = 0;
  564. last_input_dir_mtime = 0;
  565. /* Manually scan first, since we sort by device number and udev doesn't */
  566. LINUX_JoystickDetect();
  567. #if SDL_USE_LIBUDEV
  568. if (enumeration_method == ENUMERATION_LIBUDEV) {
  569. if (SDL_UDEV_Init() < 0) {
  570. return SDL_SetError("Could not initialize UDEV");
  571. }
  572. /* Set up the udev callback */
  573. if (SDL_UDEV_AddCallback(joystick_udev_callback) < 0) {
  574. SDL_UDEV_Quit();
  575. return SDL_SetError("Could not set up joystick <-> udev callback");
  576. }
  577. /* Force a scan to build the initial device list */
  578. SDL_UDEV_Scan();
  579. }
  580. else
  581. #endif
  582. {
  583. #if defined(HAVE_INOTIFY)
  584. inotify_fd = SDL_inotify_init1();
  585. if (inotify_fd < 0) {
  586. SDL_LogWarn(SDL_LOG_CATEGORY_INPUT,
  587. "Unable to initialize inotify, falling back to polling: %s",
  588. strerror (errno));
  589. } else {
  590. /* We need to watch for attribute changes in addition to
  591. * creation, because when a device is first created, it has
  592. * permissions that we can't read. When udev chmods it to
  593. * something that we maybe *can* read, we'll get an
  594. * IN_ATTRIB event to tell us. */
  595. if (inotify_add_watch(inotify_fd, "/dev/input",
  596. IN_CREATE | IN_DELETE | IN_MOVE | IN_ATTRIB) < 0) {
  597. close(inotify_fd);
  598. inotify_fd = -1;
  599. SDL_LogWarn(SDL_LOG_CATEGORY_INPUT,
  600. "Unable to add inotify watch, falling back to polling: %s",
  601. strerror (errno));
  602. }
  603. }
  604. #endif /* HAVE_INOTIFY */
  605. }
  606. return 0;
  607. }
  608. static int
  609. LINUX_JoystickGetCount(void)
  610. {
  611. return numjoysticks;
  612. }
  613. static SDL_joylist_item *
  614. JoystickByDevIndex(int device_index)
  615. {
  616. SDL_joylist_item *item = SDL_joylist;
  617. if ((device_index < 0) || (device_index >= numjoysticks)) {
  618. return NULL;
  619. }
  620. while (device_index > 0) {
  621. SDL_assert(item != NULL);
  622. device_index--;
  623. item = item->next;
  624. }
  625. return item;
  626. }
  627. /* Function to get the device-dependent name of a joystick */
  628. static const char *
  629. LINUX_JoystickGetDeviceName(int device_index)
  630. {
  631. return JoystickByDevIndex(device_index)->name;
  632. }
  633. static int
  634. LINUX_JoystickGetDevicePlayerIndex(int device_index)
  635. {
  636. return -1;
  637. }
  638. static void
  639. LINUX_JoystickSetDevicePlayerIndex(int device_index, int player_index)
  640. {
  641. }
  642. static SDL_JoystickGUID
  643. LINUX_JoystickGetDeviceGUID( int device_index )
  644. {
  645. return JoystickByDevIndex(device_index)->guid;
  646. }
  647. /* Function to perform the mapping from device index to the instance id for this index */
  648. static SDL_JoystickID
  649. LINUX_JoystickGetDeviceInstanceID(int device_index)
  650. {
  651. return JoystickByDevIndex(device_index)->device_instance;
  652. }
  653. static int
  654. allocate_hatdata(SDL_Joystick *joystick)
  655. {
  656. int i;
  657. joystick->hwdata->hats =
  658. (struct hwdata_hat *) SDL_malloc(joystick->nhats *
  659. sizeof(struct hwdata_hat));
  660. if (joystick->hwdata->hats == NULL) {
  661. return (-1);
  662. }
  663. for (i = 0; i < joystick->nhats; ++i) {
  664. joystick->hwdata->hats[i].axis[0] = 1;
  665. joystick->hwdata->hats[i].axis[1] = 1;
  666. }
  667. return (0);
  668. }
  669. static int
  670. allocate_balldata(SDL_Joystick *joystick)
  671. {
  672. int i;
  673. joystick->hwdata->balls =
  674. (struct hwdata_ball *) SDL_malloc(joystick->nballs *
  675. sizeof(struct hwdata_ball));
  676. if (joystick->hwdata->balls == NULL) {
  677. return (-1);
  678. }
  679. for (i = 0; i < joystick->nballs; ++i) {
  680. joystick->hwdata->balls[i].axis[0] = 0;
  681. joystick->hwdata->balls[i].axis[1] = 0;
  682. }
  683. return (0);
  684. }
  685. static void
  686. ConfigJoystick(SDL_Joystick *joystick, int fd)
  687. {
  688. int i, t;
  689. unsigned long keybit[NBITS(KEY_MAX)] = { 0 };
  690. unsigned long absbit[NBITS(ABS_MAX)] = { 0 };
  691. unsigned long relbit[NBITS(REL_MAX)] = { 0 };
  692. unsigned long ffbit[NBITS(FF_MAX)] = { 0 };
  693. SDL_bool use_deadzones = SDL_GetHintBoolean(SDL_HINT_LINUX_JOYSTICK_DEADZONES, SDL_FALSE);
  694. /* See if this device uses the new unified event API */
  695. if ((ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) >= 0) &&
  696. (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) >= 0) &&
  697. (ioctl(fd, EVIOCGBIT(EV_REL, sizeof(relbit)), relbit) >= 0)) {
  698. /* Get the number of buttons, axes, and other thingamajigs */
  699. for (i = BTN_JOYSTICK; i < KEY_MAX; ++i) {
  700. if (test_bit(i, keybit)) {
  701. #ifdef DEBUG_INPUT_EVENTS
  702. printf("Joystick has button: 0x%x\n", i);
  703. #endif
  704. joystick->hwdata->key_map[i] = joystick->nbuttons;
  705. joystick->hwdata->has_key[i] = SDL_TRUE;
  706. ++joystick->nbuttons;
  707. }
  708. }
  709. for (i = 0; i < BTN_JOYSTICK; ++i) {
  710. if (test_bit(i, keybit)) {
  711. #ifdef DEBUG_INPUT_EVENTS
  712. printf("Joystick has button: 0x%x\n", i);
  713. #endif
  714. joystick->hwdata->key_map[i] = joystick->nbuttons;
  715. joystick->hwdata->has_key[i] = SDL_TRUE;
  716. ++joystick->nbuttons;
  717. }
  718. }
  719. for (i = 0; i < ABS_MAX; ++i) {
  720. /* Skip hats */
  721. if (i == ABS_HAT0X) {
  722. i = ABS_HAT3Y;
  723. continue;
  724. }
  725. if (test_bit(i, absbit)) {
  726. struct input_absinfo absinfo;
  727. struct axis_correct *correct = &joystick->hwdata->abs_correct[i];
  728. if (ioctl(fd, EVIOCGABS(i), &absinfo) < 0) {
  729. continue;
  730. }
  731. #ifdef DEBUG_INPUT_EVENTS
  732. printf("Joystick has absolute axis: 0x%.2x\n", i);
  733. printf("Values = { %d, %d, %d, %d, %d }\n",
  734. absinfo.value, absinfo.minimum, absinfo.maximum,
  735. absinfo.fuzz, absinfo.flat);
  736. #endif /* DEBUG_INPUT_EVENTS */
  737. joystick->hwdata->abs_map[i] = joystick->naxes;
  738. joystick->hwdata->has_abs[i] = SDL_TRUE;
  739. correct->minimum = absinfo.minimum;
  740. correct->maximum = absinfo.maximum;
  741. if (correct->minimum != correct->maximum) {
  742. if (use_deadzones) {
  743. correct->use_deadzones = SDL_TRUE;
  744. correct->coef[0] = (absinfo.maximum + absinfo.minimum) - 2 * absinfo.flat;
  745. correct->coef[1] = (absinfo.maximum + absinfo.minimum) + 2 * absinfo.flat;
  746. t = ((absinfo.maximum - absinfo.minimum) - 4 * absinfo.flat);
  747. if (t != 0) {
  748. correct->coef[2] = (1 << 28) / t;
  749. } else {
  750. correct->coef[2] = 0;
  751. }
  752. } else {
  753. float value_range = (correct->maximum - correct->minimum);
  754. float output_range = (SDL_JOYSTICK_AXIS_MAX - SDL_JOYSTICK_AXIS_MIN);
  755. correct->scale = (output_range / value_range);
  756. }
  757. }
  758. ++joystick->naxes;
  759. }
  760. }
  761. for (i = ABS_HAT0X; i <= ABS_HAT3Y; i += 2) {
  762. if (test_bit(i, absbit) || test_bit(i + 1, absbit)) {
  763. struct input_absinfo absinfo;
  764. int hat_index = (i - ABS_HAT0X) / 2;
  765. if (ioctl(fd, EVIOCGABS(i), &absinfo) < 0) {
  766. continue;
  767. }
  768. #ifdef DEBUG_INPUT_EVENTS
  769. printf("Joystick has hat %d\n", hat_index);
  770. printf("Values = { %d, %d, %d, %d, %d }\n",
  771. absinfo.value, absinfo.minimum, absinfo.maximum,
  772. absinfo.fuzz, absinfo.flat);
  773. #endif /* DEBUG_INPUT_EVENTS */
  774. joystick->hwdata->hats_indices[hat_index] = joystick->nhats++;
  775. joystick->hwdata->has_hat[hat_index] = SDL_TRUE;
  776. }
  777. }
  778. if (test_bit(REL_X, relbit) || test_bit(REL_Y, relbit)) {
  779. ++joystick->nballs;
  780. }
  781. /* Allocate data to keep track of these thingamajigs */
  782. if (joystick->nhats > 0) {
  783. if (allocate_hatdata(joystick) < 0) {
  784. joystick->nhats = 0;
  785. }
  786. }
  787. if (joystick->nballs > 0) {
  788. if (allocate_balldata(joystick) < 0) {
  789. joystick->nballs = 0;
  790. }
  791. }
  792. }
  793. if (ioctl(fd, EVIOCGBIT(EV_FF, sizeof(ffbit)), ffbit) >= 0) {
  794. if (test_bit(FF_RUMBLE, ffbit)) {
  795. joystick->hwdata->ff_rumble = SDL_TRUE;
  796. }
  797. if (test_bit(FF_SINE, ffbit)) {
  798. joystick->hwdata->ff_sine = SDL_TRUE;
  799. }
  800. }
  801. }
  802. /* This is used to do the heavy lifting for LINUX_JoystickOpen and
  803. also LINUX_JoystickGetGamepadMapping, so we can query the hardware
  804. without adding an opened SDL_Joystick object to the system.
  805. This expects `joystick->hwdata` to be allocated and will not free it
  806. on error. Returns -1 on error, 0 on success. */
  807. static int
  808. PrepareJoystickHwdata(SDL_Joystick *joystick, SDL_joylist_item *item)
  809. {
  810. joystick->hwdata->item = item;
  811. joystick->hwdata->guid = item->guid;
  812. joystick->hwdata->effect.id = -1;
  813. joystick->hwdata->m_bSteamController = item->m_bSteamController;
  814. SDL_memset(joystick->hwdata->abs_map, 0xFF, sizeof(joystick->hwdata->abs_map));
  815. if (item->m_bSteamController) {
  816. joystick->hwdata->fd = -1;
  817. SDL_GetSteamControllerInputs(&joystick->nbuttons,
  818. &joystick->naxes,
  819. &joystick->nhats);
  820. } else {
  821. const int fd = open(item->path, O_RDWR, 0);
  822. if (fd < 0) {
  823. return SDL_SetError("Unable to open %s", item->path);
  824. }
  825. joystick->hwdata->fd = fd;
  826. joystick->hwdata->fname = SDL_strdup(item->path);
  827. if (joystick->hwdata->fname == NULL) {
  828. close(fd);
  829. return SDL_OutOfMemory();
  830. }
  831. /* Set the joystick to non-blocking read mode */
  832. fcntl(fd, F_SETFL, O_NONBLOCK);
  833. /* Get the number of buttons and axes on the joystick */
  834. ConfigJoystick(joystick, fd);
  835. }
  836. return 0;
  837. }
  838. /* Function to open a joystick for use.
  839. The joystick to open is specified by the device index.
  840. This should fill the nbuttons and naxes fields of the joystick structure.
  841. It returns 0, or -1 if there is an error.
  842. */
  843. static int
  844. LINUX_JoystickOpen(SDL_Joystick *joystick, int device_index)
  845. {
  846. SDL_joylist_item *item = JoystickByDevIndex(device_index);
  847. if (item == NULL) {
  848. return SDL_SetError("No such device");
  849. }
  850. joystick->instance_id = item->device_instance;
  851. joystick->hwdata = (struct joystick_hwdata *)
  852. SDL_calloc(1, sizeof(*joystick->hwdata));
  853. if (joystick->hwdata == NULL) {
  854. return SDL_OutOfMemory();
  855. }
  856. if (PrepareJoystickHwdata(joystick, item) == -1) {
  857. SDL_free(joystick->hwdata);
  858. joystick->hwdata = NULL;
  859. return -1; /* SDL_SetError will already have been called */
  860. }
  861. SDL_assert(item->hwdata == NULL);
  862. item->hwdata = joystick->hwdata;
  863. /* mark joystick as fresh and ready */
  864. joystick->hwdata->fresh = SDL_TRUE;
  865. return 0;
  866. }
  867. static int
  868. LINUX_JoystickRumble(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
  869. {
  870. struct input_event event;
  871. if (joystick->hwdata->ff_rumble) {
  872. struct ff_effect *effect = &joystick->hwdata->effect;
  873. effect->type = FF_RUMBLE;
  874. effect->replay.length = SDL_MAX_RUMBLE_DURATION_MS;
  875. effect->u.rumble.strong_magnitude = low_frequency_rumble;
  876. effect->u.rumble.weak_magnitude = high_frequency_rumble;
  877. } else if (joystick->hwdata->ff_sine) {
  878. /* Scale and average the two rumble strengths */
  879. Sint16 magnitude = (Sint16)(((low_frequency_rumble / 2) + (high_frequency_rumble / 2)) / 2);
  880. struct ff_effect *effect = &joystick->hwdata->effect;
  881. effect->type = FF_PERIODIC;
  882. effect->replay.length = SDL_MAX_RUMBLE_DURATION_MS;
  883. effect->u.periodic.waveform = FF_SINE;
  884. effect->u.periodic.magnitude = magnitude;
  885. } else {
  886. return SDL_Unsupported();
  887. }
  888. if (ioctl(joystick->hwdata->fd, EVIOCSFF, &joystick->hwdata->effect) < 0) {
  889. /* The kernel may have lost this effect, try to allocate a new one */
  890. joystick->hwdata->effect.id = -1;
  891. if (ioctl(joystick->hwdata->fd, EVIOCSFF, &joystick->hwdata->effect) < 0) {
  892. return SDL_SetError("Couldn't update rumble effect: %s", strerror(errno));
  893. }
  894. }
  895. event.type = EV_FF;
  896. event.code = joystick->hwdata->effect.id;
  897. event.value = 1;
  898. if (write(joystick->hwdata->fd, &event, sizeof(event)) < 0) {
  899. return SDL_SetError("Couldn't start rumble effect: %s", strerror(errno));
  900. }
  901. return 0;
  902. }
  903. static int
  904. LINUX_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
  905. {
  906. return SDL_Unsupported();
  907. }
  908. static SDL_bool
  909. LINUX_JoystickHasLED(SDL_Joystick *joystick)
  910. {
  911. return SDL_FALSE;
  912. }
  913. static int
  914. LINUX_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
  915. {
  916. return SDL_Unsupported();
  917. }
  918. static int
  919. LINUX_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)
  920. {
  921. return SDL_Unsupported();
  922. }
  923. static int
  924. LINUX_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled)
  925. {
  926. return SDL_Unsupported();
  927. }
  928. static SDL_INLINE void
  929. HandleHat(SDL_Joystick *stick, Uint8 hat, int axis, int value)
  930. {
  931. struct hwdata_hat *the_hat;
  932. const Uint8 position_map[3][3] = {
  933. {SDL_HAT_LEFTUP, SDL_HAT_UP, SDL_HAT_RIGHTUP},
  934. {SDL_HAT_LEFT, SDL_HAT_CENTERED, SDL_HAT_RIGHT},
  935. {SDL_HAT_LEFTDOWN, SDL_HAT_DOWN, SDL_HAT_RIGHTDOWN}
  936. };
  937. the_hat = &stick->hwdata->hats[hat];
  938. if (value < 0) {
  939. value = 0;
  940. } else if (value == 0) {
  941. value = 1;
  942. } else if (value > 0) {
  943. value = 2;
  944. }
  945. if (value != the_hat->axis[axis]) {
  946. the_hat->axis[axis] = value;
  947. SDL_PrivateJoystickHat(stick, hat,
  948. position_map[the_hat->axis[1]][the_hat->axis[0]]);
  949. }
  950. }
  951. static SDL_INLINE void
  952. HandleBall(SDL_Joystick *stick, Uint8 ball, int axis, int value)
  953. {
  954. stick->hwdata->balls[ball].axis[axis] += value;
  955. }
  956. static SDL_INLINE int
  957. AxisCorrect(SDL_Joystick *joystick, int which, int value)
  958. {
  959. struct axis_correct *correct;
  960. correct = &joystick->hwdata->abs_correct[which];
  961. if (correct->minimum != correct->maximum) {
  962. if (correct->use_deadzones) {
  963. value *= 2;
  964. if (value > correct->coef[0]) {
  965. if (value < correct->coef[1]) {
  966. return 0;
  967. }
  968. value -= correct->coef[1];
  969. } else {
  970. value -= correct->coef[0];
  971. }
  972. value *= correct->coef[2];
  973. value >>= 13;
  974. } else {
  975. value = (int)SDL_floorf((value - correct->minimum) * correct->scale + SDL_JOYSTICK_AXIS_MIN + 0.5f);
  976. }
  977. }
  978. /* Clamp and return */
  979. if (value < SDL_JOYSTICK_AXIS_MIN) {
  980. return SDL_JOYSTICK_AXIS_MIN;
  981. }
  982. if (value > SDL_JOYSTICK_AXIS_MAX) {
  983. return SDL_JOYSTICK_AXIS_MAX;
  984. }
  985. return value;
  986. }
  987. static SDL_INLINE void
  988. PollAllValues(SDL_Joystick *joystick)
  989. {
  990. struct input_absinfo absinfo;
  991. unsigned long keyinfo[NBITS(KEY_MAX)];
  992. int i;
  993. /* Poll all axis */
  994. for (i = ABS_X; i < ABS_MAX; i++) {
  995. if (i == ABS_HAT0X) { /* we handle hats in the next loop, skip them for now. */
  996. i = ABS_HAT3Y;
  997. continue;
  998. }
  999. if (joystick->hwdata->has_abs[i]) {
  1000. if (ioctl(joystick->hwdata->fd, EVIOCGABS(i), &absinfo) >= 0) {
  1001. absinfo.value = AxisCorrect(joystick, i, absinfo.value);
  1002. #ifdef DEBUG_INPUT_EVENTS
  1003. printf("Joystick : Re-read Axis %d (%d) val= %d\n",
  1004. joystick->hwdata->abs_map[i], i, absinfo.value);
  1005. #endif
  1006. SDL_PrivateJoystickAxis(joystick,
  1007. joystick->hwdata->abs_map[i],
  1008. absinfo.value);
  1009. }
  1010. }
  1011. }
  1012. /* Poll all hats */
  1013. for (i = ABS_HAT0X; i <= ABS_HAT3Y; i++) {
  1014. const int baseaxis = i - ABS_HAT0X;
  1015. const int hatidx = baseaxis / 2;
  1016. SDL_assert(hatidx < SDL_arraysize(joystick->hwdata->has_hat));
  1017. if (joystick->hwdata->has_hat[hatidx]) {
  1018. if (ioctl(joystick->hwdata->fd, EVIOCGABS(i), &absinfo) >= 0) {
  1019. const int hataxis = baseaxis % 2;
  1020. HandleHat(joystick, joystick->hwdata->hats_indices[hatidx], hataxis, absinfo.value);
  1021. }
  1022. }
  1023. }
  1024. /* Poll all buttons */
  1025. SDL_zeroa(keyinfo);
  1026. if (ioctl(joystick->hwdata->fd, EVIOCGKEY(sizeof (keyinfo)), keyinfo) >= 0) {
  1027. for (i = 0; i < KEY_MAX; i++) {
  1028. if (joystick->hwdata->has_key[i]) {
  1029. const Uint8 value = test_bit(i, keyinfo) ? SDL_PRESSED : SDL_RELEASED;
  1030. #ifdef DEBUG_INPUT_EVENTS
  1031. printf("Joystick : Re-read Button %d (%d) val= %d\n",
  1032. joystick->hwdata->key_map[i], i, value);
  1033. #endif
  1034. SDL_PrivateJoystickButton(joystick,
  1035. joystick->hwdata->key_map[i], value);
  1036. }
  1037. }
  1038. }
  1039. /* Joyballs are relative input, so there's no poll state. Events only! */
  1040. }
  1041. static SDL_INLINE void
  1042. HandleInputEvents(SDL_Joystick *joystick)
  1043. {
  1044. struct input_event events[32];
  1045. int i, len;
  1046. int code;
  1047. if (joystick->hwdata->fresh) {
  1048. PollAllValues(joystick);
  1049. joystick->hwdata->fresh = SDL_FALSE;
  1050. }
  1051. while ((len = read(joystick->hwdata->fd, events, (sizeof events))) > 0) {
  1052. len /= sizeof(events[0]);
  1053. for (i = 0; i < len; ++i) {
  1054. code = events[i].code;
  1055. /* If the kernel sent a SYN_DROPPED, we are supposed to ignore the
  1056. rest of the packet (the end of it signified by a SYN_REPORT) */
  1057. if ( joystick->hwdata->recovering_from_dropped &&
  1058. ((events[i].type != EV_SYN) || (code != SYN_REPORT)) ) {
  1059. continue;
  1060. }
  1061. switch (events[i].type) {
  1062. case EV_KEY:
  1063. SDL_PrivateJoystickButton(joystick,
  1064. joystick->hwdata->key_map[code],
  1065. events[i].value);
  1066. break;
  1067. case EV_ABS:
  1068. switch (code) {
  1069. case ABS_HAT0X:
  1070. case ABS_HAT0Y:
  1071. case ABS_HAT1X:
  1072. case ABS_HAT1Y:
  1073. case ABS_HAT2X:
  1074. case ABS_HAT2Y:
  1075. case ABS_HAT3X:
  1076. case ABS_HAT3Y:
  1077. code -= ABS_HAT0X;
  1078. HandleHat(joystick, joystick->hwdata->hats_indices[code / 2], code % 2, events[i].value);
  1079. break;
  1080. default:
  1081. if (joystick->hwdata->abs_map[code] != 0xFF) {
  1082. events[i].value =
  1083. AxisCorrect(joystick, code, events[i].value);
  1084. SDL_PrivateJoystickAxis(joystick,
  1085. joystick->hwdata->abs_map[code],
  1086. events[i].value);
  1087. }
  1088. break;
  1089. }
  1090. break;
  1091. case EV_REL:
  1092. switch (code) {
  1093. case REL_X:
  1094. case REL_Y:
  1095. code -= REL_X;
  1096. HandleBall(joystick, code / 2, code % 2, events[i].value);
  1097. break;
  1098. default:
  1099. break;
  1100. }
  1101. break;
  1102. case EV_SYN:
  1103. switch (code) {
  1104. case SYN_DROPPED :
  1105. #ifdef DEBUG_INPUT_EVENTS
  1106. printf("Event SYN_DROPPED detected\n");
  1107. #endif
  1108. joystick->hwdata->recovering_from_dropped = SDL_TRUE;
  1109. break;
  1110. case SYN_REPORT :
  1111. if (joystick->hwdata->recovering_from_dropped) {
  1112. joystick->hwdata->recovering_from_dropped = SDL_FALSE;
  1113. PollAllValues(joystick); /* try to sync up to current state now */
  1114. }
  1115. break;
  1116. default:
  1117. break;
  1118. }
  1119. default:
  1120. break;
  1121. }
  1122. }
  1123. }
  1124. if (errno == ENODEV) {
  1125. /* We have to wait until the JoystickDetect callback to remove this */
  1126. joystick->hwdata->gone = SDL_TRUE;
  1127. }
  1128. }
  1129. static void
  1130. LINUX_JoystickUpdate(SDL_Joystick *joystick)
  1131. {
  1132. int i;
  1133. if (joystick->hwdata->m_bSteamController) {
  1134. SDL_UpdateSteamController(joystick);
  1135. return;
  1136. }
  1137. HandleInputEvents(joystick);
  1138. /* Deliver ball motion updates */
  1139. for (i = 0; i < joystick->nballs; ++i) {
  1140. int xrel, yrel;
  1141. xrel = joystick->hwdata->balls[i].axis[0];
  1142. yrel = joystick->hwdata->balls[i].axis[1];
  1143. if (xrel || yrel) {
  1144. joystick->hwdata->balls[i].axis[0] = 0;
  1145. joystick->hwdata->balls[i].axis[1] = 0;
  1146. SDL_PrivateJoystickBall(joystick, (Uint8) i, xrel, yrel);
  1147. }
  1148. }
  1149. }
  1150. /* Function to close a joystick after use */
  1151. static void
  1152. LINUX_JoystickClose(SDL_Joystick *joystick)
  1153. {
  1154. if (joystick->hwdata) {
  1155. if (joystick->hwdata->effect.id >= 0) {
  1156. ioctl(joystick->hwdata->fd, EVIOCRMFF, joystick->hwdata->effect.id);
  1157. joystick->hwdata->effect.id = -1;
  1158. }
  1159. if (joystick->hwdata->fd >= 0) {
  1160. close(joystick->hwdata->fd);
  1161. }
  1162. if (joystick->hwdata->item) {
  1163. joystick->hwdata->item->hwdata = NULL;
  1164. }
  1165. SDL_free(joystick->hwdata->hats);
  1166. SDL_free(joystick->hwdata->balls);
  1167. SDL_free(joystick->hwdata->fname);
  1168. SDL_free(joystick->hwdata);
  1169. }
  1170. }
  1171. /* Function to perform any system-specific joystick related cleanup */
  1172. static void
  1173. LINUX_JoystickQuit(void)
  1174. {
  1175. SDL_joylist_item *item = NULL;
  1176. SDL_joylist_item *next = NULL;
  1177. if (inotify_fd >= 0) {
  1178. close(inotify_fd);
  1179. inotify_fd = -1;
  1180. }
  1181. for (item = SDL_joylist; item; item = next) {
  1182. next = item->next;
  1183. FreeJoylistItem(item);
  1184. }
  1185. SDL_joylist = SDL_joylist_tail = NULL;
  1186. numjoysticks = 0;
  1187. #if SDL_USE_LIBUDEV
  1188. if (enumeration_method == ENUMERATION_LIBUDEV) {
  1189. SDL_UDEV_DelCallback(joystick_udev_callback);
  1190. SDL_UDEV_Quit();
  1191. }
  1192. #endif
  1193. SDL_QuitSteamControllers();
  1194. }
  1195. /*
  1196. This is based on the Linux Gamepad Specification
  1197. available at: https://www.kernel.org/doc/html/v4.15/input/gamepad.html
  1198. */
  1199. static SDL_bool
  1200. LINUX_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
  1201. {
  1202. SDL_Joystick *joystick;
  1203. SDL_joylist_item *item = JoystickByDevIndex(device_index);
  1204. if (item->mapping) {
  1205. SDL_memcpy(out, item->mapping, sizeof(*out));
  1206. return SDL_TRUE;
  1207. }
  1208. /* We temporarily open the device to check how it's configured. Make
  1209. a fake SDL_Joystick object to do so. */
  1210. joystick = (SDL_Joystick *) SDL_calloc(sizeof(*joystick), 1);
  1211. if (joystick == NULL) {
  1212. SDL_OutOfMemory();
  1213. return SDL_FALSE;
  1214. }
  1215. joystick->hwdata = (struct joystick_hwdata *)
  1216. SDL_calloc(1, sizeof(*joystick->hwdata));
  1217. if (joystick->hwdata == NULL) {
  1218. SDL_free(joystick);
  1219. SDL_OutOfMemory();
  1220. return SDL_FALSE;
  1221. }
  1222. if (PrepareJoystickHwdata(joystick, item) == -1) {
  1223. SDL_free(joystick->hwdata);
  1224. SDL_free(joystick);
  1225. return SDL_FALSE; /* SDL_SetError will already have been called */
  1226. }
  1227. /* don't assign `item->hwdata` so it's not in any global state. */
  1228. /* it is now safe to call LINUX_JoystickClose on this fake joystick. */
  1229. if (!joystick->hwdata->has_key[BTN_GAMEPAD]) {
  1230. /* Not a gamepad according to the specs. */
  1231. LINUX_JoystickClose(joystick);
  1232. SDL_free(joystick);
  1233. return SDL_FALSE;
  1234. }
  1235. /* We have a gamepad, start filling out the mappings */
  1236. if (joystick->hwdata->has_key[BTN_A]) {
  1237. out->a.kind = EMappingKind_Button;
  1238. out->a.target = joystick->hwdata->key_map[BTN_A];
  1239. }
  1240. if (joystick->hwdata->has_key[BTN_B]) {
  1241. out->b.kind = EMappingKind_Button;
  1242. out->b.target = joystick->hwdata->key_map[BTN_B];
  1243. }
  1244. if (joystick->hwdata->has_key[BTN_X]) {
  1245. out->x.kind = EMappingKind_Button;
  1246. out->x.target = joystick->hwdata->key_map[BTN_X];
  1247. }
  1248. if (joystick->hwdata->has_key[BTN_Y]) {
  1249. out->y.kind = EMappingKind_Button;
  1250. out->y.target = joystick->hwdata->key_map[BTN_Y];
  1251. }
  1252. if (joystick->hwdata->has_key[BTN_SELECT]) {
  1253. out->back.kind = EMappingKind_Button;
  1254. out->back.target = joystick->hwdata->key_map[BTN_SELECT];
  1255. }
  1256. if (joystick->hwdata->has_key[BTN_START]) {
  1257. out->start.kind = EMappingKind_Button;
  1258. out->start.target = joystick->hwdata->key_map[BTN_START];
  1259. }
  1260. if (joystick->hwdata->has_key[BTN_THUMBL]) {
  1261. out->leftstick.kind = EMappingKind_Button;
  1262. out->leftstick.target = joystick->hwdata->key_map[BTN_THUMBL];
  1263. }
  1264. if (joystick->hwdata->has_key[BTN_THUMBR]) {
  1265. out->rightstick.kind = EMappingKind_Button;
  1266. out->rightstick.target = joystick->hwdata->key_map[BTN_THUMBR];
  1267. }
  1268. if (joystick->hwdata->has_key[BTN_MODE]) {
  1269. out->guide.kind = EMappingKind_Button;
  1270. out->guide.target = joystick->hwdata->key_map[BTN_MODE];
  1271. }
  1272. /*
  1273. According to the specs the D-Pad, the shoulder buttons and the triggers
  1274. can be digital, or analog, or both at the same time.
  1275. */
  1276. /* Prefer digital shoulder buttons, but settle for analog if missing. */
  1277. if (joystick->hwdata->has_key[BTN_TL]) {
  1278. out->leftshoulder.kind = EMappingKind_Button;
  1279. out->leftshoulder.target = joystick->hwdata->key_map[BTN_TL];
  1280. }
  1281. if (joystick->hwdata->has_key[BTN_TR]) {
  1282. out->rightshoulder.kind = EMappingKind_Button;
  1283. out->rightshoulder.target = joystick->hwdata->key_map[BTN_TR];
  1284. }
  1285. if (joystick->hwdata->has_hat[1] && /* Check if ABS_HAT1{X, Y} is available. */
  1286. (!joystick->hwdata->has_key[BTN_TL] || !joystick->hwdata->has_key[BTN_TR])) {
  1287. int hat = joystick->hwdata->hats_indices[1] << 4;
  1288. out->leftshoulder.kind = EMappingKind_Hat;
  1289. out->rightshoulder.kind = EMappingKind_Hat;
  1290. out->leftshoulder.target = hat | 0x4;
  1291. out->rightshoulder.target = hat | 0x2;
  1292. }
  1293. /* Prefer analog triggers, but settle for digital if missing. */
  1294. if (joystick->hwdata->has_hat[2]) { /* Check if ABS_HAT2{X,Y} is available. */
  1295. int hat = joystick->hwdata->hats_indices[2] << 4;
  1296. out->lefttrigger.kind = EMappingKind_Hat;
  1297. out->righttrigger.kind = EMappingKind_Hat;
  1298. out->lefttrigger.target = hat | 0x4;
  1299. out->righttrigger.target = hat | 0x2;
  1300. } else {
  1301. if (joystick->hwdata->has_key[BTN_TL2]) {
  1302. out->lefttrigger.kind = EMappingKind_Button;
  1303. out->lefttrigger.target = joystick->hwdata->key_map[BTN_TL2];
  1304. } else if (joystick->hwdata->has_abs[ABS_Z]) {
  1305. out->lefttrigger.kind = EMappingKind_Axis;
  1306. out->lefttrigger.target = joystick->hwdata->abs_map[ABS_Z];
  1307. }
  1308. if (joystick->hwdata->has_key[BTN_TR2]) {
  1309. out->righttrigger.kind = EMappingKind_Button;
  1310. out->righttrigger.target = joystick->hwdata->key_map[BTN_TR2];
  1311. } else if (joystick->hwdata->has_abs[ABS_RZ]) {
  1312. out->righttrigger.kind = EMappingKind_Axis;
  1313. out->righttrigger.target = joystick->hwdata->abs_map[ABS_RZ];
  1314. }
  1315. }
  1316. /* Prefer digital D-Pad, but settle for analog if missing. */
  1317. if (joystick->hwdata->has_key[BTN_DPAD_UP]) {
  1318. out->dpup.kind = EMappingKind_Button;
  1319. out->dpup.target = joystick->hwdata->key_map[BTN_DPAD_UP];
  1320. }
  1321. if (joystick->hwdata->has_key[BTN_DPAD_DOWN]) {
  1322. out->dpdown.kind = EMappingKind_Button;
  1323. out->dpdown.target = joystick->hwdata->key_map[BTN_DPAD_DOWN];
  1324. }
  1325. if (joystick->hwdata->has_key[BTN_DPAD_LEFT]) {
  1326. out->dpleft.kind = EMappingKind_Button;
  1327. out->dpleft.target = joystick->hwdata->key_map[BTN_DPAD_LEFT];
  1328. }
  1329. if (joystick->hwdata->has_key[BTN_DPAD_RIGHT]) {
  1330. out->dpright.kind = EMappingKind_Button;
  1331. out->dpright.target = joystick->hwdata->key_map[BTN_DPAD_RIGHT];
  1332. }
  1333. if (joystick->hwdata->has_hat[0] && /* Check if ABS_HAT0{X,Y} is available. */
  1334. (!joystick->hwdata->has_key[BTN_DPAD_LEFT] || !joystick->hwdata->has_key[BTN_DPAD_RIGHT] ||
  1335. !joystick->hwdata->has_key[BTN_DPAD_UP] || !joystick->hwdata->has_key[BTN_DPAD_DOWN])) {
  1336. int hat = joystick->hwdata->hats_indices[0] << 4;
  1337. out->dpleft.kind = EMappingKind_Hat;
  1338. out->dpright.kind = EMappingKind_Hat;
  1339. out->dpup.kind = EMappingKind_Hat;
  1340. out->dpdown.kind = EMappingKind_Hat;
  1341. out->dpleft.target = hat | 0x8;
  1342. out->dpright.target = hat | 0x2;
  1343. out->dpup.target = hat | 0x1;
  1344. out->dpdown.target = hat | 0x4;
  1345. }
  1346. if (joystick->hwdata->has_abs[ABS_X] && joystick->hwdata->has_abs[ABS_Y]) {
  1347. out->leftx.kind = EMappingKind_Axis;
  1348. out->lefty.kind = EMappingKind_Axis;
  1349. out->leftx.target = joystick->hwdata->abs_map[ABS_X];
  1350. out->lefty.target = joystick->hwdata->abs_map[ABS_Y];
  1351. }
  1352. if (joystick->hwdata->has_abs[ABS_RX] && joystick->hwdata->has_abs[ABS_RY]) {
  1353. out->rightx.kind = EMappingKind_Axis;
  1354. out->righty.kind = EMappingKind_Axis;
  1355. out->rightx.target = joystick->hwdata->abs_map[ABS_RX];
  1356. out->righty.target = joystick->hwdata->abs_map[ABS_RY];
  1357. }
  1358. LINUX_JoystickClose(joystick);
  1359. SDL_free(joystick);
  1360. /* Cache the mapping for later */
  1361. item->mapping = (SDL_GamepadMapping *)SDL_malloc(sizeof(*item->mapping));
  1362. if (item->mapping) {
  1363. SDL_memcpy(item->mapping, out, sizeof(*out));
  1364. }
  1365. return SDL_TRUE;
  1366. }
  1367. SDL_JoystickDriver SDL_LINUX_JoystickDriver =
  1368. {
  1369. LINUX_JoystickInit,
  1370. LINUX_JoystickGetCount,
  1371. LINUX_JoystickDetect,
  1372. LINUX_JoystickGetDeviceName,
  1373. LINUX_JoystickGetDevicePlayerIndex,
  1374. LINUX_JoystickSetDevicePlayerIndex,
  1375. LINUX_JoystickGetDeviceGUID,
  1376. LINUX_JoystickGetDeviceInstanceID,
  1377. LINUX_JoystickOpen,
  1378. LINUX_JoystickRumble,
  1379. LINUX_JoystickRumbleTriggers,
  1380. LINUX_JoystickHasLED,
  1381. LINUX_JoystickSetLED,
  1382. LINUX_JoystickSendEffect,
  1383. LINUX_JoystickSetSensorsEnabled,
  1384. LINUX_JoystickUpdate,
  1385. LINUX_JoystickClose,
  1386. LINUX_JoystickQuit,
  1387. LINUX_JoystickGetGamepadMapping
  1388. };
  1389. #endif /* SDL_JOYSTICK_LINUX */
  1390. /* vi: set ts=4 sw=4 expandtab: */