SDL_windowshaptic.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 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. #if defined(SDL_HAPTIC_DINPUT) || defined(SDL_HAPTIC_XINPUT)
  20. #include "../SDL_syshaptic.h"
  21. #include "../../joystick/SDL_sysjoystick.h" /* For the real SDL_Joystick */
  22. #include "../../joystick/windows/SDL_windowsjoystick_c.h" /* For joystick hwdata */
  23. #include "../../joystick/windows/SDL_xinputjoystick_c.h" /* For xinput rumble */
  24. #include "SDL_windowshaptic_c.h"
  25. #include "SDL_dinputhaptic_c.h"
  26. #include "SDL_xinputhaptic_c.h"
  27. /* Set up for C function definitions, even when using C++ */
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. /*
  32. * Internal stuff.
  33. */
  34. SDL_hapticlist_item *SDL_hapticlist = NULL;
  35. static SDL_hapticlist_item *SDL_hapticlist_tail = NULL;
  36. static int numhaptics = 0;
  37. /*
  38. * Initializes the haptic subsystem.
  39. */
  40. int SDL_SYS_HapticInit(void)
  41. {
  42. JoyStick_DeviceData *device;
  43. if (SDL_DINPUT_HapticInit() < 0) {
  44. return -1;
  45. }
  46. if (SDL_XINPUT_HapticInit() < 0) {
  47. return -1;
  48. }
  49. /* The joystick subsystem will usually be initialized before haptics,
  50. * so the initial HapticMaybeAddDevice() calls from the joystick
  51. * subsystem will arrive too early to create haptic devices. We will
  52. * invoke those callbacks again here to pick up any joysticks that
  53. * were added prior to haptics initialization. */
  54. for (device = SYS_Joystick; device; device = device->pNext) {
  55. if (device->bXInputDevice) {
  56. SDL_XINPUT_HapticMaybeAddDevice(device->XInputUserId);
  57. } else {
  58. SDL_DINPUT_HapticMaybeAddDevice(&device->dxdevice);
  59. }
  60. }
  61. return numhaptics;
  62. }
  63. int SDL_SYS_AddHapticDevice(SDL_hapticlist_item *item)
  64. {
  65. if (!SDL_hapticlist_tail) {
  66. SDL_hapticlist = SDL_hapticlist_tail = item;
  67. } else {
  68. SDL_hapticlist_tail->next = item;
  69. SDL_hapticlist_tail = item;
  70. }
  71. /* Device has been added. */
  72. ++numhaptics;
  73. return numhaptics;
  74. }
  75. int SDL_SYS_RemoveHapticDevice(SDL_hapticlist_item *prev, SDL_hapticlist_item *item)
  76. {
  77. const int retval = item->haptic ? item->haptic->index : -1;
  78. if (prev) {
  79. prev->next = item->next;
  80. } else {
  81. SDL_assert(SDL_hapticlist == item);
  82. SDL_hapticlist = item->next;
  83. }
  84. if (item == SDL_hapticlist_tail) {
  85. SDL_hapticlist_tail = prev;
  86. }
  87. --numhaptics;
  88. /* !!! TODO: Send a haptic remove event? */
  89. SDL_free(item);
  90. return retval;
  91. }
  92. int SDL_SYS_NumHaptics(void)
  93. {
  94. return numhaptics;
  95. }
  96. static SDL_hapticlist_item *HapticByDevIndex(int device_index)
  97. {
  98. SDL_hapticlist_item *item = SDL_hapticlist;
  99. if ((device_index < 0) || (device_index >= numhaptics)) {
  100. return NULL;
  101. }
  102. while (device_index > 0) {
  103. SDL_assert(item != NULL);
  104. --device_index;
  105. item = item->next;
  106. }
  107. return item;
  108. }
  109. /*
  110. * Return the name of a haptic device, does not need to be opened.
  111. */
  112. const char *SDL_SYS_HapticName(int index)
  113. {
  114. SDL_hapticlist_item *item = HapticByDevIndex(index);
  115. return item->name;
  116. }
  117. /*
  118. * Opens a haptic device for usage.
  119. */
  120. int SDL_SYS_HapticOpen(SDL_Haptic *haptic)
  121. {
  122. SDL_hapticlist_item *item = HapticByDevIndex(haptic->index);
  123. if (item->bXInputHaptic) {
  124. return SDL_XINPUT_HapticOpen(haptic, item);
  125. } else {
  126. return SDL_DINPUT_HapticOpen(haptic, item);
  127. }
  128. }
  129. /*
  130. * Opens a haptic device from first mouse it finds for usage.
  131. */
  132. int SDL_SYS_HapticMouse(void)
  133. {
  134. #ifdef SDL_HAPTIC_DINPUT
  135. SDL_hapticlist_item *item;
  136. int index = 0;
  137. /* Grab the first mouse haptic device we find. */
  138. for (item = SDL_hapticlist; item; item = item->next) {
  139. if (item->capabilities.dwDevType == DI8DEVCLASS_POINTER) {
  140. return index;
  141. }
  142. ++index;
  143. }
  144. #endif /* SDL_HAPTIC_DINPUT */
  145. return -1;
  146. }
  147. /*
  148. * Checks to see if a joystick has haptic features.
  149. */
  150. int SDL_SYS_JoystickIsHaptic(SDL_Joystick *joystick)
  151. {
  152. if (joystick->driver != &SDL_WINDOWS_JoystickDriver) {
  153. return 0;
  154. }
  155. #ifdef SDL_HAPTIC_XINPUT
  156. if (joystick->hwdata->bXInputHaptic) {
  157. return 1;
  158. }
  159. #endif
  160. #ifdef SDL_HAPTIC_DINPUT
  161. if (joystick->hwdata->Capabilities.dwFlags & DIDC_FORCEFEEDBACK) {
  162. return 1;
  163. }
  164. #endif
  165. return 0;
  166. }
  167. /*
  168. * Checks to see if the haptic device and joystick are in reality the same.
  169. */
  170. int SDL_SYS_JoystickSameHaptic(SDL_Haptic *haptic, SDL_Joystick *joystick)
  171. {
  172. if (joystick->driver != &SDL_WINDOWS_JoystickDriver) {
  173. return 0;
  174. }
  175. if (joystick->hwdata->bXInputHaptic != haptic->hwdata->bXInputHaptic) {
  176. return 0; /* one is XInput, one is not; not the same device. */
  177. } else if (joystick->hwdata->bXInputHaptic) {
  178. return SDL_XINPUT_JoystickSameHaptic(haptic, joystick);
  179. } else {
  180. return SDL_DINPUT_JoystickSameHaptic(haptic, joystick);
  181. }
  182. }
  183. /*
  184. * Opens a SDL_Haptic from a SDL_Joystick.
  185. */
  186. int SDL_SYS_HapticOpenFromJoystick(SDL_Haptic *haptic, SDL_Joystick *joystick)
  187. {
  188. SDL_assert(joystick->driver == &SDL_WINDOWS_JoystickDriver);
  189. if (joystick->hwdata->bXInputDevice) {
  190. return SDL_XINPUT_HapticOpenFromJoystick(haptic, joystick);
  191. } else {
  192. return SDL_DINPUT_HapticOpenFromJoystick(haptic, joystick);
  193. }
  194. }
  195. /*
  196. * Closes the haptic device.
  197. */
  198. void SDL_SYS_HapticClose(SDL_Haptic *haptic)
  199. {
  200. if (haptic->hwdata) {
  201. /* Free effects. */
  202. SDL_free(haptic->effects);
  203. haptic->effects = NULL;
  204. haptic->neffects = 0;
  205. /* Clean up */
  206. if (haptic->hwdata->bXInputHaptic) {
  207. SDL_XINPUT_HapticClose(haptic);
  208. } else {
  209. SDL_DINPUT_HapticClose(haptic);
  210. }
  211. /* Free */
  212. SDL_free(haptic->hwdata);
  213. haptic->hwdata = NULL;
  214. }
  215. }
  216. /*
  217. * Clean up after system specific haptic stuff
  218. */
  219. void SDL_SYS_HapticQuit(void)
  220. {
  221. SDL_hapticlist_item *item;
  222. SDL_hapticlist_item *next = NULL;
  223. SDL_Haptic *hapticitem = NULL;
  224. extern SDL_Haptic *SDL_haptics;
  225. for (hapticitem = SDL_haptics; hapticitem; hapticitem = hapticitem->next) {
  226. if ((hapticitem->hwdata->bXInputHaptic) && (hapticitem->hwdata->thread)) {
  227. /* we _have_ to stop the thread before we free the XInput DLL! */
  228. SDL_AtomicSet(&hapticitem->hwdata->stopThread, 1);
  229. SDL_WaitThread(hapticitem->hwdata->thread, NULL);
  230. hapticitem->hwdata->thread = NULL;
  231. }
  232. }
  233. for (item = SDL_hapticlist; item; item = next) {
  234. /* Opened and not closed haptics are leaked, this is on purpose.
  235. * Close your haptic devices after usage. */
  236. /* !!! FIXME: (...is leaking on purpose a good idea?) - No, of course not. */
  237. next = item->next;
  238. SDL_free(item->name);
  239. SDL_free(item);
  240. }
  241. SDL_XINPUT_HapticQuit();
  242. SDL_DINPUT_HapticQuit();
  243. numhaptics = 0;
  244. SDL_hapticlist = NULL;
  245. SDL_hapticlist_tail = NULL;
  246. }
  247. /*
  248. * Creates a new haptic effect.
  249. */
  250. int SDL_SYS_HapticNewEffect(SDL_Haptic *haptic, struct haptic_effect *effect,
  251. SDL_HapticEffect *base)
  252. {
  253. int result;
  254. /* Alloc the effect. */
  255. effect->hweffect = (struct haptic_hweffect *) SDL_calloc(1, sizeof(struct haptic_hweffect));
  256. if (!effect->hweffect) {
  257. return -1;
  258. }
  259. if (haptic->hwdata->bXInputHaptic) {
  260. result = SDL_XINPUT_HapticNewEffect(haptic, effect, base);
  261. } else {
  262. result = SDL_DINPUT_HapticNewEffect(haptic, effect, base);
  263. }
  264. if (result < 0) {
  265. SDL_free(effect->hweffect);
  266. effect->hweffect = NULL;
  267. }
  268. return result;
  269. }
  270. /*
  271. * Updates an effect.
  272. */
  273. int SDL_SYS_HapticUpdateEffect(SDL_Haptic *haptic,
  274. struct haptic_effect *effect,
  275. SDL_HapticEffect *data)
  276. {
  277. if (haptic->hwdata->bXInputHaptic) {
  278. return SDL_XINPUT_HapticUpdateEffect(haptic, effect, data);
  279. } else {
  280. return SDL_DINPUT_HapticUpdateEffect(haptic, effect, data);
  281. }
  282. }
  283. /*
  284. * Runs an effect.
  285. */
  286. int SDL_SYS_HapticRunEffect(SDL_Haptic *haptic, struct haptic_effect *effect,
  287. Uint32 iterations)
  288. {
  289. if (haptic->hwdata->bXInputHaptic) {
  290. return SDL_XINPUT_HapticRunEffect(haptic, effect, iterations);
  291. } else {
  292. return SDL_DINPUT_HapticRunEffect(haptic, effect, iterations);
  293. }
  294. }
  295. /*
  296. * Stops an effect.
  297. */
  298. int SDL_SYS_HapticStopEffect(SDL_Haptic *haptic, struct haptic_effect *effect)
  299. {
  300. if (haptic->hwdata->bXInputHaptic) {
  301. return SDL_XINPUT_HapticStopEffect(haptic, effect);
  302. } else {
  303. return SDL_DINPUT_HapticStopEffect(haptic, effect);
  304. }
  305. }
  306. /*
  307. * Frees the effect.
  308. */
  309. void SDL_SYS_HapticDestroyEffect(SDL_Haptic *haptic, struct haptic_effect *effect)
  310. {
  311. if (haptic->hwdata->bXInputHaptic) {
  312. SDL_XINPUT_HapticDestroyEffect(haptic, effect);
  313. } else {
  314. SDL_DINPUT_HapticDestroyEffect(haptic, effect);
  315. }
  316. SDL_free(effect->hweffect);
  317. effect->hweffect = NULL;
  318. }
  319. /*
  320. * Gets the status of a haptic effect.
  321. */
  322. int SDL_SYS_HapticGetEffectStatus(SDL_Haptic *haptic,
  323. struct haptic_effect *effect)
  324. {
  325. if (haptic->hwdata->bXInputHaptic) {
  326. return SDL_XINPUT_HapticGetEffectStatus(haptic, effect);
  327. } else {
  328. return SDL_DINPUT_HapticGetEffectStatus(haptic, effect);
  329. }
  330. }
  331. /*
  332. * Sets the gain.
  333. */
  334. int SDL_SYS_HapticSetGain(SDL_Haptic *haptic, int gain)
  335. {
  336. if (haptic->hwdata->bXInputHaptic) {
  337. return SDL_XINPUT_HapticSetGain(haptic, gain);
  338. } else {
  339. return SDL_DINPUT_HapticSetGain(haptic, gain);
  340. }
  341. }
  342. /*
  343. * Sets the autocentering.
  344. */
  345. int SDL_SYS_HapticSetAutocenter(SDL_Haptic *haptic, int autocenter)
  346. {
  347. if (haptic->hwdata->bXInputHaptic) {
  348. return SDL_XINPUT_HapticSetAutocenter(haptic, autocenter);
  349. } else {
  350. return SDL_DINPUT_HapticSetAutocenter(haptic, autocenter);
  351. }
  352. }
  353. /*
  354. * Pauses the device.
  355. */
  356. int SDL_SYS_HapticPause(SDL_Haptic *haptic)
  357. {
  358. if (haptic->hwdata->bXInputHaptic) {
  359. return SDL_XINPUT_HapticPause(haptic);
  360. } else {
  361. return SDL_DINPUT_HapticPause(haptic);
  362. }
  363. }
  364. /*
  365. * Pauses the device.
  366. */
  367. int SDL_SYS_HapticUnpause(SDL_Haptic *haptic)
  368. {
  369. if (haptic->hwdata->bXInputHaptic) {
  370. return SDL_XINPUT_HapticUnpause(haptic);
  371. } else {
  372. return SDL_DINPUT_HapticUnpause(haptic);
  373. }
  374. }
  375. /*
  376. * Stops all the playing effects on the device.
  377. */
  378. int SDL_SYS_HapticStopAll(SDL_Haptic *haptic)
  379. {
  380. if (haptic->hwdata->bXInputHaptic) {
  381. return SDL_XINPUT_HapticStopAll(haptic);
  382. } else {
  383. return SDL_DINPUT_HapticStopAll(haptic);
  384. }
  385. }
  386. /* Ends C function definitions when using C++ */
  387. #ifdef __cplusplus
  388. }
  389. #endif
  390. #endif /* SDL_HAPTIC_DINPUT || SDL_HAPTIC_XINPUT */