SDL_sysjoystick.c 91 KB

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