SDL_hidapi_rumble.c 7.5 KB

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