src/video/wincommon/SDL_sysevents.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 13 Oct 2011 16:38:05 -0400
branchSDL-1.2
changeset 5988 bf927e528813
parent 5983 1361a12b2e58
child 6110 676a54593bc4
permissions -rw-r--r--
Fixed a bunch more compiler warnings in the 1.2 branch.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@4159
     3
    Copyright (C) 1997-2009 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@1433
    24
#define WIN32_LEAN_AND_MEAN
slouken@1433
    25
#include <windows.h>
slouken@0
    26
icculus@3970
    27
/* Make sure XBUTTON stuff is defined that isn't in older Platform SDKs... */
icculus@3970
    28
#ifndef WM_XBUTTONDOWN
icculus@3970
    29
#define WM_XBUTTONDOWN 0x020B
icculus@3970
    30
#endif
icculus@3970
    31
#ifndef WM_XBUTTONUP
icculus@3970
    32
#define WM_XBUTTONUP 0x020C
icculus@3970
    33
#endif
icculus@3970
    34
#ifndef GET_XBUTTON_WPARAM
icculus@3970
    35
#define GET_XBUTTON_WPARAM(w) (HIWORD(w))
icculus@3970
    36
#endif
icculus@3970
    37
slouken@0
    38
#include "SDL_events.h"
slouken@0
    39
#include "SDL_video.h"
slouken@0
    40
#include "SDL_syswm.h"
slouken@1361
    41
#include "../SDL_sysvideo.h"
slouken@1361
    42
#include "../../events/SDL_sysevents.h"
slouken@1361
    43
#include "../../events/SDL_events_c.h"
slouken@0
    44
#include "SDL_lowvideo.h"
slouken@0
    45
#include "SDL_syswm_c.h"
slouken@0
    46
#include "SDL_main.h"
icculus@1152
    47
#include "SDL_loadso.h"
slouken@0
    48
slouken@0
    49
#ifdef WMMSG_DEBUG
slouken@0
    50
#include "wmmsg.h"
slouken@0
    51
#endif
slouken@0
    52
slouken@4162
    53
#include "../windib/SDL_gapidibvideo.h"
slouken@4162
    54
slouken@4162
    55
#ifdef SDL_VIDEO_DRIVER_GAPI
slouken@4162
    56
#include "../gapi/SDL_gapivideo.h"
slouken@4162
    57
#endif
slouken@4162
    58
slouken@0
    59
#ifdef _WIN32_WCE
slouken@1465
    60
#define IsZoomed(HWND) 1
slouken@0
    61
#define NO_GETKEYBOARDSTATE
slouken@1465
    62
#if _WIN32_WCE < 420
slouken@453
    63
#define NO_CHANGEDISPLAYSETTINGS
slouken@0
    64
#endif
slouken@1465
    65
#endif
slouken@0
    66
slouken@0
    67
/* The window we use for everything... */
slouken@453
    68
#ifdef _WIN32_WCE
slouken@453
    69
LPWSTR SDL_Appname = NULL;
slouken@453
    70
#else
slouken@453
    71
LPSTR SDL_Appname = NULL;
slouken@453
    72
#endif
slouken@1288
    73
Uint32 SDL_Appstyle = 0;
slouken@0
    74
HINSTANCE SDL_Instance = NULL;
slouken@0
    75
HWND SDL_Window = NULL;
slouken@0
    76
RECT SDL_bounds = {0, 0, 0, 0};
slouken@833
    77
int SDL_windowX = 0;
slouken@833
    78
int SDL_windowY = 0;
slouken@0
    79
int SDL_resizing = 0;
slouken@0
    80
int mouse_relative = 0;
slouken@0
    81
int posted = 0;
slouken@304
    82
#ifndef NO_CHANGEDISPLAYSETTINGS
slouken@1295
    83
DEVMODE SDL_desktop_mode;
slouken@304
    84
DEVMODE SDL_fullscreen_mode;
slouken@304
    85
#endif
slouken@338
    86
WORD *gamma_saved = NULL;
slouken@0
    87
slouken@0
    88
slouken@0
    89
/* Functions called by the message processing function */
icculus@1152
    90
LONG (*HandleMessage)(_THIS, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)=NULL;
slouken@3992
    91
void (*WIN_Activate)(_THIS, BOOL active, BOOL iconic);
slouken@0
    92
void (*WIN_RealizePalette)(_THIS);
slouken@0
    93
void (*WIN_PaletteChanged)(_THIS, HWND window);
slouken@0
    94
void (*WIN_WinPAINT)(_THIS, HDC hdc);
slouken@338
    95
extern void DIB_SwapGamma(_THIS);
slouken@0
    96
slouken@1272
    97
#ifndef NO_GETKEYBOARDSTATE
icculus@5983
    98
#ifndef _WIN64
slouken@1253
    99
/* Variables and support functions for SDL_ToUnicode() */
slouken@1253
   100
static int codepage;
slouken@1253
   101
static int Is9xME();
slouken@1253
   102
static int GetCodePage();
icculus@5890
   103
static int WINAPI ToUnicode9xME(UINT vkey, UINT scancode, const BYTE *keystate, LPWSTR wchars, int wsize, UINT flags);
slouken@1253
   104
slouken@1253
   105
ToUnicodeFN SDL_ToUnicode = ToUnicode9xME;
icculus@5983
   106
#endif
slouken@1272
   107
#endif /* !NO_GETKEYBOARDSTATE */
slouken@1253
   108
slouken@1253
   109
icculus@1152
   110
#if defined(_WIN32_WCE)
icculus@1152
   111
slouken@4162
   112
//AdjustWindowRect is not available under WinCE 2003
slouken@4162
   113
#define AdjustWindowRect(a,b,c) (AdjustWindowRectEx((a),(b),(c),0))
slouken@4162
   114
icculus@1152
   115
// dynamically load aygshell dll because we want SDL to work on HPC and be300
icculus@1152
   116
HINSTANCE aygshell = NULL;
icculus@1152
   117
BOOL (WINAPI *SHFullScreen)(HWND hwndRequester, DWORD dwState) = 0;
icculus@1152
   118
icculus@1152
   119
#define SHFS_SHOWTASKBAR            0x0001
icculus@1152
   120
#define SHFS_HIDETASKBAR            0x0002
icculus@1152
   121
#define SHFS_SHOWSIPBUTTON          0x0004
icculus@1152
   122
#define SHFS_HIDESIPBUTTON          0x0008
icculus@1152
   123
#define SHFS_SHOWSTARTICON          0x0010
icculus@1152
   124
#define SHFS_HIDESTARTICON          0x0020
icculus@1152
   125
icculus@1152
   126
static void LoadAygshell(void)
icculus@1152
   127
{
icculus@1152
   128
	if( !aygshell )
icculus@1152
   129
		 aygshell = SDL_LoadObject("aygshell.dll");
slouken@1497
   130
	if( (aygshell != 0) && (SHFullScreen == 0) )
icculus@1152
   131
	{
icculus@1152
   132
		SHFullScreen = (int (WINAPI *)(struct HWND__ *,unsigned long)) SDL_LoadFunction(aygshell, "SHFullScreen");
icculus@1152
   133
	}
icculus@1152
   134
}
icculus@1152
   135
icculus@1152
   136
#endif
icculus@1152
   137
slouken@1523
   138
/* JC 14 Mar 2006
slouken@1523
   139
   This is used all over the place, in the windib driver and in the dx5 driver
slouken@1523
   140
   So we may as well stick it here instead of having multiple copies scattered
slouken@1523
   141
   about
slouken@1523
   142
*/
slouken@1523
   143
void WIN_FlushMessageQueue()
slouken@1523
   144
{
slouken@1523
   145
	MSG  msg;
slouken@1523
   146
	while ( PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) ) {
slouken@1523
   147
		if ( msg.message == WM_QUIT ) break;
slouken@1523
   148
		TranslateMessage( &msg );
slouken@1523
   149
		DispatchMessage( &msg );
slouken@1523
   150
	}
slouken@1523
   151
}
slouken@1523
   152
slouken@304
   153
static void SDL_RestoreGameMode(void)
slouken@304
   154
{
slouken@4162
   155
#ifdef _WIN32_WCE //Under ce we don't minimize, therefore no restore
slouken@4162
   156
	
slouken@4162
   157
#ifdef SDL_VIDEO_DRIVER_GAPI
slouken@1497
   158
	SDL_VideoDevice *this = current_video;
slouken@1497
   159
	if(SDL_strcmp(this->name, "gapi") == 0)
slouken@1497
   160
	{
slouken@4162
   161
		if( this->hidden->gapiInfo->suspended )
slouken@1497
   162
		{
slouken@4162
   163
			this->hidden->gapiInfo->suspended = 0;
slouken@1497
   164
		}
slouken@1497
   165
	}
slouken@4162
   166
#endif
slouken@4162
   167
	
slouken@1497
   168
#else
slouken@1465
   169
	ShowWindow(SDL_Window, SW_RESTORE);
slouken@1497
   170
#endif
slouken@1497
   171
slouken@304
   172
#ifndef NO_CHANGEDISPLAYSETTINGS
slouken@1465
   173
#ifndef _WIN32_WCE
slouken@304
   174
	ChangeDisplaySettings(&SDL_fullscreen_mode, CDS_FULLSCREEN);
slouken@304
   175
#endif
slouken@1465
   176
#endif /* NO_CHANGEDISPLAYSETTINGS */
slouken@304
   177
}
slouken@304
   178
static void SDL_RestoreDesktopMode(void)
slouken@304
   179
{
slouken@1497
   180
slouken@1497
   181
#ifdef _WIN32_WCE
slouken@4162
   182
	
slouken@4162
   183
#ifdef SDL_VIDEO_DRIVER_GAPI
slouken@1497
   184
	SDL_VideoDevice *this = current_video;
slouken@1497
   185
	if(SDL_strcmp(this->name, "gapi") == 0)
slouken@1497
   186
	{
slouken@4162
   187
		if( !this->hidden->gapiInfo->suspended )
slouken@1497
   188
		{
slouken@4162
   189
			this->hidden->gapiInfo->suspended = 1;
slouken@1497
   190
		}
slouken@1497
   191
	}
slouken@4162
   192
#endif
slouken@4162
   193
	
slouken@1497
   194
#else
slouken@1497
   195
	/* WinCE does not have a taskbar, so minimizing is not convenient */
slouken@1465
   196
	ShowWindow(SDL_Window, SW_MINIMIZE);
slouken@1497
   197
#endif
slouken@1497
   198
slouken@304
   199
#ifndef NO_CHANGEDISPLAYSETTINGS
slouken@1465
   200
#ifndef _WIN32_WCE
slouken@304
   201
	ChangeDisplaySettings(NULL, 0);
slouken@304
   202
#endif
slouken@1465
   203
#endif /* NO_CHANGEDISPLAYSETTINGS */
slouken@304
   204
}
slouken@304
   205
slouken@0
   206
#ifdef WM_MOUSELEAVE
slouken@0
   207
/* 
slouken@0
   208
   Special code to handle mouse leave events - this sucks...
slouken@0
   209
   http://support.microsoft.com/support/kb/articles/q183/1/07.asp
slouken@0
   210
slouken@0
   211
   TrackMouseEvent() is only available on Win98 and WinNT.
slouken@0
   212
   _TrackMouseEvent() is available on Win95, but isn't yet in the mingw32
slouken@0
   213
   development environment, and only works on systems that have had IE 3.0
slouken@0
   214
   or newer installed on them (which is not the case with the base Win95).
slouken@0
   215
   Therefore, we implement our own version of _TrackMouseEvent() which
slouken@0
   216
   uses our own implementation if TrackMouseEvent() is not available.
slouken@0
   217
*/
slouken@0
   218
static BOOL (WINAPI *_TrackMouseEvent)(TRACKMOUSEEVENT *ptme) = NULL;
slouken@0
   219
slouken@0
   220
static VOID CALLBACK
slouken@0
   221
TrackMouseTimerProc(HWND hWnd, UINT uMsg, UINT idEvent, DWORD dwTime)
slouken@0
   222
{
icculus@5988
   223
	union { RECT rect; POINT pt; } rectpt;  /* prevent type-punning issue. */
slouken@0
   224
	POINT pt;
slouken@0
   225
icculus@5988
   226
	GetClientRect(hWnd, &rectpt.rect);
icculus@5988
   227
	MapWindowPoints(hWnd, NULL, &rectpt.pt, 2);
slouken@0
   228
	GetCursorPos(&pt);
icculus@5988
   229
	if ( !PtInRect(&rectpt.rect, pt) || (WindowFromPoint(pt) != hWnd) ) {
slouken@0
   230
		if ( !KillTimer(hWnd, idEvent) ) {
slouken@0
   231
			/* Error killing the timer! */
slouken@0
   232
		}
slouken@0
   233
		PostMessage(hWnd, WM_MOUSELEAVE, 0, 0);
slouken@0
   234
	}
slouken@0
   235
}
slouken@0
   236
static BOOL WINAPI WIN_TrackMouseEvent(TRACKMOUSEEVENT *ptme)
slouken@0
   237
{
slouken@0
   238
	if ( ptme->dwFlags == TME_LEAVE ) {
slouken@0
   239
		return SetTimer(ptme->hwndTrack, ptme->dwFlags, 100,
slouken@1456
   240
		                (TIMERPROC)TrackMouseTimerProc) != 0;
slouken@0
   241
	}
slouken@0
   242
	return FALSE;
slouken@0
   243
}
slouken@0
   244
#endif /* WM_MOUSELEAVE */
slouken@0
   245
slouken@145
   246
/* The main Win32 event handler
slouken@145
   247
DJM: This is no longer static as (DX5/DIB)_CreateWindow needs it
slouken@145
   248
*/
slouken@1456
   249
LRESULT CALLBACK WinMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
slouken@0
   250
{
slouken@0
   251
	SDL_VideoDevice *this = current_video;
slouken@0
   252
	static int mouse_pressed = 0;
slouken@0
   253
#ifdef WMMSG_DEBUG
slouken@0
   254
	fprintf(stderr, "Received windows message:  ");
slouken@0
   255
	if ( msg > MAX_WMMSG ) {
slouken@0
   256
		fprintf(stderr, "%d", msg);
slouken@0
   257
	} else {
slouken@0
   258
		fprintf(stderr, "%s", wmtab[msg]);
slouken@0
   259
	}
slouken@0
   260
	fprintf(stderr, " -- 0x%X, 0x%X\n", wParam, lParam);
slouken@0
   261
#endif
slouken@0
   262
	switch (msg) {
slouken@0
   263
slouken@0
   264
		case WM_ACTIVATE: {
slouken@0
   265
			SDL_VideoDevice *this = current_video;
slouken@3992
   266
			BOOL active, minimized;
slouken@0
   267
			Uint8 appstate;
slouken@0
   268
slouken@0
   269
			minimized = HIWORD(wParam);
slouken@3992
   270
			active = (LOWORD(wParam) != WA_INACTIVE) && !minimized;
slouken@3992
   271
			if ( active ) {
slouken@0
   272
				/* Gain the following states */
slouken@0
   273
				appstate = SDL_APPACTIVE|SDL_APPINPUTFOCUS;
slouken@4391
   274
				if ( !(SDL_GetAppState() & SDL_APPINPUTFOCUS) ) {
slouken@4391
   275
					if ( this->input_grab != SDL_GRAB_OFF ) {
slouken@4391
   276
						WIN_GrabInput(this, SDL_GRAB_ON);
slouken@4391
   277
					}
slouken@338
   278
					if ( ! DDRAW_FULLSCREEN() ) {
slouken@338
   279
						DIB_SwapGamma(this);
slouken@338
   280
					}
slouken@304
   281
					if ( WINDIB_FULLSCREEN() ) {
slouken@304
   282
						SDL_RestoreGameMode();
slouken@304
   283
					}
slouken@0
   284
				}
icculus@1152
   285
#if defined(_WIN32_WCE)
slouken@3992
   286
				if ( WINDIB_FULLSCREEN() ) {
slouken@3992
   287
					LoadAygshell();
slouken@3992
   288
					if( SHFullScreen )
slouken@3992
   289
						SHFullScreen(SDL_Window, SHFS_HIDESTARTICON|SHFS_HIDETASKBAR|SHFS_HIDESIPBUTTON);
slouken@3992
   290
					else
slouken@3992
   291
						ShowWindow(FindWindow(TEXT("HHTaskBar"),NULL),SW_HIDE);
slouken@3992
   292
				}
icculus@1152
   293
#endif
slouken@0
   294
				posted = SDL_PrivateAppActive(1, appstate);
slouken@0
   295
			} else {
slouken@0
   296
				/* Lose the following states */
slouken@0
   297
				appstate = SDL_APPINPUTFOCUS;
slouken@0
   298
				if ( minimized ) {
slouken@0
   299
					appstate |= SDL_APPACTIVE;
slouken@0
   300
				}
slouken@4391
   301
slouken@0
   302
				if ( SDL_GetAppState() & SDL_APPINPUTFOCUS ) {
slouken@4391
   303
					if ( this->input_grab != SDL_GRAB_OFF ) {
slouken@4391
   304
						WIN_GrabInput(this, SDL_GRAB_OFF);
slouken@4391
   305
					}
slouken@338
   306
					if ( ! DDRAW_FULLSCREEN() ) {
slouken@338
   307
						DIB_SwapGamma(this);
slouken@338
   308
					}
slouken@304
   309
					if ( WINDIB_FULLSCREEN() ) {
slouken@4391
   310
						appstate |= SDL_APPMOUSEFOCUS;
slouken@304
   311
						SDL_RestoreDesktopMode();
icculus@1152
   312
#if defined(_WIN32_WCE)
icculus@1152
   313
						LoadAygshell();
slouken@1497
   314
						if( SHFullScreen ) 
icculus@1152
   315
							SHFullScreen(SDL_Window, SHFS_SHOWSTARTICON|SHFS_SHOWTASKBAR|SHFS_SHOWSIPBUTTON);
icculus@1152
   316
						else
icculus@1152
   317
							ShowWindow(FindWindow(TEXT("HHTaskBar"),NULL),SW_SHOW);
icculus@1152
   318
#endif
slouken@304
   319
					}
slouken@0
   320
				}
slouken@0
   321
				posted = SDL_PrivateAppActive(0, appstate);
slouken@0
   322
			}
slouken@3992
   323
			WIN_Activate(this, active, minimized);
slouken@0
   324
			return(0);
slouken@0
   325
		}
slouken@0
   326
		break;
slouken@0
   327
slouken@0
   328
		case WM_MOUSEMOVE: {
slouken@0
   329
slouken@4169
   330
#ifdef WM_MOUSELEAVE
slouken@4391
   331
			if ( SDL_VideoSurface ) {
slouken@0
   332
				/* mouse has entered the window */
slouken@4169
   333
slouken@4169
   334
				if ( !(SDL_GetAppState() & SDL_APPMOUSEFOCUS) ) {
slouken@0
   335
					TRACKMOUSEEVENT tme;
slouken@0
   336
slouken@0
   337
					tme.cbSize = sizeof(tme);
slouken@0
   338
					tme.dwFlags = TME_LEAVE;
slouken@0
   339
					tme.hwndTrack = SDL_Window;
slouken@0
   340
					_TrackMouseEvent(&tme);
slouken@0
   341
				}
slouken@0
   342
			}
slouken@4169
   343
#endif /* WM_MOUSELEAVE */
slouken@4169
   344
slouken@4169
   345
			/* Mouse motion is handled in DIB_PumpEvents or
slouken@4169
   346
			 * DX5_PumpEvents, depending on the video driver
slouken@4169
   347
			 * in use */
slouken@4169
   348
slouken@4169
   349
			posted = SDL_PrivateAppActive(1, SDL_APPMOUSEFOCUS);
slouken@0
   350
		}
slouken@0
   351
		return(0);
slouken@0
   352
slouken@0
   353
#ifdef WM_MOUSELEAVE
slouken@0
   354
		case WM_MOUSELEAVE: {
slouken@0
   355
slouken@4391
   356
			if ( SDL_VideoSurface ) {
slouken@0
   357
				/* mouse has left the window */
slouken@0
   358
				posted = SDL_PrivateAppActive(0, SDL_APPMOUSEFOCUS);
slouken@0
   359
			}
slouken@0
   360
		}
slouken@0
   361
		return(0);
slouken@0
   362
#endif /* WM_MOUSELEAVE */
slouken@0
   363
slouken@0
   364
		case WM_LBUTTONDOWN:
slouken@0
   365
		case WM_LBUTTONUP:
slouken@0
   366
		case WM_MBUTTONDOWN:
slouken@0
   367
		case WM_MBUTTONUP:
slouken@0
   368
		case WM_RBUTTONDOWN:
icculus@3970
   369
		case WM_RBUTTONUP:
icculus@3970
   370
		case WM_XBUTTONDOWN:
icculus@3970
   371
		case WM_XBUTTONUP: {
slouken@0
   372
			/* Mouse is handled by DirectInput when fullscreen */
slouken@4167
   373
			if ( SDL_VideoSurface && ! DINPUT() ) {
icculus@3970
   374
				WORD xbuttonval = 0;
slouken@0
   375
				Uint8 button, state;
slouken@5170
   376
                int x, y;
slouken@0
   377
slouken@145
   378
				/* DJM:
slouken@145
   379
				   We want the SDL window to take focus so that
slouken@145
   380
				   it acts like a normal windows "component"
slouken@145
   381
				   (e.g. gains keyboard focus on a mouse click).
slouken@145
   382
				 */
slouken@145
   383
				SetFocus(SDL_Window);
slouken@145
   384
slouken@0
   385
				/* Figure out which button to use */
slouken@0
   386
				switch (msg) {
slouken@0
   387
					case WM_LBUTTONDOWN:
slouken@457
   388
						button = SDL_BUTTON_LEFT;
slouken@0
   389
						state = SDL_PRESSED;
slouken@0
   390
						break;
slouken@0
   391
					case WM_LBUTTONUP:
slouken@457
   392
						button = SDL_BUTTON_LEFT;
slouken@0
   393
						state = SDL_RELEASED;
slouken@0
   394
						break;
slouken@0
   395
					case WM_MBUTTONDOWN:
slouken@457
   396
						button = SDL_BUTTON_MIDDLE;
slouken@0
   397
						state = SDL_PRESSED;
slouken@0
   398
						break;
slouken@0
   399
					case WM_MBUTTONUP:
slouken@457
   400
						button = SDL_BUTTON_MIDDLE;
slouken@0
   401
						state = SDL_RELEASED;
slouken@0
   402
						break;
slouken@0
   403
					case WM_RBUTTONDOWN:
slouken@457
   404
						button = SDL_BUTTON_RIGHT;
slouken@0
   405
						state = SDL_PRESSED;
slouken@0
   406
						break;
slouken@0
   407
					case WM_RBUTTONUP:
slouken@457
   408
						button = SDL_BUTTON_RIGHT;
slouken@0
   409
						state = SDL_RELEASED;
slouken@0
   410
						break;
icculus@3970
   411
					case WM_XBUTTONDOWN:
icculus@3970
   412
						xbuttonval = GET_XBUTTON_WPARAM(wParam);
slouken@4113
   413
						button = SDL_BUTTON_X1 + xbuttonval - 1;
icculus@3970
   414
						state = SDL_PRESSED;
icculus@3970
   415
						break;
icculus@3970
   416
					case WM_XBUTTONUP:
icculus@3970
   417
						xbuttonval = GET_XBUTTON_WPARAM(wParam);
slouken@4113
   418
						button = SDL_BUTTON_X1 + xbuttonval - 1;
icculus@3970
   419
						state = SDL_RELEASED;
icculus@3970
   420
						break;
slouken@0
   421
					default:
slouken@0
   422
						/* Eh? Unknown button? */
slouken@0
   423
						return(0);
slouken@0
   424
				}
slouken@0
   425
				if ( state == SDL_PRESSED ) {
slouken@0
   426
					/* Grab mouse so we get up events */
slouken@0
   427
					if ( ++mouse_pressed > 0 ) {
slouken@0
   428
						SetCapture(hwnd);
slouken@0
   429
					}
slouken@0
   430
				} else {
slouken@0
   431
					/* Release mouse after all up events */
slouken@0
   432
					if ( --mouse_pressed <= 0 ) {
slouken@0
   433
						ReleaseCapture();
slouken@0
   434
						mouse_pressed = 0;
slouken@0
   435
					}
slouken@0
   436
				}
slouken@5077
   437
				if ( mouse_relative ) {
slouken@5077
   438
				/*	RJR: March 28, 2000
slouken@5077
   439
					report internal mouse position if in relative mode */
slouken@5077
   440
					x = 0; y = 0;
slouken@5077
   441
				} else {
slouken@5077
   442
					x = (Sint16)LOWORD(lParam);
slouken@5077
   443
					y = (Sint16)HIWORD(lParam);
slouken@5077
   444
#ifdef _WIN32_WCE
slouken@5077
   445
					if (SDL_VideoSurface)
slouken@5077
   446
						GapiTransform(this->hidden->userOrientation,
slouken@5077
   447
this->hidden->hiresFix, &x, &y);
slouken@5077
   448
#endif
slouken@5077
   449
				}
slouken@0
   450
				posted = SDL_PrivateMouseButton(
slouken@5077
   451
							state, button, x, y);
icculus@3970
   452
icculus@3970
   453
				/*
icculus@3970
   454
				 * MSDN says:
icculus@3970
   455
				 *  "Unlike the WM_LBUTTONUP, WM_MBUTTONUP, and WM_RBUTTONUP
icculus@3970
   456
				 *   messages, an application should return TRUE from [an 
icculus@3970
   457
				 *   XBUTTON message] if it processes it. Doing so will allow
icculus@3970
   458
				 *   software that simulates this message on Microsoft Windows
icculus@3970
   459
				 *   systems earlier than Windows 2000 to determine whether
icculus@3970
   460
				 *   the window procedure processed the message or called
icculus@3970
   461
				 *   DefWindowProc to process it.
icculus@3970
   462
				 */
icculus@3970
   463
				if (xbuttonval > 0)
icculus@3970
   464
					return(TRUE);
slouken@0
   465
			}
slouken@0
   466
		}
slouken@0
   467
		return(0);
slouken@0
   468
slouken@61
   469
slouken@61
   470
#if (_WIN32_WINNT >= 0x0400) || (_WIN32_WINDOWS > 0x0400)
slouken@61
   471
		case WM_MOUSEWHEEL: 
slouken@4167
   472
			if ( SDL_VideoSurface && ! DINPUT() ) {
slouken@61
   473
				int move = (short)HIWORD(wParam);
slouken@162
   474
				if ( move ) {
slouken@162
   475
					Uint8 button;
slouken@162
   476
					if ( move > 0 )
slouken@451
   477
						button = SDL_BUTTON_WHEELUP;
slouken@162
   478
					else
slouken@451
   479
						button = SDL_BUTTON_WHEELDOWN;
slouken@61
   480
					posted = SDL_PrivateMouseButton(
slouken@162
   481
						SDL_PRESSED, button, 0, 0);
slouken@332
   482
					posted |= SDL_PrivateMouseButton(
slouken@332
   483
						SDL_RELEASED, button, 0, 0);
slouken@61
   484
				}
slouken@61
   485
			}
slouken@61
   486
			return(0);
slouken@61
   487
#endif
slouken@61
   488
slouken@0
   489
#ifdef WM_GETMINMAXINFO
slouken@0
   490
		/* This message is sent as a way for us to "check" the values
slouken@0
   491
		 * of a position change.  If we don't like it, we can adjust
slouken@0
   492
		 * the values before they are changed.
slouken@0
   493
		 */
slouken@0
   494
		case WM_GETMINMAXINFO: {
slouken@0
   495
			MINMAXINFO *info;
slouken@0
   496
			RECT        size;
slouken@0
   497
			int x, y;
slouken@565
   498
			int style;
slouken@0
   499
			int width;
slouken@0
   500
			int height;
slouken@0
   501
slouken@0
   502
			/* We don't want to clobber an internal resize */
slouken@0
   503
			if ( SDL_resizing )
slouken@0
   504
				return(0);
slouken@0
   505
slouken@0
   506
			/* We allow resizing with the SDL_RESIZABLE flag */
slouken@0
   507
			if ( SDL_PublicSurface &&
slouken@0
   508
				(SDL_PublicSurface->flags & SDL_RESIZABLE) ) {
slouken@0
   509
				return(0);
slouken@0
   510
			}
slouken@0
   511
slouken@0
   512
			/* Get the current position of our window */
slouken@0
   513
			GetWindowRect(SDL_Window, &size);
slouken@0
   514
			x = size.left;
slouken@0
   515
			y = size.top;
slouken@0
   516
slouken@0
   517
			/* Calculate current width and height of our window */
slouken@0
   518
			size.top = 0;
slouken@0
   519
			size.left = 0;
slouken@0
   520
			if ( SDL_PublicSurface != NULL ) {
slouken@0
   521
				size.bottom = SDL_PublicSurface->h;
slouken@0
   522
				size.right = SDL_PublicSurface->w;
slouken@0
   523
			} else {
slouken@0
   524
				size.bottom = 0;
slouken@0
   525
				size.right = 0;
slouken@0
   526
			}
slouken@565
   527
slouken@565
   528
			/* DJM - according to the docs for GetMenu(), the
slouken@565
   529
			   return value is undefined if hwnd is a child window.
slouken@565
   530
			   Aparently it's too difficult for MS to check
slouken@565
   531
			   inside their function, so I have to do it here.
slouken@565
   532
          		 */
slouken@1480
   533
         		style = GetWindowLong(hwnd, GWL_STYLE);
slouken@565
   534
         		AdjustWindowRect(
slouken@565
   535
				&size,
slouken@565
   536
				style,
slouken@565
   537
            			style & WS_CHILDWINDOW ? FALSE
slouken@565
   538
						       : GetMenu(hwnd) != NULL);
slouken@565
   539
slouken@0
   540
			width = size.right - size.left;
slouken@0
   541
			height = size.bottom - size.top;
slouken@0
   542
slouken@0
   543
			/* Fix our size to the current size */
slouken@0
   544
			info = (MINMAXINFO *)lParam;
slouken@0
   545
			info->ptMaxSize.x = width;
slouken@0
   546
			info->ptMaxSize.y = height;
slouken@0
   547
			info->ptMaxPosition.x = x;
slouken@0
   548
			info->ptMaxPosition.y = y;
slouken@0
   549
			info->ptMinTrackSize.x = width;
slouken@0
   550
			info->ptMinTrackSize.y = height;
slouken@0
   551
			info->ptMaxTrackSize.x = width;
slouken@0
   552
			info->ptMaxTrackSize.y = height;
slouken@0
   553
		}
slouken@0
   554
		return(0);
slouken@0
   555
#endif /* WM_GETMINMAXINFO */
slouken@0
   556
slouken@4250
   557
		case WM_WINDOWPOSCHANGING: {
slouken@4250
   558
			WINDOWPOS *windowpos = (WINDOWPOS*)lParam;
slouken@4250
   559
slouken@4250
   560
			/* When menu is at the side or top, Windows likes
slouken@4250
   561
			   to try to reposition the fullscreen window when
slouken@4250
   562
			   changing video modes.
slouken@4250
   563
			 */
slouken@4250
   564
			if ( !SDL_resizing &&
slouken@4250
   565
			     SDL_PublicSurface &&
slouken@4250
   566
			     (SDL_PublicSurface->flags & SDL_FULLSCREEN) ) {
slouken@4250
   567
				windowpos->x = 0;
slouken@4250
   568
				windowpos->y = 0;
slouken@4250
   569
			}
slouken@4250
   570
		}
slouken@4250
   571
		return(0);
slouken@4250
   572
slouken@447
   573
		case WM_WINDOWPOSCHANGED: {
slouken@0
   574
			SDL_VideoDevice *this = current_video;
icculus@5988
   575
			POINT pt;
slouken@447
   576
			int w, h;
slouken@0
   577
slouken@0
   578
			GetClientRect(SDL_Window, &SDL_bounds);
icculus@5988
   579
icculus@5988
   580
			/* avoiding type-punning here... */
icculus@5988
   581
			pt.x = SDL_bounds.left;
icculus@5988
   582
			pt.y = SDL_bounds.top;
icculus@5988
   583
			ClientToScreen(SDL_Window, &pt);
icculus@5988
   584
			SDL_bounds.left = pt.x;
icculus@5988
   585
			SDL_bounds.top = pt.y;
icculus@5988
   586
icculus@5988
   587
			pt.x = SDL_bounds.right;
icculus@5988
   588
			pt.y = SDL_bounds.bottom;
icculus@5988
   589
			ClientToScreen(SDL_Window, &pt);
icculus@5988
   590
			SDL_bounds.right = pt.x;
icculus@5988
   591
			SDL_bounds.bottom = pt.y;
icculus@5988
   592
slouken@1291
   593
			if ( !SDL_resizing && !IsZoomed(SDL_Window) &&
slouken@1291
   594
			     SDL_PublicSurface &&
slouken@1291
   595
				!(SDL_PublicSurface->flags & SDL_FULLSCREEN) ) {
slouken@1291
   596
				SDL_windowX = SDL_bounds.left;
slouken@1291
   597
				SDL_windowY = SDL_bounds.top;
slouken@1291
   598
			}
slouken@447
   599
			w = SDL_bounds.right-SDL_bounds.left;
slouken@447
   600
			h = SDL_bounds.bottom-SDL_bounds.top;
slouken@0
   601
			if ( this->input_grab != SDL_GRAB_OFF ) {
slouken@0
   602
				ClipCursor(&SDL_bounds);
slouken@0
   603
			}
slouken@447
   604
			if ( SDL_PublicSurface && 
slouken@0
   605
				(SDL_PublicSurface->flags & SDL_RESIZABLE) ) {
slouken@447
   606
				SDL_PrivateResize(w, h);
slouken@0
   607
			}
slouken@0
   608
		}
slouken@0
   609
		break;
slouken@0
   610
slouken@0
   611
		/* We need to set the cursor */
slouken@0
   612
		case WM_SETCURSOR: {
slouken@0
   613
			Uint16 hittest;
slouken@0
   614
slouken@0
   615
			hittest = LOWORD(lParam);
slouken@0
   616
			if ( hittest == HTCLIENT ) {
slouken@0
   617
				SetCursor(SDL_hcursor);
slouken@0
   618
				return(TRUE);
slouken@0
   619
			}
slouken@0
   620
		}
slouken@0
   621
		break;
slouken@0
   622
slouken@0
   623
		/* We are about to get palette focus! */
slouken@0
   624
		case WM_QUERYNEWPALETTE: {
slouken@0
   625
			WIN_RealizePalette(current_video);
slouken@0
   626
			return(TRUE);
slouken@0
   627
		}
slouken@0
   628
		break;
slouken@0
   629
slouken@0
   630
		/* Another application changed the palette */
slouken@0
   631
		case WM_PALETTECHANGED: {
slouken@0
   632
			WIN_PaletteChanged(current_video, (HWND)wParam);
slouken@0
   633
		}
slouken@0
   634
		break;
slouken@0
   635
slouken@0
   636
		/* We were occluded, refresh our display */
slouken@0
   637
		case WM_PAINT: {
slouken@0
   638
			HDC hdc;
slouken@0
   639
			PAINTSTRUCT ps;
slouken@0
   640
slouken@0
   641
			hdc = BeginPaint(SDL_Window, &ps);
slouken@0
   642
			if ( current_video->screen &&
slouken@0
   643
			     !(current_video->screen->flags & SDL_OPENGL) ) {
slouken@0
   644
				WIN_WinPAINT(current_video, hdc);
slouken@0
   645
			}
slouken@0
   646
			EndPaint(SDL_Window, &ps);
slouken@0
   647
		}
slouken@0
   648
		return(0);
slouken@0
   649
slouken@145
   650
		/* DJM: Send an expose event in this case */
slouken@0
   651
		case WM_ERASEBKGND: {
slouken@145
   652
			posted = SDL_PrivateExpose();
slouken@0
   653
		}
slouken@145
   654
		return(0);
slouken@0
   655
slouken@0
   656
		case WM_CLOSE: {
slouken@0
   657
			if ( (posted = SDL_PrivateQuit()) )
slouken@0
   658
				PostQuitMessage(0);
slouken@0
   659
		}
slouken@0
   660
		return(0);
slouken@0
   661
slouken@0
   662
		case WM_DESTROY: {
slouken@0
   663
			PostQuitMessage(0);
slouken@0
   664
		}
slouken@0
   665
		return(0);
slouken@0
   666
slouken@1272
   667
#ifndef NO_GETKEYBOARDSTATE
icculus@5983
   668
		case WM_INPUTLANGCHANGE:
icculus@5983
   669
#ifndef _WIN64
slouken@1253
   670
			codepage = GetCodePage();
icculus@5983
   671
#endif
slouken@1253
   672
		return(TRUE);
slouken@1272
   673
#endif
slouken@1253
   674
slouken@0
   675
		default: {
slouken@0
   676
			/* Special handling by the video driver */
slouken@0
   677
			if (HandleMessage) {
slouken@0
   678
				return(HandleMessage(current_video,
slouken@0
   679
			                     hwnd, msg, wParam, lParam));
slouken@0
   680
			}
slouken@0
   681
		}
slouken@0
   682
		break;
slouken@0
   683
	}
slouken@0
   684
	return(DefWindowProc(hwnd, msg, wParam, lParam));
slouken@0
   685
}
slouken@0
   686
slouken@145
   687
/* Allow the application handle to be stored and retrieved later */
slouken@149
   688
static void *SDL_handle = NULL;
slouken@145
   689
slouken@149
   690
void SDL_SetModuleHandle(void *handle)
slouken@145
   691
{
slouken@145
   692
	SDL_handle = handle;
slouken@145
   693
}
slouken@149
   694
void *SDL_GetModuleHandle(void)
slouken@145
   695
{
slouken@145
   696
	void *handle;
slouken@145
   697
slouken@145
   698
	if ( SDL_handle ) {
slouken@145
   699
		handle = SDL_handle;
slouken@145
   700
	} else {
slouken@145
   701
		handle = GetModuleHandle(NULL);
slouken@145
   702
	}
slouken@145
   703
	return(handle);
slouken@145
   704
}
slouken@145
   705
slouken@0
   706
/* This allows the SDL_WINDOWID hack */
slouken@1280
   707
BOOL SDL_windowid = FALSE;
slouken@0
   708
icculus@1145
   709
static int app_registered = 0;
icculus@1145
   710
slouken@0
   711
/* Register the class for this application -- exported for winmain.c */
slouken@149
   712
int SDL_RegisterApp(char *name, Uint32 style, void *hInst)
slouken@0
   713
{
slouken@0
   714
	WNDCLASS class;
slouken@0
   715
#ifdef WM_MOUSELEAVE
slouken@0
   716
	HMODULE handle;
slouken@0
   717
#endif
slouken@0
   718
slouken@0
   719
	/* Only do this once... */
icculus@1145
   720
	if ( app_registered ) {
slouken@1498
   721
		++app_registered;
slouken@0
   722
		return(0);
slouken@0
   723
	}
slouken@0
   724
slouken@1288
   725
#ifndef CS_BYTEALIGNCLIENT
slouken@1288
   726
#define CS_BYTEALIGNCLIENT	0
slouken@1288
   727
#endif
slouken@1288
   728
	if ( ! name && ! SDL_Appname ) {
slouken@1288
   729
		name = "SDL_app";
slouken@1288
   730
		SDL_Appstyle = CS_BYTEALIGNCLIENT;
slouken@1288
   731
		SDL_Instance = hInst ? hInst : SDL_GetModuleHandle();
slouken@1288
   732
	}
slouken@1288
   733
slouken@1288
   734
	if ( name ) {
slouken@1288
   735
#ifdef _WIN32_WCE
slouken@1288
   736
		/* WinCE uses the UNICODE version */
slouken@1505
   737
		SDL_Appname = SDL_iconv_utf8_ucs2(name);
slouken@1288
   738
#else
slouken@4007
   739
		SDL_Appname = SDL_iconv_utf8_locale(name);
slouken@1288
   740
#endif /* _WIN32_WCE */
slouken@1288
   741
		SDL_Appstyle = style;
slouken@1288
   742
		SDL_Instance = hInst ? hInst : SDL_GetModuleHandle();
slouken@0
   743
	}
slouken@0
   744
slouken@0
   745
	/* Register the application class */
slouken@0
   746
	class.hCursor		= NULL;
slouken@1288
   747
	class.hIcon		= LoadImage(SDL_Instance, SDL_Appname,
slouken@1288
   748
				            IMAGE_ICON,
slouken@0
   749
	                                    0, 0, LR_DEFAULTCOLOR);
slouken@36
   750
	class.lpszMenuName	= NULL;
slouken@453
   751
	class.lpszClassName	= SDL_Appname;
slouken@0
   752
	class.hbrBackground	= NULL;
slouken@1288
   753
	class.hInstance		= SDL_Instance;
slouken@1288
   754
	class.style		= SDL_Appstyle;
slouken@1361
   755
#if SDL_VIDEO_OPENGL
slouken@0
   756
	class.style		|= CS_OWNDC;
slouken@0
   757
#endif
slouken@0
   758
	class.lpfnWndProc	= WinMessage;
slouken@0
   759
	class.cbWndExtra	= 0;
slouken@0
   760
	class.cbClsExtra	= 0;
slouken@0
   761
	if ( ! RegisterClass(&class) ) {
slouken@0
   762
		SDL_SetError("Couldn't register application class");
slouken@0
   763
		return(-1);
slouken@0
   764
	}
slouken@0
   765
slouken@0
   766
#ifdef WM_MOUSELEAVE
slouken@0
   767
	/* Get the version of TrackMouseEvent() we use */
slouken@0
   768
	_TrackMouseEvent = NULL;
slouken@0
   769
	handle = GetModuleHandle("USER32.DLL");
slouken@0
   770
	if ( handle ) {
slouken@0
   771
		_TrackMouseEvent = (BOOL (WINAPI *)(TRACKMOUSEEVENT *))GetProcAddress(handle, "TrackMouseEvent");
slouken@0
   772
	}
slouken@0
   773
	if ( _TrackMouseEvent == NULL ) {
slouken@0
   774
		_TrackMouseEvent = WIN_TrackMouseEvent;
slouken@0
   775
	}
slouken@0
   776
#endif /* WM_MOUSELEAVE */
slouken@0
   777
slouken@1272
   778
#ifndef NO_GETKEYBOARDSTATE
icculus@5983
   779
#ifndef _WIN64
slouken@1253
   780
	/* Initialise variables for SDL_ToUnicode() */
slouken@1253
   781
	codepage = GetCodePage();
icculus@5897
   782
icculus@5897
   783
	/* Cygwin headers don't match windows.h, so we have to cast around a
icculus@5897
   784
	   const issue here... */
icculus@5898
   785
	SDL_ToUnicode = Is9xME() ? ToUnicode9xME : (ToUnicodeFN) ToUnicode;
slouken@1272
   786
#endif
icculus@5983
   787
#endif /* NO_GETKEYBOARDSTATE */
slouken@1253
   788
icculus@1145
   789
	app_registered = 1;
slouken@0
   790
	return(0);
slouken@0
   791
}
slouken@0
   792
slouken@1288
   793
/* Unregisters the windowclass registered in SDL_RegisterApp above. */
icculus@1150
   794
void SDL_UnregisterApp()
icculus@1145
   795
{
icculus@1145
   796
	WNDCLASS class;
icculus@1145
   797
icculus@1145
   798
	/* SDL_RegisterApp might not have been called before */
slouken@1498
   799
	if ( !app_registered ) {
slouken@1498
   800
		return;
slouken@1498
   801
	}
slouken@1498
   802
	--app_registered;
slouken@1498
   803
	if ( app_registered == 0 ) {
slouken@1288
   804
		/* Check for any registered window classes. */
slouken@1288
   805
		if ( GetClassInfo(SDL_Instance, SDL_Appname, &class) ) {
icculus@1145
   806
			UnregisterClass(SDL_Appname, SDL_Instance);
icculus@1145
   807
		}
slouken@1498
   808
		SDL_free(SDL_Appname);
slouken@1498
   809
		SDL_Appname = NULL;
icculus@1145
   810
	}
icculus@1145
   811
}
icculus@1145
   812
slouken@1272
   813
#ifndef NO_GETKEYBOARDSTATE
icculus@5983
   814
#ifndef _WIN64
slouken@1253
   815
/* JFP: Implementation of ToUnicode() that works on 9x/ME/2K/XP */
slouken@1253
   816
slouken@1253
   817
static int Is9xME()
slouken@1253
   818
{
slouken@1253
   819
	OSVERSIONINFO   info;
slouken@1253
   820
slouken@1336
   821
	SDL_memset(&info, 0, sizeof(info));
slouken@1253
   822
	info.dwOSVersionInfoSize = sizeof(info);
slouken@1253
   823
	if (!GetVersionEx(&info)) {
slouken@1253
   824
		return 0;
slouken@1253
   825
	}
slouken@1253
   826
	return (info.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS);
slouken@1253
   827
}
slouken@1253
   828
slouken@1253
   829
static int GetCodePage()
slouken@1253
   830
{
slouken@1253
   831
	char	buff[8];
slouken@1253
   832
	int	lcid = MAKELCID(LOWORD(GetKeyboardLayout(0)), SORT_DEFAULT);
slouken@1253
   833
	int	cp = GetACP();
slouken@1253
   834
slouken@1253
   835
	if (GetLocaleInfo(lcid, LOCALE_IDEFAULTANSICODEPAGE, buff, sizeof(buff))) {
slouken@1341
   836
		cp = SDL_atoi(buff);
slouken@1253
   837
	}
slouken@1253
   838
	return cp;
slouken@1253
   839
}
slouken@1253
   840
icculus@5891
   841
static int WINAPI ToUnicode9xME(UINT vkey, UINT scancode, const BYTE *keystate, LPWSTR wchars, int wsize, UINT flags)
slouken@1253
   842
{
slouken@1253
   843
	BYTE	chars[2];
slouken@1253
   844
icculus@5892
   845
	/* arg #3 should be const BYTE *, but cygwin lists it as PBYTE. */
icculus@5892
   846
	if (ToAsciiEx(vkey, scancode, (PBYTE) keystate, (WORD*)chars, 0, GetKeyboardLayout(0)) == 1) {
icculus@5988
   847
		return MultiByteToWideChar(codepage, 0, (LPCSTR) chars, 1, wchars, wsize);
slouken@1253
   848
	}
slouken@1253
   849
	return 0;
slouken@1253
   850
}
icculus@5983
   851
#endif
slouken@1272
   852
#endif /* !NO_GETKEYBOARDSTATE */