SDL_windowswindow.c 47 KB

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