SDL_sysjoystick.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2014 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 <unistd.h>
  26. #include <fcntl.h>
  27. #include <sys/ioctl.h>
  28. #include <limits.h> /* For the definition of PATH_MAX */
  29. #include <linux/joystick.h>
  30. #include "SDL_assert.h"
  31. #include "SDL_joystick.h"
  32. #include "SDL_endian.h"
  33. #include "../SDL_sysjoystick.h"
  34. #include "../SDL_joystick_c.h"
  35. #include "SDL_sysjoystick_c.h"
  36. /* !!! FIXME: move this somewhere else. */
  37. #if !SDL_EVENTS_DISABLED
  38. #include "../../events/SDL_events_c.h"
  39. #endif
  40. /* This isn't defined in older Linux kernel headers */
  41. #ifndef SYN_DROPPED
  42. #define SYN_DROPPED 3
  43. #endif
  44. #include "../../core/linux/SDL_udev.h"
  45. static int MaybeAddDevice(const char *path);
  46. #if SDL_USE_LIBUDEV
  47. static int MaybeRemoveDevice(const char *path);
  48. void joystick_udev_callback(SDL_UDEV_deviceevent udev_type, int udev_class, const char *devpath);
  49. #endif /* SDL_USE_LIBUDEV */
  50. /* A linked list of available joysticks */
  51. typedef struct SDL_joylist_item
  52. {
  53. int device_instance;
  54. char *path; /* "/dev/input/event2" or whatever */
  55. char *name; /* "SideWinder 3D Pro" or whatever */
  56. SDL_JoystickGUID guid;
  57. dev_t devnum;
  58. struct joystick_hwdata *hwdata;
  59. struct SDL_joylist_item *next;
  60. } SDL_joylist_item;
  61. static SDL_joylist_item *SDL_joylist = NULL;
  62. static SDL_joylist_item *SDL_joylist_tail = NULL;
  63. static int numjoysticks = 0;
  64. static int instance_counter = 0;
  65. #define test_bit(nr, addr) \
  66. (((1UL << ((nr) % (sizeof(long) * 8))) & ((addr)[(nr) / (sizeof(long) * 8)])) != 0)
  67. #define NBITS(x) ((((x)-1)/(sizeof(long) * 8))+1)
  68. static int
  69. IsJoystick(int fd, char *namebuf, const size_t namebuflen, SDL_JoystickGUID *guid)
  70. {
  71. struct input_id inpid;
  72. Uint16 *guid16 = (Uint16 *) ((char *) &guid->data);
  73. #if !SDL_USE_LIBUDEV
  74. /* When udev is enabled we only get joystick devices here, so there's no need to test them */
  75. unsigned long evbit[NBITS(EV_MAX)] = { 0 };
  76. unsigned long keybit[NBITS(KEY_MAX)] = { 0 };
  77. unsigned long absbit[NBITS(ABS_MAX)] = { 0 };
  78. if ((ioctl(fd, EVIOCGBIT(0, sizeof(evbit)), evbit) < 0) ||
  79. (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) < 0) ||
  80. (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) < 0)) {
  81. return (0);
  82. }
  83. if (!(test_bit(EV_KEY, evbit) && test_bit(EV_ABS, evbit) &&
  84. test_bit(ABS_X, absbit) && test_bit(ABS_Y, absbit))) {
  85. return 0;
  86. }
  87. #endif
  88. if (ioctl(fd, EVIOCGNAME(namebuflen), namebuf) < 0) {
  89. return 0;
  90. }
  91. if (ioctl(fd, EVIOCGID, &inpid) < 0) {
  92. return 0;
  93. }
  94. #ifdef DEBUG_JOYSTICK
  95. printf("Joystick: %s, bustype = %d, vendor = 0x%x, product = 0x%x, version = %d\n", namebuf, inpid.bustype, inpid.vendor, inpid.product, inpid.version);
  96. #endif
  97. SDL_memset(guid->data, 0, sizeof(guid->data));
  98. /* We only need 16 bits for each of these; space them out to fill 128. */
  99. /* Byteswap so devices get same GUID on little/big endian platforms. */
  100. *(guid16++) = SDL_SwapLE16(inpid.bustype);
  101. *(guid16++) = 0;
  102. if (inpid.vendor && inpid.product && inpid.version) {
  103. *(guid16++) = SDL_SwapLE16(inpid.vendor);
  104. *(guid16++) = 0;
  105. *(guid16++) = SDL_SwapLE16(inpid.product);
  106. *(guid16++) = 0;
  107. *(guid16++) = SDL_SwapLE16(inpid.version);
  108. *(guid16++) = 0;
  109. } else {
  110. SDL_strlcpy((char*)guid16, namebuf, sizeof(guid->data) - 4);
  111. }
  112. return 1;
  113. }
  114. #if SDL_USE_LIBUDEV
  115. void joystick_udev_callback(SDL_UDEV_deviceevent udev_type, int udev_class, const char *devpath)
  116. {
  117. if (devpath == NULL) {
  118. return;
  119. }
  120. switch (udev_type) {
  121. case SDL_UDEV_DEVICEADDED:
  122. if (!(udev_class & SDL_UDEV_DEVICE_JOYSTICK)) {
  123. return;
  124. }
  125. MaybeAddDevice(devpath);
  126. break;
  127. case SDL_UDEV_DEVICEREMOVED:
  128. MaybeRemoveDevice(devpath);
  129. break;
  130. default:
  131. break;
  132. }
  133. }
  134. #endif /* SDL_USE_LIBUDEV */
  135. /* !!! FIXME: I would love to dump this code and use libudev instead. */
  136. static int
  137. MaybeAddDevice(const char *path)
  138. {
  139. struct stat sb;
  140. int fd = -1;
  141. int isstick = 0;
  142. char namebuf[128];
  143. SDL_JoystickGUID guid;
  144. SDL_joylist_item *item;
  145. #if !SDL_EVENTS_DISABLED
  146. SDL_Event event;
  147. #endif
  148. if (path == NULL) {
  149. return -1;
  150. }
  151. if (stat(path, &sb) == -1) {
  152. return -1;
  153. }
  154. /* Check to make sure it's not already in list. */
  155. for (item = SDL_joylist; item != NULL; item = item->next) {
  156. if (sb.st_rdev == item->devnum) {
  157. return -1; /* already have this one */
  158. }
  159. }
  160. fd = open(path, O_RDONLY, 0);
  161. if (fd < 0) {
  162. return -1;
  163. }
  164. #ifdef DEBUG_INPUT_EVENTS
  165. printf("Checking %s\n", path);
  166. #endif
  167. isstick = IsJoystick(fd, namebuf, sizeof (namebuf), &guid);
  168. close(fd);
  169. if (!isstick) {
  170. return -1;
  171. }
  172. item = (SDL_joylist_item *) SDL_malloc(sizeof (SDL_joylist_item));
  173. if (item == NULL) {
  174. return -1;
  175. }
  176. SDL_zerop(item);
  177. item->devnum = sb.st_rdev;
  178. item->path = SDL_strdup(path);
  179. item->name = SDL_strdup(namebuf);
  180. item->guid = guid;
  181. if ( (item->path == NULL) || (item->name == NULL) ) {
  182. SDL_free(item->path);
  183. SDL_free(item->name);
  184. SDL_free(item);
  185. return -1;
  186. }
  187. item->device_instance = instance_counter++;
  188. if (SDL_joylist_tail == NULL) {
  189. SDL_joylist = SDL_joylist_tail = item;
  190. } else {
  191. SDL_joylist_tail->next = item;
  192. SDL_joylist_tail = item;
  193. }
  194. /* Need to increment the joystick count before we post the event */
  195. ++numjoysticks;
  196. /* !!! FIXME: Move this to an SDL_PrivateJoyDeviceAdded() function? */
  197. #if !SDL_EVENTS_DISABLED
  198. event.type = SDL_JOYDEVICEADDED;
  199. if (SDL_GetEventState(event.type) == SDL_ENABLE) {
  200. event.jdevice.which = (numjoysticks - 1);
  201. if ( (SDL_EventOK == NULL) ||
  202. (*SDL_EventOK) (SDL_EventOKParam, &event) ) {
  203. SDL_PushEvent(&event);
  204. }
  205. }
  206. #endif /* !SDL_EVENTS_DISABLED */
  207. return numjoysticks;
  208. }
  209. #if SDL_USE_LIBUDEV
  210. /* !!! FIXME: I would love to dump this code and use libudev instead. */
  211. static int
  212. MaybeRemoveDevice(const char *path)
  213. {
  214. SDL_joylist_item *item;
  215. SDL_joylist_item *prev = NULL;
  216. #if !SDL_EVENTS_DISABLED
  217. SDL_Event event;
  218. #endif
  219. if (path == NULL) {
  220. return -1;
  221. }
  222. for (item = SDL_joylist; item != NULL; item = item->next) {
  223. /* found it, remove it. */
  224. if (SDL_strcmp(path, item->path) == 0) {
  225. const int retval = item->device_instance;
  226. if (item->hwdata) {
  227. item->hwdata->item = NULL;
  228. }
  229. if (prev != NULL) {
  230. prev->next = item->next;
  231. } else {
  232. SDL_assert(SDL_joylist == item);
  233. SDL_joylist = item->next;
  234. }
  235. if (item == SDL_joylist_tail) {
  236. SDL_joylist_tail = prev;
  237. }
  238. /* Need to decrement the joystick count before we post the event */
  239. --numjoysticks;
  240. /* !!! FIXME: Move this to an SDL_PrivateJoyDeviceRemoved() function? */
  241. #if !SDL_EVENTS_DISABLED
  242. event.type = SDL_JOYDEVICEREMOVED;
  243. if (SDL_GetEventState(event.type) == SDL_ENABLE) {
  244. event.jdevice.which = item->device_instance;
  245. if ( (SDL_EventOK == NULL) ||
  246. (*SDL_EventOK) (SDL_EventOKParam, &event) ) {
  247. SDL_PushEvent(&event);
  248. }
  249. }
  250. #endif /* !SDL_EVENTS_DISABLED */
  251. SDL_free(item->path);
  252. SDL_free(item->name);
  253. SDL_free(item);
  254. return retval;
  255. }
  256. prev = item;
  257. }
  258. return -1;
  259. }
  260. #endif
  261. static int
  262. JoystickInitWithoutUdev(void)
  263. {
  264. int i;
  265. char path[PATH_MAX];
  266. /* !!! FIXME: only finds sticks if they're called /dev/input/event[0..31] */
  267. /* !!! FIXME: we could at least readdir() through /dev/input...? */
  268. /* !!! FIXME: (or delete this and rely on libudev?) */
  269. for (i = 0; i < 32; i++) {
  270. SDL_snprintf(path, SDL_arraysize(path), "/dev/input/event%d", i);
  271. MaybeAddDevice(path);
  272. }
  273. return numjoysticks;
  274. }
  275. #if SDL_USE_LIBUDEV
  276. static int
  277. JoystickInitWithUdev(void)
  278. {
  279. if (SDL_UDEV_Init() < 0) {
  280. return SDL_SetError("Could not initialize UDEV");
  281. }
  282. /* Set up the udev callback */
  283. if (SDL_UDEV_AddCallback(joystick_udev_callback) < 0) {
  284. SDL_UDEV_Quit();
  285. return SDL_SetError("Could not set up joystick <-> udev callback");
  286. }
  287. /* Force a scan to build the initial device list */
  288. SDL_UDEV_Scan();
  289. return numjoysticks;
  290. }
  291. #endif
  292. int
  293. SDL_SYS_JoystickInit(void)
  294. {
  295. /* First see if the user specified one or more joysticks to use */
  296. if (SDL_getenv("SDL_JOYSTICK_DEVICE") != NULL) {
  297. char *envcopy, *envpath, *delim;
  298. envcopy = SDL_strdup(SDL_getenv("SDL_JOYSTICK_DEVICE"));
  299. envpath = envcopy;
  300. while (envpath != NULL) {
  301. delim = SDL_strchr(envpath, ':');
  302. if (delim != NULL) {
  303. *delim++ = '\0';
  304. }
  305. MaybeAddDevice(envpath);
  306. envpath = delim;
  307. }
  308. SDL_free(envcopy);
  309. }
  310. #if SDL_USE_LIBUDEV
  311. return JoystickInitWithUdev();
  312. #endif
  313. return JoystickInitWithoutUdev();
  314. }
  315. int SDL_SYS_NumJoysticks()
  316. {
  317. return numjoysticks;
  318. }
  319. void SDL_SYS_JoystickDetect()
  320. {
  321. #if SDL_USE_LIBUDEV
  322. SDL_UDEV_Poll();
  323. #endif
  324. }
  325. static SDL_joylist_item *
  326. JoystickByDevIndex(int device_index)
  327. {
  328. SDL_joylist_item *item = SDL_joylist;
  329. if ((device_index < 0) || (device_index >= numjoysticks)) {
  330. return NULL;
  331. }
  332. while (device_index > 0) {
  333. SDL_assert(item != NULL);
  334. device_index--;
  335. item = item->next;
  336. }
  337. return item;
  338. }
  339. /* Function to get the device-dependent name of a joystick */
  340. const char *
  341. SDL_SYS_JoystickNameForDeviceIndex(int device_index)
  342. {
  343. return JoystickByDevIndex(device_index)->name;
  344. }
  345. /* Function to perform the mapping from device index to the instance id for this index */
  346. SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
  347. {
  348. return JoystickByDevIndex(device_index)->device_instance;
  349. }
  350. static int
  351. allocate_hatdata(SDL_Joystick * joystick)
  352. {
  353. int i;
  354. joystick->hwdata->hats =
  355. (struct hwdata_hat *) SDL_malloc(joystick->nhats *
  356. sizeof(struct hwdata_hat));
  357. if (joystick->hwdata->hats == NULL) {
  358. return (-1);
  359. }
  360. for (i = 0; i < joystick->nhats; ++i) {
  361. joystick->hwdata->hats[i].axis[0] = 1;
  362. joystick->hwdata->hats[i].axis[1] = 1;
  363. }
  364. return (0);
  365. }
  366. static int
  367. allocate_balldata(SDL_Joystick * joystick)
  368. {
  369. int i;
  370. joystick->hwdata->balls =
  371. (struct hwdata_ball *) SDL_malloc(joystick->nballs *
  372. sizeof(struct hwdata_ball));
  373. if (joystick->hwdata->balls == NULL) {
  374. return (-1);
  375. }
  376. for (i = 0; i < joystick->nballs; ++i) {
  377. joystick->hwdata->balls[i].axis[0] = 0;
  378. joystick->hwdata->balls[i].axis[1] = 0;
  379. }
  380. return (0);
  381. }
  382. static void
  383. ConfigJoystick(SDL_Joystick * joystick, int fd)
  384. {
  385. int i, t;
  386. unsigned long keybit[NBITS(KEY_MAX)] = { 0 };
  387. unsigned long absbit[NBITS(ABS_MAX)] = { 0 };
  388. unsigned long relbit[NBITS(REL_MAX)] = { 0 };
  389. /* See if this device uses the new unified event API */
  390. if ((ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) >= 0) &&
  391. (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) >= 0) &&
  392. (ioctl(fd, EVIOCGBIT(EV_REL, sizeof(relbit)), relbit) >= 0)) {
  393. /* Get the number of buttons, axes, and other thingamajigs */
  394. for (i = BTN_JOYSTICK; i < KEY_MAX; ++i) {
  395. if (test_bit(i, keybit)) {
  396. #ifdef DEBUG_INPUT_EVENTS
  397. printf("Joystick has button: 0x%x\n", i);
  398. #endif
  399. joystick->hwdata->key_map[i - BTN_MISC] = joystick->nbuttons;
  400. ++joystick->nbuttons;
  401. }
  402. }
  403. for (i = BTN_MISC; i < BTN_JOYSTICK; ++i) {
  404. if (test_bit(i, keybit)) {
  405. #ifdef DEBUG_INPUT_EVENTS
  406. printf("Joystick has button: 0x%x\n", i);
  407. #endif
  408. joystick->hwdata->key_map[i - BTN_MISC] = joystick->nbuttons;
  409. ++joystick->nbuttons;
  410. }
  411. }
  412. for (i = 0; i < ABS_MISC; ++i) {
  413. /* Skip hats */
  414. if (i == ABS_HAT0X) {
  415. i = ABS_HAT3Y;
  416. continue;
  417. }
  418. if (test_bit(i, absbit)) {
  419. struct input_absinfo absinfo;
  420. if (ioctl(fd, EVIOCGABS(i), &absinfo) < 0) {
  421. continue;
  422. }
  423. #ifdef DEBUG_INPUT_EVENTS
  424. printf("Joystick has absolute axis: 0x%.2x\n", i);
  425. printf("Values = { %d, %d, %d, %d, %d }\n",
  426. absinfo.value, absinfo.minimum, absinfo.maximum,
  427. absinfo.fuzz, absinfo.flat);
  428. #endif /* DEBUG_INPUT_EVENTS */
  429. joystick->hwdata->abs_map[i] = joystick->naxes;
  430. if (absinfo.minimum == absinfo.maximum) {
  431. joystick->hwdata->abs_correct[i].used = 0;
  432. } else {
  433. joystick->hwdata->abs_correct[i].used = 1;
  434. joystick->hwdata->abs_correct[i].coef[0] =
  435. (absinfo.maximum + absinfo.minimum) - 2 * absinfo.flat;
  436. joystick->hwdata->abs_correct[i].coef[1] =
  437. (absinfo.maximum + absinfo.minimum) + 2 * absinfo.flat;
  438. t = ((absinfo.maximum - absinfo.minimum) - 4 * absinfo.flat);
  439. if (t != 0) {
  440. joystick->hwdata->abs_correct[i].coef[2] =
  441. (1 << 28) / t;
  442. } else {
  443. joystick->hwdata->abs_correct[i].coef[2] = 0;
  444. }
  445. }
  446. ++joystick->naxes;
  447. }
  448. }
  449. for (i = ABS_HAT0X; i <= ABS_HAT3Y; i += 2) {
  450. if (test_bit(i, absbit) || test_bit(i + 1, absbit)) {
  451. struct input_absinfo absinfo;
  452. if (ioctl(fd, EVIOCGABS(i), &absinfo) < 0) {
  453. continue;
  454. }
  455. #ifdef DEBUG_INPUT_EVENTS
  456. printf("Joystick has hat %d\n", (i - ABS_HAT0X) / 2);
  457. printf("Values = { %d, %d, %d, %d, %d }\n",
  458. absinfo.value, absinfo.minimum, absinfo.maximum,
  459. absinfo.fuzz, absinfo.flat);
  460. #endif /* DEBUG_INPUT_EVENTS */
  461. ++joystick->nhats;
  462. }
  463. }
  464. if (test_bit(REL_X, relbit) || test_bit(REL_Y, relbit)) {
  465. ++joystick->nballs;
  466. }
  467. /* Allocate data to keep track of these thingamajigs */
  468. if (joystick->nhats > 0) {
  469. if (allocate_hatdata(joystick) < 0) {
  470. joystick->nhats = 0;
  471. }
  472. }
  473. if (joystick->nballs > 0) {
  474. if (allocate_balldata(joystick) < 0) {
  475. joystick->nballs = 0;
  476. }
  477. }
  478. }
  479. }
  480. /* Function to open a joystick for use.
  481. The joystick to open is specified by the index field of the joystick.
  482. This should fill the nbuttons and naxes fields of the joystick structure.
  483. It returns 0, or -1 if there is an error.
  484. */
  485. int
  486. SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
  487. {
  488. SDL_joylist_item *item = JoystickByDevIndex(device_index);
  489. char *fname = NULL;
  490. int fd = -1;
  491. if (item == NULL) {
  492. return SDL_SetError("No such device");
  493. }
  494. fname = item->path;
  495. fd = open(fname, O_RDONLY, 0);
  496. if (fd < 0) {
  497. return SDL_SetError("Unable to open %s", fname);
  498. }
  499. joystick->instance_id = item->device_instance;
  500. joystick->hwdata = (struct joystick_hwdata *)
  501. SDL_malloc(sizeof(*joystick->hwdata));
  502. if (joystick->hwdata == NULL) {
  503. close(fd);
  504. return SDL_OutOfMemory();
  505. }
  506. SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata));
  507. joystick->hwdata->item = item;
  508. joystick->hwdata->guid = item->guid;
  509. joystick->hwdata->fd = fd;
  510. joystick->hwdata->fname = SDL_strdup(item->path);
  511. if (joystick->hwdata->fname == NULL) {
  512. SDL_free(joystick->hwdata);
  513. joystick->hwdata = NULL;
  514. close(fd);
  515. return SDL_OutOfMemory();
  516. }
  517. SDL_assert(item->hwdata == NULL);
  518. item->hwdata = joystick->hwdata;
  519. /* Set the joystick to non-blocking read mode */
  520. fcntl(fd, F_SETFL, O_NONBLOCK);
  521. /* Get the number of buttons and axes on the joystick */
  522. ConfigJoystick(joystick, fd);
  523. /* mark joystick as fresh and ready */
  524. joystick->hwdata->fresh = 1;
  525. return (0);
  526. }
  527. /* Function to determine is this joystick is attached to the system right now */
  528. SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
  529. {
  530. return !joystick->closed && (joystick->hwdata->item != NULL);
  531. }
  532. static SDL_INLINE void
  533. HandleHat(SDL_Joystick * stick, Uint8 hat, int axis, int value)
  534. {
  535. struct hwdata_hat *the_hat;
  536. const Uint8 position_map[3][3] = {
  537. {SDL_HAT_LEFTUP, SDL_HAT_UP, SDL_HAT_RIGHTUP},
  538. {SDL_HAT_LEFT, SDL_HAT_CENTERED, SDL_HAT_RIGHT},
  539. {SDL_HAT_LEFTDOWN, SDL_HAT_DOWN, SDL_HAT_RIGHTDOWN}
  540. };
  541. the_hat = &stick->hwdata->hats[hat];
  542. if (value < 0) {
  543. value = 0;
  544. } else if (value == 0) {
  545. value = 1;
  546. } else if (value > 0) {
  547. value = 2;
  548. }
  549. if (value != the_hat->axis[axis]) {
  550. the_hat->axis[axis] = value;
  551. SDL_PrivateJoystickHat(stick, hat,
  552. position_map[the_hat->
  553. axis[1]][the_hat->axis[0]]);
  554. }
  555. }
  556. static SDL_INLINE void
  557. HandleBall(SDL_Joystick * stick, Uint8 ball, int axis, int value)
  558. {
  559. stick->hwdata->balls[ball].axis[axis] += value;
  560. }
  561. static SDL_INLINE int
  562. AxisCorrect(SDL_Joystick * joystick, int which, int value)
  563. {
  564. struct axis_correct *correct;
  565. correct = &joystick->hwdata->abs_correct[which];
  566. if (correct->used) {
  567. value *= 2;
  568. if (value > correct->coef[0]) {
  569. if (value < correct->coef[1]) {
  570. return 0;
  571. }
  572. value -= correct->coef[1];
  573. } else {
  574. value -= correct->coef[0];
  575. }
  576. value *= correct->coef[2];
  577. value >>= 13;
  578. }
  579. /* Clamp and return */
  580. if (value < -32768)
  581. return -32768;
  582. if (value > 32767)
  583. return 32767;
  584. return value;
  585. }
  586. static SDL_INLINE void
  587. PollAllValues(SDL_Joystick * joystick)
  588. {
  589. struct input_absinfo absinfo;
  590. int a, b = 0;
  591. /* Poll all axis */
  592. for (a = ABS_X; b < ABS_MAX; a++) {
  593. switch (a) {
  594. case ABS_HAT0X:
  595. case ABS_HAT0Y:
  596. case ABS_HAT1X:
  597. case ABS_HAT1Y:
  598. case ABS_HAT2X:
  599. case ABS_HAT2Y:
  600. case ABS_HAT3X:
  601. case ABS_HAT3Y:
  602. /* ingore hats */
  603. break;
  604. default:
  605. if (joystick->hwdata->abs_correct[b].used) {
  606. if (ioctl(joystick->hwdata->fd, EVIOCGABS(a), &absinfo) >= 0) {
  607. absinfo.value = AxisCorrect(joystick, b, absinfo.value);
  608. #ifdef DEBUG_INPUT_EVENTS
  609. printf("Joystick : Re-read Axis %d (%d) val= %d\n",
  610. joystick->hwdata->abs_map[b], a, absinfo.value);
  611. #endif
  612. SDL_PrivateJoystickAxis(joystick,
  613. joystick->hwdata->abs_map[b],
  614. absinfo.value);
  615. }
  616. }
  617. b++;
  618. }
  619. }
  620. }
  621. static SDL_INLINE void
  622. HandleInputEvents(SDL_Joystick * joystick)
  623. {
  624. struct input_event events[32];
  625. int i, len;
  626. int code;
  627. if (joystick->hwdata->fresh) {
  628. PollAllValues(joystick);
  629. joystick->hwdata->fresh = 0;
  630. }
  631. while ((len = read(joystick->hwdata->fd, events, (sizeof events))) > 0) {
  632. len /= sizeof(events[0]);
  633. for (i = 0; i < len; ++i) {
  634. code = events[i].code;
  635. switch (events[i].type) {
  636. case EV_KEY:
  637. if (code >= BTN_MISC) {
  638. code -= BTN_MISC;
  639. SDL_PrivateJoystickButton(joystick,
  640. joystick->hwdata->key_map[code],
  641. events[i].value);
  642. }
  643. break;
  644. case EV_ABS:
  645. if (code >= ABS_MISC) {
  646. break;
  647. }
  648. switch (code) {
  649. case ABS_HAT0X:
  650. case ABS_HAT0Y:
  651. case ABS_HAT1X:
  652. case ABS_HAT1Y:
  653. case ABS_HAT2X:
  654. case ABS_HAT2Y:
  655. case ABS_HAT3X:
  656. case ABS_HAT3Y:
  657. code -= ABS_HAT0X;
  658. HandleHat(joystick, code / 2, code % 2, events[i].value);
  659. break;
  660. default:
  661. events[i].value =
  662. AxisCorrect(joystick, code, events[i].value);
  663. SDL_PrivateJoystickAxis(joystick,
  664. joystick->hwdata->abs_map[code],
  665. events[i].value);
  666. break;
  667. }
  668. break;
  669. case EV_REL:
  670. switch (code) {
  671. case REL_X:
  672. case REL_Y:
  673. code -= REL_X;
  674. HandleBall(joystick, code / 2, code % 2, events[i].value);
  675. break;
  676. default:
  677. break;
  678. }
  679. break;
  680. case EV_SYN:
  681. switch (code) {
  682. case SYN_DROPPED :
  683. #ifdef DEBUG_INPUT_EVENTS
  684. printf("Event SYN_DROPPED detected\n");
  685. #endif
  686. PollAllValues(joystick);
  687. break;
  688. default:
  689. break;
  690. }
  691. default:
  692. break;
  693. }
  694. }
  695. }
  696. }
  697. void
  698. SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
  699. {
  700. int i;
  701. HandleInputEvents(joystick);
  702. /* Deliver ball motion updates */
  703. for (i = 0; i < joystick->nballs; ++i) {
  704. int xrel, yrel;
  705. xrel = joystick->hwdata->balls[i].axis[0];
  706. yrel = joystick->hwdata->balls[i].axis[1];
  707. if (xrel || yrel) {
  708. joystick->hwdata->balls[i].axis[0] = 0;
  709. joystick->hwdata->balls[i].axis[1] = 0;
  710. SDL_PrivateJoystickBall(joystick, (Uint8) i, xrel, yrel);
  711. }
  712. }
  713. }
  714. /* Function to close a joystick after use */
  715. void
  716. SDL_SYS_JoystickClose(SDL_Joystick * joystick)
  717. {
  718. if (joystick->hwdata) {
  719. close(joystick->hwdata->fd);
  720. if (joystick->hwdata->item) {
  721. joystick->hwdata->item->hwdata = NULL;
  722. }
  723. SDL_free(joystick->hwdata->hats);
  724. SDL_free(joystick->hwdata->balls);
  725. SDL_free(joystick->hwdata->fname);
  726. SDL_free(joystick->hwdata);
  727. joystick->hwdata = NULL;
  728. }
  729. joystick->closed = 1;
  730. }
  731. /* Function to perform any system-specific joystick related cleanup */
  732. void
  733. SDL_SYS_JoystickQuit(void)
  734. {
  735. SDL_joylist_item *item = NULL;
  736. SDL_joylist_item *next = NULL;
  737. for (item = SDL_joylist; item; item = next) {
  738. next = item->next;
  739. SDL_free(item->path);
  740. SDL_free(item->name);
  741. SDL_free(item);
  742. }
  743. SDL_joylist = SDL_joylist_tail = NULL;
  744. numjoysticks = 0;
  745. instance_counter = 0;
  746. #if SDL_USE_LIBUDEV
  747. SDL_UDEV_DelCallback(joystick_udev_callback);
  748. SDL_UDEV_Quit();
  749. #endif
  750. }
  751. SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
  752. {
  753. return JoystickByDevIndex(device_index)->guid;
  754. }
  755. SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
  756. {
  757. return joystick->hwdata->guid;
  758. }
  759. #endif /* SDL_JOYSTICK_LINUX */
  760. /* vi: set ts=4 sw=4 expandtab: */