2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2012 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "SDL_config.h"
24 #define WIN32_LEAN_AND_MEAN
27 /* Make sure XBUTTON stuff is defined that isn't in older Platform SDKs... */
28 #ifndef WM_XBUTTONDOWN
29 #define WM_XBUTTONDOWN 0x020B
32 #define WM_XBUTTONUP 0x020C
34 #ifndef GET_XBUTTON_WPARAM
35 #define GET_XBUTTON_WPARAM(w) (HIWORD(w))
38 #include "SDL_events.h"
39 #include "SDL_video.h"
40 #include "SDL_syswm.h"
41 #include "../SDL_sysvideo.h"
42 #include "../../events/SDL_sysevents.h"
43 #include "../../events/SDL_events_c.h"
44 #include "SDL_lowvideo.h"
45 #include "SDL_syswm_c.h"
47 #include "SDL_loadso.h"
53 #include "../windib/SDL_gapidibvideo.h"
55 #ifdef SDL_VIDEO_DRIVER_GAPI
56 #include "../gapi/SDL_gapivideo.h"
60 #define IsZoomed(HWND) 1
61 #define NO_GETKEYBOARDSTATE
63 #define NO_CHANGEDISPLAYSETTINGS
67 /* The window we use for everything... */
69 LPWSTR SDL_Appname = NULL;
71 LPSTR SDL_Appname = NULL;
73 Uint32 SDL_Appstyle = 0;
74 HINSTANCE SDL_Instance = NULL;
75 HWND SDL_Window = NULL;
76 RECT SDL_bounds = {0, 0, 0, 0};
80 int mouse_relative = 0;
82 #ifndef NO_CHANGEDISPLAYSETTINGS
83 DEVMODE SDL_desktop_mode;
84 DEVMODE SDL_fullscreen_mode;
86 WORD *gamma_saved = NULL;
89 /* Functions called by the message processing function */
90 LONG (*HandleMessage)(_THIS, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)=NULL;
91 void (*WIN_Activate)(_THIS, BOOL active, BOOL iconic);
92 void (*WIN_RealizePalette)(_THIS);
93 void (*WIN_PaletteChanged)(_THIS, HWND window);
94 void (*WIN_WinPAINT)(_THIS, HDC hdc);
95 extern void DIB_SwapGamma(_THIS);
97 #ifndef NO_GETKEYBOARDSTATE
99 /* Variables and support functions for SDL_ToUnicode() */
102 static int GetCodePage();
103 static int WINAPI ToUnicode9xME(UINT vkey, UINT scancode, const BYTE *keystate, LPWSTR wchars, int wsize, UINT flags);
105 ToUnicodeFN SDL_ToUnicode = ToUnicode9xME;
107 #endif /* !NO_GETKEYBOARDSTATE */
110 #if defined(_WIN32_WCE)
112 //AdjustWindowRect is not available under WinCE 2003
113 #define AdjustWindowRect(a,b,c) (AdjustWindowRectEx((a),(b),(c),0))
115 // dynamically load aygshell dll because we want SDL to work on HPC and be300
116 HINSTANCE aygshell = NULL;
117 BOOL (WINAPI *SHFullScreen)(HWND hwndRequester, DWORD dwState) = 0;
119 #define SHFS_SHOWTASKBAR 0x0001
120 #define SHFS_HIDETASKBAR 0x0002
121 #define SHFS_SHOWSIPBUTTON 0x0004
122 #define SHFS_HIDESIPBUTTON 0x0008
123 #define SHFS_SHOWSTARTICON 0x0010
124 #define SHFS_HIDESTARTICON 0x0020
126 static void LoadAygshell(void)
129 aygshell = SDL_LoadObject("aygshell.dll");
130 if( (aygshell != 0) && (SHFullScreen == 0) )
132 SHFullScreen = (int (WINAPI *)(struct HWND__ *,unsigned long)) SDL_LoadFunction(aygshell, "SHFullScreen");
139 This is used all over the place, in the windib driver and in the dx5 driver
140 So we may as well stick it here instead of having multiple copies scattered
143 void WIN_FlushMessageQueue()
146 while ( PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) ) {
147 if ( msg.message == WM_QUIT ) break;
148 TranslateMessage( &msg );
149 DispatchMessage( &msg );
153 static void SDL_RestoreGameMode(void)
155 #ifdef _WIN32_WCE //Under ce we don't minimize, therefore no restore
157 #ifdef SDL_VIDEO_DRIVER_GAPI
158 SDL_VideoDevice *this = current_video;
159 if(SDL_strcmp(this->name, "gapi") == 0)
161 if( this->hidden->gapiInfo->suspended )
163 this->hidden->gapiInfo->suspended = 0;
169 ShowWindow(SDL_Window, SW_RESTORE);
172 #ifndef NO_CHANGEDISPLAYSETTINGS
174 ChangeDisplaySettings(&SDL_fullscreen_mode, CDS_FULLSCREEN);
176 #endif /* NO_CHANGEDISPLAYSETTINGS */
178 static void SDL_RestoreDesktopMode(void)
183 #ifdef SDL_VIDEO_DRIVER_GAPI
184 SDL_VideoDevice *this = current_video;
185 if(SDL_strcmp(this->name, "gapi") == 0)
187 if( !this->hidden->gapiInfo->suspended )
189 this->hidden->gapiInfo->suspended = 1;
195 /* WinCE does not have a taskbar, so minimizing is not convenient */
196 ShowWindow(SDL_Window, SW_MINIMIZE);
199 #ifndef NO_CHANGEDISPLAYSETTINGS
201 ChangeDisplaySettings(NULL, 0);
203 #endif /* NO_CHANGEDISPLAYSETTINGS */
208 Special code to handle mouse leave events - this sucks...
209 http://support.microsoft.com/support/kb/articles/q183/1/07.asp
211 TrackMouseEvent() is only available on Win98 and WinNT.
212 _TrackMouseEvent() is available on Win95, but isn't yet in the mingw32
213 development environment, and only works on systems that have had IE 3.0
214 or newer installed on them (which is not the case with the base Win95).
215 Therefore, we implement our own version of _TrackMouseEvent() which
216 uses our own implementation if TrackMouseEvent() is not available.
218 static BOOL (WINAPI *_TrackMouseEvent)(TRACKMOUSEEVENT *ptme) = NULL;
221 TrackMouseTimerProc(HWND hWnd, UINT uMsg, UINT idEvent, DWORD dwTime)
223 union { RECT rect; POINT pt; } rectpt; /* prevent type-punning issue. */
226 GetClientRect(hWnd, &rectpt.rect);
227 MapWindowPoints(hWnd, NULL, &rectpt.pt, 2);
229 if ( !PtInRect(&rectpt.rect, pt) || (WindowFromPoint(pt) != hWnd) ) {
230 if ( !KillTimer(hWnd, idEvent) ) {
231 /* Error killing the timer! */
233 PostMessage(hWnd, WM_MOUSELEAVE, 0, 0);
236 static BOOL WINAPI WIN_TrackMouseEvent(TRACKMOUSEEVENT *ptme)
238 if ( ptme->dwFlags == TME_LEAVE ) {
239 return SetTimer(ptme->hwndTrack, ptme->dwFlags, 100,
240 (TIMERPROC)TrackMouseTimerProc) != 0;
244 #endif /* WM_MOUSELEAVE */
246 int sysevents_mouse_pressed = 0;
248 /* The main Win32 event handler
249 DJM: This is no longer static as (DX5/DIB)_CreateWindow needs it
251 LRESULT CALLBACK WinMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
253 SDL_VideoDevice *this = current_video;
255 fprintf(stderr, "Received windows message: ");
256 if ( msg > MAX_WMMSG ) {
257 fprintf(stderr, "%d", msg);
259 fprintf(stderr, "%s", wmtab[msg]);
261 fprintf(stderr, " -- 0x%X, 0x%X\n", wParam, lParam);
266 SDL_VideoDevice *this = current_video;
267 BOOL active, minimized;
270 minimized = HIWORD(wParam);
271 active = (LOWORD(wParam) != WA_INACTIVE) && !minimized;
273 /* Gain the following states */
274 appstate = SDL_APPACTIVE|SDL_APPINPUTFOCUS;
275 if ( !(SDL_GetAppState() & SDL_APPINPUTFOCUS) ) {
276 if ( this->input_grab != SDL_GRAB_OFF ) {
277 WIN_GrabInput(this, SDL_GRAB_ON);
279 if ( ! DDRAW_FULLSCREEN() ) {
282 if ( WINDIB_FULLSCREEN() ) {
283 SDL_RestoreGameMode();
286 #if defined(_WIN32_WCE)
287 if ( WINDIB_FULLSCREEN() ) {
290 SHFullScreen(SDL_Window, SHFS_HIDESTARTICON|SHFS_HIDETASKBAR|SHFS_HIDESIPBUTTON);
292 ShowWindow(FindWindow(TEXT("HHTaskBar"),NULL),SW_HIDE);
295 posted = SDL_PrivateAppActive(1, appstate);
297 /* Lose the following states */
298 appstate = SDL_APPINPUTFOCUS;
300 appstate |= SDL_APPACTIVE;
303 if ( SDL_GetAppState() & SDL_APPINPUTFOCUS ) {
304 if ( this->input_grab != SDL_GRAB_OFF ) {
305 WIN_GrabInput(this, SDL_GRAB_OFF);
307 if ( ! DDRAW_FULLSCREEN() ) {
310 if ( WINDIB_FULLSCREEN() ) {
311 appstate |= SDL_APPMOUSEFOCUS;
312 SDL_RestoreDesktopMode();
313 /* A fullscreen app gets hidden but will not get a minimize event */
\r
314 appstate |= (SDL_APPACTIVE | SDL_APPMOUSEFOCUS);
\r
315 #if defined(_WIN32_WCE)
318 SHFullScreen(SDL_Window, SHFS_SHOWSTARTICON|SHFS_SHOWTASKBAR|SHFS_SHOWSIPBUTTON);
320 ShowWindow(FindWindow(TEXT("HHTaskBar"),NULL),SW_SHOW);
324 posted = SDL_PrivateAppActive(0, appstate);
326 WIN_Activate(this, active, minimized);
334 if ( SDL_VideoSurface ) {
335 /* mouse has entered the window */
337 if ( !(SDL_GetAppState() & SDL_APPMOUSEFOCUS) ) {
340 tme.cbSize = sizeof(tme);
341 tme.dwFlags = TME_LEAVE;
342 tme.hwndTrack = SDL_Window;
343 _TrackMouseEvent(&tme);
346 #endif /* WM_MOUSELEAVE */
348 /* Mouse motion is handled in DIB_PumpEvents or
349 * DX5_PumpEvents, depending on the video driver
352 posted = SDL_PrivateAppActive(1, SDL_APPMOUSEFOCUS);
357 case WM_MOUSELEAVE: {
359 if ( SDL_VideoSurface ) {
360 /* mouse has left the window */
361 posted = SDL_PrivateAppActive(0, SDL_APPMOUSEFOCUS);
365 #endif /* WM_MOUSELEAVE */
375 /* Mouse is handled by DirectInput when fullscreen */
376 if ( SDL_VideoSurface && ! DINPUT() ) {
382 We want the SDL window to take focus so that
383 it acts like a normal windows "component"
384 (e.g. gains keyboard focus on a mouse click).
386 SetFocus(SDL_Window);
388 /* Figure out which button to use */
391 button = SDL_BUTTON_LEFT;
395 button = SDL_BUTTON_LEFT;
396 state = SDL_RELEASED;
399 button = SDL_BUTTON_MIDDLE;
403 button = SDL_BUTTON_MIDDLE;
404 state = SDL_RELEASED;
407 button = SDL_BUTTON_RIGHT;
411 button = SDL_BUTTON_RIGHT;
412 state = SDL_RELEASED;
415 xbuttonval = GET_XBUTTON_WPARAM(wParam);
416 button = SDL_BUTTON_X1 + xbuttonval - 1;
420 xbuttonval = GET_XBUTTON_WPARAM(wParam);
421 button = SDL_BUTTON_X1 + xbuttonval - 1;
422 state = SDL_RELEASED;
425 /* Eh? Unknown button? */
428 if ( state == SDL_PRESSED ) {
429 /* Grab mouse so we get up events */
430 if ( ++sysevents_mouse_pressed > 0 ) {
434 /* Release mouse after all up events */
435 if ( --sysevents_mouse_pressed <= 0 ) {
437 sysevents_mouse_pressed = 0;
440 if ( mouse_relative ) {
441 /* RJR: March 28, 2000
442 report internal mouse position if in relative mode */
445 x = (Sint16)LOWORD(lParam);
446 y = (Sint16)HIWORD(lParam);
448 if (SDL_VideoSurface)
449 GapiTransform(this->hidden->userOrientation,
450 this->hidden->hiresFix, &x, &y);
453 posted = SDL_PrivateMouseButton(
454 state, button, x, y);
458 * "Unlike the WM_LBUTTONUP, WM_MBUTTONUP, and WM_RBUTTONUP
459 * messages, an application should return TRUE from [an
460 * XBUTTON message] if it processes it. Doing so will allow
461 * software that simulates this message on Microsoft Windows
462 * systems earlier than Windows 2000 to determine whether
463 * the window procedure processed the message or called
464 * DefWindowProc to process it.
473 #if (_WIN32_WINNT >= 0x0400) || (_WIN32_WINDOWS > 0x0400)
475 if ( SDL_VideoSurface && ! DINPUT() ) {
476 int move = (short)HIWORD(wParam);
480 button = SDL_BUTTON_WHEELUP;
482 button = SDL_BUTTON_WHEELDOWN;
483 posted = SDL_PrivateMouseButton(
484 SDL_PRESSED, button, 0, 0);
485 posted |= SDL_PrivateMouseButton(
486 SDL_RELEASED, button, 0, 0);
492 #ifdef WM_GETMINMAXINFO
493 /* This message is sent as a way for us to "check" the values
494 * of a position change. If we don't like it, we can adjust
495 * the values before they are changed.
497 case WM_GETMINMAXINFO: {
505 /* We don't want to clobber an internal resize */
509 /* We allow resizing with the SDL_RESIZABLE flag */
510 if ( SDL_PublicSurface &&
511 (SDL_PublicSurface->flags & SDL_RESIZABLE) ) {
515 /* Get the current position of our window */
516 GetWindowRect(SDL_Window, &size);
520 /* Calculate current width and height of our window */
523 if ( SDL_PublicSurface != NULL ) {
524 size.bottom = SDL_PublicSurface->h;
525 size.right = SDL_PublicSurface->w;
531 /* DJM - according to the docs for GetMenu(), the
532 return value is undefined if hwnd is a child window.
533 Aparently it's too difficult for MS to check
534 inside their function, so I have to do it here.
536 style = GetWindowLong(hwnd, GWL_STYLE);
540 style & WS_CHILDWINDOW ? FALSE
541 : GetMenu(hwnd) != NULL);
543 width = size.right - size.left;
544 height = size.bottom - size.top;
546 /* Fix our size to the current size */
547 info = (MINMAXINFO *)lParam;
548 info->ptMaxSize.x = width;
549 info->ptMaxSize.y = height;
550 info->ptMaxPosition.x = x;
551 info->ptMaxPosition.y = y;
552 info->ptMinTrackSize.x = width;
553 info->ptMinTrackSize.y = height;
554 info->ptMaxTrackSize.x = width;
555 info->ptMaxTrackSize.y = height;
558 #endif /* WM_GETMINMAXINFO */
560 case WM_WINDOWPOSCHANGING: {
561 WINDOWPOS *windowpos = (WINDOWPOS*)lParam;
563 /* When menu is at the side or top, Windows likes
564 to try to reposition the fullscreen window when
565 changing video modes.
567 if ( !SDL_resizing &&
569 (SDL_PublicSurface->flags & SDL_FULLSCREEN) ) {
576 case WM_WINDOWPOSCHANGED: {
577 SDL_VideoDevice *this = current_video;
581 GetClientRect(SDL_Window, &SDL_bounds);
583 /* avoiding type-punning here... */
584 pt.x = SDL_bounds.left;
585 pt.y = SDL_bounds.top;
586 ClientToScreen(SDL_Window, &pt);
587 SDL_bounds.left = pt.x;
588 SDL_bounds.top = pt.y;
590 pt.x = SDL_bounds.right;
591 pt.y = SDL_bounds.bottom;
592 ClientToScreen(SDL_Window, &pt);
593 SDL_bounds.right = pt.x;
594 SDL_bounds.bottom = pt.y;
596 if ( !SDL_resizing && !IsZoomed(SDL_Window) &&
598 !(SDL_PublicSurface->flags & SDL_FULLSCREEN) ) {
599 SDL_windowX = SDL_bounds.left;
600 SDL_windowY = SDL_bounds.top;
602 w = SDL_bounds.right-SDL_bounds.left;
603 h = SDL_bounds.bottom-SDL_bounds.top;
604 if ( this->input_grab != SDL_GRAB_OFF ) {
605 ClipCursor(&SDL_bounds);
607 if ( SDL_PublicSurface &&
608 (SDL_PublicSurface->flags & SDL_RESIZABLE) ) {
609 SDL_PrivateResize(w, h);
614 /* We need to set the cursor */
618 hittest = LOWORD(lParam);
619 if ( hittest == HTCLIENT ) {
620 SetCursor(SDL_hcursor);
626 /* We are about to get palette focus! */
627 case WM_QUERYNEWPALETTE: {
628 WIN_RealizePalette(current_video);
633 /* Another application changed the palette */
634 case WM_PALETTECHANGED: {
635 WIN_PaletteChanged(current_video, (HWND)wParam);
639 /* We were occluded, refresh our display */
644 hdc = BeginPaint(SDL_Window, &ps);
645 if ( current_video->screen &&
646 !(current_video->screen->flags & SDL_OPENGL) ) {
647 WIN_WinPAINT(current_video, hdc);
649 EndPaint(SDL_Window, &ps);
653 /* DJM: Send an expose event in this case */
654 case WM_ERASEBKGND: {
655 posted = SDL_PrivateExpose();
660 if ( (posted = SDL_PrivateQuit()) )
670 #ifndef NO_GETKEYBOARDSTATE
671 case WM_INPUTLANGCHANGE:
673 codepage = GetCodePage();
679 /* Special handling by the video driver */
681 return(HandleMessage(current_video,
682 hwnd, msg, wParam, lParam));
687 return(DefWindowProc(hwnd, msg, wParam, lParam));
690 /* Allow the application handle to be stored and retrieved later */
691 static void *SDL_handle = NULL;
693 void SDL_SetModuleHandle(void *handle)
697 void *SDL_GetModuleHandle(void)
704 handle = GetModuleHandle(NULL);
709 /* This allows the SDL_WINDOWID hack */
710 BOOL SDL_windowid = FALSE;
712 static int app_registered = 0;
714 /* Register the class for this application -- exported for winmain.c */
715 int SDL_RegisterApp(char *name, Uint32 style, void *hInst)
722 /* Only do this once... */
723 if ( app_registered ) {
728 #ifndef CS_BYTEALIGNCLIENT
729 #define CS_BYTEALIGNCLIENT 0
731 if ( ! name && ! SDL_Appname ) {
733 SDL_Appstyle = CS_BYTEALIGNCLIENT;
734 SDL_Instance = hInst ? hInst : SDL_GetModuleHandle();
739 /* WinCE uses the UNICODE version */
740 SDL_Appname = SDL_iconv_utf8_ucs2(name);
742 SDL_Appname = SDL_iconv_utf8_locale(name);
743 #endif /* _WIN32_WCE */
744 SDL_Appstyle = style;
745 SDL_Instance = hInst ? hInst : SDL_GetModuleHandle();
748 /* Register the application class */
749 class.hCursor = NULL;
750 class.hIcon = LoadImage(SDL_Instance, SDL_Appname,
752 0, 0, LR_DEFAULTCOLOR);
753 class.lpszMenuName = NULL;
754 class.lpszClassName = SDL_Appname;
755 class.hbrBackground = NULL;
756 class.hInstance = SDL_Instance;
757 class.style = SDL_Appstyle;
759 class.style |= CS_OWNDC;
761 class.lpfnWndProc = WinMessage;
762 class.cbWndExtra = 0;
763 class.cbClsExtra = 0;
764 if ( ! RegisterClass(&class) ) {
765 SDL_SetError("Couldn't register application class");
770 /* Get the version of TrackMouseEvent() we use */
771 _TrackMouseEvent = NULL;
772 handle = GetModuleHandle("USER32.DLL");
774 _TrackMouseEvent = (BOOL (WINAPI *)(TRACKMOUSEEVENT *))GetProcAddress(handle, "TrackMouseEvent");
776 if ( _TrackMouseEvent == NULL ) {
777 _TrackMouseEvent = WIN_TrackMouseEvent;
779 #endif /* WM_MOUSELEAVE */
781 #ifndef NO_GETKEYBOARDSTATE
783 /* Initialise variables for SDL_ToUnicode() */
784 codepage = GetCodePage();
786 /* Cygwin headers don't match windows.h, so we have to cast around a
787 const issue here... */
788 SDL_ToUnicode = Is9xME() ? ToUnicode9xME : (ToUnicodeFN) ToUnicode;
790 #endif /* NO_GETKEYBOARDSTATE */
796 /* Unregisters the windowclass registered in SDL_RegisterApp above. */
797 void SDL_UnregisterApp()
801 /* SDL_RegisterApp might not have been called before */
802 if ( !app_registered ) {
806 if ( app_registered == 0 ) {
807 /* Check for any registered window classes. */
808 if ( GetClassInfo(SDL_Instance, SDL_Appname, &class) ) {
809 UnregisterClass(SDL_Appname, SDL_Instance);
811 SDL_free(SDL_Appname);
816 #ifndef NO_GETKEYBOARDSTATE
818 /* JFP: Implementation of ToUnicode() that works on 9x/ME/2K/XP */
824 SDL_memset(&info, 0, sizeof(info));
825 info.dwOSVersionInfoSize = sizeof(info);
826 if (!GetVersionEx(&info)) {
829 return (info.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS);
832 static int GetCodePage()
835 int lcid = MAKELCID(LOWORD(GetKeyboardLayout(0)), SORT_DEFAULT);
838 if (GetLocaleInfo(lcid, LOCALE_IDEFAULTANSICODEPAGE, buff, sizeof(buff))) {
844 static int WINAPI ToUnicode9xME(UINT vkey, UINT scancode, const BYTE *keystate, LPWSTR wchars, int wsize, UINT flags)
848 /* arg #3 should be const BYTE *, but cygwin lists it as PBYTE. */
849 if (ToAsciiEx(vkey, scancode, (PBYTE) keystate, (WORD*)chars, 0, GetKeyboardLayout(0)) == 1) {
850 return MultiByteToWideChar(codepage, 0, (LPCSTR) chars, 1, wchars, wsize);
855 #endif /* !NO_GETKEYBOARDSTATE */