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