src/video/wincommon/SDL_sysevents.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 19 Jan 2006 09:09:32 +0000
changeset 1253 7c7ddaf195bf
parent 1251 86d0d01290ea
child 1272 94c0709f8856
permissions -rw-r--r--
Implemented ToUnicode() support on Windows 95/98/ME/NT/2000/XP
This is a collaborative effort between Alex Volkov and John Popplewell.
Thanks guys! (Fixes bug #39)
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@0
     3
    Copyright (C) 1997, 1998, 1999  Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@0
     6
    modify it under the terms of the GNU Library General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@0
     8
    version 2 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@0
    13
    Library General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU Library General Public
slouken@0
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
#ifdef SAVE_RCSID
slouken@0
    24
static char rcsid =
slouken@0
    25
 "@(#) $Id$";
slouken@0
    26
#endif
slouken@0
    27
slouken@0
    28
#include <stdlib.h>
slouken@0
    29
#include <stdio.h>
slouken@0
    30
#include <windows.h>
slouken@0
    31
slouken@0
    32
#include "SDL_getenv.h"
slouken@0
    33
#include "SDL_events.h"
slouken@0
    34
#include "SDL_video.h"
slouken@0
    35
#include "SDL_error.h"
slouken@0
    36
#include "SDL_syswm.h"
slouken@0
    37
#include "SDL_sysevents.h"
slouken@0
    38
#include "SDL_events_c.h"
slouken@0
    39
#include "SDL_sysvideo.h"
slouken@0
    40
#include "SDL_lowvideo.h"
slouken@0
    41
#include "SDL_syswm_c.h"
slouken@0
    42
#include "SDL_main.h"
icculus@1152
    43
#include "SDL_loadso.h"
slouken@0
    44
slouken@0
    45
#ifdef WMMSG_DEBUG
slouken@0
    46
#include "wmmsg.h"
slouken@0
    47
#endif
slouken@0
    48
slouken@0
    49
#ifdef _WIN32_WCE
icculus@1251
    50
#include "SDL_gapivideo.h"
slouken@0
    51
#define NO_GETKEYBOARDSTATE
slouken@453
    52
#define NO_CHANGEDISPLAYSETTINGS
slouken@0
    53
#endif
slouken@0
    54
slouken@0
    55
/* The window we use for everything... */
slouken@453
    56
#ifdef _WIN32_WCE
slouken@453
    57
LPWSTR SDL_Appname = NULL;
slouken@453
    58
#else
slouken@453
    59
LPSTR SDL_Appname = NULL;
slouken@453
    60
#endif
slouken@0
    61
HINSTANCE SDL_Instance = NULL;
slouken@0
    62
HWND SDL_Window = NULL;
slouken@0
    63
RECT SDL_bounds = {0, 0, 0, 0};
slouken@833
    64
int SDL_windowX = 0;
slouken@833
    65
int SDL_windowY = 0;
slouken@0
    66
int SDL_resizing = 0;
slouken@0
    67
int mouse_relative = 0;
slouken@0
    68
int posted = 0;
slouken@304
    69
#ifndef NO_CHANGEDISPLAYSETTINGS
slouken@304
    70
DEVMODE SDL_fullscreen_mode;
slouken@304
    71
#endif
slouken@338
    72
WORD *gamma_saved = NULL;
slouken@0
    73
slouken@0
    74
slouken@0
    75
/* Functions called by the message processing function */
icculus@1152
    76
LONG (*HandleMessage)(_THIS, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)=NULL;
slouken@0
    77
void (*WIN_RealizePalette)(_THIS);
slouken@0
    78
void (*WIN_PaletteChanged)(_THIS, HWND window);
slouken@0
    79
void (*WIN_WinPAINT)(_THIS, HDC hdc);
slouken@338
    80
extern void DIB_SwapGamma(_THIS);
slouken@0
    81
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@1253
    86
static int WINAPI ToUnicode9xME(UINT vkey, UINT scancode, BYTE *keystate, Uint16 *wchars, int wsize, UINT flags);
slouken@1253
    87
slouken@1253
    88
ToUnicodeFN SDL_ToUnicode = ToUnicode9xME;
slouken@1253
    89
slouken@1253
    90
icculus@1152
    91
#if defined(_WIN32_WCE)
icculus@1152
    92
icculus@1152
    93
// dynamically load aygshell dll because we want SDL to work on HPC and be300
icculus@1152
    94
HINSTANCE aygshell = NULL;
icculus@1152
    95
BOOL (WINAPI *SHFullScreen)(HWND hwndRequester, DWORD dwState) = 0;
icculus@1152
    96
icculus@1152
    97
#define SHFS_SHOWTASKBAR            0x0001
icculus@1152
    98
#define SHFS_HIDETASKBAR            0x0002
icculus@1152
    99
#define SHFS_SHOWSIPBUTTON          0x0004
icculus@1152
   100
#define SHFS_HIDESIPBUTTON          0x0008
icculus@1152
   101
#define SHFS_SHOWSTARTICON          0x0010
icculus@1152
   102
#define SHFS_HIDESTARTICON          0x0020
icculus@1152
   103
icculus@1152
   104
static void LoadAygshell(void)
icculus@1152
   105
{
icculus@1152
   106
	if( !aygshell )
icculus@1152
   107
		 aygshell = SDL_LoadObject("aygshell.dll");
icculus@1152
   108
	if( aygshell )
icculus@1152
   109
	{
icculus@1152
   110
		SHFullScreen = (int (WINAPI *)(struct HWND__ *,unsigned long)) SDL_LoadFunction(aygshell, "SHFullScreen");
icculus@1152
   111
	}
icculus@1152
   112
}
icculus@1152
   113
icculus@1251
   114
/* for gapi landscape mode */
icculus@1251
   115
static void GapiTransform(SDL_ScreenOrientation rotate, char hires, Sint16 *x, Sint16 *y) {
icculus@1251
   116
	Sint16 rotatedX;
icculus@1251
   117
	Sint16 rotatedY;
icculus@1251
   118
icculus@1251
   119
	if (hires) {
icculus@1251
   120
		*x = *x * 2;
icculus@1251
   121
		*y = *y * 2;
icculus@1251
   122
	}
icculus@1251
   123
icculus@1251
   124
	switch(rotate) {
icculus@1251
   125
		case SDL_ORIENTATION_UP:
icculus@1251
   126
			break;
icculus@1251
   127
		case SDL_ORIENTATION_RIGHT:
icculus@1251
   128
			if (!SDL_VideoSurface)
icculus@1251
   129
				break;
icculus@1251
   130
			rotatedX = SDL_VideoSurface->w - *y;
icculus@1251
   131
			rotatedY = *x;
icculus@1251
   132
			*x = rotatedX;
icculus@1251
   133
			*y = rotatedY;
icculus@1251
   134
			break;
icculus@1251
   135
		case SDL_ORIENTATION_LEFT:
icculus@1251
   136
			if (!SDL_VideoSurface)
icculus@1251
   137
				break;
icculus@1251
   138
			rotatedX = *y;
icculus@1251
   139
			rotatedY = SDL_VideoSurface->h - *x;
icculus@1251
   140
			*x = rotatedX;
icculus@1251
   141
			*y = rotatedY;
icculus@1251
   142
			break;
icculus@1251
   143
	}
icculus@1251
   144
}
icculus@1251
   145
icculus@1152
   146
#endif
icculus@1152
   147
slouken@304
   148
static void SDL_RestoreGameMode(void)
slouken@304
   149
{
slouken@304
   150
#ifndef NO_CHANGEDISPLAYSETTINGS
slouken@304
   151
	ShowWindow(SDL_Window, SW_RESTORE);
slouken@304
   152
	ChangeDisplaySettings(&SDL_fullscreen_mode, CDS_FULLSCREEN);
slouken@304
   153
#endif
slouken@304
   154
}
slouken@304
   155
static void SDL_RestoreDesktopMode(void)
slouken@304
   156
{
slouken@304
   157
#ifndef NO_CHANGEDISPLAYSETTINGS
slouken@304
   158
	ShowWindow(SDL_Window, SW_MINIMIZE);
slouken@304
   159
	ChangeDisplaySettings(NULL, 0);
slouken@304
   160
#endif
slouken@304
   161
}
slouken@304
   162
slouken@0
   163
#ifdef WM_MOUSELEAVE
slouken@0
   164
/* 
slouken@0
   165
   Special code to handle mouse leave events - this sucks...
slouken@0
   166
   http://support.microsoft.com/support/kb/articles/q183/1/07.asp
slouken@0
   167
slouken@0
   168
   TrackMouseEvent() is only available on Win98 and WinNT.
slouken@0
   169
   _TrackMouseEvent() is available on Win95, but isn't yet in the mingw32
slouken@0
   170
   development environment, and only works on systems that have had IE 3.0
slouken@0
   171
   or newer installed on them (which is not the case with the base Win95).
slouken@0
   172
   Therefore, we implement our own version of _TrackMouseEvent() which
slouken@0
   173
   uses our own implementation if TrackMouseEvent() is not available.
slouken@0
   174
*/
slouken@0
   175
static BOOL (WINAPI *_TrackMouseEvent)(TRACKMOUSEEVENT *ptme) = NULL;
slouken@0
   176
slouken@0
   177
static VOID CALLBACK
slouken@0
   178
TrackMouseTimerProc(HWND hWnd, UINT uMsg, UINT idEvent, DWORD dwTime)
slouken@0
   179
{
slouken@0
   180
	RECT rect;
slouken@0
   181
	POINT pt;
slouken@0
   182
slouken@0
   183
	GetClientRect(hWnd, &rect);
slouken@0
   184
	MapWindowPoints(hWnd, NULL, (LPPOINT)&rect, 2);
slouken@0
   185
	GetCursorPos(&pt);
slouken@0
   186
	if ( !PtInRect(&rect, pt) || (WindowFromPoint(pt) != hWnd) ) {
slouken@0
   187
		if ( !KillTimer(hWnd, idEvent) ) {
slouken@0
   188
			/* Error killing the timer! */
slouken@0
   189
		}
slouken@0
   190
		PostMessage(hWnd, WM_MOUSELEAVE, 0, 0);
slouken@0
   191
	}
slouken@0
   192
}
slouken@0
   193
static BOOL WINAPI WIN_TrackMouseEvent(TRACKMOUSEEVENT *ptme)
slouken@0
   194
{
slouken@0
   195
	if ( ptme->dwFlags == TME_LEAVE ) {
slouken@0
   196
		return SetTimer(ptme->hwndTrack, ptme->dwFlags, 100,
slouken@0
   197
		                (TIMERPROC)TrackMouseTimerProc);
slouken@0
   198
	}
slouken@0
   199
	return FALSE;
slouken@0
   200
}
slouken@0
   201
#endif /* WM_MOUSELEAVE */
slouken@0
   202
slouken@0
   203
/* Function to retrieve the current keyboard modifiers */
slouken@0
   204
static void WIN_GetKeyboardState(void)
slouken@0
   205
{
slouken@0
   206
#ifndef NO_GETKEYBOARDSTATE
slouken@0
   207
	SDLMod state;
slouken@0
   208
	BYTE keyboard[256];
slouken@327
   209
	Uint8 *kstate = SDL_GetKeyState(NULL);
slouken@0
   210
slouken@0
   211
	state = KMOD_NONE;
slouken@0
   212
	if ( GetKeyboardState(keyboard) ) {
slouken@0
   213
		if ( keyboard[VK_LSHIFT] & 0x80) {
slouken@0
   214
			state |= KMOD_LSHIFT;
slouken@0
   215
		}
slouken@0
   216
		if ( keyboard[VK_RSHIFT] & 0x80) {
slouken@0
   217
			state |= KMOD_RSHIFT;
slouken@0
   218
		}
slouken@0
   219
		if ( keyboard[VK_LCONTROL] & 0x80) {
slouken@0
   220
			state |= KMOD_LCTRL;
slouken@0
   221
		}
slouken@0
   222
		if ( keyboard[VK_RCONTROL] & 0x80) {
slouken@0
   223
			state |= KMOD_RCTRL;
slouken@0
   224
		}
slouken@0
   225
		if ( keyboard[VK_LMENU] & 0x80) {
slouken@0
   226
			state |= KMOD_LALT;
slouken@0
   227
		}
slouken@0
   228
		if ( keyboard[VK_RMENU] & 0x80) {
slouken@0
   229
			state |= KMOD_RALT;
slouken@0
   230
		}
slouken@1
   231
		if ( keyboard[VK_NUMLOCK] & 0x01) {
slouken@0
   232
			state |= KMOD_NUM;
slouken@327
   233
			kstate[SDLK_NUMLOCK] = SDL_PRESSED;
slouken@0
   234
		}
slouken@1
   235
		if ( keyboard[VK_CAPITAL] & 0x01) {
slouken@0
   236
			state |= KMOD_CAPS;
slouken@327
   237
			kstate[SDLK_CAPSLOCK] = SDL_PRESSED;
slouken@0
   238
		}
slouken@0
   239
	}
slouken@0
   240
	SDL_SetModState(state);
slouken@0
   241
#endif /* !NO_GETKEYBOARDSTATE */
slouken@0
   242
}
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@145
   247
LONG 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
	static int in_window = 0;
slouken@0
   252
#ifdef WMMSG_DEBUG
slouken@0
   253
	fprintf(stderr, "Received windows message:  ");
slouken@0
   254
	if ( msg > MAX_WMMSG ) {
slouken@0
   255
		fprintf(stderr, "%d", msg);
slouken@0
   256
	} else {
slouken@0
   257
		fprintf(stderr, "%s", wmtab[msg]);
slouken@0
   258
	}
slouken@0
   259
	fprintf(stderr, " -- 0x%X, 0x%X\n", wParam, lParam);
slouken@0
   260
#endif
slouken@0
   261
	switch (msg) {
slouken@0
   262
slouken@0
   263
		case WM_ACTIVATE: {
slouken@0
   264
			SDL_VideoDevice *this = current_video;
slouken@0
   265
			BOOL minimized;
slouken@0
   266
			Uint8 appstate;
slouken@0
   267
slouken@0
   268
			minimized = HIWORD(wParam);
slouken@0
   269
			if ( !minimized && (LOWORD(wParam) != WA_INACTIVE) ) {
slouken@0
   270
				/* Gain the following states */
slouken@0
   271
				appstate = SDL_APPACTIVE|SDL_APPINPUTFOCUS;
slouken@0
   272
				if ( this->input_grab != SDL_GRAB_OFF ) {
slouken@0
   273
					WIN_GrabInput(this, SDL_GRAB_ON);
slouken@0
   274
				}
slouken@0
   275
				if ( !(SDL_GetAppState()&SDL_APPINPUTFOCUS) ) {
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)
icculus@1152
   284
			if ( WINDIB_FULLSCREEN() )
icculus@1152
   285
			{
icculus@1152
   286
						LoadAygshell();
icculus@1152
   287
						if( aygshell ) 
icculus@1152
   288
							SHFullScreen(SDL_Window, SHFS_HIDESTARTICON|SHFS_HIDETASKBAR|SHFS_HIDESIPBUTTON);
icculus@1152
   289
						else
icculus@1152
   290
							ShowWindow(FindWindow(TEXT("HHTaskBar"),NULL),SW_HIDE);
icculus@1152
   291
icculus@1152
   292
			}
icculus@1152
   293
#endif
icculus@1152
   294
slouken@0
   295
				posted = SDL_PrivateAppActive(1, appstate);
slouken@0
   296
				WIN_GetKeyboardState();
slouken@0
   297
			} else {
slouken@0
   298
				/* Lose the following states */
slouken@0
   299
				appstate = SDL_APPINPUTFOCUS;
slouken@0
   300
				if ( minimized ) {
slouken@0
   301
					appstate |= SDL_APPACTIVE;
slouken@0
   302
				}
slouken@0
   303
				if ( this->input_grab != SDL_GRAB_OFF ) {
slouken@0
   304
					WIN_GrabInput(this, SDL_GRAB_OFF);
slouken@0
   305
				}
slouken@0
   306
				if ( SDL_GetAppState() & SDL_APPINPUTFOCUS ) {
slouken@338
   307
					if ( ! DDRAW_FULLSCREEN() ) {
slouken@338
   308
						DIB_SwapGamma(this);
slouken@338
   309
					}
slouken@304
   310
					if ( WINDIB_FULLSCREEN() ) {
slouken@304
   311
						SDL_RestoreDesktopMode();
icculus@1152
   312
#if defined(_WIN32_WCE)
icculus@1152
   313
						LoadAygshell();
icculus@1152
   314
						if( aygshell ) 
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
icculus@1152
   319
#endif
slouken@304
   320
					}
slouken@0
   321
				}
slouken@0
   322
				posted = SDL_PrivateAppActive(0, appstate);
slouken@0
   323
			}
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@0
   330
			/* Mouse is handled by DirectInput when fullscreen */
slouken@13
   331
			if ( SDL_VideoSurface && ! DINPUT_FULLSCREEN() ) {
slouken@0
   332
				Sint16 x, y;
slouken@0
   333
slouken@0
   334
				/* mouse has entered the window */
slouken@0
   335
				if ( ! in_window ) {
slouken@0
   336
#ifdef WM_MOUSELEAVE
slouken@0
   337
					TRACKMOUSEEVENT tme;
slouken@0
   338
slouken@0
   339
					tme.cbSize = sizeof(tme);
slouken@0
   340
					tme.dwFlags = TME_LEAVE;
slouken@0
   341
					tme.hwndTrack = SDL_Window;
slouken@0
   342
					_TrackMouseEvent(&tme);
slouken@0
   343
#endif /* WM_MOUSELEAVE */
slouken@0
   344
					in_window = TRUE;
slouken@0
   345
slouken@0
   346
					posted = SDL_PrivateAppActive(1, SDL_APPMOUSEFOCUS);
slouken@0
   347
				}
slouken@0
   348
slouken@0
   349
				/* mouse has moved within the window */
slouken@0
   350
				x = LOWORD(lParam);
slouken@0
   351
				y = HIWORD(lParam);
slouken@0
   352
				if ( mouse_relative ) {
slouken@0
   353
					POINT center;
slouken@0
   354
					center.x = (SDL_VideoSurface->w/2);
slouken@0
   355
					center.y = (SDL_VideoSurface->h/2);
slouken@0
   356
					x -= (Sint16)center.x;
slouken@0
   357
					y -= (Sint16)center.y;
slouken@0
   358
					if ( x || y ) {
slouken@0
   359
						ClientToScreen(SDL_Window, &center);
slouken@0
   360
						SetCursorPos(center.x, center.y);
slouken@0
   361
						posted = SDL_PrivateMouseMotion(0, 1, x, y);
slouken@0
   362
					}
slouken@0
   363
				} else {
icculus@1251
   364
#ifdef _WIN32_WCE
icculus@1251
   365
					if (SDL_VideoSurface)
icculus@1251
   366
						GapiTransform(this->hidden->userOrientation, this->hidden->hiresFix, &x, &y);
icculus@1251
   367
#endif
slouken@0
   368
					posted = SDL_PrivateMouseMotion(0, 0, x, y);
slouken@0
   369
				}
slouken@0
   370
			}
slouken@0
   371
		}
slouken@0
   372
		return(0);
slouken@0
   373
slouken@0
   374
#ifdef WM_MOUSELEAVE
slouken@0
   375
		case WM_MOUSELEAVE: {
slouken@0
   376
slouken@0
   377
			/* Mouse is handled by DirectInput when fullscreen */
slouken@13
   378
			if ( SDL_VideoSurface && ! DINPUT_FULLSCREEN() ) {
slouken@0
   379
				/* mouse has left the window */
slouken@0
   380
				/* or */
slouken@0
   381
				/* Elvis has left the building! */
slouken@0
   382
				posted = SDL_PrivateAppActive(0, SDL_APPMOUSEFOCUS);
slouken@0
   383
			}
slouken@829
   384
			in_window = FALSE;
slouken@0
   385
		}
slouken@0
   386
		return(0);
slouken@0
   387
#endif /* WM_MOUSELEAVE */
slouken@0
   388
slouken@0
   389
		case WM_LBUTTONDOWN:
slouken@0
   390
		case WM_LBUTTONUP:
slouken@0
   391
		case WM_MBUTTONDOWN:
slouken@0
   392
		case WM_MBUTTONUP:
slouken@0
   393
		case WM_RBUTTONDOWN:
slouken@0
   394
		case WM_RBUTTONUP: {
slouken@0
   395
			/* Mouse is handled by DirectInput when fullscreen */
slouken@13
   396
			if ( SDL_VideoSurface && ! DINPUT_FULLSCREEN() ) {
slouken@0
   397
				Sint16 x, y;
slouken@0
   398
				Uint8 button, state;
slouken@0
   399
slouken@145
   400
				/* DJM:
slouken@145
   401
				   We want the SDL window to take focus so that
slouken@145
   402
				   it acts like a normal windows "component"
slouken@145
   403
				   (e.g. gains keyboard focus on a mouse click).
slouken@145
   404
				 */
slouken@145
   405
				SetFocus(SDL_Window);
slouken@145
   406
slouken@0
   407
				/* Figure out which button to use */
slouken@0
   408
				switch (msg) {
slouken@0
   409
					case WM_LBUTTONDOWN:
slouken@457
   410
						button = SDL_BUTTON_LEFT;
slouken@0
   411
						state = SDL_PRESSED;
slouken@0
   412
						break;
slouken@0
   413
					case WM_LBUTTONUP:
slouken@457
   414
						button = SDL_BUTTON_LEFT;
slouken@0
   415
						state = SDL_RELEASED;
slouken@0
   416
						break;
slouken@0
   417
					case WM_MBUTTONDOWN:
slouken@457
   418
						button = SDL_BUTTON_MIDDLE;
slouken@0
   419
						state = SDL_PRESSED;
slouken@0
   420
						break;
slouken@0
   421
					case WM_MBUTTONUP:
slouken@457
   422
						button = SDL_BUTTON_MIDDLE;
slouken@0
   423
						state = SDL_RELEASED;
slouken@0
   424
						break;
slouken@0
   425
					case WM_RBUTTONDOWN:
slouken@457
   426
						button = SDL_BUTTON_RIGHT;
slouken@0
   427
						state = SDL_PRESSED;
slouken@0
   428
						break;
slouken@0
   429
					case WM_RBUTTONUP:
slouken@457
   430
						button = SDL_BUTTON_RIGHT;
slouken@0
   431
						state = SDL_RELEASED;
slouken@0
   432
						break;
slouken@0
   433
					default:
slouken@0
   434
						/* Eh? Unknown button? */
slouken@0
   435
						return(0);
slouken@0
   436
				}
slouken@0
   437
				if ( state == SDL_PRESSED ) {
slouken@0
   438
					/* Grab mouse so we get up events */
slouken@0
   439
					if ( ++mouse_pressed > 0 ) {
slouken@0
   440
						SetCapture(hwnd);
slouken@0
   441
					}
slouken@0
   442
				} else {
slouken@0
   443
					/* Release mouse after all up events */
slouken@0
   444
					if ( --mouse_pressed <= 0 ) {
slouken@0
   445
						ReleaseCapture();
slouken@0
   446
						mouse_pressed = 0;
slouken@0
   447
					}
slouken@0
   448
				}
slouken@0
   449
				if ( mouse_relative ) {
slouken@0
   450
				/*	RJR: March 28, 2000
slouken@0
   451
					report internal mouse position if in relative mode */
slouken@0
   452
					x = 0; y = 0;
slouken@0
   453
				} else {
slouken@0
   454
					x = (Sint16)LOWORD(lParam);
slouken@0
   455
					y = (Sint16)HIWORD(lParam);
icculus@1251
   456
#ifdef _WIN32_WCE
icculus@1251
   457
					if (SDL_VideoSurface)
icculus@1251
   458
						GapiTransform(this->hidden->userOrientation, this->hidden->hiresFix, &x, &y);
icculus@1251
   459
#endif
slouken@0
   460
				}
slouken@0
   461
				posted = SDL_PrivateMouseButton(
slouken@0
   462
							state, button, x, y);
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@61
   470
			if ( SDL_VideoSurface && ! DINPUT_FULLSCREEN() ) {
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@565
   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@447
   555
		case WM_WINDOWPOSCHANGED: {
slouken@0
   556
			SDL_VideoDevice *this = current_video;
slouken@447
   557
			int w, h;
slouken@0
   558
slouken@0
   559
			GetClientRect(SDL_Window, &SDL_bounds);
slouken@0
   560
			ClientToScreen(SDL_Window, (LPPOINT)&SDL_bounds);
slouken@0
   561
			ClientToScreen(SDL_Window, (LPPOINT)&SDL_bounds+1);
slouken@833
   562
			if ( SDL_bounds.left || SDL_bounds.top ) {
slouken@833
   563
				SDL_windowX = SDL_bounds.left;
slouken@833
   564
				SDL_windowY = SDL_bounds.top;
slouken@833
   565
			}
slouken@447
   566
			w = SDL_bounds.right-SDL_bounds.left;
slouken@447
   567
			h = SDL_bounds.bottom-SDL_bounds.top;
slouken@0
   568
			if ( this->input_grab != SDL_GRAB_OFF ) {
slouken@0
   569
				ClipCursor(&SDL_bounds);
slouken@0
   570
			}
slouken@447
   571
			if ( SDL_PublicSurface && 
slouken@0
   572
				(SDL_PublicSurface->flags & SDL_RESIZABLE) ) {
slouken@447
   573
				SDL_PrivateResize(w, h);
slouken@0
   574
			}
slouken@0
   575
		}
slouken@0
   576
		break;
slouken@0
   577
slouken@0
   578
		/* We need to set the cursor */
slouken@0
   579
		case WM_SETCURSOR: {
slouken@0
   580
			Uint16 hittest;
slouken@0
   581
slouken@0
   582
			hittest = LOWORD(lParam);
slouken@0
   583
			if ( hittest == HTCLIENT ) {
slouken@0
   584
				SetCursor(SDL_hcursor);
slouken@0
   585
				return(TRUE);
slouken@0
   586
			}
slouken@0
   587
		}
slouken@0
   588
		break;
slouken@0
   589
slouken@0
   590
		/* We are about to get palette focus! */
slouken@0
   591
		case WM_QUERYNEWPALETTE: {
slouken@0
   592
			WIN_RealizePalette(current_video);
slouken@0
   593
			return(TRUE);
slouken@0
   594
		}
slouken@0
   595
		break;
slouken@0
   596
slouken@0
   597
		/* Another application changed the palette */
slouken@0
   598
		case WM_PALETTECHANGED: {
slouken@0
   599
			WIN_PaletteChanged(current_video, (HWND)wParam);
slouken@0
   600
		}
slouken@0
   601
		break;
slouken@0
   602
slouken@0
   603
		/* We were occluded, refresh our display */
slouken@0
   604
		case WM_PAINT: {
slouken@0
   605
			HDC hdc;
slouken@0
   606
			PAINTSTRUCT ps;
slouken@0
   607
slouken@0
   608
			hdc = BeginPaint(SDL_Window, &ps);
slouken@0
   609
			if ( current_video->screen &&
slouken@0
   610
			     !(current_video->screen->flags & SDL_OPENGL) ) {
slouken@0
   611
				WIN_WinPAINT(current_video, hdc);
slouken@0
   612
			}
slouken@0
   613
			EndPaint(SDL_Window, &ps);
slouken@0
   614
		}
slouken@0
   615
		return(0);
slouken@0
   616
slouken@145
   617
		/* DJM: Send an expose event in this case */
slouken@0
   618
		case WM_ERASEBKGND: {
slouken@145
   619
			posted = SDL_PrivateExpose();
slouken@0
   620
		}
slouken@145
   621
		return(0);
slouken@0
   622
slouken@0
   623
		case WM_CLOSE: {
slouken@0
   624
			if ( (posted = SDL_PrivateQuit()) )
slouken@0
   625
				PostQuitMessage(0);
slouken@0
   626
		}
slouken@0
   627
		return(0);
slouken@0
   628
slouken@0
   629
		case WM_DESTROY: {
slouken@0
   630
			PostQuitMessage(0);
slouken@0
   631
		}
slouken@0
   632
		return(0);
slouken@0
   633
slouken@1253
   634
		case WM_INPUTLANGCHANGE: {
slouken@1253
   635
			codepage = GetCodePage();
slouken@1253
   636
		}
slouken@1253
   637
		return(TRUE);
slouken@1253
   638
slouken@0
   639
		default: {
slouken@0
   640
			/* Special handling by the video driver */
slouken@0
   641
			if (HandleMessage) {
slouken@0
   642
				return(HandleMessage(current_video,
slouken@0
   643
			                     hwnd, msg, wParam, lParam));
slouken@0
   644
			}
slouken@0
   645
		}
slouken@0
   646
		break;
slouken@0
   647
	}
slouken@0
   648
	return(DefWindowProc(hwnd, msg, wParam, lParam));
slouken@0
   649
}
slouken@0
   650
slouken@145
   651
/* Allow the application handle to be stored and retrieved later */
slouken@149
   652
static void *SDL_handle = NULL;
slouken@145
   653
slouken@149
   654
void SDL_SetModuleHandle(void *handle)
slouken@145
   655
{
slouken@145
   656
	SDL_handle = handle;
slouken@145
   657
}
slouken@149
   658
void *SDL_GetModuleHandle(void)
slouken@145
   659
{
slouken@145
   660
	void *handle;
slouken@145
   661
slouken@145
   662
	if ( SDL_handle ) {
slouken@145
   663
		handle = SDL_handle;
slouken@145
   664
	} else {
slouken@145
   665
		handle = GetModuleHandle(NULL);
slouken@145
   666
	}
slouken@145
   667
	return(handle);
slouken@145
   668
}
slouken@145
   669
slouken@0
   670
/* This allows the SDL_WINDOWID hack */
slouken@0
   671
const char *SDL_windowid = NULL;
slouken@0
   672
icculus@1145
   673
static int app_registered = 0;
icculus@1145
   674
slouken@0
   675
/* Register the class for this application -- exported for winmain.c */
slouken@149
   676
int SDL_RegisterApp(char *name, Uint32 style, void *hInst)
slouken@0
   677
{
slouken@0
   678
	WNDCLASS class;
slouken@0
   679
#ifdef WM_MOUSELEAVE
slouken@0
   680
	HMODULE handle;
slouken@0
   681
#endif
slouken@0
   682
slouken@0
   683
	/* Only do this once... */
icculus@1145
   684
	if ( app_registered ) {
slouken@0
   685
		return(0);
slouken@0
   686
	}
slouken@0
   687
slouken@0
   688
	/* This function needs to be passed the correct process handle
slouken@145
   689
	   by the application.
slouken@0
   690
	 */
slouken@0
   691
	if ( ! hInst ) {
slouken@145
   692
		hInst = SDL_GetModuleHandle();
slouken@0
   693
	}
slouken@0
   694
slouken@0
   695
	/* Register the application class */
slouken@0
   696
	class.hCursor		= NULL;
slouken@0
   697
#ifdef _WIN32_WCE
slouken@457
   698
	{
slouken@453
   699
		/* WinCE uses the UNICODE version */
slouken@453
   700
		int nLen = strlen(name)+1;
slouken@453
   701
		SDL_Appname = malloc(nLen*2);
slouken@453
   702
		MultiByteToWideChar(CP_ACP, 0, name, -1, SDL_Appname, nLen);
slouken@457
   703
	}
slouken@453
   704
#else
slouken@453
   705
	{
slouken@453
   706
		int nLen = strlen(name)+1;
slouken@453
   707
		SDL_Appname = malloc(nLen);
slouken@453
   708
		strcpy(SDL_Appname, name);
slouken@453
   709
	}
slouken@453
   710
#endif /* _WIN32_WCE */
slouken@453
   711
	class.hIcon		= LoadImage(hInst, SDL_Appname, IMAGE_ICON,
slouken@0
   712
	                                    0, 0, LR_DEFAULTCOLOR);
slouken@36
   713
	class.lpszMenuName	= NULL;
slouken@453
   714
	class.lpszClassName	= SDL_Appname;
slouken@0
   715
	class.hbrBackground	= NULL;
slouken@149
   716
	class.hInstance		= hInst;
slouken@0
   717
	class.style		= style;
slouken@0
   718
#ifdef HAVE_OPENGL
slouken@0
   719
	class.style		|= CS_OWNDC;
slouken@0
   720
#endif
slouken@0
   721
	class.lpfnWndProc	= WinMessage;
slouken@0
   722
	class.cbWndExtra	= 0;
slouken@0
   723
	class.cbClsExtra	= 0;
slouken@0
   724
	if ( ! RegisterClass(&class) ) {
slouken@0
   725
		SDL_SetError("Couldn't register application class");
slouken@0
   726
		return(-1);
slouken@0
   727
	}
slouken@0
   728
	SDL_Instance = hInst;
slouken@0
   729
slouken@0
   730
#ifdef WM_MOUSELEAVE
slouken@0
   731
	/* Get the version of TrackMouseEvent() we use */
slouken@0
   732
	_TrackMouseEvent = NULL;
slouken@0
   733
	handle = GetModuleHandle("USER32.DLL");
slouken@0
   734
	if ( handle ) {
slouken@0
   735
		_TrackMouseEvent = (BOOL (WINAPI *)(TRACKMOUSEEVENT *))GetProcAddress(handle, "TrackMouseEvent");
slouken@0
   736
	}
slouken@0
   737
	if ( _TrackMouseEvent == NULL ) {
slouken@0
   738
		_TrackMouseEvent = WIN_TrackMouseEvent;
slouken@0
   739
	}
slouken@0
   740
#endif /* WM_MOUSELEAVE */
slouken@0
   741
slouken@0
   742
	/* Check for SDL_WINDOWID hack */
slouken@0
   743
	SDL_windowid = getenv("SDL_WINDOWID");
slouken@0
   744
slouken@1253
   745
	/* Initialise variables for SDL_ToUnicode() */
slouken@1253
   746
	codepage = GetCodePage();
slouken@1253
   747
	SDL_ToUnicode = Is9xME() ? ToUnicode9xME : ToUnicode;
slouken@1253
   748
icculus@1145
   749
	app_registered = 1;
slouken@0
   750
	return(0);
slouken@0
   751
}
slouken@0
   752
icculus@1145
   753
/*
icculus@1145
   754
 * Unregisters the windowclass registered in SDL_RegisterApp above.
icculus@1145
   755
 *  Called from DIB_VideoQuit and DX5_VideoQuit when
icculus@1145
   756
 *  SDL_QuitSubSystem(INIT_VIDEO) is called.
icculus@1145
   757
 */
icculus@1150
   758
void SDL_UnregisterApp()
icculus@1145
   759
{
icculus@1145
   760
	WNDCLASS class;
icculus@1145
   761
icculus@1145
   762
	/* SDL_RegisterApp might not have been called before */
icculus@1145
   763
	if (app_registered) {
icculus@1145
   764
		/* Check for any registered windowclasses. */
icculus@1145
   765
		if (GetClassInfo(SDL_Instance, SDL_Appname, &class)) {
icculus@1145
   766
			UnregisterClass(SDL_Appname, SDL_Instance);
icculus@1145
   767
		}
icculus@1145
   768
	}
icculus@1145
   769
	app_registered = 0;
icculus@1145
   770
}
icculus@1145
   771
slouken@1253
   772
/* JFP: Implementation of ToUnicode() that works on 9x/ME/2K/XP */
slouken@1253
   773
slouken@1253
   774
static int Is9xME()
slouken@1253
   775
{
slouken@1253
   776
	OSVERSIONINFO   info;
slouken@1253
   777
slouken@1253
   778
	memset(&info, 0, sizeof(info));
slouken@1253
   779
	info.dwOSVersionInfoSize = sizeof(info);
slouken@1253
   780
	if (!GetVersionEx(&info)) {
slouken@1253
   781
		return 0;
slouken@1253
   782
	}
slouken@1253
   783
	return (info.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS);
slouken@1253
   784
}
slouken@1253
   785
slouken@1253
   786
static int GetCodePage()
slouken@1253
   787
{
slouken@1253
   788
	char	buff[8];
slouken@1253
   789
	int	lcid = MAKELCID(LOWORD(GetKeyboardLayout(0)), SORT_DEFAULT);
slouken@1253
   790
	int	cp = GetACP();
slouken@1253
   791
slouken@1253
   792
	if (GetLocaleInfo(lcid, LOCALE_IDEFAULTANSICODEPAGE, buff, sizeof(buff))) {
slouken@1253
   793
		cp = atoi(buff);
slouken@1253
   794
	}
slouken@1253
   795
	return cp;
slouken@1253
   796
}
slouken@1253
   797
slouken@1253
   798
static int WINAPI ToUnicode9xME(UINT vkey, UINT scancode, PBYTE keystate, LPWSTR wchars, int wsize, UINT flags)
slouken@1253
   799
{
slouken@1253
   800
	BYTE	chars[2];
slouken@1253
   801
slouken@1253
   802
	if (ToAsciiEx(vkey, scancode, keystate, (WORD*)chars, 0, GetKeyboardLayout(0)) == 1) {
slouken@1253
   803
		return MultiByteToWideChar(codepage, 0, chars, 1, wchars, wsize);
slouken@1253
   804
	}
slouken@1253
   805
	return 0;
slouken@1253
   806
}
slouken@1253
   807