SDL_sysjoystick.c 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 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. #include <string.h> /* strerror */
  31. #endif
  32. #include <sys/ioctl.h>
  33. #include <unistd.h>
  34. #include <dirent.h>
  35. #include <linux/joystick.h>
  36. #include "../../SDL_utils_c.h"
  37. #include "../../events/SDL_events_c.h"
  38. #include "../../core/linux/SDL_evdev.h"
  39. #include "../SDL_sysjoystick.h"
  40. #include "../SDL_joystick_c.h"
  41. #include "../usb_ids.h"
  42. #include "../steam/SDL_steamcontroller.h"
  43. #include "SDL_sysjoystick_c.h"
  44. #include "../hidapi/SDL_hidapijoystick_c.h"
  45. /* This isn't defined in older Linux kernel headers */
  46. #ifndef MSC_TIMESTAMP
  47. #define MSC_TIMESTAMP 0x05
  48. #endif
  49. #ifndef SYN_DROPPED
  50. #define SYN_DROPPED 3
  51. #endif
  52. #ifndef BTN_NORTH
  53. #define BTN_NORTH 0x133
  54. #endif
  55. #ifndef BTN_WEST
  56. #define BTN_WEST 0x134
  57. #endif
  58. #ifndef BTN_DPAD_UP
  59. #define BTN_DPAD_UP 0x220
  60. #endif
  61. #ifndef BTN_DPAD_DOWN
  62. #define BTN_DPAD_DOWN 0x221
  63. #endif
  64. #ifndef BTN_DPAD_LEFT
  65. #define BTN_DPAD_LEFT 0x222
  66. #endif
  67. #ifndef BTN_DPAD_RIGHT
  68. #define BTN_DPAD_RIGHT 0x223
  69. #endif
  70. #ifndef BTN_TRIGGER_HAPPY
  71. #define BTN_TRIGGER_HAPPY 0x2c0
  72. #define BTN_TRIGGER_HAPPY1 0x2c0
  73. #define BTN_TRIGGER_HAPPY2 0x2c1
  74. #define BTN_TRIGGER_HAPPY3 0x2c2
  75. #define BTN_TRIGGER_HAPPY4 0x2c3
  76. #define BTN_TRIGGER_HAPPY5 0x2c4
  77. #define BTN_TRIGGER_HAPPY6 0x2c5
  78. #define BTN_TRIGGER_HAPPY7 0x2c6
  79. #define BTN_TRIGGER_HAPPY8 0x2c7
  80. #define BTN_TRIGGER_HAPPY9 0x2c8
  81. #define BTN_TRIGGER_HAPPY10 0x2c9
  82. #define BTN_TRIGGER_HAPPY11 0x2ca
  83. #define BTN_TRIGGER_HAPPY12 0x2cb
  84. #define BTN_TRIGGER_HAPPY13 0x2cc
  85. #define BTN_TRIGGER_HAPPY14 0x2cd
  86. #define BTN_TRIGGER_HAPPY15 0x2ce
  87. #define BTN_TRIGGER_HAPPY16 0x2cf
  88. #define BTN_TRIGGER_HAPPY17 0x2d0
  89. #define BTN_TRIGGER_HAPPY18 0x2d1
  90. #define BTN_TRIGGER_HAPPY19 0x2d2
  91. #define BTN_TRIGGER_HAPPY20 0x2d3
  92. #define BTN_TRIGGER_HAPPY21 0x2d4
  93. #define BTN_TRIGGER_HAPPY22 0x2d5
  94. #define BTN_TRIGGER_HAPPY23 0x2d6
  95. #define BTN_TRIGGER_HAPPY24 0x2d7
  96. #define BTN_TRIGGER_HAPPY25 0x2d8
  97. #define BTN_TRIGGER_HAPPY26 0x2d9
  98. #define BTN_TRIGGER_HAPPY27 0x2da
  99. #define BTN_TRIGGER_HAPPY28 0x2db
  100. #define BTN_TRIGGER_HAPPY29 0x2dc
  101. #define BTN_TRIGGER_HAPPY30 0x2dd
  102. #define BTN_TRIGGER_HAPPY31 0x2de
  103. #define BTN_TRIGGER_HAPPY32 0x2df
  104. #define BTN_TRIGGER_HAPPY33 0x2e0
  105. #define BTN_TRIGGER_HAPPY34 0x2e1
  106. #define BTN_TRIGGER_HAPPY35 0x2e2
  107. #define BTN_TRIGGER_HAPPY36 0x2e3
  108. #define BTN_TRIGGER_HAPPY37 0x2e4
  109. #define BTN_TRIGGER_HAPPY38 0x2e5
  110. #define BTN_TRIGGER_HAPPY39 0x2e6
  111. #define BTN_TRIGGER_HAPPY40 0x2e7
  112. #endif
  113. #include "../../core/linux/SDL_evdev_capabilities.h"
  114. #include "../../core/linux/SDL_udev.h"
  115. #include "../../core/linux/SDL_sandbox.h"
  116. #if 0
  117. #define DEBUG_INPUT_EVENTS 1
  118. #endif
  119. #if 0
  120. #define DEBUG_GAMEPAD_MAPPING 1
  121. #endif
  122. typedef enum
  123. {
  124. ENUMERATION_UNSET,
  125. ENUMERATION_LIBUDEV,
  126. ENUMERATION_FALLBACK
  127. } EnumerationMethod;
  128. static EnumerationMethod enumeration_method = ENUMERATION_UNSET;
  129. static SDL_bool IsJoystickJSNode(const char *node);
  130. static void MaybeAddDevice(const char *path);
  131. static void MaybeRemoveDevice(const char *path);
  132. /* A linked list of available joysticks */
  133. typedef struct SDL_joylist_item
  134. {
  135. SDL_JoystickID device_instance;
  136. char *path; /* "/dev/input/event2" or whatever */
  137. char *name; /* "SideWinder 3D Pro" or whatever */
  138. SDL_JoystickGUID guid;
  139. dev_t devnum;
  140. int steam_virtual_gamepad_slot;
  141. struct joystick_hwdata *hwdata;
  142. struct SDL_joylist_item *next;
  143. /* Steam Controller support */
  144. SDL_bool m_bSteamController;
  145. SDL_bool checked_mapping;
  146. SDL_GamepadMapping *mapping;
  147. } SDL_joylist_item;
  148. /* A linked list of available gamepad sensors */
  149. typedef struct SDL_sensorlist_item
  150. {
  151. char *path; /* "/dev/input/event2" or whatever */
  152. dev_t devnum;
  153. struct joystick_hwdata *hwdata;
  154. struct SDL_sensorlist_item *next;
  155. } SDL_sensorlist_item;
  156. static SDL_bool SDL_classic_joysticks = SDL_FALSE;
  157. static SDL_joylist_item *SDL_joylist SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
  158. static SDL_joylist_item *SDL_joylist_tail SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
  159. static int numjoysticks SDL_GUARDED_BY(SDL_joystick_lock) = 0;
  160. static SDL_sensorlist_item *SDL_sensorlist SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
  161. static int inotify_fd = -1;
  162. static Uint64 last_joy_detect_time;
  163. static time_t last_input_dir_mtime;
  164. static void FixupDeviceInfoForMapping(int fd, struct input_id *inpid)
  165. {
  166. if (inpid->vendor == 0x045e && inpid->product == 0x0b05 && inpid->version == 0x0903) {
  167. /* This is a Microsoft Xbox One Elite Series 2 controller */
  168. unsigned long keybit[NBITS(KEY_MAX)] = { 0 };
  169. /* The first version of the firmware duplicated all the inputs */
  170. if ((ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) >= 0) &&
  171. test_bit(0x2c0, keybit)) {
  172. /* Change the version to 0x0902, so we can map it differently */
  173. inpid->version = 0x0902;
  174. }
  175. }
  176. /* For Atari vcs modern and classic controllers have the version reflecting
  177. * firmware version, but the mapping stays stable so ignore
  178. * version information */
  179. if (inpid->vendor == 0x3250 && (inpid->product == 0x1001 || inpid->product == 0x1002)) {
  180. inpid->version = 0;
  181. }
  182. }
  183. #ifdef SDL_JOYSTICK_HIDAPI
  184. static SDL_bool IsVirtualJoystick(Uint16 vendor, Uint16 product, Uint16 version, const char *name)
  185. {
  186. if (vendor == USB_VENDOR_MICROSOFT && product == USB_PRODUCT_XBOX_ONE_S && version == 0 &&
  187. SDL_strcmp(name, "Xbox One S Controller") == 0) {
  188. /* This is the virtual device created by the xow driver */
  189. return SDL_TRUE;
  190. }
  191. return SDL_FALSE;
  192. }
  193. #endif /* SDL_JOYSTICK_HIDAPI */
  194. static SDL_bool GetSteamVirtualGamepadSlot(int fd, int *slot)
  195. {
  196. char name[128];
  197. if (ioctl(fd, EVIOCGNAME(sizeof(name)), name) > 0) {
  198. const char *digits = SDL_strstr(name, "pad ");
  199. if (digits) {
  200. digits += 4;
  201. if (SDL_isdigit(*digits)) {
  202. *slot = SDL_atoi(digits);
  203. return SDL_TRUE;
  204. }
  205. }
  206. }
  207. return SDL_FALSE;
  208. }
  209. static int GuessDeviceClass(int fd)
  210. {
  211. unsigned long propbit[NBITS(INPUT_PROP_MAX)] = { 0 };
  212. unsigned long evbit[NBITS(EV_MAX)] = { 0 };
  213. unsigned long keybit[NBITS(KEY_MAX)] = { 0 };
  214. unsigned long absbit[NBITS(ABS_MAX)] = { 0 };
  215. unsigned long relbit[NBITS(REL_MAX)] = { 0 };
  216. if ((ioctl(fd, EVIOCGBIT(0, sizeof(evbit)), evbit) < 0) ||
  217. (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) < 0) ||
  218. (ioctl(fd, EVIOCGBIT(EV_REL, sizeof(relbit)), relbit) < 0) ||
  219. (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) < 0)) {
  220. return 0;
  221. }
  222. /* This is a newer feature, so it's allowed to fail - if so, then the
  223. * device just doesn't have any properties. */
  224. (void) ioctl(fd, EVIOCGPROP(sizeof(propbit)), propbit);
  225. return SDL_EVDEV_GuessDeviceClass(propbit, evbit, absbit, keybit, relbit);
  226. }
  227. static int GuessIsJoystick(int fd)
  228. {
  229. if (GuessDeviceClass(fd) & SDL_UDEV_DEVICE_JOYSTICK) {
  230. return 1;
  231. }
  232. return 0;
  233. }
  234. static int GuessIsSensor(int fd)
  235. {
  236. if (GuessDeviceClass(fd) & SDL_UDEV_DEVICE_ACCELEROMETER) {
  237. return 1;
  238. }
  239. return 0;
  240. }
  241. static int IsJoystick(const char *path, int fd, char **name_return, Uint16 *vendor_return, Uint16 *product_return, SDL_JoystickGUID *guid)
  242. {
  243. struct input_id inpid;
  244. char *name;
  245. char product_string[128];
  246. int class = 0;
  247. SDL_zero(inpid);
  248. #ifdef SDL_USE_LIBUDEV
  249. SDL_UDEV_GetProductInfo(path, &inpid.vendor, &inpid.product, &inpid.version, &class);
  250. #endif
  251. if (ioctl(fd, JSIOCGNAME(sizeof(product_string)), product_string) <= 0) {
  252. /* When udev is enabled we only get joystick devices here, so there's no need to test them */
  253. if (enumeration_method != ENUMERATION_LIBUDEV &&
  254. !(class & SDL_UDEV_DEVICE_JOYSTICK) && ( class || !GuessIsJoystick(fd))) {
  255. return 0;
  256. }
  257. /* Could have vendor and product already from udev, but should agree with evdev */
  258. if (ioctl(fd, EVIOCGID, &inpid) < 0) {
  259. return 0;
  260. }
  261. if (ioctl(fd, EVIOCGNAME(sizeof(product_string)), product_string) < 0) {
  262. return 0;
  263. }
  264. }
  265. name = SDL_CreateJoystickName(inpid.vendor, inpid.product, NULL, product_string);
  266. if (!name) {
  267. return 0;
  268. }
  269. #ifdef SDL_JOYSTICK_HIDAPI
  270. if (!IsVirtualJoystick(inpid.vendor, inpid.product, inpid.version, name) &&
  271. HIDAPI_IsDevicePresent(inpid.vendor, inpid.product, inpid.version, name)) {
  272. /* The HIDAPI driver is taking care of this device */
  273. SDL_free(name);
  274. return 0;
  275. }
  276. #endif
  277. FixupDeviceInfoForMapping(fd, &inpid);
  278. #ifdef DEBUG_JOYSTICK
  279. SDL_Log("Joystick: %s, bustype = %d, vendor = 0x%.4x, product = 0x%.4x, version = %d\n", name, inpid.bustype, inpid.vendor, inpid.product, inpid.version);
  280. #endif
  281. *guid = SDL_CreateJoystickGUID(inpid.bustype, inpid.vendor, inpid.product, inpid.version, NULL, product_string, 0, 0);
  282. if (SDL_ShouldIgnoreJoystick(name, *guid)) {
  283. SDL_free(name);
  284. return 0;
  285. }
  286. *name_return = name;
  287. *vendor_return = inpid.vendor;
  288. *product_return = inpid.product;
  289. return 1;
  290. }
  291. static int IsSensor(const char *path, int fd)
  292. {
  293. struct input_id inpid;
  294. if (ioctl(fd, EVIOCGID, &inpid) < 0) {
  295. return 0;
  296. }
  297. if (inpid.vendor == USB_VENDOR_NINTENDO && inpid.product == USB_PRODUCT_NINTENDO_WII_REMOTE) {
  298. /* Wii extension controls */
  299. /* These may create 3 sensor devices but we only support reading from 1: ignore them */
  300. return 0;
  301. }
  302. return GuessIsSensor(fd);
  303. }
  304. #ifdef SDL_USE_LIBUDEV
  305. static void joystick_udev_callback(SDL_UDEV_deviceevent udev_type, int udev_class, const char *devpath)
  306. {
  307. if (!devpath) {
  308. return;
  309. }
  310. switch (udev_type) {
  311. case SDL_UDEV_DEVICEADDED:
  312. if (!(udev_class & SDL_UDEV_DEVICE_JOYSTICK)) {
  313. return;
  314. }
  315. if (SDL_classic_joysticks) {
  316. if (!IsJoystickJSNode(devpath)) {
  317. return;
  318. }
  319. } else {
  320. if (IsJoystickJSNode(devpath)) {
  321. return;
  322. }
  323. }
  324. /* Wait a bit for the hidraw udev node to initialize */
  325. SDL_Delay(10);
  326. MaybeAddDevice(devpath);
  327. break;
  328. case SDL_UDEV_DEVICEREMOVED:
  329. MaybeRemoveDevice(devpath);
  330. break;
  331. default:
  332. break;
  333. }
  334. }
  335. #endif /* SDL_USE_LIBUDEV */
  336. static void FreeJoylistItem(SDL_joylist_item *item)
  337. {
  338. SDL_free(item->mapping);
  339. SDL_free(item->path);
  340. SDL_free(item->name);
  341. SDL_free(item);
  342. }
  343. static void FreeSensorlistItem(SDL_sensorlist_item *item)
  344. {
  345. SDL_free(item->path);
  346. SDL_free(item);
  347. }
  348. static void MaybeAddDevice(const char *path)
  349. {
  350. struct stat sb;
  351. int fd = -1;
  352. char *name = NULL;
  353. Uint16 vendor, product;
  354. SDL_JoystickGUID guid;
  355. SDL_joylist_item *item;
  356. SDL_sensorlist_item *item_sensor;
  357. if (!path) {
  358. return;
  359. }
  360. if (stat(path, &sb) == -1) {
  361. return;
  362. }
  363. SDL_LockJoysticks();
  364. /* Check to make sure it's not already in list. */
  365. for (item = SDL_joylist; item; item = item->next) {
  366. if (sb.st_rdev == item->devnum) {
  367. goto done; /* already have this one */
  368. }
  369. }
  370. for (item_sensor = SDL_sensorlist; item_sensor; item_sensor = item_sensor->next) {
  371. if (sb.st_rdev == item_sensor->devnum) {
  372. goto done; /* already have this one */
  373. }
  374. }
  375. fd = open(path, O_RDONLY | O_CLOEXEC, 0);
  376. if (fd < 0) {
  377. goto done;
  378. }
  379. #ifdef DEBUG_INPUT_EVENTS
  380. SDL_Log("Checking %s\n", path);
  381. #endif
  382. if (IsJoystick(path, fd, &name, &vendor, &product, &guid)) {
  383. #ifdef DEBUG_INPUT_EVENTS
  384. SDL_Log("found joystick: %s\n", path);
  385. #endif
  386. item = (SDL_joylist_item *)SDL_calloc(1, sizeof(SDL_joylist_item));
  387. if (!item) {
  388. SDL_free(name);
  389. goto done;
  390. }
  391. item->devnum = sb.st_rdev;
  392. item->steam_virtual_gamepad_slot = -1;
  393. item->path = SDL_strdup(path);
  394. item->name = name;
  395. item->guid = guid;
  396. if (vendor == USB_VENDOR_VALVE &&
  397. product == USB_PRODUCT_STEAM_VIRTUAL_GAMEPAD) {
  398. GetSteamVirtualGamepadSlot(fd, &item->steam_virtual_gamepad_slot);
  399. }
  400. if ((!item->path) || (!item->name)) {
  401. FreeJoylistItem(item);
  402. goto done;
  403. }
  404. item->device_instance = SDL_GetNextObjectID();
  405. if (!SDL_joylist_tail) {
  406. SDL_joylist = SDL_joylist_tail = item;
  407. } else {
  408. SDL_joylist_tail->next = item;
  409. SDL_joylist_tail = item;
  410. }
  411. /* Need to increment the joystick count before we post the event */
  412. ++numjoysticks;
  413. SDL_PrivateJoystickAdded(item->device_instance);
  414. goto done;
  415. }
  416. if (IsSensor(path, fd)) {
  417. #ifdef DEBUG_INPUT_EVENTS
  418. SDL_Log("found sensor: %s\n", path);
  419. #endif
  420. item_sensor = (SDL_sensorlist_item *)SDL_calloc(1, sizeof(SDL_sensorlist_item));
  421. if (!item_sensor) {
  422. goto done;
  423. }
  424. item_sensor->devnum = sb.st_rdev;
  425. item_sensor->path = SDL_strdup(path);
  426. if (!item_sensor->path) {
  427. FreeSensorlistItem(item_sensor);
  428. goto done;
  429. }
  430. item_sensor->next = SDL_sensorlist;
  431. SDL_sensorlist = item_sensor;
  432. goto done;
  433. }
  434. done:
  435. if (fd >= 0) {
  436. close(fd);
  437. }
  438. SDL_UnlockJoysticks();
  439. }
  440. static void RemoveJoylistItem(SDL_joylist_item *item, SDL_joylist_item *prev)
  441. {
  442. SDL_AssertJoysticksLocked();
  443. if (item->hwdata) {
  444. item->hwdata->item = NULL;
  445. }
  446. if (prev) {
  447. prev->next = item->next;
  448. } else {
  449. SDL_assert(SDL_joylist == item);
  450. SDL_joylist = item->next;
  451. }
  452. if (item == SDL_joylist_tail) {
  453. SDL_joylist_tail = prev;
  454. }
  455. /* Need to decrement the joystick count before we post the event */
  456. --numjoysticks;
  457. SDL_PrivateJoystickRemoved(item->device_instance);
  458. FreeJoylistItem(item);
  459. }
  460. static void RemoveSensorlistItem(SDL_sensorlist_item *item, SDL_sensorlist_item *prev)
  461. {
  462. SDL_AssertJoysticksLocked();
  463. if (item->hwdata) {
  464. item->hwdata->item_sensor = NULL;
  465. }
  466. if (prev) {
  467. prev->next = item->next;
  468. } else {
  469. SDL_assert(SDL_sensorlist == item);
  470. SDL_sensorlist = item->next;
  471. }
  472. /* Do not call SDL_PrivateJoystickRemoved here as RemoveJoylistItem will do it,
  473. * assuming both sensor and joy item are removed at the same time */
  474. FreeSensorlistItem(item);
  475. }
  476. static void MaybeRemoveDevice(const char *path)
  477. {
  478. SDL_joylist_item *item;
  479. SDL_joylist_item *prev = NULL;
  480. SDL_sensorlist_item *item_sensor;
  481. SDL_sensorlist_item *prev_sensor = NULL;
  482. if (!path) {
  483. return;
  484. }
  485. SDL_LockJoysticks();
  486. for (item = SDL_joylist; item; item = item->next) {
  487. /* found it, remove it. */
  488. if (SDL_strcmp(path, item->path) == 0) {
  489. RemoveJoylistItem(item, prev);
  490. goto done;
  491. }
  492. prev = item;
  493. }
  494. for (item_sensor = SDL_sensorlist; item_sensor; item_sensor = item_sensor->next) {
  495. /* found it, remove it. */
  496. if (SDL_strcmp(path, item_sensor->path) == 0) {
  497. RemoveSensorlistItem(item_sensor, prev_sensor);
  498. goto done;
  499. }
  500. prev_sensor = item_sensor;
  501. }
  502. done:
  503. SDL_UnlockJoysticks();
  504. }
  505. static void HandlePendingRemovals(void)
  506. {
  507. SDL_joylist_item *prev = NULL;
  508. SDL_joylist_item *item = NULL;
  509. SDL_sensorlist_item *prev_sensor = NULL;
  510. SDL_sensorlist_item *item_sensor = NULL;
  511. SDL_AssertJoysticksLocked();
  512. item = SDL_joylist;
  513. while (item) {
  514. if (item->hwdata && item->hwdata->gone) {
  515. RemoveJoylistItem(item, prev);
  516. if (prev) {
  517. item = prev->next;
  518. } else {
  519. item = SDL_joylist;
  520. }
  521. } else {
  522. prev = item;
  523. item = item->next;
  524. }
  525. }
  526. item_sensor = SDL_sensorlist;
  527. while (item_sensor) {
  528. if (item_sensor->hwdata && item_sensor->hwdata->sensor_gone) {
  529. RemoveSensorlistItem(item_sensor, prev_sensor);
  530. if (prev_sensor) {
  531. item_sensor = prev_sensor->next;
  532. } else {
  533. item_sensor = SDL_sensorlist;
  534. }
  535. } else {
  536. prev_sensor = item_sensor;
  537. item_sensor = item_sensor->next;
  538. }
  539. }
  540. }
  541. static SDL_bool SteamControllerConnectedCallback(const char *name, SDL_JoystickGUID guid, SDL_JoystickID *device_instance)
  542. {
  543. SDL_joylist_item *item;
  544. item = (SDL_joylist_item *)SDL_calloc(1, sizeof(SDL_joylist_item));
  545. if (!item) {
  546. return SDL_FALSE;
  547. }
  548. item->path = SDL_strdup("");
  549. item->name = SDL_strdup(name);
  550. item->guid = guid;
  551. item->m_bSteamController = SDL_TRUE;
  552. if ((!item->path) || (!item->name)) {
  553. FreeJoylistItem(item);
  554. return SDL_FALSE;
  555. }
  556. *device_instance = item->device_instance = SDL_GetNextObjectID();
  557. SDL_LockJoysticks();
  558. if (!SDL_joylist_tail) {
  559. SDL_joylist = SDL_joylist_tail = item;
  560. } else {
  561. SDL_joylist_tail->next = item;
  562. SDL_joylist_tail = item;
  563. }
  564. /* Need to increment the joystick count before we post the event */
  565. ++numjoysticks;
  566. SDL_PrivateJoystickAdded(item->device_instance);
  567. SDL_UnlockJoysticks();
  568. return SDL_TRUE;
  569. }
  570. static void SteamControllerDisconnectedCallback(SDL_JoystickID device_instance)
  571. {
  572. SDL_joylist_item *item;
  573. SDL_joylist_item *prev = NULL;
  574. SDL_LockJoysticks();
  575. for (item = SDL_joylist; item; item = item->next) {
  576. /* found it, remove it. */
  577. if (item->device_instance == device_instance) {
  578. RemoveJoylistItem(item, prev);
  579. break;
  580. }
  581. prev = item;
  582. }
  583. SDL_UnlockJoysticks();
  584. }
  585. static int StrHasPrefix(const char *string, const char *prefix)
  586. {
  587. return SDL_strncmp(string, prefix, SDL_strlen(prefix)) == 0;
  588. }
  589. static int StrIsInteger(const char *string)
  590. {
  591. const char *p;
  592. if (*string == '\0') {
  593. return 0;
  594. }
  595. for (p = string; *p != '\0'; p++) {
  596. if (*p < '0' || *p > '9') {
  597. return 0;
  598. }
  599. }
  600. return 1;
  601. }
  602. static SDL_bool IsJoystickJSNode(const char *node)
  603. {
  604. const char *last_slash = SDL_strrchr(node, '/');
  605. if (last_slash) {
  606. node = last_slash + 1;
  607. }
  608. return StrHasPrefix(node, "js") && StrIsInteger(node + 2);
  609. }
  610. static SDL_bool IsJoystickEventNode(const char *node)
  611. {
  612. const char *last_slash = SDL_strrchr(node, '/');
  613. if (last_slash) {
  614. node = last_slash + 1;
  615. }
  616. return StrHasPrefix(node, "event") && StrIsInteger(node + 5);
  617. }
  618. static SDL_bool IsJoystickDeviceNode(const char *node)
  619. {
  620. if (SDL_classic_joysticks) {
  621. return IsJoystickJSNode(node);
  622. } else {
  623. return IsJoystickEventNode(node);
  624. }
  625. }
  626. #ifdef HAVE_INOTIFY
  627. #ifdef HAVE_INOTIFY_INIT1
  628. static int SDL_inotify_init1(void)
  629. {
  630. return inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
  631. }
  632. #else
  633. static int SDL_inotify_init1(void)
  634. {
  635. int fd = inotify_init();
  636. if (fd < 0) {
  637. return -1;
  638. }
  639. fcntl(fd, F_SETFL, O_NONBLOCK);
  640. fcntl(fd, F_SETFD, FD_CLOEXEC);
  641. return fd;
  642. }
  643. #endif
  644. static void LINUX_InotifyJoystickDetect(void)
  645. {
  646. union
  647. {
  648. struct inotify_event event;
  649. char storage[4096];
  650. char enough_for_inotify[sizeof(struct inotify_event) + NAME_MAX + 1];
  651. } buf;
  652. ssize_t bytes;
  653. size_t remain = 0;
  654. size_t len;
  655. char path[PATH_MAX];
  656. bytes = read(inotify_fd, &buf, sizeof(buf));
  657. if (bytes > 0) {
  658. remain = (size_t)bytes;
  659. }
  660. while (remain > 0) {
  661. if (buf.event.len > 0) {
  662. if (IsJoystickDeviceNode(buf.event.name)) {
  663. (void)SDL_snprintf(path, SDL_arraysize(path), "/dev/input/%s", buf.event.name);
  664. if (buf.event.mask & (IN_CREATE | IN_MOVED_TO | IN_ATTRIB)) {
  665. MaybeAddDevice(path);
  666. } else if (buf.event.mask & (IN_DELETE | IN_MOVED_FROM)) {
  667. MaybeRemoveDevice(path);
  668. }
  669. }
  670. }
  671. len = sizeof(struct inotify_event) + buf.event.len;
  672. remain -= len;
  673. if (remain != 0) {
  674. SDL_memmove(&buf.storage[0], &buf.storage[len], remain);
  675. }
  676. }
  677. }
  678. #endif /* HAVE_INOTIFY */
  679. static int get_event_joystick_index(int event)
  680. {
  681. int joystick_index = -1;
  682. int i, count;
  683. struct dirent **entries = NULL;
  684. char path[PATH_MAX];
  685. (void)SDL_snprintf(path, SDL_arraysize(path), "/sys/class/input/event%d/device", event);
  686. count = scandir(path, &entries, NULL, alphasort);
  687. for (i = 0; i < count; ++i) {
  688. if (SDL_strncmp(entries[i]->d_name, "js", 2) == 0) {
  689. joystick_index = SDL_atoi(entries[i]->d_name + 2);
  690. }
  691. free(entries[i]); /* This should NOT be SDL_free() */
  692. }
  693. free(entries); /* This should NOT be SDL_free() */
  694. return joystick_index;
  695. }
  696. /* Detect devices by reading /dev/input. In the inotify code path we
  697. * have to do this the first time, to detect devices that already existed
  698. * before we started; in the non-inotify code path we do this repeatedly
  699. * (polling). */
  700. static int filter_entries(const struct dirent *entry)
  701. {
  702. return IsJoystickDeviceNode(entry->d_name);
  703. }
  704. static int SDLCALL sort_entries(const void *_a, const void *_b)
  705. {
  706. const struct dirent **a = (const struct dirent **)_a;
  707. const struct dirent **b = (const struct dirent **)_b;
  708. int numA, numB;
  709. int offset;
  710. if (SDL_classic_joysticks) {
  711. offset = 2; /* strlen("js") */
  712. numA = SDL_atoi((*a)->d_name + offset);
  713. numB = SDL_atoi((*b)->d_name + offset);
  714. } else {
  715. offset = 5; /* strlen("event") */
  716. numA = SDL_atoi((*a)->d_name + offset);
  717. numB = SDL_atoi((*b)->d_name + offset);
  718. /* See if we can get the joystick ordering */
  719. {
  720. int jsA = get_event_joystick_index(numA);
  721. int jsB = get_event_joystick_index(numB);
  722. if (jsA >= 0 && jsB >= 0) {
  723. numA = jsA;
  724. numB = jsB;
  725. } else if (jsA >= 0) {
  726. return -1;
  727. } else if (jsB >= 0) {
  728. return 1;
  729. }
  730. }
  731. }
  732. return numA - numB;
  733. }
  734. typedef struct
  735. {
  736. char *path;
  737. int slot;
  738. } VirtualGamepadEntry;
  739. static int SDLCALL sort_virtual_gamepads(const void *_a, const void *_b)
  740. {
  741. const VirtualGamepadEntry *a = (const VirtualGamepadEntry *)_a;
  742. const VirtualGamepadEntry *b = (const VirtualGamepadEntry *)_b;
  743. return a->slot - b->slot;
  744. }
  745. static void LINUX_ScanSteamVirtualGamepads(void)
  746. {
  747. int i, count;
  748. int fd;
  749. struct dirent **entries = NULL;
  750. char path[PATH_MAX];
  751. struct input_id inpid;
  752. int num_virtual_gamepads = 0;
  753. int virtual_gamepad_slot;
  754. VirtualGamepadEntry *virtual_gamepads = NULL;
  755. count = scandir("/dev/input", &entries, filter_entries, NULL);
  756. for (i = 0; i < count; ++i) {
  757. (void)SDL_snprintf(path, SDL_arraysize(path), "/dev/input/%s", entries[i]->d_name);
  758. fd = open(path, O_RDONLY | O_CLOEXEC, 0);
  759. if (fd >= 0) {
  760. if (ioctl(fd, EVIOCGID, &inpid) == 0 &&
  761. inpid.vendor == USB_VENDOR_VALVE &&
  762. inpid.product == USB_PRODUCT_STEAM_VIRTUAL_GAMEPAD &&
  763. GetSteamVirtualGamepadSlot(fd, &virtual_gamepad_slot)) {
  764. VirtualGamepadEntry *new_virtual_gamepads = (VirtualGamepadEntry *)SDL_realloc(virtual_gamepads, (num_virtual_gamepads + 1) * sizeof(*virtual_gamepads));
  765. if (new_virtual_gamepads) {
  766. VirtualGamepadEntry *entry = &new_virtual_gamepads[num_virtual_gamepads];
  767. entry->path = SDL_strdup(path);
  768. entry->slot = virtual_gamepad_slot;
  769. if (entry->path) {
  770. virtual_gamepads = new_virtual_gamepads;
  771. ++num_virtual_gamepads;
  772. } else {
  773. SDL_free(entry->path);
  774. SDL_free(new_virtual_gamepads);
  775. }
  776. }
  777. }
  778. close(fd);
  779. }
  780. free(entries[i]); /* This should NOT be SDL_free() */
  781. }
  782. free(entries); /* This should NOT be SDL_free() */
  783. if (num_virtual_gamepads > 1) {
  784. SDL_qsort(virtual_gamepads, num_virtual_gamepads, sizeof(*virtual_gamepads), sort_virtual_gamepads);
  785. }
  786. for (i = 0; i < num_virtual_gamepads; ++i) {
  787. MaybeAddDevice(virtual_gamepads[i].path);
  788. SDL_free(virtual_gamepads[i].path);
  789. }
  790. SDL_free(virtual_gamepads);
  791. }
  792. static void LINUX_ScanInputDevices(void)
  793. {
  794. int i, count;
  795. struct dirent **entries = NULL;
  796. char path[PATH_MAX];
  797. count = scandir("/dev/input", &entries, filter_entries, NULL);
  798. if (count > 1) {
  799. SDL_qsort(entries, count, sizeof(*entries), sort_entries);
  800. }
  801. for (i = 0; i < count; ++i) {
  802. (void)SDL_snprintf(path, SDL_arraysize(path), "/dev/input/%s", entries[i]->d_name);
  803. MaybeAddDevice(path);
  804. free(entries[i]); /* This should NOT be SDL_free() */
  805. }
  806. free(entries); /* This should NOT be SDL_free() */
  807. }
  808. static void LINUX_FallbackJoystickDetect(void)
  809. {
  810. const Uint32 SDL_JOY_DETECT_INTERVAL_MS = 3000; /* Update every 3 seconds */
  811. Uint64 now = SDL_GetTicks();
  812. if (!last_joy_detect_time || now >= (last_joy_detect_time + SDL_JOY_DETECT_INTERVAL_MS)) {
  813. struct stat sb;
  814. /* Opening input devices can generate synchronous device I/O, so avoid it if we can */
  815. if (stat("/dev/input", &sb) == 0 && sb.st_mtime != last_input_dir_mtime) {
  816. /* Look for Steam virtual gamepads first, and sort by Steam controller slot */
  817. LINUX_ScanSteamVirtualGamepads();
  818. LINUX_ScanInputDevices();
  819. last_input_dir_mtime = sb.st_mtime;
  820. }
  821. last_joy_detect_time = now;
  822. }
  823. }
  824. static void LINUX_JoystickDetect(void)
  825. {
  826. #ifdef SDL_USE_LIBUDEV
  827. if (enumeration_method == ENUMERATION_LIBUDEV) {
  828. SDL_UDEV_Poll();
  829. } else
  830. #endif
  831. #ifdef HAVE_INOTIFY
  832. if (inotify_fd >= 0 && last_joy_detect_time != 0) {
  833. LINUX_InotifyJoystickDetect();
  834. } else
  835. #endif
  836. {
  837. LINUX_FallbackJoystickDetect();
  838. }
  839. HandlePendingRemovals();
  840. SDL_UpdateSteamControllers();
  841. }
  842. static int LINUX_JoystickInit(void)
  843. {
  844. const char *devices = SDL_GetHint(SDL_HINT_JOYSTICK_DEVICE);
  845. SDL_classic_joysticks = SDL_GetHintBoolean(SDL_HINT_LINUX_JOYSTICK_CLASSIC, SDL_FALSE);
  846. enumeration_method = ENUMERATION_UNSET;
  847. /* First see if the user specified one or more joysticks to use */
  848. if (devices) {
  849. char *envcopy, *envpath, *delim;
  850. envcopy = SDL_strdup(devices);
  851. envpath = envcopy;
  852. while (envpath) {
  853. delim = SDL_strchr(envpath, ':');
  854. if (delim) {
  855. *delim++ = '\0';
  856. }
  857. MaybeAddDevice(envpath);
  858. envpath = delim;
  859. }
  860. SDL_free(envcopy);
  861. }
  862. SDL_InitSteamControllers(SteamControllerConnectedCallback,
  863. SteamControllerDisconnectedCallback);
  864. /* Force immediate joystick detection if using fallback */
  865. last_joy_detect_time = 0;
  866. last_input_dir_mtime = 0;
  867. /* Manually scan first, since we sort by device number and udev doesn't */
  868. LINUX_JoystickDetect();
  869. #ifdef SDL_USE_LIBUDEV
  870. if (enumeration_method == ENUMERATION_UNSET) {
  871. if (SDL_GetHintBoolean("SDL_JOYSTICK_DISABLE_UDEV", SDL_FALSE)) {
  872. SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
  873. "udev disabled by SDL_JOYSTICK_DISABLE_UDEV");
  874. enumeration_method = ENUMERATION_FALLBACK;
  875. } else if (SDL_DetectSandbox() != SDL_SANDBOX_NONE) {
  876. SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
  877. "Container detected, disabling udev integration");
  878. enumeration_method = ENUMERATION_FALLBACK;
  879. } else {
  880. SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
  881. "Using udev for joystick device discovery");
  882. enumeration_method = ENUMERATION_LIBUDEV;
  883. }
  884. }
  885. if (enumeration_method == ENUMERATION_LIBUDEV) {
  886. if (SDL_UDEV_Init() < 0) {
  887. return SDL_SetError("Could not initialize UDEV");
  888. }
  889. /* Set up the udev callback */
  890. if (SDL_UDEV_AddCallback(joystick_udev_callback) < 0) {
  891. SDL_UDEV_Quit();
  892. return SDL_SetError("Could not set up joystick <-> udev callback");
  893. }
  894. /* Force a scan to build the initial device list */
  895. SDL_UDEV_Scan();
  896. } else
  897. #endif
  898. {
  899. #ifdef HAVE_INOTIFY
  900. inotify_fd = SDL_inotify_init1();
  901. if (inotify_fd < 0) {
  902. SDL_LogWarn(SDL_LOG_CATEGORY_INPUT,
  903. "Unable to initialize inotify, falling back to polling: %s",
  904. strerror(errno));
  905. } else {
  906. /* We need to watch for attribute changes in addition to
  907. * creation, because when a device is first created, it has
  908. * permissions that we can't read. When udev chmods it to
  909. * something that we maybe *can* read, we'll get an
  910. * IN_ATTRIB event to tell us. */
  911. if (inotify_add_watch(inotify_fd, "/dev/input",
  912. IN_CREATE | IN_DELETE | IN_MOVE | IN_ATTRIB) < 0) {
  913. close(inotify_fd);
  914. inotify_fd = -1;
  915. SDL_LogWarn(SDL_LOG_CATEGORY_INPUT,
  916. "Unable to add inotify watch, falling back to polling: %s",
  917. strerror(errno));
  918. }
  919. }
  920. #endif /* HAVE_INOTIFY */
  921. }
  922. return 0;
  923. }
  924. static int LINUX_JoystickGetCount(void)
  925. {
  926. SDL_AssertJoysticksLocked();
  927. return numjoysticks;
  928. }
  929. static SDL_joylist_item *GetJoystickByDevIndex(int device_index)
  930. {
  931. SDL_joylist_item *item;
  932. SDL_AssertJoysticksLocked();
  933. if ((device_index < 0) || (device_index >= numjoysticks)) {
  934. return NULL;
  935. }
  936. item = SDL_joylist;
  937. while (device_index > 0) {
  938. SDL_assert(item != NULL);
  939. device_index--;
  940. item = item->next;
  941. }
  942. return item;
  943. }
  944. static const char *LINUX_JoystickGetDeviceName(int device_index)
  945. {
  946. return GetJoystickByDevIndex(device_index)->name;
  947. }
  948. static const char *LINUX_JoystickGetDevicePath(int device_index)
  949. {
  950. return GetJoystickByDevIndex(device_index)->path;
  951. }
  952. static int LINUX_JoystickGetDeviceSteamVirtualGamepadSlot(int device_index)
  953. {
  954. return GetJoystickByDevIndex(device_index)->steam_virtual_gamepad_slot;
  955. }
  956. static int LINUX_JoystickGetDevicePlayerIndex(int device_index)
  957. {
  958. return -1;
  959. }
  960. static void LINUX_JoystickSetDevicePlayerIndex(int device_index, int player_index)
  961. {
  962. }
  963. static SDL_JoystickGUID LINUX_JoystickGetDeviceGUID(int device_index)
  964. {
  965. return GetJoystickByDevIndex(device_index)->guid;
  966. }
  967. /* Function to perform the mapping from device index to the instance id for this index */
  968. static SDL_JoystickID LINUX_JoystickGetDeviceInstanceID(int device_index)
  969. {
  970. return GetJoystickByDevIndex(device_index)->device_instance;
  971. }
  972. static int allocate_hatdata(SDL_Joystick *joystick)
  973. {
  974. int i;
  975. SDL_AssertJoysticksLocked();
  976. joystick->hwdata->hats =
  977. (struct hwdata_hat *)SDL_malloc(joystick->nhats *
  978. sizeof(struct hwdata_hat));
  979. if (!joystick->hwdata->hats) {
  980. return -1;
  981. }
  982. for (i = 0; i < joystick->nhats; ++i) {
  983. joystick->hwdata->hats[i].axis[0] = 1;
  984. joystick->hwdata->hats[i].axis[1] = 1;
  985. }
  986. return 0;
  987. }
  988. static SDL_bool GuessIfAxesAreDigitalHat(struct input_absinfo *absinfo_x, struct input_absinfo *absinfo_y)
  989. {
  990. /* A "hat" is assumed to be a digital input with at most 9 possible states
  991. * (3 per axis: negative/zero/positive), as opposed to a true "axis" which
  992. * can report a continuous range of possible values. Unfortunately the Linux
  993. * joystick interface makes no distinction between digital hat axes and any
  994. * other continuous analog axis, so we have to guess. */
  995. /* If both axes are missing, they're not anything. */
  996. if (!absinfo_x && !absinfo_y) {
  997. return SDL_FALSE;
  998. }
  999. /* If the hint says so, treat all hats as digital. */
  1000. if (SDL_GetHintBoolean(SDL_HINT_LINUX_DIGITAL_HATS, SDL_FALSE)) {
  1001. return SDL_TRUE;
  1002. }
  1003. /* If both axes have ranges constrained between -1 and 1, they're definitely digital. */
  1004. if ((!absinfo_x || (absinfo_x->minimum == -1 && absinfo_x->maximum == 1)) && (!absinfo_y || (absinfo_y->minimum == -1 && absinfo_y->maximum == 1))) {
  1005. return SDL_TRUE;
  1006. }
  1007. /* If both axes lack fuzz, flat, and resolution values, they're probably digital. */
  1008. if ((!absinfo_x || (!absinfo_x->fuzz && !absinfo_x->flat && !absinfo_x->resolution)) && (!absinfo_y || (!absinfo_y->fuzz && !absinfo_y->flat && !absinfo_y->resolution))) {
  1009. return SDL_TRUE;
  1010. }
  1011. /* Otherwise, treat them as analog. */
  1012. return SDL_FALSE;
  1013. }
  1014. static void ConfigJoystick(SDL_Joystick *joystick, int fd, int fd_sensor)
  1015. {
  1016. int i, t;
  1017. unsigned long keybit[NBITS(KEY_MAX)] = { 0 };
  1018. unsigned long absbit[NBITS(ABS_MAX)] = { 0 };
  1019. unsigned long relbit[NBITS(REL_MAX)] = { 0 };
  1020. unsigned long ffbit[NBITS(FF_MAX)] = { 0 };
  1021. Uint8 key_pam_size, abs_pam_size;
  1022. SDL_bool use_deadzones = SDL_GetHintBoolean(SDL_HINT_LINUX_JOYSTICK_DEADZONES, SDL_FALSE);
  1023. SDL_bool use_hat_deadzones = SDL_GetHintBoolean(SDL_HINT_LINUX_HAT_DEADZONES, SDL_TRUE);
  1024. SDL_AssertJoysticksLocked();
  1025. /* See if this device uses the new unified event API */
  1026. if ((ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) >= 0) &&
  1027. (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) >= 0) &&
  1028. (ioctl(fd, EVIOCGBIT(EV_REL, sizeof(relbit)), relbit) >= 0)) {
  1029. /* Get the number of buttons, axes, and other thingamajigs */
  1030. for (i = BTN_JOYSTICK; i < KEY_MAX; ++i) {
  1031. if (test_bit(i, keybit)) {
  1032. #ifdef DEBUG_INPUT_EVENTS
  1033. SDL_Log("Joystick has button: 0x%x\n", i);
  1034. #endif
  1035. joystick->hwdata->key_map[i] = joystick->nbuttons;
  1036. joystick->hwdata->has_key[i] = SDL_TRUE;
  1037. ++joystick->nbuttons;
  1038. }
  1039. }
  1040. for (i = 0; i < BTN_JOYSTICK; ++i) {
  1041. if (test_bit(i, keybit)) {
  1042. #ifdef DEBUG_INPUT_EVENTS
  1043. SDL_Log("Joystick has button: 0x%x\n", i);
  1044. #endif
  1045. joystick->hwdata->key_map[i] = joystick->nbuttons;
  1046. joystick->hwdata->has_key[i] = SDL_TRUE;
  1047. ++joystick->nbuttons;
  1048. }
  1049. }
  1050. for (i = ABS_HAT0X; i <= ABS_HAT3Y; i += 2) {
  1051. int hat_x = -1;
  1052. int hat_y = -1;
  1053. struct input_absinfo absinfo_x;
  1054. struct input_absinfo absinfo_y;
  1055. if (test_bit(i, absbit)) {
  1056. hat_x = ioctl(fd, EVIOCGABS(i), &absinfo_x);
  1057. }
  1058. if (test_bit(i + 1, absbit)) {
  1059. hat_y = ioctl(fd, EVIOCGABS(i + 1), &absinfo_y);
  1060. }
  1061. if (GuessIfAxesAreDigitalHat((hat_x < 0 ? (void *)0 : &absinfo_x),
  1062. (hat_y < 0 ? (void *)0 : &absinfo_y))) {
  1063. const int hat_index = (i - ABS_HAT0X) / 2;
  1064. struct hat_axis_correct *correct = &joystick->hwdata->hat_correct[hat_index];
  1065. #ifdef DEBUG_INPUT_EVENTS
  1066. SDL_Log("Joystick has digital hat: #%d\n", hat_index);
  1067. if (hat_x >= 0) {
  1068. SDL_Log("X Values = { val:%d, min:%d, max:%d, fuzz:%d, flat:%d, res:%d }\n",
  1069. absinfo_x.value, absinfo_x.minimum, absinfo_x.maximum,
  1070. absinfo_x.fuzz, absinfo_x.flat, absinfo_x.resolution);
  1071. }
  1072. if (hat_y >= 0) {
  1073. SDL_Log("Y Values = { val:%d, min:%d, max:%d, fuzz:%d, flat:%d, res:%d }\n",
  1074. absinfo_y.value, absinfo_y.minimum, absinfo_y.maximum,
  1075. absinfo_y.fuzz, absinfo_y.flat, absinfo_y.resolution);
  1076. }
  1077. #endif /* DEBUG_INPUT_EVENTS */
  1078. joystick->hwdata->hats_indices[hat_index] = joystick->nhats;
  1079. joystick->hwdata->has_hat[hat_index] = SDL_TRUE;
  1080. correct->use_deadzones = use_hat_deadzones;
  1081. correct->minimum[0] = (hat_x < 0) ? -1 : absinfo_x.minimum;
  1082. correct->maximum[0] = (hat_x < 0) ? 1 : absinfo_x.maximum;
  1083. correct->minimum[1] = (hat_y < 0) ? -1 : absinfo_y.minimum;
  1084. correct->maximum[1] = (hat_y < 0) ? 1 : absinfo_y.maximum;
  1085. ++joystick->nhats;
  1086. }
  1087. }
  1088. for (i = 0; i < ABS_MAX; ++i) {
  1089. /* Skip digital hats */
  1090. if (i >= ABS_HAT0X && i <= ABS_HAT3Y && joystick->hwdata->has_hat[(i - ABS_HAT0X) / 2]) {
  1091. continue;
  1092. }
  1093. if (test_bit(i, absbit)) {
  1094. struct input_absinfo absinfo;
  1095. struct axis_correct *correct = &joystick->hwdata->abs_correct[i];
  1096. if (ioctl(fd, EVIOCGABS(i), &absinfo) < 0) {
  1097. continue;
  1098. }
  1099. #ifdef DEBUG_INPUT_EVENTS
  1100. SDL_Log("Joystick has absolute axis: 0x%.2x\n", i);
  1101. SDL_Log("Values = { val:%d, min:%d, max:%d, fuzz:%d, flat:%d, res:%d }\n",
  1102. absinfo.value, absinfo.minimum, absinfo.maximum,
  1103. absinfo.fuzz, absinfo.flat, absinfo.resolution);
  1104. #endif /* DEBUG_INPUT_EVENTS */
  1105. joystick->hwdata->abs_map[i] = joystick->naxes;
  1106. joystick->hwdata->has_abs[i] = SDL_TRUE;
  1107. correct->minimum = absinfo.minimum;
  1108. correct->maximum = absinfo.maximum;
  1109. if (correct->minimum != correct->maximum) {
  1110. if (use_deadzones) {
  1111. correct->use_deadzones = SDL_TRUE;
  1112. correct->coef[0] = (absinfo.maximum + absinfo.minimum) - 2 * absinfo.flat;
  1113. correct->coef[1] = (absinfo.maximum + absinfo.minimum) + 2 * absinfo.flat;
  1114. t = ((absinfo.maximum - absinfo.minimum) - 4 * absinfo.flat);
  1115. if (t != 0) {
  1116. correct->coef[2] = (1 << 28) / t;
  1117. } else {
  1118. correct->coef[2] = 0;
  1119. }
  1120. } else {
  1121. float value_range = (correct->maximum - correct->minimum);
  1122. float output_range = (SDL_JOYSTICK_AXIS_MAX - SDL_JOYSTICK_AXIS_MIN);
  1123. correct->scale = (output_range / value_range);
  1124. }
  1125. }
  1126. ++joystick->naxes;
  1127. }
  1128. }
  1129. } else if ((ioctl(fd, JSIOCGBUTTONS, &key_pam_size, sizeof(key_pam_size)) >= 0) &&
  1130. (ioctl(fd, JSIOCGAXES, &abs_pam_size, sizeof(abs_pam_size)) >= 0)) {
  1131. size_t len;
  1132. joystick->hwdata->classic = SDL_TRUE;
  1133. len = (KEY_MAX - BTN_MISC + 1) * sizeof(*joystick->hwdata->key_pam);
  1134. joystick->hwdata->key_pam = (Uint16 *)SDL_calloc(1, len);
  1135. if (joystick->hwdata->key_pam) {
  1136. if (ioctl(fd, JSIOCGBTNMAP, joystick->hwdata->key_pam, len) < 0) {
  1137. SDL_free(joystick->hwdata->key_pam);
  1138. joystick->hwdata->key_pam = NULL;
  1139. key_pam_size = 0;
  1140. }
  1141. } else {
  1142. key_pam_size = 0;
  1143. }
  1144. for (i = 0; i < key_pam_size; ++i) {
  1145. Uint16 code = joystick->hwdata->key_pam[i];
  1146. #ifdef DEBUG_INPUT_EVENTS
  1147. SDL_Log("Joystick has button: 0x%x\n", code);
  1148. #endif
  1149. joystick->hwdata->key_map[code] = joystick->nbuttons;
  1150. joystick->hwdata->has_key[code] = SDL_TRUE;
  1151. ++joystick->nbuttons;
  1152. }
  1153. len = ABS_CNT * sizeof(*joystick->hwdata->abs_pam);
  1154. joystick->hwdata->abs_pam = (Uint8 *)SDL_calloc(1, len);
  1155. if (joystick->hwdata->abs_pam) {
  1156. if (ioctl(fd, JSIOCGAXMAP, joystick->hwdata->abs_pam, len) < 0) {
  1157. SDL_free(joystick->hwdata->abs_pam);
  1158. joystick->hwdata->abs_pam = NULL;
  1159. abs_pam_size = 0;
  1160. }
  1161. } else {
  1162. abs_pam_size = 0;
  1163. }
  1164. for (i = 0; i < abs_pam_size; ++i) {
  1165. Uint8 code = joystick->hwdata->abs_pam[i];
  1166. // TODO: is there any way to detect analog hats in advance via this API?
  1167. if (code >= ABS_HAT0X && code <= ABS_HAT3Y) {
  1168. int hat_index = (code - ABS_HAT0X) / 2;
  1169. if (!joystick->hwdata->has_hat[hat_index]) {
  1170. #ifdef DEBUG_INPUT_EVENTS
  1171. SDL_Log("Joystick has digital hat: #%d\n", hat_index);
  1172. #endif
  1173. joystick->hwdata->hats_indices[hat_index] = joystick->nhats++;
  1174. joystick->hwdata->has_hat[hat_index] = SDL_TRUE;
  1175. joystick->hwdata->hat_correct[hat_index].minimum[0] = -1;
  1176. joystick->hwdata->hat_correct[hat_index].maximum[0] = 1;
  1177. joystick->hwdata->hat_correct[hat_index].minimum[1] = -1;
  1178. joystick->hwdata->hat_correct[hat_index].maximum[1] = 1;
  1179. }
  1180. } else {
  1181. #ifdef DEBUG_INPUT_EVENTS
  1182. SDL_Log("Joystick has absolute axis: 0x%.2x\n", code);
  1183. #endif
  1184. joystick->hwdata->abs_map[code] = joystick->naxes;
  1185. joystick->hwdata->has_abs[code] = SDL_TRUE;
  1186. ++joystick->naxes;
  1187. }
  1188. }
  1189. }
  1190. /* Sensors are only available through the new unified event API */
  1191. if (fd_sensor >= 0 && (ioctl(fd_sensor, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) >= 0)) {
  1192. if (test_bit(ABS_X, absbit) && test_bit(ABS_Y, absbit) && test_bit(ABS_Z, absbit)) {
  1193. joystick->hwdata->has_accelerometer = SDL_TRUE;
  1194. for (i = 0; i < 3; ++i) {
  1195. struct input_absinfo absinfo;
  1196. if (ioctl(fd_sensor, EVIOCGABS(ABS_X + i), &absinfo) < 0) {
  1197. joystick->hwdata->has_accelerometer = SDL_FALSE;
  1198. break; /* do not report an accelerometer if we can't read all axes */
  1199. }
  1200. joystick->hwdata->accelerometer_scale[i] = absinfo.resolution;
  1201. #ifdef DEBUG_INPUT_EVENTS
  1202. SDL_Log("Joystick has accelerometer axis: 0x%.2x\n", ABS_X + i);
  1203. SDL_Log("Values = { val:%d, min:%d, max:%d, fuzz:%d, flat:%d, res:%d }\n",
  1204. absinfo.value, absinfo.minimum, absinfo.maximum,
  1205. absinfo.fuzz, absinfo.flat, absinfo.resolution);
  1206. #endif /* DEBUG_INPUT_EVENTS */
  1207. }
  1208. }
  1209. if (test_bit(ABS_RX, absbit) && test_bit(ABS_RY, absbit) && test_bit(ABS_RZ, absbit)) {
  1210. joystick->hwdata->has_gyro = SDL_TRUE;
  1211. for (i = 0; i < 3; ++i) {
  1212. struct input_absinfo absinfo;
  1213. if (ioctl(fd_sensor, EVIOCGABS(ABS_RX + i), &absinfo) < 0) {
  1214. joystick->hwdata->has_gyro = SDL_FALSE;
  1215. break; /* do not report a gyro if we can't read all axes */
  1216. }
  1217. joystick->hwdata->gyro_scale[i] = absinfo.resolution;
  1218. #ifdef DEBUG_INPUT_EVENTS
  1219. SDL_Log("Joystick has gyro axis: 0x%.2x\n", ABS_RX + i);
  1220. SDL_Log("Values = { val:%d, min:%d, max:%d, fuzz:%d, flat:%d, res:%d }\n",
  1221. absinfo.value, absinfo.minimum, absinfo.maximum,
  1222. absinfo.fuzz, absinfo.flat, absinfo.resolution);
  1223. #endif /* DEBUG_INPUT_EVENTS */
  1224. }
  1225. }
  1226. }
  1227. /* Allocate data to keep track of these thingamajigs */
  1228. if (joystick->nhats > 0) {
  1229. if (allocate_hatdata(joystick) < 0) {
  1230. joystick->nhats = 0;
  1231. }
  1232. }
  1233. if (ioctl(fd, EVIOCGBIT(EV_FF, sizeof(ffbit)), ffbit) >= 0) {
  1234. if (test_bit(FF_RUMBLE, ffbit)) {
  1235. joystick->hwdata->ff_rumble = SDL_TRUE;
  1236. }
  1237. if (test_bit(FF_SINE, ffbit)) {
  1238. joystick->hwdata->ff_sine = SDL_TRUE;
  1239. }
  1240. }
  1241. }
  1242. /* This is used to do the heavy lifting for LINUX_JoystickOpen and
  1243. also LINUX_JoystickGetGamepadMapping, so we can query the hardware
  1244. without adding an opened SDL_Joystick object to the system.
  1245. This expects `joystick->hwdata` to be allocated and will not free it
  1246. on error. Returns -1 on error, 0 on success. */
  1247. static int PrepareJoystickHwdata(SDL_Joystick *joystick, SDL_joylist_item *item, SDL_sensorlist_item *item_sensor)
  1248. {
  1249. SDL_AssertJoysticksLocked();
  1250. joystick->hwdata->item = item;
  1251. joystick->hwdata->item_sensor = item_sensor;
  1252. joystick->hwdata->guid = item->guid;
  1253. joystick->hwdata->effect.id = -1;
  1254. joystick->hwdata->m_bSteamController = item->m_bSteamController;
  1255. SDL_memset(joystick->hwdata->key_map, 0xFF, sizeof(joystick->hwdata->key_map));
  1256. SDL_memset(joystick->hwdata->abs_map, 0xFF, sizeof(joystick->hwdata->abs_map));
  1257. if (item->m_bSteamController) {
  1258. joystick->hwdata->fd = -1;
  1259. joystick->hwdata->fd_sensor = -1;
  1260. SDL_GetSteamControllerInputs(&joystick->nbuttons,
  1261. &joystick->naxes,
  1262. &joystick->nhats);
  1263. } else {
  1264. int fd = -1, fd_sensor = -1;
  1265. /* Try read-write first, so we can do rumble */
  1266. fd = open(item->path, O_RDWR | O_CLOEXEC, 0);
  1267. if (fd < 0) {
  1268. /* Try read-only again, at least we'll get events in this case */
  1269. fd = open(item->path, O_RDONLY | O_CLOEXEC, 0);
  1270. }
  1271. if (fd < 0) {
  1272. return SDL_SetError("Unable to open %s", item->path);
  1273. }
  1274. /* If opening sensor fail, continue with buttons and axes only */
  1275. if (item_sensor) {
  1276. fd_sensor = open(item_sensor->path, O_RDONLY | O_CLOEXEC, 0);
  1277. }
  1278. joystick->hwdata->fd = fd;
  1279. joystick->hwdata->fd_sensor = fd_sensor;
  1280. joystick->hwdata->fname = SDL_strdup(item->path);
  1281. if (!joystick->hwdata->fname) {
  1282. close(fd);
  1283. if (fd_sensor >= 0) {
  1284. close(fd_sensor);
  1285. }
  1286. return -1;
  1287. }
  1288. /* Set the joystick to non-blocking read mode */
  1289. fcntl(fd, F_SETFL, O_NONBLOCK);
  1290. if (fd_sensor >= 0) {
  1291. fcntl(fd_sensor, F_SETFL, O_NONBLOCK);
  1292. }
  1293. /* Get the number of buttons and axes on the joystick */
  1294. ConfigJoystick(joystick, fd, fd_sensor);
  1295. }
  1296. return 0;
  1297. }
  1298. static SDL_sensorlist_item *GetSensor(SDL_joylist_item *item)
  1299. {
  1300. SDL_sensorlist_item *item_sensor;
  1301. char uniq_item[128];
  1302. int fd_item = -1;
  1303. SDL_AssertJoysticksLocked();
  1304. if (!item || !SDL_sensorlist) {
  1305. return NULL;
  1306. }
  1307. SDL_memset(uniq_item, 0, sizeof(uniq_item));
  1308. fd_item = open(item->path, O_RDONLY | O_CLOEXEC, 0);
  1309. if (fd_item < 0) {
  1310. return NULL;
  1311. }
  1312. if (ioctl(fd_item, EVIOCGUNIQ(sizeof(uniq_item) - 1), &uniq_item) < 0) {
  1313. return NULL;
  1314. }
  1315. close(fd_item);
  1316. #ifdef DEBUG_INPUT_EVENTS
  1317. SDL_Log("Joystick UNIQ: %s\n", uniq_item);
  1318. #endif /* DEBUG_INPUT_EVENTS */
  1319. for (item_sensor = SDL_sensorlist; item_sensor; item_sensor = item_sensor->next) {
  1320. char uniq_sensor[128];
  1321. int fd_sensor = -1;
  1322. if (item_sensor->hwdata) {
  1323. /* already associated with another joystick */
  1324. continue;
  1325. }
  1326. SDL_memset(uniq_sensor, 0, sizeof(uniq_sensor));
  1327. fd_sensor = open(item_sensor->path, O_RDONLY | O_CLOEXEC, 0);
  1328. if (fd_sensor < 0) {
  1329. continue;
  1330. }
  1331. if (ioctl(fd_sensor, EVIOCGUNIQ(sizeof(uniq_sensor) - 1), &uniq_sensor) < 0) {
  1332. close(fd_sensor);
  1333. continue;
  1334. }
  1335. close(fd_sensor);
  1336. #ifdef DEBUG_INPUT_EVENTS
  1337. SDL_Log("Sensor UNIQ: %s\n", uniq_sensor);
  1338. #endif /* DEBUG_INPUT_EVENTS */
  1339. if (SDL_strcmp(uniq_item, uniq_sensor) == 0) {
  1340. return item_sensor;
  1341. }
  1342. }
  1343. return NULL;
  1344. }
  1345. /* Function to open a joystick for use.
  1346. The joystick to open is specified by the device index.
  1347. This should fill the nbuttons and naxes fields of the joystick structure.
  1348. It returns 0, or -1 if there is an error.
  1349. */
  1350. static int LINUX_JoystickOpen(SDL_Joystick *joystick, int device_index)
  1351. {
  1352. SDL_joylist_item *item;
  1353. SDL_sensorlist_item *item_sensor;
  1354. SDL_AssertJoysticksLocked();
  1355. item = GetJoystickByDevIndex(device_index);
  1356. if (!item) {
  1357. return SDL_SetError("No such device");
  1358. }
  1359. joystick->instance_id = item->device_instance;
  1360. joystick->hwdata = (struct joystick_hwdata *)
  1361. SDL_calloc(1, sizeof(*joystick->hwdata));
  1362. if (!joystick->hwdata) {
  1363. return -1;
  1364. }
  1365. item_sensor = GetSensor(item);
  1366. if (PrepareJoystickHwdata(joystick, item, item_sensor) == -1) {
  1367. SDL_free(joystick->hwdata);
  1368. joystick->hwdata = NULL;
  1369. return -1; /* SDL_SetError will already have been called */
  1370. }
  1371. SDL_assert(item->hwdata == NULL);
  1372. SDL_assert(!item_sensor || item_sensor->hwdata == NULL);
  1373. item->hwdata = joystick->hwdata;
  1374. if (item_sensor) {
  1375. item_sensor->hwdata = joystick->hwdata;
  1376. }
  1377. /* mark joystick as fresh and ready */
  1378. joystick->hwdata->fresh = SDL_TRUE;
  1379. if (joystick->hwdata->has_gyro) {
  1380. SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_GYRO, 0.0f);
  1381. }
  1382. if (joystick->hwdata->has_accelerometer) {
  1383. SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_ACCEL, 0.0f);
  1384. }
  1385. if (joystick->hwdata->fd_sensor >= 0) {
  1386. /* Don't keep fd_sensor opened while sensor is disabled */
  1387. close(joystick->hwdata->fd_sensor);
  1388. joystick->hwdata->fd_sensor = -1;
  1389. }
  1390. return 0;
  1391. }
  1392. static int LINUX_JoystickRumble(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
  1393. {
  1394. struct input_event event;
  1395. SDL_AssertJoysticksLocked();
  1396. if (joystick->hwdata->ff_rumble) {
  1397. struct ff_effect *effect = &joystick->hwdata->effect;
  1398. effect->type = FF_RUMBLE;
  1399. effect->replay.length = SDL_MAX_RUMBLE_DURATION_MS;
  1400. effect->u.rumble.strong_magnitude = low_frequency_rumble;
  1401. effect->u.rumble.weak_magnitude = high_frequency_rumble;
  1402. } else if (joystick->hwdata->ff_sine) {
  1403. /* Scale and average the two rumble strengths */
  1404. Sint16 magnitude = (Sint16)(((low_frequency_rumble / 2) + (high_frequency_rumble / 2)) / 2);
  1405. struct ff_effect *effect = &joystick->hwdata->effect;
  1406. effect->type = FF_PERIODIC;
  1407. effect->replay.length = SDL_MAX_RUMBLE_DURATION_MS;
  1408. effect->u.periodic.waveform = FF_SINE;
  1409. effect->u.periodic.magnitude = magnitude;
  1410. } else {
  1411. return SDL_Unsupported();
  1412. }
  1413. if (ioctl(joystick->hwdata->fd, EVIOCSFF, &joystick->hwdata->effect) < 0) {
  1414. /* The kernel may have lost this effect, try to allocate a new one */
  1415. joystick->hwdata->effect.id = -1;
  1416. if (ioctl(joystick->hwdata->fd, EVIOCSFF, &joystick->hwdata->effect) < 0) {
  1417. return SDL_SetError("Couldn't update rumble effect: %s", strerror(errno));
  1418. }
  1419. }
  1420. event.type = EV_FF;
  1421. event.code = joystick->hwdata->effect.id;
  1422. event.value = 1;
  1423. if (write(joystick->hwdata->fd, &event, sizeof(event)) < 0) {
  1424. return SDL_SetError("Couldn't start rumble effect: %s", strerror(errno));
  1425. }
  1426. return 0;
  1427. }
  1428. static int LINUX_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
  1429. {
  1430. return SDL_Unsupported();
  1431. }
  1432. static Uint32 LINUX_JoystickGetCapabilities(SDL_Joystick *joystick)
  1433. {
  1434. Uint32 result = 0;
  1435. SDL_AssertJoysticksLocked();
  1436. if (joystick->hwdata->ff_rumble || joystick->hwdata->ff_sine) {
  1437. result |= SDL_JOYSTICK_CAP_RUMBLE;
  1438. }
  1439. return result;
  1440. }
  1441. static int LINUX_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
  1442. {
  1443. return SDL_Unsupported();
  1444. }
  1445. static int LINUX_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)
  1446. {
  1447. return SDL_Unsupported();
  1448. }
  1449. static int LINUX_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled)
  1450. {
  1451. SDL_AssertJoysticksLocked();
  1452. if (!joystick->hwdata->has_accelerometer && !joystick->hwdata->has_gyro) {
  1453. return SDL_Unsupported();
  1454. }
  1455. if (enabled == joystick->hwdata->report_sensor) {
  1456. return 0;
  1457. }
  1458. if (enabled) {
  1459. if (!joystick->hwdata->item_sensor) {
  1460. return SDL_SetError("Sensors unplugged.");
  1461. }
  1462. joystick->hwdata->fd_sensor = open(joystick->hwdata->item_sensor->path, O_RDONLY | O_CLOEXEC, 0);
  1463. if (joystick->hwdata->fd_sensor < 0) {
  1464. return SDL_SetError("Couldn't open sensor file %s.", joystick->hwdata->item_sensor->path);
  1465. }
  1466. fcntl(joystick->hwdata->fd_sensor, F_SETFL, O_NONBLOCK);
  1467. } else {
  1468. SDL_assert(joystick->hwdata->fd_sensor >= 0);
  1469. close(joystick->hwdata->fd_sensor);
  1470. joystick->hwdata->fd_sensor = -1;
  1471. }
  1472. joystick->hwdata->report_sensor = enabled;
  1473. return 0;
  1474. }
  1475. static void HandleHat(Uint64 timestamp, SDL_Joystick *stick, int hatidx, int axis, int value)
  1476. {
  1477. int hatnum;
  1478. struct hwdata_hat *the_hat;
  1479. struct hat_axis_correct *correct;
  1480. const Uint8 position_map[3][3] = {
  1481. { SDL_HAT_LEFTUP, SDL_HAT_UP, SDL_HAT_RIGHTUP },
  1482. { SDL_HAT_LEFT, SDL_HAT_CENTERED, SDL_HAT_RIGHT },
  1483. { SDL_HAT_LEFTDOWN, SDL_HAT_DOWN, SDL_HAT_RIGHTDOWN }
  1484. };
  1485. SDL_AssertJoysticksLocked();
  1486. hatnum = stick->hwdata->hats_indices[hatidx];
  1487. the_hat = &stick->hwdata->hats[hatnum];
  1488. correct = &stick->hwdata->hat_correct[hatidx];
  1489. /* Hopefully we detected any analog axes and left them as is rather than trying
  1490. * to use them as digital hats, but just in case, the deadzones here will
  1491. * prevent the slightest of twitches on an analog axis from registering as a hat
  1492. * movement. If the axes really are digital, this won't hurt since they should
  1493. * only ever be sending min, 0, or max anyway. */
  1494. if (value < 0) {
  1495. if (value <= correct->minimum[axis]) {
  1496. correct->minimum[axis] = value;
  1497. value = 0;
  1498. } else if (!correct->use_deadzones || value < correct->minimum[axis] / 3) {
  1499. value = 0;
  1500. } else {
  1501. value = 1;
  1502. }
  1503. } else if (value > 0) {
  1504. if (value >= correct->maximum[axis]) {
  1505. correct->maximum[axis] = value;
  1506. value = 2;
  1507. } else if (!correct->use_deadzones || value > correct->maximum[axis] / 3) {
  1508. value = 2;
  1509. } else {
  1510. value = 1;
  1511. }
  1512. } else { // value == 0
  1513. value = 1;
  1514. }
  1515. if (value != the_hat->axis[axis]) {
  1516. the_hat->axis[axis] = value;
  1517. SDL_SendJoystickHat(timestamp, stick, hatnum,
  1518. position_map[the_hat->axis[1]][the_hat->axis[0]]);
  1519. }
  1520. }
  1521. static int AxisCorrect(SDL_Joystick *joystick, int which, int value)
  1522. {
  1523. struct axis_correct *correct;
  1524. SDL_AssertJoysticksLocked();
  1525. correct = &joystick->hwdata->abs_correct[which];
  1526. if (correct->minimum != correct->maximum) {
  1527. if (correct->use_deadzones) {
  1528. value *= 2;
  1529. if (value > correct->coef[0]) {
  1530. if (value < correct->coef[1]) {
  1531. return 0;
  1532. }
  1533. value -= correct->coef[1];
  1534. } else {
  1535. value -= correct->coef[0];
  1536. }
  1537. value *= correct->coef[2];
  1538. value >>= 13;
  1539. } else {
  1540. value = (int)SDL_floorf((value - correct->minimum) * correct->scale + SDL_JOYSTICK_AXIS_MIN + 0.5f);
  1541. }
  1542. }
  1543. /* Clamp and return */
  1544. if (value < SDL_JOYSTICK_AXIS_MIN) {
  1545. return SDL_JOYSTICK_AXIS_MIN;
  1546. }
  1547. if (value > SDL_JOYSTICK_AXIS_MAX) {
  1548. return SDL_JOYSTICK_AXIS_MAX;
  1549. }
  1550. return value;
  1551. }
  1552. static void PollAllValues(Uint64 timestamp, SDL_Joystick *joystick)
  1553. {
  1554. struct input_absinfo absinfo;
  1555. unsigned long keyinfo[NBITS(KEY_MAX)];
  1556. int i;
  1557. SDL_AssertJoysticksLocked();
  1558. /* Poll all axis */
  1559. for (i = ABS_X; i < ABS_MAX; i++) {
  1560. /* We don't need to test for digital hats here, they won't have has_abs[] set */
  1561. if (joystick->hwdata->has_abs[i]) {
  1562. if (ioctl(joystick->hwdata->fd, EVIOCGABS(i), &absinfo) >= 0) {
  1563. absinfo.value = AxisCorrect(joystick, i, absinfo.value);
  1564. #ifdef DEBUG_INPUT_EVENTS
  1565. SDL_Log("Joystick : Re-read Axis %d (%d) val= %d\n",
  1566. joystick->hwdata->abs_map[i], i, absinfo.value);
  1567. #endif
  1568. SDL_SendJoystickAxis(timestamp, joystick,
  1569. joystick->hwdata->abs_map[i],
  1570. absinfo.value);
  1571. }
  1572. }
  1573. }
  1574. /* Poll all digital hats */
  1575. for (i = ABS_HAT0X; i <= ABS_HAT3Y; i++) {
  1576. const int baseaxis = i - ABS_HAT0X;
  1577. const int hatidx = baseaxis / 2;
  1578. SDL_assert(hatidx < SDL_arraysize(joystick->hwdata->has_hat));
  1579. /* We don't need to test for analog axes here, they won't have has_hat[] set */
  1580. if (joystick->hwdata->has_hat[hatidx]) {
  1581. if (ioctl(joystick->hwdata->fd, EVIOCGABS(i), &absinfo) >= 0) {
  1582. const int hataxis = baseaxis % 2;
  1583. HandleHat(timestamp, joystick, hatidx, hataxis, absinfo.value);
  1584. }
  1585. }
  1586. }
  1587. /* Poll all buttons */
  1588. SDL_zeroa(keyinfo);
  1589. if (ioctl(joystick->hwdata->fd, EVIOCGKEY(sizeof(keyinfo)), keyinfo) >= 0) {
  1590. for (i = 0; i < KEY_MAX; i++) {
  1591. if (joystick->hwdata->has_key[i]) {
  1592. const Uint8 value = test_bit(i, keyinfo) ? SDL_PRESSED : SDL_RELEASED;
  1593. #ifdef DEBUG_INPUT_EVENTS
  1594. SDL_Log("Joystick : Re-read Button %d (%d) val= %d\n",
  1595. joystick->hwdata->key_map[i], i, value);
  1596. #endif
  1597. SDL_SendJoystickButton(timestamp, joystick,
  1598. joystick->hwdata->key_map[i], value);
  1599. }
  1600. }
  1601. }
  1602. }
  1603. static void PollAllSensors(Uint64 timestamp, SDL_Joystick *joystick)
  1604. {
  1605. struct input_absinfo absinfo;
  1606. int i;
  1607. SDL_AssertJoysticksLocked();
  1608. SDL_assert(joystick->hwdata->fd_sensor >= 0);
  1609. if (joystick->hwdata->has_gyro) {
  1610. float data[3] = {0.0f, 0.0f, 0.0f};
  1611. for (i = 0; i < 3; i++) {
  1612. if (ioctl(joystick->hwdata->fd_sensor, EVIOCGABS(ABS_RX + i), &absinfo) >= 0) {
  1613. data[i] = absinfo.value * (SDL_PI_F / 180.f) / joystick->hwdata->gyro_scale[i];
  1614. #ifdef DEBUG_INPUT_EVENTS
  1615. SDL_Log("Joystick : Re-read Gyro (axis %d) val= %f\n", i, data[i]);
  1616. #endif
  1617. }
  1618. }
  1619. SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_GYRO, SDL_US_TO_NS(joystick->hwdata->sensor_tick), data, 3);
  1620. }
  1621. if (joystick->hwdata->has_accelerometer) {
  1622. float data[3] = {0.0f, 0.0f, 0.0f};
  1623. for (i = 0; i < 3; i++) {
  1624. if (ioctl(joystick->hwdata->fd_sensor, EVIOCGABS(ABS_X + i), &absinfo) >= 0) {
  1625. data[i] = absinfo.value * SDL_STANDARD_GRAVITY / joystick->hwdata->accelerometer_scale[i];
  1626. #ifdef DEBUG_INPUT_EVENTS
  1627. SDL_Log("Joystick : Re-read Accelerometer (axis %d) val= %f\n", i, data[i]);
  1628. #endif
  1629. }
  1630. }
  1631. SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_ACCEL, SDL_US_TO_NS(joystick->hwdata->sensor_tick), data, 3);
  1632. }
  1633. }
  1634. static void HandleInputEvents(SDL_Joystick *joystick)
  1635. {
  1636. struct input_event events[32];
  1637. int i, len, code, hat_index;
  1638. SDL_AssertJoysticksLocked();
  1639. if (joystick->hwdata->fresh) {
  1640. Uint64 ticks = SDL_GetTicksNS();
  1641. PollAllValues(ticks, joystick);
  1642. if (joystick->hwdata->report_sensor) {
  1643. PollAllSensors(ticks, joystick);
  1644. }
  1645. joystick->hwdata->fresh = SDL_FALSE;
  1646. }
  1647. errno = 0;
  1648. while ((len = read(joystick->hwdata->fd, events, sizeof(events))) > 0) {
  1649. len /= sizeof(events[0]);
  1650. for (i = 0; i < len; ++i) {
  1651. struct input_event *event = &events[i];
  1652. code = event->code;
  1653. /* If the kernel sent a SYN_DROPPED, we are supposed to ignore the
  1654. rest of the packet (the end of it signified by a SYN_REPORT) */
  1655. if (joystick->hwdata->recovering_from_dropped &&
  1656. ((event->type != EV_SYN) || (code != SYN_REPORT))) {
  1657. continue;
  1658. }
  1659. switch (event->type) {
  1660. case EV_KEY:
  1661. #ifdef DEBUG_INPUT_EVENTS
  1662. SDL_Log("Key 0x%.2x %s\n", code, event->value ? "PRESSED" : "RELEASED");
  1663. #endif
  1664. SDL_SendJoystickButton(SDL_EVDEV_GetEventTimestamp(event), joystick,
  1665. joystick->hwdata->key_map[code],
  1666. event->value);
  1667. break;
  1668. case EV_ABS:
  1669. switch (code) {
  1670. case ABS_HAT0X:
  1671. case ABS_HAT0Y:
  1672. case ABS_HAT1X:
  1673. case ABS_HAT1Y:
  1674. case ABS_HAT2X:
  1675. case ABS_HAT2Y:
  1676. case ABS_HAT3X:
  1677. case ABS_HAT3Y:
  1678. hat_index = (code - ABS_HAT0X) / 2;
  1679. if (joystick->hwdata->has_hat[hat_index]) {
  1680. #ifdef DEBUG_INPUT_EVENTS
  1681. SDL_Log("Axis 0x%.2x = %d\n", code, event->value);
  1682. #endif
  1683. HandleHat(SDL_EVDEV_GetEventTimestamp(event), joystick, hat_index, code % 2, event->value);
  1684. break;
  1685. }
  1686. SDL_FALLTHROUGH;
  1687. default:
  1688. #ifdef DEBUG_INPUT_EVENTS
  1689. SDL_Log("Axis 0x%.2x = %d\n", code, event->value);
  1690. #endif
  1691. event->value = AxisCorrect(joystick, code, event->value);
  1692. SDL_SendJoystickAxis(SDL_EVDEV_GetEventTimestamp(event), joystick,
  1693. joystick->hwdata->abs_map[code],
  1694. event->value);
  1695. break;
  1696. }
  1697. break;
  1698. case EV_SYN:
  1699. switch (code) {
  1700. case SYN_DROPPED:
  1701. #ifdef DEBUG_INPUT_EVENTS
  1702. SDL_Log("Event SYN_DROPPED detected\n");
  1703. #endif
  1704. joystick->hwdata->recovering_from_dropped = SDL_TRUE;
  1705. break;
  1706. case SYN_REPORT:
  1707. if (joystick->hwdata->recovering_from_dropped) {
  1708. joystick->hwdata->recovering_from_dropped = SDL_FALSE;
  1709. PollAllValues(SDL_GetTicksNS(), joystick); /* try to sync up to current state now */
  1710. }
  1711. break;
  1712. default:
  1713. break;
  1714. }
  1715. default:
  1716. break;
  1717. }
  1718. }
  1719. }
  1720. if (errno == ENODEV) {
  1721. /* We have to wait until the JoystickDetect callback to remove this */
  1722. joystick->hwdata->gone = SDL_TRUE;
  1723. errno = 0;
  1724. }
  1725. if (joystick->hwdata->report_sensor) {
  1726. SDL_assert(joystick->hwdata->fd_sensor >= 0);
  1727. while ((len = read(joystick->hwdata->fd_sensor, events, sizeof(events))) > 0) {
  1728. len /= sizeof(events[0]);
  1729. for (i = 0; i < len; ++i) {
  1730. unsigned int j;
  1731. struct input_event *event = &events[i];
  1732. code = event->code;
  1733. /* If the kernel sent a SYN_DROPPED, we are supposed to ignore the
  1734. rest of the packet (the end of it signified by a SYN_REPORT) */
  1735. if (joystick->hwdata->recovering_from_dropped_sensor &&
  1736. ((event->type != EV_SYN) || (code != SYN_REPORT))) {
  1737. continue;
  1738. }
  1739. switch (event->type) {
  1740. case EV_KEY:
  1741. SDL_assert(0);
  1742. break;
  1743. case EV_ABS:
  1744. switch (code) {
  1745. case ABS_X:
  1746. case ABS_Y:
  1747. case ABS_Z:
  1748. j = code - ABS_X;
  1749. joystick->hwdata->accel_data[j] = event->value * SDL_STANDARD_GRAVITY
  1750. / joystick->hwdata->accelerometer_scale[j];
  1751. break;
  1752. case ABS_RX:
  1753. case ABS_RY:
  1754. case ABS_RZ:
  1755. j = code - ABS_RX;
  1756. joystick->hwdata->gyro_data[j] = event->value * (SDL_PI_F / 180.f)
  1757. / joystick->hwdata->gyro_scale[j];
  1758. break;
  1759. }
  1760. break;
  1761. case EV_MSC:
  1762. if (code == MSC_TIMESTAMP) {
  1763. Sint32 tick = event->value;
  1764. Sint32 delta;
  1765. if (joystick->hwdata->last_tick < tick) {
  1766. delta = (tick - joystick->hwdata->last_tick);
  1767. } else {
  1768. delta = (SDL_MAX_SINT32 - joystick->hwdata->last_tick + tick + 1);
  1769. }
  1770. joystick->hwdata->sensor_tick += delta;
  1771. joystick->hwdata->last_tick = tick;
  1772. }
  1773. break;
  1774. case EV_SYN:
  1775. switch (code) {
  1776. case SYN_DROPPED:
  1777. #ifdef DEBUG_INPUT_EVENTS
  1778. SDL_Log("Event SYN_DROPPED detected\n");
  1779. #endif
  1780. joystick->hwdata->recovering_from_dropped_sensor = SDL_TRUE;
  1781. break;
  1782. case SYN_REPORT:
  1783. if (joystick->hwdata->recovering_from_dropped_sensor) {
  1784. joystick->hwdata->recovering_from_dropped_sensor = SDL_FALSE;
  1785. PollAllSensors(SDL_GetTicksNS(), joystick); /* try to sync up to current state now */
  1786. } else {
  1787. Uint64 timestamp = SDL_EVDEV_GetEventTimestamp(event);
  1788. SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_GYRO,
  1789. SDL_US_TO_NS(joystick->hwdata->sensor_tick),
  1790. joystick->hwdata->gyro_data, 3);
  1791. SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_ACCEL,
  1792. SDL_US_TO_NS(joystick->hwdata->sensor_tick),
  1793. joystick->hwdata->accel_data, 3);
  1794. }
  1795. break;
  1796. default:
  1797. break;
  1798. }
  1799. default:
  1800. break;
  1801. }
  1802. }
  1803. }
  1804. }
  1805. if (errno == ENODEV) {
  1806. /* We have to wait until the JoystickDetect callback to remove this */
  1807. joystick->hwdata->sensor_gone = SDL_TRUE;
  1808. }
  1809. }
  1810. static void HandleClassicEvents(SDL_Joystick *joystick)
  1811. {
  1812. struct js_event events[32];
  1813. int i, len, code, hat_index;
  1814. Uint64 timestamp = SDL_GetTicksNS();
  1815. SDL_AssertJoysticksLocked();
  1816. joystick->hwdata->fresh = SDL_FALSE;
  1817. while ((len = read(joystick->hwdata->fd, events, sizeof(events))) > 0) {
  1818. len /= sizeof(events[0]);
  1819. for (i = 0; i < len; ++i) {
  1820. switch (events[i].type) {
  1821. case JS_EVENT_BUTTON:
  1822. code = joystick->hwdata->key_pam[events[i].number];
  1823. SDL_SendJoystickButton(timestamp, joystick,
  1824. joystick->hwdata->key_map[code],
  1825. events[i].value);
  1826. break;
  1827. case JS_EVENT_AXIS:
  1828. code = joystick->hwdata->abs_pam[events[i].number];
  1829. switch (code) {
  1830. case ABS_HAT0X:
  1831. case ABS_HAT0Y:
  1832. case ABS_HAT1X:
  1833. case ABS_HAT1Y:
  1834. case ABS_HAT2X:
  1835. case ABS_HAT2Y:
  1836. case ABS_HAT3X:
  1837. case ABS_HAT3Y:
  1838. hat_index = (code - ABS_HAT0X) / 2;
  1839. if (joystick->hwdata->has_hat[hat_index]) {
  1840. HandleHat(timestamp, joystick, hat_index, code % 2, events[i].value);
  1841. break;
  1842. }
  1843. SDL_FALLTHROUGH;
  1844. default:
  1845. SDL_SendJoystickAxis(timestamp, joystick,
  1846. joystick->hwdata->abs_map[code],
  1847. events[i].value);
  1848. break;
  1849. }
  1850. }
  1851. }
  1852. }
  1853. }
  1854. static void LINUX_JoystickUpdate(SDL_Joystick *joystick)
  1855. {
  1856. SDL_AssertJoysticksLocked();
  1857. if (joystick->hwdata->m_bSteamController) {
  1858. SDL_UpdateSteamController(joystick);
  1859. return;
  1860. }
  1861. if (joystick->hwdata->classic) {
  1862. HandleClassicEvents(joystick);
  1863. } else {
  1864. HandleInputEvents(joystick);
  1865. }
  1866. }
  1867. /* Function to close a joystick after use */
  1868. static void LINUX_JoystickClose(SDL_Joystick *joystick)
  1869. {
  1870. SDL_AssertJoysticksLocked();
  1871. if (joystick->hwdata) {
  1872. if (joystick->hwdata->effect.id >= 0) {
  1873. ioctl(joystick->hwdata->fd, EVIOCRMFF, joystick->hwdata->effect.id);
  1874. joystick->hwdata->effect.id = -1;
  1875. }
  1876. if (joystick->hwdata->fd >= 0) {
  1877. close(joystick->hwdata->fd);
  1878. }
  1879. if (joystick->hwdata->fd_sensor >= 0) {
  1880. close(joystick->hwdata->fd_sensor);
  1881. }
  1882. if (joystick->hwdata->item) {
  1883. joystick->hwdata->item->hwdata = NULL;
  1884. }
  1885. if (joystick->hwdata->item_sensor) {
  1886. joystick->hwdata->item_sensor->hwdata = NULL;
  1887. }
  1888. SDL_free(joystick->hwdata->key_pam);
  1889. SDL_free(joystick->hwdata->abs_pam);
  1890. SDL_free(joystick->hwdata->hats);
  1891. SDL_free(joystick->hwdata->fname);
  1892. SDL_free(joystick->hwdata);
  1893. }
  1894. }
  1895. /* Function to perform any system-specific joystick related cleanup */
  1896. static void LINUX_JoystickQuit(void)
  1897. {
  1898. SDL_joylist_item *item = NULL;
  1899. SDL_joylist_item *next = NULL;
  1900. SDL_sensorlist_item *item_sensor = NULL;
  1901. SDL_sensorlist_item *next_sensor = NULL;
  1902. SDL_AssertJoysticksLocked();
  1903. if (inotify_fd >= 0) {
  1904. close(inotify_fd);
  1905. inotify_fd = -1;
  1906. }
  1907. for (item = SDL_joylist; item; item = next) {
  1908. next = item->next;
  1909. FreeJoylistItem(item);
  1910. }
  1911. for (item_sensor = SDL_sensorlist; item_sensor; item_sensor = next_sensor) {
  1912. next_sensor = item_sensor->next;
  1913. FreeSensorlistItem(item_sensor);
  1914. }
  1915. SDL_joylist = SDL_joylist_tail = NULL;
  1916. SDL_sensorlist = NULL;
  1917. numjoysticks = 0;
  1918. #ifdef SDL_USE_LIBUDEV
  1919. if (enumeration_method == ENUMERATION_LIBUDEV) {
  1920. SDL_UDEV_DelCallback(joystick_udev_callback);
  1921. SDL_UDEV_Quit();
  1922. }
  1923. #endif
  1924. SDL_QuitSteamControllers();
  1925. }
  1926. /*
  1927. This is based on the Linux Gamepad Specification
  1928. available at: https://www.kernel.org/doc/html/v4.15/input/gamepad.html
  1929. and the Android gamepad documentation,
  1930. https://developer.android.com/develop/ui/views/touch-and-input/game-controllers/controller-input
  1931. */
  1932. static SDL_bool LINUX_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
  1933. {
  1934. SDL_Joystick *joystick;
  1935. SDL_joylist_item *item = GetJoystickByDevIndex(device_index);
  1936. enum {
  1937. MAPPED_TRIGGER_LEFT = 0x1,
  1938. MAPPED_TRIGGER_RIGHT = 0x2,
  1939. MAPPED_TRIGGER_BOTH = 0x3,
  1940. MAPPED_DPAD_UP = 0x1,
  1941. MAPPED_DPAD_DOWN = 0x2,
  1942. MAPPED_DPAD_LEFT = 0x4,
  1943. MAPPED_DPAD_RIGHT = 0x8,
  1944. MAPPED_DPAD_ALL = 0xF,
  1945. };
  1946. unsigned int mapped;
  1947. SDL_AssertJoysticksLocked();
  1948. if (item->checked_mapping) {
  1949. if (item->mapping) {
  1950. SDL_memcpy(out, item->mapping, sizeof(*out));
  1951. #ifdef DEBUG_GAMEPAD_MAPPING
  1952. SDL_Log("Prior mapping for device %d", device_index);
  1953. #endif
  1954. return SDL_TRUE;
  1955. } else {
  1956. return SDL_FALSE;
  1957. }
  1958. }
  1959. /* We temporarily open the device to check how it's configured. Make
  1960. a fake SDL_Joystick object to do so. */
  1961. joystick = (SDL_Joystick *)SDL_calloc(sizeof(*joystick), 1);
  1962. if (!joystick) {
  1963. return SDL_FALSE;
  1964. }
  1965. joystick->magic = &SDL_joystick_magic;
  1966. SDL_memcpy(&joystick->guid, &item->guid, sizeof(item->guid));
  1967. joystick->hwdata = (struct joystick_hwdata *)
  1968. SDL_calloc(1, sizeof(*joystick->hwdata));
  1969. if (!joystick->hwdata) {
  1970. SDL_free(joystick);
  1971. return SDL_FALSE;
  1972. }
  1973. item->checked_mapping = SDL_TRUE;
  1974. if (PrepareJoystickHwdata(joystick, item, NULL) == -1) {
  1975. SDL_free(joystick->hwdata);
  1976. SDL_free(joystick);
  1977. return SDL_FALSE; /* SDL_SetError will already have been called */
  1978. }
  1979. /* don't assign `item->hwdata` so it's not in any global state. */
  1980. /* it is now safe to call LINUX_JoystickClose on this fake joystick. */
  1981. if (!joystick->hwdata->has_key[BTN_GAMEPAD]) {
  1982. /* Not a gamepad according to the specs. */
  1983. LINUX_JoystickClose(joystick);
  1984. SDL_free(joystick);
  1985. return SDL_FALSE;
  1986. }
  1987. /* We have a gamepad, start filling out the mappings */
  1988. #ifdef DEBUG_GAMEPAD_MAPPING
  1989. SDL_Log("Mapping %s (VID/PID 0x%.4x/0x%.4x)", item->name, SDL_GetJoystickVendor(joystick), SDL_GetJoystickProduct(joystick));
  1990. #endif
  1991. if (joystick->hwdata->has_key[BTN_A]) {
  1992. out->a.kind = EMappingKind_Button;
  1993. out->a.target = joystick->hwdata->key_map[BTN_A];
  1994. #ifdef DEBUG_GAMEPAD_MAPPING
  1995. SDL_Log("Mapped A to button %d (BTN_A)", out->a.target);
  1996. #endif
  1997. }
  1998. if (joystick->hwdata->has_key[BTN_B]) {
  1999. out->b.kind = EMappingKind_Button;
  2000. out->b.target = joystick->hwdata->key_map[BTN_B];
  2001. #ifdef DEBUG_GAMEPAD_MAPPING
  2002. SDL_Log("Mapped B to button %d (BTN_B)", out->b.target);
  2003. #endif
  2004. }
  2005. /* Xbox controllers use BTN_X and BTN_Y, and PS4 controllers use BTN_WEST and BTN_NORTH */
  2006. if (SDL_GetJoystickVendor(joystick) == USB_VENDOR_SONY) {
  2007. if (joystick->hwdata->has_key[BTN_WEST]) {
  2008. out->x.kind = EMappingKind_Button;
  2009. out->x.target = joystick->hwdata->key_map[BTN_WEST];
  2010. #ifdef DEBUG_GAMEPAD_MAPPING
  2011. SDL_Log("Mapped X to button %d (BTN_WEST)", out->x.target);
  2012. #endif
  2013. }
  2014. if (joystick->hwdata->has_key[BTN_NORTH]) {
  2015. out->y.kind = EMappingKind_Button;
  2016. out->y.target = joystick->hwdata->key_map[BTN_NORTH];
  2017. #ifdef DEBUG_GAMEPAD_MAPPING
  2018. SDL_Log("Mapped Y to button %d (BTN_NORTH)", out->y.target);
  2019. #endif
  2020. }
  2021. } else {
  2022. if (joystick->hwdata->has_key[BTN_X]) {
  2023. out->x.kind = EMappingKind_Button;
  2024. out->x.target = joystick->hwdata->key_map[BTN_X];
  2025. #ifdef DEBUG_GAMEPAD_MAPPING
  2026. SDL_Log("Mapped X to button %d (BTN_X)", out->x.target);
  2027. #endif
  2028. }
  2029. if (joystick->hwdata->has_key[BTN_Y]) {
  2030. out->y.kind = EMappingKind_Button;
  2031. out->y.target = joystick->hwdata->key_map[BTN_Y];
  2032. #ifdef DEBUG_GAMEPAD_MAPPING
  2033. SDL_Log("Mapped Y to button %d (BTN_Y)", out->y.target);
  2034. #endif
  2035. }
  2036. }
  2037. if (joystick->hwdata->has_key[BTN_SELECT]) {
  2038. out->back.kind = EMappingKind_Button;
  2039. out->back.target = joystick->hwdata->key_map[BTN_SELECT];
  2040. #ifdef DEBUG_GAMEPAD_MAPPING
  2041. SDL_Log("Mapped BACK to button %d (BTN_SELECT)", out->back.target);
  2042. #endif
  2043. }
  2044. if (joystick->hwdata->has_key[BTN_START]) {
  2045. out->start.kind = EMappingKind_Button;
  2046. out->start.target = joystick->hwdata->key_map[BTN_START];
  2047. #ifdef DEBUG_GAMEPAD_MAPPING
  2048. SDL_Log("Mapped START to button %d (BTN_START)", out->start.target);
  2049. #endif
  2050. }
  2051. if (joystick->hwdata->has_key[BTN_THUMBL]) {
  2052. out->leftstick.kind = EMappingKind_Button;
  2053. out->leftstick.target = joystick->hwdata->key_map[BTN_THUMBL];
  2054. #ifdef DEBUG_GAMEPAD_MAPPING
  2055. SDL_Log("Mapped LEFTSTICK to button %d (BTN_THUMBL)", out->leftstick.target);
  2056. #endif
  2057. }
  2058. if (joystick->hwdata->has_key[BTN_THUMBR]) {
  2059. out->rightstick.kind = EMappingKind_Button;
  2060. out->rightstick.target = joystick->hwdata->key_map[BTN_THUMBR];
  2061. #ifdef DEBUG_GAMEPAD_MAPPING
  2062. SDL_Log("Mapped RIGHTSTICK to button %d (BTN_THUMBR)", out->rightstick.target);
  2063. #endif
  2064. }
  2065. if (joystick->hwdata->has_key[BTN_MODE]) {
  2066. out->guide.kind = EMappingKind_Button;
  2067. out->guide.target = joystick->hwdata->key_map[BTN_MODE];
  2068. #ifdef DEBUG_GAMEPAD_MAPPING
  2069. SDL_Log("Mapped GUIDE to button %d (BTN_MODE)", out->guide.target);
  2070. #endif
  2071. }
  2072. /*
  2073. According to the specs the D-Pad, the shoulder buttons and the triggers
  2074. can be digital, or analog, or both at the same time.
  2075. */
  2076. /* Prefer digital shoulder buttons, but settle for digital or analog hat. */
  2077. mapped = 0;
  2078. if (joystick->hwdata->has_key[BTN_TL]) {
  2079. out->leftshoulder.kind = EMappingKind_Button;
  2080. out->leftshoulder.target = joystick->hwdata->key_map[BTN_TL];
  2081. mapped |= 0x1;
  2082. #ifdef DEBUG_GAMEPAD_MAPPING
  2083. SDL_Log("Mapped LEFTSHOULDER to button %d (BTN_TL)", out->leftshoulder.target);
  2084. #endif
  2085. }
  2086. if (joystick->hwdata->has_key[BTN_TR]) {
  2087. out->rightshoulder.kind = EMappingKind_Button;
  2088. out->rightshoulder.target = joystick->hwdata->key_map[BTN_TR];
  2089. mapped |= 0x2;
  2090. #ifdef DEBUG_GAMEPAD_MAPPING
  2091. SDL_Log("Mapped RIGHTSHOULDER to button %d (BTN_TR)", out->rightshoulder.target);
  2092. #endif
  2093. }
  2094. if (mapped != 0x3 && joystick->hwdata->has_hat[1]) {
  2095. int hat = joystick->hwdata->hats_indices[1] << 4;
  2096. out->leftshoulder.kind = EMappingKind_Hat;
  2097. out->rightshoulder.kind = EMappingKind_Hat;
  2098. out->leftshoulder.target = hat | 0x4;
  2099. out->rightshoulder.target = hat | 0x2;
  2100. mapped |= 0x3;
  2101. #ifdef DEBUG_GAMEPAD_MAPPING
  2102. SDL_Log("Mapped LEFT+RIGHTSHOULDER to hat 1 (ABS_HAT1X, ABS_HAT1Y)");
  2103. #endif
  2104. }
  2105. if (!(mapped & 0x1) && joystick->hwdata->has_abs[ABS_HAT1Y]) {
  2106. out->leftshoulder.kind = EMappingKind_Axis;
  2107. out->leftshoulder.target = joystick->hwdata->abs_map[ABS_HAT1Y];
  2108. mapped |= 0x1;
  2109. #ifdef DEBUG_GAMEPAD_MAPPING
  2110. SDL_Log("Mapped LEFTSHOULDER to axis %d (ABS_HAT1Y)", out->leftshoulder.target);
  2111. #endif
  2112. }
  2113. if (!(mapped & 0x2) && joystick->hwdata->has_abs[ABS_HAT1X]) {
  2114. out->rightshoulder.kind = EMappingKind_Axis;
  2115. out->rightshoulder.target = joystick->hwdata->abs_map[ABS_HAT1X];
  2116. mapped |= 0x2;
  2117. #ifdef DEBUG_GAMEPAD_MAPPING
  2118. SDL_Log("Mapped RIGHTSHOULDER to axis %d (ABS_HAT1X)", out->rightshoulder.target);
  2119. #endif
  2120. }
  2121. /* Prefer analog triggers, but settle for digital hat or buttons. */
  2122. mapped = 0;
  2123. /* Unfortunately there are several conventions for how analog triggers
  2124. * are represented as absolute axes:
  2125. *
  2126. * - Linux Gamepad Specification:
  2127. * LT = ABS_HAT2Y, RT = ABS_HAT2X
  2128. * - Android (and therefore many Bluetooth controllers):
  2129. * LT = ABS_BRAKE, RT = ABS_GAS
  2130. * - De facto standard for older Xbox and Playstation controllers:
  2131. * LT = ABS_Z, RT = ABS_RZ
  2132. *
  2133. * We try each one in turn. */
  2134. if (joystick->hwdata->has_abs[ABS_HAT2Y]) {
  2135. /* Linux Gamepad Specification */
  2136. out->lefttrigger.kind = EMappingKind_Axis;
  2137. out->lefttrigger.target = joystick->hwdata->abs_map[ABS_HAT2Y];
  2138. mapped |= MAPPED_TRIGGER_LEFT;
  2139. #ifdef DEBUG_GAMEPAD_MAPPING
  2140. SDL_Log("Mapped LEFTTRIGGER to axis %d (ABS_HAT2Y)", out->lefttrigger.target);
  2141. #endif
  2142. } else if (joystick->hwdata->has_abs[ABS_BRAKE]) {
  2143. /* Android convention */
  2144. out->lefttrigger.kind = EMappingKind_Axis;
  2145. out->lefttrigger.target = joystick->hwdata->abs_map[ABS_BRAKE];
  2146. mapped |= MAPPED_TRIGGER_LEFT;
  2147. #ifdef DEBUG_GAMEPAD_MAPPING
  2148. SDL_Log("Mapped LEFTTRIGGER to axis %d (ABS_BRAKE)", out->lefttrigger.target);
  2149. #endif
  2150. } else if (joystick->hwdata->has_abs[ABS_Z]) {
  2151. /* De facto standard for Xbox 360 and Playstation gamepads */
  2152. out->lefttrigger.kind = EMappingKind_Axis;
  2153. out->lefttrigger.target = joystick->hwdata->abs_map[ABS_Z];
  2154. mapped |= MAPPED_TRIGGER_LEFT;
  2155. #ifdef DEBUG_GAMEPAD_MAPPING
  2156. SDL_Log("Mapped LEFTTRIGGER to axis %d (ABS_Z)", out->lefttrigger.target);
  2157. #endif
  2158. }
  2159. if (joystick->hwdata->has_abs[ABS_HAT2X]) {
  2160. /* Linux Gamepad Specification */
  2161. out->righttrigger.kind = EMappingKind_Axis;
  2162. out->righttrigger.target = joystick->hwdata->abs_map[ABS_HAT2X];
  2163. mapped |= MAPPED_TRIGGER_RIGHT;
  2164. #ifdef DEBUG_GAMEPAD_MAPPING
  2165. SDL_Log("Mapped RIGHTTRIGGER to axis %d (ABS_HAT2X)", out->righttrigger.target);
  2166. #endif
  2167. } else if (joystick->hwdata->has_abs[ABS_GAS]) {
  2168. /* Android convention */
  2169. out->righttrigger.kind = EMappingKind_Axis;
  2170. out->righttrigger.target = joystick->hwdata->abs_map[ABS_GAS];
  2171. mapped |= MAPPED_TRIGGER_RIGHT;
  2172. #ifdef DEBUG_GAMEPAD_MAPPING
  2173. SDL_Log("Mapped RIGHTTRIGGER to axis %d (ABS_GAS)", out->righttrigger.target);
  2174. #endif
  2175. } else if (joystick->hwdata->has_abs[ABS_RZ]) {
  2176. /* De facto standard for Xbox 360 and Playstation gamepads */
  2177. out->righttrigger.kind = EMappingKind_Axis;
  2178. out->righttrigger.target = joystick->hwdata->abs_map[ABS_RZ];
  2179. mapped |= MAPPED_TRIGGER_RIGHT;
  2180. #ifdef DEBUG_GAMEPAD_MAPPING
  2181. SDL_Log("Mapped RIGHTTRIGGER to axis %d (ABS_RZ)", out->righttrigger.target);
  2182. #endif
  2183. }
  2184. if (mapped != MAPPED_TRIGGER_BOTH && joystick->hwdata->has_hat[2]) {
  2185. int hat = joystick->hwdata->hats_indices[2] << 4;
  2186. out->lefttrigger.kind = EMappingKind_Hat;
  2187. out->righttrigger.kind = EMappingKind_Hat;
  2188. out->lefttrigger.target = hat | 0x4;
  2189. out->righttrigger.target = hat | 0x2;
  2190. mapped |= MAPPED_TRIGGER_BOTH;
  2191. #ifdef DEBUG_GAMEPAD_MAPPING
  2192. SDL_Log("Mapped LEFT+RIGHTTRIGGER to hat 2 (ABS_HAT2X, ABS_HAT2Y)");
  2193. #endif
  2194. }
  2195. if (!(mapped & MAPPED_TRIGGER_LEFT) && joystick->hwdata->has_key[BTN_TL2]) {
  2196. out->lefttrigger.kind = EMappingKind_Button;
  2197. out->lefttrigger.target = joystick->hwdata->key_map[BTN_TL2];
  2198. mapped |= MAPPED_TRIGGER_LEFT;
  2199. #ifdef DEBUG_GAMEPAD_MAPPING
  2200. SDL_Log("Mapped LEFTTRIGGER to button %d (BTN_TL2)", out->lefttrigger.target);
  2201. #endif
  2202. }
  2203. if (!(mapped & MAPPED_TRIGGER_RIGHT) && joystick->hwdata->has_key[BTN_TR2]) {
  2204. out->righttrigger.kind = EMappingKind_Button;
  2205. out->righttrigger.target = joystick->hwdata->key_map[BTN_TR2];
  2206. mapped |= MAPPED_TRIGGER_RIGHT;
  2207. #ifdef DEBUG_GAMEPAD_MAPPING
  2208. SDL_Log("Mapped RIGHTTRIGGER to button %d (BTN_TR2)", out->righttrigger.target);
  2209. #endif
  2210. }
  2211. /* Prefer digital D-Pad buttons, but settle for digital or analog hat. */
  2212. mapped = 0;
  2213. if (joystick->hwdata->has_key[BTN_DPAD_UP]) {
  2214. out->dpup.kind = EMappingKind_Button;
  2215. out->dpup.target = joystick->hwdata->key_map[BTN_DPAD_UP];
  2216. mapped |= MAPPED_DPAD_UP;
  2217. #ifdef DEBUG_GAMEPAD_MAPPING
  2218. SDL_Log("Mapped DPUP to button %d (BTN_DPAD_UP)", out->dpup.target);
  2219. #endif
  2220. }
  2221. if (joystick->hwdata->has_key[BTN_DPAD_DOWN]) {
  2222. out->dpdown.kind = EMappingKind_Button;
  2223. out->dpdown.target = joystick->hwdata->key_map[BTN_DPAD_DOWN];
  2224. mapped |= MAPPED_DPAD_DOWN;
  2225. #ifdef DEBUG_GAMEPAD_MAPPING
  2226. SDL_Log("Mapped DPDOWN to button %d (BTN_DPAD_DOWN)", out->dpdown.target);
  2227. #endif
  2228. }
  2229. if (joystick->hwdata->has_key[BTN_DPAD_LEFT]) {
  2230. out->dpleft.kind = EMappingKind_Button;
  2231. out->dpleft.target = joystick->hwdata->key_map[BTN_DPAD_LEFT];
  2232. mapped |= MAPPED_DPAD_LEFT;
  2233. #ifdef DEBUG_GAMEPAD_MAPPING
  2234. SDL_Log("Mapped DPLEFT to button %d (BTN_DPAD_LEFT)", out->dpleft.target);
  2235. #endif
  2236. }
  2237. if (joystick->hwdata->has_key[BTN_DPAD_RIGHT]) {
  2238. out->dpright.kind = EMappingKind_Button;
  2239. out->dpright.target = joystick->hwdata->key_map[BTN_DPAD_RIGHT];
  2240. mapped |= MAPPED_DPAD_RIGHT;
  2241. #ifdef DEBUG_GAMEPAD_MAPPING
  2242. SDL_Log("Mapped DPRIGHT to button %d (BTN_DPAD_RIGHT)", out->dpright.target);
  2243. #endif
  2244. }
  2245. if (mapped != MAPPED_DPAD_ALL) {
  2246. if (joystick->hwdata->has_hat[0]) {
  2247. int hat = joystick->hwdata->hats_indices[0] << 4;
  2248. out->dpleft.kind = EMappingKind_Hat;
  2249. out->dpright.kind = EMappingKind_Hat;
  2250. out->dpup.kind = EMappingKind_Hat;
  2251. out->dpdown.kind = EMappingKind_Hat;
  2252. out->dpleft.target = hat | 0x8;
  2253. out->dpright.target = hat | 0x2;
  2254. out->dpup.target = hat | 0x1;
  2255. out->dpdown.target = hat | 0x4;
  2256. mapped |= MAPPED_DPAD_ALL;
  2257. #ifdef DEBUG_GAMEPAD_MAPPING
  2258. SDL_Log("Mapped DPUP+DOWN+LEFT+RIGHT to hat 0 (ABS_HAT0X, ABS_HAT0Y)");
  2259. #endif
  2260. } else if (joystick->hwdata->has_abs[ABS_HAT0X] && joystick->hwdata->has_abs[ABS_HAT0Y]) {
  2261. out->dpleft.kind = EMappingKind_Axis;
  2262. out->dpright.kind = EMappingKind_Axis;
  2263. out->dpup.kind = EMappingKind_Axis;
  2264. out->dpdown.kind = EMappingKind_Axis;
  2265. out->dpleft.target = joystick->hwdata->abs_map[ABS_HAT0X];
  2266. out->dpright.target = joystick->hwdata->abs_map[ABS_HAT0X];
  2267. out->dpup.target = joystick->hwdata->abs_map[ABS_HAT0Y];
  2268. out->dpdown.target = joystick->hwdata->abs_map[ABS_HAT0Y];
  2269. mapped |= MAPPED_DPAD_ALL;
  2270. #ifdef DEBUG_GAMEPAD_MAPPING
  2271. SDL_Log("Mapped DPUP+DOWN to axis %d (ABS_HAT0Y)", out->dpup.target);
  2272. SDL_Log("Mapped DPLEFT+RIGHT to axis %d (ABS_HAT0X)", out->dpleft.target);
  2273. #endif
  2274. }
  2275. }
  2276. if (joystick->hwdata->has_abs[ABS_X] && joystick->hwdata->has_abs[ABS_Y]) {
  2277. out->leftx.kind = EMappingKind_Axis;
  2278. out->lefty.kind = EMappingKind_Axis;
  2279. out->leftx.target = joystick->hwdata->abs_map[ABS_X];
  2280. out->lefty.target = joystick->hwdata->abs_map[ABS_Y];
  2281. #ifdef DEBUG_GAMEPAD_MAPPING
  2282. SDL_Log("Mapped LEFTX to axis %d (ABS_X)", out->leftx.target);
  2283. SDL_Log("Mapped LEFTY to axis %d (ABS_Y)", out->lefty.target);
  2284. #endif
  2285. }
  2286. /* The Linux Gamepad Specification uses the RX and RY axes,
  2287. * originally intended to represent X and Y rotation, as a second
  2288. * joystick. This is common for USB gamepads, and also many Bluetooth
  2289. * gamepads, particularly older ones.
  2290. *
  2291. * The Android mapping convention used by many Bluetooth controllers
  2292. * instead uses the Z axis as a secondary X axis, and the RZ axis as
  2293. * a secondary Y axis. */
  2294. if (joystick->hwdata->has_abs[ABS_RX] && joystick->hwdata->has_abs[ABS_RY]) {
  2295. /* Linux Gamepad Specification, Xbox 360, Playstation etc. */
  2296. out->rightx.kind = EMappingKind_Axis;
  2297. out->righty.kind = EMappingKind_Axis;
  2298. out->rightx.target = joystick->hwdata->abs_map[ABS_RX];
  2299. out->righty.target = joystick->hwdata->abs_map[ABS_RY];
  2300. #ifdef DEBUG_GAMEPAD_MAPPING
  2301. SDL_Log("Mapped RIGHTX to axis %d (ABS_RX)", out->rightx.target);
  2302. SDL_Log("Mapped RIGHTY to axis %d (ABS_RY)", out->righty.target);
  2303. #endif
  2304. } else if (joystick->hwdata->has_abs[ABS_Z] && joystick->hwdata->has_abs[ABS_RZ]) {
  2305. /* Android convention */
  2306. out->rightx.kind = EMappingKind_Axis;
  2307. out->righty.kind = EMappingKind_Axis;
  2308. out->rightx.target = joystick->hwdata->abs_map[ABS_Z];
  2309. out->righty.target = joystick->hwdata->abs_map[ABS_RZ];
  2310. #ifdef DEBUG_GAMEPAD_MAPPING
  2311. SDL_Log("Mapped RIGHTX to axis %d (ABS_Z)", out->rightx.target);
  2312. SDL_Log("Mapped RIGHTY to axis %d (ABS_RZ)", out->righty.target);
  2313. #endif
  2314. }
  2315. if (SDL_GetJoystickVendor(joystick) == USB_VENDOR_MICROSOFT) {
  2316. /* The Xbox Elite controllers have the paddles as BTN_TRIGGER_HAPPY5 - BTN_TRIGGER_HAPPY8 */
  2317. if (joystick->hwdata->has_key[BTN_TRIGGER_HAPPY5] &&
  2318. joystick->hwdata->has_key[BTN_TRIGGER_HAPPY6] &&
  2319. joystick->hwdata->has_key[BTN_TRIGGER_HAPPY7] &&
  2320. joystick->hwdata->has_key[BTN_TRIGGER_HAPPY8]) {
  2321. out->right_paddle1.kind = EMappingKind_Button;
  2322. out->right_paddle1.target = joystick->hwdata->key_map[BTN_TRIGGER_HAPPY5];
  2323. out->left_paddle1.kind = EMappingKind_Button;
  2324. out->left_paddle1.target = joystick->hwdata->key_map[BTN_TRIGGER_HAPPY7];
  2325. out->right_paddle2.kind = EMappingKind_Button;
  2326. out->right_paddle2.target = joystick->hwdata->key_map[BTN_TRIGGER_HAPPY6];
  2327. out->left_paddle2.kind = EMappingKind_Button;
  2328. out->left_paddle2.target = joystick->hwdata->key_map[BTN_TRIGGER_HAPPY8];
  2329. #ifdef DEBUG_GAMEPAD_MAPPING
  2330. SDL_Log("Mapped RIGHT_PADDLE1 to button %d (BTN_TRIGGER_HAPPY5)", out->right_paddle1.target);
  2331. SDL_Log("Mapped LEFT_PADDLE1 to button %d (BTN_TRIGGER_HAPPY7)", out->left_paddle1.target);
  2332. SDL_Log("Mapped RIGHT_PADDLE2 to button %d (BTN_TRIGGER_HAPPY6)", out->right_paddle2.target);
  2333. SDL_Log("Mapped LEFT_PADDLE2 to button %d (BTN_TRIGGER_HAPPY8)", out->left_paddle2.target);
  2334. #endif
  2335. }
  2336. /* The Xbox Series X controllers have the Share button as KEY_RECORD */
  2337. if (joystick->hwdata->has_key[KEY_RECORD]) {
  2338. out->misc1.kind = EMappingKind_Button;
  2339. out->misc1.target = joystick->hwdata->key_map[KEY_RECORD];
  2340. #ifdef DEBUG_GAMEPAD_MAPPING
  2341. SDL_Log("Mapped MISC1 to button %d (KEY_RECORD)", out->misc1.target);
  2342. #endif
  2343. }
  2344. }
  2345. LINUX_JoystickClose(joystick);
  2346. SDL_free(joystick);
  2347. /* Cache the mapping for later */
  2348. item->mapping = (SDL_GamepadMapping *)SDL_malloc(sizeof(*item->mapping));
  2349. if (item->mapping) {
  2350. SDL_memcpy(item->mapping, out, sizeof(*out));
  2351. }
  2352. #ifdef DEBUG_GAMEPAD_MAPPING
  2353. SDL_Log("Generated mapping for device %d", device_index);
  2354. #endif
  2355. return SDL_TRUE;
  2356. }
  2357. SDL_JoystickDriver SDL_LINUX_JoystickDriver = {
  2358. LINUX_JoystickInit,
  2359. LINUX_JoystickGetCount,
  2360. LINUX_JoystickDetect,
  2361. LINUX_JoystickGetDeviceName,
  2362. LINUX_JoystickGetDevicePath,
  2363. LINUX_JoystickGetDeviceSteamVirtualGamepadSlot,
  2364. LINUX_JoystickGetDevicePlayerIndex,
  2365. LINUX_JoystickSetDevicePlayerIndex,
  2366. LINUX_JoystickGetDeviceGUID,
  2367. LINUX_JoystickGetDeviceInstanceID,
  2368. LINUX_JoystickOpen,
  2369. LINUX_JoystickRumble,
  2370. LINUX_JoystickRumbleTriggers,
  2371. LINUX_JoystickGetCapabilities,
  2372. LINUX_JoystickSetLED,
  2373. LINUX_JoystickSendEffect,
  2374. LINUX_JoystickSetSensorsEnabled,
  2375. LINUX_JoystickUpdate,
  2376. LINUX_JoystickClose,
  2377. LINUX_JoystickQuit,
  2378. LINUX_JoystickGetGamepadMapping
  2379. };
  2380. #endif /* SDL_JOYSTICK_LINUX */