SDL_windowsvideo.c 25 KB

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