SDL_windowswindow.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2016 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_assert.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_windowsvideo.h"
  27. #include "SDL_windowswindow.h"
  28. #include "SDL_hints.h"
  29. /* Dropfile support */
  30. #include <shellapi.h>
  31. /* This is included after SDL_windowsvideo.h, which includes windows.h */
  32. #include "SDL_syswm.h"
  33. /* Windows CE compatibility */
  34. #ifndef SWP_NOCOPYBITS
  35. #define SWP_NOCOPYBITS 0
  36. #endif
  37. /* Fake window to help with DirectInput events. */
  38. HWND SDL_HelperWindow = NULL;
  39. static WCHAR *SDL_HelperWindowClassName = TEXT("SDLHelperWindowInputCatcher");
  40. static WCHAR *SDL_HelperWindowName = TEXT("SDLHelperWindowInputMsgWindow");
  41. static ATOM SDL_HelperWindowClass = 0;
  42. #define STYLE_BASIC (WS_CLIPSIBLINGS | WS_CLIPCHILDREN)
  43. #define STYLE_FULLSCREEN (WS_POPUP)
  44. #define STYLE_BORDERLESS (WS_POPUP)
  45. #define STYLE_NORMAL (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX)
  46. #define STYLE_RESIZABLE (WS_THICKFRAME | WS_MAXIMIZEBOX)
  47. #define STYLE_MASK (STYLE_FULLSCREEN | STYLE_BORDERLESS | STYLE_NORMAL | STYLE_RESIZABLE)
  48. static DWORD
  49. GetWindowStyle(SDL_Window * window)
  50. {
  51. DWORD style = 0;
  52. if (window->flags & SDL_WINDOW_FULLSCREEN) {
  53. style |= STYLE_FULLSCREEN;
  54. } else {
  55. if (window->flags & SDL_WINDOW_BORDERLESS) {
  56. style |= STYLE_BORDERLESS;
  57. } else {
  58. style |= STYLE_NORMAL;
  59. }
  60. if (window->flags & SDL_WINDOW_RESIZABLE) {
  61. style |= STYLE_RESIZABLE;
  62. }
  63. }
  64. return style;
  65. }
  66. static void
  67. WIN_SetWindowPositionInternal(_THIS, SDL_Window * window, UINT flags)
  68. {
  69. SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
  70. HWND hwnd = data->hwnd;
  71. RECT rect;
  72. DWORD style;
  73. HWND top;
  74. BOOL menu;
  75. int x, y;
  76. int w, h;
  77. /* Figure out what the window area will be */
  78. if (SDL_ShouldAllowTopmost() && (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_INPUT_FOCUS)) == (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_INPUT_FOCUS)) {
  79. top = HWND_TOPMOST;
  80. } else {
  81. top = HWND_NOTOPMOST;
  82. }
  83. style = GetWindowLong(hwnd, GWL_STYLE);
  84. rect.left = 0;
  85. rect.top = 0;
  86. rect.right = window->w;
  87. rect.bottom = window->h;
  88. menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
  89. AdjustWindowRectEx(&rect, style, menu, 0);
  90. w = (rect.right - rect.left);
  91. h = (rect.bottom - rect.top);
  92. x = window->x + rect.left;
  93. y = window->y + rect.top;
  94. data->expected_resize = SDL_TRUE;
  95. SetWindowPos(hwnd, top, x, y, w, h, flags);
  96. data->expected_resize = SDL_FALSE;
  97. }
  98. static int
  99. SetupWindowData(_THIS, SDL_Window * window, HWND hwnd, SDL_bool created)
  100. {
  101. SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
  102. SDL_WindowData *data;
  103. /* Allocate the window data */
  104. data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
  105. if (!data) {
  106. return SDL_OutOfMemory();
  107. }
  108. data->window = window;
  109. data->hwnd = hwnd;
  110. data->hdc = GetDC(hwnd);
  111. data->created = created;
  112. data->mouse_button_flags = 0;
  113. data->videodata = videodata;
  114. data->initializing = SDL_TRUE;
  115. window->driverdata = data;
  116. /* Associate the data with the window */
  117. if (!SetProp(hwnd, TEXT("SDL_WindowData"), data)) {
  118. ReleaseDC(hwnd, data->hdc);
  119. SDL_free(data);
  120. return WIN_SetError("SetProp() failed");
  121. }
  122. /* Set up the window proc function */
  123. #ifdef GWLP_WNDPROC
  124. data->wndproc = (WNDPROC) GetWindowLongPtr(hwnd, GWLP_WNDPROC);
  125. if (data->wndproc == WIN_WindowProc) {
  126. data->wndproc = NULL;
  127. } else {
  128. SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR) WIN_WindowProc);
  129. }
  130. #else
  131. data->wndproc = (WNDPROC) GetWindowLong(hwnd, GWL_WNDPROC);
  132. if (data->wndproc == WIN_WindowProc) {
  133. data->wndproc = NULL;
  134. } else {
  135. SetWindowLong(hwnd, GWL_WNDPROC, (LONG_PTR) WIN_WindowProc);
  136. }
  137. #endif
  138. /* Fill in the SDL window with the window data */
  139. {
  140. RECT rect;
  141. if (GetClientRect(hwnd, &rect)) {
  142. int w = rect.right;
  143. int h = rect.bottom;
  144. if ((window->w && window->w != w) || (window->h && window->h != h)) {
  145. /* We tried to create a window larger than the desktop and Windows didn't allow it. Override! */
  146. RECT rect;
  147. DWORD style;
  148. BOOL menu;
  149. int x, y;
  150. int w, h;
  151. /* Figure out what the window area will be */
  152. style = GetWindowLong(hwnd, GWL_STYLE);
  153. rect.left = 0;
  154. rect.top = 0;
  155. rect.right = window->w;
  156. rect.bottom = window->h;
  157. menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
  158. AdjustWindowRectEx(&rect, style, menu, 0);
  159. w = (rect.right - rect.left);
  160. h = (rect.bottom - rect.top);
  161. x = window->x + rect.left;
  162. y = window->y + rect.top;
  163. SetWindowPos(hwnd, HWND_NOTOPMOST, x, y, w, h, SWP_NOCOPYBITS | SWP_NOZORDER | SWP_NOACTIVATE);
  164. } else {
  165. window->w = w;
  166. window->h = h;
  167. }
  168. }
  169. }
  170. {
  171. POINT point;
  172. point.x = 0;
  173. point.y = 0;
  174. if (ClientToScreen(hwnd, &point)) {
  175. window->x = point.x;
  176. window->y = point.y;
  177. }
  178. }
  179. {
  180. DWORD style = GetWindowLong(hwnd, GWL_STYLE);
  181. if (style & WS_VISIBLE) {
  182. window->flags |= SDL_WINDOW_SHOWN;
  183. } else {
  184. window->flags &= ~SDL_WINDOW_SHOWN;
  185. }
  186. if (style & (WS_BORDER | WS_THICKFRAME)) {
  187. window->flags &= ~SDL_WINDOW_BORDERLESS;
  188. } else {
  189. window->flags |= SDL_WINDOW_BORDERLESS;
  190. }
  191. if (style & WS_THICKFRAME) {
  192. window->flags |= SDL_WINDOW_RESIZABLE;
  193. } else {
  194. window->flags &= ~SDL_WINDOW_RESIZABLE;
  195. }
  196. #ifdef WS_MAXIMIZE
  197. if (style & WS_MAXIMIZE) {
  198. window->flags |= SDL_WINDOW_MAXIMIZED;
  199. } else
  200. #endif
  201. {
  202. window->flags &= ~SDL_WINDOW_MAXIMIZED;
  203. }
  204. #ifdef WS_MINIMIZE
  205. if (style & WS_MINIMIZE) {
  206. window->flags |= SDL_WINDOW_MINIMIZED;
  207. } else
  208. #endif
  209. {
  210. window->flags &= ~SDL_WINDOW_MINIMIZED;
  211. }
  212. }
  213. if (GetFocus() == hwnd) {
  214. window->flags |= SDL_WINDOW_INPUT_FOCUS;
  215. SDL_SetKeyboardFocus(data->window);
  216. if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
  217. RECT rect;
  218. GetClientRect(hwnd, &rect);
  219. ClientToScreen(hwnd, (LPPOINT) & rect);
  220. ClientToScreen(hwnd, (LPPOINT) & rect + 1);
  221. ClipCursor(&rect);
  222. }
  223. }
  224. /* Enable multi-touch */
  225. if (videodata->RegisterTouchWindow) {
  226. videodata->RegisterTouchWindow(hwnd, (TWF_FINETOUCH|TWF_WANTPALM));
  227. }
  228. /* Enable dropping files */
  229. DragAcceptFiles(hwnd, TRUE);
  230. data->initializing = SDL_FALSE;
  231. /* All done! */
  232. return 0;
  233. }
  234. int
  235. WIN_CreateWindow(_THIS, SDL_Window * window)
  236. {
  237. HWND hwnd;
  238. RECT rect;
  239. DWORD style = STYLE_BASIC;
  240. int x, y;
  241. int w, h;
  242. style |= GetWindowStyle(window);
  243. /* Figure out what the window area will be */
  244. rect.left = window->x;
  245. rect.top = window->y;
  246. rect.right = window->x + window->w;
  247. rect.bottom = window->y + window->h;
  248. AdjustWindowRectEx(&rect, style, FALSE, 0);
  249. x = rect.left;
  250. y = rect.top;
  251. w = (rect.right - rect.left);
  252. h = (rect.bottom - rect.top);
  253. hwnd =
  254. CreateWindow(SDL_Appname, TEXT(""), style, x, y, w, h, NULL, NULL,
  255. SDL_Instance, NULL);
  256. if (!hwnd) {
  257. return WIN_SetError("Couldn't create window");
  258. }
  259. WIN_PumpEvents(_this);
  260. if (SetupWindowData(_this, window, hwnd, SDL_TRUE) < 0) {
  261. DestroyWindow(hwnd);
  262. return -1;
  263. }
  264. #if SDL_VIDEO_OPENGL_WGL
  265. /* We need to initialize the extensions before deciding how to create ES profiles */
  266. if (window->flags & SDL_WINDOW_OPENGL) {
  267. WIN_GL_InitExtensions(_this);
  268. }
  269. #endif
  270. #if SDL_VIDEO_OPENGL_ES2
  271. if ((window->flags & SDL_WINDOW_OPENGL) &&
  272. _this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES
  273. #if SDL_VIDEO_OPENGL_WGL
  274. && (!_this->gl_data || !_this->gl_data->HAS_WGL_EXT_create_context_es2_profile)
  275. #endif
  276. ) {
  277. #if SDL_VIDEO_OPENGL_EGL
  278. if (WIN_GLES_SetupWindow(_this, window) < 0) {
  279. WIN_DestroyWindow(_this, window);
  280. return -1;
  281. }
  282. #else
  283. return SDL_SetError("Could not create GLES window surface (no EGL support available)");
  284. #endif /* SDL_VIDEO_OPENGL_EGL */
  285. } else
  286. #endif /* SDL_VIDEO_OPENGL_ES2 */
  287. #if SDL_VIDEO_OPENGL_WGL
  288. if (window->flags & SDL_WINDOW_OPENGL) {
  289. if (WIN_GL_SetupWindow(_this, window) < 0) {
  290. WIN_DestroyWindow(_this, window);
  291. return -1;
  292. }
  293. }
  294. #endif
  295. return 0;
  296. }
  297. int
  298. WIN_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
  299. {
  300. HWND hwnd = (HWND) data;
  301. LPTSTR title;
  302. int titleLen;
  303. /* Query the title from the existing window */
  304. titleLen = GetWindowTextLength(hwnd);
  305. title = SDL_stack_alloc(TCHAR, titleLen + 1);
  306. if (title) {
  307. titleLen = GetWindowText(hwnd, title, titleLen);
  308. } else {
  309. titleLen = 0;
  310. }
  311. if (titleLen > 0) {
  312. window->title = WIN_StringToUTF8(title);
  313. }
  314. if (title) {
  315. SDL_stack_free(title);
  316. }
  317. if (SetupWindowData(_this, window, hwnd, SDL_FALSE) < 0) {
  318. return -1;
  319. }
  320. #if SDL_VIDEO_OPENGL_WGL
  321. {
  322. const char *hint = SDL_GetHint(SDL_HINT_VIDEO_WINDOW_SHARE_PIXEL_FORMAT);
  323. if (hint) {
  324. /* This hint is a pointer (in string form) of the address of
  325. the window to share a pixel format with
  326. */
  327. SDL_Window *otherWindow = NULL;
  328. SDL_sscanf(hint, "%p", (void**)&otherWindow);
  329. /* Do some error checking on the pointer */
  330. if (otherWindow != NULL && otherWindow->magic == &_this->window_magic)
  331. {
  332. /* If the otherWindow has SDL_WINDOW_OPENGL set, set it for the new window as well */
  333. if (otherWindow->flags & SDL_WINDOW_OPENGL)
  334. {
  335. window->flags |= SDL_WINDOW_OPENGL;
  336. if(!WIN_GL_SetPixelFormatFrom(_this, otherWindow, window)) {
  337. return -1;
  338. }
  339. }
  340. }
  341. }
  342. }
  343. #endif
  344. return 0;
  345. }
  346. void
  347. WIN_SetWindowTitle(_THIS, SDL_Window * window)
  348. {
  349. HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
  350. LPTSTR title = WIN_UTF8ToString(window->title);
  351. SetWindowText(hwnd, title);
  352. SDL_free(title);
  353. }
  354. void
  355. WIN_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
  356. {
  357. HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
  358. HICON hicon = NULL;
  359. BYTE *icon_bmp;
  360. int icon_len, y;
  361. SDL_RWops *dst;
  362. /* Create temporary bitmap buffer */
  363. icon_len = 40 + icon->h * icon->w * 4;
  364. icon_bmp = SDL_stack_alloc(BYTE, icon_len);
  365. dst = SDL_RWFromMem(icon_bmp, icon_len);
  366. if (!dst) {
  367. SDL_stack_free(icon_bmp);
  368. return;
  369. }
  370. /* Write the BITMAPINFO header */
  371. SDL_WriteLE32(dst, 40);
  372. SDL_WriteLE32(dst, icon->w);
  373. SDL_WriteLE32(dst, icon->h * 2);
  374. SDL_WriteLE16(dst, 1);
  375. SDL_WriteLE16(dst, 32);
  376. SDL_WriteLE32(dst, BI_RGB);
  377. SDL_WriteLE32(dst, icon->h * icon->w * 4);
  378. SDL_WriteLE32(dst, 0);
  379. SDL_WriteLE32(dst, 0);
  380. SDL_WriteLE32(dst, 0);
  381. SDL_WriteLE32(dst, 0);
  382. /* Write the pixels upside down into the bitmap buffer */
  383. SDL_assert(icon->format->format == SDL_PIXELFORMAT_ARGB8888);
  384. y = icon->h;
  385. while (y--) {
  386. Uint8 *src = (Uint8 *) icon->pixels + y * icon->pitch;
  387. SDL_RWwrite(dst, src, icon->pitch, 1);
  388. }
  389. hicon = CreateIconFromResource(icon_bmp, icon_len, TRUE, 0x00030000);
  390. SDL_RWclose(dst);
  391. SDL_stack_free(icon_bmp);
  392. /* Set the icon for the window */
  393. SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM) hicon);
  394. /* Set the icon in the task manager (should we do this?) */
  395. SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM) hicon);
  396. }
  397. void
  398. WIN_SetWindowPosition(_THIS, SDL_Window * window)
  399. {
  400. WIN_SetWindowPositionInternal(_this, window, SWP_NOCOPYBITS | SWP_NOSIZE | SWP_NOACTIVATE);
  401. }
  402. void
  403. WIN_SetWindowSize(_THIS, SDL_Window * window)
  404. {
  405. WIN_SetWindowPositionInternal(_this, window, SWP_NOCOPYBITS | SWP_NOMOVE | SWP_NOACTIVATE);
  406. }
  407. void
  408. WIN_ShowWindow(_THIS, SDL_Window * window)
  409. {
  410. HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
  411. ShowWindow(hwnd, SW_SHOW);
  412. }
  413. void
  414. WIN_HideWindow(_THIS, SDL_Window * window)
  415. {
  416. HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
  417. ShowWindow(hwnd, SW_HIDE);
  418. }
  419. void
  420. WIN_RaiseWindow(_THIS, SDL_Window * window)
  421. {
  422. HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
  423. SetForegroundWindow(hwnd);
  424. }
  425. void
  426. WIN_MaximizeWindow(_THIS, SDL_Window * window)
  427. {
  428. SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
  429. HWND hwnd = data->hwnd;
  430. data->expected_resize = SDL_TRUE;
  431. ShowWindow(hwnd, SW_MAXIMIZE);
  432. data->expected_resize = SDL_FALSE;
  433. }
  434. void
  435. WIN_MinimizeWindow(_THIS, SDL_Window * window)
  436. {
  437. HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
  438. ShowWindow(hwnd, SW_MINIMIZE);
  439. }
  440. void
  441. WIN_SetWindowBordered(_THIS, SDL_Window * window, SDL_bool bordered)
  442. {
  443. SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
  444. HWND hwnd = data->hwnd;
  445. DWORD style = GetWindowLong(hwnd, GWL_STYLE);
  446. if (bordered) {
  447. style &= ~STYLE_BORDERLESS;
  448. style |= STYLE_NORMAL;
  449. } else {
  450. style &= ~STYLE_NORMAL;
  451. style |= STYLE_BORDERLESS;
  452. }
  453. data->in_border_change = SDL_TRUE;
  454. SetWindowLong(hwnd, GWL_STYLE, style);
  455. WIN_SetWindowPositionInternal(_this, window, SWP_NOCOPYBITS | SWP_FRAMECHANGED | SWP_NOZORDER | SWP_NOACTIVATE);
  456. data->in_border_change = SDL_FALSE;
  457. }
  458. void
  459. WIN_SetWindowResizable(_THIS, SDL_Window * window, SDL_bool resizable)
  460. {
  461. SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
  462. HWND hwnd = data->hwnd;
  463. DWORD style = GetWindowLong(hwnd, GWL_STYLE);
  464. if (resizable) {
  465. style |= STYLE_RESIZABLE;
  466. } else {
  467. style &= ~STYLE_RESIZABLE;
  468. }
  469. SetWindowLong(hwnd, GWL_STYLE, style);
  470. }
  471. void
  472. WIN_RestoreWindow(_THIS, SDL_Window * window)
  473. {
  474. SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
  475. HWND hwnd = data->hwnd;
  476. data->expected_resize = SDL_TRUE;
  477. ShowWindow(hwnd, SW_RESTORE);
  478. data->expected_resize = SDL_FALSE;
  479. }
  480. void
  481. WIN_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
  482. {
  483. SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
  484. HWND hwnd = data->hwnd;
  485. RECT rect;
  486. SDL_Rect bounds;
  487. DWORD style;
  488. HWND top;
  489. BOOL menu;
  490. int x, y;
  491. int w, h;
  492. if (SDL_ShouldAllowTopmost() && (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_INPUT_FOCUS)) == (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_INPUT_FOCUS)) {
  493. top = HWND_TOPMOST;
  494. } else {
  495. top = HWND_NOTOPMOST;
  496. }
  497. style = GetWindowLong(hwnd, GWL_STYLE);
  498. style &= ~STYLE_MASK;
  499. style |= GetWindowStyle(window);
  500. WIN_GetDisplayBounds(_this, display, &bounds);
  501. if (fullscreen) {
  502. x = bounds.x;
  503. y = bounds.y;
  504. w = bounds.w;
  505. h = bounds.h;
  506. /* Unset the maximized flag. This fixes
  507. https://bugzilla.libsdl.org/show_bug.cgi?id=3215
  508. */
  509. if (style & WS_MAXIMIZE) {
  510. data->windowed_mode_was_maximized = SDL_TRUE;
  511. style &= ~WS_MAXIMIZE;
  512. }
  513. } else {
  514. /* Restore window-maximization state, as applicable.
  515. Special care is taken to *not* do this if and when we're
  516. alt-tab'ing away (to some other window; as indicated by
  517. in_window_deactivation), otherwise
  518. https://bugzilla.libsdl.org/show_bug.cgi?id=3215 can reproduce!
  519. */
  520. if (data->windowed_mode_was_maximized && !data->in_window_deactivation) {
  521. style |= WS_MAXIMIZE;
  522. data->windowed_mode_was_maximized = SDL_FALSE;
  523. }
  524. rect.left = 0;
  525. rect.top = 0;
  526. rect.right = window->windowed.w;
  527. rect.bottom = window->windowed.h;
  528. menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
  529. AdjustWindowRectEx(&rect, style, menu, 0);
  530. w = (rect.right - rect.left);
  531. h = (rect.bottom - rect.top);
  532. x = window->windowed.x + rect.left;
  533. y = window->windowed.y + rect.top;
  534. }
  535. SetWindowLong(hwnd, GWL_STYLE, style);
  536. data->expected_resize = SDL_TRUE;
  537. SetWindowPos(hwnd, top, x, y, w, h, SWP_NOCOPYBITS | SWP_NOACTIVATE);
  538. data->expected_resize = SDL_FALSE;
  539. }
  540. int
  541. WIN_SetWindowGammaRamp(_THIS, SDL_Window * window, const Uint16 * ramp)
  542. {
  543. SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
  544. SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
  545. HDC hdc;
  546. BOOL succeeded = FALSE;
  547. hdc = CreateDC(data->DeviceName, NULL, NULL, NULL);
  548. if (hdc) {
  549. succeeded = SetDeviceGammaRamp(hdc, (LPVOID)ramp);
  550. if (!succeeded) {
  551. WIN_SetError("SetDeviceGammaRamp()");
  552. }
  553. DeleteDC(hdc);
  554. }
  555. return succeeded ? 0 : -1;
  556. }
  557. int
  558. WIN_GetWindowGammaRamp(_THIS, SDL_Window * window, Uint16 * ramp)
  559. {
  560. SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
  561. SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
  562. HDC hdc;
  563. BOOL succeeded = FALSE;
  564. hdc = CreateDC(data->DeviceName, NULL, NULL, NULL);
  565. if (hdc) {
  566. succeeded = GetDeviceGammaRamp(hdc, (LPVOID)ramp);
  567. if (!succeeded) {
  568. WIN_SetError("GetDeviceGammaRamp()");
  569. }
  570. DeleteDC(hdc);
  571. }
  572. return succeeded ? 0 : -1;
  573. }
  574. void
  575. WIN_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed)
  576. {
  577. WIN_UpdateClipCursor(window);
  578. if (window->flags & SDL_WINDOW_FULLSCREEN) {
  579. UINT flags = SWP_NOCOPYBITS | SWP_NOMOVE | SWP_NOSIZE;
  580. if (!(window->flags & SDL_WINDOW_SHOWN)) {
  581. flags |= SWP_NOACTIVATE;
  582. }
  583. WIN_SetWindowPositionInternal(_this, window, flags);
  584. }
  585. }
  586. void
  587. WIN_DestroyWindow(_THIS, SDL_Window * window)
  588. {
  589. SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
  590. if (data) {
  591. ReleaseDC(data->hwnd, data->hdc);
  592. RemoveProp(data->hwnd, TEXT("SDL_WindowData"));
  593. if (data->created) {
  594. DestroyWindow(data->hwnd);
  595. } else {
  596. /* Restore any original event handler... */
  597. if (data->wndproc != NULL) {
  598. #ifdef GWLP_WNDPROC
  599. SetWindowLongPtr(data->hwnd, GWLP_WNDPROC,
  600. (LONG_PTR) data->wndproc);
  601. #else
  602. SetWindowLong(data->hwnd, GWL_WNDPROC,
  603. (LONG_PTR) data->wndproc);
  604. #endif
  605. }
  606. }
  607. SDL_free(data);
  608. }
  609. window->driverdata = NULL;
  610. }
  611. SDL_bool
  612. WIN_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
  613. {
  614. const SDL_WindowData *data = (const SDL_WindowData *) window->driverdata;
  615. if (info->version.major <= SDL_MAJOR_VERSION) {
  616. int versionnum = SDL_VERSIONNUM(info->version.major, info->version.minor, info->version.patch);
  617. info->subsystem = SDL_SYSWM_WINDOWS;
  618. info->info.win.window = data->hwnd;
  619. if (versionnum >= SDL_VERSIONNUM(2, 0, 4)) {
  620. info->info.win.hdc = data->hdc;
  621. }
  622. return SDL_TRUE;
  623. } else {
  624. SDL_SetError("Application not compiled with SDL %d.%d\n",
  625. SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
  626. return SDL_FALSE;
  627. }
  628. }
  629. /*
  630. * Creates a HelperWindow used for DirectInput events.
  631. */
  632. int
  633. SDL_HelperWindowCreate(void)
  634. {
  635. HINSTANCE hInstance = GetModuleHandle(NULL);
  636. WNDCLASS wce;
  637. /* Make sure window isn't created twice. */
  638. if (SDL_HelperWindow != NULL) {
  639. return 0;
  640. }
  641. /* Create the class. */
  642. SDL_zero(wce);
  643. wce.lpfnWndProc = DefWindowProc;
  644. wce.lpszClassName = (LPCWSTR) SDL_HelperWindowClassName;
  645. wce.hInstance = hInstance;
  646. /* Register the class. */
  647. SDL_HelperWindowClass = RegisterClass(&wce);
  648. if (SDL_HelperWindowClass == 0 && GetLastError() != ERROR_CLASS_ALREADY_EXISTS) {
  649. return WIN_SetError("Unable to create Helper Window Class");
  650. }
  651. /* Create the window. */
  652. SDL_HelperWindow = CreateWindowEx(0, SDL_HelperWindowClassName,
  653. SDL_HelperWindowName,
  654. WS_OVERLAPPED, CW_USEDEFAULT,
  655. CW_USEDEFAULT, CW_USEDEFAULT,
  656. CW_USEDEFAULT, HWND_MESSAGE, NULL,
  657. hInstance, NULL);
  658. if (SDL_HelperWindow == NULL) {
  659. UnregisterClass(SDL_HelperWindowClassName, hInstance);
  660. return WIN_SetError("Unable to create Helper Window");
  661. }
  662. return 0;
  663. }
  664. /*
  665. * Destroys the HelperWindow previously created with SDL_HelperWindowCreate.
  666. */
  667. void
  668. SDL_HelperWindowDestroy(void)
  669. {
  670. HINSTANCE hInstance = GetModuleHandle(NULL);
  671. /* Destroy the window. */
  672. if (SDL_HelperWindow != NULL) {
  673. if (DestroyWindow(SDL_HelperWindow) == 0) {
  674. WIN_SetError("Unable to destroy Helper Window");
  675. return;
  676. }
  677. SDL_HelperWindow = NULL;
  678. }
  679. /* Unregister the class. */
  680. if (SDL_HelperWindowClass != 0) {
  681. if ((UnregisterClass(SDL_HelperWindowClassName, hInstance)) == 0) {
  682. WIN_SetError("Unable to destroy Helper Window Class");
  683. return;
  684. }
  685. SDL_HelperWindowClass = 0;
  686. }
  687. }
  688. void WIN_OnWindowEnter(_THIS, SDL_Window * window)
  689. {
  690. #ifdef WM_MOUSELEAVE
  691. SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
  692. TRACKMOUSEEVENT trackMouseEvent;
  693. if (!data || !data->hwnd) {
  694. /* The window wasn't fully initialized */
  695. return;
  696. }
  697. trackMouseEvent.cbSize = sizeof(TRACKMOUSEEVENT);
  698. trackMouseEvent.dwFlags = TME_LEAVE;
  699. trackMouseEvent.hwndTrack = data->hwnd;
  700. TrackMouseEvent(&trackMouseEvent);
  701. #endif /* WM_MOUSELEAVE */
  702. }
  703. void
  704. WIN_UpdateClipCursor(SDL_Window *window)
  705. {
  706. SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
  707. SDL_Mouse *mouse = SDL_GetMouse();
  708. if (data->focus_click_pending) {
  709. return;
  710. }
  711. if ((mouse->relative_mode || (window->flags & SDL_WINDOW_INPUT_GRABBED)) &&
  712. (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
  713. if (mouse->relative_mode && !mouse->relative_mode_warp) {
  714. LONG cx, cy;
  715. RECT rect;
  716. GetWindowRect(data->hwnd, &rect);
  717. cx = (rect.left + rect.right) / 2;
  718. cy = (rect.top + rect.bottom) / 2;
  719. /* Make an absurdly small clip rect */
  720. rect.left = cx - 1;
  721. rect.right = cx + 1;
  722. rect.top = cy - 1;
  723. rect.bottom = cy + 1;
  724. ClipCursor(&rect);
  725. } else {
  726. RECT rect;
  727. if (GetClientRect(data->hwnd, &rect) && !IsRectEmpty(&rect)) {
  728. ClientToScreen(data->hwnd, (LPPOINT) & rect);
  729. ClientToScreen(data->hwnd, (LPPOINT) & rect + 1);
  730. ClipCursor(&rect);
  731. }
  732. }
  733. } else {
  734. ClipCursor(NULL);
  735. }
  736. }
  737. int
  738. WIN_SetWindowHitTest(SDL_Window *window, SDL_bool enabled)
  739. {
  740. return 0; /* just succeed, the real work is done elsewhere. */
  741. }
  742. int
  743. WIN_SetWindowOpacity(_THIS, SDL_Window * window, float opacity)
  744. {
  745. const SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
  746. const HWND hwnd = data->hwnd;
  747. const LONG style = GetWindowLong(hwnd, GWL_EXSTYLE);
  748. SDL_assert(style != 0);
  749. if (opacity == 1.0f) {
  750. /* want it fully opaque, just mark it unlayered if necessary. */
  751. if (style & WS_EX_LAYERED) {
  752. if (SetWindowLong(hwnd, GWL_EXSTYLE, style & ~WS_EX_LAYERED) == 0) {
  753. return WIN_SetError("SetWindowLong()");
  754. }
  755. }
  756. } else {
  757. const BYTE alpha = (BYTE) ((int) (opacity * 255.0f));
  758. /* want it transparent, mark it layered if necessary. */
  759. if ((style & WS_EX_LAYERED) == 0) {
  760. if (SetWindowLong(hwnd, GWL_EXSTYLE, style | WS_EX_LAYERED) == 0) {
  761. return WIN_SetError("SetWindowLong()");
  762. }
  763. }
  764. if (SetLayeredWindowAttributes(hwnd, 0, alpha, LWA_ALPHA) == 0) {
  765. return WIN_SetError("SetLayeredWindowAttributes()");
  766. }
  767. }
  768. return 0;
  769. }
  770. #endif /* SDL_VIDEO_DRIVER_WINDOWS */
  771. /* vi: set ts=4 sw=4 expandtab: */