SDL_sysjoystick.c 64 KB

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