SDL_sysjoystick.c 90 KB

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