2
0

SDL_syshaptic.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 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_HAPTIC_LINUX
  20. #include "../SDL_syshaptic.h"
  21. #include "../../joystick/SDL_sysjoystick.h" // For the real SDL_Joystick
  22. #include "../../joystick/linux/SDL_sysjoystick_c.h" // For joystick hwdata
  23. #include "../../core/linux/SDL_evdev_capabilities.h"
  24. #include "../../core/linux/SDL_udev.h"
  25. #include <unistd.h> // close
  26. #include <linux/input.h> // Force feedback linux stuff.
  27. #include <fcntl.h> // O_RDWR
  28. #include <limits.h> // INT_MAX
  29. #include <errno.h> // errno
  30. #include <string.h> // strerror
  31. #include <sys/stat.h> // stat
  32. #define MAX_HAPTICS 32 // It's doubtful someone has more then 32 evdev
  33. static bool MaybeAddDevice(const char *path);
  34. #ifdef SDL_USE_LIBUDEV
  35. static bool MaybeRemoveDevice(const char *path);
  36. static void haptic_udev_callback(SDL_UDEV_deviceevent udev_type, int udev_class, const char *devpath);
  37. #endif // SDL_USE_LIBUDEV
  38. /*
  39. * List of available haptic devices.
  40. */
  41. typedef struct SDL_hapticlist_item
  42. {
  43. SDL_HapticID instance_id;
  44. char *fname; // Dev path name (like /dev/input/event1)
  45. SDL_Haptic *haptic; // Associated haptic.
  46. dev_t dev_num;
  47. struct SDL_hapticlist_item *next;
  48. } SDL_hapticlist_item;
  49. /*
  50. * Haptic system hardware data.
  51. */
  52. struct haptic_hwdata
  53. {
  54. int fd; // File descriptor of the device.
  55. char *fname; // Points to the name in SDL_hapticlist.
  56. };
  57. /*
  58. * Haptic system effect data.
  59. */
  60. struct haptic_hweffect
  61. {
  62. struct ff_effect effect; // The linux kernel effect structure.
  63. };
  64. static SDL_hapticlist_item *SDL_hapticlist = NULL;
  65. static SDL_hapticlist_item *SDL_hapticlist_tail = NULL;
  66. static int numhaptics = 0;
  67. #define EV_TEST(ev, f) \
  68. if (test_bit((ev), features)) { \
  69. ret |= (f); \
  70. }
  71. /*
  72. * Test whether a device has haptic properties.
  73. * Returns available properties or 0 if there are none.
  74. */
  75. static Uint32 EV_IsHaptic(int fd)
  76. {
  77. unsigned long features[1 + FF_MAX / sizeof(unsigned long)];
  78. Uint32 ret = 0;
  79. // Ask device for what it has.
  80. if (ioctl(fd, EVIOCGBIT(EV_FF, sizeof(features)), features) < 0) {
  81. SDL_SetError("Haptic: Unable to get device's features: %s", strerror(errno));
  82. return 0;
  83. }
  84. // Convert supported features to SDL_HAPTIC platform-neutral features.
  85. EV_TEST(FF_CONSTANT, SDL_HAPTIC_CONSTANT);
  86. EV_TEST(FF_SINE, SDL_HAPTIC_SINE);
  87. EV_TEST(FF_SQUARE, SDL_HAPTIC_SQUARE);
  88. EV_TEST(FF_TRIANGLE, SDL_HAPTIC_TRIANGLE);
  89. EV_TEST(FF_SAW_UP, SDL_HAPTIC_SAWTOOTHUP);
  90. EV_TEST(FF_SAW_DOWN, SDL_HAPTIC_SAWTOOTHDOWN);
  91. EV_TEST(FF_RAMP, SDL_HAPTIC_RAMP);
  92. EV_TEST(FF_SPRING, SDL_HAPTIC_SPRING);
  93. EV_TEST(FF_FRICTION, SDL_HAPTIC_FRICTION);
  94. EV_TEST(FF_DAMPER, SDL_HAPTIC_DAMPER);
  95. EV_TEST(FF_INERTIA, SDL_HAPTIC_INERTIA);
  96. EV_TEST(FF_CUSTOM, SDL_HAPTIC_CUSTOM);
  97. EV_TEST(FF_GAIN, SDL_HAPTIC_GAIN);
  98. EV_TEST(FF_AUTOCENTER, SDL_HAPTIC_AUTOCENTER);
  99. EV_TEST(FF_RUMBLE, SDL_HAPTIC_LEFTRIGHT);
  100. // Return what it supports.
  101. return ret;
  102. }
  103. /*
  104. * Tests whether a device is a mouse or not.
  105. */
  106. static bool EV_IsMouse(int fd)
  107. {
  108. unsigned long argp[40];
  109. // Ask for supported features.
  110. if (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(argp)), argp) < 0) {
  111. return false;
  112. }
  113. // Currently we only test for BTN_MOUSE which can give fake positives.
  114. if (test_bit(BTN_MOUSE, argp) != 0) {
  115. return true;
  116. }
  117. return true;
  118. }
  119. /*
  120. * Initializes the haptic subsystem by finding available devices.
  121. */
  122. bool SDL_SYS_HapticInit(void)
  123. {
  124. const char joydev_pattern[] = "/dev/input/event%d";
  125. char path[PATH_MAX];
  126. int i, j;
  127. /*
  128. * Limit amount of checks to MAX_HAPTICS since we may or may not have
  129. * permission to some or all devices.
  130. */
  131. i = 0;
  132. for (j = 0; j < MAX_HAPTICS; ++j) {
  133. (void)SDL_snprintf(path, PATH_MAX, joydev_pattern, i++);
  134. MaybeAddDevice(path);
  135. }
  136. #ifdef SDL_USE_LIBUDEV
  137. if (!SDL_UDEV_Init()) {
  138. return SDL_SetError("Could not initialize UDEV");
  139. }
  140. if (!SDL_UDEV_AddCallback(haptic_udev_callback)) {
  141. SDL_UDEV_Quit();
  142. return SDL_SetError("Could not setup haptic <-> udev callback");
  143. }
  144. // Force a scan to build the initial device list
  145. SDL_UDEV_Scan();
  146. #endif // SDL_USE_LIBUDEV
  147. return true;
  148. }
  149. int SDL_SYS_NumHaptics(void)
  150. {
  151. return numhaptics;
  152. }
  153. static SDL_hapticlist_item *HapticByDevIndex(int device_index)
  154. {
  155. SDL_hapticlist_item *item = SDL_hapticlist;
  156. if ((device_index < 0) || (device_index >= numhaptics)) {
  157. return NULL;
  158. }
  159. while (device_index > 0) {
  160. SDL_assert(item != NULL);
  161. --device_index;
  162. item = item->next;
  163. }
  164. return item;
  165. }
  166. static SDL_hapticlist_item *HapticByInstanceID(SDL_HapticID instance_id)
  167. {
  168. SDL_hapticlist_item *item;
  169. for (item = SDL_hapticlist; item; item = item->next) {
  170. if (instance_id == item->instance_id) {
  171. return item;
  172. }
  173. }
  174. return NULL;
  175. }
  176. #ifdef SDL_USE_LIBUDEV
  177. static void haptic_udev_callback(SDL_UDEV_deviceevent udev_type, int udev_class, const char *devpath)
  178. {
  179. if (!devpath || !(udev_class & SDL_UDEV_DEVICE_JOYSTICK)) {
  180. return;
  181. }
  182. switch (udev_type) {
  183. case SDL_UDEV_DEVICEADDED:
  184. MaybeAddDevice(devpath);
  185. break;
  186. case SDL_UDEV_DEVICEREMOVED:
  187. MaybeRemoveDevice(devpath);
  188. break;
  189. default:
  190. break;
  191. }
  192. }
  193. #endif // SDL_USE_LIBUDEV
  194. static bool MaybeAddDevice(const char *path)
  195. {
  196. struct stat sb;
  197. int fd;
  198. Uint32 supported;
  199. SDL_hapticlist_item *item;
  200. if (!path) {
  201. return false;
  202. }
  203. // try to open
  204. fd = open(path, O_RDWR | O_CLOEXEC, 0);
  205. if (fd < 0) {
  206. return false;
  207. }
  208. // get file status
  209. if (fstat(fd, &sb) != 0) {
  210. close(fd);
  211. return false;
  212. }
  213. // check for duplicates
  214. for (item = SDL_hapticlist; item; item = item->next) {
  215. if (item->dev_num == sb.st_rdev) {
  216. close(fd);
  217. return false; // duplicate.
  218. }
  219. }
  220. #ifdef DEBUG_INPUT_EVENTS
  221. printf("Checking %s\n", path);
  222. #endif
  223. // see if it works
  224. supported = EV_IsHaptic(fd);
  225. close(fd);
  226. if (!supported) {
  227. return false;
  228. }
  229. item = (SDL_hapticlist_item *)SDL_calloc(1, sizeof(SDL_hapticlist_item));
  230. if (!item) {
  231. return false;
  232. }
  233. item->instance_id = SDL_GetNextObjectID();
  234. item->fname = SDL_strdup(path);
  235. if (!item->fname) {
  236. SDL_free(item);
  237. return false;
  238. }
  239. item->dev_num = sb.st_rdev;
  240. // TODO: should we add instance IDs?
  241. if (!SDL_hapticlist_tail) {
  242. SDL_hapticlist = SDL_hapticlist_tail = item;
  243. } else {
  244. SDL_hapticlist_tail->next = item;
  245. SDL_hapticlist_tail = item;
  246. }
  247. ++numhaptics;
  248. // !!! TODO: Send a haptic add event?
  249. return true;
  250. }
  251. #ifdef SDL_USE_LIBUDEV
  252. static bool MaybeRemoveDevice(const char *path)
  253. {
  254. SDL_hapticlist_item *item;
  255. SDL_hapticlist_item *prev = NULL;
  256. if (!path) {
  257. return false;
  258. }
  259. for (item = SDL_hapticlist; item; item = item->next) {
  260. // found it, remove it.
  261. if (SDL_strcmp(path, item->fname) == 0) {
  262. const bool result = item->haptic ? true : false;
  263. if (prev) {
  264. prev->next = item->next;
  265. } else {
  266. SDL_assert(SDL_hapticlist == item);
  267. SDL_hapticlist = item->next;
  268. }
  269. if (item == SDL_hapticlist_tail) {
  270. SDL_hapticlist_tail = prev;
  271. }
  272. // Need to decrement the haptic count
  273. --numhaptics;
  274. // !!! TODO: Send a haptic remove event?
  275. SDL_free(item->fname);
  276. SDL_free(item);
  277. return result;
  278. }
  279. prev = item;
  280. }
  281. return false;
  282. }
  283. #endif // SDL_USE_LIBUDEV
  284. /*
  285. * Return the instance ID of a haptic device, does not need to be opened.
  286. */
  287. SDL_HapticID SDL_SYS_HapticInstanceID(int index)
  288. {
  289. SDL_hapticlist_item *item;
  290. item = HapticByDevIndex(index);
  291. if (item) {
  292. return item->instance_id;
  293. }
  294. return 0;
  295. }
  296. /*
  297. * Gets the name from a file descriptor.
  298. */
  299. static const char *SDL_SYS_HapticNameFromFD(int fd)
  300. {
  301. static char namebuf[128];
  302. // We use the evdev name ioctl.
  303. if (ioctl(fd, EVIOCGNAME(sizeof(namebuf)), namebuf) <= 0) {
  304. return NULL;
  305. }
  306. return namebuf;
  307. }
  308. /*
  309. * Return the name of a haptic device, does not need to be opened.
  310. */
  311. const char *SDL_SYS_HapticName(int index)
  312. {
  313. SDL_hapticlist_item *item;
  314. int fd;
  315. const char *name = NULL;
  316. item = HapticByDevIndex(index);
  317. if (item) {
  318. // Open the haptic device.
  319. fd = open(item->fname, O_RDONLY | O_CLOEXEC, 0);
  320. if (fd >= 0) {
  321. name = SDL_SYS_HapticNameFromFD(fd);
  322. if (!name) {
  323. // No name found, return device character device
  324. name = item->fname;
  325. }
  326. close(fd);
  327. }
  328. }
  329. return name;
  330. }
  331. /*
  332. * Opens the haptic device from the file descriptor.
  333. */
  334. static bool SDL_SYS_HapticOpenFromFD(SDL_Haptic *haptic, int fd)
  335. {
  336. // Allocate the hwdata
  337. haptic->hwdata = (struct haptic_hwdata *)
  338. SDL_calloc(1, sizeof(*haptic->hwdata));
  339. if (!haptic->hwdata) {
  340. goto open_err;
  341. }
  342. // Set the data.
  343. haptic->hwdata->fd = fd;
  344. haptic->supported = EV_IsHaptic(fd);
  345. haptic->naxes = 2; // Hardcoded for now, not sure if it's possible to find out.
  346. // Set the effects
  347. if (ioctl(fd, EVIOCGEFFECTS, &haptic->neffects) < 0) {
  348. SDL_SetError("Haptic: Unable to query device memory: %s",
  349. strerror(errno));
  350. goto open_err;
  351. }
  352. haptic->nplaying = haptic->neffects; // Linux makes no distinction.
  353. haptic->effects = (struct haptic_effect *)
  354. SDL_malloc(sizeof(struct haptic_effect) * haptic->neffects);
  355. if (!haptic->effects) {
  356. goto open_err;
  357. }
  358. // Clear the memory
  359. SDL_memset(haptic->effects, 0,
  360. sizeof(struct haptic_effect) * haptic->neffects);
  361. return true;
  362. // Error handling
  363. open_err:
  364. close(fd);
  365. if (haptic->hwdata) {
  366. SDL_free(haptic->hwdata);
  367. haptic->hwdata = NULL;
  368. }
  369. return false;
  370. }
  371. /*
  372. * Opens a haptic device for usage.
  373. */
  374. bool SDL_SYS_HapticOpen(SDL_Haptic *haptic)
  375. {
  376. int fd;
  377. SDL_hapticlist_item *item;
  378. item = HapticByInstanceID(haptic->instance_id);
  379. // Open the character device
  380. fd = open(item->fname, O_RDWR | O_CLOEXEC, 0);
  381. if (fd < 0) {
  382. return SDL_SetError("Haptic: Unable to open %s: %s",
  383. item->fname, strerror(errno));
  384. }
  385. // Try to create the haptic.
  386. if (!SDL_SYS_HapticOpenFromFD(haptic, fd)) {
  387. // Already closes on error.
  388. return false;
  389. }
  390. // Set the fname.
  391. haptic->hwdata->fname = SDL_strdup(item->fname);
  392. return true;
  393. }
  394. /*
  395. * Opens a haptic device from first mouse it finds for usage.
  396. */
  397. int SDL_SYS_HapticMouse(void)
  398. {
  399. int fd;
  400. int device_index = 0;
  401. SDL_hapticlist_item *item;
  402. for (item = SDL_hapticlist; item; item = item->next) {
  403. // Open the device.
  404. fd = open(item->fname, O_RDWR | O_CLOEXEC, 0);
  405. if (fd < 0) {
  406. return SDL_SetError("Haptic: Unable to open %s: %s",
  407. item->fname, strerror(errno));
  408. }
  409. // Is it a mouse?
  410. if (EV_IsMouse(fd)) {
  411. close(fd);
  412. return device_index;
  413. }
  414. close(fd);
  415. ++device_index;
  416. }
  417. return -1;
  418. }
  419. /*
  420. * Checks to see if a joystick has haptic features.
  421. */
  422. bool SDL_SYS_JoystickIsHaptic(SDL_Joystick *joystick)
  423. {
  424. #ifdef SDL_JOYSTICK_LINUX
  425. SDL_AssertJoysticksLocked();
  426. if (joystick->driver != &SDL_LINUX_JoystickDriver) {
  427. return false;
  428. }
  429. if (EV_IsHaptic(joystick->hwdata->fd)) {
  430. return true;
  431. }
  432. #endif
  433. return false;
  434. }
  435. /*
  436. * Checks to see if the haptic device and joystick are in reality the same.
  437. */
  438. bool SDL_SYS_JoystickSameHaptic(SDL_Haptic *haptic, SDL_Joystick *joystick)
  439. {
  440. #ifdef SDL_JOYSTICK_LINUX
  441. SDL_AssertJoysticksLocked();
  442. if (joystick->driver != &SDL_LINUX_JoystickDriver) {
  443. return false;
  444. }
  445. /* We are assuming Linux is using evdev which should trump the old
  446. * joystick methods. */
  447. if (SDL_strcmp(joystick->hwdata->fname, haptic->hwdata->fname) == 0) {
  448. return true;
  449. }
  450. #endif
  451. return false;
  452. }
  453. /*
  454. * Opens a SDL_Haptic from a SDL_Joystick.
  455. */
  456. bool SDL_SYS_HapticOpenFromJoystick(SDL_Haptic *haptic, SDL_Joystick *joystick)
  457. {
  458. #ifdef SDL_JOYSTICK_LINUX
  459. int fd;
  460. SDL_hapticlist_item *item;
  461. const char *name;
  462. SDL_AssertJoysticksLocked();
  463. if (joystick->driver != &SDL_LINUX_JoystickDriver) {
  464. return false;
  465. }
  466. // Find the joystick in the haptic list.
  467. for (item = SDL_hapticlist; item; item = item->next) {
  468. if (SDL_strcmp(item->fname, joystick->hwdata->fname) == 0) {
  469. haptic->instance_id = item->instance_id;
  470. break;
  471. }
  472. }
  473. fd = open(joystick->hwdata->fname, O_RDWR | O_CLOEXEC, 0);
  474. if (fd < 0) {
  475. return SDL_SetError("Haptic: Unable to open %s: %s",
  476. joystick->hwdata->fname, strerror(errno));
  477. }
  478. if (!SDL_SYS_HapticOpenFromFD(haptic, fd)) {
  479. // Already closes on error.
  480. return false;
  481. }
  482. haptic->hwdata->fname = SDL_strdup(joystick->hwdata->fname);
  483. name = SDL_SYS_HapticNameFromFD(fd);
  484. if (name) {
  485. haptic->name = SDL_strdup(name);
  486. }
  487. return true;
  488. #else
  489. return false;
  490. #endif
  491. }
  492. /*
  493. * Closes the haptic device.
  494. */
  495. void SDL_SYS_HapticClose(SDL_Haptic *haptic)
  496. {
  497. if (haptic->hwdata) {
  498. // Free effects.
  499. SDL_free(haptic->effects);
  500. haptic->effects = NULL;
  501. haptic->neffects = 0;
  502. // Clean up
  503. close(haptic->hwdata->fd);
  504. // Free
  505. SDL_free(haptic->hwdata->fname);
  506. SDL_free(haptic->hwdata);
  507. haptic->hwdata = NULL;
  508. }
  509. // Clear the rest.
  510. SDL_memset(haptic, 0, sizeof(SDL_Haptic));
  511. }
  512. /*
  513. * Clean up after system specific haptic stuff
  514. */
  515. void SDL_SYS_HapticQuit(void)
  516. {
  517. SDL_hapticlist_item *item = NULL;
  518. SDL_hapticlist_item *next = NULL;
  519. for (item = SDL_hapticlist; item; item = next) {
  520. next = item->next;
  521. /* Opened and not closed haptics are leaked, this is on purpose.
  522. * Close your haptic devices after usage. */
  523. SDL_free(item->fname);
  524. SDL_free(item);
  525. }
  526. #ifdef SDL_USE_LIBUDEV
  527. SDL_UDEV_DelCallback(haptic_udev_callback);
  528. SDL_UDEV_Quit();
  529. #endif // SDL_USE_LIBUDEV
  530. numhaptics = 0;
  531. SDL_hapticlist = NULL;
  532. SDL_hapticlist_tail = NULL;
  533. }
  534. /*
  535. * Converts an SDL button to a ff_trigger button.
  536. */
  537. static Uint16 SDL_SYS_ToButton(Uint16 button)
  538. {
  539. Uint16 ff_button;
  540. ff_button = 0;
  541. /*
  542. * Not sure what the proper syntax is because this actually isn't implemented
  543. * in the current kernel from what I've seen (2.6.26).
  544. */
  545. if (button != 0) {
  546. ff_button = BTN_GAMEPAD + button - 1;
  547. }
  548. return ff_button;
  549. }
  550. /*
  551. * Initializes the ff_effect usable direction from a SDL_HapticDirection.
  552. */
  553. static bool SDL_SYS_ToDirection(Uint16 *dest, const SDL_HapticDirection *src)
  554. {
  555. Uint32 tmp;
  556. switch (src->type) {
  557. case SDL_HAPTIC_POLAR:
  558. tmp = ((src->dir[0] % 36000) * 0x8000) / 18000; // convert to range [0,0xFFFF]
  559. *dest = (Uint16)tmp;
  560. break;
  561. case SDL_HAPTIC_SPHERICAL:
  562. /*
  563. We convert to polar, because that's the only supported direction on Linux.
  564. The first value of a spherical direction is practically the same as a
  565. Polar direction, except that we have to add 90 degrees. It is the angle
  566. from EAST {1,0} towards SOUTH {0,1}.
  567. --> add 9000
  568. --> finally convert to [0,0xFFFF] as in case SDL_HAPTIC_POLAR.
  569. */
  570. tmp = ((src->dir[0]) + 9000) % 36000; // Convert to polars
  571. tmp = (tmp * 0x8000) / 18000; // convert to range [0,0xFFFF]
  572. *dest = (Uint16)tmp;
  573. break;
  574. case SDL_HAPTIC_CARTESIAN:
  575. if (!src->dir[1]) {
  576. *dest = (src->dir[0] >= 0 ? 0x4000 : 0xC000);
  577. } else if (!src->dir[0]) {
  578. *dest = (src->dir[1] >= 0 ? 0x8000 : 0);
  579. } else {
  580. float f = SDL_atan2f(src->dir[1], src->dir[0]); // Ideally we'd use fixed point math instead of floats...
  581. /*
  582. SDL_atan2 takes the parameters: Y-axis-value and X-axis-value (in that order)
  583. - Y-axis-value is the second coordinate (from center to SOUTH)
  584. - X-axis-value is the first coordinate (from center to EAST)
  585. We add 36000, because SDL_atan2 also returns negative values. Then we practically
  586. have the first spherical value. Therefore we proceed as in case
  587. SDL_HAPTIC_SPHERICAL and add another 9000 to get the polar value.
  588. --> add 45000 in total
  589. --> finally convert to [0,0xFFFF] as in case SDL_HAPTIC_POLAR.
  590. */
  591. tmp = (((Sint32)(f * 18000.0 / SDL_PI_D)) + 45000) % 36000;
  592. tmp = (tmp * 0x8000) / 18000; // convert to range [0,0xFFFF]
  593. *dest = (Uint16)tmp;
  594. }
  595. break;
  596. case SDL_HAPTIC_STEERING_AXIS:
  597. *dest = 0x4000;
  598. break;
  599. default:
  600. return SDL_SetError("Haptic: Unsupported direction type.");
  601. }
  602. return true;
  603. }
  604. #define CLAMP(x) (((x) > 32767) ? 32767 : x)
  605. /*
  606. * Initializes the Linux effect struct from a haptic_effect.
  607. * Values above 32767 (for unsigned) are unspecified so we must clamp.
  608. */
  609. static bool SDL_SYS_ToFFEffect(struct ff_effect *dest, const SDL_HapticEffect *src)
  610. {
  611. const SDL_HapticConstant *constant;
  612. const SDL_HapticPeriodic *periodic;
  613. const SDL_HapticCondition *condition;
  614. const SDL_HapticRamp *ramp;
  615. const SDL_HapticLeftRight *leftright;
  616. // Clear up
  617. SDL_memset(dest, 0, sizeof(struct ff_effect));
  618. switch (src->type) {
  619. case SDL_HAPTIC_CONSTANT:
  620. constant = &src->constant;
  621. // Header
  622. dest->type = FF_CONSTANT;
  623. if (!SDL_SYS_ToDirection(&dest->direction, &constant->direction)) {
  624. return false;
  625. }
  626. // Replay
  627. dest->replay.length = (constant->length == SDL_HAPTIC_INFINITY) ? 0 : CLAMP(constant->length);
  628. dest->replay.delay = CLAMP(constant->delay);
  629. // Trigger
  630. dest->trigger.button = SDL_SYS_ToButton(constant->button);
  631. dest->trigger.interval = CLAMP(constant->interval);
  632. // Constant
  633. dest->u.constant.level = constant->level;
  634. // Envelope
  635. dest->u.constant.envelope.attack_length =
  636. CLAMP(constant->attack_length);
  637. dest->u.constant.envelope.attack_level =
  638. CLAMP(constant->attack_level);
  639. dest->u.constant.envelope.fade_length = CLAMP(constant->fade_length);
  640. dest->u.constant.envelope.fade_level = CLAMP(constant->fade_level);
  641. break;
  642. case SDL_HAPTIC_SINE:
  643. case SDL_HAPTIC_SQUARE:
  644. case SDL_HAPTIC_TRIANGLE:
  645. case SDL_HAPTIC_SAWTOOTHUP:
  646. case SDL_HAPTIC_SAWTOOTHDOWN:
  647. periodic = &src->periodic;
  648. // Header
  649. dest->type = FF_PERIODIC;
  650. if (!SDL_SYS_ToDirection(&dest->direction, &periodic->direction)) {
  651. return false;
  652. }
  653. // Replay
  654. dest->replay.length = (periodic->length == SDL_HAPTIC_INFINITY) ? 0 : CLAMP(periodic->length);
  655. dest->replay.delay = CLAMP(periodic->delay);
  656. // Trigger
  657. dest->trigger.button = SDL_SYS_ToButton(periodic->button);
  658. dest->trigger.interval = CLAMP(periodic->interval);
  659. // Periodic
  660. if (periodic->type == SDL_HAPTIC_SINE) {
  661. dest->u.periodic.waveform = FF_SINE;
  662. } else if (periodic->type == SDL_HAPTIC_SQUARE) {
  663. dest->u.periodic.waveform = FF_SQUARE;
  664. } else if (periodic->type == SDL_HAPTIC_TRIANGLE) {
  665. dest->u.periodic.waveform = FF_TRIANGLE;
  666. } else if (periodic->type == SDL_HAPTIC_SAWTOOTHUP) {
  667. dest->u.periodic.waveform = FF_SAW_UP;
  668. } else if (periodic->type == SDL_HAPTIC_SAWTOOTHDOWN) {
  669. dest->u.periodic.waveform = FF_SAW_DOWN;
  670. }
  671. dest->u.periodic.period = CLAMP(periodic->period);
  672. dest->u.periodic.magnitude = periodic->magnitude;
  673. dest->u.periodic.offset = periodic->offset;
  674. // Linux phase is defined in interval "[0x0000, 0x10000[", corresponds with "[0deg, 360deg[" phase shift.
  675. dest->u.periodic.phase = ((Uint32)periodic->phase * 0x10000U) / 36000;
  676. // Envelope
  677. dest->u.periodic.envelope.attack_length =
  678. CLAMP(periodic->attack_length);
  679. dest->u.periodic.envelope.attack_level =
  680. CLAMP(periodic->attack_level);
  681. dest->u.periodic.envelope.fade_length = CLAMP(periodic->fade_length);
  682. dest->u.periodic.envelope.fade_level = CLAMP(periodic->fade_level);
  683. break;
  684. case SDL_HAPTIC_SPRING:
  685. case SDL_HAPTIC_DAMPER:
  686. case SDL_HAPTIC_INERTIA:
  687. case SDL_HAPTIC_FRICTION:
  688. condition = &src->condition;
  689. // Header
  690. if (condition->type == SDL_HAPTIC_SPRING) {
  691. dest->type = FF_SPRING;
  692. } else if (condition->type == SDL_HAPTIC_DAMPER) {
  693. dest->type = FF_DAMPER;
  694. } else if (condition->type == SDL_HAPTIC_INERTIA) {
  695. dest->type = FF_INERTIA;
  696. } else if (condition->type == SDL_HAPTIC_FRICTION) {
  697. dest->type = FF_FRICTION;
  698. }
  699. if (!SDL_SYS_ToDirection(&dest->direction, &condition->direction)) {
  700. return false;
  701. }
  702. // Replay
  703. dest->replay.length = (condition->length == SDL_HAPTIC_INFINITY) ? 0 : CLAMP(condition->length);
  704. dest->replay.delay = CLAMP(condition->delay);
  705. // Trigger
  706. dest->trigger.button = SDL_SYS_ToButton(condition->button);
  707. dest->trigger.interval = CLAMP(condition->interval);
  708. // Condition
  709. // X axis
  710. dest->u.condition[0].right_saturation = condition->right_sat[0];
  711. dest->u.condition[0].left_saturation = condition->left_sat[0];
  712. dest->u.condition[0].right_coeff = condition->right_coeff[0];
  713. dest->u.condition[0].left_coeff = condition->left_coeff[0];
  714. dest->u.condition[0].deadband = condition->deadband[0];
  715. dest->u.condition[0].center = condition->center[0];
  716. // Y axis
  717. dest->u.condition[1].right_saturation = condition->right_sat[1];
  718. dest->u.condition[1].left_saturation = condition->left_sat[1];
  719. dest->u.condition[1].right_coeff = condition->right_coeff[1];
  720. dest->u.condition[1].left_coeff = condition->left_coeff[1];
  721. dest->u.condition[1].deadband = condition->deadband[1];
  722. dest->u.condition[1].center = condition->center[1];
  723. /*
  724. * There is no envelope in the linux force feedback api for conditions.
  725. */
  726. break;
  727. case SDL_HAPTIC_RAMP:
  728. ramp = &src->ramp;
  729. // Header
  730. dest->type = FF_RAMP;
  731. if (!SDL_SYS_ToDirection(&dest->direction, &ramp->direction)) {
  732. return false;
  733. }
  734. // Replay
  735. dest->replay.length = (ramp->length == SDL_HAPTIC_INFINITY) ? 0 : CLAMP(ramp->length);
  736. dest->replay.delay = CLAMP(ramp->delay);
  737. // Trigger
  738. dest->trigger.button = SDL_SYS_ToButton(ramp->button);
  739. dest->trigger.interval = CLAMP(ramp->interval);
  740. // Ramp
  741. dest->u.ramp.start_level = ramp->start;
  742. dest->u.ramp.end_level = ramp->end;
  743. // Envelope
  744. dest->u.ramp.envelope.attack_length = CLAMP(ramp->attack_length);
  745. dest->u.ramp.envelope.attack_level = CLAMP(ramp->attack_level);
  746. dest->u.ramp.envelope.fade_length = CLAMP(ramp->fade_length);
  747. dest->u.ramp.envelope.fade_level = CLAMP(ramp->fade_level);
  748. break;
  749. case SDL_HAPTIC_LEFTRIGHT:
  750. leftright = &src->leftright;
  751. // Header
  752. dest->type = FF_RUMBLE;
  753. dest->direction = 0x4000;
  754. // Replay
  755. dest->replay.length = (leftright->length == SDL_HAPTIC_INFINITY) ? 0 : CLAMP(leftright->length);
  756. // Trigger
  757. dest->trigger.button = 0;
  758. dest->trigger.interval = 0;
  759. // Rumble (Linux expects 0-65535, so multiply by 2)
  760. dest->u.rumble.strong_magnitude = CLAMP(leftright->large_magnitude) * 2;
  761. dest->u.rumble.weak_magnitude = CLAMP(leftright->small_magnitude) * 2;
  762. break;
  763. default:
  764. return SDL_SetError("Haptic: Unknown effect type.");
  765. }
  766. return true;
  767. }
  768. /*
  769. * Creates a new haptic effect.
  770. */
  771. bool SDL_SYS_HapticNewEffect(SDL_Haptic *haptic, struct haptic_effect *effect,
  772. const SDL_HapticEffect *base)
  773. {
  774. struct ff_effect *linux_effect;
  775. // Allocate the hardware effect
  776. effect->hweffect = (struct haptic_hweffect *)
  777. SDL_calloc(1, sizeof(struct haptic_hweffect));
  778. if (!effect->hweffect) {
  779. return false;
  780. }
  781. // Prepare the ff_effect
  782. linux_effect = &effect->hweffect->effect;
  783. if (!SDL_SYS_ToFFEffect(linux_effect, base)) {
  784. goto new_effect_err;
  785. }
  786. linux_effect->id = -1; // Have the kernel give it an id
  787. // Upload the effect
  788. if (ioctl(haptic->hwdata->fd, EVIOCSFF, linux_effect) < 0) {
  789. SDL_SetError("Haptic: Error uploading effect to the device: %s",
  790. strerror(errno));
  791. goto new_effect_err;
  792. }
  793. return true;
  794. new_effect_err:
  795. SDL_free(effect->hweffect);
  796. effect->hweffect = NULL;
  797. return false;
  798. }
  799. /*
  800. * Updates an effect.
  801. *
  802. * Note: Dynamically updating the direction can in some cases force
  803. * the effect to restart and run once.
  804. */
  805. bool SDL_SYS_HapticUpdateEffect(SDL_Haptic *haptic,
  806. struct haptic_effect *effect,
  807. const SDL_HapticEffect *data)
  808. {
  809. struct ff_effect linux_effect;
  810. // Create the new effect
  811. if (!SDL_SYS_ToFFEffect(&linux_effect, data)) {
  812. return false;
  813. }
  814. linux_effect.id = effect->hweffect->effect.id;
  815. // See if it can be uploaded.
  816. if (ioctl(haptic->hwdata->fd, EVIOCSFF, &linux_effect) < 0) {
  817. return SDL_SetError("Haptic: Error updating the effect: %s",
  818. strerror(errno));
  819. }
  820. // Copy the new effect into memory.
  821. SDL_memcpy(&effect->hweffect->effect, &linux_effect,
  822. sizeof(struct ff_effect));
  823. return true;
  824. }
  825. /*
  826. * Runs an effect.
  827. */
  828. bool SDL_SYS_HapticRunEffect(SDL_Haptic *haptic, struct haptic_effect *effect,
  829. Uint32 iterations)
  830. {
  831. struct input_event run;
  832. // Prepare to run the effect
  833. run.type = EV_FF;
  834. run.code = effect->hweffect->effect.id;
  835. // We don't actually have infinity here, so we just do INT_MAX which is pretty damn close.
  836. run.value = (iterations > INT_MAX) ? INT_MAX : iterations;
  837. if (write(haptic->hwdata->fd, (const void *)&run, sizeof(run)) < 0) {
  838. return SDL_SetError("Haptic: Unable to run the effect: %s", strerror(errno));
  839. }
  840. return true;
  841. }
  842. /*
  843. * Stops an effect.
  844. */
  845. bool SDL_SYS_HapticStopEffect(SDL_Haptic *haptic, struct haptic_effect *effect)
  846. {
  847. struct input_event stop;
  848. stop.type = EV_FF;
  849. stop.code = effect->hweffect->effect.id;
  850. stop.value = 0;
  851. if (write(haptic->hwdata->fd, (const void *)&stop, sizeof(stop)) < 0) {
  852. return SDL_SetError("Haptic: Unable to stop the effect: %s",
  853. strerror(errno));
  854. }
  855. return true;
  856. }
  857. /*
  858. * Frees the effect.
  859. */
  860. void SDL_SYS_HapticDestroyEffect(SDL_Haptic *haptic, struct haptic_effect *effect)
  861. {
  862. if (ioctl(haptic->hwdata->fd, EVIOCRMFF, effect->hweffect->effect.id) < 0) {
  863. SDL_SetError("Haptic: Error removing the effect from the device: %s",
  864. strerror(errno));
  865. }
  866. SDL_free(effect->hweffect);
  867. effect->hweffect = NULL;
  868. }
  869. /*
  870. * Gets the status of a haptic effect.
  871. */
  872. int SDL_SYS_HapticGetEffectStatus(SDL_Haptic *haptic,
  873. struct haptic_effect *effect)
  874. {
  875. #if 0 // Not supported atm.
  876. struct input_event ie;
  877. ie.type = EV_FF;
  878. ie.type = EV_FF_STATUS;
  879. ie.code = effect->hweffect->effect.id;
  880. if (write(haptic->hwdata->fd, &ie, sizeof(ie)) < 0) {
  881. SDL_SetError("Haptic: Error getting device status.");
  882. return -1;
  883. }
  884. return 1;
  885. #endif
  886. SDL_Unsupported();
  887. return -1;
  888. }
  889. /*
  890. * Sets the gain.
  891. */
  892. bool SDL_SYS_HapticSetGain(SDL_Haptic *haptic, int gain)
  893. {
  894. struct input_event ie;
  895. ie.type = EV_FF;
  896. ie.code = FF_GAIN;
  897. ie.value = (0xFFFFUL * gain) / 100;
  898. if (write(haptic->hwdata->fd, &ie, sizeof(ie)) < 0) {
  899. return SDL_SetError("Haptic: Error setting gain: %s", strerror(errno));
  900. }
  901. return true;
  902. }
  903. /*
  904. * Sets the autocentering.
  905. */
  906. bool SDL_SYS_HapticSetAutocenter(SDL_Haptic *haptic, int autocenter)
  907. {
  908. struct input_event ie;
  909. ie.type = EV_FF;
  910. ie.code = FF_AUTOCENTER;
  911. ie.value = (0xFFFFUL * autocenter) / 100;
  912. if (write(haptic->hwdata->fd, &ie, sizeof(ie)) < 0) {
  913. return SDL_SetError("Haptic: Error setting autocenter: %s", strerror(errno));
  914. }
  915. return true;
  916. }
  917. /*
  918. * Pausing is not supported atm by linux.
  919. */
  920. bool SDL_SYS_HapticPause(SDL_Haptic *haptic)
  921. {
  922. return SDL_Unsupported();
  923. }
  924. /*
  925. * Unpausing is not supported atm by linux.
  926. */
  927. bool SDL_SYS_HapticResume(SDL_Haptic *haptic)
  928. {
  929. return SDL_Unsupported();
  930. }
  931. /*
  932. * Stops all the currently playing effects.
  933. */
  934. bool SDL_SYS_HapticStopAll(SDL_Haptic *haptic)
  935. {
  936. int i, ret;
  937. // Linux does not support this natively so we have to loop.
  938. for (i = 0; i < haptic->neffects; i++) {
  939. if (haptic->effects[i].hweffect != NULL) {
  940. ret = SDL_SYS_HapticStopEffect(haptic, &haptic->effects[i]);
  941. if (ret < 0) {
  942. return SDL_SetError("Haptic: Error while trying to stop all playing effects.");
  943. }
  944. }
  945. }
  946. return true;
  947. }
  948. #endif // SDL_HAPTIC_LINUX