SDL_hidapihaptic.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 2025 Katharine Chui <katharine.chui@gmail.com>
  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_JOYSTICK_HIDAPI
  20. #include "SDL_hidapihaptic.h"
  21. #include "SDL_hidapihaptic_c.h"
  22. #include "SDL3/SDL_mutex.h"
  23. #include "SDL3/SDL_error.h"
  24. typedef struct haptic_list_node
  25. {
  26. SDL_Haptic *haptic;
  27. struct haptic_list_node *next;
  28. } haptic_list_node;
  29. static haptic_list_node *haptic_list_head = NULL;
  30. static SDL_Mutex *haptic_list_mutex = NULL;
  31. static SDL_HIDAPI_HapticDriver *drivers[] = {
  32. #ifdef SDL_HAPTIC_HIDAPI_LG4FF
  33. &SDL_HIDAPI_HapticDriverLg4ff,
  34. #endif
  35. NULL
  36. };
  37. bool SDL_HIDAPI_HapticInit(void)
  38. {
  39. haptic_list_head = NULL;
  40. haptic_list_mutex = SDL_CreateMutex();
  41. if (haptic_list_mutex == NULL) {
  42. return SDL_OutOfMemory();
  43. }
  44. return true;
  45. }
  46. bool SDL_HIDAPI_HapticIsHidapi(SDL_Haptic *haptic)
  47. {
  48. haptic_list_node *cur;
  49. bool ret = false;
  50. SDL_LockMutex(haptic_list_mutex);
  51. cur = haptic_list_head;
  52. while (cur != NULL) {
  53. if (cur->haptic == haptic) {
  54. ret = true;
  55. break;
  56. }
  57. cur = cur->next;
  58. }
  59. SDL_UnlockMutex(haptic_list_mutex);
  60. return ret;
  61. }
  62. bool SDL_HIDAPI_JoystickIsHaptic(SDL_Joystick *joystick)
  63. {
  64. const int numdrivers = SDL_arraysize(drivers) - 1;
  65. int i;
  66. SDL_AssertJoysticksLocked();
  67. if (joystick->driver != &SDL_HIDAPI_JoystickDriver) {
  68. return false;
  69. }
  70. for (i = 0; i < numdrivers; ++i) {
  71. if (drivers[i]->JoystickSupported(joystick)) {
  72. return true;
  73. }
  74. }
  75. return false;
  76. }
  77. bool SDL_HIDAPI_HapticOpenFromJoystick(SDL_Haptic *haptic, SDL_Joystick *joystick)
  78. {
  79. const int numdrivers = SDL_arraysize(drivers) - 1;
  80. int i;
  81. SDL_AssertJoysticksLocked();
  82. if (joystick->driver != &SDL_HIDAPI_JoystickDriver) {
  83. return SDL_SetError("Cannot open hidapi haptic from non hidapi joystick");
  84. }
  85. for (i = 0; i < numdrivers; ++i) {
  86. if (drivers[i]->JoystickSupported(joystick)) {
  87. SDL_HIDAPI_HapticDevice *device;
  88. haptic_list_node *list_node;
  89. // the driver is responsible for calling SDL_SetError
  90. void *ctx = drivers[i]->Open(joystick);
  91. if (ctx == NULL) {
  92. return false;
  93. }
  94. device = SDL_malloc(sizeof(SDL_HIDAPI_HapticDevice));
  95. if (device == NULL) {
  96. SDL_HIDAPI_HapticDevice temp;
  97. temp.ctx = ctx;
  98. temp.driver = drivers[i];
  99. temp.joystick = joystick;
  100. temp.driver->Close(&temp);
  101. return SDL_OutOfMemory();
  102. }
  103. device->driver = drivers[i];
  104. device->haptic = haptic;
  105. device->joystick = joystick;
  106. device->ctx = ctx;
  107. list_node = SDL_malloc(sizeof(haptic_list_node));
  108. if (list_node == NULL) {
  109. device->driver->Close(device);
  110. SDL_free(device);
  111. return SDL_OutOfMemory();
  112. }
  113. haptic->hwdata = (struct haptic_hwdata *)device;
  114. // this is outside of the syshaptic driver
  115. haptic->neffects = device->driver->NumEffects(device);
  116. haptic->nplaying = device->driver->NumEffectsPlaying(device);
  117. haptic->supported = device->driver->GetFeatures(device);
  118. haptic->naxes = device->driver->NumAxes(device);
  119. // outside of SYS_HAPTIC
  120. haptic->instance_id = 255;
  121. list_node->haptic = haptic;
  122. list_node->next = NULL;
  123. // grab a joystick ref so that it doesn't get fully destroyed before the haptic is closed
  124. SDL_OpenJoystick(SDL_GetJoystickID(joystick));
  125. SDL_LockMutex(haptic_list_mutex);
  126. if (haptic_list_head == NULL) {
  127. haptic_list_head = list_node;
  128. } else {
  129. haptic_list_node *cur = haptic_list_head;
  130. while(cur->next != NULL) {
  131. cur = cur->next;
  132. }
  133. cur->next = list_node;
  134. }
  135. SDL_UnlockMutex(haptic_list_mutex);
  136. return true;
  137. }
  138. }
  139. return SDL_SetError("No supported HIDAPI haptic driver found for joystick");
  140. }
  141. bool SDL_HIDAPI_JoystickSameHaptic(SDL_Haptic *haptic, SDL_Joystick *joystick)
  142. {
  143. SDL_HIDAPI_HapticDevice *device;
  144. SDL_AssertJoysticksLocked();
  145. if (joystick->driver != &SDL_HIDAPI_JoystickDriver) {
  146. return false;
  147. }
  148. device = (SDL_HIDAPI_HapticDevice *)haptic->hwdata;
  149. if (joystick == device->joystick) {
  150. return true;
  151. }
  152. return false;
  153. }
  154. void SDL_HIDAPI_HapticClose(SDL_Haptic *haptic)
  155. {
  156. haptic_list_node *cur, *last;
  157. SDL_LockMutex(haptic_list_mutex);
  158. cur = haptic_list_head;
  159. last = NULL;
  160. while (cur != NULL) {
  161. if (cur->haptic == haptic) {
  162. SDL_HIDAPI_HapticDevice *device = (SDL_HIDAPI_HapticDevice *)haptic->hwdata;
  163. device->driver->Close(device);
  164. // a reference was grabbed during open, now release it
  165. SDL_CloseJoystick(device->joystick);
  166. if (cur == haptic_list_head) {
  167. haptic_list_head = cur->next;
  168. } else {
  169. last->next = cur->next;
  170. }
  171. SDL_free(device->ctx);
  172. SDL_free(device);
  173. SDL_free(cur);
  174. SDL_UnlockMutex(haptic_list_mutex);
  175. return;
  176. }
  177. last = cur;
  178. cur = cur->next;
  179. }
  180. SDL_UnlockMutex(haptic_list_mutex);
  181. }
  182. void SDL_HIDAPI_HapticQuit(void)
  183. {
  184. // the list is cleared in SDL_haptic.c
  185. if (haptic_list_mutex != NULL) {
  186. SDL_DestroyMutex(haptic_list_mutex);
  187. haptic_list_mutex = NULL;
  188. }
  189. }
  190. SDL_HapticEffectID SDL_HIDAPI_HapticNewEffect(SDL_Haptic *haptic, const SDL_HapticEffect *base)
  191. {
  192. SDL_HIDAPI_HapticDevice *device = (SDL_HIDAPI_HapticDevice *)haptic->hwdata;
  193. return device->driver->CreateEffect(device, base);
  194. }
  195. bool SDL_HIDAPI_HapticUpdateEffect(SDL_Haptic *haptic, SDL_HapticEffectID id, const SDL_HapticEffect *data)
  196. {
  197. SDL_HIDAPI_HapticDevice *device = (SDL_HIDAPI_HapticDevice *)haptic->hwdata;
  198. return device->driver->UpdateEffect(device, id, data);
  199. }
  200. bool SDL_HIDAPI_HapticRunEffect(SDL_Haptic *haptic, SDL_HapticEffectID id, Uint32 iterations)
  201. {
  202. SDL_HIDAPI_HapticDevice *device = (SDL_HIDAPI_HapticDevice *)haptic->hwdata;
  203. return device->driver->RunEffect(device, id, iterations);
  204. }
  205. bool SDL_HIDAPI_HapticStopEffect(SDL_Haptic *haptic, SDL_HapticEffectID id)
  206. {
  207. SDL_HIDAPI_HapticDevice *device = (SDL_HIDAPI_HapticDevice *)haptic->hwdata;
  208. return device->driver->StopEffect(device, id);
  209. }
  210. void SDL_HIDAPI_HapticDestroyEffect(SDL_Haptic *haptic, SDL_HapticEffectID id)
  211. {
  212. SDL_HIDAPI_HapticDevice *device = (SDL_HIDAPI_HapticDevice *)haptic->hwdata;
  213. device->driver->DestroyEffect(device, id);
  214. }
  215. bool SDL_HIDAPI_HapticGetEffectStatus(SDL_Haptic *haptic, SDL_HapticEffectID id)
  216. {
  217. SDL_HIDAPI_HapticDevice *device = (SDL_HIDAPI_HapticDevice *)haptic->hwdata;
  218. return device->driver->GetEffectStatus(device, id);
  219. }
  220. bool SDL_HIDAPI_HapticSetGain(SDL_Haptic *haptic, int gain)
  221. {
  222. SDL_HIDAPI_HapticDevice *device = (SDL_HIDAPI_HapticDevice *)haptic->hwdata;
  223. return device->driver->SetGain(device, gain);
  224. }
  225. bool SDL_HIDAPI_HapticSetAutocenter(SDL_Haptic *haptic, int autocenter)
  226. {
  227. SDL_HIDAPI_HapticDevice *device = (SDL_HIDAPI_HapticDevice *)haptic->hwdata;
  228. return device->driver->SetAutocenter(device, autocenter);
  229. }
  230. bool SDL_HIDAPI_HapticPause(SDL_Haptic *haptic)
  231. {
  232. SDL_HIDAPI_HapticDevice *device = (SDL_HIDAPI_HapticDevice *)haptic->hwdata;
  233. return device->driver->Pause(device);
  234. }
  235. bool SDL_HIDAPI_HapticResume(SDL_Haptic *haptic)
  236. {
  237. SDL_HIDAPI_HapticDevice *device = (SDL_HIDAPI_HapticDevice *)haptic->hwdata;
  238. return device->driver->Resume(device);
  239. }
  240. bool SDL_HIDAPI_HapticStopAll(SDL_Haptic *haptic)
  241. {
  242. SDL_HIDAPI_HapticDevice *device = (SDL_HIDAPI_HapticDevice *)haptic->hwdata;
  243. return device->driver->StopEffects(device);
  244. }
  245. #endif //SDL_JOYSTICK_HIDAPI