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