SDL_hidapi_rumble.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. #ifdef SDL_JOYSTICK_HIDAPI
  20. /* Handle rumble on a separate thread so it doesn't block the application */
  21. #include "SDL_hidapijoystick_c.h"
  22. #include "SDL_hidapi_rumble.h"
  23. #include "../../thread/SDL_systhread.h"
  24. typedef struct SDL_HIDAPI_RumbleRequest
  25. {
  26. SDL_HIDAPI_Device *device;
  27. Uint8 data[2 * USB_PACKET_LENGTH]; /* need enough space for the biggest report: dualshock4 is 78 bytes */
  28. int size;
  29. SDL_HIDAPI_RumbleSentCallback callback;
  30. void *userdata;
  31. struct SDL_HIDAPI_RumbleRequest *prev;
  32. } SDL_HIDAPI_RumbleRequest;
  33. typedef struct SDL_HIDAPI_RumbleContext
  34. {
  35. SDL_AtomicInt initialized;
  36. SDL_AtomicInt running;
  37. SDL_Thread *thread;
  38. SDL_sem *request_sem;
  39. SDL_HIDAPI_RumbleRequest *requests_head;
  40. SDL_HIDAPI_RumbleRequest *requests_tail;
  41. } SDL_HIDAPI_RumbleContext;
  42. #ifndef SDL_THREAD_SAFETY_ANALYSIS
  43. static
  44. #endif
  45. SDL_mutex *SDL_HIDAPI_rumble_lock;
  46. static SDL_HIDAPI_RumbleContext rumble_context SDL_GUARDED_BY(SDL_HIDAPI_rumble_lock);
  47. static int SDLCALL SDL_HIDAPI_RumbleThread(void *data)
  48. {
  49. SDL_HIDAPI_RumbleContext *ctx = (SDL_HIDAPI_RumbleContext *)data;
  50. SDL_SetThreadPriority(SDL_THREAD_PRIORITY_HIGH);
  51. while (SDL_AtomicGet(&ctx->running)) {
  52. SDL_HIDAPI_RumbleRequest *request = NULL;
  53. SDL_WaitSemaphore(ctx->request_sem);
  54. SDL_LockMutex(SDL_HIDAPI_rumble_lock);
  55. request = ctx->requests_tail;
  56. if (request) {
  57. if (request == ctx->requests_head) {
  58. ctx->requests_head = NULL;
  59. }
  60. ctx->requests_tail = request->prev;
  61. }
  62. SDL_UnlockMutex(SDL_HIDAPI_rumble_lock);
  63. if (request) {
  64. SDL_LockMutex(request->device->dev_lock);
  65. if (request->device->dev) {
  66. #ifdef DEBUG_RUMBLE
  67. HIDAPI_DumpPacket("Rumble packet: size = %d", request->data, request->size);
  68. #endif
  69. SDL_hid_write(request->device->dev, request->data, request->size);
  70. }
  71. SDL_UnlockMutex(request->device->dev_lock);
  72. if (request->callback) {
  73. request->callback(request->userdata);
  74. }
  75. (void)SDL_AtomicDecRef(&request->device->rumble_pending);
  76. SDL_free(request);
  77. /* Make sure we're not starving report reads when there's lots of rumble */
  78. SDL_Delay(10);
  79. }
  80. }
  81. return 0;
  82. }
  83. static void SDL_HIDAPI_StopRumbleThread(SDL_HIDAPI_RumbleContext *ctx)
  84. {
  85. SDL_HIDAPI_RumbleRequest *request;
  86. SDL_AtomicSet(&ctx->running, SDL_FALSE);
  87. if (ctx->thread) {
  88. int result;
  89. SDL_PostSemaphore(ctx->request_sem);
  90. SDL_WaitThread(ctx->thread, &result);
  91. ctx->thread = NULL;
  92. }
  93. SDL_LockMutex(SDL_HIDAPI_rumble_lock);
  94. while (ctx->requests_tail) {
  95. request = ctx->requests_tail;
  96. if (request == ctx->requests_head) {
  97. ctx->requests_head = NULL;
  98. }
  99. ctx->requests_tail = request->prev;
  100. if (request->callback) {
  101. request->callback(request->userdata);
  102. }
  103. (void)SDL_AtomicDecRef(&request->device->rumble_pending);
  104. SDL_free(request);
  105. }
  106. SDL_UnlockMutex(SDL_HIDAPI_rumble_lock);
  107. if (ctx->request_sem) {
  108. SDL_DestroySemaphore(ctx->request_sem);
  109. ctx->request_sem = NULL;
  110. }
  111. if (SDL_HIDAPI_rumble_lock) {
  112. SDL_DestroyMutex(SDL_HIDAPI_rumble_lock);
  113. SDL_HIDAPI_rumble_lock = NULL;
  114. }
  115. SDL_AtomicSet(&ctx->initialized, SDL_FALSE);
  116. }
  117. static int SDL_HIDAPI_StartRumbleThread(SDL_HIDAPI_RumbleContext *ctx)
  118. {
  119. SDL_HIDAPI_rumble_lock = SDL_CreateMutex();
  120. if (!SDL_HIDAPI_rumble_lock) {
  121. SDL_HIDAPI_StopRumbleThread(ctx);
  122. return -1;
  123. }
  124. ctx->request_sem = SDL_CreateSemaphore(0);
  125. if (!ctx->request_sem) {
  126. SDL_HIDAPI_StopRumbleThread(ctx);
  127. return -1;
  128. }
  129. SDL_AtomicSet(&ctx->running, SDL_TRUE);
  130. ctx->thread = SDL_CreateThreadInternal(SDL_HIDAPI_RumbleThread, "HIDAPI Rumble", 0, ctx);
  131. if (!ctx->thread) {
  132. SDL_HIDAPI_StopRumbleThread(ctx);
  133. return -1;
  134. }
  135. return 0;
  136. }
  137. int SDL_HIDAPI_LockRumble(void)
  138. {
  139. SDL_HIDAPI_RumbleContext *ctx = &rumble_context;
  140. if (SDL_AtomicCAS(&ctx->initialized, SDL_FALSE, SDL_TRUE)) {
  141. if (SDL_HIDAPI_StartRumbleThread(ctx) < 0) {
  142. return -1;
  143. }
  144. }
  145. SDL_LockMutex(SDL_HIDAPI_rumble_lock);
  146. return 0;
  147. }
  148. SDL_bool SDL_HIDAPI_GetPendingRumbleLocked(SDL_HIDAPI_Device *device, Uint8 **data, int **size, int *maximum_size)
  149. {
  150. SDL_HIDAPI_RumbleContext *ctx = &rumble_context;
  151. SDL_HIDAPI_RumbleRequest *request, *found;
  152. found = NULL;
  153. for (request = ctx->requests_tail; request; request = request->prev) {
  154. if (request->device == device) {
  155. found = request;
  156. }
  157. }
  158. if (found) {
  159. *data = found->data;
  160. *size = &found->size;
  161. *maximum_size = sizeof(found->data);
  162. return SDL_TRUE;
  163. }
  164. return SDL_FALSE;
  165. }
  166. int SDL_HIDAPI_SendRumbleAndUnlock(SDL_HIDAPI_Device *device, const Uint8 *data, int size)
  167. {
  168. return SDL_HIDAPI_SendRumbleWithCallbackAndUnlock(device, data, size, NULL, NULL);
  169. }
  170. int SDL_HIDAPI_SendRumbleWithCallbackAndUnlock(SDL_HIDAPI_Device *device, const Uint8 *data, int size, SDL_HIDAPI_RumbleSentCallback callback, void *userdata)
  171. {
  172. SDL_HIDAPI_RumbleContext *ctx = &rumble_context;
  173. SDL_HIDAPI_RumbleRequest *request;
  174. if (size > sizeof(request->data)) {
  175. SDL_HIDAPI_UnlockRumble();
  176. return SDL_SetError("Couldn't send rumble, size %d is greater than %d", size, (int)sizeof(request->data));
  177. }
  178. request = (SDL_HIDAPI_RumbleRequest *)SDL_calloc(1, sizeof(*request));
  179. if (request == NULL) {
  180. SDL_HIDAPI_UnlockRumble();
  181. return SDL_OutOfMemory();
  182. }
  183. request->device = device;
  184. SDL_memcpy(request->data, data, size);
  185. request->size = size;
  186. request->callback = callback;
  187. request->userdata = userdata;
  188. SDL_AtomicIncRef(&device->rumble_pending);
  189. if (ctx->requests_head) {
  190. ctx->requests_head->prev = request;
  191. } else {
  192. ctx->requests_tail = request;
  193. }
  194. ctx->requests_head = request;
  195. /* Make sure we unlock before posting the semaphore so the rumble thread can run immediately */
  196. SDL_HIDAPI_UnlockRumble();
  197. SDL_PostSemaphore(ctx->request_sem);
  198. return size;
  199. }
  200. void SDL_HIDAPI_UnlockRumble(void)
  201. {
  202. SDL_UnlockMutex(SDL_HIDAPI_rumble_lock);
  203. }
  204. int SDL_HIDAPI_SendRumble(SDL_HIDAPI_Device *device, const Uint8 *data, int size)
  205. {
  206. Uint8 *pending_data;
  207. int *pending_size;
  208. int maximum_size;
  209. if (size <= 0) {
  210. return SDL_SetError("Tried to send rumble with invalid size");
  211. }
  212. if (SDL_HIDAPI_LockRumble() != 0) {
  213. return -1;
  214. }
  215. /* check if there is a pending request for the device and update it */
  216. if (SDL_HIDAPI_GetPendingRumbleLocked(device, &pending_data, &pending_size, &maximum_size) &&
  217. size == *pending_size && data[0] == pending_data[0]) {
  218. SDL_memcpy(pending_data, data, size);
  219. SDL_HIDAPI_UnlockRumble();
  220. return size;
  221. }
  222. return SDL_HIDAPI_SendRumbleAndUnlock(device, data, size);
  223. }
  224. void SDL_HIDAPI_QuitRumble(void)
  225. {
  226. SDL_HIDAPI_RumbleContext *ctx = &rumble_context;
  227. if (SDL_AtomicGet(&ctx->running)) {
  228. SDL_HIDAPI_StopRumbleThread(ctx);
  229. }
  230. }
  231. #endif /* SDL_JOYSTICK_HIDAPI */