SDL_sysjoystick.c 55 KB

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