SDL_sysjoystick.c 56 KB

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