SDL_windowswindow.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2014 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. window->driverdata = data;
  115. /* Associate the data with the window */
  116. if (!SetProp(hwnd, TEXT("SDL_WindowData"), data)) {
  117. ReleaseDC(hwnd, data->hdc);
  118. SDL_free(data);
  119. return WIN_SetError("SetProp() failed");
  120. }
  121. /* Set up the window proc function */
  122. #ifdef GWLP_WNDPROC
  123. data->wndproc = (WNDPROC) GetWindowLongPtr(hwnd, GWLP_WNDPROC);
  124. if (data->wndproc == WIN_WindowProc) {
  125. data->wndproc = NULL;
  126. } else {
  127. SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR) WIN_WindowProc);
  128. }
  129. #else
  130. data->wndproc = (WNDPROC) GetWindowLong(hwnd, GWL_WNDPROC);
  131. if (data->wndproc == WIN_WindowProc) {
  132. data->wndproc = NULL;
  133. } else {
  134. SetWindowLong(hwnd, GWL_WNDPROC, (LONG_PTR) WIN_WindowProc);
  135. }
  136. #endif
  137. /* Fill in the SDL window with the window data */
  138. {
  139. RECT rect;
  140. if (GetClientRect(hwnd, &rect)) {
  141. int w = rect.right;
  142. int h = rect.bottom;
  143. if ((window->w && window->w != w) || (window->h && window->h != h)) {
  144. /* We tried to create a window larger than the desktop and Windows didn't allow it. Override! */
  145. WIN_SetWindowPositionInternal(_this, window, SWP_NOCOPYBITS | SWP_NOZORDER | SWP_NOACTIVATE);
  146. } else {
  147. window->w = w;
  148. window->h = h;
  149. }
  150. }
  151. }
  152. {
  153. POINT point;
  154. point.x = 0;
  155. point.y = 0;
  156. if (ClientToScreen(hwnd, &point)) {
  157. window->x = point.x;
  158. window->y = point.y;
  159. }
  160. }
  161. {
  162. DWORD style = GetWindowLong(hwnd, GWL_STYLE);
  163. if (style & WS_VISIBLE) {
  164. window->flags |= SDL_WINDOW_SHOWN;
  165. } else {
  166. window->flags &= ~SDL_WINDOW_SHOWN;
  167. }
  168. if (style & (WS_BORDER | WS_THICKFRAME)) {
  169. window->flags &= ~SDL_WINDOW_BORDERLESS;
  170. } else {
  171. window->flags |= SDL_WINDOW_BORDERLESS;
  172. }
  173. if (style & WS_THICKFRAME) {
  174. window->flags |= SDL_WINDOW_RESIZABLE;
  175. } else {
  176. window->flags &= ~SDL_WINDOW_RESIZABLE;
  177. }
  178. #ifdef WS_MAXIMIZE
  179. if (style & WS_MAXIMIZE) {
  180. window->flags |= SDL_WINDOW_MAXIMIZED;
  181. } else
  182. #endif
  183. {
  184. window->flags &= ~SDL_WINDOW_MAXIMIZED;
  185. }
  186. #ifdef WS_MINIMIZE
  187. if (style & WS_MINIMIZE) {
  188. window->flags |= SDL_WINDOW_MINIMIZED;
  189. } else
  190. #endif
  191. {
  192. window->flags &= ~SDL_WINDOW_MINIMIZED;
  193. }
  194. }
  195. if (GetFocus() == hwnd) {
  196. window->flags |= SDL_WINDOW_INPUT_FOCUS;
  197. SDL_SetKeyboardFocus(data->window);
  198. if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
  199. RECT rect;
  200. GetClientRect(hwnd, &rect);
  201. ClientToScreen(hwnd, (LPPOINT) & rect);
  202. ClientToScreen(hwnd, (LPPOINT) & rect + 1);
  203. ClipCursor(&rect);
  204. }
  205. }
  206. /* Enable multi-touch */
  207. if (videodata->RegisterTouchWindow) {
  208. videodata->RegisterTouchWindow(hwnd, (TWF_FINETOUCH|TWF_WANTPALM));
  209. }
  210. /* Enable dropping files */
  211. DragAcceptFiles(hwnd, TRUE);
  212. /* All done! */
  213. return 0;
  214. }
  215. int
  216. WIN_CreateWindow(_THIS, SDL_Window * window)
  217. {
  218. HWND hwnd;
  219. RECT rect;
  220. DWORD style = STYLE_BASIC;
  221. int x, y;
  222. int w, h;
  223. style |= GetWindowStyle(window);
  224. /* Figure out what the window area will be */
  225. rect.left = window->x;
  226. rect.top = window->y;
  227. rect.right = window->x + window->w;
  228. rect.bottom = window->y + window->h;
  229. AdjustWindowRectEx(&rect, style, FALSE, 0);
  230. x = rect.left;
  231. y = rect.top;
  232. w = (rect.right - rect.left);
  233. h = (rect.bottom - rect.top);
  234. hwnd =
  235. CreateWindow(SDL_Appname, TEXT(""), style, x, y, w, h, NULL, NULL,
  236. SDL_Instance, NULL);
  237. if (!hwnd) {
  238. return WIN_SetError("Couldn't create window");
  239. }
  240. WIN_PumpEvents(_this);
  241. if (SetupWindowData(_this, window, hwnd, SDL_TRUE) < 0) {
  242. DestroyWindow(hwnd);
  243. return -1;
  244. }
  245. #if SDL_VIDEO_OPENGL_WGL
  246. /* We need to initialize the extensions before deciding how to create ES profiles */
  247. if (window->flags & SDL_WINDOW_OPENGL) {
  248. WIN_GL_InitExtensions(_this);
  249. }
  250. #endif
  251. #if SDL_VIDEO_OPENGL_ES2
  252. if ((window->flags & SDL_WINDOW_OPENGL) &&
  253. _this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES
  254. #if SDL_VIDEO_OPENGL_WGL
  255. && (!_this->gl_data || !_this->gl_data->HAS_WGL_EXT_create_context_es2_profile)
  256. #endif
  257. ) {
  258. #if SDL_VIDEO_OPENGL_EGL
  259. if (WIN_GLES_SetupWindow(_this, window) < 0) {
  260. WIN_DestroyWindow(_this, window);
  261. return -1;
  262. }
  263. #else
  264. return SDL_SetError("Could not create GLES window surface (no EGL support available)");
  265. #endif /* SDL_VIDEO_OPENGL_EGL */
  266. } else
  267. #endif /* SDL_VIDEO_OPENGL_ES2 */
  268. #if SDL_VIDEO_OPENGL_WGL
  269. if (window->flags & SDL_WINDOW_OPENGL) {
  270. if (WIN_GL_SetupWindow(_this, window) < 0) {
  271. WIN_DestroyWindow(_this, window);
  272. return -1;
  273. }
  274. }
  275. #endif
  276. return 0;
  277. }
  278. int
  279. WIN_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
  280. {
  281. HWND hwnd = (HWND) data;
  282. LPTSTR title;
  283. int titleLen;
  284. /* Query the title from the existing window */
  285. titleLen = GetWindowTextLength(hwnd);
  286. title = SDL_stack_alloc(TCHAR, titleLen + 1);
  287. if (title) {
  288. titleLen = GetWindowText(hwnd, title, titleLen);
  289. } else {
  290. titleLen = 0;
  291. }
  292. if (titleLen > 0) {
  293. window->title = WIN_StringToUTF8(title);
  294. }
  295. if (title) {
  296. SDL_stack_free(title);
  297. }
  298. if (SetupWindowData(_this, window, hwnd, SDL_FALSE) < 0) {
  299. return -1;
  300. }
  301. #if SDL_VIDEO_OPENGL_WGL
  302. {
  303. const char *hint = SDL_GetHint(SDL_HINT_VIDEO_WINDOW_SHARE_PIXEL_FORMAT);
  304. if (hint) {
  305. /* This hint is a pointer (in string form) of the address of
  306. the window to share a pixel format with
  307. */
  308. SDL_Window *otherWindow = NULL;
  309. SDL_sscanf(hint, "%p", (void**)&otherWindow);
  310. /* Do some error checking on the pointer */
  311. if (otherWindow != NULL && otherWindow->magic == &_this->window_magic)
  312. {
  313. /* If the otherWindow has SDL_WINDOW_OPENGL set, set it for the new window as well */
  314. if (otherWindow->flags & SDL_WINDOW_OPENGL)
  315. {
  316. window->flags |= SDL_WINDOW_OPENGL;
  317. if(!WIN_GL_SetPixelFormatFrom(_this, otherWindow, window)) {
  318. return -1;
  319. }
  320. }
  321. }
  322. }
  323. }
  324. #endif
  325. return 0;
  326. }
  327. void
  328. WIN_SetWindowTitle(_THIS, SDL_Window * window)
  329. {
  330. HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
  331. LPTSTR title;
  332. if (window->title) {
  333. title = WIN_UTF8ToString(window->title);
  334. } else {
  335. title = NULL;
  336. }
  337. SetWindowText(hwnd, title ? title : TEXT(""));
  338. SDL_free(title);
  339. }
  340. void
  341. WIN_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
  342. {
  343. HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
  344. HICON hicon = NULL;
  345. BYTE *icon_bmp;
  346. int icon_len, y;
  347. SDL_RWops *dst;
  348. /* Create temporary bitmap buffer */
  349. icon_len = 40 + icon->h * icon->w * 4;
  350. icon_bmp = SDL_stack_alloc(BYTE, icon_len);
  351. dst = SDL_RWFromMem(icon_bmp, icon_len);
  352. if (!dst) {
  353. SDL_stack_free(icon_bmp);
  354. return;
  355. }
  356. /* Write the BITMAPINFO header */
  357. SDL_WriteLE32(dst, 40);
  358. SDL_WriteLE32(dst, icon->w);
  359. SDL_WriteLE32(dst, icon->h * 2);
  360. SDL_WriteLE16(dst, 1);
  361. SDL_WriteLE16(dst, 32);
  362. SDL_WriteLE32(dst, BI_RGB);
  363. SDL_WriteLE32(dst, icon->h * icon->w * 4);
  364. SDL_WriteLE32(dst, 0);
  365. SDL_WriteLE32(dst, 0);
  366. SDL_WriteLE32(dst, 0);
  367. SDL_WriteLE32(dst, 0);
  368. /* Write the pixels upside down into the bitmap buffer */
  369. SDL_assert(icon->format->format == SDL_PIXELFORMAT_ARGB8888);
  370. y = icon->h;
  371. while (y--) {
  372. Uint8 *src = (Uint8 *) icon->pixels + y * icon->pitch;
  373. SDL_RWwrite(dst, src, icon->pitch, 1);
  374. }
  375. hicon = CreateIconFromResource(icon_bmp, icon_len, TRUE, 0x00030000);
  376. SDL_RWclose(dst);
  377. SDL_stack_free(icon_bmp);
  378. /* Set the icon for the window */
  379. SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM) hicon);
  380. /* Set the icon in the task manager (should we do this?) */
  381. SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM) hicon);
  382. }
  383. void
  384. WIN_SetWindowPosition(_THIS, SDL_Window * window)
  385. {
  386. WIN_SetWindowPositionInternal(_this, window, SWP_NOCOPYBITS | SWP_NOSIZE | SWP_NOACTIVATE);
  387. }
  388. void
  389. WIN_SetWindowSize(_THIS, SDL_Window * window)
  390. {
  391. WIN_SetWindowPositionInternal(_this, window, SWP_NOCOPYBITS | SWP_NOMOVE | SWP_NOACTIVATE);
  392. }
  393. void
  394. WIN_ShowWindow(_THIS, SDL_Window * window)
  395. {
  396. HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
  397. ShowWindow(hwnd, SW_SHOW);
  398. }
  399. void
  400. WIN_HideWindow(_THIS, SDL_Window * window)
  401. {
  402. HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
  403. ShowWindow(hwnd, SW_HIDE);
  404. }
  405. void
  406. WIN_RaiseWindow(_THIS, SDL_Window * window)
  407. {
  408. WIN_SetWindowPositionInternal(_this, window, SWP_NOCOPYBITS | SWP_NOMOVE | SWP_NOSIZE);
  409. }
  410. void
  411. WIN_MaximizeWindow(_THIS, SDL_Window * window)
  412. {
  413. SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
  414. HWND hwnd = data->hwnd;
  415. data->expected_resize = SDL_TRUE;
  416. ShowWindow(hwnd, SW_MAXIMIZE);
  417. data->expected_resize = SDL_FALSE;
  418. }
  419. void
  420. WIN_MinimizeWindow(_THIS, SDL_Window * window)
  421. {
  422. HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
  423. ShowWindow(hwnd, SW_MINIMIZE);
  424. }
  425. void
  426. WIN_SetWindowBordered(_THIS, SDL_Window * window, SDL_bool bordered)
  427. {
  428. SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
  429. HWND hwnd = data->hwnd;
  430. DWORD style = GetWindowLong(hwnd, GWL_STYLE);
  431. if (bordered) {
  432. style &= ~STYLE_BORDERLESS;
  433. style |= STYLE_NORMAL;
  434. } else {
  435. style &= ~STYLE_NORMAL;
  436. style |= STYLE_BORDERLESS;
  437. }
  438. data->in_border_change = SDL_TRUE;
  439. SetWindowLong( hwnd, GWL_STYLE, style );
  440. WIN_SetWindowPositionInternal(_this, window, SWP_NOCOPYBITS | SWP_FRAMECHANGED | SWP_NOZORDER | SWP_NOACTIVATE);
  441. data->in_border_change = SDL_FALSE;
  442. }
  443. void
  444. WIN_RestoreWindow(_THIS, SDL_Window * window)
  445. {
  446. SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
  447. HWND hwnd = data->hwnd;
  448. data->expected_resize = SDL_TRUE;
  449. ShowWindow(hwnd, SW_RESTORE);
  450. data->expected_resize = SDL_FALSE;
  451. }
  452. void
  453. WIN_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
  454. {
  455. SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
  456. HWND hwnd = data->hwnd;
  457. RECT rect;
  458. SDL_Rect bounds;
  459. DWORD style;
  460. HWND top;
  461. BOOL menu;
  462. int x, y;
  463. int w, h;
  464. if (SDL_ShouldAllowTopmost() && (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_INPUT_FOCUS)) == (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_INPUT_FOCUS)) {
  465. top = HWND_TOPMOST;
  466. } else {
  467. top = HWND_NOTOPMOST;
  468. }
  469. style = GetWindowLong(hwnd, GWL_STYLE);
  470. style &= ~STYLE_MASK;
  471. style |= GetWindowStyle(window);
  472. WIN_GetDisplayBounds(_this, display, &bounds);
  473. if (fullscreen) {
  474. x = bounds.x;
  475. y = bounds.y;
  476. w = bounds.w;
  477. h = bounds.h;
  478. } else {
  479. rect.left = 0;
  480. rect.top = 0;
  481. rect.right = window->windowed.w;
  482. rect.bottom = window->windowed.h;
  483. menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
  484. AdjustWindowRectEx(&rect, style, menu, 0);
  485. w = (rect.right - rect.left);
  486. h = (rect.bottom - rect.top);
  487. x = window->windowed.x + rect.left;
  488. y = window->windowed.y + rect.top;
  489. }
  490. SetWindowLong(hwnd, GWL_STYLE, style);
  491. data->expected_resize = SDL_TRUE;
  492. SetWindowPos(hwnd, top, x, y, w, h, SWP_NOCOPYBITS | SWP_NOACTIVATE);
  493. data->expected_resize = SDL_FALSE;
  494. }
  495. int
  496. WIN_SetWindowGammaRamp(_THIS, SDL_Window * window, const Uint16 * ramp)
  497. {
  498. SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
  499. SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
  500. HDC hdc;
  501. BOOL succeeded = FALSE;
  502. hdc = CreateDC(data->DeviceName, NULL, NULL, NULL);
  503. if (hdc) {
  504. succeeded = SetDeviceGammaRamp(hdc, (LPVOID)ramp);
  505. if (!succeeded) {
  506. WIN_SetError("SetDeviceGammaRamp()");
  507. }
  508. DeleteDC(hdc);
  509. }
  510. return succeeded ? 0 : -1;
  511. }
  512. int
  513. WIN_GetWindowGammaRamp(_THIS, SDL_Window * window, Uint16 * ramp)
  514. {
  515. SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
  516. SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
  517. HDC hdc;
  518. BOOL succeeded = FALSE;
  519. hdc = CreateDC(data->DeviceName, NULL, NULL, NULL);
  520. if (hdc) {
  521. succeeded = GetDeviceGammaRamp(hdc, (LPVOID)ramp);
  522. if (!succeeded) {
  523. WIN_SetError("GetDeviceGammaRamp()");
  524. }
  525. DeleteDC(hdc);
  526. }
  527. return succeeded ? 0 : -1;
  528. }
  529. void
  530. WIN_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed)
  531. {
  532. WIN_UpdateClipCursor(window);
  533. if (window->flags & SDL_WINDOW_FULLSCREEN) {
  534. UINT flags = SWP_NOCOPYBITS | SWP_NOMOVE | SWP_NOSIZE;
  535. if (!(window->flags & SDL_WINDOW_SHOWN)) {
  536. flags |= SWP_NOACTIVATE;
  537. }
  538. WIN_SetWindowPositionInternal(_this, window, flags);
  539. }
  540. }
  541. void
  542. WIN_DestroyWindow(_THIS, SDL_Window * window)
  543. {
  544. SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
  545. window->driverdata = NULL;
  546. if (data) {
  547. ReleaseDC(data->hwnd, data->hdc);
  548. if (data->created) {
  549. DestroyWindow(data->hwnd);
  550. } else {
  551. /* Restore any original event handler... */
  552. if (data->wndproc != NULL) {
  553. #ifdef GWLP_WNDPROC
  554. SetWindowLongPtr(data->hwnd, GWLP_WNDPROC,
  555. (LONG_PTR) data->wndproc);
  556. #else
  557. SetWindowLong(data->hwnd, GWL_WNDPROC,
  558. (LONG_PTR) data->wndproc);
  559. #endif
  560. }
  561. }
  562. SDL_free(data);
  563. }
  564. }
  565. SDL_bool
  566. WIN_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
  567. {
  568. HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
  569. if (info->version.major <= SDL_MAJOR_VERSION) {
  570. info->subsystem = SDL_SYSWM_WINDOWS;
  571. info->info.win.window = hwnd;
  572. return SDL_TRUE;
  573. } else {
  574. SDL_SetError("Application not compiled with SDL %d.%d\n",
  575. SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
  576. return SDL_FALSE;
  577. }
  578. }
  579. /*
  580. * Creates a HelperWindow used for DirectInput events.
  581. */
  582. int
  583. SDL_HelperWindowCreate(void)
  584. {
  585. HINSTANCE hInstance = GetModuleHandle(NULL);
  586. WNDCLASS wce;
  587. /* Make sure window isn't created twice. */
  588. if (SDL_HelperWindow != NULL) {
  589. return 0;
  590. }
  591. /* Create the class. */
  592. SDL_zero(wce);
  593. wce.lpfnWndProc = DefWindowProc;
  594. wce.lpszClassName = (LPCWSTR) SDL_HelperWindowClassName;
  595. wce.hInstance = hInstance;
  596. /* Register the class. */
  597. SDL_HelperWindowClass = RegisterClass(&wce);
  598. if (SDL_HelperWindowClass == 0 && GetLastError() != ERROR_CLASS_ALREADY_EXISTS) {
  599. return WIN_SetError("Unable to create Helper Window Class");
  600. }
  601. /* Create the window. */
  602. SDL_HelperWindow = CreateWindowEx(0, SDL_HelperWindowClassName,
  603. SDL_HelperWindowName,
  604. WS_OVERLAPPED, CW_USEDEFAULT,
  605. CW_USEDEFAULT, CW_USEDEFAULT,
  606. CW_USEDEFAULT, HWND_MESSAGE, NULL,
  607. hInstance, NULL);
  608. if (SDL_HelperWindow == NULL) {
  609. UnregisterClass(SDL_HelperWindowClassName, hInstance);
  610. return WIN_SetError("Unable to create Helper Window");
  611. }
  612. return 0;
  613. }
  614. /*
  615. * Destroys the HelperWindow previously created with SDL_HelperWindowCreate.
  616. */
  617. void
  618. SDL_HelperWindowDestroy(void)
  619. {
  620. HINSTANCE hInstance = GetModuleHandle(NULL);
  621. /* Destroy the window. */
  622. if (SDL_HelperWindow != NULL) {
  623. if (DestroyWindow(SDL_HelperWindow) == 0) {
  624. WIN_SetError("Unable to destroy Helper Window");
  625. return;
  626. }
  627. SDL_HelperWindow = NULL;
  628. }
  629. /* Unregister the class. */
  630. if (SDL_HelperWindowClass != 0) {
  631. if ((UnregisterClass(SDL_HelperWindowClassName, hInstance)) == 0) {
  632. WIN_SetError("Unable to destroy Helper Window Class");
  633. return;
  634. }
  635. SDL_HelperWindowClass = 0;
  636. }
  637. }
  638. void WIN_OnWindowEnter(_THIS, SDL_Window * window)
  639. {
  640. #ifdef WM_MOUSELEAVE
  641. SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
  642. TRACKMOUSEEVENT trackMouseEvent;
  643. if (!data || !data->hwnd) {
  644. /* The window wasn't fully initialized */
  645. return;
  646. }
  647. trackMouseEvent.cbSize = sizeof(TRACKMOUSEEVENT);
  648. trackMouseEvent.dwFlags = TME_LEAVE;
  649. trackMouseEvent.hwndTrack = data->hwnd;
  650. TrackMouseEvent(&trackMouseEvent);
  651. #endif /* WM_MOUSELEAVE */
  652. }
  653. void
  654. WIN_UpdateClipCursor(SDL_Window *window)
  655. {
  656. SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
  657. SDL_Mouse *mouse = SDL_GetMouse();
  658. /* Don't clip the cursor while we're in the modal resize or move loop */
  659. if (data->in_title_click || data->in_modal_loop) {
  660. ClipCursor(NULL);
  661. return;
  662. }
  663. if ((mouse->relative_mode || (window->flags & SDL_WINDOW_INPUT_GRABBED)) &&
  664. (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
  665. if (mouse->relative_mode && !mouse->relative_mode_warp) {
  666. LONG cx, cy;
  667. RECT rect;
  668. GetWindowRect(data->hwnd, &rect);
  669. cx = (rect.left + rect.right) / 2;
  670. cy = (rect.top + rect.bottom) / 2;
  671. /* Make an absurdly small clip rect */
  672. rect.left = cx - 1;
  673. rect.right = cx + 1;
  674. rect.top = cy - 1;
  675. rect.bottom = cy + 1;
  676. ClipCursor(&rect);
  677. } else {
  678. RECT rect;
  679. if (GetClientRect(data->hwnd, &rect) && !IsRectEmpty(&rect)) {
  680. ClientToScreen(data->hwnd, (LPPOINT) & rect);
  681. ClientToScreen(data->hwnd, (LPPOINT) & rect + 1);
  682. ClipCursor(&rect);
  683. }
  684. }
  685. } else {
  686. ClipCursor(NULL);
  687. }
  688. }
  689. #endif /* SDL_VIDEO_DRIVER_WINDOWS */
  690. /* vi: set ts=4 sw=4 expandtab: */