SDL_haptic.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2015 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. #include "SDL_syshaptic.h"
  20. #include "SDL_haptic_c.h"
  21. #include "../joystick/SDL_joystick_c.h" /* For SDL_PrivateJoystickValid */
  22. #include "SDL_assert.h"
  23. SDL_Haptic *SDL_haptics = NULL;
  24. /*
  25. * Initializes the Haptic devices.
  26. */
  27. int
  28. SDL_HapticInit(void)
  29. {
  30. int status;
  31. status = SDL_SYS_HapticInit();
  32. if (status >= 0) {
  33. status = 0;
  34. }
  35. return status;
  36. }
  37. /*
  38. * Checks to see if the haptic device is valid
  39. */
  40. static int
  41. ValidHaptic(SDL_Haptic * haptic)
  42. {
  43. int valid;
  44. SDL_Haptic *hapticlist;
  45. valid = 0;
  46. if (haptic != NULL) {
  47. hapticlist = SDL_haptics;
  48. while ( hapticlist )
  49. {
  50. if (hapticlist == haptic) {
  51. valid = 1;
  52. break;
  53. }
  54. hapticlist = hapticlist->next;
  55. }
  56. }
  57. /* Create the error here. */
  58. if (valid == 0) {
  59. SDL_SetError("Haptic: Invalid haptic device identifier");
  60. }
  61. return valid;
  62. }
  63. /*
  64. * Returns the number of available devices.
  65. */
  66. int
  67. SDL_NumHaptics(void)
  68. {
  69. return SDL_SYS_NumHaptics();
  70. }
  71. /*
  72. * Gets the name of a Haptic device by index.
  73. */
  74. const char *
  75. SDL_HapticName(int device_index)
  76. {
  77. if ((device_index < 0) || (device_index >= SDL_NumHaptics())) {
  78. SDL_SetError("Haptic: There are %d haptic devices available",
  79. SDL_NumHaptics());
  80. return NULL;
  81. }
  82. return SDL_SYS_HapticName(device_index);
  83. }
  84. /*
  85. * Opens a Haptic device.
  86. */
  87. SDL_Haptic *
  88. SDL_HapticOpen(int device_index)
  89. {
  90. SDL_Haptic *haptic;
  91. SDL_Haptic *hapticlist;
  92. if ((device_index < 0) || (device_index >= SDL_NumHaptics())) {
  93. SDL_SetError("Haptic: There are %d haptic devices available",
  94. SDL_NumHaptics());
  95. return NULL;
  96. }
  97. hapticlist = SDL_haptics;
  98. /* If the haptic is already open, return it
  99. * TODO: Should we create haptic instance IDs like the Joystick API?
  100. */
  101. while ( hapticlist )
  102. {
  103. if (device_index == hapticlist->index) {
  104. haptic = hapticlist;
  105. ++haptic->ref_count;
  106. return haptic;
  107. }
  108. hapticlist = hapticlist->next;
  109. }
  110. /* Create the haptic device */
  111. haptic = (SDL_Haptic *) SDL_malloc((sizeof *haptic));
  112. if (haptic == NULL) {
  113. SDL_OutOfMemory();
  114. return NULL;
  115. }
  116. /* Initialize the haptic device */
  117. SDL_memset(haptic, 0, (sizeof *haptic));
  118. haptic->rumble_id = -1;
  119. haptic->index = device_index;
  120. if (SDL_SYS_HapticOpen(haptic) < 0) {
  121. SDL_free(haptic);
  122. return NULL;
  123. }
  124. /* Add haptic to list */
  125. ++haptic->ref_count;
  126. /* Link the haptic in the list */
  127. haptic->next = SDL_haptics;
  128. SDL_haptics = haptic;
  129. /* Disable autocenter and set gain to max. */
  130. if (haptic->supported & SDL_HAPTIC_GAIN)
  131. SDL_HapticSetGain(haptic, 100);
  132. if (haptic->supported & SDL_HAPTIC_AUTOCENTER)
  133. SDL_HapticSetAutocenter(haptic, 0);
  134. return haptic;
  135. }
  136. /*
  137. * Returns 1 if the device has been opened.
  138. */
  139. int
  140. SDL_HapticOpened(int device_index)
  141. {
  142. int opened;
  143. SDL_Haptic *hapticlist;
  144. /* Make sure it's valid. */
  145. if ((device_index < 0) || (device_index >= SDL_NumHaptics())) {
  146. SDL_SetError("Haptic: There are %d haptic devices available",
  147. SDL_NumHaptics());
  148. return 0;
  149. }
  150. opened = 0;
  151. hapticlist = SDL_haptics;
  152. /* TODO Should this use an instance ID? */
  153. while ( hapticlist )
  154. {
  155. if (hapticlist->index == (Uint8) device_index) {
  156. opened = 1;
  157. break;
  158. }
  159. hapticlist = hapticlist->next;
  160. }
  161. return opened;
  162. }
  163. /*
  164. * Returns the index to a haptic device.
  165. */
  166. int
  167. SDL_HapticIndex(SDL_Haptic * haptic)
  168. {
  169. if (!ValidHaptic(haptic)) {
  170. return -1;
  171. }
  172. return haptic->index;
  173. }
  174. /*
  175. * Returns SDL_TRUE if mouse is haptic, SDL_FALSE if it isn't.
  176. */
  177. int
  178. SDL_MouseIsHaptic(void)
  179. {
  180. if (SDL_SYS_HapticMouse() < 0)
  181. return SDL_FALSE;
  182. return SDL_TRUE;
  183. }
  184. /*
  185. * Returns the haptic device if mouse is haptic or NULL elsewise.
  186. */
  187. SDL_Haptic *
  188. SDL_HapticOpenFromMouse(void)
  189. {
  190. int device_index;
  191. device_index = SDL_SYS_HapticMouse();
  192. if (device_index < 0) {
  193. SDL_SetError("Haptic: Mouse isn't a haptic device.");
  194. return NULL;
  195. }
  196. return SDL_HapticOpen(device_index);
  197. }
  198. /*
  199. * Returns SDL_TRUE if joystick has haptic features.
  200. */
  201. int
  202. SDL_JoystickIsHaptic(SDL_Joystick * joystick)
  203. {
  204. int ret;
  205. /* Must be a valid joystick */
  206. if (!SDL_PrivateJoystickValid(joystick)) {
  207. return -1;
  208. }
  209. ret = SDL_SYS_JoystickIsHaptic(joystick);
  210. if (ret > 0)
  211. return SDL_TRUE;
  212. else if (ret == 0)
  213. return SDL_FALSE;
  214. else
  215. return -1;
  216. }
  217. /*
  218. * Opens a haptic device from a joystick.
  219. */
  220. SDL_Haptic *
  221. SDL_HapticOpenFromJoystick(SDL_Joystick * joystick)
  222. {
  223. SDL_Haptic *haptic;
  224. SDL_Haptic *hapticlist;
  225. /* Make sure there is room. */
  226. if (SDL_NumHaptics() <= 0) {
  227. SDL_SetError("Haptic: There are %d haptic devices available",
  228. SDL_NumHaptics());
  229. return NULL;
  230. }
  231. /* Must be a valid joystick */
  232. if (!SDL_PrivateJoystickValid(joystick)) {
  233. SDL_SetError("Haptic: Joystick isn't valid.");
  234. return NULL;
  235. }
  236. /* Joystick must be haptic */
  237. if (SDL_SYS_JoystickIsHaptic(joystick) <= 0) {
  238. SDL_SetError("Haptic: Joystick isn't a haptic device.");
  239. return NULL;
  240. }
  241. hapticlist = SDL_haptics;
  242. /* Check to see if joystick's haptic is already open */
  243. while ( hapticlist )
  244. {
  245. if (SDL_SYS_JoystickSameHaptic(hapticlist, joystick)) {
  246. haptic = hapticlist;
  247. ++haptic->ref_count;
  248. return haptic;
  249. }
  250. hapticlist = hapticlist->next;
  251. }
  252. /* Create the haptic device */
  253. haptic = (SDL_Haptic *) SDL_malloc((sizeof *haptic));
  254. if (haptic == NULL) {
  255. SDL_OutOfMemory();
  256. return NULL;
  257. }
  258. /* Initialize the haptic device */
  259. SDL_memset(haptic, 0, sizeof(SDL_Haptic));
  260. haptic->rumble_id = -1;
  261. if (SDL_SYS_HapticOpenFromJoystick(haptic, joystick) < 0) {
  262. SDL_free(haptic);
  263. return NULL;
  264. }
  265. /* Add haptic to list */
  266. ++haptic->ref_count;
  267. /* Link the haptic in the list */
  268. haptic->next = SDL_haptics;
  269. SDL_haptics = haptic;
  270. return haptic;
  271. }
  272. /*
  273. * Closes a SDL_Haptic device.
  274. */
  275. void
  276. SDL_HapticClose(SDL_Haptic * haptic)
  277. {
  278. int i;
  279. SDL_Haptic *hapticlist;
  280. SDL_Haptic *hapticlistprev;
  281. /* Must be valid */
  282. if (!ValidHaptic(haptic)) {
  283. return;
  284. }
  285. /* Check if it's still in use */
  286. if (--haptic->ref_count > 0) {
  287. return;
  288. }
  289. /* Close it, properly removing effects if needed */
  290. for (i = 0; i < haptic->neffects; i++) {
  291. if (haptic->effects[i].hweffect != NULL) {
  292. SDL_HapticDestroyEffect(haptic, i);
  293. }
  294. }
  295. SDL_SYS_HapticClose(haptic);
  296. /* Remove from the list */
  297. hapticlist = SDL_haptics;
  298. hapticlistprev = NULL;
  299. while ( hapticlist )
  300. {
  301. if (haptic == hapticlist)
  302. {
  303. if ( hapticlistprev )
  304. {
  305. /* unlink this entry */
  306. hapticlistprev->next = hapticlist->next;
  307. }
  308. else
  309. {
  310. SDL_haptics = haptic->next;
  311. }
  312. break;
  313. }
  314. hapticlistprev = hapticlist;
  315. hapticlist = hapticlist->next;
  316. }
  317. /* Free */
  318. SDL_free(haptic);
  319. }
  320. /*
  321. * Cleans up after the subsystem.
  322. */
  323. void
  324. SDL_HapticQuit(void)
  325. {
  326. SDL_SYS_HapticQuit();
  327. SDL_assert(SDL_haptics == NULL);
  328. SDL_haptics = NULL;
  329. }
  330. /*
  331. * Returns the number of effects a haptic device has.
  332. */
  333. int
  334. SDL_HapticNumEffects(SDL_Haptic * haptic)
  335. {
  336. if (!ValidHaptic(haptic)) {
  337. return -1;
  338. }
  339. return haptic->neffects;
  340. }
  341. /*
  342. * Returns the number of effects a haptic device can play.
  343. */
  344. int
  345. SDL_HapticNumEffectsPlaying(SDL_Haptic * haptic)
  346. {
  347. if (!ValidHaptic(haptic)) {
  348. return -1;
  349. }
  350. return haptic->nplaying;
  351. }
  352. /*
  353. * Returns supported effects by the device.
  354. */
  355. unsigned int
  356. SDL_HapticQuery(SDL_Haptic * haptic)
  357. {
  358. if (!ValidHaptic(haptic)) {
  359. return 0; /* same as if no effects were supported */
  360. }
  361. return haptic->supported;
  362. }
  363. /*
  364. * Returns the number of axis on the device.
  365. */
  366. int
  367. SDL_HapticNumAxes(SDL_Haptic * haptic)
  368. {
  369. if (!ValidHaptic(haptic)) {
  370. return -1;
  371. }
  372. return haptic->naxes;
  373. }
  374. /*
  375. * Checks to see if the device can support the effect.
  376. */
  377. int
  378. SDL_HapticEffectSupported(SDL_Haptic * haptic, SDL_HapticEffect * effect)
  379. {
  380. if (!ValidHaptic(haptic)) {
  381. return -1;
  382. }
  383. if ((haptic->supported & effect->type) != 0)
  384. return SDL_TRUE;
  385. return SDL_FALSE;
  386. }
  387. /*
  388. * Creates a new haptic effect.
  389. */
  390. int
  391. SDL_HapticNewEffect(SDL_Haptic * haptic, SDL_HapticEffect * effect)
  392. {
  393. int i;
  394. /* Check for device validity. */
  395. if (!ValidHaptic(haptic)) {
  396. return -1;
  397. }
  398. /* Check to see if effect is supported */
  399. if (SDL_HapticEffectSupported(haptic, effect) == SDL_FALSE) {
  400. return SDL_SetError("Haptic: Effect not supported by haptic device.");
  401. }
  402. /* See if there's a free slot */
  403. for (i = 0; i < haptic->neffects; i++) {
  404. if (haptic->effects[i].hweffect == NULL) {
  405. /* Now let the backend create the real effect */
  406. if (SDL_SYS_HapticNewEffect(haptic, &haptic->effects[i], effect)
  407. != 0) {
  408. return -1; /* Backend failed to create effect */
  409. }
  410. SDL_memcpy(&haptic->effects[i].effect, effect,
  411. sizeof(SDL_HapticEffect));
  412. return i;
  413. }
  414. }
  415. return SDL_SetError("Haptic: Device has no free space left.");
  416. }
  417. /*
  418. * Checks to see if an effect is valid.
  419. */
  420. static int
  421. ValidEffect(SDL_Haptic * haptic, int effect)
  422. {
  423. if ((effect < 0) || (effect >= haptic->neffects)) {
  424. SDL_SetError("Haptic: Invalid effect identifier.");
  425. return 0;
  426. }
  427. return 1;
  428. }
  429. /*
  430. * Updates an effect.
  431. */
  432. int
  433. SDL_HapticUpdateEffect(SDL_Haptic * haptic, int effect,
  434. SDL_HapticEffect * data)
  435. {
  436. if (!ValidHaptic(haptic) || !ValidEffect(haptic, effect)) {
  437. return -1;
  438. }
  439. /* Can't change type dynamically. */
  440. if (data->type != haptic->effects[effect].effect.type) {
  441. return SDL_SetError("Haptic: Updating effect type is illegal.");
  442. }
  443. /* Updates the effect */
  444. if (SDL_SYS_HapticUpdateEffect(haptic, &haptic->effects[effect], data) <
  445. 0) {
  446. return -1;
  447. }
  448. SDL_memcpy(&haptic->effects[effect].effect, data,
  449. sizeof(SDL_HapticEffect));
  450. return 0;
  451. }
  452. /*
  453. * Runs the haptic effect on the device.
  454. */
  455. int
  456. SDL_HapticRunEffect(SDL_Haptic * haptic, int effect, Uint32 iterations)
  457. {
  458. if (!ValidHaptic(haptic) || !ValidEffect(haptic, effect)) {
  459. return -1;
  460. }
  461. /* Run the effect */
  462. if (SDL_SYS_HapticRunEffect(haptic, &haptic->effects[effect], iterations)
  463. < 0) {
  464. return -1;
  465. }
  466. return 0;
  467. }
  468. /*
  469. * Stops the haptic effect on the device.
  470. */
  471. int
  472. SDL_HapticStopEffect(SDL_Haptic * haptic, int effect)
  473. {
  474. if (!ValidHaptic(haptic) || !ValidEffect(haptic, effect)) {
  475. return -1;
  476. }
  477. /* Stop the effect */
  478. if (SDL_SYS_HapticStopEffect(haptic, &haptic->effects[effect]) < 0) {
  479. return -1;
  480. }
  481. return 0;
  482. }
  483. /*
  484. * Gets rid of a haptic effect.
  485. */
  486. void
  487. SDL_HapticDestroyEffect(SDL_Haptic * haptic, int effect)
  488. {
  489. if (!ValidHaptic(haptic) || !ValidEffect(haptic, effect)) {
  490. return;
  491. }
  492. /* Not allocated */
  493. if (haptic->effects[effect].hweffect == NULL) {
  494. return;
  495. }
  496. SDL_SYS_HapticDestroyEffect(haptic, &haptic->effects[effect]);
  497. }
  498. /*
  499. * Gets the status of a haptic effect.
  500. */
  501. int
  502. SDL_HapticGetEffectStatus(SDL_Haptic * haptic, int effect)
  503. {
  504. if (!ValidHaptic(haptic) || !ValidEffect(haptic, effect)) {
  505. return -1;
  506. }
  507. if ((haptic->supported & SDL_HAPTIC_STATUS) == 0) {
  508. return SDL_SetError("Haptic: Device does not support status queries.");
  509. }
  510. return SDL_SYS_HapticGetEffectStatus(haptic, &haptic->effects[effect]);
  511. }
  512. /*
  513. * Sets the global gain of the device.
  514. */
  515. int
  516. SDL_HapticSetGain(SDL_Haptic * haptic, int gain)
  517. {
  518. const char *env;
  519. int real_gain, max_gain;
  520. if (!ValidHaptic(haptic)) {
  521. return -1;
  522. }
  523. if ((haptic->supported & SDL_HAPTIC_GAIN) == 0) {
  524. return SDL_SetError("Haptic: Device does not support setting gain.");
  525. }
  526. if ((gain < 0) || (gain > 100)) {
  527. return SDL_SetError("Haptic: Gain must be between 0 and 100.");
  528. }
  529. /* We use the envvar to get the maximum gain. */
  530. env = SDL_getenv("SDL_HAPTIC_GAIN_MAX");
  531. if (env != NULL) {
  532. max_gain = SDL_atoi(env);
  533. /* Check for sanity. */
  534. if (max_gain < 0)
  535. max_gain = 0;
  536. else if (max_gain > 100)
  537. max_gain = 100;
  538. /* We'll scale it linearly with SDL_HAPTIC_GAIN_MAX */
  539. real_gain = (gain * max_gain) / 100;
  540. } else {
  541. real_gain = gain;
  542. }
  543. if (SDL_SYS_HapticSetGain(haptic, real_gain) < 0) {
  544. return -1;
  545. }
  546. return 0;
  547. }
  548. /*
  549. * Makes the device autocenter, 0 disables.
  550. */
  551. int
  552. SDL_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter)
  553. {
  554. if (!ValidHaptic(haptic)) {
  555. return -1;
  556. }
  557. if ((haptic->supported & SDL_HAPTIC_AUTOCENTER) == 0) {
  558. return SDL_SetError("Haptic: Device does not support setting autocenter.");
  559. }
  560. if ((autocenter < 0) || (autocenter > 100)) {
  561. return SDL_SetError("Haptic: Autocenter must be between 0 and 100.");
  562. }
  563. if (SDL_SYS_HapticSetAutocenter(haptic, autocenter) < 0) {
  564. return -1;
  565. }
  566. return 0;
  567. }
  568. /*
  569. * Pauses the haptic device.
  570. */
  571. int
  572. SDL_HapticPause(SDL_Haptic * haptic)
  573. {
  574. if (!ValidHaptic(haptic)) {
  575. return -1;
  576. }
  577. if ((haptic->supported & SDL_HAPTIC_PAUSE) == 0) {
  578. return SDL_SetError("Haptic: Device does not support setting pausing.");
  579. }
  580. return SDL_SYS_HapticPause(haptic);
  581. }
  582. /*
  583. * Unpauses the haptic device.
  584. */
  585. int
  586. SDL_HapticUnpause(SDL_Haptic * haptic)
  587. {
  588. if (!ValidHaptic(haptic)) {
  589. return -1;
  590. }
  591. if ((haptic->supported & SDL_HAPTIC_PAUSE) == 0) {
  592. return 0; /* Not going to be paused, so we pretend it's unpaused. */
  593. }
  594. return SDL_SYS_HapticUnpause(haptic);
  595. }
  596. /*
  597. * Stops all the currently playing effects.
  598. */
  599. int
  600. SDL_HapticStopAll(SDL_Haptic * haptic)
  601. {
  602. if (!ValidHaptic(haptic)) {
  603. return -1;
  604. }
  605. return SDL_SYS_HapticStopAll(haptic);
  606. }
  607. /*
  608. * Checks to see if rumble is supported.
  609. */
  610. int
  611. SDL_HapticRumbleSupported(SDL_Haptic * haptic)
  612. {
  613. if (!ValidHaptic(haptic)) {
  614. return -1;
  615. }
  616. /* Most things can use SINE, but XInput only has LEFTRIGHT. */
  617. return ((haptic->supported & (SDL_HAPTIC_SINE|SDL_HAPTIC_LEFTRIGHT)) != 0);
  618. }
  619. /*
  620. * Initializes the haptic device for simple rumble playback.
  621. */
  622. int
  623. SDL_HapticRumbleInit(SDL_Haptic * haptic)
  624. {
  625. SDL_HapticEffect *efx = &haptic->rumble_effect;
  626. if (!ValidHaptic(haptic)) {
  627. return -1;
  628. }
  629. /* Already allocated. */
  630. if (haptic->rumble_id >= 0) {
  631. return 0;
  632. }
  633. SDL_zerop(efx);
  634. if (haptic->supported & SDL_HAPTIC_SINE) {
  635. efx->type = SDL_HAPTIC_SINE;
  636. efx->periodic.period = 1000;
  637. efx->periodic.magnitude = 0x4000;
  638. efx->periodic.length = 5000;
  639. efx->periodic.attack_length = 0;
  640. efx->periodic.fade_length = 0;
  641. } else if (haptic->supported & SDL_HAPTIC_LEFTRIGHT) { /* XInput? */
  642. efx->type = SDL_HAPTIC_LEFTRIGHT;
  643. efx->leftright.length = 5000;
  644. efx->leftright.large_magnitude = 0x4000;
  645. efx->leftright.small_magnitude = 0x4000;
  646. } else {
  647. return SDL_SetError("Device doesn't support rumble");
  648. }
  649. haptic->rumble_id = SDL_HapticNewEffect(haptic, &haptic->rumble_effect);
  650. if (haptic->rumble_id >= 0) {
  651. return 0;
  652. }
  653. return -1;
  654. }
  655. /*
  656. * Runs simple rumble on a haptic device
  657. */
  658. int
  659. SDL_HapticRumblePlay(SDL_Haptic * haptic, float strength, Uint32 length)
  660. {
  661. SDL_HapticEffect *efx;
  662. Sint16 magnitude;
  663. if (!ValidHaptic(haptic)) {
  664. return -1;
  665. }
  666. if (haptic->rumble_id < 0) {
  667. return SDL_SetError("Haptic: Rumble effect not initialized on haptic device");
  668. }
  669. /* Clamp strength. */
  670. if (strength > 1.0f) {
  671. strength = 1.0f;
  672. } else if (strength < 0.0f) {
  673. strength = 0.0f;
  674. }
  675. magnitude = (Sint16)(32767.0f*strength);
  676. efx = &haptic->rumble_effect;
  677. if (efx->type == SDL_HAPTIC_SINE) {
  678. efx->periodic.magnitude = magnitude;
  679. efx->periodic.length = length;
  680. } else if (efx->type == SDL_HAPTIC_LEFTRIGHT) {
  681. efx->leftright.small_magnitude = efx->leftright.large_magnitude = magnitude;
  682. efx->leftright.length = length;
  683. } else {
  684. SDL_assert(0 && "This should have been caught elsewhere");
  685. }
  686. if (SDL_HapticUpdateEffect(haptic, haptic->rumble_id, &haptic->rumble_effect) < 0) {
  687. return -1;
  688. }
  689. return SDL_HapticRunEffect(haptic, haptic->rumble_id, 1);
  690. }
  691. /*
  692. * Stops the simple rumble on a haptic device.
  693. */
  694. int
  695. SDL_HapticRumbleStop(SDL_Haptic * haptic)
  696. {
  697. if (!ValidHaptic(haptic)) {
  698. return -1;
  699. }
  700. if (haptic->rumble_id < 0) {
  701. return SDL_SetError("Haptic: Rumble effect not initialized on haptic device");
  702. }
  703. return SDL_HapticStopEffect(haptic, haptic->rumble_id);
  704. }
  705. /* vi: set ts=4 sw=4 expandtab: */