diff options
| author | 3gg <3gg@shellblade.net> | 2025-12-27 12:03:39 -0800 |
|---|---|---|
| committer | 3gg <3gg@shellblade.net> | 2025-12-27 12:03:39 -0800 |
| commit | 5a079a2d114f96d4847d1ee305d5b7c16eeec50e (patch) | |
| tree | 8926ab44f168acf787d8e19608857b3af0f82758 /contrib/SDL-3.2.8/src/video/windows/SDL_windowsrawinput.c | |
Initial commit
Diffstat (limited to 'contrib/SDL-3.2.8/src/video/windows/SDL_windowsrawinput.c')
| -rw-r--r-- | contrib/SDL-3.2.8/src/video/windows/SDL_windowsrawinput.c | 262 |
1 files changed, 262 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/video/windows/SDL_windowsrawinput.c b/contrib/SDL-3.2.8/src/video/windows/SDL_windowsrawinput.c new file mode 100644 index 0000000..fa24991 --- /dev/null +++ b/contrib/SDL-3.2.8/src/video/windows/SDL_windowsrawinput.c | |||
| @@ -0,0 +1,262 @@ | |||
| 1 | /* | ||
| 2 | Simple DirectMedia Layer | ||
| 3 | Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org> | ||
| 4 | |||
| 5 | This software is provided 'as-is', without any express or implied | ||
| 6 | warranty. In no event will the authors be held liable for any damages | ||
| 7 | arising from the use of this software. | ||
| 8 | |||
| 9 | Permission is granted to anyone to use this software for any purpose, | ||
| 10 | including commercial applications, and to alter it and redistribute it | ||
| 11 | freely, subject to the following restrictions: | ||
| 12 | |||
| 13 | 1. The origin of this software must not be misrepresented; you must not | ||
| 14 | claim that you wrote the original software. If you use this software | ||
| 15 | in a product, an acknowledgment in the product documentation would be | ||
| 16 | appreciated but is not required. | ||
| 17 | 2. Altered source versions must be plainly marked as such, and must not be | ||
| 18 | misrepresented as being the original software. | ||
| 19 | 3. This notice may not be removed or altered from any source distribution. | ||
| 20 | */ | ||
| 21 | #include "SDL_internal.h" | ||
| 22 | |||
| 23 | #if defined(SDL_VIDEO_DRIVER_WINDOWS) | ||
| 24 | |||
| 25 | #include "SDL_windowsvideo.h" | ||
| 26 | |||
| 27 | #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES) | ||
| 28 | |||
| 29 | #include "SDL_windowsevents.h" | ||
| 30 | |||
| 31 | #include "../../joystick/usb_ids.h" | ||
| 32 | #include "../../events/SDL_events_c.h" | ||
| 33 | |||
| 34 | #define ENABLE_RAW_MOUSE_INPUT 0x01 | ||
| 35 | #define ENABLE_RAW_KEYBOARD_INPUT 0x02 | ||
| 36 | |||
| 37 | typedef struct | ||
| 38 | { | ||
| 39 | bool done; | ||
| 40 | Uint32 flags; | ||
| 41 | HANDLE ready_event; | ||
| 42 | HANDLE done_event; | ||
| 43 | HANDLE thread; | ||
| 44 | } RawInputThreadData; | ||
| 45 | |||
| 46 | static RawInputThreadData thread_data = { | ||
| 47 | false, | ||
| 48 | 0, | ||
| 49 | INVALID_HANDLE_VALUE, | ||
| 50 | INVALID_HANDLE_VALUE, | ||
| 51 | INVALID_HANDLE_VALUE | ||
| 52 | }; | ||
| 53 | |||
| 54 | static DWORD WINAPI WIN_RawInputThread(LPVOID param) | ||
| 55 | { | ||
| 56 | SDL_VideoDevice *_this = SDL_GetVideoDevice(); | ||
| 57 | RawInputThreadData *data = (RawInputThreadData *)param; | ||
| 58 | RAWINPUTDEVICE devices[2]; | ||
| 59 | HWND window; | ||
| 60 | UINT count = 0; | ||
| 61 | |||
| 62 | window = CreateWindowEx(0, TEXT("Message"), NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL); | ||
| 63 | if (!window) { | ||
| 64 | return 0; | ||
| 65 | } | ||
| 66 | |||
| 67 | SDL_zeroa(devices); | ||
| 68 | |||
| 69 | if (data->flags & ENABLE_RAW_MOUSE_INPUT) { | ||
| 70 | devices[count].usUsagePage = USB_USAGEPAGE_GENERIC_DESKTOP; | ||
| 71 | devices[count].usUsage = USB_USAGE_GENERIC_MOUSE; | ||
| 72 | devices[count].dwFlags = 0; | ||
| 73 | devices[count].hwndTarget = window; | ||
| 74 | ++count; | ||
| 75 | } | ||
| 76 | |||
| 77 | if (data->flags & ENABLE_RAW_KEYBOARD_INPUT) { | ||
| 78 | devices[count].usUsagePage = USB_USAGEPAGE_GENERIC_DESKTOP; | ||
| 79 | devices[count].usUsage = USB_USAGE_GENERIC_KEYBOARD; | ||
| 80 | devices[count].dwFlags = 0; | ||
| 81 | devices[count].hwndTarget = window; | ||
| 82 | ++count; | ||
| 83 | } | ||
| 84 | |||
| 85 | if (!RegisterRawInputDevices(devices, count, sizeof(devices[0]))) { | ||
| 86 | DestroyWindow(window); | ||
| 87 | return 0; | ||
| 88 | } | ||
| 89 | |||
| 90 | // Make sure we get events as soon as possible | ||
| 91 | SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL); | ||
| 92 | |||
| 93 | // Tell the parent we're ready to go! | ||
| 94 | SetEvent(data->ready_event); | ||
| 95 | |||
| 96 | while (!data->done) { | ||
| 97 | Uint64 idle_begin = SDL_GetTicksNS(); | ||
| 98 | DWORD result = MsgWaitForMultipleObjects(1, &data->done_event, FALSE, INFINITE, QS_RAWINPUT); | ||
| 99 | Uint64 idle_end = SDL_GetTicksNS(); | ||
| 100 | if (result != (WAIT_OBJECT_0 + 1)) { | ||
| 101 | break; | ||
| 102 | } | ||
| 103 | |||
| 104 | // Clear the queue status so MsgWaitForMultipleObjects() will wait again | ||
| 105 | (void)GetQueueStatus(QS_RAWINPUT); | ||
| 106 | |||
| 107 | Uint64 idle_time = idle_end - idle_begin; | ||
| 108 | Uint64 usb_8khz_interval = SDL_US_TO_NS(125); | ||
| 109 | Uint64 poll_start = idle_time < usb_8khz_interval ? _this->internal->last_rawinput_poll : idle_end; | ||
| 110 | |||
| 111 | WIN_PollRawInput(_this, poll_start); | ||
| 112 | } | ||
| 113 | |||
| 114 | devices[0].dwFlags |= RIDEV_REMOVE; | ||
| 115 | devices[1].dwFlags |= RIDEV_REMOVE; | ||
| 116 | RegisterRawInputDevices(devices, count, sizeof(devices[0])); | ||
| 117 | |||
| 118 | DestroyWindow(window); | ||
| 119 | |||
| 120 | return 0; | ||
| 121 | } | ||
| 122 | |||
| 123 | static void CleanupRawInputThreadData(RawInputThreadData *data) | ||
| 124 | { | ||
| 125 | if (data->thread != INVALID_HANDLE_VALUE) { | ||
| 126 | data->done = true; | ||
| 127 | SetEvent(data->done_event); | ||
| 128 | WaitForSingleObject(data->thread, 3000); | ||
| 129 | CloseHandle(data->thread); | ||
| 130 | data->thread = INVALID_HANDLE_VALUE; | ||
| 131 | } | ||
| 132 | |||
| 133 | if (data->ready_event != INVALID_HANDLE_VALUE) { | ||
| 134 | CloseHandle(data->ready_event); | ||
| 135 | data->ready_event = INVALID_HANDLE_VALUE; | ||
| 136 | } | ||
| 137 | |||
| 138 | if (data->done_event != INVALID_HANDLE_VALUE) { | ||
| 139 | CloseHandle(data->done_event); | ||
| 140 | data->done_event = INVALID_HANDLE_VALUE; | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | static bool WIN_SetRawInputEnabled(SDL_VideoDevice *_this, Uint32 flags) | ||
| 145 | { | ||
| 146 | bool result = false; | ||
| 147 | |||
| 148 | CleanupRawInputThreadData(&thread_data); | ||
| 149 | |||
| 150 | if (flags) { | ||
| 151 | HANDLE handles[2]; | ||
| 152 | |||
| 153 | thread_data.flags = flags; | ||
| 154 | thread_data.ready_event = CreateEvent(NULL, FALSE, FALSE, NULL); | ||
| 155 | if (thread_data.ready_event == INVALID_HANDLE_VALUE) { | ||
| 156 | WIN_SetError("CreateEvent"); | ||
| 157 | goto done; | ||
| 158 | } | ||
| 159 | |||
| 160 | thread_data.done = false; | ||
| 161 | thread_data.done_event = CreateEvent(NULL, FALSE, FALSE, NULL); | ||
| 162 | if (thread_data.done_event == INVALID_HANDLE_VALUE) { | ||
| 163 | WIN_SetError("CreateEvent"); | ||
| 164 | goto done; | ||
| 165 | } | ||
| 166 | |||
| 167 | thread_data.thread = CreateThread(NULL, 0, WIN_RawInputThread, &thread_data, 0, NULL); | ||
| 168 | if (thread_data.thread == INVALID_HANDLE_VALUE) { | ||
| 169 | WIN_SetError("CreateThread"); | ||
| 170 | goto done; | ||
| 171 | } | ||
| 172 | |||
| 173 | // Wait for the thread to signal ready or exit | ||
| 174 | handles[0] = thread_data.ready_event; | ||
| 175 | handles[1] = thread_data.thread; | ||
| 176 | if (WaitForMultipleObjects(2, handles, FALSE, INFINITE) != WAIT_OBJECT_0) { | ||
| 177 | SDL_SetError("Couldn't set up raw input handling"); | ||
| 178 | goto done; | ||
| 179 | } | ||
| 180 | result = true; | ||
| 181 | } else { | ||
| 182 | result = true; | ||
| 183 | } | ||
| 184 | |||
| 185 | done: | ||
| 186 | if (!result) { | ||
| 187 | CleanupRawInputThreadData(&thread_data); | ||
| 188 | } | ||
| 189 | return result; | ||
| 190 | } | ||
| 191 | |||
| 192 | static bool WIN_UpdateRawInputEnabled(SDL_VideoDevice *_this) | ||
| 193 | { | ||
| 194 | SDL_VideoData *data = _this->internal; | ||
| 195 | Uint32 flags = 0; | ||
| 196 | if (data->raw_mouse_enabled) { | ||
| 197 | flags |= ENABLE_RAW_MOUSE_INPUT; | ||
| 198 | } | ||
| 199 | if (data->raw_keyboard_enabled) { | ||
| 200 | flags |= ENABLE_RAW_KEYBOARD_INPUT; | ||
| 201 | } | ||
| 202 | if (flags != data->raw_input_enabled) { | ||
| 203 | if (WIN_SetRawInputEnabled(_this, flags)) { | ||
| 204 | data->raw_input_enabled = flags; | ||
| 205 | } else { | ||
| 206 | return false; | ||
| 207 | } | ||
| 208 | } | ||
| 209 | return true; | ||
| 210 | } | ||
| 211 | |||
| 212 | bool WIN_SetRawMouseEnabled(SDL_VideoDevice *_this, bool enabled) | ||
| 213 | { | ||
| 214 | SDL_VideoData *data = _this->internal; | ||
| 215 | data->raw_mouse_enabled = enabled; | ||
| 216 | if (data->gameinput_context) { | ||
| 217 | if (!WIN_UpdateGameInputEnabled(_this)) { | ||
| 218 | data->raw_mouse_enabled = !enabled; | ||
| 219 | return false; | ||
| 220 | } | ||
| 221 | } else { | ||
| 222 | if (!WIN_UpdateRawInputEnabled(_this)) { | ||
| 223 | data->raw_mouse_enabled = !enabled; | ||
| 224 | return false; | ||
| 225 | } | ||
| 226 | } | ||
| 227 | return true; | ||
| 228 | } | ||
| 229 | |||
| 230 | bool WIN_SetRawKeyboardEnabled(SDL_VideoDevice *_this, bool enabled) | ||
| 231 | { | ||
| 232 | SDL_VideoData *data = _this->internal; | ||
| 233 | data->raw_keyboard_enabled = enabled; | ||
| 234 | if (data->gameinput_context) { | ||
| 235 | if (!WIN_UpdateGameInputEnabled(_this)) { | ||
| 236 | data->raw_keyboard_enabled = !enabled; | ||
| 237 | return false; | ||
| 238 | } | ||
| 239 | } else { | ||
| 240 | if (!WIN_UpdateRawInputEnabled(_this)) { | ||
| 241 | data->raw_keyboard_enabled = !enabled; | ||
| 242 | return false; | ||
| 243 | } | ||
| 244 | } | ||
| 245 | return true; | ||
| 246 | } | ||
| 247 | |||
| 248 | #else | ||
| 249 | |||
| 250 | bool WIN_SetRawMouseEnabled(SDL_VideoDevice *_this, bool enabled) | ||
| 251 | { | ||
| 252 | return SDL_Unsupported(); | ||
| 253 | } | ||
| 254 | |||
| 255 | bool WIN_SetRawKeyboardEnabled(SDL_VideoDevice *_this, bool enabled) | ||
| 256 | { | ||
| 257 | return SDL_Unsupported(); | ||
| 258 | } | ||
| 259 | |||
| 260 | #endif // !SDL_PLATFORM_XBOXONE && !SDL_PLATFORM_XBOXSERIES | ||
| 261 | |||
| 262 | #endif // SDL_VIDEO_DRIVER_WINDOWS | ||
