Atlas - SDL_windowsrawinput.c

Home / ext / SDL / src / video / windows Lines: 1 | Size: 13481 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2026 Sam Lantinga <[email protected]> 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#include "../../thread/SDL_systhread.h" 34 35#define ENABLE_RAW_MOUSE_INPUT 0x01 36#define ENABLE_RAW_KEYBOARD_INPUT 0x02 37#define RAW_KEYBOARD_FLAG_NOHOTKEYS 0x04 38#define RAW_KEYBOARD_FLAG_INPUTSINK 0x08 39 40typedef struct 41{ 42 bool done; 43 SDL_AtomicU32 flags; // Thread sets this to the actually-set flags if updating state failed 44 HANDLE ready_event; 45 HANDLE signal_event; 46 HANDLE thread; 47} RawInputThreadData; 48 49static RawInputThreadData thread_data = { 0 }; 50 51typedef enum 52{ 53 RINP_QUIT, 54 RINP_UPDATE, 55 RINP_CONTINUE 56} RawInputIterateResult; 57 58static RawInputIterateResult IterateRawInputThread(void) 59{ 60 // The high-order word of GetQueueStatus() will let us know if there's immediate raw input to be processed. 61 // A (necessary!) side effect is that it also marks the message queue bits as stale, 62 // so MsgWaitForMultipleObjects will block. 63 64 // Any pending flag update won't be processed until the queue drains, but this is 65 // at most one poll cycle since GetQueueStatus clears the wake bits. 66 if (HIWORD(GetQueueStatus(QS_RAWINPUT)) & QS_RAWINPUT) { 67 return thread_data.done ? RINP_QUIT : RINP_CONTINUE; 68 } 69 70 static const DWORD WAIT_SIGNAL = WAIT_OBJECT_0; 71 static const DWORD WAIT_INPUT = WAIT_OBJECT_0 + 1; 72 73 // There wasn't anything, so we'll wait until new data (or signal_event) wakes us up. 74 DWORD wait_status = MsgWaitForMultipleObjects(1, &thread_data.signal_event, FALSE, INFINITE, QS_RAWINPUT); 75 if (wait_status == WAIT_SIGNAL) { 76 // signal_event can only be set if we need to update or quit. 77 return thread_data.done ? RINP_QUIT : RINP_UPDATE; 78 } else if (wait_status == WAIT_INPUT) { 79 return RINP_CONTINUE; 80 } else { 81 SDL_LogWarn(SDL_LOG_CATEGORY_INPUT, "Raw input thread exiting, unexpected wait result: %lu", wait_status); 82 return RINP_QUIT; 83 } 84} 85 86static bool UpdateRawInputDeviceFlags(HWND window, Uint32 last_flags, Uint32 new_flags) 87{ 88 // We had nothing enabled, and we're trying to stop everything. Nothing to do. 89 if (last_flags == new_flags) { 90 return true; 91 } 92 93 RAWINPUTDEVICE devices[2] = { 0 }; 94 UINT count = 0; 95 96 if ((new_flags & ENABLE_RAW_MOUSE_INPUT) != (last_flags & ENABLE_RAW_MOUSE_INPUT)) { 97 devices[count].usUsagePage = USB_USAGEPAGE_GENERIC_DESKTOP; 98 devices[count].usUsage = USB_USAGE_GENERIC_MOUSE; 99 100 if (new_flags & ENABLE_RAW_MOUSE_INPUT) { 101 devices[count].dwFlags = 0; 102 devices[count].hwndTarget = window; 103 } else { 104 devices[count].dwFlags = RIDEV_REMOVE; 105 devices[count].hwndTarget = NULL; 106 } 107 108 ++count; 109 } 110 111 const Uint32 old_kb_flags = last_flags & (ENABLE_RAW_KEYBOARD_INPUT | RAW_KEYBOARD_FLAG_NOHOTKEYS | RAW_KEYBOARD_FLAG_INPUTSINK); 112 const Uint32 new_kb_flags = new_flags & (ENABLE_RAW_KEYBOARD_INPUT | RAW_KEYBOARD_FLAG_NOHOTKEYS | RAW_KEYBOARD_FLAG_INPUTSINK); 113 114 if (old_kb_flags != new_kb_flags) { 115 devices[count].usUsagePage = USB_USAGEPAGE_GENERIC_DESKTOP; 116 devices[count].usUsage = USB_USAGE_GENERIC_KEYBOARD; 117 118 if (new_kb_flags & ENABLE_RAW_KEYBOARD_INPUT) { 119 devices[count].dwFlags = 0; 120 devices[count].hwndTarget = window; 121 if (new_kb_flags & RAW_KEYBOARD_FLAG_NOHOTKEYS) { 122 devices[count].dwFlags |= RIDEV_NOHOTKEYS; 123 } 124 125 if (new_kb_flags & RAW_KEYBOARD_FLAG_INPUTSINK) { 126 devices[count].dwFlags |= RIDEV_INPUTSINK; 127 } 128 } else { 129 devices[count].dwFlags = RIDEV_REMOVE; 130 devices[count].hwndTarget = NULL; 131 } 132 133 ++count; 134 } 135 136 if (RegisterRawInputDevices(devices, count, sizeof(devices[0]))) { 137 return true; 138 } 139 return false; 140} 141 142static DWORD WINAPI WIN_RawInputThread(LPVOID param) 143{ 144 SDL_VideoDevice *_this = SDL_GetVideoDevice(); 145 Uint32 last_flags = 0; 146 HWND window; 147 148 SDL_SYS_SetupThread("SDLRawInput"); 149 150 window = CreateWindowEx(0, TEXT("Message"), NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL); 151 if (!window) { 152 return 0; 153 } 154 155 // This doesn't *really* need to be atomic, because the parent is waiting for us 156 last_flags = SDL_GetAtomicU32(&thread_data.flags); 157 if (!UpdateRawInputDeviceFlags(window, 0, last_flags)) { 158 DestroyWindow(window); 159 return 0; 160 } 161 162 // Make sure we get events as soon as possible 163 SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL); 164 165 // Tell the parent we're ready to go! 166 SetEvent(thread_data.ready_event); 167 168 RawInputIterateResult iter_result; 169 170 Uint64 idle_begin = SDL_GetTicksNS(); 171 while ((iter_result = IterateRawInputThread()) != RINP_QUIT) { 172 switch (iter_result) { 173 case RINP_QUIT: // Unreachable 174 break; 175 case RINP_UPDATE: 176 { 177 const Uint32 new_flags = SDL_GetAtomicU32(&thread_data.flags); 178 if (!UpdateRawInputDeviceFlags(window, last_flags, new_flags)) { 179 // Revert the shared flags so the main thread can detect the failure 180 SDL_SetAtomicU32(&thread_data.flags, last_flags); 181 } else { 182 last_flags = new_flags; 183 } 184 } break; 185 case RINP_CONTINUE: 186 { 187 Uint64 idle_end = SDL_GetTicksNS(); 188 Uint64 idle_time = idle_end - idle_begin; 189 Uint64 usb_8khz_interval = SDL_US_TO_NS(125); 190 Uint64 poll_start = idle_time < usb_8khz_interval ? _this->internal->last_rawinput_poll : idle_end; 191 192 WIN_PollRawInput(_this, poll_start); 193 194 // Reset idle_begin for the next go-around 195 idle_begin = SDL_GetTicksNS(); 196 } break; 197 } 198 } 199 200 if (_this->internal->raw_input_fake_pen_id) { 201 SDL_RemovePenDevice(0, SDL_GetKeyboardFocus(), _this->internal->raw_input_fake_pen_id); 202 _this->internal->raw_input_fake_pen_id = 0; 203 } 204 205 // Reset this here, since if we're exiting due to failure, WIN_UpdateRawInputEnabled would see a stale value. 206 SDL_SetAtomicU32(&thread_data.flags, 0); 207 208 UpdateRawInputDeviceFlags(NULL, last_flags, 0); 209 210 DestroyWindow(window); 211 212 return 0; 213} 214 215static void CleanupRawInputThreadData(void) 216{ 217 if (thread_data.thread) { 218 thread_data.done = true; 219 SetEvent(thread_data.signal_event); 220 WaitForSingleObject(thread_data.thread, 3000); 221 CloseHandle(thread_data.thread); 222 } 223 224 if (thread_data.ready_event) { 225 CloseHandle(thread_data.ready_event); 226 } 227 228 if (thread_data.signal_event) { 229 CloseHandle(thread_data.signal_event); 230 } 231 232 thread_data = (RawInputThreadData){ 0 }; 233} 234 235// Computes the desired raw input flags from SDL_VideoData and ensures the 236// raw input thread's device registrations match. 237// Creates the thread on first use, only WIN_QuitRawInput actually shuts it down. 238static bool WIN_UpdateRawInputEnabled(SDL_VideoDevice *_this) 239{ 240 bool result = false; 241 SDL_VideoData *data = _this->internal; 242 Uint32 desired_flags = 0; 243 244 if (data->raw_mouse_enabled) { 245 desired_flags |= ENABLE_RAW_MOUSE_INPUT; 246 } 247 if (data->raw_keyboard_enabled) { 248 desired_flags |= ENABLE_RAW_KEYBOARD_INPUT; 249 } 250 if (data->raw_keyboard_flag_nohotkeys) { 251 desired_flags |= RAW_KEYBOARD_FLAG_NOHOTKEYS; 252 } 253 if (data->raw_keyboard_flag_inputsink) { 254 desired_flags |= RAW_KEYBOARD_FLAG_INPUTSINK; 255 } 256 257 if (desired_flags == SDL_GetAtomicU32(&thread_data.flags)) { 258 result = true; 259 goto done; 260 } 261 262 // If the thread exited unexpectedly (e.g. MsgWaitForMultipleObjects failed), 263 // the handle is stale. Clean it up so the creation path below can recover. 264 if (thread_data.thread && WaitForSingleObject(thread_data.thread, 0) != WAIT_TIMEOUT) { 265 CleanupRawInputThreadData(); 266 } 267 268 // The thread will read from this to update its flags 269 SDL_SetAtomicU32(&thread_data.flags, desired_flags); 270 271 if (thread_data.thread) { 272 // Thread is already running. Fire (the event) and forget, it'll read the atomic flags on wakeup. 273 274 // If RegisterRawInputDevices fails, the thread reverts the atomic and the next call 275 // to this function will see the mismatch and retry. 276 SDL_assert(thread_data.signal_event); 277 SetEvent(thread_data.signal_event); 278 result = true; 279 } else if (desired_flags) { 280 HANDLE wait_handles[2]; 281 282 // Thread isn't running, spin it up 283 thread_data.ready_event = CreateEvent(NULL, FALSE, FALSE, NULL); 284 if (!thread_data.ready_event) { 285 WIN_SetError("CreateEvent"); 286 goto done; 287 } 288 289 thread_data.done = false; 290 thread_data.signal_event = CreateEvent(NULL, FALSE, FALSE, NULL); 291 if (!thread_data.signal_event) { 292 WIN_SetError("CreateEvent"); 293 goto done; 294 } 295 296 thread_data.thread = CreateThread(NULL, 0, WIN_RawInputThread, NULL, 0, NULL); 297 if (!thread_data.thread) { 298 WIN_SetError("CreateThread"); 299 goto done; 300 } 301 302 // Wait for the thread to complete initial setup or exit 303 wait_handles[0] = thread_data.ready_event; 304 wait_handles[1] = thread_data.thread; 305 if (WaitForMultipleObjects(2, wait_handles, FALSE, INFINITE) != WAIT_OBJECT_0) { 306 SDL_SetError("Couldn't set up raw input handling"); 307 goto done; 308 } 309 result = true; 310 } else { 311 // Thread isn't running and we tried to disable raw input, nothing to do 312 result = true; 313 } 314done: 315 if (!result) { 316 CleanupRawInputThreadData(); 317 } 318 return result; 319} 320 321bool WIN_SetRawMouseEnabled(SDL_VideoDevice *_this, bool enabled) 322{ 323 SDL_VideoData *data = _this->internal; 324 data->raw_mouse_enabled = enabled; 325 if (data->gameinput_context) { 326 if (!WIN_UpdateGameInputEnabled(_this)) { 327 data->raw_mouse_enabled = !enabled; 328 return false; 329 } 330 } else { 331 if (!WIN_UpdateRawInputEnabled(_this)) { 332 data->raw_mouse_enabled = !enabled; 333 return false; 334 } 335 } 336 return true; 337} 338 339bool WIN_SetRawKeyboardEnabled(SDL_VideoDevice *_this, bool enabled) 340{ 341 SDL_VideoData *data = _this->internal; 342 data->raw_keyboard_enabled = enabled; 343 if (data->gameinput_context) { 344 if (!WIN_UpdateGameInputEnabled(_this)) { 345 data->raw_keyboard_enabled = !enabled; 346 return false; 347 } 348 } else { 349 if (!WIN_UpdateRawInputEnabled(_this)) { 350 data->raw_keyboard_enabled = !enabled; 351 return false; 352 } 353 } 354 return true; 355} 356 357typedef enum WIN_RawKeyboardFlag { 358 NOHOTKEYS, 359 INPUTSINK, 360} WIN_RawKeyboardFlag; 361 362static bool WIN_SetRawKeyboardFlag(SDL_VideoDevice *_this, WIN_RawKeyboardFlag flag, bool enabled) 363{ 364 SDL_VideoData *data = _this->internal; 365 366 switch(flag) { 367 case NOHOTKEYS: 368 data->raw_keyboard_flag_nohotkeys = enabled; 369 break; 370 case INPUTSINK: 371 data->raw_keyboard_flag_inputsink = enabled; 372 break; 373 default: 374 return false; 375 } 376 377 if (data->gameinput_context) { 378 return true; 379 } 380 381 return WIN_UpdateRawInputEnabled(_this); 382} 383 384bool WIN_SetRawKeyboardFlag_NoHotkeys(SDL_VideoDevice *_this, bool enabled) 385{ 386 return WIN_SetRawKeyboardFlag(_this, NOHOTKEYS, enabled); 387} 388 389bool WIN_SetRawKeyboardFlag_Inputsink(SDL_VideoDevice *_this, bool enabled) 390{ 391 return WIN_SetRawKeyboardFlag(_this, INPUTSINK, enabled); 392} 393 394void WIN_QuitRawInput(SDL_VideoDevice *_this) 395{ 396 CleanupRawInputThreadData(); 397} 398 399#else 400 401bool WIN_SetRawMouseEnabled(SDL_VideoDevice *_this, bool enabled) 402{ 403 return SDL_Unsupported(); 404} 405 406bool WIN_SetRawKeyboardEnabled(SDL_VideoDevice *_this, bool enabled) 407{ 408 return SDL_Unsupported(); 409} 410 411bool WIN_SetRawKeyboardFlag_NoHotkeys(SDL_VideoDevice *_this, bool enabled) 412{ 413 return SDL_Unsupported(); 414} 415 416bool WIN_SetRawKeyboardFlag_Inputsink(SDL_VideoDevice *_this, bool enabled) 417{ 418 return SDL_Unsupported(); 419} 420 421void WIN_QuitRawInput(SDL_VideoDevice *_this) 422{ 423} 424 425#endif // !SDL_PLATFORM_XBOXONE && !SDL_PLATFORM_XBOXSERIES 426 427#endif // SDL_VIDEO_DRIVER_WINDOWS 428
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.