SDL_windowswindow.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2013 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_config.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 "SDL_windowsvideo.h"
  26. #include "SDL_windowswindow.h"
  27. /* Dropfile support */
  28. #include <shellapi.h>
  29. /* This is included after SDL_windowsvideo.h, which includes windows.h */
  30. #include "SDL_syswm.h"
  31. /* Windows CE compatibility */
  32. #ifndef SWP_NOCOPYBITS
  33. #define SWP_NOCOPYBITS 0
  34. #endif
  35. /* Fake window to help with DirectInput events. */
  36. HWND SDL_HelperWindow = NULL;
  37. static WCHAR *SDL_HelperWindowClassName = TEXT("SDLHelperWindowInputCatcher");
  38. static WCHAR *SDL_HelperWindowName = TEXT("SDLHelperWindowInputMsgWindow");
  39. static ATOM SDL_HelperWindowClass = 0;
  40. #define STYLE_BASIC (WS_CLIPSIBLINGS | WS_CLIPCHILDREN)
  41. #define STYLE_FULLSCREEN (WS_POPUP)
  42. #define STYLE_BORDERLESS (WS_POPUP)
  43. #define STYLE_NORMAL (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX)
  44. #define STYLE_RESIZABLE (WS_THICKFRAME | WS_MAXIMIZEBOX)
  45. #define STYLE_MASK (STYLE_FULLSCREEN | STYLE_BORDERLESS | STYLE_NORMAL | STYLE_RESIZABLE)
  46. static DWORD
  47. GetWindowStyle(SDL_Window * window)
  48. {
  49. DWORD style = 0;
  50. if (window->flags & SDL_WINDOW_FULLSCREEN) {
  51. style |= STYLE_FULLSCREEN;
  52. } else {
  53. if (window->flags & SDL_WINDOW_BORDERLESS) {
  54. style |= STYLE_BORDERLESS;
  55. } else {
  56. style |= STYLE_NORMAL;
  57. }
  58. if (window->flags & SDL_WINDOW_RESIZABLE) {
  59. style |= STYLE_RESIZABLE;
  60. }
  61. }
  62. return style;
  63. }
  64. static void
  65. WIN_SetWindowPositionInternal(_THIS, SDL_Window * window, UINT flags)
  66. {
  67. HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
  68. RECT rect;
  69. DWORD style;
  70. HWND top;
  71. BOOL menu;
  72. int x, y;
  73. int w, h;
  74. /* Figure out what the window area will be */
  75. if (SDL_ShouldAllowTopmost() && (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_INPUT_FOCUS)) == (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_INPUT_FOCUS)) {
  76. top = HWND_TOPMOST;
  77. } else {
  78. top = HWND_NOTOPMOST;
  79. }
  80. style = GetWindowLong(hwnd, GWL_STYLE);
  81. rect.left = 0;
  82. rect.top = 0;
  83. rect.right = window->w;
  84. rect.bottom = window->h;
  85. menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
  86. AdjustWindowRectEx(&rect, style, menu, 0);
  87. w = (rect.right - rect.left);
  88. h = (rect.bottom - rect.top);
  89. x = window->x + rect.left;
  90. y = window->y + rect.top;
  91. SetWindowPos(hwnd, top, x, y, w, h, flags);
  92. }
  93. static int
  94. SetupWindowData(_THIS, SDL_Window * window, HWND hwnd, SDL_bool created)
  95. {
  96. SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
  97. SDL_WindowData *data;
  98. /* Allocate the window data */
  99. data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
  100. if (!data) {
  101. return SDL_OutOfMemory();
  102. }
  103. data->window = window;
  104. data->hwnd = hwnd;
  105. data->hdc = GetDC(hwnd);
  106. data->created = created;
  107. data->mouse_button_flags = 0;
  108. data->videodata = videodata;
  109. window->driverdata = data;
  110. /* Associate the data with the window */
  111. if (!SetProp(hwnd, TEXT("SDL_WindowData"), data)) {
  112. ReleaseDC(hwnd, data->hdc);
  113. SDL_free(data);
  114. return WIN_SetError("SetProp() failed");
  115. }
  116. /* Set up the window proc function */
  117. #ifdef GWLP_WNDPROC
  118. data->wndproc = (WNDPROC) GetWindowLongPtr(hwnd, GWLP_WNDPROC);
  119. if (data->wndproc == WIN_WindowProc) {
  120. data->wndproc = NULL;
  121. } else {
  122. SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR) WIN_WindowProc);
  123. }
  124. #else
  125. data->wndproc = (WNDPROC) GetWindowLong(hwnd, GWL_WNDPROC);
  126. if (data->wndproc == WIN_WindowProc) {
  127. data->wndproc = NULL;
  128. } else {
  129. SetWindowLong(hwnd, GWL_WNDPROC, (LONG_PTR) WIN_WindowProc);
  130. }
  131. #endif
  132. /* Fill in the SDL window with the window data */
  133. {
  134. RECT rect;
  135. if (GetClientRect(hwnd, &rect)) {
  136. int w = rect.right;
  137. int h = rect.bottom;
  138. if ((window->w && window->w != w) || (window->h && window->h != h)) {
  139. /* We tried to create a window larger than the desktop and Windows didn't allow it. Override! */
  140. WIN_SetWindowPositionInternal(_this, window, SWP_NOCOPYBITS | SWP_NOZORDER | SWP_NOACTIVATE);
  141. } else {
  142. window->w = w;
  143. window->h = h;
  144. }
  145. }
  146. }
  147. {
  148. POINT point;
  149. point.x = 0;
  150. point.y = 0;
  151. if (ClientToScreen(hwnd, &point)) {
  152. window->x = point.x;
  153. window->y = point.y;
  154. }
  155. }
  156. {
  157. DWORD style = GetWindowLong(hwnd, GWL_STYLE);
  158. if (style & WS_VISIBLE) {
  159. window->flags |= SDL_WINDOW_SHOWN;
  160. } else {
  161. window->flags &= ~SDL_WINDOW_SHOWN;
  162. }
  163. if (style & (WS_BORDER | WS_THICKFRAME)) {
  164. window->flags &= ~SDL_WINDOW_BORDERLESS;
  165. } else {
  166. window->flags |= SDL_WINDOW_BORDERLESS;
  167. }
  168. if (style & WS_THICKFRAME) {
  169. window->flags |= SDL_WINDOW_RESIZABLE;
  170. } else {
  171. window->flags &= ~SDL_WINDOW_RESIZABLE;
  172. }
  173. #ifdef WS_MAXIMIZE
  174. if (style & WS_MAXIMIZE) {
  175. window->flags |= SDL_WINDOW_MAXIMIZED;
  176. } else
  177. #endif
  178. {
  179. window->flags &= ~SDL_WINDOW_MAXIMIZED;
  180. }
  181. #ifdef WS_MINIMIZE
  182. if (style & WS_MINIMIZE) {
  183. window->flags |= SDL_WINDOW_MINIMIZED;
  184. } else
  185. #endif
  186. {
  187. window->flags &= ~SDL_WINDOW_MINIMIZED;
  188. }
  189. }
  190. if (GetFocus() == hwnd) {
  191. window->flags |= SDL_WINDOW_INPUT_FOCUS;
  192. SDL_SetKeyboardFocus(data->window);
  193. if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
  194. RECT rect;
  195. GetClientRect(hwnd, &rect);
  196. ClientToScreen(hwnd, (LPPOINT) & rect);
  197. ClientToScreen(hwnd, (LPPOINT) & rect + 1);
  198. ClipCursor(&rect);
  199. }
  200. }
  201. /* Enable multi-touch */
  202. if (videodata->RegisterTouchWindow) {
  203. videodata->RegisterTouchWindow(hwnd, (TWF_FINETOUCH|TWF_WANTPALM));
  204. }
  205. /* Enable dropping files */
  206. DragAcceptFiles(hwnd, TRUE);
  207. /* All done! */
  208. return 0;
  209. }
  210. int
  211. WIN_CreateWindow(_THIS, SDL_Window * window)
  212. {
  213. HWND hwnd;
  214. RECT rect;
  215. DWORD style = STYLE_BASIC;
  216. int x, y;
  217. int w, h;
  218. style |= GetWindowStyle(window);
  219. /* Figure out what the window area will be */
  220. rect.left = window->x;
  221. rect.top = window->y;
  222. rect.right = window->x + window->w;
  223. rect.bottom = window->y + window->h;
  224. AdjustWindowRectEx(&rect, style, FALSE, 0);
  225. x = rect.left;
  226. y = rect.top;
  227. w = (rect.right - rect.left);
  228. h = (rect.bottom - rect.top);
  229. hwnd =
  230. CreateWindow(SDL_Appname, TEXT(""), style, x, y, w, h, NULL, NULL,
  231. SDL_Instance, NULL);
  232. if (!hwnd) {
  233. return WIN_SetError("Couldn't create window");
  234. }
  235. WIN_PumpEvents(_this);
  236. if (SetupWindowData(_this, window, hwnd, SDL_TRUE) < 0) {
  237. DestroyWindow(hwnd);
  238. return -1;
  239. }
  240. #if SDL_VIDEO_OPENGL_WGL
  241. if (window->flags & SDL_WINDOW_OPENGL) {
  242. if (WIN_GL_SetupWindow(_this, window) < 0) {
  243. WIN_DestroyWindow(_this, window);
  244. return -1;
  245. }
  246. }
  247. #endif
  248. return 0;
  249. }
  250. int
  251. WIN_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
  252. {
  253. HWND hwnd = (HWND) data;
  254. LPTSTR title;
  255. int titleLen;
  256. /* Query the title from the existing window */
  257. titleLen = GetWindowTextLength(hwnd);
  258. title = SDL_stack_alloc(TCHAR, titleLen + 1);
  259. if (title) {
  260. titleLen = GetWindowText(hwnd, title, titleLen);
  261. } else {
  262. titleLen = 0;
  263. }
  264. if (titleLen > 0) {
  265. window->title = WIN_StringToUTF8(title);
  266. }
  267. if (title) {
  268. SDL_stack_free(title);
  269. }
  270. if (SetupWindowData(_this, window, hwnd, SDL_FALSE) < 0) {
  271. return -1;
  272. }
  273. return 0;
  274. }
  275. void
  276. WIN_SetWindowTitle(_THIS, SDL_Window * window)
  277. {
  278. HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
  279. LPTSTR title;
  280. if (window->title) {
  281. title = WIN_UTF8ToString(window->title);
  282. } else {
  283. title = NULL;
  284. }
  285. SetWindowText(hwnd, title ? title : TEXT(""));
  286. SDL_free(title);
  287. }
  288. void
  289. WIN_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
  290. {
  291. HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
  292. HICON hicon = NULL;
  293. BYTE *icon_bmp;
  294. int icon_len, y;
  295. SDL_RWops *dst;
  296. /* Create temporary bitmap buffer */
  297. icon_len = 40 + icon->h * icon->w * 4;
  298. icon_bmp = SDL_stack_alloc(BYTE, icon_len);
  299. dst = SDL_RWFromMem(icon_bmp, icon_len);
  300. if (!dst) {
  301. SDL_stack_free(icon_bmp);
  302. return;
  303. }
  304. /* Write the BITMAPINFO header */
  305. SDL_WriteLE32(dst, 40);
  306. SDL_WriteLE32(dst, icon->w);
  307. SDL_WriteLE32(dst, icon->h * 2);
  308. SDL_WriteLE16(dst, 1);
  309. SDL_WriteLE16(dst, 32);
  310. SDL_WriteLE32(dst, BI_RGB);
  311. SDL_WriteLE32(dst, icon->h * icon->w * 4);
  312. SDL_WriteLE32(dst, 0);
  313. SDL_WriteLE32(dst, 0);
  314. SDL_WriteLE32(dst, 0);
  315. SDL_WriteLE32(dst, 0);
  316. /* Write the pixels upside down into the bitmap buffer */
  317. SDL_assert(icon->format->format == SDL_PIXELFORMAT_ARGB8888);
  318. y = icon->h;
  319. while (y--) {
  320. Uint8 *src = (Uint8 *) icon->pixels + y * icon->pitch;
  321. SDL_RWwrite(dst, src, icon->pitch, 1);
  322. }
  323. hicon = CreateIconFromResource(icon_bmp, icon_len, TRUE, 0x00030000);
  324. SDL_RWclose(dst);
  325. SDL_stack_free(icon_bmp);
  326. /* Set the icon for the window */
  327. SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM) hicon);
  328. /* Set the icon in the task manager (should we do this?) */
  329. SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM) hicon);
  330. }
  331. void
  332. WIN_SetWindowPosition(_THIS, SDL_Window * window)
  333. {
  334. WIN_SetWindowPositionInternal(_this, window, SWP_NOCOPYBITS | SWP_NOSIZE | SWP_NOACTIVATE);
  335. }
  336. void
  337. WIN_SetWindowSize(_THIS, SDL_Window * window)
  338. {
  339. WIN_SetWindowPositionInternal(_this, window, SWP_NOCOPYBITS | SWP_NOMOVE | SWP_NOACTIVATE);
  340. }
  341. void
  342. WIN_ShowWindow(_THIS, SDL_Window * window)
  343. {
  344. HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
  345. ShowWindow(hwnd, SW_SHOW);
  346. }
  347. void
  348. WIN_HideWindow(_THIS, SDL_Window * window)
  349. {
  350. HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
  351. ShowWindow(hwnd, SW_HIDE);
  352. }
  353. void
  354. WIN_RaiseWindow(_THIS, SDL_Window * window)
  355. {
  356. WIN_SetWindowPositionInternal(_this, window, SWP_NOCOPYBITS | SWP_NOMOVE | SWP_NOSIZE);
  357. }
  358. void
  359. WIN_MaximizeWindow(_THIS, SDL_Window * window)
  360. {
  361. HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
  362. ShowWindow(hwnd, SW_MAXIMIZE);
  363. }
  364. void
  365. WIN_MinimizeWindow(_THIS, SDL_Window * window)
  366. {
  367. HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
  368. ShowWindow(hwnd, SW_MINIMIZE);
  369. }
  370. void
  371. WIN_SetWindowBordered(_THIS, SDL_Window * window, SDL_bool bordered)
  372. {
  373. HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
  374. DWORD style = GetWindowLong(hwnd, GWL_STYLE);
  375. if (bordered) {
  376. style &= ~STYLE_BORDERLESS;
  377. style |= STYLE_NORMAL;
  378. } else {
  379. style &= ~STYLE_NORMAL;
  380. style |= STYLE_BORDERLESS;
  381. }
  382. SetWindowLong(hwnd, GWL_STYLE, style);
  383. WIN_SetWindowPositionInternal(_this, window, SWP_NOCOPYBITS | SWP_FRAMECHANGED | SWP_NOREPOSITION | SWP_NOZORDER |SWP_NOACTIVATE | SWP_NOSENDCHANGING);
  384. }
  385. void
  386. WIN_RestoreWindow(_THIS, SDL_Window * window)
  387. {
  388. HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
  389. ShowWindow(hwnd, SW_RESTORE);
  390. }
  391. void
  392. WIN_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
  393. {
  394. SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
  395. HWND hwnd = data->hwnd;
  396. RECT rect;
  397. SDL_Rect bounds;
  398. DWORD style;
  399. HWND top;
  400. BOOL menu;
  401. int x, y;
  402. int w, h;
  403. if (SDL_ShouldAllowTopmost() && (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_INPUT_FOCUS)) == (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_INPUT_FOCUS)) {
  404. top = HWND_TOPMOST;
  405. } else {
  406. top = HWND_NOTOPMOST;
  407. }
  408. style = GetWindowLong(hwnd, GWL_STYLE);
  409. style &= ~STYLE_MASK;
  410. style |= GetWindowStyle(window);
  411. WIN_GetDisplayBounds(_this, display, &bounds);
  412. if (fullscreen) {
  413. x = bounds.x;
  414. y = bounds.y;
  415. w = bounds.w;
  416. h = bounds.h;
  417. } else {
  418. rect.left = 0;
  419. rect.top = 0;
  420. rect.right = window->windowed.w;
  421. rect.bottom = window->windowed.h;
  422. menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
  423. AdjustWindowRectEx(&rect, style, menu, 0);
  424. w = (rect.right - rect.left);
  425. h = (rect.bottom - rect.top);
  426. x = window->windowed.x + rect.left;
  427. y = window->windowed.y + rect.top;
  428. }
  429. SetWindowLong(hwnd, GWL_STYLE, style);
  430. SetWindowPos(hwnd, top, x, y, w, h, SWP_NOCOPYBITS);
  431. }
  432. int
  433. WIN_SetWindowGammaRamp(_THIS, SDL_Window * window, const Uint16 * ramp)
  434. {
  435. SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
  436. SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
  437. HDC hdc;
  438. BOOL succeeded = FALSE;
  439. hdc = CreateDC(data->DeviceName, NULL, NULL, NULL);
  440. if (hdc) {
  441. succeeded = SetDeviceGammaRamp(hdc, (LPVOID)ramp);
  442. if (!succeeded) {
  443. WIN_SetError("SetDeviceGammaRamp()");
  444. }
  445. DeleteDC(hdc);
  446. }
  447. return succeeded ? 0 : -1;
  448. }
  449. int
  450. WIN_GetWindowGammaRamp(_THIS, SDL_Window * window, Uint16 * ramp)
  451. {
  452. SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
  453. SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
  454. HDC hdc;
  455. BOOL succeeded = FALSE;
  456. hdc = CreateDC(data->DeviceName, NULL, NULL, NULL);
  457. if (hdc) {
  458. succeeded = GetDeviceGammaRamp(hdc, (LPVOID)ramp);
  459. if (!succeeded) {
  460. WIN_SetError("GetDeviceGammaRamp()");
  461. }
  462. DeleteDC(hdc);
  463. }
  464. return succeeded ? 0 : -1;
  465. }
  466. void
  467. WIN_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed)
  468. {
  469. HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
  470. if (grabbed) {
  471. RECT rect;
  472. GetClientRect(hwnd, &rect);
  473. ClientToScreen(hwnd, (LPPOINT) & rect);
  474. ClientToScreen(hwnd, (LPPOINT) & rect + 1);
  475. ClipCursor(&rect);
  476. } else {
  477. ClipCursor(NULL);
  478. }
  479. if (window->flags & SDL_WINDOW_FULLSCREEN) {
  480. UINT flags = SWP_NOCOPYBITS | SWP_NOMOVE | SWP_NOSIZE;
  481. if (!(window->flags & SDL_WINDOW_SHOWN)) {
  482. flags |= SWP_NOACTIVATE;
  483. }
  484. WIN_SetWindowPositionInternal(_this, window, flags);
  485. }
  486. }
  487. void
  488. WIN_DestroyWindow(_THIS, SDL_Window * window)
  489. {
  490. SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
  491. if (data) {
  492. ReleaseDC(data->hwnd, data->hdc);
  493. if (data->created) {
  494. DestroyWindow(data->hwnd);
  495. } else {
  496. /* Restore any original event handler... */
  497. if (data->wndproc != NULL) {
  498. #ifdef GWLP_WNDPROC
  499. SetWindowLongPtr(data->hwnd, GWLP_WNDPROC,
  500. (LONG_PTR) data->wndproc);
  501. #else
  502. SetWindowLong(data->hwnd, GWL_WNDPROC,
  503. (LONG_PTR) data->wndproc);
  504. #endif
  505. }
  506. }
  507. SDL_free(data);
  508. }
  509. }
  510. SDL_bool
  511. WIN_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
  512. {
  513. HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
  514. if (info->version.major <= SDL_MAJOR_VERSION) {
  515. info->subsystem = SDL_SYSWM_WINDOWS;
  516. info->info.win.window = hwnd;
  517. return SDL_TRUE;
  518. } else {
  519. SDL_SetError("Application not compiled with SDL %d.%d\n",
  520. SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
  521. return SDL_FALSE;
  522. }
  523. }
  524. /*
  525. * Creates a HelperWindow used for DirectInput events.
  526. */
  527. int
  528. SDL_HelperWindowCreate(void)
  529. {
  530. HINSTANCE hInstance = GetModuleHandle(NULL);
  531. WNDCLASS wce;
  532. /* Make sure window isn't created twice. */
  533. if (SDL_HelperWindow != NULL) {
  534. return 0;
  535. }
  536. /* Create the class. */
  537. SDL_zero(wce);
  538. wce.lpfnWndProc = DefWindowProc;
  539. wce.lpszClassName = (LPCWSTR) SDL_HelperWindowClassName;
  540. wce.hInstance = hInstance;
  541. /* Register the class. */
  542. SDL_HelperWindowClass = RegisterClass(&wce);
  543. if (SDL_HelperWindowClass == 0) {
  544. return WIN_SetError("Unable to create Helper Window Class");
  545. }
  546. /* Create the window. */
  547. SDL_HelperWindow = CreateWindowEx(0, SDL_HelperWindowClassName,
  548. SDL_HelperWindowName,
  549. WS_OVERLAPPED, CW_USEDEFAULT,
  550. CW_USEDEFAULT, CW_USEDEFAULT,
  551. CW_USEDEFAULT, HWND_MESSAGE, NULL,
  552. hInstance, NULL);
  553. if (SDL_HelperWindow == NULL) {
  554. UnregisterClass(SDL_HelperWindowClassName, hInstance);
  555. return WIN_SetError("Unable to create Helper Window");
  556. }
  557. return 0;
  558. }
  559. /*
  560. * Destroys the HelperWindow previously created with SDL_HelperWindowCreate.
  561. */
  562. void
  563. SDL_HelperWindowDestroy(void)
  564. {
  565. HINSTANCE hInstance = GetModuleHandle(NULL);
  566. /* Destroy the window. */
  567. if (SDL_HelperWindow != NULL) {
  568. if (DestroyWindow(SDL_HelperWindow) == 0) {
  569. WIN_SetError("Unable to destroy Helper Window");
  570. return;
  571. }
  572. SDL_HelperWindow = NULL;
  573. }
  574. /* Unregister the class. */
  575. if (SDL_HelperWindowClass != 0) {
  576. if ((UnregisterClass(SDL_HelperWindowClassName, hInstance)) == 0) {
  577. WIN_SetError("Unable to destroy Helper Window Class");
  578. return;
  579. }
  580. SDL_HelperWindowClass = 0;
  581. }
  582. }
  583. void WIN_OnWindowEnter(_THIS, SDL_Window * window)
  584. {
  585. #ifdef WM_MOUSELEAVE
  586. SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
  587. TRACKMOUSEEVENT trackMouseEvent;
  588. if (!data || !data->hwnd) {
  589. /* The window wasn't fully initialized */
  590. return;
  591. }
  592. trackMouseEvent.cbSize = sizeof(TRACKMOUSEEVENT);
  593. trackMouseEvent.dwFlags = TME_LEAVE;
  594. trackMouseEvent.hwndTrack = data->hwnd;
  595. TrackMouseEvent(&trackMouseEvent);
  596. #endif /* WM_MOUSELEAVE */
  597. }
  598. #endif /* SDL_VIDEO_DRIVER_WINDOWS */
  599. /* vi: set ts=4 sw=4 expandtab: */