SDL_sysjoystick.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 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_NORTH
  51. #define BTN_NORTH 0x133
  52. #endif
  53. #ifndef BTN_WEST
  54. #define BTN_WEST 0x134
  55. #endif
  56. #ifndef BTN_DPAD_UP
  57. #define BTN_DPAD_UP 0x220
  58. #endif
  59. #ifndef BTN_DPAD_DOWN
  60. #define BTN_DPAD_DOWN 0x221
  61. #endif
  62. #ifndef BTN_DPAD_LEFT
  63. #define BTN_DPAD_LEFT 0x222
  64. #endif
  65. #ifndef BTN_DPAD_RIGHT
  66. #define BTN_DPAD_RIGHT 0x223
  67. #endif
  68. #include "../../core/linux/SDL_evdev_capabilities.h"
  69. #include "../../core/linux/SDL_udev.h"
  70. #if 0
  71. #define DEBUG_INPUT_EVENTS 1
  72. #endif
  73. #if 0
  74. #define DEBUG_GAMEPAD_MAPPING 1
  75. #endif
  76. typedef enum
  77. {
  78. ENUMERATION_UNSET,
  79. ENUMERATION_LIBUDEV,
  80. ENUMERATION_FALLBACK
  81. } EnumerationMethod;
  82. static EnumerationMethod enumeration_method = ENUMERATION_UNSET;
  83. static SDL_bool IsJoystickJSNode(const char *node);
  84. static int MaybeAddDevice(const char *path);
  85. static int MaybeRemoveDevice(const char *path);
  86. /* A linked list of available joysticks */
  87. typedef struct SDL_joylist_item
  88. {
  89. SDL_JoystickID device_instance;
  90. char *path; /* "/dev/input/event2" or whatever */
  91. char *name; /* "SideWinder 3D Pro" or whatever */
  92. SDL_JoystickGUID guid;
  93. dev_t devnum;
  94. struct joystick_hwdata *hwdata;
  95. struct SDL_joylist_item *next;
  96. /* Steam Controller support */
  97. SDL_bool m_bSteamController;
  98. SDL_bool checked_mapping;
  99. SDL_GamepadMapping *mapping;
  100. } SDL_joylist_item;
  101. static SDL_bool SDL_classic_joysticks = SDL_FALSE;
  102. static SDL_joylist_item *SDL_joylist = NULL;
  103. static SDL_joylist_item *SDL_joylist_tail = NULL;
  104. static int numjoysticks = 0;
  105. static int inotify_fd = -1;
  106. static Uint32 last_joy_detect_time;
  107. static time_t last_input_dir_mtime;
  108. static void
  109. FixupDeviceInfoForMapping(int fd, struct input_id *inpid)
  110. {
  111. if (inpid->vendor == 0x045e && inpid->product == 0x0b05 && inpid->version == 0x0903) {
  112. /* This is a Microsoft Xbox One Elite Series 2 controller */
  113. unsigned long keybit[NBITS(KEY_MAX)] = { 0 };
  114. /* The first version of the firmware duplicated all the inputs */
  115. if ((ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) >= 0) &&
  116. test_bit(0x2c0, keybit)) {
  117. /* Change the version to 0x0902, so we can map it differently */
  118. inpid->version = 0x0902;
  119. }
  120. }
  121. /* For Atari vcs modern and classic controllers have the version reflecting
  122. * firmware version, but the mapping stays stable so ignore
  123. * version information */
  124. if (inpid->vendor == 0x3250
  125. && (inpid->product == 0x1001 || inpid->product == 0x1002)) {
  126. inpid->version = 0;
  127. }
  128. }
  129. #ifdef SDL_JOYSTICK_HIDAPI
  130. static SDL_bool
  131. IsVirtualJoystick(Uint16 vendor, Uint16 product, Uint16 version, const char *name)
  132. {
  133. if (vendor == USB_VENDOR_MICROSOFT && product == USB_PRODUCT_XBOX_ONE_S && version == 0 &&
  134. SDL_strcmp(name, "Xbox One S Controller") == 0) {
  135. /* This is the virtual device created by the xow driver */
  136. return SDL_TRUE;
  137. }
  138. return SDL_FALSE;
  139. }
  140. #endif /* SDL_JOYSTICK_HIDAPI */
  141. static int
  142. GuessIsJoystick(int fd)
  143. {
  144. unsigned long evbit[NBITS(EV_MAX)] = { 0 };
  145. unsigned long keybit[NBITS(KEY_MAX)] = { 0 };
  146. unsigned long absbit[NBITS(ABS_MAX)] = { 0 };
  147. unsigned long relbit[NBITS(REL_MAX)] = { 0 };
  148. int devclass;
  149. if ((ioctl(fd, EVIOCGBIT(0, sizeof(evbit)), evbit) < 0) ||
  150. (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) < 0) ||
  151. (ioctl(fd, EVIOCGBIT(EV_REL, sizeof(relbit)), relbit) < 0) ||
  152. (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) < 0)) {
  153. return (0);
  154. }
  155. devclass = SDL_EVDEV_GuessDeviceClass(evbit, absbit, keybit, relbit);
  156. if (devclass & SDL_UDEV_DEVICE_JOYSTICK) {
  157. return 1;
  158. }
  159. return 0;
  160. }
  161. static int
  162. IsJoystick(const char *path, int fd, char **name_return, SDL_JoystickGUID *guid)
  163. {
  164. struct input_id inpid;
  165. char *name;
  166. char product_string[128];
  167. if (ioctl(fd, JSIOCGNAME(sizeof(product_string)), product_string) >= 0) {
  168. SDL_zero(inpid);
  169. #if SDL_USE_LIBUDEV
  170. SDL_UDEV_GetProductInfo(path, &inpid.vendor, &inpid.product, &inpid.version);
  171. #endif
  172. } else {
  173. /* When udev is enabled we only get joystick devices here, so there's no need to test them */
  174. if (enumeration_method != ENUMERATION_LIBUDEV && !GuessIsJoystick(fd)) {
  175. return 0;
  176. }
  177. if (ioctl(fd, EVIOCGID, &inpid) < 0) {
  178. return 0;
  179. }
  180. if (ioctl(fd, EVIOCGNAME(sizeof(product_string)), product_string) < 0) {
  181. return 0;
  182. }
  183. }
  184. name = SDL_CreateJoystickName(inpid.vendor, inpid.product, NULL, product_string);
  185. if (!name) {
  186. return 0;
  187. }
  188. #ifdef SDL_JOYSTICK_HIDAPI
  189. if (!IsVirtualJoystick(inpid.vendor, inpid.product, inpid.version, name) &&
  190. HIDAPI_IsDevicePresent(inpid.vendor, inpid.product, inpid.version, name)) {
  191. /* The HIDAPI driver is taking care of this device */
  192. SDL_free(name);
  193. return 0;
  194. }
  195. #endif
  196. FixupDeviceInfoForMapping(fd, &inpid);
  197. #ifdef DEBUG_JOYSTICK
  198. SDL_Log("Joystick: %s, bustype = %d, vendor = 0x%.4x, product = 0x%.4x, version = %d\n", name, inpid.bustype, inpid.vendor, inpid.product, inpid.version);
  199. #endif
  200. *guid = SDL_CreateJoystickGUID(inpid.bustype, inpid.vendor, inpid.product, inpid.version, name, 0, 0);
  201. if (SDL_ShouldIgnoreJoystick(name, *guid)) {
  202. SDL_free(name);
  203. return 0;
  204. }
  205. *name_return = name;
  206. return 1;
  207. }
  208. #if SDL_USE_LIBUDEV
  209. static void joystick_udev_callback(SDL_UDEV_deviceevent udev_type, int udev_class, const char *devpath)
  210. {
  211. if (devpath == NULL) {
  212. return;
  213. }
  214. switch (udev_type) {
  215. case SDL_UDEV_DEVICEADDED:
  216. if (!(udev_class & SDL_UDEV_DEVICE_JOYSTICK)) {
  217. return;
  218. }
  219. if (SDL_classic_joysticks) {
  220. if (!IsJoystickJSNode(devpath)) {
  221. return;
  222. }
  223. } else {
  224. if (IsJoystickJSNode(devpath)) {
  225. return;
  226. }
  227. }
  228. MaybeAddDevice(devpath);
  229. break;
  230. case SDL_UDEV_DEVICEREMOVED:
  231. MaybeRemoveDevice(devpath);
  232. break;
  233. default:
  234. break;
  235. }
  236. }
  237. #endif /* SDL_USE_LIBUDEV */
  238. static void
  239. FreeJoylistItem(SDL_joylist_item *item)
  240. {
  241. SDL_free(item->mapping);
  242. SDL_free(item->path);
  243. SDL_free(item->name);
  244. SDL_free(item);
  245. }
  246. static int
  247. MaybeAddDevice(const char *path)
  248. {
  249. struct stat sb;
  250. int fd = -1;
  251. int isstick = 0;
  252. char *name = NULL;
  253. SDL_JoystickGUID guid;
  254. SDL_joylist_item *item;
  255. if (path == NULL) {
  256. return -1;
  257. }
  258. if (stat(path, &sb) == -1) {
  259. return -1;
  260. }
  261. /* Check to make sure it's not already in list. */
  262. for (item = SDL_joylist; item != NULL; item = item->next) {
  263. if (sb.st_rdev == item->devnum) {
  264. return -1; /* already have this one */
  265. }
  266. }
  267. fd = open(path, O_RDONLY | O_CLOEXEC, 0);
  268. if (fd < 0) {
  269. return -1;
  270. }
  271. #ifdef DEBUG_INPUT_EVENTS
  272. SDL_Log("Checking %s\n", path);
  273. #endif
  274. isstick = IsJoystick(path, fd, &name, &guid);
  275. close(fd);
  276. if (!isstick) {
  277. return -1;
  278. }
  279. item = (SDL_joylist_item *) SDL_calloc(1, sizeof (SDL_joylist_item));
  280. if (item == NULL) {
  281. SDL_free(name);
  282. return -1;
  283. }
  284. item->devnum = sb.st_rdev;
  285. item->path = SDL_strdup(path);
  286. item->name = name;
  287. item->guid = guid;
  288. if ((item->path == NULL) || (item->name == NULL)) {
  289. FreeJoylistItem(item);
  290. return -1;
  291. }
  292. item->device_instance = SDL_GetNextJoystickInstanceID();
  293. if (SDL_joylist_tail == NULL) {
  294. SDL_joylist = SDL_joylist_tail = item;
  295. } else {
  296. SDL_joylist_tail->next = item;
  297. SDL_joylist_tail = item;
  298. }
  299. /* Need to increment the joystick count before we post the event */
  300. ++numjoysticks;
  301. SDL_PrivateJoystickAdded(item->device_instance);
  302. return numjoysticks;
  303. }
  304. static void
  305. RemoveJoylistItem(SDL_joylist_item *item, SDL_joylist_item *prev)
  306. {
  307. if (item->hwdata) {
  308. item->hwdata->item = NULL;
  309. }
  310. if (prev != NULL) {
  311. prev->next = item->next;
  312. } else {
  313. SDL_assert(SDL_joylist == item);
  314. SDL_joylist = item->next;
  315. }
  316. if (item == SDL_joylist_tail) {
  317. SDL_joylist_tail = prev;
  318. }
  319. /* Need to decrement the joystick count before we post the event */
  320. --numjoysticks;
  321. SDL_PrivateJoystickRemoved(item->device_instance);
  322. FreeJoylistItem(item);
  323. }
  324. static int
  325. MaybeRemoveDevice(const char *path)
  326. {
  327. SDL_joylist_item *item;
  328. SDL_joylist_item *prev = NULL;
  329. if (path == NULL) {
  330. return -1;
  331. }
  332. for (item = SDL_joylist; item != NULL; item = item->next) {
  333. /* found it, remove it. */
  334. if (SDL_strcmp(path, item->path) == 0) {
  335. const int retval = item->device_instance;
  336. RemoveJoylistItem(item, prev);
  337. return retval;
  338. }
  339. prev = item;
  340. }
  341. return -1;
  342. }
  343. static void
  344. HandlePendingRemovals(void)
  345. {
  346. SDL_joylist_item *prev = NULL;
  347. SDL_joylist_item *item = SDL_joylist;
  348. while (item != NULL) {
  349. if (item->hwdata && item->hwdata->gone) {
  350. RemoveJoylistItem(item, prev);
  351. if (prev != NULL) {
  352. item = prev->next;
  353. } else {
  354. item = SDL_joylist;
  355. }
  356. } else {
  357. prev = item;
  358. item = item->next;
  359. }
  360. }
  361. }
  362. static SDL_bool SteamControllerConnectedCallback(const char *name, SDL_JoystickGUID guid, int *device_instance)
  363. {
  364. SDL_joylist_item *item;
  365. item = (SDL_joylist_item *) SDL_calloc(1, sizeof (SDL_joylist_item));
  366. if (item == NULL) {
  367. return SDL_FALSE;
  368. }
  369. item->path = SDL_strdup("");
  370. item->name = SDL_strdup(name);
  371. item->guid = guid;
  372. item->m_bSteamController = SDL_TRUE;
  373. if ((item->path == NULL) || (item->name == NULL)) {
  374. FreeJoylistItem(item);
  375. return SDL_FALSE;
  376. }
  377. *device_instance = item->device_instance = SDL_GetNextJoystickInstanceID();
  378. if (SDL_joylist_tail == NULL) {
  379. SDL_joylist = SDL_joylist_tail = item;
  380. } else {
  381. SDL_joylist_tail->next = item;
  382. SDL_joylist_tail = item;
  383. }
  384. /* Need to increment the joystick count before we post the event */
  385. ++numjoysticks;
  386. SDL_PrivateJoystickAdded(item->device_instance);
  387. return SDL_TRUE;
  388. }
  389. static void SteamControllerDisconnectedCallback(int device_instance)
  390. {
  391. SDL_joylist_item *item;
  392. SDL_joylist_item *prev = NULL;
  393. for (item = SDL_joylist; item != NULL; item = item->next) {
  394. /* found it, remove it. */
  395. if (item->device_instance == device_instance) {
  396. RemoveJoylistItem(item, prev);
  397. return;
  398. }
  399. prev = item;
  400. }
  401. }
  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 SDL_bool
  422. IsJoystickJSNode(const char *node)
  423. {
  424. const char *last_slash = SDL_strrchr(node, '/');
  425. if (last_slash) {
  426. node = last_slash + 1;
  427. }
  428. return (StrHasPrefix(node, "js") && StrIsInteger(node + 2));
  429. }
  430. static SDL_bool
  431. IsJoystickEventNode(const char *node)
  432. {
  433. const char *last_slash = SDL_strrchr(node, '/');
  434. if (last_slash) {
  435. node = last_slash + 1;
  436. }
  437. return (StrHasPrefix(node, "event") && StrIsInteger(node + 5));
  438. }
  439. static SDL_bool
  440. IsJoystickDeviceNode(const char *node)
  441. {
  442. if (SDL_classic_joysticks) {
  443. return IsJoystickJSNode(node);
  444. } else {
  445. return IsJoystickEventNode(node);
  446. }
  447. }
  448. #ifdef HAVE_INOTIFY
  449. #ifdef HAVE_INOTIFY_INIT1
  450. static int SDL_inotify_init1(void) {
  451. return inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
  452. }
  453. #else
  454. static int SDL_inotify_init1(void) {
  455. int fd = inotify_init();
  456. if (fd < 0) return -1;
  457. fcntl(fd, F_SETFL, O_NONBLOCK);
  458. fcntl(fd, F_SETFD, FD_CLOEXEC);
  459. return fd;
  460. }
  461. #endif
  462. static void
  463. LINUX_InotifyJoystickDetect(void)
  464. {
  465. union
  466. {
  467. struct inotify_event event;
  468. char storage[4096];
  469. char enough_for_inotify[sizeof (struct inotify_event) + NAME_MAX + 1];
  470. } buf;
  471. ssize_t bytes;
  472. size_t remain = 0;
  473. size_t len;
  474. char path[PATH_MAX];
  475. bytes = read(inotify_fd, &buf, sizeof (buf));
  476. if (bytes > 0) {
  477. remain = (size_t) bytes;
  478. }
  479. while (remain > 0) {
  480. if (buf.event.len > 0) {
  481. if (IsJoystickDeviceNode(buf.event.name)) {
  482. SDL_snprintf(path, SDL_arraysize(path), "/dev/input/%s", buf.event.name);
  483. if (buf.event.mask & (IN_CREATE | IN_MOVED_TO | IN_ATTRIB)) {
  484. MaybeAddDevice(path);
  485. }
  486. else if (buf.event.mask & (IN_DELETE | IN_MOVED_FROM)) {
  487. MaybeRemoveDevice(path);
  488. }
  489. }
  490. }
  491. len = sizeof (struct inotify_event) + buf.event.len;
  492. remain -= len;
  493. if (remain != 0) {
  494. SDL_memmove (&buf.storage[0], &buf.storage[len], remain);
  495. }
  496. }
  497. }
  498. #endif /* HAVE_INOTIFY */
  499. static int get_event_joystick_index(int event)
  500. {
  501. int joystick_index = -1;
  502. int i, count;
  503. struct dirent **entries = NULL;
  504. char path[PATH_MAX];
  505. SDL_snprintf(path, SDL_arraysize(path), "/sys/class/input/event%d/device", event);
  506. count = scandir(path, &entries, NULL, alphasort);
  507. for (i = 0; i < count; ++i) {
  508. if (SDL_strncmp(entries[i]->d_name, "js", 2) == 0) {
  509. joystick_index = SDL_atoi(entries[i]->d_name+2);
  510. }
  511. free(entries[i]); /* This should NOT be SDL_free() */
  512. }
  513. free(entries); /* This should NOT be SDL_free() */
  514. return joystick_index;
  515. }
  516. /* Detect devices by reading /dev/input. In the inotify code path we
  517. * have to do this the first time, to detect devices that already existed
  518. * before we started; in the non-inotify code path we do this repeatedly
  519. * (polling). */
  520. static int
  521. filter_entries(const struct dirent *entry)
  522. {
  523. return IsJoystickDeviceNode(entry->d_name);
  524. }
  525. static int
  526. sort_entries(const void *_a, const void *_b)
  527. {
  528. const struct dirent **a = (const struct dirent **)_a;
  529. const struct dirent **b = (const struct dirent **)_b;
  530. int numA, numB;
  531. int offset;
  532. if (SDL_classic_joysticks) {
  533. offset = 2; /* strlen("js") */
  534. numA = SDL_atoi((*a)->d_name+offset);
  535. numB = SDL_atoi((*b)->d_name+offset);
  536. } else {
  537. offset = 5; /* strlen("event") */
  538. numA = SDL_atoi((*a)->d_name+offset);
  539. numB = SDL_atoi((*b)->d_name+offset);
  540. /* See if we can get the joystick ordering */
  541. {
  542. int jsA = get_event_joystick_index(numA);
  543. int jsB = get_event_joystick_index(numB);
  544. if (jsA >= 0 && jsB >= 0) {
  545. numA = jsA;
  546. numB = jsB;
  547. } else if (jsA >= 0) {
  548. return -1;
  549. } else if (jsB >= 0) {
  550. return 1;
  551. }
  552. }
  553. }
  554. return (numA - numB);
  555. }
  556. static void
  557. LINUX_FallbackJoystickDetect(void)
  558. {
  559. const Uint32 SDL_JOY_DETECT_INTERVAL_MS = 3000; /* Update every 3 seconds */
  560. Uint32 now = SDL_GetTicks();
  561. if (!last_joy_detect_time || SDL_TICKS_PASSED(now, last_joy_detect_time + SDL_JOY_DETECT_INTERVAL_MS)) {
  562. struct stat sb;
  563. /* Opening input devices can generate synchronous device I/O, so avoid it if we can */
  564. if (stat("/dev/input", &sb) == 0 && sb.st_mtime != last_input_dir_mtime) {
  565. int i, count;
  566. struct dirent **entries = NULL;
  567. char path[PATH_MAX];
  568. count = scandir("/dev/input", &entries, filter_entries, NULL);
  569. if (count > 1) {
  570. qsort(entries, count, sizeof(*entries), sort_entries);
  571. }
  572. for (i = 0; i < count; ++i) {
  573. SDL_snprintf(path, SDL_arraysize(path), "/dev/input/%s", entries[i]->d_name);
  574. MaybeAddDevice(path);
  575. free(entries[i]); /* This should NOT be SDL_free() */
  576. }
  577. free(entries); /* This should NOT be SDL_free() */
  578. last_input_dir_mtime = sb.st_mtime;
  579. }
  580. last_joy_detect_time = now;
  581. }
  582. }
  583. static void
  584. LINUX_JoystickDetect(void)
  585. {
  586. #if SDL_USE_LIBUDEV
  587. if (enumeration_method == ENUMERATION_LIBUDEV) {
  588. SDL_UDEV_Poll();
  589. }
  590. else
  591. #endif
  592. #ifdef HAVE_INOTIFY
  593. if (inotify_fd >= 0 && last_joy_detect_time != 0) {
  594. LINUX_InotifyJoystickDetect();
  595. }
  596. else
  597. #endif
  598. {
  599. LINUX_FallbackJoystickDetect();
  600. }
  601. HandlePendingRemovals();
  602. SDL_UpdateSteamControllers();
  603. }
  604. static int
  605. LINUX_JoystickInit(void)
  606. {
  607. const char *devices = SDL_GetHint(SDL_HINT_JOYSTICK_DEVICE);
  608. SDL_classic_joysticks = SDL_GetHintBoolean(SDL_HINT_LINUX_JOYSTICK_CLASSIC, SDL_FALSE);
  609. enumeration_method = ENUMERATION_UNSET;
  610. /* First see if the user specified one or more joysticks to use */
  611. if (devices != NULL) {
  612. char *envcopy, *envpath, *delim;
  613. envcopy = SDL_strdup(devices);
  614. envpath = envcopy;
  615. while (envpath != NULL) {
  616. delim = SDL_strchr(envpath, ':');
  617. if (delim != NULL) {
  618. *delim++ = '\0';
  619. }
  620. MaybeAddDevice(envpath);
  621. envpath = delim;
  622. }
  623. SDL_free(envcopy);
  624. }
  625. SDL_InitSteamControllers(SteamControllerConnectedCallback,
  626. SteamControllerDisconnectedCallback);
  627. /* Force immediate joystick detection if using fallback */
  628. last_joy_detect_time = 0;
  629. last_input_dir_mtime = 0;
  630. /* Manually scan first, since we sort by device number and udev doesn't */
  631. LINUX_JoystickDetect();
  632. #if SDL_USE_LIBUDEV
  633. if (enumeration_method == ENUMERATION_UNSET) {
  634. if (SDL_GetHintBoolean("SDL_JOYSTICK_DISABLE_UDEV", SDL_FALSE)) {
  635. SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
  636. "udev disabled by SDL_JOYSTICK_DISABLE_UDEV");
  637. enumeration_method = ENUMERATION_FALLBACK;
  638. } else if (access("/.flatpak-info", F_OK) == 0
  639. || access("/run/host/container-manager", F_OK) == 0) {
  640. /* Explicitly check `/.flatpak-info` because, for old versions of
  641. * Flatpak, this was the only available way to tell if we were in
  642. * a Flatpak container. */
  643. SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
  644. "Container detected, disabling udev integration");
  645. enumeration_method = ENUMERATION_FALLBACK;
  646. } else {
  647. SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
  648. "Using udev for joystick device discovery");
  649. enumeration_method = ENUMERATION_LIBUDEV;
  650. }
  651. }
  652. if (enumeration_method == ENUMERATION_LIBUDEV) {
  653. if (SDL_UDEV_Init() < 0) {
  654. return SDL_SetError("Could not initialize UDEV");
  655. }
  656. /* Set up the udev callback */
  657. if (SDL_UDEV_AddCallback(joystick_udev_callback) < 0) {
  658. SDL_UDEV_Quit();
  659. return SDL_SetError("Could not set up joystick <-> udev callback");
  660. }
  661. /* Force a scan to build the initial device list */
  662. SDL_UDEV_Scan();
  663. }
  664. else
  665. #endif
  666. {
  667. #if defined(HAVE_INOTIFY)
  668. inotify_fd = SDL_inotify_init1();
  669. if (inotify_fd < 0) {
  670. SDL_LogWarn(SDL_LOG_CATEGORY_INPUT,
  671. "Unable to initialize inotify, falling back to polling: %s",
  672. strerror (errno));
  673. } else {
  674. /* We need to watch for attribute changes in addition to
  675. * creation, because when a device is first created, it has
  676. * permissions that we can't read. When udev chmods it to
  677. * something that we maybe *can* read, we'll get an
  678. * IN_ATTRIB event to tell us. */
  679. if (inotify_add_watch(inotify_fd, "/dev/input",
  680. IN_CREATE | IN_DELETE | IN_MOVE | IN_ATTRIB) < 0) {
  681. close(inotify_fd);
  682. inotify_fd = -1;
  683. SDL_LogWarn(SDL_LOG_CATEGORY_INPUT,
  684. "Unable to add inotify watch, falling back to polling: %s",
  685. strerror (errno));
  686. }
  687. }
  688. #endif /* HAVE_INOTIFY */
  689. }
  690. return 0;
  691. }
  692. static int
  693. LINUX_JoystickGetCount(void)
  694. {
  695. return numjoysticks;
  696. }
  697. static SDL_joylist_item *
  698. JoystickByDevIndex(int device_index)
  699. {
  700. SDL_joylist_item *item = SDL_joylist;
  701. if ((device_index < 0) || (device_index >= numjoysticks)) {
  702. return NULL;
  703. }
  704. while (device_index > 0) {
  705. SDL_assert(item != NULL);
  706. device_index--;
  707. item = item->next;
  708. }
  709. return item;
  710. }
  711. static const char *
  712. LINUX_JoystickGetDeviceName(int device_index)
  713. {
  714. return JoystickByDevIndex(device_index)->name;
  715. }
  716. static const char *
  717. LINUX_JoystickGetDevicePath(int device_index)
  718. {
  719. return JoystickByDevIndex(device_index)->path;
  720. }
  721. static int
  722. LINUX_JoystickGetDevicePlayerIndex(int device_index)
  723. {
  724. return -1;
  725. }
  726. static void
  727. LINUX_JoystickSetDevicePlayerIndex(int device_index, int player_index)
  728. {
  729. }
  730. static SDL_JoystickGUID
  731. LINUX_JoystickGetDeviceGUID( int device_index )
  732. {
  733. return JoystickByDevIndex(device_index)->guid;
  734. }
  735. /* Function to perform the mapping from device index to the instance id for this index */
  736. static SDL_JoystickID
  737. LINUX_JoystickGetDeviceInstanceID(int device_index)
  738. {
  739. return JoystickByDevIndex(device_index)->device_instance;
  740. }
  741. static int
  742. allocate_hatdata(SDL_Joystick *joystick)
  743. {
  744. int i;
  745. joystick->hwdata->hats =
  746. (struct hwdata_hat *) SDL_malloc(joystick->nhats *
  747. sizeof(struct hwdata_hat));
  748. if (joystick->hwdata->hats == NULL) {
  749. return (-1);
  750. }
  751. for (i = 0; i < joystick->nhats; ++i) {
  752. joystick->hwdata->hats[i].axis[0] = 1;
  753. joystick->hwdata->hats[i].axis[1] = 1;
  754. }
  755. return (0);
  756. }
  757. static int
  758. allocate_balldata(SDL_Joystick *joystick)
  759. {
  760. int i;
  761. joystick->hwdata->balls =
  762. (struct hwdata_ball *) SDL_malloc(joystick->nballs *
  763. sizeof(struct hwdata_ball));
  764. if (joystick->hwdata->balls == NULL) {
  765. return (-1);
  766. }
  767. for (i = 0; i < joystick->nballs; ++i) {
  768. joystick->hwdata->balls[i].axis[0] = 0;
  769. joystick->hwdata->balls[i].axis[1] = 0;
  770. }
  771. return (0);
  772. }
  773. static SDL_bool
  774. GuessIfAxesAreDigitalHat(struct input_absinfo *absinfo_x, struct input_absinfo *absinfo_y)
  775. {
  776. /* A "hat" is assumed to be a digital input with at most 9 possible states
  777. * (3 per axis: negative/zero/positive), as opposed to a true "axis" which
  778. * can report a continuous range of possible values. Unfortunately the Linux
  779. * joystick interface makes no distinction between digital hat axes and any
  780. * other continuous analog axis, so we have to guess. */
  781. /* If both axes are missing, they're not anything. */
  782. if (!absinfo_x && !absinfo_y)
  783. return SDL_FALSE;
  784. /* If the hint says so, treat all hats as digital. */
  785. if (SDL_GetHintBoolean(SDL_HINT_LINUX_DIGITAL_HATS, SDL_FALSE))
  786. return SDL_TRUE;
  787. /* If both axes have ranges constrained between -1 and 1, they're definitely digital. */
  788. if ((!absinfo_x || (absinfo_x->minimum == -1 && absinfo_x->maximum == 1)) &&
  789. (!absinfo_y || (absinfo_y->minimum == -1 && absinfo_y->maximum == 1)))
  790. return SDL_TRUE;
  791. /* If both axes lack fuzz, flat, and resolution values, they're probably digital. */
  792. if ((!absinfo_x || (!absinfo_x->fuzz && !absinfo_x->flat && !absinfo_x->resolution)) &&
  793. (!absinfo_y || (!absinfo_y->fuzz && !absinfo_y->flat && !absinfo_y->resolution)))
  794. return SDL_TRUE;
  795. /* Otherwise, treat them as analog. */
  796. return SDL_FALSE;
  797. }
  798. static void
  799. ConfigJoystick(SDL_Joystick *joystick, int fd)
  800. {
  801. int i, t;
  802. unsigned long keybit[NBITS(KEY_MAX)] = { 0 };
  803. unsigned long absbit[NBITS(ABS_MAX)] = { 0 };
  804. unsigned long relbit[NBITS(REL_MAX)] = { 0 };
  805. unsigned long ffbit[NBITS(FF_MAX)] = { 0 };
  806. Uint8 key_pam_size, abs_pam_size;
  807. SDL_bool use_deadzones = SDL_GetHintBoolean(SDL_HINT_LINUX_JOYSTICK_DEADZONES, SDL_FALSE);
  808. SDL_bool use_hat_deadzones = SDL_GetHintBoolean(SDL_HINT_LINUX_HAT_DEADZONES, SDL_TRUE);
  809. /* See if this device uses the new unified event API */
  810. if ((ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) >= 0) &&
  811. (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) >= 0) &&
  812. (ioctl(fd, EVIOCGBIT(EV_REL, sizeof(relbit)), relbit) >= 0)) {
  813. /* Get the number of buttons, axes, and other thingamajigs */
  814. for (i = BTN_JOYSTICK; i < KEY_MAX; ++i) {
  815. if (test_bit(i, keybit)) {
  816. #ifdef DEBUG_INPUT_EVENTS
  817. SDL_Log("Joystick has button: 0x%x\n", i);
  818. #endif
  819. joystick->hwdata->key_map[i] = joystick->nbuttons;
  820. joystick->hwdata->has_key[i] = SDL_TRUE;
  821. ++joystick->nbuttons;
  822. }
  823. }
  824. for (i = 0; i < BTN_JOYSTICK; ++i) {
  825. if (test_bit(i, keybit)) {
  826. #ifdef DEBUG_INPUT_EVENTS
  827. SDL_Log("Joystick has button: 0x%x\n", i);
  828. #endif
  829. joystick->hwdata->key_map[i] = joystick->nbuttons;
  830. joystick->hwdata->has_key[i] = SDL_TRUE;
  831. ++joystick->nbuttons;
  832. }
  833. }
  834. for (i = ABS_HAT0X; i <= ABS_HAT3Y; i += 2) {
  835. int hat_x = -1;
  836. int hat_y = -1;
  837. struct input_absinfo absinfo_x;
  838. struct input_absinfo absinfo_y;
  839. if (test_bit(i, absbit))
  840. hat_x = ioctl(fd, EVIOCGABS(i), &absinfo_x);
  841. if (test_bit(i + 1, absbit))
  842. hat_y = ioctl(fd, EVIOCGABS(i + 1), &absinfo_y);
  843. if (GuessIfAxesAreDigitalHat((hat_x < 0 ? (void*)0 : &absinfo_x),
  844. (hat_y < 0 ? (void*)0 : &absinfo_y))) {
  845. const int hat_index = (i - ABS_HAT0X) / 2;
  846. struct hat_axis_correct *correct = &joystick->hwdata->hat_correct[hat_index];
  847. #ifdef DEBUG_INPUT_EVENTS
  848. SDL_Log("Joystick has digital hat: #%d\n", hat_index);
  849. if (hat_x >= 0) {
  850. SDL_Log("X Values = { val:%d, min:%d, max:%d, fuzz:%d, flat:%d, res:%d }\n",
  851. absinfo_x.value, absinfo_x.minimum, absinfo_x.maximum,
  852. absinfo_x.fuzz, absinfo_x.flat, absinfo_x.resolution);
  853. }
  854. if (hat_y >= 0) {
  855. SDL_Log("Y Values = { val:%d, min:%d, max:%d, fuzz:%d, flat:%d, res:%d }\n",
  856. absinfo_y.value, absinfo_y.minimum, absinfo_y.maximum,
  857. absinfo_y.fuzz, absinfo_y.flat, absinfo_y.resolution);
  858. }
  859. #endif /* DEBUG_INPUT_EVENTS */
  860. joystick->hwdata->hats_indices[hat_index] = joystick->nhats;
  861. joystick->hwdata->has_hat[hat_index] = SDL_TRUE;
  862. correct->use_deadzones = use_hat_deadzones;
  863. correct->minimum[0] = (hat_x < 0) ? -1 : absinfo_x.minimum;
  864. correct->maximum[0] = (hat_x < 0) ? 1 : absinfo_x.maximum;
  865. correct->minimum[1] = (hat_y < 0) ? -1 : absinfo_y.minimum;
  866. correct->maximum[1] = (hat_y < 0) ? 1 : absinfo_y.maximum;
  867. ++joystick->nhats;
  868. }
  869. }
  870. for (i = 0; i < ABS_MAX; ++i) {
  871. /* Skip digital hats */
  872. if (joystick->hwdata->has_hat[(i - ABS_HAT0X) / 2]) {
  873. continue;
  874. }
  875. if (test_bit(i, absbit)) {
  876. struct input_absinfo absinfo;
  877. struct axis_correct *correct = &joystick->hwdata->abs_correct[i];
  878. if (ioctl(fd, EVIOCGABS(i), &absinfo) < 0) {
  879. continue;
  880. }
  881. #ifdef DEBUG_INPUT_EVENTS
  882. SDL_Log("Joystick has absolute axis: 0x%.2x\n", i);
  883. SDL_Log("Values = { val:%d, min:%d, max:%d, fuzz:%d, flat:%d, res:%d }\n",
  884. absinfo.value, absinfo.minimum, absinfo.maximum,
  885. absinfo.fuzz, absinfo.flat, absinfo.resolution);
  886. #endif /* DEBUG_INPUT_EVENTS */
  887. joystick->hwdata->abs_map[i] = joystick->naxes;
  888. joystick->hwdata->has_abs[i] = SDL_TRUE;
  889. correct->minimum = absinfo.minimum;
  890. correct->maximum = absinfo.maximum;
  891. if (correct->minimum != correct->maximum) {
  892. if (use_deadzones) {
  893. correct->use_deadzones = SDL_TRUE;
  894. correct->coef[0] = (absinfo.maximum + absinfo.minimum) - 2 * absinfo.flat;
  895. correct->coef[1] = (absinfo.maximum + absinfo.minimum) + 2 * absinfo.flat;
  896. t = ((absinfo.maximum - absinfo.minimum) - 4 * absinfo.flat);
  897. if (t != 0) {
  898. correct->coef[2] = (1 << 28) / t;
  899. } else {
  900. correct->coef[2] = 0;
  901. }
  902. } else {
  903. float value_range = (correct->maximum - correct->minimum);
  904. float output_range = (SDL_JOYSTICK_AXIS_MAX - SDL_JOYSTICK_AXIS_MIN);
  905. correct->scale = (output_range / value_range);
  906. }
  907. }
  908. ++joystick->naxes;
  909. }
  910. }
  911. if (test_bit(REL_X, relbit) || test_bit(REL_Y, relbit)) {
  912. ++joystick->nballs;
  913. }
  914. } else if ((ioctl(fd, JSIOCGBUTTONS, &key_pam_size, sizeof(key_pam_size)) >= 0) &&
  915. (ioctl(fd, JSIOCGAXES, &abs_pam_size, sizeof(abs_pam_size)) >= 0)) {
  916. size_t len;
  917. joystick->hwdata->classic = SDL_TRUE;
  918. len = (KEY_MAX - BTN_MISC + 1) * sizeof(*joystick->hwdata->key_pam);
  919. joystick->hwdata->key_pam = (Uint16 *)SDL_calloc(1, len);
  920. if (joystick->hwdata->key_pam) {
  921. if (ioctl(fd, JSIOCGBTNMAP, joystick->hwdata->key_pam, len) < 0) {
  922. SDL_free(joystick->hwdata->key_pam);
  923. joystick->hwdata->key_pam = NULL;
  924. key_pam_size = 0;
  925. }
  926. } else {
  927. key_pam_size = 0;
  928. }
  929. for (i = 0; i < key_pam_size; ++i) {
  930. Uint16 code = joystick->hwdata->key_pam[i];
  931. #ifdef DEBUG_INPUT_EVENTS
  932. SDL_Log("Joystick has button: 0x%x\n", code);
  933. #endif
  934. joystick->hwdata->key_map[code] = joystick->nbuttons;
  935. joystick->hwdata->has_key[code] = SDL_TRUE;
  936. ++joystick->nbuttons;
  937. }
  938. len = ABS_CNT * sizeof(*joystick->hwdata->abs_pam);
  939. joystick->hwdata->abs_pam = (Uint8 *)SDL_calloc(1, len);
  940. if (joystick->hwdata->abs_pam) {
  941. if (ioctl(fd, JSIOCGAXMAP, joystick->hwdata->abs_pam, len) < 0) {
  942. SDL_free(joystick->hwdata->abs_pam);
  943. joystick->hwdata->abs_pam = NULL;
  944. abs_pam_size = 0;
  945. }
  946. } else {
  947. abs_pam_size = 0;
  948. }
  949. for (i = 0; i < abs_pam_size; ++i) {
  950. Uint8 code = joystick->hwdata->abs_pam[i];
  951. // TODO: is there any way to detect analog hats in advance via this API?
  952. if (code >= ABS_HAT0X && code <= ABS_HAT3Y) {
  953. int hat_index = (code - ABS_HAT0X) / 2;
  954. if (!joystick->hwdata->has_hat[hat_index]) {
  955. #ifdef DEBUG_INPUT_EVENTS
  956. SDL_Log("Joystick has digital hat: #%d\n", hat_index);
  957. #endif
  958. joystick->hwdata->hats_indices[hat_index] = joystick->nhats++;
  959. joystick->hwdata->has_hat[hat_index] = SDL_TRUE;
  960. joystick->hwdata->hat_correct[hat_index].minimum[0] = -1;
  961. joystick->hwdata->hat_correct[hat_index].maximum[0] = 1;
  962. joystick->hwdata->hat_correct[hat_index].minimum[1] = -1;
  963. joystick->hwdata->hat_correct[hat_index].maximum[1] = 1;
  964. }
  965. } else {
  966. #ifdef DEBUG_INPUT_EVENTS
  967. SDL_Log("Joystick has absolute axis: 0x%.2x\n", code);
  968. #endif
  969. joystick->hwdata->abs_map[code] = joystick->naxes;
  970. joystick->hwdata->has_abs[code] = SDL_TRUE;
  971. ++joystick->naxes;
  972. }
  973. }
  974. }
  975. /* Allocate data to keep track of these thingamajigs */
  976. if (joystick->nhats > 0) {
  977. if (allocate_hatdata(joystick) < 0) {
  978. joystick->nhats = 0;
  979. }
  980. }
  981. if (joystick->nballs > 0) {
  982. if (allocate_balldata(joystick) < 0) {
  983. joystick->nballs = 0;
  984. }
  985. }
  986. if (ioctl(fd, EVIOCGBIT(EV_FF, sizeof(ffbit)), ffbit) >= 0) {
  987. if (test_bit(FF_RUMBLE, ffbit)) {
  988. joystick->hwdata->ff_rumble = SDL_TRUE;
  989. }
  990. if (test_bit(FF_SINE, ffbit)) {
  991. joystick->hwdata->ff_sine = SDL_TRUE;
  992. }
  993. }
  994. }
  995. /* This is used to do the heavy lifting for LINUX_JoystickOpen and
  996. also LINUX_JoystickGetGamepadMapping, so we can query the hardware
  997. without adding an opened SDL_Joystick object to the system.
  998. This expects `joystick->hwdata` to be allocated and will not free it
  999. on error. Returns -1 on error, 0 on success. */
  1000. static int
  1001. PrepareJoystickHwdata(SDL_Joystick *joystick, SDL_joylist_item *item)
  1002. {
  1003. joystick->hwdata->item = item;
  1004. joystick->hwdata->guid = item->guid;
  1005. joystick->hwdata->effect.id = -1;
  1006. joystick->hwdata->m_bSteamController = item->m_bSteamController;
  1007. SDL_memset(joystick->hwdata->key_map, 0xFF, sizeof(joystick->hwdata->key_map));
  1008. SDL_memset(joystick->hwdata->abs_map, 0xFF, sizeof(joystick->hwdata->abs_map));
  1009. if (item->m_bSteamController) {
  1010. joystick->hwdata->fd = -1;
  1011. SDL_GetSteamControllerInputs(&joystick->nbuttons,
  1012. &joystick->naxes,
  1013. &joystick->nhats);
  1014. } else {
  1015. /* Try read-write first, so we can do rumble */
  1016. int fd = open(item->path, O_RDWR | O_CLOEXEC, 0);
  1017. if (fd < 0) {
  1018. /* Try read-only again, at least we'll get events in this case */
  1019. fd = open(item->path, O_RDONLY | O_CLOEXEC, 0);
  1020. }
  1021. if (fd < 0) {
  1022. return SDL_SetError("Unable to open %s", item->path);
  1023. }
  1024. joystick->hwdata->fd = fd;
  1025. joystick->hwdata->fname = SDL_strdup(item->path);
  1026. if (joystick->hwdata->fname == NULL) {
  1027. close(fd);
  1028. return SDL_OutOfMemory();
  1029. }
  1030. /* Set the joystick to non-blocking read mode */
  1031. fcntl(fd, F_SETFL, O_NONBLOCK);
  1032. /* Get the number of buttons and axes on the joystick */
  1033. ConfigJoystick(joystick, fd);
  1034. }
  1035. return 0;
  1036. }
  1037. /* Function to open a joystick for use.
  1038. The joystick to open is specified by the device index.
  1039. This should fill the nbuttons and naxes fields of the joystick structure.
  1040. It returns 0, or -1 if there is an error.
  1041. */
  1042. static int
  1043. LINUX_JoystickOpen(SDL_Joystick *joystick, int device_index)
  1044. {
  1045. SDL_joylist_item *item = JoystickByDevIndex(device_index);
  1046. if (item == NULL) {
  1047. return SDL_SetError("No such device");
  1048. }
  1049. joystick->instance_id = item->device_instance;
  1050. joystick->hwdata = (struct joystick_hwdata *)
  1051. SDL_calloc(1, sizeof(*joystick->hwdata));
  1052. if (joystick->hwdata == NULL) {
  1053. return SDL_OutOfMemory();
  1054. }
  1055. if (PrepareJoystickHwdata(joystick, item) == -1) {
  1056. SDL_free(joystick->hwdata);
  1057. joystick->hwdata = NULL;
  1058. return -1; /* SDL_SetError will already have been called */
  1059. }
  1060. SDL_assert(item->hwdata == NULL);
  1061. item->hwdata = joystick->hwdata;
  1062. /* mark joystick as fresh and ready */
  1063. joystick->hwdata->fresh = SDL_TRUE;
  1064. return 0;
  1065. }
  1066. static int
  1067. LINUX_JoystickRumble(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
  1068. {
  1069. struct input_event event;
  1070. if (joystick->hwdata->ff_rumble) {
  1071. struct ff_effect *effect = &joystick->hwdata->effect;
  1072. effect->type = FF_RUMBLE;
  1073. effect->replay.length = SDL_MAX_RUMBLE_DURATION_MS;
  1074. effect->u.rumble.strong_magnitude = low_frequency_rumble;
  1075. effect->u.rumble.weak_magnitude = high_frequency_rumble;
  1076. } else if (joystick->hwdata->ff_sine) {
  1077. /* Scale and average the two rumble strengths */
  1078. Sint16 magnitude = (Sint16)(((low_frequency_rumble / 2) + (high_frequency_rumble / 2)) / 2);
  1079. struct ff_effect *effect = &joystick->hwdata->effect;
  1080. effect->type = FF_PERIODIC;
  1081. effect->replay.length = SDL_MAX_RUMBLE_DURATION_MS;
  1082. effect->u.periodic.waveform = FF_SINE;
  1083. effect->u.periodic.magnitude = magnitude;
  1084. } else {
  1085. return SDL_Unsupported();
  1086. }
  1087. if (ioctl(joystick->hwdata->fd, EVIOCSFF, &joystick->hwdata->effect) < 0) {
  1088. /* The kernel may have lost this effect, try to allocate a new one */
  1089. joystick->hwdata->effect.id = -1;
  1090. if (ioctl(joystick->hwdata->fd, EVIOCSFF, &joystick->hwdata->effect) < 0) {
  1091. return SDL_SetError("Couldn't update rumble effect: %s", strerror(errno));
  1092. }
  1093. }
  1094. event.type = EV_FF;
  1095. event.code = joystick->hwdata->effect.id;
  1096. event.value = 1;
  1097. if (write(joystick->hwdata->fd, &event, sizeof(event)) < 0) {
  1098. return SDL_SetError("Couldn't start rumble effect: %s", strerror(errno));
  1099. }
  1100. return 0;
  1101. }
  1102. static int
  1103. LINUX_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
  1104. {
  1105. return SDL_Unsupported();
  1106. }
  1107. static Uint32
  1108. LINUX_JoystickGetCapabilities(SDL_Joystick *joystick)
  1109. {
  1110. Uint32 result = 0;
  1111. if (joystick->hwdata->ff_rumble || joystick->hwdata->ff_sine) {
  1112. result |= SDL_JOYCAP_RUMBLE;
  1113. }
  1114. return result;
  1115. }
  1116. static int
  1117. LINUX_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
  1118. {
  1119. return SDL_Unsupported();
  1120. }
  1121. static int
  1122. LINUX_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)
  1123. {
  1124. return SDL_Unsupported();
  1125. }
  1126. static int
  1127. LINUX_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled)
  1128. {
  1129. return SDL_Unsupported();
  1130. }
  1131. static void
  1132. HandleHat(SDL_Joystick *stick, int hatidx, int axis, int value)
  1133. {
  1134. const int hatnum = stick->hwdata->hats_indices[hatidx];
  1135. struct hwdata_hat *the_hat;
  1136. struct hat_axis_correct *correct;
  1137. const Uint8 position_map[3][3] = {
  1138. {SDL_HAT_LEFTUP, SDL_HAT_UP, SDL_HAT_RIGHTUP},
  1139. {SDL_HAT_LEFT, SDL_HAT_CENTERED, SDL_HAT_RIGHT},
  1140. {SDL_HAT_LEFTDOWN, SDL_HAT_DOWN, SDL_HAT_RIGHTDOWN}
  1141. };
  1142. the_hat = &stick->hwdata->hats[hatnum];
  1143. correct = &stick->hwdata->hat_correct[hatidx];
  1144. /* Hopefully we detected any analog axes and left them as is rather than trying
  1145. * to use them as digital hats, but just in case, the deadzones here will
  1146. * prevent the slightest of twitches on an analog axis from registering as a hat
  1147. * movement. If the axes really are digital, this won't hurt since they should
  1148. * only ever be sending min, 0, or max anyway. */
  1149. if (value < 0) {
  1150. if (value <= correct->minimum[axis]) {
  1151. correct->minimum[axis] = value;
  1152. value = 0;
  1153. } else if (!correct->use_deadzones || value < correct->minimum[axis] / 3) {
  1154. value = 0;
  1155. } else {
  1156. value = 1;
  1157. }
  1158. } else if (value > 0) {
  1159. if (value >= correct->maximum[axis]) {
  1160. correct->maximum[axis] = value;
  1161. value = 2;
  1162. } else if (!correct->use_deadzones || value > correct->maximum[axis] / 3) {
  1163. value = 2;
  1164. } else {
  1165. value = 1;
  1166. }
  1167. } else { // value == 0
  1168. value = 1;
  1169. }
  1170. if (value != the_hat->axis[axis]) {
  1171. the_hat->axis[axis] = value;
  1172. SDL_PrivateJoystickHat(stick, hatnum,
  1173. position_map[the_hat->axis[1]][the_hat->axis[0]]);
  1174. }
  1175. }
  1176. static void
  1177. HandleBall(SDL_Joystick *stick, Uint8 ball, int axis, int value)
  1178. {
  1179. stick->hwdata->balls[ball].axis[axis] += value;
  1180. }
  1181. static int
  1182. AxisCorrect(SDL_Joystick *joystick, int which, int value)
  1183. {
  1184. struct axis_correct *correct;
  1185. correct = &joystick->hwdata->abs_correct[which];
  1186. if (correct->minimum != correct->maximum) {
  1187. if (correct->use_deadzones) {
  1188. value *= 2;
  1189. if (value > correct->coef[0]) {
  1190. if (value < correct->coef[1]) {
  1191. return 0;
  1192. }
  1193. value -= correct->coef[1];
  1194. } else {
  1195. value -= correct->coef[0];
  1196. }
  1197. value *= correct->coef[2];
  1198. value >>= 13;
  1199. } else {
  1200. value = (int)SDL_floorf((value - correct->minimum) * correct->scale + SDL_JOYSTICK_AXIS_MIN + 0.5f);
  1201. }
  1202. }
  1203. /* Clamp and return */
  1204. if (value < SDL_JOYSTICK_AXIS_MIN) {
  1205. return SDL_JOYSTICK_AXIS_MIN;
  1206. }
  1207. if (value > SDL_JOYSTICK_AXIS_MAX) {
  1208. return SDL_JOYSTICK_AXIS_MAX;
  1209. }
  1210. return value;
  1211. }
  1212. static void
  1213. PollAllValues(SDL_Joystick *joystick)
  1214. {
  1215. struct input_absinfo absinfo;
  1216. unsigned long keyinfo[NBITS(KEY_MAX)];
  1217. int i;
  1218. /* Poll all axis */
  1219. for (i = ABS_X; i < ABS_MAX; i++) {
  1220. /* We don't need to test for digital hats here, they won't have has_abs[] set */
  1221. if (joystick->hwdata->has_abs[i]) {
  1222. if (ioctl(joystick->hwdata->fd, EVIOCGABS(i), &absinfo) >= 0) {
  1223. absinfo.value = AxisCorrect(joystick, i, absinfo.value);
  1224. #ifdef DEBUG_INPUT_EVENTS
  1225. SDL_Log("Joystick : Re-read Axis %d (%d) val= %d\n",
  1226. joystick->hwdata->abs_map[i], i, absinfo.value);
  1227. #endif
  1228. SDL_PrivateJoystickAxis(joystick,
  1229. joystick->hwdata->abs_map[i],
  1230. absinfo.value);
  1231. }
  1232. }
  1233. }
  1234. /* Poll all digital hats */
  1235. for (i = ABS_HAT0X; i <= ABS_HAT3Y; i++) {
  1236. const int baseaxis = i - ABS_HAT0X;
  1237. const int hatidx = baseaxis / 2;
  1238. SDL_assert(hatidx < SDL_arraysize(joystick->hwdata->has_hat));
  1239. /* We don't need to test for analog axes here, they won't have has_hat[] set */
  1240. if (joystick->hwdata->has_hat[hatidx]) {
  1241. if (ioctl(joystick->hwdata->fd, EVIOCGABS(i), &absinfo) >= 0) {
  1242. const int hataxis = baseaxis % 2;
  1243. HandleHat(joystick, hatidx, hataxis, absinfo.value);
  1244. }
  1245. }
  1246. }
  1247. /* Poll all buttons */
  1248. SDL_zeroa(keyinfo);
  1249. if (ioctl(joystick->hwdata->fd, EVIOCGKEY(sizeof (keyinfo)), keyinfo) >= 0) {
  1250. for (i = 0; i < KEY_MAX; i++) {
  1251. if (joystick->hwdata->has_key[i]) {
  1252. const Uint8 value = test_bit(i, keyinfo) ? SDL_PRESSED : SDL_RELEASED;
  1253. #ifdef DEBUG_INPUT_EVENTS
  1254. SDL_Log("Joystick : Re-read Button %d (%d) val= %d\n",
  1255. joystick->hwdata->key_map[i], i, value);
  1256. #endif
  1257. SDL_PrivateJoystickButton(joystick,
  1258. joystick->hwdata->key_map[i], value);
  1259. }
  1260. }
  1261. }
  1262. /* Joyballs are relative input, so there's no poll state. Events only! */
  1263. }
  1264. static void
  1265. HandleInputEvents(SDL_Joystick *joystick)
  1266. {
  1267. struct input_event events[32];
  1268. int i, len, code, hat_index;
  1269. if (joystick->hwdata->fresh) {
  1270. PollAllValues(joystick);
  1271. joystick->hwdata->fresh = SDL_FALSE;
  1272. }
  1273. while ((len = read(joystick->hwdata->fd, events, (sizeof events))) > 0) {
  1274. len /= sizeof(events[0]);
  1275. for (i = 0; i < len; ++i) {
  1276. code = events[i].code;
  1277. /* If the kernel sent a SYN_DROPPED, we are supposed to ignore the
  1278. rest of the packet (the end of it signified by a SYN_REPORT) */
  1279. if ( joystick->hwdata->recovering_from_dropped &&
  1280. ((events[i].type != EV_SYN) || (code != SYN_REPORT)) ) {
  1281. continue;
  1282. }
  1283. switch (events[i].type) {
  1284. case EV_KEY:
  1285. SDL_PrivateJoystickButton(joystick,
  1286. joystick->hwdata->key_map[code],
  1287. events[i].value);
  1288. break;
  1289. case EV_ABS:
  1290. switch (code) {
  1291. case ABS_HAT0X:
  1292. case ABS_HAT0Y:
  1293. case ABS_HAT1X:
  1294. case ABS_HAT1Y:
  1295. case ABS_HAT2X:
  1296. case ABS_HAT2Y:
  1297. case ABS_HAT3X:
  1298. case ABS_HAT3Y:
  1299. hat_index = (code - ABS_HAT0X) / 2;
  1300. if (joystick->hwdata->has_hat[hat_index]) {
  1301. HandleHat(joystick, hat_index, code % 2, events[i].value);
  1302. break;
  1303. }
  1304. default:
  1305. events[i].value = AxisCorrect(joystick, code, events[i].value);
  1306. SDL_PrivateJoystickAxis(joystick,
  1307. joystick->hwdata->abs_map[code],
  1308. events[i].value);
  1309. break;
  1310. }
  1311. break;
  1312. case EV_REL:
  1313. switch (code) {
  1314. case REL_X:
  1315. case REL_Y:
  1316. code -= REL_X;
  1317. HandleBall(joystick, code / 2, code % 2, events[i].value);
  1318. break;
  1319. default:
  1320. break;
  1321. }
  1322. break;
  1323. case EV_SYN:
  1324. switch (code) {
  1325. case SYN_DROPPED :
  1326. #ifdef DEBUG_INPUT_EVENTS
  1327. SDL_Log("Event SYN_DROPPED detected\n");
  1328. #endif
  1329. joystick->hwdata->recovering_from_dropped = SDL_TRUE;
  1330. break;
  1331. case SYN_REPORT :
  1332. if (joystick->hwdata->recovering_from_dropped) {
  1333. joystick->hwdata->recovering_from_dropped = SDL_FALSE;
  1334. PollAllValues(joystick); /* try to sync up to current state now */
  1335. }
  1336. break;
  1337. default:
  1338. break;
  1339. }
  1340. default:
  1341. break;
  1342. }
  1343. }
  1344. }
  1345. if (errno == ENODEV) {
  1346. /* We have to wait until the JoystickDetect callback to remove this */
  1347. joystick->hwdata->gone = SDL_TRUE;
  1348. }
  1349. }
  1350. static void
  1351. HandleClassicEvents(SDL_Joystick *joystick)
  1352. {
  1353. struct js_event events[32];
  1354. int i, len, code, hat_index;
  1355. joystick->hwdata->fresh = SDL_FALSE;
  1356. while ((len = read(joystick->hwdata->fd, events, (sizeof events))) > 0) {
  1357. len /= sizeof(events[0]);
  1358. for (i = 0; i < len; ++i) {
  1359. switch (events[i].type) {
  1360. case JS_EVENT_BUTTON:
  1361. code = joystick->hwdata->key_pam[events[i].number];
  1362. SDL_PrivateJoystickButton(joystick,
  1363. joystick->hwdata->key_map[code],
  1364. events[i].value);
  1365. break;
  1366. case JS_EVENT_AXIS:
  1367. code = joystick->hwdata->abs_pam[events[i].number];
  1368. switch (code) {
  1369. case ABS_HAT0X:
  1370. case ABS_HAT0Y:
  1371. case ABS_HAT1X:
  1372. case ABS_HAT1Y:
  1373. case ABS_HAT2X:
  1374. case ABS_HAT2Y:
  1375. case ABS_HAT3X:
  1376. case ABS_HAT3Y:
  1377. hat_index = (code - ABS_HAT0X) / 2;
  1378. if (joystick->hwdata->has_hat[hat_index]) {
  1379. HandleHat(joystick, hat_index, code % 2, events[i].value);
  1380. break;
  1381. }
  1382. default:
  1383. SDL_PrivateJoystickAxis(joystick,
  1384. joystick->hwdata->abs_map[code],
  1385. events[i].value);
  1386. break;
  1387. }
  1388. }
  1389. }
  1390. }
  1391. }
  1392. static void
  1393. LINUX_JoystickUpdate(SDL_Joystick *joystick)
  1394. {
  1395. int i;
  1396. if (joystick->hwdata->m_bSteamController) {
  1397. SDL_UpdateSteamController(joystick);
  1398. return;
  1399. }
  1400. if (joystick->hwdata->classic) {
  1401. HandleClassicEvents(joystick);
  1402. } else {
  1403. HandleInputEvents(joystick);
  1404. }
  1405. /* Deliver ball motion updates */
  1406. for (i = 0; i < joystick->nballs; ++i) {
  1407. int xrel, yrel;
  1408. xrel = joystick->hwdata->balls[i].axis[0];
  1409. yrel = joystick->hwdata->balls[i].axis[1];
  1410. if (xrel || yrel) {
  1411. joystick->hwdata->balls[i].axis[0] = 0;
  1412. joystick->hwdata->balls[i].axis[1] = 0;
  1413. SDL_PrivateJoystickBall(joystick, (Uint8) i, xrel, yrel);
  1414. }
  1415. }
  1416. }
  1417. /* Function to close a joystick after use */
  1418. static void
  1419. LINUX_JoystickClose(SDL_Joystick *joystick)
  1420. {
  1421. if (joystick->hwdata) {
  1422. if (joystick->hwdata->effect.id >= 0) {
  1423. ioctl(joystick->hwdata->fd, EVIOCRMFF, joystick->hwdata->effect.id);
  1424. joystick->hwdata->effect.id = -1;
  1425. }
  1426. if (joystick->hwdata->fd >= 0) {
  1427. close(joystick->hwdata->fd);
  1428. }
  1429. if (joystick->hwdata->item) {
  1430. joystick->hwdata->item->hwdata = NULL;
  1431. }
  1432. SDL_free(joystick->hwdata->key_pam);
  1433. SDL_free(joystick->hwdata->abs_pam);
  1434. SDL_free(joystick->hwdata->hats);
  1435. SDL_free(joystick->hwdata->balls);
  1436. SDL_free(joystick->hwdata->fname);
  1437. SDL_free(joystick->hwdata);
  1438. }
  1439. }
  1440. /* Function to perform any system-specific joystick related cleanup */
  1441. static void
  1442. LINUX_JoystickQuit(void)
  1443. {
  1444. SDL_joylist_item *item = NULL;
  1445. SDL_joylist_item *next = NULL;
  1446. if (inotify_fd >= 0) {
  1447. close(inotify_fd);
  1448. inotify_fd = -1;
  1449. }
  1450. for (item = SDL_joylist; item; item = next) {
  1451. next = item->next;
  1452. FreeJoylistItem(item);
  1453. }
  1454. SDL_joylist = SDL_joylist_tail = NULL;
  1455. numjoysticks = 0;
  1456. #if SDL_USE_LIBUDEV
  1457. if (enumeration_method == ENUMERATION_LIBUDEV) {
  1458. SDL_UDEV_DelCallback(joystick_udev_callback);
  1459. SDL_UDEV_Quit();
  1460. }
  1461. #endif
  1462. SDL_QuitSteamControllers();
  1463. }
  1464. /*
  1465. This is based on the Linux Gamepad Specification
  1466. available at: https://www.kernel.org/doc/html/v4.15/input/gamepad.html
  1467. */
  1468. static SDL_bool
  1469. LINUX_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
  1470. {
  1471. SDL_Joystick *joystick;
  1472. SDL_joylist_item *item = JoystickByDevIndex(device_index);
  1473. unsigned int mapped;
  1474. if (item->checked_mapping) {
  1475. if (item->mapping) {
  1476. SDL_memcpy(out, item->mapping, sizeof(*out));
  1477. #ifdef DEBUG_GAMEPAD_MAPPING
  1478. SDL_Log("Prior mapping for device %d", device_index);
  1479. #endif
  1480. return SDL_TRUE;
  1481. } else {
  1482. return SDL_FALSE;
  1483. }
  1484. }
  1485. /* We temporarily open the device to check how it's configured. Make
  1486. a fake SDL_Joystick object to do so. */
  1487. joystick = (SDL_Joystick *) SDL_calloc(sizeof(*joystick), 1);
  1488. if (joystick == NULL) {
  1489. SDL_OutOfMemory();
  1490. return SDL_FALSE;
  1491. }
  1492. SDL_memcpy(&joystick->guid, &item->guid, sizeof(item->guid));
  1493. joystick->hwdata = (struct joystick_hwdata *)
  1494. SDL_calloc(1, sizeof(*joystick->hwdata));
  1495. if (joystick->hwdata == NULL) {
  1496. SDL_free(joystick);
  1497. SDL_OutOfMemory();
  1498. return SDL_FALSE;
  1499. }
  1500. item->checked_mapping = SDL_TRUE;
  1501. if (PrepareJoystickHwdata(joystick, item) == -1) {
  1502. SDL_free(joystick->hwdata);
  1503. SDL_free(joystick);
  1504. return SDL_FALSE; /* SDL_SetError will already have been called */
  1505. }
  1506. /* don't assign `item->hwdata` so it's not in any global state. */
  1507. /* it is now safe to call LINUX_JoystickClose on this fake joystick. */
  1508. if (!joystick->hwdata->has_key[BTN_GAMEPAD]) {
  1509. /* Not a gamepad according to the specs. */
  1510. LINUX_JoystickClose(joystick);
  1511. SDL_free(joystick);
  1512. return SDL_FALSE;
  1513. }
  1514. /* We have a gamepad, start filling out the mappings */
  1515. if (joystick->hwdata->has_key[BTN_A]) {
  1516. out->a.kind = EMappingKind_Button;
  1517. out->a.target = joystick->hwdata->key_map[BTN_A];
  1518. #ifdef DEBUG_GAMEPAD_MAPPING
  1519. SDL_Log("Mapped A to button %d (BTN_A)", out->a.target);
  1520. #endif
  1521. }
  1522. if (joystick->hwdata->has_key[BTN_B]) {
  1523. out->b.kind = EMappingKind_Button;
  1524. out->b.target = joystick->hwdata->key_map[BTN_B];
  1525. #ifdef DEBUG_GAMEPAD_MAPPING
  1526. SDL_Log("Mapped B to button %d (BTN_B)", out->b.target);
  1527. #endif
  1528. }
  1529. /* Xbox controllers use BTN_X and BTN_Y, and PS4 controllers use BTN_WEST and BTN_NORTH */
  1530. if (SDL_JoystickGetVendor(joystick) == USB_VENDOR_SONY) {
  1531. if (joystick->hwdata->has_key[BTN_WEST]) {
  1532. out->x.kind = EMappingKind_Button;
  1533. out->x.target = joystick->hwdata->key_map[BTN_WEST];
  1534. #ifdef DEBUG_GAMEPAD_MAPPING
  1535. SDL_Log("Mapped X to button %d (BTN_WEST)", out->x.target);
  1536. #endif
  1537. }
  1538. if (joystick->hwdata->has_key[BTN_NORTH]) {
  1539. out->y.kind = EMappingKind_Button;
  1540. out->y.target = joystick->hwdata->key_map[BTN_NORTH];
  1541. #ifdef DEBUG_GAMEPAD_MAPPING
  1542. SDL_Log("Mapped Y to button %d (BTN_NORTH)", out->y.target);
  1543. #endif
  1544. }
  1545. } else {
  1546. if (joystick->hwdata->has_key[BTN_X]) {
  1547. out->x.kind = EMappingKind_Button;
  1548. out->x.target = joystick->hwdata->key_map[BTN_X];
  1549. #ifdef DEBUG_GAMEPAD_MAPPING
  1550. SDL_Log("Mapped X to button %d (BTN_X)", out->x.target);
  1551. #endif
  1552. }
  1553. if (joystick->hwdata->has_key[BTN_Y]) {
  1554. out->y.kind = EMappingKind_Button;
  1555. out->y.target = joystick->hwdata->key_map[BTN_Y];
  1556. #ifdef DEBUG_GAMEPAD_MAPPING
  1557. SDL_Log("Mapped Y to button %d (BTN_Y)", out->y.target);
  1558. #endif
  1559. }
  1560. }
  1561. if (joystick->hwdata->has_key[BTN_SELECT]) {
  1562. out->back.kind = EMappingKind_Button;
  1563. out->back.target = joystick->hwdata->key_map[BTN_SELECT];
  1564. #ifdef DEBUG_GAMEPAD_MAPPING
  1565. SDL_Log("Mapped BACK to button %d (BTN_SELECT)", out->back.target);
  1566. #endif
  1567. }
  1568. if (joystick->hwdata->has_key[BTN_START]) {
  1569. out->start.kind = EMappingKind_Button;
  1570. out->start.target = joystick->hwdata->key_map[BTN_START];
  1571. #ifdef DEBUG_GAMEPAD_MAPPING
  1572. SDL_Log("Mapped START to button %d (BTN_START)", out->start.target);
  1573. #endif
  1574. }
  1575. if (joystick->hwdata->has_key[BTN_THUMBL]) {
  1576. out->leftstick.kind = EMappingKind_Button;
  1577. out->leftstick.target = joystick->hwdata->key_map[BTN_THUMBL];
  1578. #ifdef DEBUG_GAMEPAD_MAPPING
  1579. SDL_Log("Mapped LEFTSTICK to button %d (BTN_THUMBL)", out->leftstick.target);
  1580. #endif
  1581. }
  1582. if (joystick->hwdata->has_key[BTN_THUMBR]) {
  1583. out->rightstick.kind = EMappingKind_Button;
  1584. out->rightstick.target = joystick->hwdata->key_map[BTN_THUMBR];
  1585. #ifdef DEBUG_GAMEPAD_MAPPING
  1586. SDL_Log("Mapped RIGHTSTICK to button %d (BTN_THUMBR)", out->rightstick.target);
  1587. #endif
  1588. }
  1589. if (joystick->hwdata->has_key[BTN_MODE]) {
  1590. out->guide.kind = EMappingKind_Button;
  1591. out->guide.target = joystick->hwdata->key_map[BTN_MODE];
  1592. #ifdef DEBUG_GAMEPAD_MAPPING
  1593. SDL_Log("Mapped GUIDE to button %d (BTN_MODE)", out->guide.target);
  1594. #endif
  1595. }
  1596. /*
  1597. According to the specs the D-Pad, the shoulder buttons and the triggers
  1598. can be digital, or analog, or both at the same time.
  1599. */
  1600. /* Prefer digital shoulder buttons, but settle for digital or analog hat. */
  1601. mapped = 0;
  1602. if (joystick->hwdata->has_key[BTN_TL]) {
  1603. out->leftshoulder.kind = EMappingKind_Button;
  1604. out->leftshoulder.target = joystick->hwdata->key_map[BTN_TL];
  1605. mapped |= 0x1;
  1606. #ifdef DEBUG_GAMEPAD_MAPPING
  1607. SDL_Log("Mapped LEFTSHOULDER to button %d (BTN_TL)", out->leftshoulder.target);
  1608. #endif
  1609. }
  1610. if (joystick->hwdata->has_key[BTN_TR]) {
  1611. out->rightshoulder.kind = EMappingKind_Button;
  1612. out->rightshoulder.target = joystick->hwdata->key_map[BTN_TR];
  1613. mapped |= 0x2;
  1614. #ifdef DEBUG_GAMEPAD_MAPPING
  1615. SDL_Log("Mapped RIGHTSHOULDER to button %d (BTN_TR)", out->rightshoulder.target);
  1616. #endif
  1617. }
  1618. if (mapped != 0x3 && joystick->hwdata->has_hat[1]) {
  1619. int hat = joystick->hwdata->hats_indices[1] << 4;
  1620. out->leftshoulder.kind = EMappingKind_Hat;
  1621. out->rightshoulder.kind = EMappingKind_Hat;
  1622. out->leftshoulder.target = hat | 0x4;
  1623. out->rightshoulder.target = hat | 0x2;
  1624. mapped |= 0x3;
  1625. #ifdef DEBUG_GAMEPAD_MAPPING
  1626. SDL_Log("Mapped LEFT+RIGHTSHOULDER to hat 1 (ABS_HAT1X, ABS_HAT1Y)");
  1627. #endif
  1628. }
  1629. if (!(mapped & 0x1) && joystick->hwdata->has_abs[ABS_HAT1Y]) {
  1630. out->leftshoulder.kind = EMappingKind_Axis;
  1631. out->leftshoulder.target = joystick->hwdata->abs_map[ABS_HAT1Y];
  1632. mapped |= 0x1;
  1633. #ifdef DEBUG_GAMEPAD_MAPPING
  1634. SDL_Log("Mapped LEFTSHOULDER to axis %d (ABS_HAT1Y)", out->leftshoulder.target);
  1635. #endif
  1636. }
  1637. if (!(mapped & 0x2) && joystick->hwdata->has_abs[ABS_HAT1X]) {
  1638. out->rightshoulder.kind = EMappingKind_Axis;
  1639. out->rightshoulder.target = joystick->hwdata->abs_map[ABS_HAT1X];
  1640. mapped |= 0x2;
  1641. #ifdef DEBUG_GAMEPAD_MAPPING
  1642. SDL_Log("Mapped RIGHTSHOULDER to axis %d (ABS_HAT1X)", out->rightshoulder.target);
  1643. #endif
  1644. }
  1645. /* Prefer analog triggers, but settle for digital hat or buttons. */
  1646. mapped = 0;
  1647. if (joystick->hwdata->has_abs[ABS_HAT2Y]) {
  1648. out->lefttrigger.kind = EMappingKind_Axis;
  1649. out->lefttrigger.target = joystick->hwdata->abs_map[ABS_HAT2Y];
  1650. mapped |= 0x1;
  1651. #ifdef DEBUG_GAMEPAD_MAPPING
  1652. SDL_Log("Mapped LEFTTRIGGER to axis %d (ABS_HAT2Y)", out->lefttrigger.target);
  1653. #endif
  1654. } else if (joystick->hwdata->has_abs[ABS_Z]) {
  1655. out->lefttrigger.kind = EMappingKind_Axis;
  1656. out->lefttrigger.target = joystick->hwdata->abs_map[ABS_Z];
  1657. mapped |= 0x1;
  1658. #ifdef DEBUG_GAMEPAD_MAPPING
  1659. SDL_Log("Mapped LEFTTRIGGER to axis %d (ABS_Z)", out->lefttrigger.target);
  1660. #endif
  1661. }
  1662. if (joystick->hwdata->has_abs[ABS_HAT2X]) {
  1663. out->righttrigger.kind = EMappingKind_Axis;
  1664. out->righttrigger.target = joystick->hwdata->abs_map[ABS_HAT2X];
  1665. mapped |= 0x2;
  1666. #ifdef DEBUG_GAMEPAD_MAPPING
  1667. SDL_Log("Mapped RIGHTTRIGGER to axis %d (ABS_HAT2X)", out->righttrigger.target);
  1668. #endif
  1669. } else if (joystick->hwdata->has_abs[ABS_RZ]) {
  1670. out->righttrigger.kind = EMappingKind_Axis;
  1671. out->righttrigger.target = joystick->hwdata->abs_map[ABS_RZ];
  1672. mapped |= 0x2;
  1673. #ifdef DEBUG_GAMEPAD_MAPPING
  1674. SDL_Log("Mapped RIGHTTRIGGER to axis %d (ABS_RZ)", out->righttrigger.target);
  1675. #endif
  1676. }
  1677. if (mapped != 0x3 && joystick->hwdata->has_hat[2]) {
  1678. int hat = joystick->hwdata->hats_indices[2] << 4;
  1679. out->lefttrigger.kind = EMappingKind_Hat;
  1680. out->righttrigger.kind = EMappingKind_Hat;
  1681. out->lefttrigger.target = hat | 0x4;
  1682. out->righttrigger.target = hat | 0x2;
  1683. mapped |= 0x3;
  1684. #ifdef DEBUG_GAMEPAD_MAPPING
  1685. SDL_Log("Mapped LEFT+RIGHTTRIGGER to hat 2 (ABS_HAT2X, ABS_HAT2Y)");
  1686. #endif
  1687. }
  1688. if (!(mapped & 0x1) && joystick->hwdata->has_key[BTN_TL2]) {
  1689. out->lefttrigger.kind = EMappingKind_Button;
  1690. out->lefttrigger.target = joystick->hwdata->key_map[BTN_TL2];
  1691. mapped |= 0x1;
  1692. #ifdef DEBUG_GAMEPAD_MAPPING
  1693. SDL_Log("Mapped LEFTTRIGGER to button %d (BTN_TL2)", out->lefttrigger.target);
  1694. #endif
  1695. }
  1696. if (!(mapped & 0x2) && joystick->hwdata->has_key[BTN_TR2]) {
  1697. out->righttrigger.kind = EMappingKind_Button;
  1698. out->righttrigger.target = joystick->hwdata->key_map[BTN_TR2];
  1699. mapped |= 0x2;
  1700. #ifdef DEBUG_GAMEPAD_MAPPING
  1701. SDL_Log("Mapped RIGHTTRIGGER to button %d (BTN_TR2)", out->righttrigger.target);
  1702. #endif
  1703. }
  1704. /* Prefer digital D-Pad buttons, but settle for digital or analog hat. */
  1705. mapped = 0;
  1706. if (joystick->hwdata->has_key[BTN_DPAD_UP]) {
  1707. out->dpup.kind = EMappingKind_Button;
  1708. out->dpup.target = joystick->hwdata->key_map[BTN_DPAD_UP];
  1709. mapped |= 0x1;
  1710. #ifdef DEBUG_GAMEPAD_MAPPING
  1711. SDL_Log("Mapped DPUP to button %d (BTN_DPAD_UP)", out->dpup.target);
  1712. #endif
  1713. }
  1714. if (joystick->hwdata->has_key[BTN_DPAD_DOWN]) {
  1715. out->dpdown.kind = EMappingKind_Button;
  1716. out->dpdown.target = joystick->hwdata->key_map[BTN_DPAD_DOWN];
  1717. mapped |= 0x2;
  1718. #ifdef DEBUG_GAMEPAD_MAPPING
  1719. SDL_Log("Mapped DPDOWN to button %d (BTN_DPAD_DOWN)", out->dpdown.target);
  1720. #endif
  1721. }
  1722. if (joystick->hwdata->has_key[BTN_DPAD_LEFT]) {
  1723. out->dpleft.kind = EMappingKind_Button;
  1724. out->dpleft.target = joystick->hwdata->key_map[BTN_DPAD_LEFT];
  1725. mapped |= 0x4;
  1726. #ifdef DEBUG_GAMEPAD_MAPPING
  1727. SDL_Log("Mapped DPLEFT to button %d (BTN_DPAD_LEFT)", out->dpleft.target);
  1728. #endif
  1729. }
  1730. if (joystick->hwdata->has_key[BTN_DPAD_RIGHT]) {
  1731. out->dpright.kind = EMappingKind_Button;
  1732. out->dpright.target = joystick->hwdata->key_map[BTN_DPAD_RIGHT];
  1733. mapped |= 0x8;
  1734. #ifdef DEBUG_GAMEPAD_MAPPING
  1735. SDL_Log("Mapped DPRIGHT to button %d (BTN_DPAD_RIGHT)", out->dpright.target);
  1736. #endif
  1737. }
  1738. if (mapped != 0xF) {
  1739. if (joystick->hwdata->has_hat[0]) {
  1740. int hat = joystick->hwdata->hats_indices[0] << 4;
  1741. out->dpleft.kind = EMappingKind_Hat;
  1742. out->dpright.kind = EMappingKind_Hat;
  1743. out->dpup.kind = EMappingKind_Hat;
  1744. out->dpdown.kind = EMappingKind_Hat;
  1745. out->dpleft.target = hat | 0x8;
  1746. out->dpright.target = hat | 0x2;
  1747. out->dpup.target = hat | 0x1;
  1748. out->dpdown.target = hat | 0x4;
  1749. mapped |= 0xF;
  1750. #ifdef DEBUG_GAMEPAD_MAPPING
  1751. SDL_Log("Mapped DPUP+DOWN+LEFT+RIGHT to hat 0 (ABS_HAT0X, ABS_HAT0Y)");
  1752. #endif
  1753. } else if (joystick->hwdata->has_abs[ABS_HAT0X] && joystick->hwdata->has_abs[ABS_HAT0Y]) {
  1754. out->dpleft.kind = EMappingKind_Axis;
  1755. out->dpright.kind = EMappingKind_Axis;
  1756. out->dpup.kind = EMappingKind_Axis;
  1757. out->dpdown.kind = EMappingKind_Axis;
  1758. out->dpleft.target = joystick->hwdata->abs_map[ABS_HAT0X];
  1759. out->dpright.target = joystick->hwdata->abs_map[ABS_HAT0X];
  1760. out->dpup.target = joystick->hwdata->abs_map[ABS_HAT0Y];
  1761. out->dpdown.target = joystick->hwdata->abs_map[ABS_HAT0Y];
  1762. mapped |= 0xF;
  1763. #ifdef DEBUG_GAMEPAD_MAPPING
  1764. SDL_Log("Mapped DPUP+DOWN to axis %d (ABS_HAT0Y)", out->dpup.target);
  1765. SDL_Log("Mapped DPLEFT+RIGHT to axis %d (ABS_HAT0X)", out->dpleft.target);
  1766. #endif
  1767. }
  1768. }
  1769. if (joystick->hwdata->has_abs[ABS_X] && joystick->hwdata->has_abs[ABS_Y]) {
  1770. out->leftx.kind = EMappingKind_Axis;
  1771. out->lefty.kind = EMappingKind_Axis;
  1772. out->leftx.target = joystick->hwdata->abs_map[ABS_X];
  1773. out->lefty.target = joystick->hwdata->abs_map[ABS_Y];
  1774. #ifdef DEBUG_GAMEPAD_MAPPING
  1775. SDL_Log("Mapped LEFTX to axis %d (ABS_X)", out->leftx.target);
  1776. SDL_Log("Mapped LEFTY to axis %d (ABS_Y)", out->lefty.target);
  1777. #endif
  1778. }
  1779. if (joystick->hwdata->has_abs[ABS_RX] && joystick->hwdata->has_abs[ABS_RY]) {
  1780. out->rightx.kind = EMappingKind_Axis;
  1781. out->righty.kind = EMappingKind_Axis;
  1782. out->rightx.target = joystick->hwdata->abs_map[ABS_RX];
  1783. out->righty.target = joystick->hwdata->abs_map[ABS_RY];
  1784. #ifdef DEBUG_GAMEPAD_MAPPING
  1785. SDL_Log("Mapped RIGHTX to axis %d (ABS_RX)", out->rightx.target);
  1786. SDL_Log("Mapped RIGHTY to axis %d (ABS_RY)", out->righty.target);
  1787. #endif
  1788. }
  1789. LINUX_JoystickClose(joystick);
  1790. SDL_free(joystick);
  1791. /* Cache the mapping for later */
  1792. item->mapping = (SDL_GamepadMapping *)SDL_malloc(sizeof(*item->mapping));
  1793. if (item->mapping) {
  1794. SDL_memcpy(item->mapping, out, sizeof(*out));
  1795. }
  1796. #ifdef DEBUG_GAMEPAD_MAPPING
  1797. SDL_Log("Generated mapping for device %d", device_index);
  1798. #endif
  1799. return SDL_TRUE;
  1800. }
  1801. SDL_JoystickDriver SDL_LINUX_JoystickDriver =
  1802. {
  1803. LINUX_JoystickInit,
  1804. LINUX_JoystickGetCount,
  1805. LINUX_JoystickDetect,
  1806. LINUX_JoystickGetDeviceName,
  1807. LINUX_JoystickGetDevicePath,
  1808. LINUX_JoystickGetDevicePlayerIndex,
  1809. LINUX_JoystickSetDevicePlayerIndex,
  1810. LINUX_JoystickGetDeviceGUID,
  1811. LINUX_JoystickGetDeviceInstanceID,
  1812. LINUX_JoystickOpen,
  1813. LINUX_JoystickRumble,
  1814. LINUX_JoystickRumbleTriggers,
  1815. LINUX_JoystickGetCapabilities,
  1816. LINUX_JoystickSetLED,
  1817. LINUX_JoystickSendEffect,
  1818. LINUX_JoystickSetSensorsEnabled,
  1819. LINUX_JoystickUpdate,
  1820. LINUX_JoystickClose,
  1821. LINUX_JoystickQuit,
  1822. LINUX_JoystickGetGamepadMapping
  1823. };
  1824. #endif /* SDL_JOYSTICK_LINUX */
  1825. /* vi: set ts=4 sw=4 expandtab: */