2
0

SDL_sysjoystick.c 66 KB

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