SDL_windowsrawinput.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 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_VIDEO_DRIVER_WINDOWS)
  20. #include "SDL_windowsvideo.h"
  21. #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  22. #include "SDL_windowsevents.h"
  23. #include "../../joystick/usb_ids.h"
  24. #include "../../events/SDL_events_c.h"
  25. #include "../../thread/SDL_systhread.h"
  26. #define ENABLE_RAW_MOUSE_INPUT 0x01
  27. #define ENABLE_RAW_KEYBOARD_INPUT 0x02
  28. typedef struct
  29. {
  30. bool done;
  31. Uint32 flags;
  32. HANDLE ready_event;
  33. HANDLE done_event;
  34. HANDLE thread;
  35. } RawInputThreadData;
  36. static RawInputThreadData thread_data = {
  37. false,
  38. 0,
  39. INVALID_HANDLE_VALUE,
  40. INVALID_HANDLE_VALUE,
  41. INVALID_HANDLE_VALUE
  42. };
  43. static DWORD WINAPI WIN_RawInputThread(LPVOID param)
  44. {
  45. SDL_VideoDevice *_this = SDL_GetVideoDevice();
  46. RawInputThreadData *data = (RawInputThreadData *)param;
  47. RAWINPUTDEVICE devices[2];
  48. HWND window;
  49. UINT count = 0;
  50. SDL_SYS_SetupThread("SDLRawInput");
  51. window = CreateWindowEx(0, TEXT("Message"), NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL);
  52. if (!window) {
  53. return 0;
  54. }
  55. SDL_zeroa(devices);
  56. if (data->flags & ENABLE_RAW_MOUSE_INPUT) {
  57. devices[count].usUsagePage = USB_USAGEPAGE_GENERIC_DESKTOP;
  58. devices[count].usUsage = USB_USAGE_GENERIC_MOUSE;
  59. devices[count].dwFlags = 0;
  60. devices[count].hwndTarget = window;
  61. ++count;
  62. }
  63. if (data->flags & ENABLE_RAW_KEYBOARD_INPUT) {
  64. devices[count].usUsagePage = USB_USAGEPAGE_GENERIC_DESKTOP;
  65. devices[count].usUsage = USB_USAGE_GENERIC_KEYBOARD;
  66. devices[count].dwFlags = 0;
  67. devices[count].hwndTarget = window;
  68. ++count;
  69. }
  70. if (!RegisterRawInputDevices(devices, count, sizeof(devices[0]))) {
  71. DestroyWindow(window);
  72. return 0;
  73. }
  74. // Make sure we get events as soon as possible
  75. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
  76. // Tell the parent we're ready to go!
  77. SetEvent(data->ready_event);
  78. while (!data->done) {
  79. Uint64 idle_begin = SDL_GetTicksNS();
  80. DWORD result = MsgWaitForMultipleObjects(1, &data->done_event, FALSE, INFINITE, QS_RAWINPUT);
  81. Uint64 idle_end = SDL_GetTicksNS();
  82. if (result != (WAIT_OBJECT_0 + 1)) {
  83. break;
  84. }
  85. // Clear the queue status so MsgWaitForMultipleObjects() will wait again
  86. (void)GetQueueStatus(QS_RAWINPUT);
  87. Uint64 idle_time = idle_end - idle_begin;
  88. Uint64 usb_8khz_interval = SDL_US_TO_NS(125);
  89. Uint64 poll_start = idle_time < usb_8khz_interval ? _this->internal->last_rawinput_poll : idle_end;
  90. WIN_PollRawInput(_this, poll_start);
  91. }
  92. devices[0].dwFlags |= RIDEV_REMOVE;
  93. devices[0].hwndTarget = NULL;
  94. devices[1].dwFlags |= RIDEV_REMOVE;
  95. devices[1].hwndTarget = NULL;
  96. RegisterRawInputDevices(devices, count, sizeof(devices[0]));
  97. DestroyWindow(window);
  98. return 0;
  99. }
  100. static void CleanupRawInputThreadData(RawInputThreadData *data)
  101. {
  102. if (data->thread != INVALID_HANDLE_VALUE) {
  103. data->done = true;
  104. SetEvent(data->done_event);
  105. WaitForSingleObject(data->thread, 3000);
  106. CloseHandle(data->thread);
  107. data->thread = INVALID_HANDLE_VALUE;
  108. }
  109. if (data->ready_event != INVALID_HANDLE_VALUE) {
  110. CloseHandle(data->ready_event);
  111. data->ready_event = INVALID_HANDLE_VALUE;
  112. }
  113. if (data->done_event != INVALID_HANDLE_VALUE) {
  114. CloseHandle(data->done_event);
  115. data->done_event = INVALID_HANDLE_VALUE;
  116. }
  117. }
  118. static bool WIN_SetRawInputEnabled(SDL_VideoDevice *_this, Uint32 flags)
  119. {
  120. bool result = false;
  121. CleanupRawInputThreadData(&thread_data);
  122. if (flags) {
  123. HANDLE handles[2];
  124. thread_data.flags = flags;
  125. thread_data.ready_event = CreateEvent(NULL, FALSE, FALSE, NULL);
  126. if (thread_data.ready_event == INVALID_HANDLE_VALUE) {
  127. WIN_SetError("CreateEvent");
  128. goto done;
  129. }
  130. thread_data.done = false;
  131. thread_data.done_event = CreateEvent(NULL, FALSE, FALSE, NULL);
  132. if (thread_data.done_event == INVALID_HANDLE_VALUE) {
  133. WIN_SetError("CreateEvent");
  134. goto done;
  135. }
  136. thread_data.thread = CreateThread(NULL, 0, WIN_RawInputThread, &thread_data, 0, NULL);
  137. if (thread_data.thread == INVALID_HANDLE_VALUE) {
  138. WIN_SetError("CreateThread");
  139. goto done;
  140. }
  141. // Wait for the thread to signal ready or exit
  142. handles[0] = thread_data.ready_event;
  143. handles[1] = thread_data.thread;
  144. if (WaitForMultipleObjects(2, handles, FALSE, INFINITE) != WAIT_OBJECT_0) {
  145. SDL_SetError("Couldn't set up raw input handling");
  146. goto done;
  147. }
  148. result = true;
  149. } else {
  150. result = true;
  151. }
  152. done:
  153. if (!result) {
  154. CleanupRawInputThreadData(&thread_data);
  155. }
  156. return result;
  157. }
  158. static bool WIN_UpdateRawInputEnabled(SDL_VideoDevice *_this)
  159. {
  160. SDL_VideoData *data = _this->internal;
  161. Uint32 flags = 0;
  162. if (data->raw_mouse_enabled) {
  163. flags |= ENABLE_RAW_MOUSE_INPUT;
  164. }
  165. if (data->raw_keyboard_enabled) {
  166. flags |= ENABLE_RAW_KEYBOARD_INPUT;
  167. }
  168. if (flags != data->raw_input_enabled) {
  169. if (WIN_SetRawInputEnabled(_this, flags)) {
  170. data->raw_input_enabled = flags;
  171. } else {
  172. return false;
  173. }
  174. }
  175. return true;
  176. }
  177. bool WIN_SetRawMouseEnabled(SDL_VideoDevice *_this, bool enabled)
  178. {
  179. SDL_VideoData *data = _this->internal;
  180. data->raw_mouse_enabled = enabled;
  181. if (data->gameinput_context) {
  182. if (!WIN_UpdateGameInputEnabled(_this)) {
  183. data->raw_mouse_enabled = !enabled;
  184. return false;
  185. }
  186. } else {
  187. if (!WIN_UpdateRawInputEnabled(_this)) {
  188. data->raw_mouse_enabled = !enabled;
  189. return false;
  190. }
  191. }
  192. return true;
  193. }
  194. bool WIN_SetRawKeyboardEnabled(SDL_VideoDevice *_this, bool enabled)
  195. {
  196. SDL_VideoData *data = _this->internal;
  197. data->raw_keyboard_enabled = enabled;
  198. if (data->gameinput_context) {
  199. if (!WIN_UpdateGameInputEnabled(_this)) {
  200. data->raw_keyboard_enabled = !enabled;
  201. return false;
  202. }
  203. } else {
  204. if (!WIN_UpdateRawInputEnabled(_this)) {
  205. data->raw_keyboard_enabled = !enabled;
  206. return false;
  207. }
  208. }
  209. return true;
  210. }
  211. #else
  212. bool WIN_SetRawMouseEnabled(SDL_VideoDevice *_this, bool enabled)
  213. {
  214. return SDL_Unsupported();
  215. }
  216. bool WIN_SetRawKeyboardEnabled(SDL_VideoDevice *_this, bool enabled)
  217. {
  218. return SDL_Unsupported();
  219. }
  220. #endif // !SDL_PLATFORM_XBOXONE && !SDL_PLATFORM_XBOXSERIES
  221. #endif // SDL_VIDEO_DRIVER_WINDOWS