SDL_windowswindow.c 87 KB

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