SDL_windowsvideo.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  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. #ifdef SDL_VIDEO_DRIVER_WINDOWS
  20. #ifdef SDL_VIDEO_VULKAN
  21. #include "../SDL_vulkan_internal.h"
  22. #endif
  23. #include "../SDL_sysvideo.h"
  24. #include "../SDL_pixels_c.h"
  25. #include "../../SDL_hints_c.h"
  26. #include "../../core/windows/SDL_hid.h"
  27. #include "SDL_windowsvideo.h"
  28. #include "SDL_windowsframebuffer.h"
  29. #include "SDL_windowsmessagebox.h"
  30. #include "SDL_windowsrawinput.h"
  31. #include "SDL_windowsvulkan.h"
  32. #ifdef SDL_GDK_TEXTINPUT
  33. #include "../gdk/SDL_gdktextinput.h"
  34. #endif
  35. /* #define HIGHDPI_DEBUG */
  36. /* Initialization/Query functions */
  37. static int WIN_VideoInit(SDL_VideoDevice *_this);
  38. static void WIN_VideoQuit(SDL_VideoDevice *_this);
  39. /* Hints */
  40. SDL_bool g_WindowsEnableMessageLoop = SDL_TRUE;
  41. SDL_bool g_WindowsEnableMenuMnemonics = SDL_FALSE;
  42. SDL_bool g_WindowFrameUsableWhileCursorHidden = SDL_TRUE;
  43. static void SDLCALL UpdateWindowsRawKeyboard(void *userdata, const char *name, const char *oldValue, const char *newValue)
  44. {
  45. SDL_VideoDevice *_this = (SDL_VideoDevice *)userdata;
  46. SDL_bool enabled = SDL_GetStringBoolean(newValue, SDL_FALSE);
  47. WIN_SetRawKeyboardEnabled(_this, enabled);
  48. }
  49. static void SDLCALL UpdateWindowsEnableMessageLoop(void *userdata, const char *name, const char *oldValue, const char *newValue)
  50. {
  51. g_WindowsEnableMessageLoop = SDL_GetStringBoolean(newValue, SDL_TRUE);
  52. }
  53. static void SDLCALL UpdateWindowsEnableMenuMnemonics(void *userdata, const char *name, const char *oldValue, const char *newValue)
  54. {
  55. g_WindowsEnableMenuMnemonics = SDL_GetStringBoolean(newValue, SDL_FALSE);
  56. }
  57. static void SDLCALL UpdateWindowFrameUsableWhileCursorHidden(void *userdata, const char *name, const char *oldValue, const char *newValue)
  58. {
  59. g_WindowFrameUsableWhileCursorHidden = SDL_GetStringBoolean(newValue, SDL_TRUE);
  60. }
  61. #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  62. static int WIN_SuspendScreenSaver(SDL_VideoDevice *_this)
  63. {
  64. if (_this->suspend_screensaver) {
  65. SetThreadExecutionState(ES_CONTINUOUS | ES_DISPLAY_REQUIRED);
  66. } else {
  67. SetThreadExecutionState(ES_CONTINUOUS);
  68. }
  69. return 0;
  70. }
  71. #endif
  72. #if defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES)
  73. extern void D3D12_XBOX_GetResolution(Uint32 *width, Uint32 *height);
  74. #endif
  75. /* Windows driver bootstrap functions */
  76. static void WIN_DeleteDevice(SDL_VideoDevice *device)
  77. {
  78. SDL_VideoData *data = device->internal;
  79. SDL_UnregisterApp();
  80. #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  81. if (data->userDLL) {
  82. SDL_UnloadObject(data->userDLL);
  83. }
  84. if (data->shcoreDLL) {
  85. SDL_UnloadObject(data->shcoreDLL);
  86. }
  87. #endif
  88. #ifdef HAVE_DXGI_H
  89. if (data->pDXGIFactory) {
  90. IDXGIFactory_Release(data->pDXGIFactory);
  91. }
  92. if (data->dxgiDLL) {
  93. SDL_UnloadObject(data->dxgiDLL);
  94. }
  95. #endif
  96. if (device->wakeup_lock) {
  97. SDL_DestroyMutex(device->wakeup_lock);
  98. }
  99. SDL_free(device->internal->rawinput);
  100. SDL_free(device->internal);
  101. SDL_free(device);
  102. }
  103. static SDL_VideoDevice *WIN_CreateDevice(void)
  104. {
  105. SDL_VideoDevice *device;
  106. SDL_VideoData *data;
  107. SDL_RegisterApp(NULL, 0, NULL);
  108. /* Initialize all variables that we clean on shutdown */
  109. device = (SDL_VideoDevice *)SDL_calloc(1, sizeof(SDL_VideoDevice));
  110. if (device) {
  111. data = (SDL_VideoData *)SDL_calloc(1, sizeof(SDL_VideoData));
  112. } else {
  113. data = NULL;
  114. }
  115. if (!data) {
  116. SDL_free(device);
  117. return NULL;
  118. }
  119. device->internal = data;
  120. device->wakeup_lock = SDL_CreateMutex();
  121. device->system_theme = WIN_GetSystemTheme();
  122. #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  123. data->userDLL = SDL_LoadObject("USER32.DLL");
  124. if (data->userDLL) {
  125. /* *INDENT-OFF* */ /* clang-format off */
  126. data->CloseTouchInputHandle = (BOOL (WINAPI *)(HTOUCHINPUT))SDL_LoadFunction(data->userDLL, "CloseTouchInputHandle");
  127. data->GetTouchInputInfo = (BOOL (WINAPI *)(HTOUCHINPUT, UINT, PTOUCHINPUT, int)) SDL_LoadFunction(data->userDLL, "GetTouchInputInfo");
  128. data->RegisterTouchWindow = (BOOL (WINAPI *)(HWND, ULONG))SDL_LoadFunction(data->userDLL, "RegisterTouchWindow");
  129. data->SetProcessDPIAware = (BOOL (WINAPI *)(void))SDL_LoadFunction(data->userDLL, "SetProcessDPIAware");
  130. data->SetProcessDpiAwarenessContext = (BOOL (WINAPI *)(DPI_AWARENESS_CONTEXT))SDL_LoadFunction(data->userDLL, "SetProcessDpiAwarenessContext");
  131. data->SetThreadDpiAwarenessContext = (DPI_AWARENESS_CONTEXT (WINAPI *)(DPI_AWARENESS_CONTEXT))SDL_LoadFunction(data->userDLL, "SetThreadDpiAwarenessContext");
  132. data->GetThreadDpiAwarenessContext = (DPI_AWARENESS_CONTEXT (WINAPI *)(void))SDL_LoadFunction(data->userDLL, "GetThreadDpiAwarenessContext");
  133. data->GetAwarenessFromDpiAwarenessContext = (DPI_AWARENESS (WINAPI *)(DPI_AWARENESS_CONTEXT))SDL_LoadFunction(data->userDLL, "GetAwarenessFromDpiAwarenessContext");
  134. data->EnableNonClientDpiScaling = (BOOL (WINAPI *)(HWND))SDL_LoadFunction(data->userDLL, "EnableNonClientDpiScaling");
  135. data->AdjustWindowRectExForDpi = (BOOL (WINAPI *)(LPRECT, DWORD, BOOL, DWORD, UINT))SDL_LoadFunction(data->userDLL, "AdjustWindowRectExForDpi");
  136. data->GetDpiForWindow = (UINT (WINAPI *)(HWND))SDL_LoadFunction(data->userDLL, "GetDpiForWindow");
  137. data->AreDpiAwarenessContextsEqual = (BOOL (WINAPI *)(DPI_AWARENESS_CONTEXT, DPI_AWARENESS_CONTEXT))SDL_LoadFunction(data->userDLL, "AreDpiAwarenessContextsEqual");
  138. data->IsValidDpiAwarenessContext = (BOOL (WINAPI *)(DPI_AWARENESS_CONTEXT))SDL_LoadFunction(data->userDLL, "IsValidDpiAwarenessContext");
  139. data->GetDisplayConfigBufferSizes = (LONG (WINAPI *)(UINT32,UINT32*,UINT32* ))SDL_LoadFunction(data->userDLL, "GetDisplayConfigBufferSizes");
  140. data->QueryDisplayConfig = (LONG (WINAPI *)(UINT32,UINT32*,DISPLAYCONFIG_PATH_INFO*,UINT32*,DISPLAYCONFIG_MODE_INFO*,DISPLAYCONFIG_TOPOLOGY_ID*))SDL_LoadFunction(data->userDLL, "QueryDisplayConfig");
  141. data->DisplayConfigGetDeviceInfo = (LONG (WINAPI *)(DISPLAYCONFIG_DEVICE_INFO_HEADER*))SDL_LoadFunction(data->userDLL, "DisplayConfigGetDeviceInfo");
  142. /* *INDENT-ON* */ /* clang-format on */
  143. } else {
  144. SDL_ClearError();
  145. }
  146. data->shcoreDLL = SDL_LoadObject("SHCORE.DLL");
  147. if (data->shcoreDLL) {
  148. /* *INDENT-OFF* */ /* clang-format off */
  149. data->GetDpiForMonitor = (HRESULT (WINAPI *)(HMONITOR, MONITOR_DPI_TYPE, UINT *, UINT *))SDL_LoadFunction(data->shcoreDLL, "GetDpiForMonitor");
  150. data->SetProcessDpiAwareness = (HRESULT (WINAPI *)(PROCESS_DPI_AWARENESS))SDL_LoadFunction(data->shcoreDLL, "SetProcessDpiAwareness");
  151. /* *INDENT-ON* */ /* clang-format on */
  152. } else {
  153. SDL_ClearError();
  154. }
  155. #endif /* #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES) */
  156. #ifdef HAVE_DXGI_H
  157. data->dxgiDLL = SDL_LoadObject("DXGI.DLL");
  158. if (data->dxgiDLL) {
  159. /* *INDENT-OFF* */ /* clang-format off */
  160. typedef HRESULT (WINAPI *CreateDXGI_t)(REFIID riid, void **ppFactory);
  161. /* *INDENT-ON* */ /* clang-format on */
  162. CreateDXGI_t CreateDXGI;
  163. CreateDXGI = (CreateDXGI_t)SDL_LoadFunction(data->dxgiDLL, "CreateDXGIFactory");
  164. if (CreateDXGI) {
  165. GUID dxgiGUID = { 0x7b7166ec, 0x21c7, 0x44ae, { 0xb2, 0x1a, 0xc9, 0xae, 0x32, 0x1a, 0xe3, 0x69 } };
  166. CreateDXGI(&dxgiGUID, (void **)&data->pDXGIFactory);
  167. }
  168. }
  169. #endif
  170. /* Set the function pointers */
  171. device->VideoInit = WIN_VideoInit;
  172. device->VideoQuit = WIN_VideoQuit;
  173. #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  174. device->RefreshDisplays = WIN_RefreshDisplays;
  175. device->GetDisplayBounds = WIN_GetDisplayBounds;
  176. device->GetDisplayUsableBounds = WIN_GetDisplayUsableBounds;
  177. device->GetDisplayModes = WIN_GetDisplayModes;
  178. device->SetDisplayMode = WIN_SetDisplayMode;
  179. #endif
  180. device->PumpEvents = WIN_PumpEvents;
  181. device->WaitEventTimeout = WIN_WaitEventTimeout;
  182. #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  183. device->SendWakeupEvent = WIN_SendWakeupEvent;
  184. device->SuspendScreenSaver = WIN_SuspendScreenSaver;
  185. #endif
  186. device->CreateSDLWindow = WIN_CreateWindow;
  187. device->SetWindowTitle = WIN_SetWindowTitle;
  188. device->SetWindowIcon = WIN_SetWindowIcon;
  189. device->SetWindowPosition = WIN_SetWindowPosition;
  190. device->SetWindowSize = WIN_SetWindowSize;
  191. device->GetWindowBordersSize = WIN_GetWindowBordersSize;
  192. device->GetWindowSizeInPixels = WIN_GetWindowSizeInPixels;
  193. device->SetWindowOpacity = WIN_SetWindowOpacity;
  194. device->ShowWindow = WIN_ShowWindow;
  195. device->HideWindow = WIN_HideWindow;
  196. device->RaiseWindow = WIN_RaiseWindow;
  197. device->MaximizeWindow = WIN_MaximizeWindow;
  198. device->MinimizeWindow = WIN_MinimizeWindow;
  199. device->RestoreWindow = WIN_RestoreWindow;
  200. device->SetWindowBordered = WIN_SetWindowBordered;
  201. device->SetWindowResizable = WIN_SetWindowResizable;
  202. device->SetWindowAlwaysOnTop = WIN_SetWindowAlwaysOnTop;
  203. device->SetWindowFullscreen = WIN_SetWindowFullscreen;
  204. device->SetWindowModalFor = WIN_SetWindowModalFor;
  205. #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  206. device->GetWindowICCProfile = WIN_GetWindowICCProfile;
  207. device->SetWindowMouseRect = WIN_SetWindowMouseRect;
  208. device->SetWindowMouseGrab = WIN_SetWindowMouseGrab;
  209. device->SetWindowKeyboardGrab = WIN_SetWindowKeyboardGrab;
  210. #endif
  211. device->DestroyWindow = WIN_DestroyWindow;
  212. #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  213. device->CreateWindowFramebuffer = WIN_CreateWindowFramebuffer;
  214. device->UpdateWindowFramebuffer = WIN_UpdateWindowFramebuffer;
  215. device->DestroyWindowFramebuffer = WIN_DestroyWindowFramebuffer;
  216. device->OnWindowEnter = WIN_OnWindowEnter;
  217. device->SetWindowHitTest = WIN_SetWindowHitTest;
  218. device->AcceptDragAndDrop = WIN_AcceptDragAndDrop;
  219. device->FlashWindow = WIN_FlashWindow;
  220. device->ShowWindowSystemMenu = WIN_ShowWindowSystemMenu;
  221. device->SetWindowFocusable = WIN_SetWindowFocusable;
  222. device->UpdateWindowShape = WIN_UpdateWindowShape;
  223. #endif
  224. #ifdef SDL_VIDEO_OPENGL_WGL
  225. device->GL_LoadLibrary = WIN_GL_LoadLibrary;
  226. device->GL_GetProcAddress = WIN_GL_GetProcAddress;
  227. device->GL_UnloadLibrary = WIN_GL_UnloadLibrary;
  228. device->GL_CreateContext = WIN_GL_CreateContext;
  229. device->GL_MakeCurrent = WIN_GL_MakeCurrent;
  230. device->GL_SetSwapInterval = WIN_GL_SetSwapInterval;
  231. device->GL_GetSwapInterval = WIN_GL_GetSwapInterval;
  232. device->GL_SwapWindow = WIN_GL_SwapWindow;
  233. device->GL_DeleteContext = WIN_GL_DeleteContext;
  234. device->GL_GetEGLSurface = NULL;
  235. #endif
  236. #ifdef SDL_VIDEO_OPENGL_EGL
  237. #ifdef SDL_VIDEO_OPENGL_WGL
  238. if (SDL_GetHintBoolean(SDL_HINT_VIDEO_FORCE_EGL, SDL_FALSE)) {
  239. #endif
  240. /* Use EGL based functions */
  241. device->GL_LoadLibrary = WIN_GLES_LoadLibrary;
  242. device->GL_GetProcAddress = WIN_GLES_GetProcAddress;
  243. device->GL_UnloadLibrary = WIN_GLES_UnloadLibrary;
  244. device->GL_CreateContext = WIN_GLES_CreateContext;
  245. device->GL_MakeCurrent = WIN_GLES_MakeCurrent;
  246. device->GL_SetSwapInterval = WIN_GLES_SetSwapInterval;
  247. device->GL_GetSwapInterval = WIN_GLES_GetSwapInterval;
  248. device->GL_SwapWindow = WIN_GLES_SwapWindow;
  249. device->GL_DeleteContext = WIN_GLES_DeleteContext;
  250. device->GL_GetEGLSurface = WIN_GLES_GetEGLSurface;
  251. #ifdef SDL_VIDEO_OPENGL_WGL
  252. }
  253. #endif
  254. #endif
  255. #ifdef SDL_VIDEO_VULKAN
  256. device->Vulkan_LoadLibrary = WIN_Vulkan_LoadLibrary;
  257. device->Vulkan_UnloadLibrary = WIN_Vulkan_UnloadLibrary;
  258. device->Vulkan_GetInstanceExtensions = WIN_Vulkan_GetInstanceExtensions;
  259. device->Vulkan_CreateSurface = WIN_Vulkan_CreateSurface;
  260. device->Vulkan_DestroySurface = WIN_Vulkan_DestroySurface;
  261. device->Vulkan_GetPresentationSupport = WIN_Vulkan_GetPresentationSupport;
  262. #endif
  263. #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  264. device->StartTextInput = WIN_StartTextInput;
  265. device->StopTextInput = WIN_StopTextInput;
  266. device->UpdateTextInputArea = WIN_UpdateTextInputArea;
  267. device->ClearComposition = WIN_ClearComposition;
  268. device->SetClipboardData = WIN_SetClipboardData;
  269. device->GetClipboardData = WIN_GetClipboardData;
  270. device->HasClipboardData = WIN_HasClipboardData;
  271. #endif
  272. #ifdef SDL_GDK_TEXTINPUT
  273. GDK_EnsureHints();
  274. device->StartTextInput = GDK_StartTextInput;
  275. device->StopTextInput = GDK_StopTextInput;
  276. device->UpdateTextInputArea = GDK_UpdateTextInputArea;
  277. device->ClearComposition = GDK_ClearComposition;
  278. device->HasScreenKeyboardSupport = GDK_HasScreenKeyboardSupport;
  279. device->ShowScreenKeyboard = GDK_ShowScreenKeyboard;
  280. device->HideScreenKeyboard = GDK_HideScreenKeyboard;
  281. device->IsScreenKeyboardShown = GDK_IsScreenKeyboardShown;
  282. #endif
  283. device->free = WIN_DeleteDevice;
  284. device->device_caps = VIDEO_DEVICE_CAPS_HAS_POPUP_WINDOW_SUPPORT |
  285. VIDEO_DEVICE_CAPS_SENDS_FULLSCREEN_DIMENSIONS;
  286. return device;
  287. }
  288. VideoBootStrap WINDOWS_bootstrap = {
  289. "windows", "SDL Windows video driver", WIN_CreateDevice,
  290. #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  291. WIN_ShowMessageBox
  292. #else
  293. NULL
  294. #endif
  295. };
  296. static BOOL WIN_DeclareDPIAwareUnaware(SDL_VideoDevice *_this)
  297. {
  298. #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  299. SDL_VideoData *data = _this->internal;
  300. if (data->SetProcessDpiAwarenessContext) {
  301. return data->SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_UNAWARE);
  302. } else if (data->SetProcessDpiAwareness) {
  303. /* Windows 8.1 */
  304. return SUCCEEDED(data->SetProcessDpiAwareness(PROCESS_DPI_UNAWARE));
  305. }
  306. #endif
  307. return FALSE;
  308. }
  309. static BOOL WIN_DeclareDPIAwareSystem(SDL_VideoDevice *_this)
  310. {
  311. #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  312. SDL_VideoData *data = _this->internal;
  313. if (data->SetProcessDpiAwarenessContext) {
  314. /* Windows 10, version 1607 */
  315. return data->SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_SYSTEM_AWARE);
  316. } else if (data->SetProcessDpiAwareness) {
  317. /* Windows 8.1 */
  318. return SUCCEEDED(data->SetProcessDpiAwareness(PROCESS_SYSTEM_DPI_AWARE));
  319. } else if (data->SetProcessDPIAware) {
  320. /* Windows Vista */
  321. return data->SetProcessDPIAware();
  322. }
  323. #endif
  324. return FALSE;
  325. }
  326. static BOOL WIN_DeclareDPIAwarePerMonitor(SDL_VideoDevice *_this)
  327. {
  328. #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  329. SDL_VideoData *data = _this->internal;
  330. if (data->SetProcessDpiAwarenessContext) {
  331. /* Windows 10, version 1607 */
  332. return data->SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE);
  333. } else if (data->SetProcessDpiAwareness) {
  334. /* Windows 8.1 */
  335. return SUCCEEDED(data->SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE));
  336. } else {
  337. /* Older OS: fall back to system DPI aware */
  338. return WIN_DeclareDPIAwareSystem(_this);
  339. }
  340. #else
  341. return FALSE;
  342. #endif
  343. }
  344. static BOOL WIN_DeclareDPIAwarePerMonitorV2(SDL_VideoDevice *_this)
  345. {
  346. #if defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES)
  347. return FALSE;
  348. #else
  349. SDL_VideoData *data = _this->internal;
  350. /* Declare DPI aware (may have been done in external code or a manifest, as well) */
  351. if (data->SetProcessDpiAwarenessContext) {
  352. /* Windows 10, version 1607 */
  353. /* NOTE: SetThreadDpiAwarenessContext doesn't work here with OpenGL - the OpenGL contents
  354. end up still getting OS scaled. (tested on Windows 10 21H1 19043.1348, NVIDIA 496.49)
  355. NOTE: Enabling DPI awareness through Windows Explorer
  356. (right click .exe -> Properties -> Compatibility -> High DPI Settings ->
  357. check "Override high DPI Scaling behaviour", select Application) gives
  358. a DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE context (at least on Windows 10 21H1), and
  359. setting DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 will fail.
  360. NOTE: Entering exclusive fullscreen in a DPI_AWARENESS_CONTEXT_UNAWARE process
  361. appears to cause Windows to change the .exe manifest to DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE
  362. on future launches. This means attempting to use DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2
  363. will fail in the future until you manually clear the "Override high DPI Scaling behaviour"
  364. setting in Windows Explorer (tested on Windows 10 21H2).
  365. */
  366. if (data->SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2)) {
  367. return TRUE;
  368. } else {
  369. return WIN_DeclareDPIAwarePerMonitor(_this);
  370. }
  371. } else {
  372. /* Older OS: fall back to per-monitor (or system) */
  373. return WIN_DeclareDPIAwarePerMonitor(_this);
  374. }
  375. #endif
  376. }
  377. #ifdef HIGHDPI_DEBUG
  378. static const char *WIN_GetDPIAwareness(SDL_VideoDevice *_this)
  379. {
  380. SDL_VideoData *data = _this->internal;
  381. if (data->GetThreadDpiAwarenessContext && data->AreDpiAwarenessContextsEqual) {
  382. DPI_AWARENESS_CONTEXT context = data->GetThreadDpiAwarenessContext();
  383. if (data->AreDpiAwarenessContextsEqual(context, DPI_AWARENESS_CONTEXT_UNAWARE)) {
  384. return "unaware";
  385. } else if (data->AreDpiAwarenessContextsEqual(context, DPI_AWARENESS_CONTEXT_SYSTEM_AWARE)) {
  386. return "system";
  387. } else if (data->AreDpiAwarenessContextsEqual(context, DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE)) {
  388. return "permonitor";
  389. } else if (data->AreDpiAwarenessContextsEqual(context, DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2)) {
  390. return "permonitorv2";
  391. } else if (data->AreDpiAwarenessContextsEqual(context, DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED)) {
  392. return "unaware_gdiscaled";
  393. }
  394. }
  395. return "";
  396. }
  397. #endif
  398. static void WIN_InitDPIAwareness(SDL_VideoDevice *_this)
  399. {
  400. const char *hint = SDL_GetHint("SDL_WINDOWS_DPI_AWARENESS");
  401. if (!hint || SDL_strcmp(hint, "permonitorv2") == 0) {
  402. WIN_DeclareDPIAwarePerMonitorV2(_this);
  403. } else if (SDL_strcmp(hint, "permonitor") == 0) {
  404. WIN_DeclareDPIAwarePerMonitor(_this);
  405. } else if (SDL_strcmp(hint, "system") == 0) {
  406. WIN_DeclareDPIAwareSystem(_this);
  407. } else if (SDL_strcmp(hint, "unaware") == 0) {
  408. WIN_DeclareDPIAwareUnaware(_this);
  409. }
  410. }
  411. int WIN_VideoInit(SDL_VideoDevice *_this)
  412. {
  413. SDL_VideoData *data = _this->internal;
  414. HRESULT hr;
  415. hr = WIN_CoInitialize();
  416. if (SUCCEEDED(hr)) {
  417. data->coinitialized = SDL_TRUE;
  418. hr = OleInitialize(NULL);
  419. if (SUCCEEDED(hr)) {
  420. data->oleinitialized = SDL_TRUE;
  421. } else {
  422. SDL_LogInfo(SDL_LOG_CATEGORY_VIDEO, "OleInitialize() failed: 0x%.8x, using fallback drag-n-drop functionality\n", (unsigned int)hr);
  423. }
  424. } else {
  425. SDL_LogInfo(SDL_LOG_CATEGORY_VIDEO, "CoInitialize() failed: 0x%.8x, using fallback drag-n-drop functionality\n", (unsigned int)hr);
  426. }
  427. WIN_InitDPIAwareness(_this);
  428. #ifdef HIGHDPI_DEBUG
  429. SDL_Log("DPI awareness: %s", WIN_GetDPIAwareness(_this));
  430. #endif
  431. #if defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES)
  432. /* For Xbox, we just need to create the single display */
  433. {
  434. SDL_DisplayMode mode;
  435. SDL_zero(mode);
  436. D3D12_XBOX_GetResolution(&mode.w, &mode.h);
  437. mode.refresh_rate = 60.0f;
  438. mode.format = SDL_PIXELFORMAT_ARGB8888;
  439. SDL_AddBasicVideoDisplay(&mode);
  440. }
  441. #else /*!defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)*/
  442. if (WIN_InitModes(_this) < 0) {
  443. return -1;
  444. }
  445. WIN_InitKeyboard(_this);
  446. WIN_InitMouse(_this);
  447. WIN_InitDeviceNotification();
  448. WIN_CheckKeyboardAndMouseHotplug(_this, SDL_TRUE);
  449. #endif
  450. SDL_AddHintCallback(SDL_HINT_WINDOWS_RAW_KEYBOARD, UpdateWindowsRawKeyboard, _this);
  451. SDL_AddHintCallback(SDL_HINT_WINDOWS_ENABLE_MESSAGELOOP, UpdateWindowsEnableMessageLoop, NULL);
  452. SDL_AddHintCallback(SDL_HINT_WINDOWS_ENABLE_MENU_MNEMONICS, UpdateWindowsEnableMenuMnemonics, NULL);
  453. SDL_AddHintCallback(SDL_HINT_WINDOW_FRAME_USABLE_WHILE_CURSOR_HIDDEN, UpdateWindowFrameUsableWhileCursorHidden, NULL);
  454. #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  455. data->_SDL_WAKEUP = RegisterWindowMessageA("_SDL_WAKEUP");
  456. #endif
  457. return 0;
  458. }
  459. void WIN_VideoQuit(SDL_VideoDevice *_this)
  460. {
  461. SDL_VideoData *data = _this->internal;
  462. #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  463. WIN_QuitModes(_this);
  464. WIN_QuitDeviceNotification();
  465. WIN_QuitKeyboard(_this);
  466. WIN_QuitMouse(_this);
  467. #endif
  468. SDL_DelHintCallback(SDL_HINT_WINDOWS_RAW_KEYBOARD, UpdateWindowsRawKeyboard, _this);
  469. SDL_DelHintCallback(SDL_HINT_WINDOWS_ENABLE_MESSAGELOOP, UpdateWindowsEnableMessageLoop, NULL);
  470. SDL_DelHintCallback(SDL_HINT_WINDOWS_ENABLE_MENU_MNEMONICS, UpdateWindowsEnableMenuMnemonics, NULL);
  471. SDL_DelHintCallback(SDL_HINT_WINDOW_FRAME_USABLE_WHILE_CURSOR_HIDDEN, UpdateWindowFrameUsableWhileCursorHidden, NULL);
  472. WIN_SetRawMouseEnabled(_this, SDL_FALSE);
  473. WIN_SetRawKeyboardEnabled(_this, SDL_FALSE);
  474. if (data->oleinitialized) {
  475. OleUninitialize();
  476. data->oleinitialized = SDL_FALSE;
  477. }
  478. if (data->coinitialized) {
  479. WIN_CoUninitialize();
  480. data->coinitialized = SDL_FALSE;
  481. }
  482. }
  483. #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  484. #define D3D_DEBUG_INFO
  485. #include <d3d9.h>
  486. #ifdef D3D_DEBUG_INFO
  487. #ifndef D3D_SDK_VERSION
  488. #define D3D_SDK_VERSION (32 | 0x80000000)
  489. #endif
  490. #ifndef D3D9b_SDK_VERSION
  491. #define D3D9b_SDK_VERSION (31 | 0x80000000)
  492. #endif
  493. #else /**/
  494. #ifndef D3D_SDK_VERSION
  495. #define D3D_SDK_VERSION 32
  496. #endif
  497. #ifndef D3D9b_SDK_VERSION
  498. #define D3D9b_SDK_VERSION 31
  499. #endif
  500. #endif
  501. SDL_bool D3D_LoadDLL(void **pD3DDLL, IDirect3D9 **pDirect3D9Interface)
  502. {
  503. *pD3DDLL = SDL_LoadObject("D3D9.DLL");
  504. if (*pD3DDLL) {
  505. /* *INDENT-OFF* */ /* clang-format off */
  506. typedef IDirect3D9 *(WINAPI *Direct3DCreate9_t)(UINT SDKVersion);
  507. typedef HRESULT (WINAPI* Direct3DCreate9Ex_t)(UINT SDKVersion, IDirect3D9Ex** ppD3D);
  508. /* *INDENT-ON* */ /* clang-format on */
  509. Direct3DCreate9_t Direct3DCreate9Func;
  510. if (SDL_GetHintBoolean(SDL_HINT_WINDOWS_USE_D3D9EX, SDL_FALSE)) {
  511. Direct3DCreate9Ex_t Direct3DCreate9ExFunc;
  512. Direct3DCreate9ExFunc = (Direct3DCreate9Ex_t)SDL_LoadFunction(*pD3DDLL, "Direct3DCreate9Ex");
  513. if (Direct3DCreate9ExFunc) {
  514. IDirect3D9Ex *pDirect3D9ExInterface;
  515. HRESULT hr = Direct3DCreate9ExFunc(D3D_SDK_VERSION, &pDirect3D9ExInterface);
  516. if (SUCCEEDED(hr)) {
  517. const GUID IDirect3D9_GUID = { 0x81bdcbca, 0x64d4, 0x426d, { 0xae, 0x8d, 0xad, 0x1, 0x47, 0xf4, 0x27, 0x5c } };
  518. hr = IDirect3D9Ex_QueryInterface(pDirect3D9ExInterface, &IDirect3D9_GUID, (void **)pDirect3D9Interface);
  519. IDirect3D9Ex_Release(pDirect3D9ExInterface);
  520. if (SUCCEEDED(hr)) {
  521. return SDL_TRUE;
  522. }
  523. }
  524. }
  525. }
  526. Direct3DCreate9Func = (Direct3DCreate9_t)SDL_LoadFunction(*pD3DDLL, "Direct3DCreate9");
  527. if (Direct3DCreate9Func) {
  528. *pDirect3D9Interface = Direct3DCreate9Func(D3D_SDK_VERSION);
  529. if (*pDirect3D9Interface) {
  530. return SDL_TRUE;
  531. }
  532. }
  533. SDL_UnloadObject(*pD3DDLL);
  534. *pD3DDLL = NULL;
  535. }
  536. *pDirect3D9Interface = NULL;
  537. return SDL_FALSE;
  538. }
  539. int SDL_GetDirect3D9AdapterIndex(SDL_DisplayID displayID)
  540. {
  541. void *pD3DDLL;
  542. IDirect3D9 *pD3D;
  543. if (!D3D_LoadDLL(&pD3DDLL, &pD3D)) {
  544. SDL_SetError("Unable to create Direct3D interface");
  545. return D3DADAPTER_DEFAULT;
  546. } else {
  547. SDL_DisplayData *pData = SDL_GetDisplayDriverData(displayID);
  548. int adapterIndex = D3DADAPTER_DEFAULT;
  549. if (!pData) {
  550. SDL_SetError("Invalid display index");
  551. adapterIndex = -1; /* make sure we return something invalid */
  552. } else {
  553. char *displayName = WIN_StringToUTF8W(pData->DeviceName);
  554. unsigned int count = IDirect3D9_GetAdapterCount(pD3D);
  555. unsigned int i;
  556. for (i = 0; i < count; i++) {
  557. D3DADAPTER_IDENTIFIER9 id;
  558. IDirect3D9_GetAdapterIdentifier(pD3D, i, 0, &id);
  559. if (SDL_strcmp(id.DeviceName, displayName) == 0) {
  560. adapterIndex = i;
  561. break;
  562. }
  563. }
  564. SDL_free(displayName);
  565. }
  566. /* free up the D3D stuff we inited */
  567. IDirect3D9_Release(pD3D);
  568. SDL_UnloadObject(pD3DDLL);
  569. return adapterIndex;
  570. }
  571. }
  572. #endif /* !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES) */
  573. int SDL_GetDXGIOutputInfo(SDL_DisplayID displayID, int *adapterIndex, int *outputIndex)
  574. {
  575. #ifndef HAVE_DXGI_H
  576. if (adapterIndex) {
  577. *adapterIndex = -1;
  578. }
  579. if (outputIndex) {
  580. *outputIndex = -1;
  581. }
  582. return SDL_SetError("SDL was compiled without DXGI support due to missing dxgi.h header");
  583. #else
  584. const SDL_VideoDevice *videodevice = SDL_GetVideoDevice();
  585. const SDL_VideoData *videodata = videodevice ? videodevice->internal : NULL;
  586. SDL_DisplayData *pData = SDL_GetDisplayDriverData(displayID);
  587. int nAdapter, nOutput;
  588. IDXGIAdapter *pDXGIAdapter;
  589. IDXGIOutput *pDXGIOutput;
  590. if (!adapterIndex) {
  591. return SDL_InvalidParamError("adapterIndex");
  592. }
  593. if (!outputIndex) {
  594. return SDL_InvalidParamError("outputIndex");
  595. }
  596. *adapterIndex = -1;
  597. *outputIndex = -1;
  598. if (!pData) {
  599. return SDL_SetError("Invalid display index");
  600. }
  601. if (!videodata || !videodata->pDXGIFactory) {
  602. return SDL_SetError("Unable to create DXGI interface");
  603. }
  604. nAdapter = 0;
  605. while (*adapterIndex == -1 && SUCCEEDED(IDXGIFactory_EnumAdapters(videodata->pDXGIFactory, nAdapter, &pDXGIAdapter))) {
  606. nOutput = 0;
  607. while (*adapterIndex == -1 && SUCCEEDED(IDXGIAdapter_EnumOutputs(pDXGIAdapter, nOutput, &pDXGIOutput))) {
  608. DXGI_OUTPUT_DESC outputDesc;
  609. if (SUCCEEDED(IDXGIOutput_GetDesc(pDXGIOutput, &outputDesc))) {
  610. if (SDL_wcscmp(outputDesc.DeviceName, pData->DeviceName) == 0) {
  611. *adapterIndex = nAdapter;
  612. *outputIndex = nOutput;
  613. }
  614. }
  615. IDXGIOutput_Release(pDXGIOutput);
  616. nOutput++;
  617. }
  618. IDXGIAdapter_Release(pDXGIAdapter);
  619. nAdapter++;
  620. }
  621. if (*adapterIndex == -1) {
  622. return SDL_SetError("Couldn't find matching adapter");
  623. }
  624. return 0;
  625. #endif
  626. }
  627. SDL_SystemTheme WIN_GetSystemTheme(void)
  628. {
  629. SDL_SystemTheme theme = SDL_SYSTEM_THEME_LIGHT;
  630. HKEY hKey;
  631. DWORD dwType = REG_DWORD;
  632. DWORD value = ~0U;
  633. DWORD length = sizeof(value);
  634. /* Technically this isn't the system theme, but it's the preference for applications */
  635. if (RegOpenKeyExW(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
  636. if (RegQueryValueExW(hKey, L"AppsUseLightTheme", 0, &dwType, (LPBYTE)&value, &length) == ERROR_SUCCESS) {
  637. if (value == 0) {
  638. theme = SDL_SYSTEM_THEME_DARK;
  639. }
  640. }
  641. RegCloseKey(hKey);
  642. }
  643. return theme;
  644. }
  645. SDL_bool WIN_IsPerMonitorV2DPIAware(SDL_VideoDevice *_this)
  646. {
  647. #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  648. SDL_VideoData *data = _this->internal;
  649. if (data->AreDpiAwarenessContextsEqual && data->GetThreadDpiAwarenessContext) {
  650. /* Windows 10, version 1607 */
  651. return data->AreDpiAwarenessContextsEqual(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2, data->GetThreadDpiAwarenessContext());
  652. }
  653. #endif
  654. return SDL_FALSE;
  655. }
  656. #endif /* SDL_VIDEO_DRIVER_WINDOWS */