SDL_windowshaptic.c 11 KB

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