SDL_windowsrawinput.c 7.0 KB

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