SDL_haptic.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  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. #include "SDL_syshaptic.h"
  20. #include "SDL_haptic_c.h"
  21. #include "../joystick/SDL_joystick_c.h" // For SDL_IsJoystickValid
  22. #include "../SDL_hints_c.h"
  23. typedef struct SDL_Haptic_VIDPID_Naxes {
  24. Uint16 vid;
  25. Uint16 pid;
  26. Uint16 naxes;
  27. } SDL_Haptic_VIDPID_Naxes;
  28. static void SDL_Haptic_Load_Axes_List(SDL_Haptic_VIDPID_Naxes **entries, int *num_entries)
  29. {
  30. SDL_Haptic_VIDPID_Naxes entry;
  31. const char *spot;
  32. int length = 0;
  33. spot = SDL_GetHint(SDL_HINT_JOYSTICK_HAPTIC_AXES);
  34. if (!spot)
  35. return;
  36. while (SDL_sscanf(spot, "0x%hx/0x%hx/%hu%n", &entry.vid, &entry.pid, &entry.naxes, &length) == 3) {
  37. SDL_assert(length > 0);
  38. spot += length;
  39. length = 0;
  40. if ((*num_entries % 8) == 0) {
  41. int new_max = *num_entries + 8;
  42. SDL_Haptic_VIDPID_Naxes *new_entries =
  43. (SDL_Haptic_VIDPID_Naxes *)SDL_realloc(*entries, new_max * sizeof(**entries));
  44. // Out of memory, go with what we have already
  45. if (!new_entries)
  46. break;
  47. *entries = new_entries;
  48. }
  49. (*entries)[(*num_entries)++] = entry;
  50. if (spot[0] == ',')
  51. spot++;
  52. }
  53. }
  54. // /* Return -1 if not found */
  55. static int SDL_Haptic_Naxes_List_Index(struct SDL_Haptic_VIDPID_Naxes *entries, int num_entries, Uint16 vid, Uint16 pid)
  56. {
  57. if (!entries)
  58. return -1;
  59. int i;
  60. for (i = 0; i < num_entries; ++i) {
  61. if (entries[i].vid == vid && entries[i].pid == pid)
  62. return i;
  63. }
  64. return -1;
  65. }
  66. // Check if device needs a custom number of naxes
  67. static int SDL_Haptic_Get_Naxes(Uint16 vid, Uint16 pid)
  68. {
  69. int num_entries = 0, index = 0, naxes = -1;
  70. SDL_Haptic_VIDPID_Naxes *naxes_list = NULL;
  71. SDL_Haptic_Load_Axes_List(&naxes_list, &num_entries);
  72. if (!num_entries || !naxes_list)
  73. return -1;
  74. // Perform "wildcard" pass
  75. index = SDL_Haptic_Naxes_List_Index(naxes_list, num_entries, 0xffff, 0xffff);
  76. if (index >= 0)
  77. naxes = naxes_list[index].naxes;
  78. index = SDL_Haptic_Naxes_List_Index(naxes_list, num_entries, vid, pid);
  79. if (index >= 0)
  80. naxes = naxes_list[index].naxes;
  81. SDL_free(naxes_list);
  82. return naxes;
  83. }
  84. static SDL_Haptic *SDL_haptics = NULL;
  85. #define CHECK_HAPTIC_MAGIC(haptic, result) \
  86. if (!SDL_ObjectValid(haptic, SDL_OBJECT_TYPE_HAPTIC)) { \
  87. SDL_InvalidParamError("haptic"); \
  88. return result; \
  89. }
  90. bool SDL_InitHaptics(void)
  91. {
  92. return SDL_SYS_HapticInit();
  93. }
  94. static bool SDL_GetHapticIndex(SDL_HapticID instance_id, int *driver_index)
  95. {
  96. int num_haptics, device_index;
  97. if (instance_id > 0) {
  98. num_haptics = SDL_SYS_NumHaptics();
  99. for (device_index = 0; device_index < num_haptics; ++device_index) {
  100. SDL_HapticID haptic_id = SDL_SYS_HapticInstanceID(device_index);
  101. if (haptic_id == instance_id) {
  102. *driver_index = device_index;
  103. return true;
  104. }
  105. }
  106. }
  107. SDL_SetError("Haptic device %" SDL_PRIu32 " not found", instance_id);
  108. return false;
  109. }
  110. SDL_HapticID *SDL_GetHaptics(int *count)
  111. {
  112. int device_index;
  113. int haptic_index = 0, num_haptics = 0;
  114. SDL_HapticID *haptics;
  115. num_haptics = SDL_SYS_NumHaptics();
  116. haptics = (SDL_HapticID *)SDL_malloc((num_haptics + 1) * sizeof(*haptics));
  117. if (haptics) {
  118. if (count) {
  119. *count = num_haptics;
  120. }
  121. for (device_index = 0; device_index < num_haptics; ++device_index) {
  122. haptics[haptic_index] = SDL_SYS_HapticInstanceID(device_index);
  123. SDL_assert(haptics[haptic_index] > 0);
  124. ++haptic_index;
  125. }
  126. haptics[haptic_index] = 0;
  127. } else {
  128. if (count) {
  129. *count = 0;
  130. }
  131. }
  132. return haptics;
  133. }
  134. const char *SDL_GetHapticNameForID(SDL_HapticID instance_id)
  135. {
  136. int device_index;
  137. const char *name = NULL;
  138. if (SDL_GetHapticIndex(instance_id, &device_index)) {
  139. name = SDL_GetPersistentString(SDL_SYS_HapticName(device_index));
  140. }
  141. return name;
  142. }
  143. SDL_Haptic *SDL_OpenHaptic(SDL_HapticID instance_id)
  144. {
  145. SDL_Haptic *haptic;
  146. SDL_Haptic *hapticlist;
  147. const char *name;
  148. int device_index = 0;
  149. if (!SDL_GetHapticIndex(instance_id, &device_index)) {
  150. return NULL;
  151. }
  152. hapticlist = SDL_haptics;
  153. /* If the haptic device is already open, return it
  154. * it is important that we have a single haptic device for each instance id
  155. */
  156. while (hapticlist) {
  157. if (instance_id == hapticlist->instance_id) {
  158. haptic = hapticlist;
  159. ++haptic->ref_count;
  160. return haptic;
  161. }
  162. hapticlist = hapticlist->next;
  163. }
  164. // Create the haptic device
  165. haptic = (SDL_Haptic *)SDL_calloc(1, sizeof(*haptic));
  166. if (!haptic) {
  167. return NULL;
  168. }
  169. // Initialize the haptic device
  170. SDL_SetObjectValid(haptic, SDL_OBJECT_TYPE_HAPTIC, true);
  171. haptic->instance_id = instance_id;
  172. haptic->rumble_id = -1;
  173. if (!SDL_SYS_HapticOpen(haptic)) {
  174. SDL_SetObjectValid(haptic, SDL_OBJECT_TYPE_HAPTIC, false);
  175. SDL_free(haptic);
  176. return NULL;
  177. }
  178. if (!haptic->name) {
  179. name = SDL_SYS_HapticName(device_index);
  180. if (name) {
  181. haptic->name = SDL_strdup(name);
  182. }
  183. }
  184. // Add haptic to list
  185. ++haptic->ref_count;
  186. // Link the haptic in the list
  187. haptic->next = SDL_haptics;
  188. SDL_haptics = haptic;
  189. // Disable autocenter and set gain to max.
  190. if (haptic->supported & SDL_HAPTIC_GAIN) {
  191. SDL_SetHapticGain(haptic, 100);
  192. }
  193. if (haptic->supported & SDL_HAPTIC_AUTOCENTER) {
  194. SDL_SetHapticAutocenter(haptic, 0);
  195. }
  196. return haptic;
  197. }
  198. SDL_Haptic *SDL_GetHapticFromID(SDL_HapticID instance_id)
  199. {
  200. SDL_Haptic *haptic;
  201. for (haptic = SDL_haptics; haptic; haptic = haptic->next) {
  202. if (instance_id == haptic->instance_id) {
  203. break;
  204. }
  205. }
  206. return haptic;
  207. }
  208. SDL_HapticID SDL_GetHapticID(SDL_Haptic *haptic)
  209. {
  210. CHECK_HAPTIC_MAGIC(haptic, 0);
  211. return haptic->instance_id;
  212. }
  213. const char *SDL_GetHapticName(SDL_Haptic *haptic)
  214. {
  215. CHECK_HAPTIC_MAGIC(haptic, NULL);
  216. return SDL_GetPersistentString(haptic->name);
  217. }
  218. bool SDL_IsMouseHaptic(void)
  219. {
  220. if (SDL_SYS_HapticMouse() < 0) {
  221. return false;
  222. }
  223. return true;
  224. }
  225. SDL_Haptic *SDL_OpenHapticFromMouse(void)
  226. {
  227. int device_index;
  228. device_index = SDL_SYS_HapticMouse();
  229. if (device_index < 0) {
  230. SDL_SetError("Haptic: Mouse isn't a haptic device.");
  231. return NULL;
  232. }
  233. return SDL_OpenHaptic(device_index);
  234. }
  235. bool SDL_IsJoystickHaptic(SDL_Joystick *joystick)
  236. {
  237. bool result = false;
  238. SDL_LockJoysticks();
  239. {
  240. // Must be a valid joystick
  241. if (SDL_IsJoystickValid(joystick) &&
  242. !SDL_IsGamepad(SDL_GetJoystickID(joystick))) {
  243. result = SDL_SYS_JoystickIsHaptic(joystick);
  244. }
  245. }
  246. SDL_UnlockJoysticks();
  247. return result;
  248. }
  249. SDL_Haptic *SDL_OpenHapticFromJoystick(SDL_Joystick *joystick)
  250. {
  251. SDL_Haptic *haptic;
  252. SDL_Haptic *hapticlist;
  253. SDL_LockJoysticks();
  254. {
  255. // Must be a valid joystick
  256. if (!SDL_IsJoystickValid(joystick)) {
  257. SDL_SetError("Haptic: Joystick isn't valid.");
  258. SDL_UnlockJoysticks();
  259. return NULL;
  260. }
  261. // Joystick must be haptic
  262. if (SDL_IsGamepad(SDL_GetJoystickID(joystick)) ||
  263. !SDL_SYS_JoystickIsHaptic(joystick)) {
  264. SDL_SetError("Haptic: Joystick isn't a haptic device.");
  265. SDL_UnlockJoysticks();
  266. return NULL;
  267. }
  268. hapticlist = SDL_haptics;
  269. // Check to see if joystick's haptic is already open
  270. while (hapticlist) {
  271. if (SDL_SYS_JoystickSameHaptic(hapticlist, joystick)) {
  272. haptic = hapticlist;
  273. ++haptic->ref_count;
  274. SDL_UnlockJoysticks();
  275. return haptic;
  276. }
  277. hapticlist = hapticlist->next;
  278. }
  279. // Create the haptic device
  280. haptic = (SDL_Haptic *)SDL_calloc(1, sizeof(*haptic));
  281. if (!haptic) {
  282. SDL_UnlockJoysticks();
  283. return NULL;
  284. }
  285. /* Initialize the haptic device
  286. * This function should fill in the instance ID and name.
  287. */
  288. SDL_SetObjectValid(haptic, SDL_OBJECT_TYPE_HAPTIC, true);
  289. haptic->rumble_id = -1;
  290. if (!SDL_SYS_HapticOpenFromJoystick(haptic, joystick)) {
  291. SDL_SetError("Haptic: SDL_SYS_HapticOpenFromJoystick failed.");
  292. SDL_SetObjectValid(haptic, SDL_OBJECT_TYPE_HAPTIC, false);
  293. SDL_free(haptic);
  294. SDL_UnlockJoysticks();
  295. return NULL;
  296. }
  297. SDL_assert(haptic->instance_id != 0);
  298. }
  299. SDL_UnlockJoysticks();
  300. // Check if custom number of haptic axes was defined
  301. Uint16 vid = SDL_GetJoystickVendor(joystick);
  302. Uint16 pid = SDL_GetJoystickProduct(joystick);
  303. int general_axes = SDL_GetNumJoystickAxes(joystick);
  304. int naxes = SDL_Haptic_Get_Naxes(vid, pid);
  305. if (naxes > 0)
  306. haptic->naxes = naxes;
  307. // Limit to the actual number of axes found on the device
  308. if (general_axes >= 0 && naxes > general_axes)
  309. haptic->naxes = general_axes;
  310. // Add haptic to list
  311. ++haptic->ref_count;
  312. // Link the haptic in the list
  313. haptic->next = SDL_haptics;
  314. SDL_haptics = haptic;
  315. return haptic;
  316. }
  317. void SDL_CloseHaptic(SDL_Haptic *haptic)
  318. {
  319. int i;
  320. SDL_Haptic *hapticlist;
  321. SDL_Haptic *hapticlistprev;
  322. CHECK_HAPTIC_MAGIC(haptic,);
  323. // Check if it's still in use
  324. if (--haptic->ref_count > 0) {
  325. return;
  326. }
  327. // Close it, properly removing effects if needed
  328. for (i = 0; i < haptic->neffects; i++) {
  329. if (haptic->effects[i].hweffect != NULL) {
  330. SDL_DestroyHapticEffect(haptic, i);
  331. }
  332. }
  333. SDL_SYS_HapticClose(haptic);
  334. SDL_SetObjectValid(haptic, SDL_OBJECT_TYPE_HAPTIC, false);
  335. // Remove from the list
  336. hapticlist = SDL_haptics;
  337. hapticlistprev = NULL;
  338. while (hapticlist) {
  339. if (haptic == hapticlist) {
  340. if (hapticlistprev) {
  341. // unlink this entry
  342. hapticlistprev->next = hapticlist->next;
  343. } else {
  344. SDL_haptics = haptic->next;
  345. }
  346. break;
  347. }
  348. hapticlistprev = hapticlist;
  349. hapticlist = hapticlist->next;
  350. }
  351. // Free the data associated with this device
  352. SDL_free(haptic->name);
  353. SDL_free(haptic);
  354. }
  355. void SDL_QuitHaptics(void)
  356. {
  357. while (SDL_haptics) {
  358. SDL_CloseHaptic(SDL_haptics);
  359. }
  360. SDL_SYS_HapticQuit();
  361. }
  362. int SDL_GetMaxHapticEffects(SDL_Haptic *haptic)
  363. {
  364. CHECK_HAPTIC_MAGIC(haptic, -1);
  365. return haptic->neffects;
  366. }
  367. int SDL_GetMaxHapticEffectsPlaying(SDL_Haptic *haptic)
  368. {
  369. CHECK_HAPTIC_MAGIC(haptic, -1);
  370. return haptic->nplaying;
  371. }
  372. Uint32 SDL_GetHapticFeatures(SDL_Haptic *haptic)
  373. {
  374. CHECK_HAPTIC_MAGIC(haptic, 0);
  375. return haptic->supported;
  376. }
  377. int SDL_GetNumHapticAxes(SDL_Haptic *haptic)
  378. {
  379. CHECK_HAPTIC_MAGIC(haptic, -1);
  380. return haptic->naxes;
  381. }
  382. bool SDL_HapticEffectSupported(SDL_Haptic *haptic, const SDL_HapticEffect *effect)
  383. {
  384. CHECK_HAPTIC_MAGIC(haptic, false);
  385. if (!effect) {
  386. return false;
  387. }
  388. if ((haptic->supported & effect->type) != 0) {
  389. return true;
  390. }
  391. return false;
  392. }
  393. int SDL_CreateHapticEffect(SDL_Haptic *haptic, const SDL_HapticEffect *effect)
  394. {
  395. int i;
  396. CHECK_HAPTIC_MAGIC(haptic, -1);
  397. if (!effect) {
  398. SDL_InvalidParamError("effect");
  399. return -1;
  400. }
  401. // Check to see if effect is supported
  402. if (SDL_HapticEffectSupported(haptic, effect) == false) {
  403. SDL_SetError("Haptic: Effect not supported by haptic device.");
  404. return -1;
  405. }
  406. // See if there's a free slot
  407. for (i = 0; i < haptic->neffects; i++) {
  408. if (haptic->effects[i].hweffect == NULL) {
  409. // Now let the backend create the real effect
  410. if (!SDL_SYS_HapticNewEffect(haptic, &haptic->effects[i], effect)) {
  411. return -1; // Backend failed to create effect
  412. }
  413. SDL_memcpy(&haptic->effects[i].effect, effect,
  414. sizeof(SDL_HapticEffect));
  415. return i;
  416. }
  417. }
  418. SDL_SetError("Haptic: Device has no free space left.");
  419. return -1;
  420. }
  421. static bool ValidEffect(SDL_Haptic *haptic, int effect)
  422. {
  423. if ((effect < 0) || (effect >= haptic->neffects)) {
  424. SDL_SetError("Haptic: Invalid effect identifier.");
  425. return false;
  426. }
  427. return true;
  428. }
  429. bool SDL_UpdateHapticEffect(SDL_Haptic *haptic, int effect, const SDL_HapticEffect *data)
  430. {
  431. CHECK_HAPTIC_MAGIC(haptic, false);
  432. if (!ValidEffect(haptic, effect)) {
  433. return false;
  434. }
  435. if (!data) {
  436. return SDL_InvalidParamError("data");
  437. }
  438. // Can't change type dynamically.
  439. if (data->type != haptic->effects[effect].effect.type) {
  440. return SDL_SetError("Haptic: Updating effect type is illegal.");
  441. }
  442. // Updates the effect
  443. if (!SDL_SYS_HapticUpdateEffect(haptic, &haptic->effects[effect], data)) {
  444. return false;
  445. }
  446. SDL_memcpy(&haptic->effects[effect].effect, data,
  447. sizeof(SDL_HapticEffect));
  448. return true;
  449. }
  450. bool SDL_RunHapticEffect(SDL_Haptic *haptic, int effect, Uint32 iterations)
  451. {
  452. CHECK_HAPTIC_MAGIC(haptic, false);
  453. if (!ValidEffect(haptic, effect)) {
  454. return false;
  455. }
  456. // Run the effect
  457. if (!SDL_SYS_HapticRunEffect(haptic, &haptic->effects[effect], iterations)) {
  458. return false;
  459. }
  460. return true;
  461. }
  462. bool SDL_StopHapticEffect(SDL_Haptic *haptic, int effect)
  463. {
  464. CHECK_HAPTIC_MAGIC(haptic, false);
  465. if (!ValidEffect(haptic, effect)) {
  466. return false;
  467. }
  468. // Stop the effect
  469. if (!SDL_SYS_HapticStopEffect(haptic, &haptic->effects[effect])) {
  470. return false;
  471. }
  472. return true;
  473. }
  474. void SDL_DestroyHapticEffect(SDL_Haptic *haptic, int effect)
  475. {
  476. CHECK_HAPTIC_MAGIC(haptic,);
  477. if (!ValidEffect(haptic, effect)) {
  478. return;
  479. }
  480. // Not allocated
  481. if (haptic->effects[effect].hweffect == NULL) {
  482. return;
  483. }
  484. SDL_SYS_HapticDestroyEffect(haptic, &haptic->effects[effect]);
  485. }
  486. bool SDL_GetHapticEffectStatus(SDL_Haptic *haptic, int effect)
  487. {
  488. CHECK_HAPTIC_MAGIC(haptic, false);
  489. if (!ValidEffect(haptic, effect)) {
  490. return false;
  491. }
  492. if (!(haptic->supported & SDL_HAPTIC_STATUS)) {
  493. return SDL_SetError("Haptic: Device does not support status queries.");
  494. }
  495. SDL_ClearError();
  496. return (SDL_SYS_HapticGetEffectStatus(haptic, &haptic->effects[effect]) > 0);
  497. }
  498. bool SDL_SetHapticGain(SDL_Haptic *haptic, int gain)
  499. {
  500. const char *env;
  501. int real_gain, max_gain;
  502. CHECK_HAPTIC_MAGIC(haptic, false);
  503. if (!(haptic->supported & SDL_HAPTIC_GAIN)) {
  504. return SDL_SetError("Haptic: Device does not support setting gain.");
  505. }
  506. if ((gain < 0) || (gain > 100)) {
  507. return SDL_SetError("Haptic: Gain must be between 0 and 100.");
  508. }
  509. // The user can use an environment variable to override the max gain.
  510. env = SDL_getenv("SDL_HAPTIC_GAIN_MAX");
  511. if (env) {
  512. max_gain = SDL_atoi(env);
  513. // Check for sanity.
  514. if (max_gain < 0) {
  515. max_gain = 0;
  516. } else if (max_gain > 100) {
  517. max_gain = 100;
  518. }
  519. // We'll scale it linearly with SDL_HAPTIC_GAIN_MAX
  520. real_gain = (gain * max_gain) / 100;
  521. } else {
  522. real_gain = gain;
  523. }
  524. return SDL_SYS_HapticSetGain(haptic, real_gain);
  525. }
  526. bool SDL_SetHapticAutocenter(SDL_Haptic *haptic, int autocenter)
  527. {
  528. CHECK_HAPTIC_MAGIC(haptic, false);
  529. if (!(haptic->supported & SDL_HAPTIC_AUTOCENTER)) {
  530. return SDL_SetError("Haptic: Device does not support setting autocenter.");
  531. }
  532. if ((autocenter < 0) || (autocenter > 100)) {
  533. return SDL_SetError("Haptic: Autocenter must be between 0 and 100.");
  534. }
  535. return SDL_SYS_HapticSetAutocenter(haptic, autocenter);
  536. }
  537. bool SDL_PauseHaptic(SDL_Haptic *haptic)
  538. {
  539. CHECK_HAPTIC_MAGIC(haptic, false);
  540. if (!(haptic->supported & SDL_HAPTIC_PAUSE)) {
  541. return SDL_SetError("Haptic: Device does not support setting pausing.");
  542. }
  543. return SDL_SYS_HapticPause(haptic);
  544. }
  545. bool SDL_ResumeHaptic(SDL_Haptic *haptic)
  546. {
  547. CHECK_HAPTIC_MAGIC(haptic, false);
  548. if (!(haptic->supported & SDL_HAPTIC_PAUSE)) {
  549. return true; // Not going to be paused, so we pretend it's unpaused.
  550. }
  551. return SDL_SYS_HapticResume(haptic);
  552. }
  553. bool SDL_StopHapticEffects(SDL_Haptic *haptic)
  554. {
  555. CHECK_HAPTIC_MAGIC(haptic, false);
  556. return SDL_SYS_HapticStopAll(haptic);
  557. }
  558. bool SDL_HapticRumbleSupported(SDL_Haptic *haptic)
  559. {
  560. CHECK_HAPTIC_MAGIC(haptic, false);
  561. // Most things can use SINE, but XInput only has LEFTRIGHT.
  562. return (haptic->supported & (SDL_HAPTIC_SINE | SDL_HAPTIC_LEFTRIGHT)) != 0;
  563. }
  564. bool SDL_InitHapticRumble(SDL_Haptic *haptic)
  565. {
  566. SDL_HapticEffect *efx = &haptic->rumble_effect;
  567. CHECK_HAPTIC_MAGIC(haptic, false);
  568. // Already allocated.
  569. if (haptic->rumble_id >= 0) {
  570. return true;
  571. }
  572. SDL_zerop(efx);
  573. if (haptic->supported & SDL_HAPTIC_SINE) {
  574. efx->type = SDL_HAPTIC_SINE;
  575. efx->periodic.direction.type = SDL_HAPTIC_CARTESIAN;
  576. efx->periodic.period = 1000;
  577. efx->periodic.magnitude = 0x4000;
  578. efx->periodic.length = 5000;
  579. efx->periodic.attack_length = 0;
  580. efx->periodic.fade_length = 0;
  581. } else if (haptic->supported & SDL_HAPTIC_LEFTRIGHT) { // XInput?
  582. efx->type = SDL_HAPTIC_LEFTRIGHT;
  583. efx->leftright.length = 5000;
  584. efx->leftright.large_magnitude = 0x4000;
  585. efx->leftright.small_magnitude = 0x4000;
  586. } else {
  587. return SDL_SetError("Device doesn't support rumble");
  588. }
  589. haptic->rumble_id = SDL_CreateHapticEffect(haptic, &haptic->rumble_effect);
  590. if (haptic->rumble_id >= 0) {
  591. return true;
  592. }
  593. return false;
  594. }
  595. bool SDL_PlayHapticRumble(SDL_Haptic *haptic, float strength, Uint32 length)
  596. {
  597. SDL_HapticEffect *efx;
  598. Sint16 magnitude;
  599. CHECK_HAPTIC_MAGIC(haptic, false);
  600. if (haptic->rumble_id < 0) {
  601. return SDL_SetError("Haptic: Rumble effect not initialized on haptic device");
  602. }
  603. // Clamp strength.
  604. if (strength > 1.0f) {
  605. strength = 1.0f;
  606. } else if (strength < 0.0f) {
  607. strength = 0.0f;
  608. }
  609. magnitude = (Sint16)(32767.0f * strength);
  610. efx = &haptic->rumble_effect;
  611. if (efx->type == SDL_HAPTIC_SINE) {
  612. efx->periodic.magnitude = magnitude;
  613. efx->periodic.length = length;
  614. } else if (efx->type == SDL_HAPTIC_LEFTRIGHT) {
  615. efx->leftright.small_magnitude = efx->leftright.large_magnitude = magnitude;
  616. efx->leftright.length = length;
  617. } else {
  618. SDL_assert(!"This should have been caught elsewhere");
  619. }
  620. if (!SDL_UpdateHapticEffect(haptic, haptic->rumble_id, &haptic->rumble_effect)) {
  621. return false;
  622. }
  623. return SDL_RunHapticEffect(haptic, haptic->rumble_id, 1);
  624. }
  625. bool SDL_StopHapticRumble(SDL_Haptic *haptic)
  626. {
  627. CHECK_HAPTIC_MAGIC(haptic, false);
  628. if (haptic->rumble_id < 0) {
  629. return SDL_SetError("Haptic: Rumble effect not initialized on haptic device");
  630. }
  631. return SDL_StopHapticEffect(haptic, haptic->rumble_id);
  632. }