SDL_windowswindow.c 88 KB

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