SDL_windowswindow.c 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 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. #include "../../core/windows/SDL_windows.h"
  21. #include "../../SDL_hints_c.h"
  22. #include "../../events/SDL_dropevents_c.h"
  23. #include "../../events/SDL_keyboard_c.h"
  24. #include "../../events/SDL_mouse_c.h"
  25. #include "../../events/SDL_windowevents_c.h"
  26. #include "../SDL_pixels_c.h"
  27. #include "../SDL_sysvideo.h"
  28. #include "SDL_windowsvideo.h"
  29. #include "SDL_windowswindow.h"
  30. // Dropfile support
  31. #include <shellapi.h>
  32. #ifdef HAVE_SHOBJIDL_CORE_H
  33. #include <shobjidl_core.h>
  34. #endif
  35. // Dark mode support
  36. typedef enum {
  37. UXTHEME_APPMODE_DEFAULT,
  38. UXTHEME_APPMODE_ALLOW_DARK,
  39. UXTHEME_APPMODE_FORCE_DARK,
  40. UXTHEME_APPMODE_FORCE_LIGHT,
  41. UXTHEME_APPMODE_MAX
  42. } UxthemePreferredAppMode;
  43. typedef enum {
  44. WCA_UNDEFINED = 0,
  45. WCA_USEDARKMODECOLORS = 26,
  46. WCA_LAST = 27
  47. } WINDOWCOMPOSITIONATTRIB;
  48. typedef struct {
  49. WINDOWCOMPOSITIONATTRIB Attrib;
  50. PVOID pvData;
  51. SIZE_T cbData;
  52. } WINDOWCOMPOSITIONATTRIBDATA;
  53. typedef struct {
  54. ULONG dwOSVersionInfoSize;
  55. ULONG dwMajorVersion;
  56. ULONG dwMinorVersion;
  57. ULONG dwBuildNumber;
  58. ULONG dwPlatformId;
  59. WCHAR szCSDVersion[128];
  60. } NT_OSVERSIONINFOW;
  61. typedef bool (WINAPI *ShouldAppsUseDarkMode_t)(void);
  62. typedef void (WINAPI *AllowDarkModeForWindow_t)(HWND, bool);
  63. typedef void (WINAPI *AllowDarkModeForApp_t)(bool);
  64. typedef void (WINAPI *RefreshImmersiveColorPolicyState_t)(void);
  65. typedef UxthemePreferredAppMode (WINAPI *SetPreferredAppMode_t)(UxthemePreferredAppMode);
  66. typedef BOOL (WINAPI *SetWindowCompositionAttribute_t)(HWND, const WINDOWCOMPOSITIONATTRIBDATA *);
  67. typedef void (NTAPI *RtlGetVersion_t)(NT_OSVERSIONINFOW *);
  68. // Windows CE compatibility
  69. #ifndef SWP_NOCOPYBITS
  70. #define SWP_NOCOPYBITS 0
  71. #endif
  72. /* An undocumented message to create a popup system menu
  73. * - wParam is always 0
  74. * - lParam = MAKELONG(x, y) where x and y are the screen coordinates where the menu should be displayed
  75. */
  76. #ifndef WM_POPUPSYSTEMMENU
  77. #define WM_POPUPSYSTEMMENU 0x313
  78. #endif
  79. // #define HIGHDPI_DEBUG
  80. // Fake window to help with DirectInput events.
  81. HWND SDL_HelperWindow = NULL;
  82. static const TCHAR *SDL_HelperWindowClassName = TEXT("SDLHelperWindowInputCatcher");
  83. static const TCHAR *SDL_HelperWindowName = TEXT("SDLHelperWindowInputMsgWindow");
  84. static ATOM SDL_HelperWindowClass = 0;
  85. /* For borderless Windows, still want the following flag:
  86. - WS_MINIMIZEBOX: window will respond to Windows minimize commands sent to all windows, such as windows key + m, shaking title bar, etc.
  87. Additionally, non-fullscreen windows can add:
  88. - WS_CAPTION: this seems to enable the Windows minimize animation
  89. - WS_SYSMENU: enables system context menu on task bar
  90. This will also cause the task bar to overlap the window and other windowed behaviors, so only use this for windows that shouldn't appear to be fullscreen
  91. - WS_THICKFRAME: allows hit-testing to resize window (doesn't actually add a frame to a borderless window).
  92. - WS_MAXIMIZEBOX: window will respond to Windows maximize commands sent to all windows, and the window will fill the usable desktop area rather than the whole screen
  93. */
  94. #define STYLE_BASIC (WS_CLIPSIBLINGS | WS_CLIPCHILDREN)
  95. #define STYLE_FULLSCREEN (WS_POPUP | WS_MINIMIZEBOX)
  96. #define STYLE_BORDERLESS (WS_POPUP | WS_MINIMIZEBOX)
  97. #define STYLE_BORDERLESS_WINDOWED (WS_POPUP | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX)
  98. #define STYLE_NORMAL (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX)
  99. #define STYLE_RESIZABLE (WS_THICKFRAME | WS_MAXIMIZEBOX)
  100. #define STYLE_MASK (STYLE_FULLSCREEN | STYLE_BORDERLESS | STYLE_NORMAL | STYLE_RESIZABLE)
  101. static DWORD GetWindowStyle(SDL_Window *window)
  102. {
  103. DWORD style = 0;
  104. if (SDL_WINDOW_IS_POPUP(window)) {
  105. style |= WS_POPUP;
  106. } else if (window->flags & SDL_WINDOW_FULLSCREEN) {
  107. style |= STYLE_FULLSCREEN;
  108. } else {
  109. if (window->flags & SDL_WINDOW_BORDERLESS) {
  110. /* This behavior more closely matches other platform where the window is borderless
  111. but still interacts with the window manager (e.g. task bar shows above it, it can
  112. be resized to fit within usable desktop area, etc.)
  113. */
  114. if (SDL_GetHintBoolean("SDL_BORDERLESS_WINDOWED_STYLE", true)) {
  115. style |= STYLE_BORDERLESS_WINDOWED;
  116. } else {
  117. style |= STYLE_BORDERLESS;
  118. }
  119. } else {
  120. style |= STYLE_NORMAL;
  121. }
  122. /* The WS_MAXIMIZEBOX style flag needs to be retained for as long as the window is maximized,
  123. * or restoration from minimized can fail, and leaving maximized can result in an odd size.
  124. */
  125. if (window->flags & SDL_WINDOW_RESIZABLE) {
  126. /* You can have a borderless resizable window, but Windows doesn't always draw it correctly,
  127. see https://bugzilla.libsdl.org/show_bug.cgi?id=4466
  128. */
  129. if (!(window->flags & SDL_WINDOW_BORDERLESS) ||
  130. SDL_GetHintBoolean("SDL_BORDERLESS_RESIZABLE_STYLE", true)) {
  131. style |= STYLE_RESIZABLE;
  132. }
  133. }
  134. if (window->internal && window->internal->force_ws_maximizebox) {
  135. /* Even if the resizable flag is cleared, WS_MAXIMIZEBOX is still needed as long
  136. * as the window is maximized, or de-maximizing or minimizing and restoring the
  137. * maximized window can result in the window disappearing or being the wrong size.
  138. */
  139. style |= WS_MAXIMIZEBOX;
  140. }
  141. // Need to set initialize minimize style, or when we call ShowWindow with WS_MINIMIZE it will activate a random window
  142. if (window->flags & SDL_WINDOW_MINIMIZED) {
  143. style |= WS_MINIMIZE;
  144. }
  145. }
  146. return style;
  147. }
  148. static DWORD GetWindowStyleEx(SDL_Window *window)
  149. {
  150. DWORD style = 0;
  151. if (SDL_WINDOW_IS_POPUP(window) || (window->flags & SDL_WINDOW_UTILITY)) {
  152. style |= WS_EX_TOOLWINDOW;
  153. }
  154. if (SDL_WINDOW_IS_POPUP(window) || (window->flags & SDL_WINDOW_NOT_FOCUSABLE)) {
  155. style |= WS_EX_NOACTIVATE;
  156. }
  157. return style;
  158. }
  159. #ifdef HAVE_SHOBJIDL_CORE_H
  160. static ITaskbarList3 *GetTaskbarList(SDL_Window* window)
  161. {
  162. const SDL_WindowData *data = window->internal;
  163. SDL_assert(data->taskbar_button_created);
  164. if (!data->videodata->taskbar_list) {
  165. HRESULT ret = CoCreateInstance(&CLSID_TaskbarList, NULL, CLSCTX_ALL, &IID_ITaskbarList3, (LPVOID *)&data->videodata->taskbar_list);
  166. if (FAILED(ret)) {
  167. WIN_SetErrorFromHRESULT("Unable to create taskbar list", ret);
  168. return NULL;
  169. }
  170. ITaskbarList3 *taskbarlist = data->videodata->taskbar_list;
  171. ret = taskbarlist->lpVtbl->HrInit(taskbarlist);
  172. if (FAILED(ret)) {
  173. taskbarlist->lpVtbl->Release(taskbarlist);
  174. data->videodata->taskbar_list = NULL;
  175. WIN_SetErrorFromHRESULT("Unable to initialize taskbar list", ret);
  176. return NULL;
  177. }
  178. }
  179. return data->videodata->taskbar_list;
  180. }
  181. #endif
  182. /**
  183. * Returns arguments to pass to SetWindowPos - the window rect, including frame, in Windows coordinates.
  184. * Can be called before we have a HWND.
  185. */
  186. static bool WIN_AdjustWindowRectWithStyle(SDL_Window *window, DWORD style, DWORD styleEx, BOOL menu, int *x, int *y, int *width, int *height, SDL_WindowRect rect_type)
  187. {
  188. SDL_VideoData *videodata = SDL_GetVideoDevice() ? SDL_GetVideoDevice()->internal : NULL;
  189. RECT rect;
  190. // Client rect, in points
  191. switch (rect_type) {
  192. case SDL_WINDOWRECT_CURRENT:
  193. SDL_RelativeToGlobalForWindow(window, window->x, window->y, x, y);
  194. *width = window->w;
  195. *height = window->h;
  196. break;
  197. case SDL_WINDOWRECT_WINDOWED:
  198. SDL_RelativeToGlobalForWindow(window, window->windowed.x, window->windowed.y, x, y);
  199. *width = window->windowed.w;
  200. *height = window->windowed.h;
  201. break;
  202. case SDL_WINDOWRECT_FLOATING:
  203. SDL_RelativeToGlobalForWindow(window, window->floating.x, window->floating.y, x, y);
  204. *width = window->floating.w;
  205. *height = window->floating.h;
  206. break;
  207. case SDL_WINDOWRECT_PENDING:
  208. SDL_RelativeToGlobalForWindow(window, window->pending.x, window->pending.y, x, y);
  209. *width = window->pending.w;
  210. *height = window->pending.h;
  211. break;
  212. default:
  213. // Should never be here
  214. SDL_assert_release(false);
  215. *width = 0;
  216. *height = 0;
  217. break;
  218. }
  219. /* Copy the client size in pixels into this rect structure,
  220. which we'll then adjust with AdjustWindowRectEx */
  221. rect.left = 0;
  222. rect.top = 0;
  223. rect.right = *width;
  224. rect.bottom = *height;
  225. /* borderless windows will have WM_NCCALCSIZE return 0 for the non-client area. When this happens, it looks like windows will send a resize message
  226. expanding the window client area to the previous window + chrome size, so shouldn't need to adjust the window size for the set styles.
  227. */
  228. if (!(window->flags & SDL_WINDOW_BORDERLESS) && !SDL_WINDOW_IS_POPUP(window)) {
  229. #if defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES)
  230. AdjustWindowRectEx(&rect, style, menu, 0);
  231. #else
  232. if (WIN_IsPerMonitorV2DPIAware(SDL_GetVideoDevice())) {
  233. /* With per-monitor v2, the window border/titlebar size depend on the DPI, so we need to call AdjustWindowRectExForDpi instead of
  234. AdjustWindowRectEx. */
  235. if (videodata) {
  236. UINT frame_dpi;
  237. SDL_WindowData *data = window->internal;
  238. frame_dpi = (data && videodata->GetDpiForWindow) ? videodata->GetDpiForWindow(data->hwnd) : USER_DEFAULT_SCREEN_DPI;
  239. if (videodata->AdjustWindowRectExForDpi(&rect, style, menu, styleEx, frame_dpi) == 0) {
  240. return WIN_SetError("AdjustWindowRectExForDpi()");
  241. }
  242. }
  243. } else {
  244. if (AdjustWindowRectEx(&rect, style, menu, styleEx) == 0) {
  245. return WIN_SetError("AdjustWindowRectEx()");
  246. }
  247. }
  248. #endif
  249. }
  250. // Final rect in Windows screen space, including the frame
  251. *x += rect.left;
  252. *y += rect.top;
  253. *width = (rect.right - rect.left);
  254. *height = (rect.bottom - rect.top);
  255. #ifdef HIGHDPI_DEBUG
  256. SDL_Log("WIN_AdjustWindowRectWithStyle: in: %d, %d, %dx%d, returning: %d, %d, %dx%d, used dpi %d for frame calculation",
  257. (rect_type == SDL_WINDOWRECT_FLOATING ? window->floating.x : rect_type == SDL_WINDOWRECT_WINDOWED ? window->windowed.x : window->x),
  258. (rect_type == SDL_WINDOWRECT_FLOATING ? window->floating.y : rect_type == SDL_WINDOWRECT_WINDOWED ? window->windowed.y : window->y),
  259. (rect_type == SDL_WINDOWRECT_FLOATING ? window->floating.w : rect_type == SDL_WINDOWRECT_WINDOWED ? window->windowed.w : window->w),
  260. (rect_type == SDL_WINDOWRECT_FLOATING ? window->floating.h : rect_type == SDL_WINDOWRECT_WINDOWED ? window->windowed.h : window->h),
  261. *x, *y, *width, *height, frame_dpi);
  262. #endif
  263. return true;
  264. }
  265. bool WIN_AdjustWindowRect(SDL_Window *window, int *x, int *y, int *width, int *height, SDL_WindowRect rect_type)
  266. {
  267. SDL_WindowData *data = window->internal;
  268. HWND hwnd = data->hwnd;
  269. DWORD style, styleEx;
  270. BOOL menu;
  271. style = GetWindowLong(hwnd, GWL_STYLE);
  272. styleEx = GetWindowLong(hwnd, GWL_EXSTYLE);
  273. #if defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES)
  274. menu = FALSE;
  275. #else
  276. menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
  277. #endif
  278. return WIN_AdjustWindowRectWithStyle(window, style, styleEx, menu, x, y, width, height, rect_type);
  279. }
  280. bool WIN_AdjustWindowRectForHWND(HWND hwnd, LPRECT lpRect, UINT frame_dpi)
  281. {
  282. SDL_VideoDevice *videodevice = SDL_GetVideoDevice();
  283. SDL_VideoData *videodata = videodevice ? videodevice->internal : NULL;
  284. DWORD style, styleEx;
  285. BOOL menu;
  286. style = GetWindowLong(hwnd, GWL_STYLE);
  287. styleEx = GetWindowLong(hwnd, GWL_EXSTYLE);
  288. #if defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES)
  289. menu = FALSE;
  290. #else
  291. menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
  292. #endif
  293. #if defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES)
  294. AdjustWindowRectEx(lpRect, style, menu, styleEx);
  295. #else
  296. if (WIN_IsPerMonitorV2DPIAware(videodevice)) {
  297. // With per-monitor v2, the window border/titlebar size depend on the DPI, so we need to call AdjustWindowRectExForDpi instead of AdjustWindowRectEx.
  298. if (!frame_dpi) {
  299. frame_dpi = videodata->GetDpiForWindow ? videodata->GetDpiForWindow(hwnd) : USER_DEFAULT_SCREEN_DPI;
  300. }
  301. if (!videodata->AdjustWindowRectExForDpi(lpRect, style, menu, styleEx, frame_dpi)) {
  302. return WIN_SetError("AdjustWindowRectExForDpi()");
  303. }
  304. } else {
  305. if (!AdjustWindowRectEx(lpRect, style, menu, styleEx)) {
  306. return WIN_SetError("AdjustWindowRectEx()");
  307. }
  308. }
  309. #endif
  310. return true;
  311. }
  312. bool WIN_SetWindowPositionInternal(SDL_Window *window, UINT flags, SDL_WindowRect rect_type)
  313. {
  314. SDL_Window *child_window;
  315. SDL_WindowData *data = window->internal;
  316. HWND hwnd = data->hwnd;
  317. HWND top;
  318. int x, y;
  319. int w, h;
  320. bool result = true;
  321. // Figure out what the window area will be
  322. if (SDL_ShouldAllowTopmost() && (window->flags & SDL_WINDOW_ALWAYS_ON_TOP)) {
  323. top = HWND_TOPMOST;
  324. } else {
  325. top = HWND_NOTOPMOST;
  326. }
  327. WIN_AdjustWindowRect(window, &x, &y, &w, &h, rect_type);
  328. data->expected_resize = true;
  329. if (SetWindowPos(hwnd, top, x, y, w, h, flags) == 0) {
  330. result = WIN_SetError("SetWindowPos()");
  331. }
  332. data->expected_resize = false;
  333. // Update any child windows
  334. for (child_window = window->first_child; child_window; child_window = child_window->next_sibling) {
  335. if (!WIN_SetWindowPositionInternal(child_window, flags, SDL_WINDOWRECT_CURRENT)) {
  336. result = false;
  337. }
  338. }
  339. return result;
  340. }
  341. static SDL_WindowEraseBackgroundMode GetEraseBackgroundModeHint(void)
  342. {
  343. const char *hint = SDL_GetHint(SDL_HINT_WINDOWS_ERASE_BACKGROUND_MODE);
  344. if (!hint)
  345. return SDL_ERASEBACKGROUNDMODE_INITIAL;
  346. if (SDL_strstr(hint, "never"))
  347. return SDL_ERASEBACKGROUNDMODE_NEVER;
  348. if (SDL_strstr(hint, "initial"))
  349. return SDL_ERASEBACKGROUNDMODE_INITIAL;
  350. if (SDL_strstr(hint, "always"))
  351. return SDL_ERASEBACKGROUNDMODE_ALWAYS;
  352. int mode = SDL_GetStringInteger(hint, 1);
  353. if (mode < 0 || mode > 2) {
  354. SDL_Log("GetEraseBackgroundModeHint: invalid value for SDL_HINT_WINDOWS_ERASE_BACKGROUND_MODE. Fallback to default");
  355. return SDL_ERASEBACKGROUNDMODE_INITIAL;
  356. }
  357. return (SDL_WindowEraseBackgroundMode)mode;
  358. }
  359. static bool SetupWindowData(SDL_VideoDevice *_this, SDL_Window *window, HWND hwnd, HWND parent)
  360. {
  361. SDL_VideoData *videodata = _this->internal;
  362. SDL_WindowData *data;
  363. // Allocate the window data
  364. data = (SDL_WindowData *)SDL_calloc(1, sizeof(*data));
  365. if (!data) {
  366. return false;
  367. }
  368. data->window = window;
  369. data->hwnd = hwnd;
  370. data->parent = parent;
  371. #if defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES)
  372. data->hdc = (HDC)data->hwnd;
  373. #else
  374. data->hdc = GetDC(hwnd);
  375. #endif
  376. data->hinstance = (HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE);
  377. data->mouse_button_flags = (WPARAM)-1;
  378. data->last_pointer_update = (LPARAM)-1;
  379. data->videodata = videodata;
  380. data->initializing = true;
  381. data->last_displayID = window->last_displayID;
  382. data->hint_erase_background_mode = GetEraseBackgroundModeHint();
  383. // WIN_WarpCursor() jitters by +1, and remote desktop warp wobble is +/- 1
  384. #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  385. LONG remote_desktop_adjustment = GetSystemMetrics(SM_REMOTESESSION) ? 2 : 0;
  386. data->cursor_ctrlock_rect.left = 0 - remote_desktop_adjustment;
  387. data->cursor_ctrlock_rect.top = 0;
  388. data->cursor_ctrlock_rect.right = 1 + remote_desktop_adjustment;
  389. data->cursor_ctrlock_rect.bottom = 1;
  390. #endif
  391. if (SDL_GetHintBoolean("SDL_WINDOW_RETAIN_CONTENT", false)) {
  392. data->copybits_flag = 0;
  393. } else {
  394. data->copybits_flag = SWP_NOCOPYBITS;
  395. }
  396. #ifdef HIGHDPI_DEBUG
  397. SDL_Log("SetupWindowData: initialized data->scaling_dpi to %d", data->scaling_dpi);
  398. #endif
  399. #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  400. // Associate the data with the window
  401. if (!SetProp(hwnd, TEXT("SDL_WindowData"), data)) {
  402. ReleaseDC(hwnd, data->hdc);
  403. SDL_free(data);
  404. return WIN_SetError("SetProp() failed");
  405. }
  406. #endif
  407. window->internal = data;
  408. // Set up the window proc function
  409. #ifdef GWLP_WNDPROC
  410. data->wndproc = (WNDPROC)GetWindowLongPtr(hwnd, GWLP_WNDPROC);
  411. if (data->wndproc == WIN_WindowProc) {
  412. data->wndproc = NULL;
  413. } else {
  414. SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)WIN_WindowProc);
  415. }
  416. #else
  417. data->wndproc = (WNDPROC)GetWindowLong(hwnd, GWL_WNDPROC);
  418. if (data->wndproc == WIN_WindowProc) {
  419. data->wndproc = NULL;
  420. } else {
  421. SetWindowLong(hwnd, GWL_WNDPROC, (LONG_PTR)WIN_WindowProc);
  422. }
  423. #endif
  424. // Fill in the SDL window with the window state
  425. {
  426. DWORD style = GetWindowLong(hwnd, GWL_STYLE);
  427. if (style & WS_VISIBLE) {
  428. window->flags &= ~SDL_WINDOW_HIDDEN;
  429. } else {
  430. window->flags |= SDL_WINDOW_HIDDEN;
  431. }
  432. if (style & WS_POPUP) {
  433. window->flags |= SDL_WINDOW_BORDERLESS;
  434. } else {
  435. window->flags &= ~SDL_WINDOW_BORDERLESS;
  436. }
  437. if (style & WS_THICKFRAME) {
  438. window->flags |= SDL_WINDOW_RESIZABLE;
  439. } else if (!(style & WS_POPUP)) {
  440. window->flags &= ~SDL_WINDOW_RESIZABLE;
  441. }
  442. #ifdef WS_MAXIMIZE
  443. if (style & WS_MAXIMIZE) {
  444. window->flags |= SDL_WINDOW_MAXIMIZED;
  445. } else
  446. #endif
  447. {
  448. window->flags &= ~SDL_WINDOW_MAXIMIZED;
  449. }
  450. #ifdef WS_MINIMIZE
  451. if (style & WS_MINIMIZE) {
  452. window->flags |= SDL_WINDOW_MINIMIZED;
  453. } else
  454. #endif
  455. {
  456. window->flags &= ~SDL_WINDOW_MINIMIZED;
  457. }
  458. }
  459. if (!(window->flags & SDL_WINDOW_MINIMIZED)) {
  460. RECT rect;
  461. if (GetClientRect(hwnd, &rect) && WIN_WindowRectValid(&rect)) {
  462. int w = rect.right;
  463. int h = rect.bottom;
  464. if (window->flags & SDL_WINDOW_EXTERNAL) {
  465. window->floating.w = window->windowed.w = window->w = w;
  466. window->floating.h = window->windowed.h = window->h = h;
  467. } else if ((window->windowed.w && window->windowed.w != w) || (window->windowed.h && window->windowed.h != h)) {
  468. // We tried to create a window larger than the desktop and Windows didn't allow it. Override!
  469. int x, y;
  470. // Figure out what the window area will be
  471. WIN_AdjustWindowRect(window, &x, &y, &w, &h, SDL_WINDOWRECT_FLOATING);
  472. data->expected_resize = true;
  473. SetWindowPos(hwnd, NULL, x, y, w, h, data->copybits_flag | SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOACTIVATE);
  474. data->expected_resize = false;
  475. } else {
  476. window->w = w;
  477. window->h = h;
  478. }
  479. }
  480. }
  481. #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  482. if (!(window->flags & SDL_WINDOW_MINIMIZED)) {
  483. POINT point;
  484. point.x = 0;
  485. point.y = 0;
  486. if (ClientToScreen(hwnd, &point)) {
  487. if (window->flags & SDL_WINDOW_EXTERNAL) {
  488. window->floating.x = window->windowed.x = point.x;
  489. window->floating.y = window->windowed.y = point.y;
  490. }
  491. window->x = point.x;
  492. window->y = point.y;
  493. }
  494. }
  495. WIN_UpdateWindowICCProfile(window, false);
  496. #endif
  497. #if defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES)
  498. window->flags |= SDL_WINDOW_INPUT_FOCUS;
  499. SDL_SetKeyboardFocus(window);
  500. #else
  501. if (GetFocus() == hwnd) {
  502. window->flags |= SDL_WINDOW_INPUT_FOCUS;
  503. SDL_SetKeyboardFocus(window);
  504. WIN_UpdateClipCursor(window);
  505. }
  506. #endif
  507. if (window->flags & SDL_WINDOW_ALWAYS_ON_TOP) {
  508. WIN_SetWindowAlwaysOnTop(_this, window, true);
  509. } else {
  510. WIN_SetWindowAlwaysOnTop(_this, window, false);
  511. }
  512. #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  513. // Enable multi-touch
  514. if (videodata->RegisterTouchWindow) {
  515. videodata->RegisterTouchWindow(hwnd, (TWF_FINETOUCH | TWF_WANTPALM));
  516. }
  517. #endif
  518. if (data->parent && !window->parent) {
  519. data->destroy_parent_with_window = true;
  520. }
  521. data->initializing = false;
  522. #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  523. if (window->flags & SDL_WINDOW_EXTERNAL) {
  524. // Query the title from the existing window
  525. LPTSTR title;
  526. int titleLen;
  527. bool isstack;
  528. titleLen = GetWindowTextLength(hwnd);
  529. title = SDL_small_alloc(TCHAR, titleLen + 1, &isstack);
  530. if (title) {
  531. titleLen = GetWindowText(hwnd, title, titleLen + 1);
  532. } else {
  533. titleLen = 0;
  534. }
  535. if (titleLen > 0) {
  536. window->title = WIN_StringToUTF8(title);
  537. }
  538. if (title) {
  539. SDL_small_free(title, isstack);
  540. }
  541. }
  542. #endif // !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  543. SDL_PropertiesID props = SDL_GetWindowProperties(window);
  544. SDL_SetPointerProperty(props, SDL_PROP_WINDOW_WIN32_HWND_POINTER, data->hwnd);
  545. SDL_SetPointerProperty(props, SDL_PROP_WINDOW_WIN32_HDC_POINTER, data->hdc);
  546. SDL_SetPointerProperty(props, SDL_PROP_WINDOW_WIN32_INSTANCE_POINTER, data->hinstance);
  547. // All done!
  548. return true;
  549. }
  550. static void CleanupWindowData(SDL_VideoDevice *_this, SDL_Window *window)
  551. {
  552. SDL_WindowData *data = window->internal;
  553. if (data) {
  554. #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  555. if (data->drop_target) {
  556. WIN_AcceptDragAndDrop(window, false);
  557. }
  558. if (data->ICMFileName) {
  559. SDL_free(data->ICMFileName);
  560. }
  561. if (data->keyboard_hook) {
  562. UnhookWindowsHookEx(data->keyboard_hook);
  563. }
  564. ReleaseDC(data->hwnd, data->hdc);
  565. RemoveProp(data->hwnd, TEXT("SDL_WindowData"));
  566. #endif
  567. if (!(window->flags & SDL_WINDOW_EXTERNAL)) {
  568. DestroyWindow(data->hwnd);
  569. if (data->destroy_parent_with_window && data->parent) {
  570. DestroyWindow(data->parent);
  571. }
  572. } else {
  573. // Restore any original event handler...
  574. if (data->wndproc) {
  575. #ifdef GWLP_WNDPROC
  576. SetWindowLongPtr(data->hwnd, GWLP_WNDPROC,
  577. (LONG_PTR)data->wndproc);
  578. #else
  579. SetWindowLong(data->hwnd, GWL_WNDPROC,
  580. (LONG_PTR)data->wndproc);
  581. #endif
  582. }
  583. }
  584. SDL_free(data);
  585. }
  586. window->internal = NULL;
  587. }
  588. static void WIN_ConstrainPopup(SDL_Window *window, bool output_to_pending)
  589. {
  590. // Clamp popup windows to the output borders
  591. if (SDL_WINDOW_IS_POPUP(window)) {
  592. SDL_Window *w;
  593. SDL_DisplayID displayID;
  594. SDL_Rect rect;
  595. int abs_x = window->last_position_pending ? window->pending.x : window->floating.x;
  596. int abs_y = window->last_position_pending ? window->pending.y : window->floating.y;
  597. const int width = window->last_size_pending ? window->pending.w : window->floating.w;
  598. const int height = window->last_size_pending ? window->pending.h : window->floating.h;
  599. int offset_x = 0, offset_y = 0;
  600. // Calculate the total offset from the parents
  601. for (w = window->parent; SDL_WINDOW_IS_POPUP(w); w = w->parent) {
  602. offset_x += w->x;
  603. offset_y += w->y;
  604. }
  605. offset_x += w->x;
  606. offset_y += w->y;
  607. abs_x += offset_x;
  608. abs_y += offset_y;
  609. // Constrain the popup window to the display of the toplevel parent
  610. displayID = SDL_GetDisplayForWindow(w);
  611. SDL_GetDisplayBounds(displayID, &rect);
  612. if (abs_x + width > rect.x + rect.w) {
  613. abs_x -= (abs_x + width) - (rect.x + rect.w);
  614. }
  615. if (abs_y + height > rect.y + rect.h) {
  616. abs_y -= (abs_y + height) - (rect.y + rect.h);
  617. }
  618. abs_x = SDL_max(abs_x, rect.x);
  619. abs_y = SDL_max(abs_y, rect.y);
  620. if (output_to_pending) {
  621. window->pending.x = abs_x - offset_x;
  622. window->pending.y = abs_y - offset_y;
  623. window->pending.w = width;
  624. window->pending.h = height;
  625. } else {
  626. window->floating.x = abs_x - offset_x;
  627. window->floating.y = abs_y - offset_y;
  628. window->floating.w = width;
  629. window->floating.h = height;
  630. }
  631. }
  632. }
  633. static void WIN_SetKeyboardFocus(SDL_Window *window, bool set_active_focus)
  634. {
  635. SDL_Window *toplevel = window;
  636. // Find the topmost parent
  637. while (SDL_WINDOW_IS_POPUP(toplevel)) {
  638. toplevel = toplevel->parent;
  639. }
  640. toplevel->internal->keyboard_focus = window;
  641. if (set_active_focus && !window->is_hiding && !window->is_destroying) {
  642. SDL_SetKeyboardFocus(window);
  643. }
  644. }
  645. bool WIN_CreateWindow(SDL_VideoDevice *_this, SDL_Window *window, SDL_PropertiesID create_props)
  646. {
  647. SDL_VideoData *videodata = _this->internal;
  648. HWND hwnd = (HWND)SDL_GetPointerProperty(create_props, SDL_PROP_WINDOW_CREATE_WIN32_HWND_POINTER, SDL_GetPointerProperty(create_props, "sdl2-compat.external_window", NULL));
  649. HWND parent = NULL;
  650. if (hwnd) {
  651. window->flags |= SDL_WINDOW_EXTERNAL;
  652. if (!SetupWindowData(_this, window, hwnd, parent)) {
  653. return false;
  654. }
  655. } else {
  656. DWORD style = STYLE_BASIC;
  657. DWORD styleEx = 0;
  658. int x, y;
  659. int w, h;
  660. if (window->flags & SDL_WINDOW_UTILITY) {
  661. parent = CreateWindow(SDL_Appname, TEXT(""), STYLE_BASIC, 0, 0, 32, 32, NULL, NULL, SDL_Instance, NULL);
  662. } else if (window->parent) {
  663. parent = window->parent->internal->hwnd;
  664. }
  665. style |= GetWindowStyle(window);
  666. styleEx |= GetWindowStyleEx(window);
  667. // Figure out what the window area will be
  668. WIN_ConstrainPopup(window, false);
  669. WIN_AdjustWindowRectWithStyle(window, style, styleEx, FALSE, &x, &y, &w, &h, SDL_WINDOWRECT_FLOATING);
  670. hwnd = CreateWindowEx(styleEx, SDL_Appname, TEXT(""), style,
  671. x, y, w, h, parent, NULL, SDL_Instance, NULL);
  672. if (!hwnd) {
  673. return WIN_SetError("Couldn't create window");
  674. }
  675. WIN_UpdateDarkModeForHWND(hwnd);
  676. WIN_PumpEvents(_this);
  677. if (!SetupWindowData(_this, window, hwnd, parent)) {
  678. DestroyWindow(hwnd);
  679. if (parent) {
  680. DestroyWindow(parent);
  681. }
  682. return false;
  683. }
  684. // Inform Windows of the frame change so we can respond to WM_NCCALCSIZE
  685. SetWindowPos(hwnd, NULL, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOACTIVATE);
  686. if (window->flags & SDL_WINDOW_MINIMIZED) {
  687. /* TODO: We have to clear SDL_WINDOW_HIDDEN here to ensure the window flags match the window state. The
  688. window is already shown after this and windows with WS_MINIMIZE do not generate a WM_SHOWWINDOW. This
  689. means you can't currently create a window that is initially hidden and is minimized when shown.
  690. */
  691. window->flags &= ~SDL_WINDOW_HIDDEN;
  692. ShowWindow(hwnd, SW_SHOWMINNOACTIVE);
  693. }
  694. }
  695. #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  696. // FIXME: does not work on all hardware configurations with different renders (i.e. hybrid GPUs)
  697. if (window->flags & SDL_WINDOW_TRANSPARENT) {
  698. if (videodata->DwmEnableBlurBehindWindow) {
  699. /* The region indicates which part of the window will be blurred and rest will be transparent. This
  700. is because the alpha value of the window will be used for non-blurred areas
  701. We can use (-1, -1, 0, 0) boundary to make sure no pixels are being blurred
  702. */
  703. HRGN rgn = CreateRectRgn(-1, -1, 0, 0);
  704. DWM_BLURBEHIND bb;
  705. bb.flags = (DWM_BB_ENABLE | DWM_BB_BLURREGION);
  706. bb.enable = TRUE;
  707. bb.blur_region = rgn;
  708. bb.transition_on_maxed = FALSE;
  709. videodata->DwmEnableBlurBehindWindow(hwnd, &bb);
  710. DeleteObject(rgn);
  711. }
  712. }
  713. HWND share_hwnd = (HWND)SDL_GetPointerProperty(create_props, SDL_PROP_WINDOW_CREATE_WIN32_PIXEL_FORMAT_HWND_POINTER, NULL);
  714. if (share_hwnd) {
  715. HDC hdc = GetDC(share_hwnd);
  716. int pixel_format = GetPixelFormat(hdc);
  717. PIXELFORMATDESCRIPTOR pfd;
  718. SDL_zero(pfd);
  719. DescribePixelFormat(hdc, pixel_format, sizeof(pfd), &pfd);
  720. ReleaseDC(share_hwnd, hdc);
  721. if (!SetPixelFormat(window->internal->hdc, pixel_format, &pfd)) {
  722. WIN_DestroyWindow(_this, window);
  723. return WIN_SetError("SetPixelFormat()");
  724. }
  725. } else {
  726. #endif // !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  727. if (!(window->flags & SDL_WINDOW_OPENGL)) {
  728. return true;
  729. }
  730. // The rest of this macro mess is for OpenGL or OpenGL ES windows
  731. #ifdef SDL_VIDEO_OPENGL_ES2
  732. if ((_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES ||
  733. SDL_GetHintBoolean(SDL_HINT_VIDEO_FORCE_EGL, false))
  734. #ifdef SDL_VIDEO_OPENGL_WGL
  735. && (!_this->gl_data || WIN_GL_UseEGL(_this))
  736. #endif // SDL_VIDEO_OPENGL_WGL
  737. ) {
  738. #ifdef SDL_VIDEO_OPENGL_EGL
  739. if (!WIN_GLES_SetupWindow(_this, window)) {
  740. WIN_DestroyWindow(_this, window);
  741. return false;
  742. }
  743. return true;
  744. #else
  745. return SDL_SetError("Could not create GLES window surface (EGL support not configured)");
  746. #endif // SDL_VIDEO_OPENGL_EGL
  747. }
  748. #endif // SDL_VIDEO_OPENGL_ES2
  749. #ifdef SDL_VIDEO_OPENGL_WGL
  750. if (!WIN_GL_SetupWindow(_this, window)) {
  751. WIN_DestroyWindow(_this, window);
  752. return false;
  753. }
  754. #else
  755. return SDL_SetError("Could not create GL window (WGL support not configured)");
  756. #endif
  757. #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  758. }
  759. #endif // !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  760. return true;
  761. }
  762. void WIN_SetWindowTitle(SDL_VideoDevice *_this, SDL_Window *window)
  763. {
  764. #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  765. HWND hwnd = window->internal->hwnd;
  766. LPTSTR title = WIN_UTF8ToString(window->title);
  767. SetWindowText(hwnd, title);
  768. SDL_free(title);
  769. #endif
  770. }
  771. bool WIN_SetWindowIcon(SDL_VideoDevice *_this, SDL_Window *window, SDL_Surface *icon)
  772. {
  773. #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  774. HWND hwnd = window->internal->hwnd;
  775. HICON hicon = NULL;
  776. BYTE *icon_bmp;
  777. int icon_len, mask_len, row_len, y;
  778. BITMAPINFOHEADER *bmi;
  779. Uint8 *dst;
  780. bool isstack;
  781. bool result = true;
  782. // Create temporary buffer for ICONIMAGE structure
  783. SDL_COMPILE_TIME_ASSERT(WIN_SetWindowIcon_uses_BITMAPINFOHEADER_to_prepare_an_ICONIMAGE, sizeof(BITMAPINFOHEADER) == 40);
  784. mask_len = (icon->h * (icon->w + 7) / 8);
  785. icon_len = sizeof(BITMAPINFOHEADER) + icon->h * icon->w * sizeof(Uint32) + mask_len;
  786. icon_bmp = SDL_small_alloc(BYTE, icon_len, &isstack);
  787. if (!icon_bmp) {
  788. return false;
  789. }
  790. // Write the BITMAPINFO header
  791. bmi = (BITMAPINFOHEADER *)icon_bmp;
  792. bmi->biSize = SDL_Swap32LE(sizeof(BITMAPINFOHEADER));
  793. bmi->biWidth = SDL_Swap32LE(icon->w);
  794. bmi->biHeight = SDL_Swap32LE(icon->h * 2);
  795. bmi->biPlanes = SDL_Swap16LE(1);
  796. bmi->biBitCount = SDL_Swap16LE(32);
  797. bmi->biCompression = SDL_Swap32LE(BI_RGB);
  798. bmi->biSizeImage = SDL_Swap32LE(icon->h * icon->w * sizeof(Uint32));
  799. bmi->biXPelsPerMeter = SDL_Swap32LE(0);
  800. bmi->biYPelsPerMeter = SDL_Swap32LE(0);
  801. bmi->biClrUsed = SDL_Swap32LE(0);
  802. bmi->biClrImportant = SDL_Swap32LE(0);
  803. // Write the pixels upside down into the bitmap buffer
  804. SDL_assert(icon->format == SDL_PIXELFORMAT_ARGB8888);
  805. dst = &icon_bmp[sizeof(BITMAPINFOHEADER)];
  806. row_len = icon->w * sizeof(Uint32);
  807. y = icon->h;
  808. while (y--) {
  809. Uint8 *src = (Uint8 *)icon->pixels + y * icon->pitch;
  810. SDL_memcpy(dst, src, row_len);
  811. dst += row_len;
  812. }
  813. // Write the mask
  814. SDL_memset(icon_bmp + icon_len - mask_len, 0xFF, mask_len);
  815. hicon = CreateIconFromResource(icon_bmp, icon_len, TRUE, 0x00030000);
  816. SDL_small_free(icon_bmp, isstack);
  817. if (!hicon) {
  818. result = SDL_SetError("SetWindowIcon() failed, error %08X", (unsigned int)GetLastError());
  819. }
  820. // Set the icon for the window
  821. SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hicon);
  822. // Set the icon in the task manager (should we do this?)
  823. SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM)hicon);
  824. return result;
  825. #else
  826. return SDL_Unsupported();
  827. #endif
  828. }
  829. bool WIN_SetWindowPosition(SDL_VideoDevice *_this, SDL_Window *window)
  830. {
  831. /* HighDPI support: removed SWP_NOSIZE. If the move results in a DPI change, we need to allow
  832. * the window to resize (e.g. AdjustWindowRectExForDpi frame sizes are different).
  833. */
  834. if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
  835. if (!(window->flags & (SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED))) {
  836. WIN_ConstrainPopup(window, true);
  837. return WIN_SetWindowPositionInternal(window,
  838. window->internal->copybits_flag | SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOSIZE |
  839. SWP_NOACTIVATE, SDL_WINDOWRECT_PENDING);
  840. }
  841. } else {
  842. return SDL_UpdateFullscreenMode(window, SDL_FULLSCREEN_OP_ENTER, true);
  843. }
  844. return true;
  845. }
  846. void WIN_SetWindowSize(SDL_VideoDevice *_this, SDL_Window *window)
  847. {
  848. if (!(window->flags & (SDL_WINDOW_FULLSCREEN | SDL_WINDOW_MAXIMIZED))) {
  849. WIN_SetWindowPositionInternal(window, window->internal->copybits_flag | SWP_NOMOVE | SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOACTIVATE, SDL_WINDOWRECT_PENDING);
  850. } else {
  851. // Can't resize the window
  852. window->last_size_pending = false;
  853. }
  854. }
  855. bool WIN_GetWindowBordersSize(SDL_VideoDevice *_this, SDL_Window *window, int *top, int *left, int *bottom, int *right)
  856. {
  857. #if defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES)
  858. HWND hwnd = window->internal->hwnd;
  859. RECT rcClient;
  860. /* rcClient stores the size of the inner window, while rcWindow stores the outer size relative to the top-left
  861. * screen position; so the top/left values of rcClient are always {0,0} and bottom/right are {height,width} */
  862. GetClientRect(hwnd, &rcClient);
  863. *top = rcClient.top;
  864. *left = rcClient.left;
  865. *bottom = rcClient.bottom;
  866. *right = rcClient.right;
  867. return true;
  868. #else // !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  869. HWND hwnd = window->internal->hwnd;
  870. RECT rcClient, rcWindow;
  871. POINT ptDiff;
  872. /* rcClient stores the size of the inner window, while rcWindow stores the outer size relative to the top-left
  873. * screen position; so the top/left values of rcClient are always {0,0} and bottom/right are {height,width} */
  874. if (!GetClientRect(hwnd, &rcClient)) {
  875. return SDL_SetError("GetClientRect() failed, error %08X", (unsigned int)GetLastError());
  876. }
  877. if (!GetWindowRect(hwnd, &rcWindow)) {
  878. return SDL_SetError("GetWindowRect() failed, error %08X", (unsigned int)GetLastError());
  879. }
  880. /* convert the top/left values to make them relative to
  881. * the window; they will end up being slightly negative */
  882. ptDiff.y = rcWindow.top;
  883. ptDiff.x = rcWindow.left;
  884. if (!ScreenToClient(hwnd, &ptDiff)) {
  885. return SDL_SetError("ScreenToClient() failed, error %08X", (unsigned int)GetLastError());
  886. }
  887. rcWindow.top = ptDiff.y;
  888. rcWindow.left = ptDiff.x;
  889. /* convert the bottom/right values to make them relative to the window,
  890. * these will be slightly bigger than the inner width/height */
  891. ptDiff.y = rcWindow.bottom;
  892. ptDiff.x = rcWindow.right;
  893. if (!ScreenToClient(hwnd, &ptDiff)) {
  894. return SDL_SetError("ScreenToClient() failed, error %08X", (unsigned int)GetLastError());
  895. }
  896. rcWindow.bottom = ptDiff.y;
  897. rcWindow.right = ptDiff.x;
  898. /* Now that both the inner and outer rects use the same coordinate system we can subtract them to get the border size.
  899. * Keep in mind that the top/left coordinates of rcWindow are negative because the border lies slightly before {0,0},
  900. * so switch them around because SDL3 wants them in positive. */
  901. *top = rcClient.top - rcWindow.top;
  902. *left = rcClient.left - rcWindow.left;
  903. *bottom = rcWindow.bottom - rcClient.bottom;
  904. *right = rcWindow.right - rcClient.right;
  905. return true;
  906. #endif // !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  907. }
  908. void WIN_GetWindowSizeInPixels(SDL_VideoDevice *_this, SDL_Window *window, int *w, int *h)
  909. {
  910. const SDL_WindowData *data = window->internal;
  911. HWND hwnd = data->hwnd;
  912. RECT rect;
  913. if (GetClientRect(hwnd, &rect) && WIN_WindowRectValid(&rect)) {
  914. *w = rect.right;
  915. *h = rect.bottom;
  916. } else if (window->last_pixel_w && window->last_pixel_h) {
  917. *w = window->last_pixel_w;
  918. *h = window->last_pixel_h;
  919. } else {
  920. // Probably created minimized, use the restored size
  921. *w = window->floating.w;
  922. *h = window->floating.h;
  923. }
  924. }
  925. void WIN_ShowWindow(SDL_VideoDevice *_this, SDL_Window *window)
  926. {
  927. DWORD style;
  928. HWND hwnd;
  929. bool bActivate = SDL_GetHintBoolean(SDL_HINT_WINDOW_ACTIVATE_WHEN_SHOWN, true);
  930. if (SDL_WINDOW_IS_POPUP(window)) {
  931. // Update our position in case our parent moved while we were hidden
  932. WIN_SetWindowPosition(_this, window);
  933. }
  934. hwnd = window->internal->hwnd;
  935. style = GetWindowLong(hwnd, GWL_EXSTYLE);
  936. if (style & WS_EX_NOACTIVATE) {
  937. bActivate = false;
  938. }
  939. if (bActivate) {
  940. ShowWindow(hwnd, SW_SHOW);
  941. } else {
  942. // Use SetWindowPos instead of ShowWindow to avoid activating the parent window if this is a child window
  943. SetWindowPos(hwnd, NULL, 0, 0, 0, 0, window->internal->copybits_flag | SWP_SHOWWINDOW | SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOOWNERZORDER);
  944. }
  945. if (window->flags & SDL_WINDOW_POPUP_MENU && bActivate) {
  946. WIN_SetKeyboardFocus(window, window->parent == SDL_GetKeyboardFocus());
  947. }
  948. if (window->flags & SDL_WINDOW_MODAL) {
  949. WIN_SetWindowModal(_this, window, true);
  950. }
  951. }
  952. void WIN_HideWindow(SDL_VideoDevice *_this, SDL_Window *window)
  953. {
  954. HWND hwnd = window->internal->hwnd;
  955. if (window->flags & SDL_WINDOW_MODAL) {
  956. WIN_SetWindowModal(_this, window, false);
  957. }
  958. ShowWindow(hwnd, SW_HIDE);
  959. // Transfer keyboard focus back to the parent
  960. if (window->flags & SDL_WINDOW_POPUP_MENU) {
  961. SDL_Window *new_focus = window->parent;
  962. bool set_focus = window == SDL_GetKeyboardFocus();
  963. // Find the highest level window, up to the toplevel parent, that isn't being hidden or destroyed.
  964. while (SDL_WINDOW_IS_POPUP(new_focus) && (new_focus->is_hiding || new_focus->is_destroying)) {
  965. new_focus = new_focus->parent;
  966. // If some window in the chain currently had keyboard focus, set it to the new lowest-level window.
  967. if (!set_focus) {
  968. set_focus = new_focus == SDL_GetKeyboardFocus();
  969. }
  970. }
  971. WIN_SetKeyboardFocus(new_focus, set_focus);
  972. }
  973. }
  974. void WIN_RaiseWindow(SDL_VideoDevice *_this, SDL_Window *window)
  975. {
  976. #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  977. /* If desired, raise the window more forcefully.
  978. * Technique taken from http://stackoverflow.com/questions/916259/ .
  979. * Specifically, http://stackoverflow.com/a/34414846 .
  980. *
  981. * The issue is that Microsoft has gone through a lot of trouble to make it
  982. * nearly impossible to programmatically move a window to the foreground,
  983. * for "security" reasons. Apparently, the following song-and-dance gets
  984. * around their objections. */
  985. bool bForce = SDL_GetHintBoolean(SDL_HINT_FORCE_RAISEWINDOW, false);
  986. bool bActivate = SDL_GetHintBoolean(SDL_HINT_WINDOW_ACTIVATE_WHEN_RAISED, true);
  987. HWND hCurWnd = NULL;
  988. DWORD dwMyID = 0u;
  989. DWORD dwCurID = 0u;
  990. SDL_WindowData *data = window->internal;
  991. HWND hwnd = data->hwnd;
  992. if (bForce) {
  993. hCurWnd = GetForegroundWindow();
  994. dwMyID = GetCurrentThreadId();
  995. dwCurID = GetWindowThreadProcessId(hCurWnd, NULL);
  996. ShowWindow(hwnd, SW_RESTORE);
  997. AttachThreadInput(dwCurID, dwMyID, TRUE);
  998. SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
  999. if (!SDL_ShouldAllowTopmost() || !(window->flags & SDL_WINDOW_ALWAYS_ON_TOP)) {
  1000. SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
  1001. }
  1002. }
  1003. if (bActivate) {
  1004. SetForegroundWindow(hwnd);
  1005. if (window->flags & SDL_WINDOW_POPUP_MENU) {
  1006. WIN_SetKeyboardFocus(window, window->parent == SDL_GetKeyboardFocus());
  1007. }
  1008. } else {
  1009. SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, data->copybits_flag | SWP_NOMOVE | SWP_NOSIZE | SWP_NOOWNERZORDER | SWP_NOACTIVATE);
  1010. }
  1011. if (bForce) {
  1012. AttachThreadInput(dwCurID, dwMyID, FALSE);
  1013. SetFocus(hwnd);
  1014. SetActiveWindow(hwnd);
  1015. }
  1016. #endif // !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  1017. }
  1018. void WIN_MaximizeWindow(SDL_VideoDevice *_this, SDL_Window *window)
  1019. {
  1020. SDL_WindowData *data = window->internal;
  1021. if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
  1022. HWND hwnd = data->hwnd;
  1023. data->expected_resize = true;
  1024. ShowWindow(hwnd, SW_MAXIMIZE);
  1025. data->expected_resize = false;
  1026. /* Clamp the maximized window size to the max window size.
  1027. * This is automatic if maximizing from the window controls.
  1028. */
  1029. if (window->max_w || window->max_h) {
  1030. int fx, fy, fw, fh;
  1031. window->windowed.w = window->max_w ? SDL_min(window->w, window->max_w) : window->windowed.w;
  1032. window->windowed.h = window->max_h ? SDL_min(window->h, window->max_h) : window->windowed.h;
  1033. WIN_AdjustWindowRect(window, &fx, &fy, &fw, &fh, SDL_WINDOWRECT_WINDOWED);
  1034. data->expected_resize = true;
  1035. SetWindowPos(hwnd, HWND_TOP, fx, fy, fw, fh, data->copybits_flag | SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOACTIVATE);
  1036. data->expected_resize = false;
  1037. }
  1038. } else {
  1039. data->windowed_mode_was_maximized = true;
  1040. }
  1041. }
  1042. void WIN_MinimizeWindow(SDL_VideoDevice *_this, SDL_Window *window)
  1043. {
  1044. HWND hwnd = window->internal->hwnd;
  1045. ShowWindow(hwnd, SW_MINIMIZE);
  1046. }
  1047. void WIN_SetWindowBordered(SDL_VideoDevice *_this, SDL_Window *window, bool bordered)
  1048. {
  1049. SDL_WindowData *data = window->internal;
  1050. HWND hwnd = data->hwnd;
  1051. DWORD style;
  1052. style = GetWindowLong(hwnd, GWL_STYLE);
  1053. style &= ~STYLE_MASK;
  1054. style |= GetWindowStyle(window);
  1055. data->in_border_change = true;
  1056. SetWindowLong(hwnd, GWL_STYLE, style);
  1057. WIN_SetWindowPositionInternal(window, data->copybits_flag | SWP_FRAMECHANGED | SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOACTIVATE, SDL_WINDOWRECT_CURRENT);
  1058. data->in_border_change = false;
  1059. }
  1060. void WIN_SetWindowResizable(SDL_VideoDevice *_this, SDL_Window *window, bool resizable)
  1061. {
  1062. SDL_WindowData *data = window->internal;
  1063. HWND hwnd = data->hwnd;
  1064. DWORD style;
  1065. style = GetWindowLong(hwnd, GWL_STYLE);
  1066. style &= ~STYLE_MASK;
  1067. style |= GetWindowStyle(window);
  1068. SetWindowLong(hwnd, GWL_STYLE, style);
  1069. }
  1070. void WIN_SetWindowAlwaysOnTop(SDL_VideoDevice *_this, SDL_Window *window, bool on_top)
  1071. {
  1072. WIN_SetWindowPositionInternal(window, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOOWNERZORDER, SDL_WINDOWRECT_CURRENT);
  1073. }
  1074. void WIN_RestoreWindow(SDL_VideoDevice *_this, SDL_Window *window)
  1075. {
  1076. SDL_WindowData *data = window->internal;
  1077. if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
  1078. HWND hwnd = data->hwnd;
  1079. data->expected_resize = true;
  1080. ShowWindow(hwnd, SW_RESTORE);
  1081. data->expected_resize = false;
  1082. } else {
  1083. data->windowed_mode_was_maximized = false;
  1084. }
  1085. }
  1086. static void WIN_UpdateCornerRoundingForHWND(SDL_VideoDevice *_this, HWND hwnd, DWM_WINDOW_CORNER_PREFERENCE cornerPref)
  1087. {
  1088. SDL_VideoData *videodata = _this->internal;
  1089. if (videodata->DwmSetWindowAttribute) {
  1090. videodata->DwmSetWindowAttribute(hwnd, DWMWA_WINDOW_CORNER_PREFERENCE, &cornerPref, sizeof(cornerPref));
  1091. }
  1092. }
  1093. static void WIN_UpdateBorderColorForHWND(SDL_VideoDevice *_this, HWND hwnd, COLORREF colorRef)
  1094. {
  1095. SDL_VideoData *videodata = _this->internal;
  1096. if (videodata->DwmSetWindowAttribute) {
  1097. videodata->DwmSetWindowAttribute(hwnd, DWMWA_BORDER_COLOR, &colorRef, sizeof(colorRef));
  1098. }
  1099. }
  1100. /**
  1101. * Reconfigures the window to fill the given display, if fullscreen is true, otherwise restores the window.
  1102. */
  1103. SDL_FullscreenResult WIN_SetWindowFullscreen(SDL_VideoDevice *_this, SDL_Window *window, SDL_VideoDisplay *display, SDL_FullscreenOp fullscreen)
  1104. {
  1105. #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  1106. SDL_DisplayData *displaydata = display->internal;
  1107. SDL_WindowData *data = window->internal;
  1108. HWND hwnd = data ? data->hwnd : NULL;
  1109. MONITORINFO minfo;
  1110. DWORD style, styleEx;
  1111. HWND top;
  1112. int x, y;
  1113. int w, h;
  1114. bool enterMaximized = false;
  1115. #ifdef HIGHDPI_DEBUG
  1116. SDL_Log("WIN_SetWindowFullscreen: %d", (int)fullscreen);
  1117. #endif
  1118. /* Early out if already not in fullscreen, or the styling on
  1119. * external windows may end up being overridden.
  1120. */
  1121. if (!(window->flags & SDL_WINDOW_FULLSCREEN) && !fullscreen) {
  1122. return SDL_FULLSCREEN_SUCCEEDED;
  1123. }
  1124. if (SDL_ShouldAllowTopmost() && (window->flags & SDL_WINDOW_ALWAYS_ON_TOP)) {
  1125. top = HWND_TOPMOST;
  1126. } else {
  1127. top = HWND_NOTOPMOST;
  1128. }
  1129. /* Use GetMonitorInfo instead of WIN_GetDisplayBounds because we want the
  1130. monitor bounds in Windows coordinates (pixels) rather than SDL coordinates (points). */
  1131. SDL_zero(minfo);
  1132. minfo.cbSize = sizeof(MONITORINFO);
  1133. if (!GetMonitorInfo(displaydata->MonitorHandle, &minfo)) {
  1134. SDL_SetError("GetMonitorInfo failed");
  1135. return SDL_FULLSCREEN_FAILED;
  1136. }
  1137. SDL_SendWindowEvent(window, fullscreen ? SDL_EVENT_WINDOW_ENTER_FULLSCREEN : SDL_EVENT_WINDOW_LEAVE_FULLSCREEN, 0, 0);
  1138. style = GetWindowLong(hwnd, GWL_STYLE);
  1139. style &= ~STYLE_MASK;
  1140. style |= GetWindowStyle(window);
  1141. styleEx = GetWindowLong(hwnd, GWL_EXSTYLE);
  1142. if (fullscreen) {
  1143. x = minfo.rcMonitor.left;
  1144. y = minfo.rcMonitor.top;
  1145. w = minfo.rcMonitor.right - minfo.rcMonitor.left;
  1146. h = minfo.rcMonitor.bottom - minfo.rcMonitor.top;
  1147. /* Unset the maximized flag. This fixes
  1148. https://bugzilla.libsdl.org/show_bug.cgi?id=3215
  1149. */
  1150. if (style & WS_MAXIMIZE) {
  1151. data->windowed_mode_was_maximized = true;
  1152. style &= ~WS_MAXIMIZE;
  1153. }
  1154. // Disable corner rounding & border color (Windows 11+) so the window fills the full screen
  1155. WIN_UpdateCornerRoundingForHWND(_this, hwnd, DWMWCP_DONOTROUND);
  1156. WIN_UpdateBorderColorForHWND(_this, hwnd, DWMWA_COLOR_NONE);
  1157. } else {
  1158. BOOL menu;
  1159. WIN_UpdateCornerRoundingForHWND(_this, hwnd, DWMWCP_DEFAULT);
  1160. WIN_UpdateBorderColorForHWND(_this, hwnd, DWMWA_COLOR_DEFAULT);
  1161. /* Restore window-maximization state, as applicable.
  1162. Special care is taken to *not* do this if and when we're
  1163. alt-tab'ing away (to some other window; as indicated by
  1164. in_window_deactivation), otherwise
  1165. https://bugzilla.libsdl.org/show_bug.cgi?id=3215 can reproduce!
  1166. */
  1167. if (data->windowed_mode_was_maximized && !data->in_window_deactivation) {
  1168. enterMaximized = true;
  1169. data->disable_move_size_events = true;
  1170. }
  1171. menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
  1172. WIN_AdjustWindowRectWithStyle(window, style, styleEx, menu,
  1173. &x, &y,
  1174. &w, &h,
  1175. SDL_WINDOWRECT_FLOATING);
  1176. data->windowed_mode_was_maximized = false;
  1177. /* A window may have been maximized by dragging it to the top of another display, in which case the floating
  1178. * position may be out-of-date. If the window is being restored to maximized, and the maximized and floating
  1179. * position are on different displays, try to center the window on the maximized display for restoration, which
  1180. * mimics native Windows behavior.
  1181. */
  1182. if (enterMaximized) {
  1183. const SDL_Point windowed_point = { window->windowed.x, window->windowed.y };
  1184. const SDL_Point floating_point = { window->floating.x, window->floating.y };
  1185. const SDL_DisplayID floating_display = SDL_GetDisplayForPoint(&floating_point);
  1186. const SDL_DisplayID windowed_display = SDL_GetDisplayForPoint(&windowed_point);
  1187. if (floating_display != windowed_display) {
  1188. SDL_Rect bounds;
  1189. SDL_zero(bounds);
  1190. SDL_GetDisplayUsableBounds(windowed_display, &bounds);
  1191. if (w < bounds.w) {
  1192. x = bounds.x + (bounds.w - w) / 2;
  1193. } else {
  1194. x = bounds.x;
  1195. }
  1196. if (h < bounds.h) {
  1197. y = bounds.y + (bounds.h - h) / 2;
  1198. } else {
  1199. y = bounds.y;
  1200. }
  1201. }
  1202. }
  1203. }
  1204. /* Always reset the window to the base floating size before possibly re-applying the maximized state,
  1205. * otherwise, the base floating size can seemingly be lost in some cases.
  1206. */
  1207. SetWindowLong(hwnd, GWL_STYLE, style);
  1208. data->expected_resize = true;
  1209. SetWindowPos(hwnd, top, x, y, w, h, data->copybits_flag | SWP_NOACTIVATE);
  1210. data->expected_resize = false;
  1211. data->disable_move_size_events = false;
  1212. if (enterMaximized) {
  1213. WIN_MaximizeWindow(_this, window);
  1214. }
  1215. #ifdef HIGHDPI_DEBUG
  1216. SDL_Log("WIN_SetWindowFullscreen: %d finished. Set window to %d,%d, %dx%d", (int)fullscreen, x, y, w, h);
  1217. #endif
  1218. #endif // !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  1219. return SDL_FULLSCREEN_SUCCEEDED;
  1220. }
  1221. #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  1222. void WIN_UpdateWindowICCProfile(SDL_Window *window, bool send_event)
  1223. {
  1224. SDL_WindowData *data = window->internal;
  1225. SDL_DisplayData *displaydata = SDL_GetDisplayDriverDataForWindow(window);
  1226. if (displaydata) {
  1227. HDC hdc = CreateDCW(displaydata->DeviceName, NULL, NULL, NULL);
  1228. if (hdc) {
  1229. WCHAR fileName[MAX_PATH];
  1230. DWORD fileNameSize = SDL_arraysize(fileName);
  1231. if (GetICMProfileW(hdc, &fileNameSize, fileName)) {
  1232. // fileNameSize includes '\0' on return
  1233. if (!data->ICMFileName ||
  1234. SDL_wcscmp(data->ICMFileName, fileName) != 0) {
  1235. if (data->ICMFileName) {
  1236. SDL_free(data->ICMFileName);
  1237. }
  1238. data->ICMFileName = SDL_wcsdup(fileName);
  1239. if (send_event) {
  1240. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_ICCPROF_CHANGED, 0, 0);
  1241. }
  1242. }
  1243. }
  1244. DeleteDC(hdc);
  1245. }
  1246. }
  1247. }
  1248. void *WIN_GetWindowICCProfile(SDL_VideoDevice *_this, SDL_Window *window, size_t *size)
  1249. {
  1250. SDL_WindowData *data = window->internal;
  1251. char *filename_utf8;
  1252. void *iccProfileData = NULL;
  1253. filename_utf8 = WIN_StringToUTF8(data->ICMFileName);
  1254. if (filename_utf8) {
  1255. iccProfileData = SDL_LoadFile(filename_utf8, size);
  1256. if (!iccProfileData) {
  1257. SDL_SetError("Could not open ICC profile");
  1258. }
  1259. SDL_free(filename_utf8);
  1260. }
  1261. return iccProfileData;
  1262. }
  1263. static void WIN_GrabKeyboard(SDL_Window *window)
  1264. {
  1265. SDL_WindowData *data = window->internal;
  1266. HMODULE module;
  1267. if (data->keyboard_hook) {
  1268. return;
  1269. }
  1270. /* SetWindowsHookEx() needs to know which module contains the hook we
  1271. want to install. This is complicated by the fact that SDL can be
  1272. linked statically or dynamically. Fortunately XP and later provide
  1273. this nice API that will go through the loaded modules and find the
  1274. one containing our code.
  1275. */
  1276. if (!GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT | GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
  1277. (LPTSTR)WIN_KeyboardHookProc,
  1278. &module)) {
  1279. return;
  1280. }
  1281. // Capture a snapshot of the current keyboard state before the hook
  1282. if (!GetKeyboardState(data->videodata->pre_hook_key_state)) {
  1283. return;
  1284. }
  1285. /* To grab the keyboard, we have to install a low-level keyboard hook to
  1286. intercept keys that would normally be captured by the OS. Intercepting
  1287. all key events on the system is rather invasive, but it's what Microsoft
  1288. actually documents that you do to capture these.
  1289. */
  1290. data->keyboard_hook = SetWindowsHookEx(WH_KEYBOARD_LL, WIN_KeyboardHookProc, module, 0);
  1291. }
  1292. void WIN_UngrabKeyboard(SDL_Window *window)
  1293. {
  1294. SDL_WindowData *data = window->internal;
  1295. if (data->keyboard_hook) {
  1296. UnhookWindowsHookEx(data->keyboard_hook);
  1297. data->keyboard_hook = NULL;
  1298. }
  1299. }
  1300. bool WIN_SetWindowMouseRect(SDL_VideoDevice *_this, SDL_Window *window)
  1301. {
  1302. WIN_UpdateClipCursor(window);
  1303. return true;
  1304. }
  1305. bool WIN_SetWindowMouseGrab(SDL_VideoDevice *_this, SDL_Window *window, bool grabbed)
  1306. {
  1307. WIN_UpdateClipCursor(window);
  1308. return true;
  1309. }
  1310. bool WIN_SetWindowKeyboardGrab(SDL_VideoDevice *_this, SDL_Window *window, bool grabbed)
  1311. {
  1312. if (grabbed) {
  1313. WIN_GrabKeyboard(window);
  1314. } else {
  1315. WIN_UngrabKeyboard(window);
  1316. }
  1317. return true;
  1318. }
  1319. #endif // !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  1320. void WIN_DestroyWindow(SDL_VideoDevice *_this, SDL_Window *window)
  1321. {
  1322. CleanupWindowData(_this, window);
  1323. }
  1324. /*
  1325. * Creates a HelperWindow used for DirectInput.
  1326. */
  1327. bool SDL_HelperWindowCreate(void)
  1328. {
  1329. HINSTANCE hInstance = GetModuleHandle(NULL);
  1330. WNDCLASS wce;
  1331. // Make sure window isn't created twice.
  1332. if (SDL_HelperWindow != NULL) {
  1333. return true;
  1334. }
  1335. // Create the class.
  1336. SDL_zero(wce);
  1337. wce.lpfnWndProc = DefWindowProc;
  1338. wce.lpszClassName = SDL_HelperWindowClassName;
  1339. wce.hInstance = hInstance;
  1340. // Register the class.
  1341. SDL_HelperWindowClass = RegisterClass(&wce);
  1342. if (SDL_HelperWindowClass == 0 && GetLastError() != ERROR_CLASS_ALREADY_EXISTS) {
  1343. return WIN_SetError("Unable to create Helper Window Class");
  1344. }
  1345. // Create the window.
  1346. SDL_HelperWindow = CreateWindowEx(0, SDL_HelperWindowClassName,
  1347. SDL_HelperWindowName,
  1348. WS_OVERLAPPED, CW_USEDEFAULT,
  1349. CW_USEDEFAULT, CW_USEDEFAULT,
  1350. CW_USEDEFAULT, HWND_MESSAGE, NULL,
  1351. hInstance, NULL);
  1352. if (!SDL_HelperWindow) {
  1353. UnregisterClass(SDL_HelperWindowClassName, hInstance);
  1354. return WIN_SetError("Unable to create Helper Window");
  1355. }
  1356. return true;
  1357. }
  1358. /*
  1359. * Destroys the HelperWindow previously created with SDL_HelperWindowCreate.
  1360. */
  1361. void SDL_HelperWindowDestroy(void)
  1362. {
  1363. HINSTANCE hInstance = GetModuleHandle(NULL);
  1364. // Destroy the window.
  1365. if (SDL_HelperWindow != NULL) {
  1366. if (DestroyWindow(SDL_HelperWindow) == 0) {
  1367. WIN_SetError("Unable to destroy Helper Window");
  1368. return;
  1369. }
  1370. SDL_HelperWindow = NULL;
  1371. }
  1372. // Unregister the class.
  1373. if (SDL_HelperWindowClass != 0) {
  1374. if ((UnregisterClass(SDL_HelperWindowClassName, hInstance)) == 0) {
  1375. WIN_SetError("Unable to destroy Helper Window Class");
  1376. return;
  1377. }
  1378. SDL_HelperWindowClass = 0;
  1379. }
  1380. }
  1381. #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  1382. void WIN_OnWindowEnter(SDL_VideoDevice *_this, SDL_Window *window)
  1383. {
  1384. SDL_WindowData *data = window->internal;
  1385. if (!data || !data->hwnd) {
  1386. // The window wasn't fully initialized
  1387. return;
  1388. }
  1389. if (window->flags & SDL_WINDOW_ALWAYS_ON_TOP) {
  1390. WIN_SetWindowPositionInternal(window, data->copybits_flag | SWP_NOSIZE | SWP_NOACTIVATE, SDL_WINDOWRECT_CURRENT);
  1391. }
  1392. }
  1393. static BOOL GetClientScreenRect(HWND hwnd, RECT *rect)
  1394. {
  1395. return GetClientRect(hwnd, rect) && // RECT( left , top , right , bottom )
  1396. ClientToScreen(hwnd, (LPPOINT)rect) && // POINT( left , top )
  1397. ClientToScreen(hwnd, (LPPOINT)rect + 1); // POINT( right , bottom )
  1398. }
  1399. void WIN_UnclipCursorForWindow(SDL_Window *window) {
  1400. SDL_WindowData *data = window->internal;
  1401. RECT rect;
  1402. if (GetClipCursor(&rect) && SDL_memcmp(&rect, &data->cursor_clipped_rect, sizeof(rect)) == 0) {
  1403. ClipCursor(NULL);
  1404. SDL_zero(data->cursor_clipped_rect);
  1405. }
  1406. }
  1407. void WIN_UpdateClipCursor(SDL_Window *window)
  1408. {
  1409. SDL_WindowData *data = window->internal;
  1410. if (data->in_title_click || data->focus_click_pending || data->skip_update_clipcursor) {
  1411. return;
  1412. }
  1413. SDL_Rect mouse_rect = window->mouse_rect;
  1414. bool win_mouse_rect = (mouse_rect.w > 0 && mouse_rect.h > 0);
  1415. bool win_have_focus = (window->flags & SDL_WINDOW_INPUT_FOCUS);
  1416. bool win_is_grabbed = (window->flags & SDL_WINDOW_MOUSE_GRABBED);
  1417. bool win_in_relmode = (window->flags & SDL_WINDOW_MOUSE_RELATIVE_MODE);
  1418. bool cursor_confine = win_in_relmode || win_is_grabbed || win_mouse_rect;
  1419. // This is verbatim translation of the old logic,
  1420. // but I don't quite get what it's trying to do.
  1421. // A clean-room implementation according to MSDN
  1422. // documentation of GetClipCursor is provided in
  1423. // a commented-out block below.
  1424. if (!win_have_focus || !cursor_confine) {
  1425. SDL_VideoDevice *videodevice = SDL_GetVideoDevice();
  1426. RECT current;
  1427. if (!GetClipCursor(&current)) {
  1428. return;
  1429. }
  1430. if (videodevice && (
  1431. current.left != videodevice->desktop_bounds.x ||
  1432. current.top != videodevice->desktop_bounds.y
  1433. )) {
  1434. POINT first, second;
  1435. first.x = current.left;
  1436. first.y = current.top;
  1437. second.x = current.right - 1;
  1438. second.y = current.bottom - 1;
  1439. if (!PtInRect(&data->cursor_clipped_rect, first) ||
  1440. !PtInRect(&data->cursor_clipped_rect, second)) {
  1441. return;
  1442. }
  1443. }
  1444. ClipCursor(NULL);
  1445. SDL_zero(data->cursor_clipped_rect);
  1446. return;
  1447. }
  1448. // if (!win_have_focus || !cursor_confine) {
  1449. // RECT current;
  1450. // SDL_VideoDevice *videodevice = SDL_GetVideoDevice();
  1451. // if (GetClipCursor(&current) && (!videodevice ||
  1452. // current.left != videodevice->desktop_bounds.x ||
  1453. // current.top != videodevice->desktop_bounds.y ||
  1454. // current.right != videodevice->desktop_bounds.x + videodevice->desktop_bounds.w ||
  1455. // current.bottom != videodevice->desktop_bounds.y + videodevice->desktop_bounds.h )) {
  1456. // ClipCursor(NULL);
  1457. // SDL_zero(data->cursor_clipped_rect);
  1458. // }
  1459. // return;
  1460. // }
  1461. SDL_Mouse *mouse = SDL_GetMouse();
  1462. bool lock_to_ctr = (mouse->relative_mode && mouse->relative_mode_center);
  1463. RECT client;
  1464. if (!GetClientScreenRect(data->hwnd, &client)) {
  1465. return;
  1466. }
  1467. RECT target = client;
  1468. if (lock_to_ctr) {
  1469. LONG cx = (client.left + client.right ) / 2;
  1470. LONG cy = (client.top + client.bottom) / 2;
  1471. target = data->cursor_ctrlock_rect;
  1472. target.left += cx;
  1473. target.right += cx;
  1474. target.top += cy;
  1475. target.bottom += cy;
  1476. } else if (win_mouse_rect) {
  1477. RECT custom, overlap;
  1478. custom.left = client.left + mouse_rect.x;
  1479. custom.top = client.top + mouse_rect.y;
  1480. custom.right = client.left + mouse_rect.x + mouse_rect.w;
  1481. custom.bottom = client.top + mouse_rect.y + mouse_rect.h;
  1482. if (IntersectRect(&overlap, &client, &custom)) {
  1483. target = overlap;
  1484. } else if (!win_is_grabbed) {
  1485. WIN_UnclipCursorForWindow(window);
  1486. return;
  1487. }
  1488. }
  1489. if (GetClipCursor(&client) &&
  1490. 0 != SDL_memcmp(&target, &client, sizeof(client)) &&
  1491. ClipCursor(&target)) {
  1492. data->cursor_clipped_rect = target; // ClipCursor may fail if rect beyond screen
  1493. }
  1494. }
  1495. bool WIN_SetWindowHitTest(SDL_Window *window, bool enabled)
  1496. {
  1497. return true; // just succeed, the real work is done elsewhere.
  1498. }
  1499. #endif // !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  1500. bool WIN_SetWindowOpacity(SDL_VideoDevice *_this, SDL_Window *window, float opacity)
  1501. {
  1502. #if defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES)
  1503. return false;
  1504. #else
  1505. const SDL_WindowData *data = window->internal;
  1506. HWND hwnd = data->hwnd;
  1507. const LONG style = GetWindowLong(hwnd, GWL_EXSTYLE);
  1508. SDL_assert(style != 0);
  1509. if (opacity == 1.0f) {
  1510. // want it fully opaque, just mark it unlayered if necessary.
  1511. if (style & WS_EX_LAYERED) {
  1512. if (SetWindowLong(hwnd, GWL_EXSTYLE, style & ~WS_EX_LAYERED) == 0) {
  1513. return WIN_SetError("SetWindowLong()");
  1514. }
  1515. }
  1516. } else {
  1517. const BYTE alpha = (BYTE)((int)(opacity * 255.0f));
  1518. // want it transparent, mark it layered if necessary.
  1519. if (!(style & WS_EX_LAYERED)) {
  1520. if (SetWindowLong(hwnd, GWL_EXSTYLE, style | WS_EX_LAYERED) == 0) {
  1521. return WIN_SetError("SetWindowLong()");
  1522. }
  1523. }
  1524. if (SetLayeredWindowAttributes(hwnd, 0, alpha, LWA_ALPHA) == 0) {
  1525. return WIN_SetError("SetLayeredWindowAttributes()");
  1526. }
  1527. }
  1528. return true;
  1529. #endif // !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  1530. }
  1531. #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  1532. static const char *SDLGetClipboardFormatName(UINT cf, char *text, int len)
  1533. {
  1534. switch (cf) {
  1535. case CF_TEXT:
  1536. return "CF_TEXT";
  1537. case CF_BITMAP:
  1538. return "CF_BITMAP";
  1539. case CF_METAFILEPICT:
  1540. return "CF_METAFILEPICT";
  1541. case CF_SYLK:
  1542. return "CF_SYLK";
  1543. case CF_DIF:
  1544. return "CF_DIF";
  1545. case CF_TIFF:
  1546. return "CF_TIFF";
  1547. case CF_OEMTEXT:
  1548. return "CF_OEMTEXT";
  1549. case CF_DIB:
  1550. return "CF_DIB";
  1551. case CF_PALETTE:
  1552. return "CF_PALETTE";
  1553. case CF_PENDATA:
  1554. return "CF_PENDATA";
  1555. case CF_RIFF:
  1556. return "CF_RIFF";
  1557. case CF_WAVE:
  1558. return "CF_WAVE";
  1559. case CF_UNICODETEXT:
  1560. return "CF_UNICODETEXT";
  1561. case CF_ENHMETAFILE:
  1562. return "CF_ENHMETAFILE";
  1563. case CF_HDROP:
  1564. return "CF_HDROP";
  1565. case CF_LOCALE:
  1566. return "CF_LOCALE";
  1567. case CF_DIBV5:
  1568. return "CF_DIBV5";
  1569. case CF_OWNERDISPLAY:
  1570. return "CF_OWNERDISPLAY";
  1571. case CF_DSPTEXT:
  1572. return "CF_DSPTEXT";
  1573. case CF_DSPBITMAP:
  1574. return "CF_DSPBITMAP";
  1575. case CF_DSPMETAFILEPICT:
  1576. return "CF_DSPMETAFILEPICT";
  1577. case CF_DSPENHMETAFILE:
  1578. return "CF_DSPENHMETAFILE";
  1579. default:
  1580. if (GetClipboardFormatNameA(cf, text, len)) {
  1581. return text;
  1582. } else {
  1583. return NULL;
  1584. }
  1585. }
  1586. }
  1587. static STDMETHODIMP_(ULONG) SDLDropTarget_AddRef(SDLDropTarget *target)
  1588. {
  1589. return ++target->refcount;
  1590. }
  1591. static STDMETHODIMP_(ULONG) SDLDropTarget_Release(SDLDropTarget *target)
  1592. {
  1593. --target->refcount;
  1594. if (target->refcount == 0) {
  1595. SDL_free(target);
  1596. return 0;
  1597. }
  1598. return target->refcount;
  1599. }
  1600. static STDMETHODIMP SDLDropTarget_QueryInterface(SDLDropTarget *target, REFIID riid, PVOID *ppv)
  1601. {
  1602. if (ppv == NULL) {
  1603. return E_INVALIDARG;
  1604. }
  1605. *ppv = NULL;
  1606. if (WIN_IsEqualIID(riid, &IID_IUnknown) ||
  1607. WIN_IsEqualIID(riid, &IID_IDropTarget)) {
  1608. *ppv = (void *)target;
  1609. }
  1610. if (*ppv) {
  1611. SDLDropTarget_AddRef(target);
  1612. return S_OK;
  1613. }
  1614. return E_NOINTERFACE;
  1615. }
  1616. static STDMETHODIMP SDLDropTarget_DragEnter(SDLDropTarget *target,
  1617. IDataObject *pDataObject, DWORD grfKeyState,
  1618. POINTL pt, DWORD *pdwEffect)
  1619. {
  1620. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1621. ". In DragEnter at %ld, %ld", pt.x, pt.y);
  1622. *pdwEffect = DROPEFFECT_COPY;
  1623. POINT pnt = { pt.x, pt.y };
  1624. if (ScreenToClient(target->hwnd, &pnt)) {
  1625. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1626. ". In DragEnter at %ld, %ld => window %u at %ld, %ld", pt.x, pt.y, target->window->id, pnt.x, pnt.y);
  1627. SDL_SendDropPosition(target->window, pnt.x, pnt.y);
  1628. } else {
  1629. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1630. ". In DragEnter at %ld, %ld => nil, nil", pt.x, pt.y);
  1631. }
  1632. return S_OK;
  1633. }
  1634. static STDMETHODIMP SDLDropTarget_DragOver(SDLDropTarget *target,
  1635. DWORD grfKeyState,
  1636. POINTL pt, DWORD *pdwEffect)
  1637. {
  1638. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1639. ". In DragOver at %ld, %ld", pt.x, pt.y);
  1640. *pdwEffect = DROPEFFECT_COPY;
  1641. POINT pnt = { pt.x, pt.y };
  1642. if (ScreenToClient(target->hwnd, &pnt)) {
  1643. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1644. ". In DragOver at %ld, %ld => window %u at %ld, %ld", pt.x, pt.y, target->window->id, pnt.x, pnt.y);
  1645. SDL_SendDropPosition(target->window, pnt.x, pnt.y);
  1646. } else {
  1647. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1648. ". In DragOver at %ld, %ld => nil, nil", pt.x, pt.y);
  1649. }
  1650. return S_OK;
  1651. }
  1652. static STDMETHODIMP SDLDropTarget_DragLeave(SDLDropTarget *target)
  1653. {
  1654. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1655. ". In DragLeave");
  1656. SDL_SendDropComplete(target->window);
  1657. return S_OK;
  1658. }
  1659. static STDMETHODIMP SDLDropTarget_Drop(SDLDropTarget *target,
  1660. IDataObject *pDataObject, DWORD grfKeyState,
  1661. POINTL pt, DWORD *pdwEffect)
  1662. {
  1663. *pdwEffect = DROPEFFECT_COPY;
  1664. POINT pnt = { pt.x, pt.y };
  1665. if (ScreenToClient(target->hwnd, &pnt)) {
  1666. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1667. ". In Drop at %ld, %ld => window %u at %ld, %ld", pt.x, pt.y, target->window->id, pnt.x, pnt.y);
  1668. SDL_SendDropPosition(target->window, pnt.x, pnt.y);
  1669. } else {
  1670. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1671. ". In Drop at %ld, %ld => nil, nil", pt.x, pt.y);
  1672. }
  1673. {
  1674. IEnumFORMATETC *pEnumFormatEtc;
  1675. HRESULT hres;
  1676. hres = pDataObject->lpVtbl->EnumFormatEtc(pDataObject, DATADIR_GET, &pEnumFormatEtc);
  1677. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1678. ". In Drop for EnumFormatEtc, HRESULT is %08lx", hres);
  1679. if (hres == S_OK) {
  1680. FORMATETC fetc;
  1681. while (pEnumFormatEtc->lpVtbl->Next(pEnumFormatEtc, 1, &fetc, NULL) == S_OK) {
  1682. char name[257] = { 0 };
  1683. const char *cfnm = SDLGetClipboardFormatName(fetc.cfFormat, name, 256);
  1684. if (cfnm) {
  1685. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1686. ". In Drop, Supported format is %08x, '%s'", fetc.cfFormat, cfnm);
  1687. } else {
  1688. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1689. ". In Drop, Supported format is %08x, Predefined", fetc.cfFormat);
  1690. }
  1691. }
  1692. }
  1693. }
  1694. {
  1695. FORMATETC fetc;
  1696. fetc.cfFormat = target->format_file;
  1697. fetc.ptd = NULL;
  1698. fetc.dwAspect = DVASPECT_CONTENT;
  1699. fetc.lindex = -1;
  1700. fetc.tymed = TYMED_HGLOBAL;
  1701. const char *format_mime = "text/uri-list";
  1702. if (SUCCEEDED(pDataObject->lpVtbl->QueryGetData(pDataObject, &fetc))) {
  1703. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1704. ". In Drop File for QueryGetData, format %08x '%s', success",
  1705. fetc.cfFormat, format_mime);
  1706. STGMEDIUM med;
  1707. HRESULT hres = pDataObject->lpVtbl->GetData(pDataObject, &fetc, &med);
  1708. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1709. ". In Drop File for GetData, format %08x '%s', HRESULT is %08lx",
  1710. fetc.cfFormat, format_mime, hres);
  1711. if (SUCCEEDED(hres)) {
  1712. const size_t bsize = GlobalSize(med.hGlobal);
  1713. const void *buffer = (void *)GlobalLock(med.hGlobal);
  1714. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1715. ". In Drop File for GlobalLock, format %08x '%s', memory (%lu) %p",
  1716. fetc.cfFormat, format_mime, (unsigned long)bsize, buffer);
  1717. if (buffer) {
  1718. char *text = (char *)SDL_malloc(bsize + sizeof(Uint32));
  1719. SDL_memcpy((Uint8 *)text, buffer, bsize);
  1720. SDL_memset((Uint8 *)text + bsize, 0, sizeof(Uint32));
  1721. char *saveptr = NULL;
  1722. char *token = SDL_strtok_r(text, "\r\n", &saveptr);
  1723. while (token != NULL) {
  1724. if (SDL_URIToLocal(token, token) >= 0) {
  1725. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1726. ". In Drop File, file (%lu of %lu) '%s'",
  1727. (unsigned long)SDL_strlen(token), (unsigned long)bsize, token);
  1728. SDL_SendDropFile(target->window, NULL, token);
  1729. }
  1730. token = SDL_strtok_r(NULL, "\r\n", &saveptr);
  1731. }
  1732. SDL_free(text);
  1733. }
  1734. GlobalUnlock(med.hGlobal);
  1735. ReleaseStgMedium(&med);
  1736. SDL_SendDropComplete(target->window);
  1737. return S_OK;
  1738. }
  1739. }
  1740. }
  1741. {
  1742. FORMATETC fetc;
  1743. fetc.cfFormat = target->format_text;
  1744. fetc.ptd = NULL;
  1745. fetc.dwAspect = DVASPECT_CONTENT;
  1746. fetc.lindex = -1;
  1747. fetc.tymed = TYMED_HGLOBAL;
  1748. const char *format_mime = "text/plain;charset=utf-8";
  1749. if (SUCCEEDED(pDataObject->lpVtbl->QueryGetData(pDataObject, &fetc))) {
  1750. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1751. ". In Drop Text for QueryGetData, format %08x '%s', success",
  1752. fetc.cfFormat, format_mime);
  1753. STGMEDIUM med;
  1754. HRESULT hres = pDataObject->lpVtbl->GetData(pDataObject, &fetc, &med);
  1755. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1756. ". In Drop Text for GetData, format %08x '%s', HRESULT is %08lx",
  1757. fetc.cfFormat, format_mime, hres);
  1758. if (SUCCEEDED(hres)) {
  1759. const size_t bsize = GlobalSize(med.hGlobal);
  1760. const void *buffer = (void *)GlobalLock(med.hGlobal);
  1761. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1762. ". In Drop Text for GlobalLock, format %08x '%s', memory (%lu) %p",
  1763. fetc.cfFormat, format_mime, (unsigned long)bsize, buffer);
  1764. if (buffer) {
  1765. char *text = (char *)SDL_malloc(bsize + sizeof(Uint32));
  1766. SDL_memcpy((Uint8 *)text, buffer, bsize);
  1767. SDL_memset((Uint8 *)text + bsize, 0, sizeof(Uint32));
  1768. char *saveptr = NULL;
  1769. char *token = SDL_strtok_r(text, "\r\n", &saveptr);
  1770. while (token != NULL) {
  1771. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1772. ". In Drop Text, text (%lu of %lu) '%s'",
  1773. (unsigned long)SDL_strlen(token), (unsigned long)bsize, token);
  1774. SDL_SendDropText(target->window, (char *)token);
  1775. token = SDL_strtok_r(NULL, "\r\n", &saveptr);
  1776. }
  1777. SDL_free(text);
  1778. }
  1779. GlobalUnlock(med.hGlobal);
  1780. ReleaseStgMedium(&med);
  1781. SDL_SendDropComplete(target->window);
  1782. return S_OK;
  1783. }
  1784. }
  1785. }
  1786. {
  1787. FORMATETC fetc;
  1788. fetc.cfFormat = CF_UNICODETEXT;
  1789. fetc.ptd = NULL;
  1790. fetc.dwAspect = DVASPECT_CONTENT;
  1791. fetc.lindex = -1;
  1792. fetc.tymed = TYMED_HGLOBAL;
  1793. const char *format_mime = "CF_UNICODETEXT";
  1794. if (SUCCEEDED(pDataObject->lpVtbl->QueryGetData(pDataObject, &fetc))) {
  1795. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1796. ". In Drop Text for QueryGetData, format %08x '%s', success",
  1797. fetc.cfFormat, format_mime);
  1798. STGMEDIUM med;
  1799. HRESULT hres = pDataObject->lpVtbl->GetData(pDataObject, &fetc, &med);
  1800. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1801. ". In Drop Text for GetData, format %08x '%s', HRESULT is %08lx",
  1802. fetc.cfFormat, format_mime, hres);
  1803. if (SUCCEEDED(hres)) {
  1804. const size_t bsize = GlobalSize(med.hGlobal);
  1805. const void *buffer = (void *)GlobalLock(med.hGlobal);
  1806. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1807. ". In Drop Text for GlobalLock, format %08x '%s', memory (%lu) %p",
  1808. fetc.cfFormat, format_mime, (unsigned long)bsize, buffer);
  1809. if (buffer) {
  1810. buffer = WIN_StringToUTF8((const wchar_t *)buffer);
  1811. if (buffer) {
  1812. const size_t lbuffer = SDL_strlen((const char *)buffer);
  1813. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1814. ". In Drop Text for StringToUTF8, format %08x '%s', memory (%lu) %p",
  1815. fetc.cfFormat, format_mime, (unsigned long)lbuffer, buffer);
  1816. char *text = (char *)SDL_malloc(lbuffer + sizeof(Uint32));
  1817. SDL_memcpy((Uint8 *)text, buffer, lbuffer);
  1818. SDL_memset((Uint8 *)text + lbuffer, 0, sizeof(Uint32));
  1819. char *saveptr = NULL;
  1820. char *token = SDL_strtok_r(text, "\r\n", &saveptr);
  1821. while (token != NULL) {
  1822. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1823. ". In Drop Text, text (%lu of %lu) '%s'",
  1824. (unsigned long)SDL_strlen(token), (unsigned long)lbuffer, token);
  1825. SDL_SendDropText(target->window, (char *)token);
  1826. token = SDL_strtok_r(NULL, "\r\n", &saveptr);
  1827. }
  1828. SDL_free(text);
  1829. SDL_free((void *)buffer);
  1830. }
  1831. }
  1832. GlobalUnlock(med.hGlobal);
  1833. ReleaseStgMedium(&med);
  1834. SDL_SendDropComplete(target->window);
  1835. return S_OK;
  1836. }
  1837. }
  1838. }
  1839. {
  1840. FORMATETC fetc;
  1841. fetc.cfFormat = CF_TEXT;
  1842. fetc.ptd = NULL;
  1843. fetc.dwAspect = DVASPECT_CONTENT;
  1844. fetc.lindex = -1;
  1845. fetc.tymed = TYMED_HGLOBAL;
  1846. const char *format_mime = "CF_TEXT";
  1847. if (SUCCEEDED(pDataObject->lpVtbl->QueryGetData(pDataObject, &fetc))) {
  1848. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1849. ". In Drop Text for QueryGetData, format %08x '%s', success",
  1850. fetc.cfFormat, format_mime);
  1851. STGMEDIUM med;
  1852. HRESULT hres = pDataObject->lpVtbl->GetData(pDataObject, &fetc, &med);
  1853. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1854. ". In Drop Text for GetData, format %08x '%s', HRESULT is %08lx",
  1855. fetc.cfFormat, format_mime, hres);
  1856. if (SUCCEEDED(hres)) {
  1857. const size_t bsize = GlobalSize(med.hGlobal);
  1858. const void *buffer = (void *)GlobalLock(med.hGlobal);
  1859. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1860. ". In Drop Text for GlobalLock, format %08x '%s', memory (%lu) %p",
  1861. fetc.cfFormat, format_mime, (unsigned long)bsize, buffer);
  1862. if (buffer) {
  1863. char *text = (char *)SDL_malloc(bsize + sizeof(Uint32));
  1864. SDL_memcpy((Uint8 *)text, buffer, bsize);
  1865. SDL_memset((Uint8 *)text + bsize, 0, sizeof(Uint32));
  1866. char *saveptr = NULL;
  1867. char *token = SDL_strtok_r(text, "\r\n", &saveptr);
  1868. while (token != NULL) {
  1869. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1870. ". In Drop Text, text (%lu of %lu) '%s'",
  1871. (unsigned long)SDL_strlen(token), (unsigned long)bsize, token);
  1872. SDL_SendDropText(target->window, (char *)token);
  1873. token = SDL_strtok_r(NULL, "\r\n", &saveptr);
  1874. }
  1875. SDL_free(text);
  1876. }
  1877. GlobalUnlock(med.hGlobal);
  1878. ReleaseStgMedium(&med);
  1879. SDL_SendDropComplete(target->window);
  1880. return S_OK;
  1881. }
  1882. }
  1883. }
  1884. {
  1885. FORMATETC fetc;
  1886. fetc.cfFormat = CF_HDROP;
  1887. fetc.ptd = NULL;
  1888. fetc.dwAspect = DVASPECT_CONTENT;
  1889. fetc.lindex = -1;
  1890. fetc.tymed = TYMED_HGLOBAL;
  1891. const char *format_mime = "CF_HDROP";
  1892. if (SUCCEEDED(pDataObject->lpVtbl->QueryGetData(pDataObject, &fetc))) {
  1893. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1894. ". In Drop File for QueryGetData, format %08x '%s', success",
  1895. fetc.cfFormat, format_mime);
  1896. STGMEDIUM med;
  1897. HRESULT hres = pDataObject->lpVtbl->GetData(pDataObject, &fetc, &med);
  1898. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1899. ". In Drop File for GetData, format %08x '%s', HRESULT is %08lx",
  1900. fetc.cfFormat, format_mime, hres);
  1901. if (SUCCEEDED(hres)) {
  1902. const size_t bsize = GlobalSize(med.hGlobal);
  1903. HDROP drop = (HDROP)GlobalLock(med.hGlobal);
  1904. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1905. ". In Drop File for GlobalLock, format %08x '%s', memory (%lu) %p",
  1906. fetc.cfFormat, format_mime, (unsigned long)bsize, drop);
  1907. UINT count = DragQueryFile(drop, 0xFFFFFFFF, NULL, 0);
  1908. for (UINT i = 0; i < count; ++i) {
  1909. UINT size = DragQueryFile(drop, i, NULL, 0) + 1;
  1910. LPTSTR buffer = (LPTSTR)SDL_malloc(size * sizeof(TCHAR));
  1911. if (buffer) {
  1912. if (DragQueryFile(drop, i, buffer, size)) {
  1913. char *file = WIN_StringToUTF8(buffer);
  1914. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1915. ". In Drop File, file (%lu of %lu) '%s'",
  1916. (unsigned long)SDL_strlen(file), (unsigned long)bsize, file);
  1917. SDL_SendDropFile(target->window, NULL, file);
  1918. SDL_free(file);
  1919. }
  1920. SDL_free(buffer);
  1921. }
  1922. }
  1923. GlobalUnlock(med.hGlobal);
  1924. ReleaseStgMedium(&med);
  1925. SDL_SendDropComplete(target->window);
  1926. return S_OK;
  1927. }
  1928. }
  1929. }
  1930. SDL_SendDropComplete(target->window);
  1931. return S_OK;
  1932. }
  1933. static void *vtDropTarget[] = {
  1934. (void *)(SDLDropTarget_QueryInterface),
  1935. (void *)(SDLDropTarget_AddRef),
  1936. (void *)(SDLDropTarget_Release),
  1937. (void *)(SDLDropTarget_DragEnter),
  1938. (void *)(SDLDropTarget_DragOver),
  1939. (void *)(SDLDropTarget_DragLeave),
  1940. (void *)(SDLDropTarget_Drop)
  1941. };
  1942. void WIN_AcceptDragAndDrop(SDL_Window *window, bool accept)
  1943. {
  1944. SDL_WindowData *data = window->internal;
  1945. if (data->videodata->oleinitialized) {
  1946. if (accept && !data->drop_target) {
  1947. SDLDropTarget *drop_target = (SDLDropTarget *)SDL_calloc(1, sizeof(SDLDropTarget));
  1948. if (drop_target != NULL) {
  1949. drop_target->lpVtbl = vtDropTarget;
  1950. drop_target->window = window;
  1951. drop_target->hwnd = data->hwnd;
  1952. drop_target->format_file = RegisterClipboardFormat(L"text/uri-list");
  1953. drop_target->format_text = RegisterClipboardFormat(L"text/plain;charset=utf-8");
  1954. data->drop_target = drop_target;
  1955. SDLDropTarget_AddRef(drop_target);
  1956. RegisterDragDrop(data->hwnd, (LPDROPTARGET)drop_target);
  1957. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1958. ". In Accept Drag and Drop, window %u, enabled Full OLE IDropTarget",
  1959. window->id);
  1960. }
  1961. } else if (!accept && data->drop_target) {
  1962. RevokeDragDrop(data->hwnd);
  1963. SDLDropTarget_Release(data->drop_target);
  1964. data->drop_target = NULL;
  1965. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1966. ". In Accept Drag and Drop, window %u, disabled Full OLE IDropTarget",
  1967. window->id);
  1968. }
  1969. } else {
  1970. DragAcceptFiles(data->hwnd, accept ? TRUE : FALSE);
  1971. SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
  1972. ". In Accept Drag and Drop, window %u, %s Fallback WM_DROPFILES",
  1973. window->id, (accept ? "enabled" : "disabled"));
  1974. }
  1975. }
  1976. bool WIN_FlashWindow(SDL_VideoDevice *_this, SDL_Window *window, SDL_FlashOperation operation)
  1977. {
  1978. FLASHWINFO desc;
  1979. SDL_zero(desc);
  1980. desc.cbSize = sizeof(desc);
  1981. desc.hwnd = window->internal->hwnd;
  1982. switch (operation) {
  1983. case SDL_FLASH_CANCEL:
  1984. desc.dwFlags = FLASHW_STOP;
  1985. break;
  1986. case SDL_FLASH_BRIEFLY:
  1987. desc.dwFlags = FLASHW_TRAY;
  1988. desc.uCount = 1;
  1989. break;
  1990. case SDL_FLASH_UNTIL_FOCUSED:
  1991. desc.dwFlags = (FLASHW_TRAY | FLASHW_TIMERNOFG);
  1992. break;
  1993. default:
  1994. return SDL_Unsupported();
  1995. }
  1996. FlashWindowEx(&desc);
  1997. return true;
  1998. }
  1999. bool WIN_ApplyWindowProgress(SDL_VideoDevice *_this, SDL_Window* window)
  2000. {
  2001. #ifdef HAVE_SHOBJIDL_CORE_H
  2002. SDL_WindowData *data = window->internal;
  2003. if (!data->taskbar_button_created) {
  2004. return true;
  2005. }
  2006. if (window->progress_state == SDL_PROGRESS_STATE_NONE && !data->videodata->taskbar_list) {
  2007. return true;
  2008. }
  2009. ITaskbarList3 *taskbar_list = GetTaskbarList(window);
  2010. if (!taskbar_list) {
  2011. return false;
  2012. }
  2013. TBPFLAG tbpFlags;
  2014. switch (window->progress_state) {
  2015. case SDL_PROGRESS_STATE_NONE:
  2016. tbpFlags = TBPF_NOPROGRESS;
  2017. break;
  2018. case SDL_PROGRESS_STATE_INDETERMINATE:
  2019. tbpFlags = TBPF_INDETERMINATE;
  2020. break;
  2021. case SDL_PROGRESS_STATE_NORMAL:
  2022. tbpFlags = TBPF_NORMAL;
  2023. break;
  2024. case SDL_PROGRESS_STATE_PAUSED:
  2025. tbpFlags = TBPF_PAUSED;
  2026. break;
  2027. case SDL_PROGRESS_STATE_ERROR:
  2028. tbpFlags = TBPF_ERROR;
  2029. break;
  2030. default:
  2031. return SDL_SetError("Parameter 'state' is not supported");
  2032. }
  2033. HRESULT ret = taskbar_list->lpVtbl->SetProgressState(taskbar_list, data->hwnd, tbpFlags);
  2034. if (FAILED(ret)) {
  2035. return WIN_SetErrorFromHRESULT("ITaskbarList3::SetProgressState()", ret);
  2036. }
  2037. if (window->progress_state >= SDL_PROGRESS_STATE_NORMAL) {
  2038. ret = taskbar_list->lpVtbl->SetProgressValue(taskbar_list, data->hwnd, (ULONGLONG)(window->progress_value * 10000.f), 10000);
  2039. if (FAILED(ret)) {
  2040. return WIN_SetErrorFromHRESULT("ITaskbarList3::SetProgressValue()", ret);
  2041. }
  2042. }
  2043. #endif
  2044. return true;
  2045. }
  2046. void WIN_ShowWindowSystemMenu(SDL_Window *window, int x, int y)
  2047. {
  2048. const SDL_WindowData *data = window->internal;
  2049. POINT pt;
  2050. pt.x = x;
  2051. pt.y = y;
  2052. ClientToScreen(data->hwnd, &pt);
  2053. SendMessage(data->hwnd, WM_POPUPSYSTEMMENU, 0, MAKELPARAM(pt.x, pt.y));
  2054. }
  2055. bool WIN_SetWindowFocusable(SDL_VideoDevice *_this, SDL_Window *window, bool focusable)
  2056. {
  2057. SDL_WindowData *data = window->internal;
  2058. HWND hwnd = data->hwnd;
  2059. const LONG style = GetWindowLong(hwnd, GWL_EXSTYLE);
  2060. SDL_assert(style != 0);
  2061. if (focusable) {
  2062. if (style & WS_EX_NOACTIVATE) {
  2063. if (SetWindowLong(hwnd, GWL_EXSTYLE, style & ~WS_EX_NOACTIVATE) == 0) {
  2064. return WIN_SetError("SetWindowLong()");
  2065. }
  2066. }
  2067. } else {
  2068. if (!(style & WS_EX_NOACTIVATE)) {
  2069. if (SetWindowLong(hwnd, GWL_EXSTYLE, style | WS_EX_NOACTIVATE) == 0) {
  2070. return WIN_SetError("SetWindowLong()");
  2071. }
  2072. }
  2073. }
  2074. return true;
  2075. }
  2076. #endif // !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  2077. void WIN_UpdateDarkModeForHWND(HWND hwnd)
  2078. {
  2079. #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  2080. SDL_SharedObject *ntdll = SDL_LoadObject("ntdll.dll");
  2081. if (!ntdll) {
  2082. return;
  2083. }
  2084. // There is no function to get Windows build number, so let's get it here via RtlGetVersion
  2085. RtlGetVersion_t RtlGetVersionFunc = (RtlGetVersion_t)SDL_LoadFunction(ntdll, "RtlGetVersion");
  2086. NT_OSVERSIONINFOW os_info;
  2087. os_info.dwOSVersionInfoSize = sizeof(NT_OSVERSIONINFOW);
  2088. os_info.dwBuildNumber = 0;
  2089. if (RtlGetVersionFunc) {
  2090. RtlGetVersionFunc(&os_info);
  2091. }
  2092. SDL_UnloadObject(ntdll);
  2093. os_info.dwBuildNumber &= ~0xF0000000;
  2094. if (os_info.dwBuildNumber < 17763) {
  2095. // Too old to support dark mode
  2096. return;
  2097. }
  2098. SDL_SharedObject *uxtheme = SDL_LoadObject("uxtheme.dll");
  2099. if (!uxtheme) {
  2100. return;
  2101. }
  2102. RefreshImmersiveColorPolicyState_t RefreshImmersiveColorPolicyStateFunc = (RefreshImmersiveColorPolicyState_t)SDL_LoadFunction(uxtheme, MAKEINTRESOURCEA(104));
  2103. ShouldAppsUseDarkMode_t ShouldAppsUseDarkModeFunc = (ShouldAppsUseDarkMode_t)SDL_LoadFunction(uxtheme, MAKEINTRESOURCEA(132));
  2104. AllowDarkModeForWindow_t AllowDarkModeForWindowFunc = (AllowDarkModeForWindow_t)SDL_LoadFunction(uxtheme, MAKEINTRESOURCEA(133));
  2105. if (os_info.dwBuildNumber < 18362) {
  2106. AllowDarkModeForApp_t AllowDarkModeForAppFunc = (AllowDarkModeForApp_t)SDL_LoadFunction(uxtheme, MAKEINTRESOURCEA(135));
  2107. if (AllowDarkModeForAppFunc) {
  2108. AllowDarkModeForAppFunc(true);
  2109. }
  2110. } else {
  2111. SetPreferredAppMode_t SetPreferredAppModeFunc = (SetPreferredAppMode_t)SDL_LoadFunction(uxtheme, MAKEINTRESOURCEA(135));
  2112. if (SetPreferredAppModeFunc) {
  2113. SetPreferredAppModeFunc(UXTHEME_APPMODE_ALLOW_DARK);
  2114. }
  2115. }
  2116. if (RefreshImmersiveColorPolicyStateFunc) {
  2117. RefreshImmersiveColorPolicyStateFunc();
  2118. }
  2119. if (AllowDarkModeForWindowFunc) {
  2120. AllowDarkModeForWindowFunc(hwnd, true);
  2121. }
  2122. BOOL value;
  2123. // Check dark mode using ShouldAppsUseDarkMode, but use SDL_GetSystemTheme as a fallback
  2124. if (ShouldAppsUseDarkModeFunc) {
  2125. value = ShouldAppsUseDarkModeFunc() ? TRUE : FALSE;
  2126. } else {
  2127. value = (SDL_GetSystemTheme() == SDL_SYSTEM_THEME_DARK) ? TRUE : FALSE;
  2128. }
  2129. SDL_UnloadObject(uxtheme);
  2130. if (os_info.dwBuildNumber < 18362) {
  2131. SetProp(hwnd, TEXT("UseImmersiveDarkModeColors"), SDL_reinterpret_cast(HANDLE, SDL_static_cast(INT_PTR, value)));
  2132. } else {
  2133. HMODULE user32 = GetModuleHandle(TEXT("user32.dll"));
  2134. if (user32) {
  2135. SetWindowCompositionAttribute_t SetWindowCompositionAttributeFunc = (SetWindowCompositionAttribute_t)GetProcAddress(user32, "SetWindowCompositionAttribute");
  2136. if (SetWindowCompositionAttributeFunc) {
  2137. WINDOWCOMPOSITIONATTRIBDATA data = { WCA_USEDARKMODECOLORS, &value, sizeof(value) };
  2138. SetWindowCompositionAttributeFunc(hwnd, &data);
  2139. }
  2140. }
  2141. }
  2142. #endif
  2143. }
  2144. bool WIN_SetWindowParent(SDL_VideoDevice *_this, SDL_Window *window, SDL_Window *parent)
  2145. {
  2146. #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  2147. SDL_WindowData *child_data = window->internal;
  2148. const LONG_PTR parent_hwnd = (LONG_PTR)(parent ? parent->internal->hwnd : NULL);
  2149. const DWORD style = GetWindowLong(child_data->hwnd, GWL_STYLE);
  2150. if (!(style & WS_CHILD)) {
  2151. /* Despite the name, this changes the *owner* of a toplevel window, not
  2152. * the parent of a child window.
  2153. *
  2154. * https://devblogs.microsoft.com/oldnewthing/20100315-00/?p=14613
  2155. */
  2156. SetWindowLongPtr(child_data->hwnd, GWLP_HWNDPARENT, parent_hwnd);
  2157. } else {
  2158. SetParent(child_data->hwnd, (HWND)parent_hwnd);
  2159. }
  2160. #endif /*!defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)*/
  2161. return true;
  2162. }
  2163. bool WIN_SetWindowModal(SDL_VideoDevice *_this, SDL_Window *window, bool modal)
  2164. {
  2165. #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  2166. const HWND parent_hwnd = window->parent->internal->hwnd;
  2167. if (modal) {
  2168. // Disable the parent window.
  2169. EnableWindow(parent_hwnd, FALSE);
  2170. } else if (!(window->flags & SDL_WINDOW_HIDDEN)) {
  2171. // Re-enable the parent window
  2172. EnableWindow(parent_hwnd, TRUE);
  2173. }
  2174. #endif // !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  2175. return true;
  2176. }
  2177. #endif // SDL_VIDEO_DRIVER_WINDOWS