SDL_syshaptic.c 31 KB

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