SDL_windowswindow.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #if SDL_VIDEO_DRIVER_WINDOWS
  20. #include "../../core/windows/SDL_windows.h"
  21. #include "../SDL_sysvideo.h"
  22. #include "../SDL_pixels_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_hints_c.h"
  27. #include "SDL_windowsvideo.h"
  28. #include "SDL_windowswindow.h"
  29. #include "SDL_windowsshape.h"
  30. /* Dropfile support */
  31. #include <shellapi.h>
  32. #include <SDL3/SDL_syswm.h>
  33. /* Windows CE compatibility */
  34. #ifndef SWP_NOCOPYBITS
  35. #define SWP_NOCOPYBITS 0
  36. #endif
  37. /* #define HIGHDPI_DEBUG */
  38. /* Fake window to help with DirectInput events. */
  39. HWND SDL_HelperWindow = NULL;
  40. static const TCHAR *SDL_HelperWindowClassName = TEXT("SDLHelperWindowInputCatcher");
  41. static const TCHAR *SDL_HelperWindowName = TEXT("SDLHelperWindowInputMsgWindow");
  42. static ATOM SDL_HelperWindowClass = 0;
  43. /* For borderless Windows, still want the following flag:
  44. - WS_MINIMIZEBOX: window will respond to Windows minimize commands sent to all windows, such as windows key + m, shaking title bar, etc.
  45. Additionally, non-fullscreen windows can add:
  46. - WS_CAPTION: this seems to enable the Windows minimize animation
  47. - WS_SYSMENU: enables system context menu on task bar
  48. 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
  49. */
  50. #define STYLE_BASIC (WS_CLIPSIBLINGS | WS_CLIPCHILDREN)
  51. #define STYLE_FULLSCREEN (WS_POPUP | WS_MINIMIZEBOX)
  52. #define STYLE_BORDERLESS (WS_POPUP | WS_MINIMIZEBOX)
  53. #define STYLE_BORDERLESS_WINDOWED (WS_POPUP | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX)
  54. #define STYLE_NORMAL (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX)
  55. #define STYLE_RESIZABLE (WS_THICKFRAME | WS_MAXIMIZEBOX)
  56. #define STYLE_MASK (STYLE_FULLSCREEN | STYLE_BORDERLESS | STYLE_NORMAL | STYLE_RESIZABLE)
  57. static DWORD GetWindowStyle(SDL_Window *window)
  58. {
  59. DWORD style = 0;
  60. if (window->flags & SDL_WINDOW_FULLSCREEN) {
  61. style |= STYLE_FULLSCREEN;
  62. } else {
  63. if (window->flags & SDL_WINDOW_BORDERLESS) {
  64. style |= STYLE_BORDERLESS_WINDOWED;
  65. } else {
  66. style |= STYLE_NORMAL;
  67. }
  68. if (window->flags & SDL_WINDOW_RESIZABLE) {
  69. /* You can have a borderless resizable window, but Windows doesn't always draw it correctly,
  70. see https://bugzilla.libsdl.org/show_bug.cgi?id=4466
  71. */
  72. if (!(window->flags & SDL_WINDOW_BORDERLESS) ||
  73. SDL_GetHintBoolean("SDL_BORDERLESS_RESIZABLE_STYLE", SDL_FALSE)) {
  74. style |= STYLE_RESIZABLE;
  75. }
  76. }
  77. /* Need to set initialize minimize style, or when we call ShowWindow with WS_MINIMIZE it will activate a random window */
  78. if (window->flags & SDL_WINDOW_MINIMIZED) {
  79. style |= WS_MINIMIZE;
  80. }
  81. }
  82. return style;
  83. }
  84. /**
  85. * Returns arguments to pass to SetWindowPos - the window rect, including frame, in Windows coordinates.
  86. * Can be called before we have a HWND.
  87. */
  88. static void WIN_AdjustWindowRectWithStyle(SDL_Window *window, DWORD style, BOOL menu, int *x, int *y, int *width, int *height, SDL_bool use_current)
  89. {
  90. SDL_VideoData *videodata = SDL_GetVideoDevice() ? SDL_GetVideoDevice()->driverdata : NULL;
  91. RECT rect;
  92. int dpi = 96;
  93. #if !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
  94. UINT frame_dpi;
  95. #endif
  96. /* Client rect, in SDL screen coordinates */
  97. *x = (use_current ? window->x : window->windowed.x);
  98. *y = (use_current ? window->y : window->windowed.y);
  99. *width = (use_current ? window->w : window->windowed.w);
  100. *height = (use_current ? window->h : window->windowed.h);
  101. /* Convert client rect from SDL coordinates to pixels (no-op if DPI scaling not enabled) */
  102. #if !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
  103. WIN_ScreenPointFromSDL(x, y, &dpi);
  104. #endif
  105. /* Note, use the guessed DPI returned from WIN_ScreenPointFromSDL rather than the cached one in
  106. data->scaling_dpi.
  107. - This is called before the window is created, so we can't rely on data->scaling_dpi
  108. - Bug workaround: when leaving exclusive fullscreen, the cached DPI and window DPI reported
  109. by GetDpiForWindow will be wrong, and would cause windows shrinking slightly when
  110. going from exclusive fullscreen to windowed on a HighDPI monitor with scaling if we used them.
  111. */
  112. *width = MulDiv(*width, dpi, 96);
  113. *height = MulDiv(*height, dpi, 96);
  114. /* Copy the client size in pixels into this rect structure,
  115. which we'll then adjust with AdjustWindowRectEx */
  116. rect.left = 0;
  117. rect.top = 0;
  118. rect.right = *width;
  119. rect.bottom = *height;
  120. /* 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
  121. expanding the window client area to the previous window + chrome size, so shouldn't need to adjust the window size for the set styles.
  122. */
  123. if (!(window->flags & SDL_WINDOW_BORDERLESS)) {
  124. #if defined(__XBOXONE__) || defined(__XBOXSERIES__)
  125. AdjustWindowRectEx(&rect, style, menu, 0);
  126. #else
  127. if (WIN_IsPerMonitorV2DPIAware(SDL_GetVideoDevice())) {
  128. /* With per-monitor v2, the window border/titlebar size depend on the DPI, so we need to call AdjustWindowRectExForDpi instead of
  129. AdjustWindowRectEx. */
  130. UINT unused;
  131. RECT screen_rect;
  132. HMONITOR mon;
  133. screen_rect.left = *x;
  134. screen_rect.top = *y;
  135. screen_rect.right = (LONG)*x + *width;
  136. screen_rect.bottom = (LONG)*y + *height;
  137. mon = MonitorFromRect(&screen_rect, MONITOR_DEFAULTTONEAREST);
  138. if (videodata != NULL) {
  139. /* GetDpiForMonitor docs promise to return the same hdpi / vdpi */
  140. if (videodata->GetDpiForMonitor(mon, MDT_EFFECTIVE_DPI, &frame_dpi, &unused) != S_OK) {
  141. frame_dpi = 96;
  142. }
  143. videodata->AdjustWindowRectExForDpi(&rect, style, menu, 0, frame_dpi);
  144. }
  145. } else {
  146. AdjustWindowRectEx(&rect, style, menu, 0);
  147. }
  148. #endif
  149. }
  150. /* Final rect in Windows screen space, including the frame */
  151. *x += rect.left;
  152. *y += rect.top;
  153. *width = (rect.right - rect.left);
  154. *height = (rect.bottom - rect.top);
  155. #ifdef HIGHDPI_DEBUG
  156. SDL_Log("WIN_AdjustWindowRectWithStyle: in: %d, %d, %dx%d, returning: %d, %d, %dx%d, used dpi %d for frame calculation",
  157. (use_current ? window->x : window->windowed.x),
  158. (use_current ? window->y : window->windowed.y),
  159. (use_current ? window->w : window->windowed.w),
  160. (use_current ? window->h : window->windowed.h),
  161. *x, *y, *width, *height, frame_dpi);
  162. #endif
  163. }
  164. static void WIN_AdjustWindowRect(SDL_Window *window, int *x, int *y, int *width, int *height, SDL_bool use_current)
  165. {
  166. SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
  167. HWND hwnd = data->hwnd;
  168. DWORD style;
  169. BOOL menu;
  170. style = GetWindowLong(hwnd, GWL_STYLE);
  171. #if defined(__XBOXONE__) || defined(__XBOXSERIES__)
  172. menu = FALSE;
  173. #else
  174. menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
  175. #endif
  176. WIN_AdjustWindowRectWithStyle(window, style, menu, x, y, width, height, use_current);
  177. }
  178. static void WIN_SetWindowPositionInternal(_THIS, SDL_Window *window, UINT flags)
  179. {
  180. SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
  181. HWND hwnd = data->hwnd;
  182. HWND top;
  183. int x, y;
  184. int w, h;
  185. /* Figure out what the window area will be */
  186. if (SDL_ShouldAllowTopmost() && (window->flags & SDL_WINDOW_ALWAYS_ON_TOP)) {
  187. top = HWND_TOPMOST;
  188. } else {
  189. top = HWND_NOTOPMOST;
  190. }
  191. WIN_AdjustWindowRect(window, &x, &y, &w, &h, SDL_TRUE);
  192. data->expected_resize = SDL_TRUE;
  193. SetWindowPos(hwnd, top, x, y, w, h, flags);
  194. data->expected_resize = SDL_FALSE;
  195. }
  196. static void SDLCALL WIN_MouseRelativeModeCenterChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  197. {
  198. SDL_WindowData *data = (SDL_WindowData *)userdata;
  199. data->mouse_relative_mode_center = SDL_GetStringBoolean(hint, SDL_TRUE);
  200. }
  201. static int WIN_GetScalingDPIForHWND(const SDL_VideoData *videodata, HWND hwnd)
  202. {
  203. #if defined(__XBOXONE__) || defined(__XBOXSERIES__)
  204. return 96;
  205. #else
  206. /* DPI scaling not requested? */
  207. if (!videodata->dpi_scaling_enabled) {
  208. return 96;
  209. }
  210. /* Window 10+ */
  211. if (videodata->GetDpiForWindow) {
  212. return videodata->GetDpiForWindow(hwnd);
  213. }
  214. /* Window 8.1+ */
  215. if (videodata->GetDpiForMonitor) {
  216. HMONITOR monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
  217. if (monitor) {
  218. UINT dpi_uint, unused;
  219. if (S_OK == videodata->GetDpiForMonitor(monitor, MDT_EFFECTIVE_DPI, &dpi_uint, &unused)) {
  220. return (int)dpi_uint;
  221. }
  222. }
  223. return 96;
  224. }
  225. /* Windows Vista-8.0 */
  226. {
  227. HDC hdc = GetDC(NULL);
  228. if (hdc) {
  229. int dpi = GetDeviceCaps(hdc, LOGPIXELSX);
  230. ReleaseDC(NULL, hdc);
  231. return dpi;
  232. }
  233. return 96;
  234. }
  235. #endif
  236. }
  237. static int SetupWindowData(_THIS, SDL_Window *window, HWND hwnd, HWND parent, SDL_bool created)
  238. {
  239. SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
  240. SDL_WindowData *data;
  241. /* Allocate the window data */
  242. data = (SDL_WindowData *)SDL_calloc(1, sizeof(*data));
  243. if (data == NULL) {
  244. return SDL_OutOfMemory();
  245. }
  246. data->window = window;
  247. data->hwnd = hwnd;
  248. data->parent = parent;
  249. #if defined(__XBOXONE__) || defined(__XBOXSERIES__)
  250. data->hdc = (HDC) data->hwnd;
  251. #else
  252. data->hdc = GetDC(hwnd);
  253. #endif
  254. data->hinstance = (HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE);
  255. data->created = created;
  256. data->high_surrogate = 0;
  257. data->mouse_button_flags = (WPARAM)-1;
  258. data->last_pointer_update = (LPARAM)-1;
  259. data->videodata = videodata;
  260. data->initializing = SDL_TRUE;
  261. data->scaling_dpi = WIN_GetScalingDPIForHWND(videodata, hwnd);
  262. #ifdef HIGHDPI_DEBUG
  263. SDL_Log("SetupWindowData: initialized data->scaling_dpi to %d", data->scaling_dpi);
  264. #endif
  265. SDL_AddHintCallback(SDL_HINT_MOUSE_RELATIVE_MODE_CENTER, WIN_MouseRelativeModeCenterChanged, data);
  266. window->driverdata = data;
  267. #if !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
  268. /* Associate the data with the window */
  269. if (!SetProp(hwnd, TEXT("SDL_WindowData"), data)) {
  270. ReleaseDC(hwnd, data->hdc);
  271. SDL_free(data);
  272. return WIN_SetError("SetProp() failed");
  273. }
  274. #endif
  275. /* Set up the window proc function */
  276. #ifdef GWLP_WNDPROC
  277. data->wndproc = (WNDPROC)GetWindowLongPtr(hwnd, GWLP_WNDPROC);
  278. if (data->wndproc == WIN_WindowProc) {
  279. data->wndproc = NULL;
  280. } else {
  281. SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)WIN_WindowProc);
  282. }
  283. #else
  284. data->wndproc = (WNDPROC)GetWindowLong(hwnd, GWL_WNDPROC);
  285. if (data->wndproc == WIN_WindowProc) {
  286. data->wndproc = NULL;
  287. } else {
  288. SetWindowLong(hwnd, GWL_WNDPROC, (LONG_PTR)WIN_WindowProc);
  289. }
  290. #endif
  291. /* Fill in the SDL window with the window data */
  292. {
  293. RECT rect;
  294. if (GetClientRect(hwnd, &rect)) {
  295. int w = rect.right;
  296. int h = rect.bottom;
  297. WIN_ClientPointToSDL(window, &w, &h);
  298. if ((window->windowed.w && window->windowed.w != w) || (window->windowed.h && window->windowed.h != h)) {
  299. /* We tried to create a window larger than the desktop and Windows didn't allow it. Override! */
  300. int x, y;
  301. /* Figure out what the window area will be */
  302. WIN_AdjustWindowRect(window, &x, &y, &w, &h, SDL_FALSE);
  303. data->expected_resize = SDL_TRUE;
  304. SetWindowPos(hwnd, HWND_NOTOPMOST, x, y, w, h, SWP_NOCOPYBITS | SWP_NOZORDER | SWP_NOACTIVATE);
  305. data->expected_resize = SDL_FALSE;
  306. } else {
  307. window->w = w;
  308. window->h = h;
  309. }
  310. }
  311. }
  312. #if !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
  313. {
  314. POINT point;
  315. point.x = 0;
  316. point.y = 0;
  317. if (ClientToScreen(hwnd, &point)) {
  318. int x = point.x;
  319. int y = point.y;
  320. WIN_ScreenPointToSDL(&x, &y);
  321. window->x = x;
  322. window->y = y;
  323. }
  324. }
  325. WIN_UpdateWindowICCProfile(window, SDL_FALSE);
  326. #endif
  327. {
  328. DWORD style = GetWindowLong(hwnd, GWL_STYLE);
  329. if (style & WS_VISIBLE) {
  330. window->flags &= ~SDL_WINDOW_HIDDEN;
  331. } else {
  332. window->flags |= SDL_WINDOW_HIDDEN;
  333. }
  334. if (style & WS_POPUP) {
  335. window->flags |= SDL_WINDOW_BORDERLESS;
  336. } else {
  337. window->flags &= ~SDL_WINDOW_BORDERLESS;
  338. }
  339. if (style & WS_THICKFRAME) {
  340. window->flags |= SDL_WINDOW_RESIZABLE;
  341. } else {
  342. window->flags &= ~SDL_WINDOW_RESIZABLE;
  343. }
  344. #ifdef WS_MAXIMIZE
  345. if (style & WS_MAXIMIZE) {
  346. window->flags |= SDL_WINDOW_MAXIMIZED;
  347. } else
  348. #endif
  349. {
  350. window->flags &= ~SDL_WINDOW_MAXIMIZED;
  351. }
  352. #ifdef WS_MINIMIZE
  353. if (style & WS_MINIMIZE) {
  354. window->flags |= SDL_WINDOW_MINIMIZED;
  355. } else
  356. #endif
  357. {
  358. window->flags &= ~SDL_WINDOW_MINIMIZED;
  359. }
  360. }
  361. #if defined(__XBOXONE__) || defined(__XBOXSERIES__)
  362. window->flags |= SDL_WINDOW_INPUT_FOCUS;
  363. #else
  364. if (GetFocus() == hwnd) {
  365. window->flags |= SDL_WINDOW_INPUT_FOCUS;
  366. SDL_SetKeyboardFocus(window);
  367. WIN_UpdateClipCursor(window);
  368. }
  369. #endif
  370. #if !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
  371. /* Enable multi-touch */
  372. if (videodata->RegisterTouchWindow) {
  373. videodata->RegisterTouchWindow(hwnd, (TWF_FINETOUCH | TWF_WANTPALM));
  374. }
  375. #endif
  376. /* Force the SDL_WINDOW_ALLOW_HIGHDPI window flag if we are doing DPI scaling */
  377. if (videodata->dpi_scaling_enabled) {
  378. window->flags |= SDL_WINDOW_ALLOW_HIGHDPI;
  379. }
  380. data->initializing = SDL_FALSE;
  381. /* All done! */
  382. return 0;
  383. }
  384. static void CleanupWindowData(_THIS, SDL_Window *window)
  385. {
  386. SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
  387. if (data) {
  388. SDL_DelHintCallback(SDL_HINT_MOUSE_RELATIVE_MODE_CENTER, WIN_MouseRelativeModeCenterChanged, data);
  389. #if !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
  390. if (data->ICMFileName) {
  391. SDL_free(data->ICMFileName);
  392. }
  393. if (data->keyboard_hook) {
  394. UnhookWindowsHookEx(data->keyboard_hook);
  395. }
  396. ReleaseDC(data->hwnd, data->hdc);
  397. RemoveProp(data->hwnd, TEXT("SDL_WindowData"));
  398. #endif
  399. if (data->created) {
  400. DestroyWindow(data->hwnd);
  401. if (data->parent) {
  402. DestroyWindow(data->parent);
  403. }
  404. } else {
  405. /* Restore any original event handler... */
  406. if (data->wndproc != NULL) {
  407. #ifdef GWLP_WNDPROC
  408. SetWindowLongPtr(data->hwnd, GWLP_WNDPROC,
  409. (LONG_PTR)data->wndproc);
  410. #else
  411. SetWindowLong(data->hwnd, GWL_WNDPROC,
  412. (LONG_PTR)data->wndproc);
  413. #endif
  414. }
  415. }
  416. SDL_free(data);
  417. }
  418. window->driverdata = NULL;
  419. }
  420. int WIN_CreateWindow(_THIS, SDL_Window *window)
  421. {
  422. HWND hwnd, parent = NULL;
  423. DWORD style = STYLE_BASIC;
  424. int x, y;
  425. int w, h;
  426. if (window->flags & SDL_WINDOW_SKIP_TASKBAR) {
  427. parent = CreateWindow(SDL_Appname, TEXT(""), STYLE_BASIC, 0, 0, 32, 32, NULL, NULL, SDL_Instance, NULL);
  428. }
  429. style |= GetWindowStyle(window);
  430. /* Figure out what the window area will be */
  431. WIN_AdjustWindowRectWithStyle(window, style, FALSE, &x, &y, &w, &h, SDL_FALSE);
  432. hwnd =
  433. CreateWindow(SDL_Appname, TEXT(""), style, x, y, w, h, parent, NULL,
  434. SDL_Instance, NULL);
  435. if (!hwnd) {
  436. return WIN_SetError("Couldn't create window");
  437. }
  438. WIN_PumpEvents(_this);
  439. if (SetupWindowData(_this, window, hwnd, parent, SDL_TRUE) < 0) {
  440. DestroyWindow(hwnd);
  441. if (parent) {
  442. DestroyWindow(parent);
  443. }
  444. return -1;
  445. }
  446. /* Inform Windows of the frame change so we can respond to WM_NCCALCSIZE */
  447. SetWindowPos(hwnd, NULL, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOSIZE | SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
  448. if (window->flags & SDL_WINDOW_MINIMIZED) {
  449. ShowWindow(hwnd, SW_SHOWMINNOACTIVE);
  450. }
  451. if (!(window->flags & SDL_WINDOW_OPENGL)) {
  452. return 0;
  453. }
  454. /* The rest of this macro mess is for OpenGL or OpenGL ES windows */
  455. #if SDL_VIDEO_OPENGL_ES2
  456. if ((_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES ||
  457. SDL_GetHintBoolean(SDL_HINT_VIDEO_FORCE_EGL, SDL_FALSE)) &&
  458. #if SDL_VIDEO_OPENGL_WGL
  459. (!_this->gl_data || WIN_GL_UseEGL(_this))
  460. #endif /* SDL_VIDEO_OPENGL_WGL */
  461. ) {
  462. #if SDL_VIDEO_OPENGL_EGL
  463. if (WIN_GLES_SetupWindow(_this, window) < 0) {
  464. WIN_DestroyWindow(_this, window);
  465. return -1;
  466. }
  467. return 0;
  468. #else
  469. return SDL_SetError("Could not create GLES window surface (EGL support not configured)");
  470. #endif /* SDL_VIDEO_OPENGL_EGL */
  471. }
  472. #endif /* SDL_VIDEO_OPENGL_ES2 */
  473. #if SDL_VIDEO_OPENGL_WGL
  474. if (WIN_GL_SetupWindow(_this, window) < 0) {
  475. WIN_DestroyWindow(_this, window);
  476. return -1;
  477. }
  478. #else
  479. return SDL_SetError("Could not create GL window (WGL support not configured)");
  480. #endif
  481. return 0;
  482. }
  483. int WIN_CreateWindowFrom(_THIS, SDL_Window *window, const void *data)
  484. {
  485. #if defined(__XBOXONE__) || defined(__XBOXSERIES__)
  486. return -1;
  487. #else
  488. HWND hwnd = (HWND)data;
  489. LPTSTR title;
  490. int titleLen;
  491. SDL_bool isstack;
  492. /* Query the title from the existing window */
  493. titleLen = GetWindowTextLength(hwnd);
  494. title = SDL_small_alloc(TCHAR, titleLen + 1, &isstack);
  495. if (title) {
  496. titleLen = GetWindowText(hwnd, title, titleLen + 1);
  497. } else {
  498. titleLen = 0;
  499. }
  500. if (titleLen > 0) {
  501. window->title = WIN_StringToUTF8(title);
  502. }
  503. if (title) {
  504. SDL_small_free(title, isstack);
  505. }
  506. if (SetupWindowData(_this, window, hwnd, GetParent(hwnd), SDL_FALSE) < 0) {
  507. return -1;
  508. }
  509. #if SDL_VIDEO_OPENGL_WGL
  510. {
  511. const char *hint = SDL_GetHint(SDL_HINT_VIDEO_WINDOW_SHARE_PIXEL_FORMAT);
  512. if (hint) {
  513. /* This hint is a pointer (in string form) of the address of
  514. the window to share a pixel format with
  515. */
  516. SDL_Window *otherWindow = NULL;
  517. (void)SDL_sscanf(hint, "%p", (void **)&otherWindow);
  518. /* Do some error checking on the pointer */
  519. if (otherWindow != NULL && otherWindow->magic == &_this->window_magic) {
  520. /* If the otherWindow has SDL_WINDOW_OPENGL set, set it for the new window as well */
  521. if (otherWindow->flags & SDL_WINDOW_OPENGL) {
  522. window->flags |= SDL_WINDOW_OPENGL;
  523. if (!WIN_GL_SetPixelFormatFrom(_this, otherWindow, window)) {
  524. return -1;
  525. }
  526. }
  527. }
  528. } else if (window->flags & SDL_WINDOW_OPENGL) {
  529. /* Try to set up the pixel format, if it hasn't been set by the application */
  530. WIN_GL_SetupWindow(_this, window);
  531. }
  532. }
  533. #endif
  534. return 0;
  535. #endif /*!defined(__XBOXONE__) && !defined(__XBOXSERIES__)*/
  536. }
  537. void WIN_SetWindowTitle(_THIS, SDL_Window *window)
  538. {
  539. #if !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
  540. HWND hwnd = ((SDL_WindowData *)window->driverdata)->hwnd;
  541. LPTSTR title = WIN_UTF8ToString(window->title);
  542. SetWindowText(hwnd, title);
  543. SDL_free(title);
  544. #endif
  545. }
  546. void WIN_SetWindowIcon(_THIS, SDL_Window *window, SDL_Surface *icon)
  547. {
  548. #if !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
  549. HWND hwnd = ((SDL_WindowData *)window->driverdata)->hwnd;
  550. HICON hicon = NULL;
  551. BYTE *icon_bmp;
  552. int icon_len, mask_len, row_len, y;
  553. BITMAPINFOHEADER *bmi;
  554. Uint8 *dst;
  555. SDL_bool isstack;
  556. /* Create temporary buffer for ICONIMAGE structure */
  557. SDL_COMPILE_TIME_ASSERT(WIN_SetWindowIcon_uses_BITMAPINFOHEADER_to_prepare_an_ICONIMAGE, sizeof(BITMAPINFOHEADER) == 40);
  558. mask_len = (icon->h * (icon->w + 7) / 8);
  559. icon_len = sizeof(BITMAPINFOHEADER) + icon->h * icon->w * sizeof(Uint32) + mask_len;
  560. icon_bmp = SDL_small_alloc(BYTE, icon_len, &isstack);
  561. /* Write the BITMAPINFO header */
  562. bmi = (BITMAPINFOHEADER *)icon_bmp;
  563. bmi->biSize = SDL_SwapLE32(sizeof(BITMAPINFOHEADER));
  564. bmi->biWidth = SDL_SwapLE32(icon->w);
  565. bmi->biHeight = SDL_SwapLE32(icon->h * 2);
  566. bmi->biPlanes = SDL_SwapLE16(1);
  567. bmi->biBitCount = SDL_SwapLE16(32);
  568. bmi->biCompression = SDL_SwapLE32(BI_RGB);
  569. bmi->biSizeImage = SDL_SwapLE32(icon->h * icon->w * sizeof(Uint32));
  570. bmi->biXPelsPerMeter = SDL_SwapLE32(0);
  571. bmi->biYPelsPerMeter = SDL_SwapLE32(0);
  572. bmi->biClrUsed = SDL_SwapLE32(0);
  573. bmi->biClrImportant = SDL_SwapLE32(0);
  574. /* Write the pixels upside down into the bitmap buffer */
  575. SDL_assert(icon->format->format == SDL_PIXELFORMAT_ARGB8888);
  576. dst = &icon_bmp[sizeof(BITMAPINFOHEADER)];
  577. row_len = icon->w * sizeof(Uint32);
  578. y = icon->h;
  579. while (y--) {
  580. Uint8 *src = (Uint8 *)icon->pixels + y * icon->pitch;
  581. SDL_memcpy(dst, src, row_len);
  582. dst += row_len;
  583. }
  584. /* Write the mask */
  585. SDL_memset(icon_bmp + icon_len - mask_len, 0xFF, mask_len);
  586. hicon = CreateIconFromResource(icon_bmp, icon_len, TRUE, 0x00030000);
  587. SDL_small_free(icon_bmp, isstack);
  588. /* Set the icon for the window */
  589. SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hicon);
  590. /* Set the icon in the task manager (should we do this?) */
  591. SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM)hicon);
  592. #endif
  593. }
  594. void WIN_SetWindowPosition(_THIS, SDL_Window *window)
  595. {
  596. /* HighDPI support: removed SWP_NOSIZE. If the move results in a DPI change, we need to allow
  597. * the window to resize (e.g. AdjustWindowRectExForDpi frame sizes are different).
  598. */
  599. WIN_SetWindowPositionInternal(_this, window, SWP_NOCOPYBITS | SWP_NOACTIVATE);
  600. }
  601. void WIN_SetWindowSize(_THIS, SDL_Window *window)
  602. {
  603. WIN_SetWindowPositionInternal(_this, window, SWP_NOCOPYBITS | SWP_NOMOVE | SWP_NOACTIVATE);
  604. }
  605. int WIN_GetWindowBordersSize(_THIS, SDL_Window *window, int *top, int *left, int *bottom, int *right)
  606. {
  607. #if defined(__XBOXONE__) || defined(__XBOXSERIES__)
  608. HWND hwnd = ((SDL_WindowData *)window->driverdata)->hwnd;
  609. RECT rcClient;
  610. /* rcClient stores the size of the inner window, while rcWindow stores the outer size relative to the top-left
  611. * screen position; so the top/left values of rcClient are always {0,0} and bottom/right are {height,width} */
  612. GetClientRect(hwnd, &rcClient);
  613. *top = rcClient.top;
  614. *left = rcClient.left;
  615. *bottom = rcClient.bottom;
  616. *right = rcClient.right;
  617. return 0;
  618. #else /*!defined(__XBOXONE__) && !defined(__XBOXSERIES__)*/
  619. HWND hwnd = ((SDL_WindowData *)window->driverdata)->hwnd;
  620. RECT rcClient, rcWindow;
  621. POINT ptDiff;
  622. /* rcClient stores the size of the inner window, while rcWindow stores the outer size relative to the top-left
  623. * screen position; so the top/left values of rcClient are always {0,0} and bottom/right are {height,width} */
  624. if (!GetClientRect(hwnd, &rcClient)) {
  625. return SDL_SetError("GetClientRect() failed, error %08X", (unsigned int)GetLastError());
  626. }
  627. if (!GetWindowRect(hwnd, &rcWindow)) {
  628. return SDL_SetError("GetWindowRect() failed, error %08X", (unsigned int)GetLastError());
  629. }
  630. /* convert the top/left values to make them relative to
  631. * the window; they will end up being slightly negative */
  632. ptDiff.y = rcWindow.top;
  633. ptDiff.x = rcWindow.left;
  634. if (!ScreenToClient(hwnd, &ptDiff)) {
  635. return SDL_SetError("ScreenToClient() failed, error %08X", (unsigned int)GetLastError());
  636. }
  637. rcWindow.top = ptDiff.y;
  638. rcWindow.left = ptDiff.x;
  639. /* convert the bottom/right values to make them relative to the window,
  640. * these will be slightly bigger than the inner width/height */
  641. ptDiff.y = rcWindow.bottom;
  642. ptDiff.x = rcWindow.right;
  643. if (!ScreenToClient(hwnd, &ptDiff)) {
  644. return SDL_SetError("ScreenToClient() failed, error %08X", (unsigned int)GetLastError());
  645. }
  646. rcWindow.bottom = ptDiff.y;
  647. rcWindow.right = ptDiff.x;
  648. /* Now that both the inner and outer rects use the same coordinate system we can substract them to get the border size.
  649. * Keep in mind that the top/left coordinates of rcWindow are negative because the border lies slightly before {0,0},
  650. * so switch them around because SDL3 wants them in positive. */
  651. *top = rcClient.top - rcWindow.top;
  652. *left = rcClient.left - rcWindow.left;
  653. *bottom = rcWindow.bottom - rcClient.bottom;
  654. *right = rcWindow.right - rcClient.right;
  655. return 0;
  656. #endif /*!defined(__XBOXONE__) && !defined(__XBOXSERIES__)*/
  657. }
  658. void WIN_GetWindowSizeInPixels(_THIS, SDL_Window *window, int *w, int *h)
  659. {
  660. const SDL_WindowData *data = ((SDL_WindowData *)window->driverdata);
  661. HWND hwnd = data->hwnd;
  662. RECT rect;
  663. if (GetClientRect(hwnd, &rect)) {
  664. *w = rect.right;
  665. *h = rect.bottom;
  666. } else {
  667. *w = 0;
  668. *h = 0;
  669. }
  670. }
  671. void WIN_ShowWindow(_THIS, SDL_Window *window)
  672. {
  673. DWORD style;
  674. HWND hwnd;
  675. int nCmdShow;
  676. hwnd = ((SDL_WindowData *)window->driverdata)->hwnd;
  677. nCmdShow = SDL_GetHintBoolean(SDL_HINT_WINDOW_NO_ACTIVATION_WHEN_SHOWN, SDL_FALSE) ? SW_SHOWNA : SW_SHOW;
  678. style = GetWindowLong(hwnd, GWL_EXSTYLE);
  679. if (style & WS_EX_NOACTIVATE) {
  680. nCmdShow = SW_SHOWNOACTIVATE;
  681. }
  682. ShowWindow(hwnd, nCmdShow);
  683. }
  684. void WIN_HideWindow(_THIS, SDL_Window *window)
  685. {
  686. HWND hwnd = ((SDL_WindowData *)window->driverdata)->hwnd;
  687. ShowWindow(hwnd, SW_HIDE);
  688. }
  689. void WIN_RaiseWindow(_THIS, SDL_Window *window)
  690. {
  691. #if !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
  692. /* If desired, raise the window more forcefully.
  693. * Technique taken from http://stackoverflow.com/questions/916259/ .
  694. * Specifically, http://stackoverflow.com/a/34414846 .
  695. *
  696. * The issue is that Microsoft has gone through a lot of trouble to make it
  697. * nearly impossible to programmatically move a window to the foreground,
  698. * for "security" reasons. Apparently, the following song-and-dance gets
  699. * around their objections. */
  700. SDL_bool bForce = SDL_GetHintBoolean(SDL_HINT_FORCE_RAISEWINDOW, SDL_FALSE);
  701. HWND hCurWnd = NULL;
  702. DWORD dwMyID = 0u;
  703. DWORD dwCurID = 0u;
  704. HWND hwnd = ((SDL_WindowData *)window->driverdata)->hwnd;
  705. if (bForce) {
  706. hCurWnd = GetForegroundWindow();
  707. dwMyID = GetCurrentThreadId();
  708. dwCurID = GetWindowThreadProcessId(hCurWnd, NULL);
  709. ShowWindow(hwnd, SW_RESTORE);
  710. AttachThreadInput(dwCurID, dwMyID, TRUE);
  711. SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
  712. SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
  713. }
  714. SetForegroundWindow(hwnd);
  715. if (bForce) {
  716. AttachThreadInput(dwCurID, dwMyID, FALSE);
  717. SetFocus(hwnd);
  718. SetActiveWindow(hwnd);
  719. }
  720. #endif /*!defined(__XBOXONE__) && !defined(__XBOXSERIES__)*/
  721. }
  722. void WIN_MaximizeWindow(_THIS, SDL_Window *window)
  723. {
  724. SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
  725. HWND hwnd = data->hwnd;
  726. data->expected_resize = SDL_TRUE;
  727. ShowWindow(hwnd, SW_MAXIMIZE);
  728. data->expected_resize = SDL_FALSE;
  729. }
  730. void WIN_MinimizeWindow(_THIS, SDL_Window *window)
  731. {
  732. HWND hwnd = ((SDL_WindowData *)window->driverdata)->hwnd;
  733. ShowWindow(hwnd, SW_MINIMIZE);
  734. }
  735. void WIN_SetWindowBordered(_THIS, SDL_Window *window, SDL_bool bordered)
  736. {
  737. SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
  738. HWND hwnd = data->hwnd;
  739. DWORD style;
  740. style = GetWindowLong(hwnd, GWL_STYLE);
  741. style &= ~STYLE_MASK;
  742. style |= GetWindowStyle(window);
  743. data->in_border_change = SDL_TRUE;
  744. SetWindowLong(hwnd, GWL_STYLE, style);
  745. WIN_SetWindowPositionInternal(_this, window, SWP_NOCOPYBITS | SWP_FRAMECHANGED | SWP_NOZORDER | SWP_NOACTIVATE);
  746. data->in_border_change = SDL_FALSE;
  747. }
  748. void WIN_SetWindowResizable(_THIS, SDL_Window *window, SDL_bool resizable)
  749. {
  750. SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
  751. HWND hwnd = data->hwnd;
  752. DWORD style;
  753. style = GetWindowLong(hwnd, GWL_STYLE);
  754. style &= ~STYLE_MASK;
  755. style |= GetWindowStyle(window);
  756. SetWindowLong(hwnd, GWL_STYLE, style);
  757. }
  758. void WIN_SetWindowAlwaysOnTop(_THIS, SDL_Window *window, SDL_bool on_top)
  759. {
  760. SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
  761. HWND hwnd = data->hwnd;
  762. if (on_top) {
  763. SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
  764. } else {
  765. SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
  766. }
  767. }
  768. void WIN_RestoreWindow(_THIS, SDL_Window *window)
  769. {
  770. SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
  771. HWND hwnd = data->hwnd;
  772. data->expected_resize = SDL_TRUE;
  773. ShowWindow(hwnd, SW_RESTORE);
  774. data->expected_resize = SDL_FALSE;
  775. }
  776. /**
  777. * Reconfigures the window to fill the given display, if fullscreen is true, otherwise restores the window.
  778. */
  779. void WIN_SetWindowFullscreen(_THIS, SDL_Window *window, SDL_VideoDisplay *display, SDL_bool fullscreen)
  780. {
  781. #if !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
  782. SDL_DisplayData *displaydata = (SDL_DisplayData *)display->driverdata;
  783. SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
  784. HWND hwnd = data->hwnd;
  785. MONITORINFO minfo;
  786. DWORD style;
  787. HWND top;
  788. int x, y;
  789. int w, h;
  790. if (!fullscreen && (window->flags & (SDL_WINDOW_FULLSCREEN | SDL_WINDOW_FULLSCREEN_DESKTOP)) != 0) {
  791. /* Resizing the window on hide causes problems restoring it in Wine, and it's unnecessary.
  792. * Also, Windows would preview the minimized window with the wrong size.
  793. */
  794. return;
  795. }
  796. #ifdef HIGHDPI_DEBUG
  797. SDL_Log("WIN_SetWindowFullscreen: %d", (int)fullscreen);
  798. #endif
  799. if (SDL_ShouldAllowTopmost() && (window->flags & SDL_WINDOW_ALWAYS_ON_TOP)) {
  800. top = HWND_TOPMOST;
  801. } else {
  802. top = HWND_NOTOPMOST;
  803. }
  804. style = GetWindowLong(hwnd, GWL_STYLE);
  805. style &= ~STYLE_MASK;
  806. style |= GetWindowStyle(window);
  807. /* Use GetMonitorInfo instead of WIN_GetDisplayBounds because we want the
  808. monitor bounds in Windows coordinates (pixels) rather than SDL coordinates (points). */
  809. SDL_zero(minfo);
  810. minfo.cbSize = sizeof(MONITORINFO);
  811. if (!GetMonitorInfo(displaydata->MonitorHandle, &minfo)) {
  812. SDL_SetError("GetMonitorInfo failed");
  813. return;
  814. }
  815. if (fullscreen) {
  816. x = minfo.rcMonitor.left;
  817. y = minfo.rcMonitor.top;
  818. w = minfo.rcMonitor.right - minfo.rcMonitor.left;
  819. h = minfo.rcMonitor.bottom - minfo.rcMonitor.top;
  820. /* Unset the maximized flag. This fixes
  821. https://bugzilla.libsdl.org/show_bug.cgi?id=3215
  822. */
  823. if (style & WS_MAXIMIZE) {
  824. data->windowed_mode_was_maximized = SDL_TRUE;
  825. style &= ~WS_MAXIMIZE;
  826. }
  827. } else {
  828. BOOL menu;
  829. /* Restore window-maximization state, as applicable.
  830. Special care is taken to *not* do this if and when we're
  831. alt-tab'ing away (to some other window; as indicated by
  832. in_window_deactivation), otherwise
  833. https://bugzilla.libsdl.org/show_bug.cgi?id=3215 can reproduce!
  834. */
  835. if (data->windowed_mode_was_maximized && !data->in_window_deactivation) {
  836. style |= WS_MAXIMIZE;
  837. data->windowed_mode_was_maximized = SDL_FALSE;
  838. }
  839. menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
  840. WIN_AdjustWindowRectWithStyle(window, style, menu, &x, &y, &w, &h, SDL_FALSE);
  841. }
  842. SetWindowLong(hwnd, GWL_STYLE, style);
  843. data->expected_resize = SDL_TRUE;
  844. SetWindowPos(hwnd, top, x, y, w, h, SWP_NOCOPYBITS | SWP_NOACTIVATE);
  845. data->expected_resize = SDL_FALSE;
  846. #ifdef HIGHDPI_DEBUG
  847. SDL_Log("WIN_SetWindowFullscreen: %d finished. Set window to %d,%d, %dx%d", (int)fullscreen, x, y, w, h);
  848. #endif
  849. #endif /*!defined(__XBOXONE__) && !defined(__XBOXSERIES__)*/
  850. }
  851. #if !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
  852. void WIN_UpdateWindowICCProfile(SDL_Window *window, SDL_bool send_event)
  853. {
  854. SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
  855. SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
  856. SDL_DisplayData *displaydata = display ? (SDL_DisplayData *)display->driverdata : NULL;
  857. if (displaydata) {
  858. HDC hdc = CreateDCW(displaydata->DeviceName, NULL, NULL, NULL);
  859. if (hdc) {
  860. WCHAR fileName[MAX_PATH];
  861. DWORD fileNameSize = SDL_arraysize(fileName);
  862. if (GetICMProfileW(hdc, &fileNameSize, fileName)) {
  863. /* fileNameSize includes '\0' on return */
  864. if (!data->ICMFileName ||
  865. SDL_wcscmp(data->ICMFileName, fileName) != 0) {
  866. if (data->ICMFileName) {
  867. SDL_free(data->ICMFileName);
  868. }
  869. data->ICMFileName = SDL_wcsdup(fileName);
  870. if (send_event) {
  871. SDL_SendWindowEvent(window, SDL_WINDOWEVENT_ICCPROF_CHANGED, 0, 0);
  872. }
  873. }
  874. }
  875. DeleteDC(hdc);
  876. }
  877. }
  878. }
  879. void *
  880. WIN_GetWindowICCProfile(_THIS, SDL_Window *window, size_t *size)
  881. {
  882. SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
  883. char *filename_utf8;
  884. void *iccProfileData = NULL;
  885. filename_utf8 = WIN_StringToUTF8(data->ICMFileName);
  886. if (filename_utf8) {
  887. iccProfileData = SDL_LoadFile(filename_utf8, size);
  888. if (iccProfileData == NULL) {
  889. SDL_SetError("Could not open ICC profile");
  890. }
  891. SDL_free(filename_utf8);
  892. } else {
  893. SDL_OutOfMemory();
  894. }
  895. return iccProfileData;
  896. }
  897. static void WIN_GrabKeyboard(SDL_Window *window)
  898. {
  899. SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
  900. HMODULE module;
  901. if (data->keyboard_hook) {
  902. return;
  903. }
  904. /* SetWindowsHookEx() needs to know which module contains the hook we
  905. want to install. This is complicated by the fact that SDL can be
  906. linked statically or dynamically. Fortunately XP and later provide
  907. this nice API that will go through the loaded modules and find the
  908. one containing our code.
  909. */
  910. if (!GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT | GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
  911. (LPTSTR)WIN_KeyboardHookProc,
  912. &module)) {
  913. return;
  914. }
  915. /* Capture a snapshot of the current keyboard state before the hook */
  916. if (!GetKeyboardState(data->videodata->pre_hook_key_state)) {
  917. return;
  918. }
  919. /* To grab the keyboard, we have to install a low-level keyboard hook to
  920. intercept keys that would normally be captured by the OS. Intercepting
  921. all key events on the system is rather invasive, but it's what Microsoft
  922. actually documents that you do to capture these.
  923. */
  924. data->keyboard_hook = SetWindowsHookEx(WH_KEYBOARD_LL, WIN_KeyboardHookProc, module, 0);
  925. }
  926. void WIN_UngrabKeyboard(SDL_Window *window)
  927. {
  928. SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
  929. if (data->keyboard_hook) {
  930. UnhookWindowsHookEx(data->keyboard_hook);
  931. data->keyboard_hook = NULL;
  932. }
  933. }
  934. void WIN_SetWindowMouseRect(_THIS, SDL_Window *window)
  935. {
  936. WIN_UpdateClipCursor(window);
  937. }
  938. void WIN_SetWindowMouseGrab(_THIS, SDL_Window *window, SDL_bool grabbed)
  939. {
  940. WIN_UpdateClipCursor(window);
  941. }
  942. void WIN_SetWindowKeyboardGrab(_THIS, SDL_Window *window, SDL_bool grabbed)
  943. {
  944. if (grabbed) {
  945. WIN_GrabKeyboard(window);
  946. } else {
  947. WIN_UngrabKeyboard(window);
  948. }
  949. }
  950. #endif /*!defined(__XBOXONE__) && !defined(__XBOXSERIES__)*/
  951. void WIN_DestroyWindow(_THIS, SDL_Window *window)
  952. {
  953. if (window->shaper) {
  954. SDL_ShapeData *shapedata = (SDL_ShapeData *)window->shaper->driverdata;
  955. if (shapedata) {
  956. if (shapedata->mask_tree) {
  957. SDL_FreeShapeTree(&shapedata->mask_tree);
  958. }
  959. SDL_free(shapedata);
  960. }
  961. SDL_free(window->shaper);
  962. window->shaper = NULL;
  963. }
  964. CleanupWindowData(_this, window);
  965. }
  966. int WIN_GetWindowWMInfo(_THIS, SDL_Window *window, SDL_SysWMinfo *info)
  967. {
  968. const SDL_WindowData *data = (const SDL_WindowData *)window->driverdata;
  969. info->subsystem = SDL_SYSWM_WINDOWS;
  970. info->info.win.window = data->hwnd;
  971. info->info.win.hdc = data->hdc;
  972. info->info.win.hinstance = data->hinstance;
  973. return 0;
  974. }
  975. /*
  976. * Creates a HelperWindow used for DirectInput.
  977. */
  978. int SDL_HelperWindowCreate(void)
  979. {
  980. HINSTANCE hInstance = GetModuleHandle(NULL);
  981. WNDCLASS wce;
  982. /* Make sure window isn't created twice. */
  983. if (SDL_HelperWindow != NULL) {
  984. return 0;
  985. }
  986. /* Create the class. */
  987. SDL_zero(wce);
  988. wce.lpfnWndProc = DefWindowProc;
  989. wce.lpszClassName = SDL_HelperWindowClassName;
  990. wce.hInstance = hInstance;
  991. /* Register the class. */
  992. SDL_HelperWindowClass = RegisterClass(&wce);
  993. if (SDL_HelperWindowClass == 0 && GetLastError() != ERROR_CLASS_ALREADY_EXISTS) {
  994. return WIN_SetError("Unable to create Helper Window Class");
  995. }
  996. /* Create the window. */
  997. SDL_HelperWindow = CreateWindowEx(0, SDL_HelperWindowClassName,
  998. SDL_HelperWindowName,
  999. WS_OVERLAPPED, CW_USEDEFAULT,
  1000. CW_USEDEFAULT, CW_USEDEFAULT,
  1001. CW_USEDEFAULT, HWND_MESSAGE, NULL,
  1002. hInstance, NULL);
  1003. if (SDL_HelperWindow == NULL) {
  1004. UnregisterClass(SDL_HelperWindowClassName, hInstance);
  1005. return WIN_SetError("Unable to create Helper Window");
  1006. }
  1007. return 0;
  1008. }
  1009. /*
  1010. * Destroys the HelperWindow previously created with SDL_HelperWindowCreate.
  1011. */
  1012. void SDL_HelperWindowDestroy(void)
  1013. {
  1014. HINSTANCE hInstance = GetModuleHandle(NULL);
  1015. /* Destroy the window. */
  1016. if (SDL_HelperWindow != NULL) {
  1017. if (DestroyWindow(SDL_HelperWindow) == 0) {
  1018. WIN_SetError("Unable to destroy Helper Window");
  1019. return;
  1020. }
  1021. SDL_HelperWindow = NULL;
  1022. }
  1023. /* Unregister the class. */
  1024. if (SDL_HelperWindowClass != 0) {
  1025. if ((UnregisterClass(SDL_HelperWindowClassName, hInstance)) == 0) {
  1026. WIN_SetError("Unable to destroy Helper Window Class");
  1027. return;
  1028. }
  1029. SDL_HelperWindowClass = 0;
  1030. }
  1031. }
  1032. #if !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
  1033. void WIN_OnWindowEnter(_THIS, SDL_Window *window)
  1034. {
  1035. SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
  1036. if (data == NULL || !data->hwnd) {
  1037. /* The window wasn't fully initialized */
  1038. return;
  1039. }
  1040. if (window->flags & SDL_WINDOW_ALWAYS_ON_TOP) {
  1041. WIN_SetWindowPositionInternal(_this, window, SWP_NOCOPYBITS | SWP_NOSIZE | SWP_NOACTIVATE);
  1042. }
  1043. }
  1044. void WIN_UpdateClipCursor(SDL_Window *window)
  1045. {
  1046. SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
  1047. SDL_Mouse *mouse = SDL_GetMouse();
  1048. RECT rect, clipped_rect;
  1049. if (data->in_title_click || data->focus_click_pending) {
  1050. return;
  1051. }
  1052. if (data->skip_update_clipcursor) {
  1053. return;
  1054. }
  1055. if (!GetClipCursor(&clipped_rect)) {
  1056. return;
  1057. }
  1058. if ((mouse->relative_mode || (window->flags & SDL_WINDOW_MOUSE_GRABBED) ||
  1059. (window->mouse_rect.w > 0 && window->mouse_rect.h > 0)) &&
  1060. (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
  1061. if (mouse->relative_mode && !mouse->relative_mode_warp && data->mouse_relative_mode_center) {
  1062. if (GetWindowRect(data->hwnd, &rect)) {
  1063. LONG cx, cy;
  1064. cx = (rect.left + rect.right) / 2;
  1065. cy = (rect.top + rect.bottom) / 2;
  1066. /* Make an absurdly small clip rect */
  1067. rect.left = cx;
  1068. rect.right = cx + 1;
  1069. rect.top = cy;
  1070. rect.bottom = cy + 1;
  1071. if (SDL_memcmp(&rect, &clipped_rect, sizeof(rect)) != 0) {
  1072. if (ClipCursor(&rect)) {
  1073. data->cursor_clipped_rect = rect;
  1074. }
  1075. }
  1076. }
  1077. } else {
  1078. if (GetClientRect(data->hwnd, &rect)) {
  1079. ClientToScreen(data->hwnd, (LPPOINT)&rect);
  1080. ClientToScreen(data->hwnd, (LPPOINT)&rect + 1);
  1081. if (window->mouse_rect.w > 0 && window->mouse_rect.h > 0) {
  1082. SDL_Rect mouse_rect_win_client;
  1083. RECT mouse_rect, intersection;
  1084. /* mouse_rect_win_client is the mouse rect in Windows client space */
  1085. mouse_rect_win_client = window->mouse_rect;
  1086. WIN_ClientPointFromSDL(window, &mouse_rect_win_client.x, &mouse_rect_win_client.y);
  1087. WIN_ClientPointFromSDL(window, &mouse_rect_win_client.w, &mouse_rect_win_client.h);
  1088. /* mouse_rect is the rect in Windows screen space */
  1089. mouse_rect.left = rect.left + mouse_rect_win_client.x;
  1090. mouse_rect.top = rect.top + mouse_rect_win_client.y;
  1091. mouse_rect.right = mouse_rect.left + mouse_rect_win_client.w;
  1092. mouse_rect.bottom = mouse_rect.top + mouse_rect_win_client.h;
  1093. if (IntersectRect(&intersection, &rect, &mouse_rect)) {
  1094. SDL_memcpy(&rect, &intersection, sizeof(rect));
  1095. } else if ((window->flags & SDL_WINDOW_MOUSE_GRABBED) != 0) {
  1096. /* Mouse rect was invalid, just do the normal grab */
  1097. } else {
  1098. SDL_zero(rect);
  1099. }
  1100. }
  1101. if (SDL_memcmp(&rect, &clipped_rect, sizeof(rect)) != 0) {
  1102. if (!IsRectEmpty(&rect)) {
  1103. if (ClipCursor(&rect)) {
  1104. data->cursor_clipped_rect = rect;
  1105. }
  1106. } else {
  1107. ClipCursor(NULL);
  1108. SDL_zero(data->cursor_clipped_rect);
  1109. }
  1110. }
  1111. }
  1112. }
  1113. } else {
  1114. POINT first, second;
  1115. first.x = clipped_rect.left;
  1116. first.y = clipped_rect.top;
  1117. second.x = clipped_rect.right - 1;
  1118. second.y = clipped_rect.bottom - 1;
  1119. if (PtInRect(&data->cursor_clipped_rect, first) &&
  1120. PtInRect(&data->cursor_clipped_rect, second)) {
  1121. ClipCursor(NULL);
  1122. SDL_zero(data->cursor_clipped_rect);
  1123. }
  1124. }
  1125. data->last_updated_clipcursor = SDL_GetTicks();
  1126. }
  1127. int WIN_SetWindowHitTest(SDL_Window *window, SDL_bool enabled)
  1128. {
  1129. return 0; /* just succeed, the real work is done elsewhere. */
  1130. }
  1131. #endif /*!defined(__XBOXONE__) && !defined(__XBOXSERIES__)*/
  1132. int WIN_SetWindowOpacity(_THIS, SDL_Window *window, float opacity)
  1133. {
  1134. #if defined(__XBOXONE__) || defined(__XBOXSERIES__)
  1135. return -1;
  1136. #else
  1137. const SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
  1138. HWND hwnd = data->hwnd;
  1139. const LONG style = GetWindowLong(hwnd, GWL_EXSTYLE);
  1140. SDL_assert(style != 0);
  1141. if (opacity == 1.0f) {
  1142. /* want it fully opaque, just mark it unlayered if necessary. */
  1143. if (style & WS_EX_LAYERED) {
  1144. if (SetWindowLong(hwnd, GWL_EXSTYLE, style & ~WS_EX_LAYERED) == 0) {
  1145. return WIN_SetError("SetWindowLong()");
  1146. }
  1147. }
  1148. } else {
  1149. const BYTE alpha = (BYTE)((int)(opacity * 255.0f));
  1150. /* want it transparent, mark it layered if necessary. */
  1151. if ((style & WS_EX_LAYERED) == 0) {
  1152. if (SetWindowLong(hwnd, GWL_EXSTYLE, style | WS_EX_LAYERED) == 0) {
  1153. return WIN_SetError("SetWindowLong()");
  1154. }
  1155. }
  1156. if (SetLayeredWindowAttributes(hwnd, 0, alpha, LWA_ALPHA) == 0) {
  1157. return WIN_SetError("SetLayeredWindowAttributes()");
  1158. }
  1159. }
  1160. return 0;
  1161. #endif /*!defined(__XBOXONE__) && !defined(__XBOXSERIES__)*/
  1162. }
  1163. /**
  1164. * Convert a point in the client area from pixels to DPI-scaled points.
  1165. *
  1166. * No-op if DPI scaling is not enabled.
  1167. */
  1168. void WIN_ClientPointToSDL(const SDL_Window *window, int *x, int *y)
  1169. {
  1170. const SDL_WindowData *data = ((SDL_WindowData *)window->driverdata);
  1171. const SDL_VideoData *videodata = data->videodata;
  1172. if (!videodata->dpi_scaling_enabled) {
  1173. return;
  1174. }
  1175. *x = MulDiv(*x, 96, data->scaling_dpi);
  1176. *y = MulDiv(*y, 96, data->scaling_dpi);
  1177. }
  1178. void WIN_ClientPointToSDLFloat(const SDL_Window *window, LONG x, LONG y, float *xOut, float *yOut)
  1179. {
  1180. const SDL_WindowData *data = ((SDL_WindowData *)window->driverdata);
  1181. const SDL_VideoData *videodata = data->videodata;
  1182. if (videodata->dpi_scaling_enabled) {
  1183. *xOut = (float)(x * 96) / data->scaling_dpi;
  1184. *yOut = (float)(y * 96) / data->scaling_dpi;
  1185. } else {
  1186. *xOut = (float)x;
  1187. *yOut = (float)y;
  1188. }
  1189. }
  1190. /**
  1191. * Convert a point in the client area from DPI-scaled points to pixels.
  1192. *
  1193. * No-op if DPI scaling is not enabled.
  1194. */
  1195. void WIN_ClientPointFromSDL(const SDL_Window *window, int *x, int *y)
  1196. {
  1197. const SDL_WindowData *data = ((SDL_WindowData *)window->driverdata);
  1198. const SDL_VideoData *videodata = data->videodata;
  1199. if (!videodata->dpi_scaling_enabled) {
  1200. return;
  1201. }
  1202. *x = MulDiv(*x, data->scaling_dpi, 96);
  1203. *y = MulDiv(*y, data->scaling_dpi, 96);
  1204. }
  1205. void WIN_ClientPointFromSDLFloat(const SDL_Window *window, float x, float y, LONG *xOut, LONG *yOut)
  1206. {
  1207. const SDL_WindowData *data = ((SDL_WindowData *)window->driverdata);
  1208. const SDL_VideoData *videodata = data->videodata;
  1209. if (videodata->dpi_scaling_enabled) {
  1210. *xOut = (LONG)SDL_roundf((x * data->scaling_dpi) / 96.0f);
  1211. *yOut = (LONG)SDL_roundf((y * data->scaling_dpi) / 96.0f);
  1212. } else {
  1213. *xOut = (LONG)SDL_roundf(x);
  1214. *yOut = (LONG)SDL_roundf(y);
  1215. }
  1216. }
  1217. #if !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
  1218. void WIN_AcceptDragAndDrop(SDL_Window *window, SDL_bool accept)
  1219. {
  1220. const SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
  1221. DragAcceptFiles(data->hwnd, accept ? TRUE : FALSE);
  1222. }
  1223. int WIN_FlashWindow(_THIS, SDL_Window *window, SDL_FlashOperation operation)
  1224. {
  1225. FLASHWINFO desc;
  1226. SDL_zero(desc);
  1227. desc.cbSize = sizeof(desc);
  1228. desc.hwnd = ((SDL_WindowData *)window->driverdata)->hwnd;
  1229. switch (operation) {
  1230. case SDL_FLASH_CANCEL:
  1231. desc.dwFlags = FLASHW_STOP;
  1232. break;
  1233. case SDL_FLASH_BRIEFLY:
  1234. desc.dwFlags = FLASHW_TRAY;
  1235. desc.uCount = 1;
  1236. break;
  1237. case SDL_FLASH_UNTIL_FOCUSED:
  1238. desc.dwFlags = (FLASHW_TRAY | FLASHW_TIMERNOFG);
  1239. break;
  1240. default:
  1241. return SDL_Unsupported();
  1242. }
  1243. FlashWindowEx(&desc);
  1244. return 0;
  1245. }
  1246. #endif /*!defined(__XBOXONE__) && !defined(__XBOXSERIES__)*/
  1247. #endif /* SDL_VIDEO_DRIVER_WINDOWS */