src/video/windib/SDL_dibevents.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 22 Sep 2009 02:42:11 +0000
branchSDL-1.2
changeset 4232 29605c881b29
parent 4220 cf958451fd8d
child 4238 cbe5222d051a
permissions -rw-r--r--
Fixed compile error on Win32
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
slouken@1358
    27
#include "SDL_main.h"
slouken@0
    28
#include "SDL_events.h"
slouken@0
    29
#include "SDL_syswm.h"
slouken@1361
    30
#include "../../events/SDL_sysevents.h"
slouken@1361
    31
#include "../../events/SDL_events_c.h"
slouken@1361
    32
#include "../wincommon/SDL_lowvideo.h"
slouken@4162
    33
#include "SDL_gapidibvideo.h"
slouken@4162
    34
#include "SDL_vkeys.h"
slouken@4162
    35
slouken@4162
    36
#ifdef SDL_VIDEO_DRIVER_GAPI
slouken@4162
    37
#include "../gapi/SDL_gapivideo.h"
slouken@4162
    38
#endif
slouken@4162
    39
slouken@4162
    40
#ifdef SDL_VIDEO_DRIVER_WINDIB
slouken@0
    41
#include "SDL_dibvideo.h"
slouken@4162
    42
#endif
slouken@0
    43
slouken@0
    44
#ifndef WM_APP
slouken@0
    45
#define WM_APP	0x8000
slouken@0
    46
#endif
slouken@0
    47
slouken@0
    48
#ifdef _WIN32_WCE
slouken@0
    49
#define NO_GETKEYBOARDSTATE
slouken@0
    50
#endif
slouken@0
    51
slouken@0
    52
/* The translation table from a Microsoft VK keysym to a SDL keysym */
slouken@0
    53
static SDLKey VK_keymap[SDLK_LAST];
slouken@1472
    54
static SDL_keysym *TranslateKey(WPARAM vkey, UINT scancode, SDL_keysym *keysym, int pressed);
slouken@4162
    55
static SDLKey Arrows_keymap[4];
slouken@0
    56
slouken@0
    57
/* Masks for processing the windows KEYDOWN and KEYUP messages */
slouken@0
    58
#define REPEATED_KEYMASK	(1<<30)
slouken@0
    59
#define EXTENDED_KEYMASK	(1<<24)
slouken@0
    60
slouken@145
    61
/* DJM: If the user setup the window for us, we want to save his window proc,
slouken@145
    62
   and give him a chance to handle some messages. */
slouken@1303
    63
#ifdef STRICT
slouken@1303
    64
#define WNDPROCTYPE	WNDPROC
slouken@1303
    65
#else
slouken@1303
    66
#define WNDPROCTYPE	FARPROC
slouken@1303
    67
#endif
slouken@1303
    68
static WNDPROCTYPE userWindowProc = NULL;
slouken@145
    69
icculus@1251
    70
slouken@4162
    71
#ifdef SDL_VIDEO_DRIVER_GAPI
icculus@1251
    72
slouken@4162
    73
WPARAM rotateKey(WPARAM key,int direction) 
icculus@1251
    74
{
slouken@4162
    75
	if(direction ==0 ) return key;
slouken@4162
    76
	
icculus@1251
    77
	switch (key) {
icculus@1251
    78
		case 0x26: /* up */
slouken@4162
    79
			return Arrows_keymap[(2 + direction) % 4];
icculus@1251
    80
		case 0x27: /* right */
slouken@4162
    81
			return Arrows_keymap[(1 + direction) % 4];
icculus@1251
    82
		case 0x28: /* down */
slouken@4162
    83
			return Arrows_keymap[direction % 4];
icculus@1251
    84
		case 0x25: /* left */
slouken@4162
    85
			return Arrows_keymap[(3 + direction) % 4];
icculus@1251
    86
	}
icculus@1251
    87
icculus@1251
    88
	return key;
icculus@1251
    89
}
icculus@1251
    90
slouken@4220
    91
static void GapiTransform(GapiInfo *gapiInfo, LONG *x, LONG *y) {
slouken@4179
    92
    Sint16 rotatedX;
slouken@4179
    93
    Sint16 rotatedY;
slouken@4179
    94
slouken@4220
    95
    if(gapiInfo->userOrientation == SDL_ORIENTATION_UP &&
slouken@4220
    96
       gapiInfo->gapiOrientation == SDL_ORIENTATION_RIGHT)
slouken@4220
    97
    {
slouken@4220
    98
        rotatedX = *x;
slouken@4220
    99
        rotatedY = *y;
slouken@4220
   100
        *x = rotatedX;
slouken@4220
   101
        *y = rotatedY;
slouken@4220
   102
     }
slouken@4220
   103
    else
slouken@4220
   104
    if(gapiInfo->userOrientation == SDL_ORIENTATION_RIGHT &&
slouken@4220
   105
       gapiInfo->gapiOrientation == SDL_ORIENTATION_RIGHT)
slouken@4220
   106
    {
slouken@4220
   107
        rotatedX = (2 * ((SDL_VideoSurface->offset%SDL_VideoSurface->pitch)/
slouken@4220
   108
                    SDL_VideoSurface->format->BytesPerPixel))
slouken@4220
   109
                    + SDL_VideoSurface->w - *y;
slouken@4220
   110
        rotatedY = *x;
slouken@4220
   111
        *x = rotatedX;
slouken@4220
   112
        *y = rotatedY;
slouken@4179
   113
    }
slouken@4220
   114
    else
slouken@4220
   115
    {
slouken@4220
   116
        rotatedX = SDL_VideoSurface->w - *y;
slouken@4220
   117
        rotatedY = *x;
slouken@4220
   118
        *y = rotatedY;
slouken@4220
   119
        *x = rotatedX;
slouken@4179
   120
    }
slouken@4179
   121
}
icculus@1251
   122
#endif 
icculus@1251
   123
icculus@1251
   124
slouken@0
   125
/* The main Win32 event handler */
slouken@1456
   126
LRESULT DIB_HandleMessage(_THIS, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
slouken@0
   127
{
slouken@0
   128
	extern int posted;
slouken@0
   129
slouken@0
   130
	switch (msg) {
slouken@0
   131
		case WM_SYSKEYDOWN:
slouken@0
   132
		case WM_KEYDOWN: {
slouken@0
   133
			SDL_keysym keysym;
slouken@0
   134
slouken@4162
   135
#ifdef SDL_VIDEO_DRIVER_GAPI
slouken@4162
   136
			if(this->hidden->gapiInfo)
slouken@4162
   137
			{
slouken@4162
   138
				// Drop GAPI artefacts
slouken@4162
   139
				if (wParam == 0x84 || wParam == 0x5B)
slouken@4162
   140
					return 0;
icculus@1251
   141
slouken@4162
   142
				wParam = rotateKey(wParam, this->hidden->gapiInfo->coordinateTransform);
slouken@4162
   143
			}
icculus@1251
   144
#endif 
slouken@0
   145
			/* Ignore repeated keys */
slouken@0
   146
			if ( lParam&REPEATED_KEYMASK ) {
slouken@0
   147
				return(0);
slouken@0
   148
			}
slouken@0
   149
			switch (wParam) {
slouken@0
   150
				case VK_CONTROL:
slouken@0
   151
					if ( lParam&EXTENDED_KEYMASK )
slouken@0
   152
						wParam = VK_RCONTROL;
slouken@0
   153
					else
slouken@0
   154
						wParam = VK_LCONTROL;
slouken@0
   155
					break;
slouken@0
   156
				case VK_SHIFT:
slouken@0
   157
					/* EXTENDED trick doesn't work here */
slouken@1282
   158
					{
slouken@1282
   159
					Uint8 *state = SDL_GetKeyState(NULL);
slouken@1282
   160
					if (state[SDLK_LSHIFT] == SDL_RELEASED && (GetKeyState(VK_LSHIFT) & 0x8000)) {
slouken@558
   161
						wParam = VK_LSHIFT;
slouken@1282
   162
					} else if (state[SDLK_RSHIFT] == SDL_RELEASED && (GetKeyState(VK_RSHIFT) & 0x8000)) {
slouken@558
   163
						wParam = VK_RSHIFT;
slouken@558
   164
					} else {
icculus@1852
   165
						/* Win9x */
icculus@1852
   166
						int sc = HIWORD(lParam) & 0xFF;
icculus@1852
   167
icculus@1852
   168
						if (sc == 0x2A)
icculus@1852
   169
							wParam = VK_LSHIFT;
icculus@1852
   170
						else
icculus@1852
   171
						if (sc == 0x36)
icculus@1852
   172
							wParam = VK_RSHIFT;
icculus@1852
   173
						else
icculus@1852
   174
							wParam = VK_LSHIFT;
slouken@1282
   175
					}
slouken@558
   176
					}
slouken@0
   177
					break;
slouken@0
   178
				case VK_MENU:
slouken@0
   179
					if ( lParam&EXTENDED_KEYMASK )
slouken@0
   180
						wParam = VK_RMENU;
slouken@0
   181
					else
slouken@0
   182
						wParam = VK_LMENU;
slouken@0
   183
					break;
slouken@0
   184
			}
slouken@112
   185
#ifdef NO_GETKEYBOARDSTATE
slouken@112
   186
			/* this is the workaround for the missing ToAscii() and ToUnicode() in CE (not necessary at KEYUP!) */
slouken@112
   187
			if ( SDL_TranslateUNICODE ) {
slouken@140
   188
				MSG m;
slouken@112
   189
slouken@140
   190
				m.hwnd = hwnd;
slouken@140
   191
				m.message = msg;
slouken@140
   192
				m.wParam = wParam;
slouken@140
   193
				m.lParam = lParam;
slouken@140
   194
				m.time = 0;
slouken@140
   195
				if ( TranslateMessage(&m) && PeekMessage(&m, hwnd, 0, WM_USER, PM_NOREMOVE) && (m.message == WM_CHAR) ) {
slouken@112
   196
					GetMessage(&m, hwnd, 0, WM_USER);
slouken@112
   197
			    		wParam = m.wParam;
slouken@112
   198
				}
slouken@112
   199
			}
slouken@112
   200
#endif /* NO_GETKEYBOARDSTATE */
slouken@0
   201
			posted = SDL_PrivateKeyboard(SDL_PRESSED,
slouken@0
   202
				TranslateKey(wParam,HIWORD(lParam),&keysym,1));
slouken@0
   203
		}
slouken@0
   204
		return(0);
slouken@0
   205
slouken@0
   206
		case WM_SYSKEYUP:
slouken@0
   207
		case WM_KEYUP: {
slouken@0
   208
			SDL_keysym keysym;
slouken@0
   209
slouken@4162
   210
#ifdef SDL_VIDEO_DRIVER_GAPI
slouken@4162
   211
			if(this->hidden->gapiInfo)
slouken@4162
   212
			{
slouken@4162
   213
				// Drop GAPI artifacts
slouken@4162
   214
				if (wParam == 0x84 || wParam == 0x5B)
slouken@4162
   215
					return 0;
slouken@4162
   216
	
slouken@4162
   217
				wParam = rotateKey(wParam, this->hidden->gapiInfo->coordinateTransform);
slouken@4162
   218
			}
icculus@1251
   219
#endif
icculus@1251
   220
slouken@0
   221
			switch (wParam) {
slouken@0
   222
				case VK_CONTROL:
slouken@0
   223
					if ( lParam&EXTENDED_KEYMASK )
slouken@0
   224
						wParam = VK_RCONTROL;
slouken@0
   225
					else
slouken@0
   226
						wParam = VK_LCONTROL;
slouken@0
   227
					break;
slouken@0
   228
				case VK_SHIFT:
slouken@0
   229
					/* EXTENDED trick doesn't work here */
slouken@1282
   230
					{
slouken@1282
   231
					Uint8 *state = SDL_GetKeyState(NULL);
slouken@1282
   232
					if (state[SDLK_LSHIFT] == SDL_PRESSED && !(GetKeyState(VK_LSHIFT) & 0x8000)) {
slouken@572
   233
						wParam = VK_LSHIFT;
slouken@1282
   234
					} else if (state[SDLK_RSHIFT] == SDL_PRESSED && !(GetKeyState(VK_RSHIFT) & 0x8000)) {
slouken@572
   235
						wParam = VK_RSHIFT;
slouken@572
   236
					} else {
icculus@1852
   237
						/* Win9x */
icculus@1852
   238
						int sc = HIWORD(lParam) & 0xFF;
icculus@1852
   239
icculus@1852
   240
						if (sc == 0x2A)
icculus@1852
   241
							wParam = VK_LSHIFT;
icculus@1852
   242
						else
icculus@1852
   243
						if (sc == 0x36)
icculus@1852
   244
							wParam = VK_RSHIFT;
icculus@1852
   245
						else
icculus@1852
   246
							wParam = VK_LSHIFT;
slouken@1282
   247
					}
slouken@572
   248
					}
slouken@0
   249
					break;
slouken@0
   250
				case VK_MENU:
slouken@0
   251
					if ( lParam&EXTENDED_KEYMASK )
slouken@0
   252
						wParam = VK_RMENU;
slouken@0
   253
					else
slouken@0
   254
						wParam = VK_LMENU;
slouken@0
   255
					break;
slouken@0
   256
			}
slouken@1526
   257
			/* Windows only reports keyup for print screen */
slouken@1526
   258
			if ( wParam == VK_SNAPSHOT && SDL_GetKeyState(NULL)[SDLK_PRINT] == SDL_RELEASED ) {
slouken@1526
   259
				posted = SDL_PrivateKeyboard(SDL_PRESSED,
slouken@1526
   260
					TranslateKey(wParam,HIWORD(lParam),&keysym,1));
slouken@1526
   261
			}
slouken@0
   262
			posted = SDL_PrivateKeyboard(SDL_RELEASED,
slouken@0
   263
				TranslateKey(wParam,HIWORD(lParam),&keysym,0));
slouken@0
   264
		}
slouken@0
   265
		return(0);
icculus@726
   266
#if defined(SC_SCREENSAVE) && defined(SC_MONITORPOWER)
icculus@726
   267
		case WM_SYSCOMMAND: {
icculus@3981
   268
			const DWORD val = (DWORD) (wParam & 0xFFF0);
icculus@3981
   269
			if ((val == SC_SCREENSAVE) || (val == SC_MONITORPOWER)) {
slouken@4162
   270
				if (this->hidden->dibInfo && !allow_screensaver) {
icculus@3981
   271
					/* Note that this doesn't stop anything on Vista
icculus@3981
   272
					   if the screensaver has a password. */
icculus@726
   273
					return(0);
icculus@3981
   274
				}
icculus@3981
   275
			}
icculus@726
   276
		}
icculus@726
   277
		/* Fall through to default processing */
icculus@726
   278
#endif /* SC_SCREENSAVE && SC_MONITORPOWER */
icculus@726
   279
slouken@0
   280
		default: {
slouken@0
   281
			/* Only post the event if we're watching for it */
slouken@0
   282
			if ( SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE ) {
slouken@0
   283
			        SDL_SysWMmsg wmmsg;
slouken@0
   284
slouken@0
   285
				SDL_VERSION(&wmmsg.version);
slouken@0
   286
				wmmsg.hwnd = hwnd;
slouken@0
   287
				wmmsg.msg = msg;
slouken@0
   288
				wmmsg.wParam = wParam;
slouken@0
   289
				wmmsg.lParam = lParam;
slouken@0
   290
				posted = SDL_PrivateSysWMEvent(&wmmsg);
slouken@145
   291
slouken@145
   292
			/* DJM: If the user isn't watching for private
slouken@145
   293
				messages in her SDL event loop, then pass it
slouken@145
   294
				along to any win32 specific window proc.
slouken@145
   295
			 */
slouken@145
   296
			} else if (userWindowProc) {
slouken@721
   297
				return CallWindowProc(userWindowProc, hwnd, msg, wParam, lParam);
slouken@0
   298
			}
slouken@0
   299
		}
slouken@0
   300
		break;
slouken@0
   301
	}
slouken@0
   302
	return(DefWindowProc(hwnd, msg, wParam, lParam));
slouken@0
   303
}
slouken@0
   304
slouken@4220
   305
#ifdef _WIN32_WCE
slouken@4220
   306
static BOOL GetLastStylusPos(POINT* ptLast)
slouken@4220
   307
{
slouken@4220
   308
    BOOL bResult = FALSE;
slouken@4220
   309
    UINT nRet;
slouken@4220
   310
    GetMouseMovePoints(ptLast, 1, &nRet);
slouken@4220
   311
    if ( nRet == 1 ) {
slouken@4220
   312
        ptLast->x /= 4;
slouken@4220
   313
        ptLast->y /= 4;
slouken@4220
   314
        bResult = TRUE;
slouken@4220
   315
    }
slouken@4220
   316
    return bResult;
slouken@4220
   317
}
slouken@4220
   318
#endif
slouken@4220
   319
slouken@4179
   320
static void DIB_GenerateMouseMotionEvent(_THIS)
slouken@4167
   321
{
slouken@4167
   322
	extern int mouse_relative;
slouken@4167
   323
	extern int posted;
slouken@4167
   324
slouken@4167
   325
	POINT mouse;
slouken@4232
   326
#ifdef _WIN32_WCE
slouken@4232
   327
	if ( !GetCursorPos(&mouse) && !GetLastStylusPos(&mouse) ) return;
slouken@4232
   328
#else
slouken@4232
   329
	if ( !GetCursorPos(&mouse) ) return;
slouken@4232
   330
#endif
slouken@4167
   331
slouken@4167
   332
	if ( mouse_relative ) {
slouken@4167
   333
		POINT center;
slouken@4167
   334
		center.x = (SDL_VideoSurface->w/2);
slouken@4167
   335
		center.y = (SDL_VideoSurface->h/2);
slouken@4167
   336
		ClientToScreen(SDL_Window, &center);
slouken@4167
   337
slouken@4167
   338
		mouse.x -= (Sint16)center.x;
slouken@4167
   339
		mouse.y -= (Sint16)center.y;
slouken@4167
   340
		if ( mouse.x || mouse.y ) {
slouken@4167
   341
			SetCursorPos(center.x, center.y);
slouken@4167
   342
			posted = SDL_PrivateMouseMotion(0, 1, mouse.x, mouse.y);
slouken@4167
   343
		}
slouken@4167
   344
	} else if ( SDL_GetAppState() & SDL_APPMOUSEFOCUS ) {
slouken@4167
   345
		ScreenToClient(SDL_Window, &mouse);
slouken@4179
   346
#ifdef SDL_VIDEO_DRIVER_GAPI
slouken@4179
   347
       if (SDL_VideoSurface && this->hidden->gapiInfo)
slouken@4220
   348
			GapiTransform(this->hidden->gapiInfo, &mouse.x, &mouse.y);
slouken@4167
   349
#endif
slouken@4167
   350
		posted = SDL_PrivateMouseMotion(0, 0, mouse.x, mouse.y);
slouken@4167
   351
	}
slouken@4167
   352
}
slouken@4167
   353
slouken@0
   354
void DIB_PumpEvents(_THIS)
slouken@0
   355
{
slouken@0
   356
	MSG msg;
slouken@0
   357
slouken@523
   358
	while ( PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) ) {
slouken@523
   359
		if ( GetMessage(&msg, NULL, 0, 0) > 0 ) {
slouken@0
   360
			DispatchMessage(&msg);
slouken@0
   361
		}
slouken@0
   362
	}
slouken@4167
   363
slouken@4167
   364
	if ( SDL_GetAppState() & SDL_APPINPUTFOCUS ) {
slouken@4179
   365
		DIB_GenerateMouseMotionEvent( this );
slouken@4167
   366
	}
slouken@0
   367
}
slouken@0
   368
icculus@1852
   369
static HKL hLayoutUS = NULL;
icculus@1852
   370
slouken@0
   371
void DIB_InitOSKeymap(_THIS)
slouken@0
   372
{
icculus@1852
   373
	int	i;
slouken@3868
   374
#ifndef _WIN32_WCE
slouken@3868
   375
	char	current_layout[KL_NAMELENGTH];
icculus@1852
   376
icculus@1852
   377
	GetKeyboardLayoutName(current_layout);
icculus@1852
   378
	//printf("Initial Keyboard Layout Name: '%s'\n", current_layout);
icculus@1852
   379
icculus@1852
   380
	hLayoutUS = LoadKeyboardLayout("00000409", KLF_NOTELLSHELL);
slouken@3868
   381
icculus@1852
   382
	if (!hLayoutUS) {
icculus@1852
   383
		//printf("Failed to load US keyboard layout. Using current.\n");
icculus@1852
   384
		hLayoutUS = GetKeyboardLayout(0);
icculus@1852
   385
	}
icculus@1852
   386
	LoadKeyboardLayout(current_layout, KLF_ACTIVATE);
slouken@3868
   387
#else
slouken@3868
   388
#if _WIN32_WCE >=420
slouken@3868
   389
	TCHAR	current_layout[KL_NAMELENGTH];
slouken@0
   390
slouken@3868
   391
	GetKeyboardLayoutName(current_layout);
slouken@3868
   392
	//printf("Initial Keyboard Layout Name: '%s'\n", current_layout);
slouken@3868
   393
slouken@3868
   394
	hLayoutUS = LoadKeyboardLayout(L"00000409", 0);
slouken@3868
   395
slouken@3868
   396
	if (!hLayoutUS) {
slouken@3868
   397
		//printf("Failed to load US keyboard layout. Using current.\n");
slouken@3868
   398
		hLayoutUS = GetKeyboardLayout(0);
slouken@3868
   399
	}
slouken@3868
   400
	LoadKeyboardLayout(current_layout, 0);
slouken@3868
   401
#endif // _WIN32_WCE >=420
slouken@3868
   402
#endif
slouken@0
   403
	/* Map the VK keysyms */
slouken@1379
   404
	for ( i=0; i<SDL_arraysize(VK_keymap); ++i )
slouken@0
   405
		VK_keymap[i] = SDLK_UNKNOWN;
slouken@0
   406
slouken@0
   407
	VK_keymap[VK_BACK] = SDLK_BACKSPACE;
slouken@0
   408
	VK_keymap[VK_TAB] = SDLK_TAB;
slouken@0
   409
	VK_keymap[VK_CLEAR] = SDLK_CLEAR;
slouken@0
   410
	VK_keymap[VK_RETURN] = SDLK_RETURN;
slouken@0
   411
	VK_keymap[VK_PAUSE] = SDLK_PAUSE;
slouken@0
   412
	VK_keymap[VK_ESCAPE] = SDLK_ESCAPE;
slouken@0
   413
	VK_keymap[VK_SPACE] = SDLK_SPACE;
slouken@0
   414
	VK_keymap[VK_APOSTROPHE] = SDLK_QUOTE;
slouken@0
   415
	VK_keymap[VK_COMMA] = SDLK_COMMA;
slouken@0
   416
	VK_keymap[VK_MINUS] = SDLK_MINUS;
slouken@0
   417
	VK_keymap[VK_PERIOD] = SDLK_PERIOD;
slouken@0
   418
	VK_keymap[VK_SLASH] = SDLK_SLASH;
slouken@0
   419
	VK_keymap[VK_0] = SDLK_0;
slouken@0
   420
	VK_keymap[VK_1] = SDLK_1;
slouken@0
   421
	VK_keymap[VK_2] = SDLK_2;
slouken@0
   422
	VK_keymap[VK_3] = SDLK_3;
slouken@0
   423
	VK_keymap[VK_4] = SDLK_4;
slouken@0
   424
	VK_keymap[VK_5] = SDLK_5;
slouken@0
   425
	VK_keymap[VK_6] = SDLK_6;
slouken@0
   426
	VK_keymap[VK_7] = SDLK_7;
slouken@0
   427
	VK_keymap[VK_8] = SDLK_8;
slouken@0
   428
	VK_keymap[VK_9] = SDLK_9;
slouken@0
   429
	VK_keymap[VK_SEMICOLON] = SDLK_SEMICOLON;
slouken@0
   430
	VK_keymap[VK_EQUALS] = SDLK_EQUALS;
slouken@0
   431
	VK_keymap[VK_LBRACKET] = SDLK_LEFTBRACKET;
slouken@0
   432
	VK_keymap[VK_BACKSLASH] = SDLK_BACKSLASH;
slouken@1253
   433
	VK_keymap[VK_OEM_102] = SDLK_LESS;
slouken@0
   434
	VK_keymap[VK_RBRACKET] = SDLK_RIGHTBRACKET;
slouken@0
   435
	VK_keymap[VK_GRAVE] = SDLK_BACKQUOTE;
slouken@327
   436
	VK_keymap[VK_BACKTICK] = SDLK_BACKQUOTE;
slouken@0
   437
	VK_keymap[VK_A] = SDLK_a;
slouken@0
   438
	VK_keymap[VK_B] = SDLK_b;
slouken@0
   439
	VK_keymap[VK_C] = SDLK_c;
slouken@0
   440
	VK_keymap[VK_D] = SDLK_d;
slouken@0
   441
	VK_keymap[VK_E] = SDLK_e;
slouken@0
   442
	VK_keymap[VK_F] = SDLK_f;
slouken@0
   443
	VK_keymap[VK_G] = SDLK_g;
slouken@0
   444
	VK_keymap[VK_H] = SDLK_h;
slouken@0
   445
	VK_keymap[VK_I] = SDLK_i;
slouken@0
   446
	VK_keymap[VK_J] = SDLK_j;
slouken@0
   447
	VK_keymap[VK_K] = SDLK_k;
slouken@0
   448
	VK_keymap[VK_L] = SDLK_l;
slouken@0
   449
	VK_keymap[VK_M] = SDLK_m;
slouken@0
   450
	VK_keymap[VK_N] = SDLK_n;
slouken@0
   451
	VK_keymap[VK_O] = SDLK_o;
slouken@0
   452
	VK_keymap[VK_P] = SDLK_p;
slouken@0
   453
	VK_keymap[VK_Q] = SDLK_q;
slouken@0
   454
	VK_keymap[VK_R] = SDLK_r;
slouken@0
   455
	VK_keymap[VK_S] = SDLK_s;
slouken@0
   456
	VK_keymap[VK_T] = SDLK_t;
slouken@0
   457
	VK_keymap[VK_U] = SDLK_u;
slouken@0
   458
	VK_keymap[VK_V] = SDLK_v;
slouken@0
   459
	VK_keymap[VK_W] = SDLK_w;
slouken@0
   460
	VK_keymap[VK_X] = SDLK_x;
slouken@0
   461
	VK_keymap[VK_Y] = SDLK_y;
slouken@0
   462
	VK_keymap[VK_Z] = SDLK_z;
slouken@0
   463
	VK_keymap[VK_DELETE] = SDLK_DELETE;
slouken@0
   464
slouken@0
   465
	VK_keymap[VK_NUMPAD0] = SDLK_KP0;
slouken@0
   466
	VK_keymap[VK_NUMPAD1] = SDLK_KP1;
slouken@0
   467
	VK_keymap[VK_NUMPAD2] = SDLK_KP2;
slouken@0
   468
	VK_keymap[VK_NUMPAD3] = SDLK_KP3;
slouken@0
   469
	VK_keymap[VK_NUMPAD4] = SDLK_KP4;
slouken@0
   470
	VK_keymap[VK_NUMPAD5] = SDLK_KP5;
slouken@0
   471
	VK_keymap[VK_NUMPAD6] = SDLK_KP6;
slouken@0
   472
	VK_keymap[VK_NUMPAD7] = SDLK_KP7;
slouken@0
   473
	VK_keymap[VK_NUMPAD8] = SDLK_KP8;
slouken@0
   474
	VK_keymap[VK_NUMPAD9] = SDLK_KP9;
slouken@0
   475
	VK_keymap[VK_DECIMAL] = SDLK_KP_PERIOD;
slouken@0
   476
	VK_keymap[VK_DIVIDE] = SDLK_KP_DIVIDE;
slouken@0
   477
	VK_keymap[VK_MULTIPLY] = SDLK_KP_MULTIPLY;
slouken@0
   478
	VK_keymap[VK_SUBTRACT] = SDLK_KP_MINUS;
slouken@0
   479
	VK_keymap[VK_ADD] = SDLK_KP_PLUS;
slouken@0
   480
slouken@0
   481
	VK_keymap[VK_UP] = SDLK_UP;
slouken@0
   482
	VK_keymap[VK_DOWN] = SDLK_DOWN;
slouken@0
   483
	VK_keymap[VK_RIGHT] = SDLK_RIGHT;
slouken@0
   484
	VK_keymap[VK_LEFT] = SDLK_LEFT;
slouken@0
   485
	VK_keymap[VK_INSERT] = SDLK_INSERT;
slouken@0
   486
	VK_keymap[VK_HOME] = SDLK_HOME;
slouken@0
   487
	VK_keymap[VK_END] = SDLK_END;
slouken@0
   488
	VK_keymap[VK_PRIOR] = SDLK_PAGEUP;
slouken@0
   489
	VK_keymap[VK_NEXT] = SDLK_PAGEDOWN;
slouken@0
   490
slouken@0
   491
	VK_keymap[VK_F1] = SDLK_F1;
slouken@0
   492
	VK_keymap[VK_F2] = SDLK_F2;
slouken@0
   493
	VK_keymap[VK_F3] = SDLK_F3;
slouken@0
   494
	VK_keymap[VK_F4] = SDLK_F4;
slouken@0
   495
	VK_keymap[VK_F5] = SDLK_F5;
slouken@0
   496
	VK_keymap[VK_F6] = SDLK_F6;
slouken@0
   497
	VK_keymap[VK_F7] = SDLK_F7;
slouken@0
   498
	VK_keymap[VK_F8] = SDLK_F8;
slouken@0
   499
	VK_keymap[VK_F9] = SDLK_F9;
slouken@0
   500
	VK_keymap[VK_F10] = SDLK_F10;
slouken@0
   501
	VK_keymap[VK_F11] = SDLK_F11;
slouken@0
   502
	VK_keymap[VK_F12] = SDLK_F12;
slouken@0
   503
	VK_keymap[VK_F13] = SDLK_F13;
slouken@0
   504
	VK_keymap[VK_F14] = SDLK_F14;
slouken@0
   505
	VK_keymap[VK_F15] = SDLK_F15;
slouken@0
   506
slouken@0
   507
	VK_keymap[VK_NUMLOCK] = SDLK_NUMLOCK;
slouken@0
   508
	VK_keymap[VK_CAPITAL] = SDLK_CAPSLOCK;
slouken@0
   509
	VK_keymap[VK_SCROLL] = SDLK_SCROLLOCK;
slouken@0
   510
	VK_keymap[VK_RSHIFT] = SDLK_RSHIFT;
slouken@0
   511
	VK_keymap[VK_LSHIFT] = SDLK_LSHIFT;
slouken@0
   512
	VK_keymap[VK_RCONTROL] = SDLK_RCTRL;
slouken@0
   513
	VK_keymap[VK_LCONTROL] = SDLK_LCTRL;
slouken@0
   514
	VK_keymap[VK_RMENU] = SDLK_RALT;
slouken@0
   515
	VK_keymap[VK_LMENU] = SDLK_LALT;
slouken@0
   516
	VK_keymap[VK_RWIN] = SDLK_RSUPER;
slouken@0
   517
	VK_keymap[VK_LWIN] = SDLK_LSUPER;
slouken@0
   518
slouken@0
   519
	VK_keymap[VK_HELP] = SDLK_HELP;
slouken@0
   520
#ifdef VK_PRINT
slouken@0
   521
	VK_keymap[VK_PRINT] = SDLK_PRINT;
slouken@0
   522
#endif
slouken@0
   523
	VK_keymap[VK_SNAPSHOT] = SDLK_PRINT;
slouken@0
   524
	VK_keymap[VK_CANCEL] = SDLK_BREAK;
slouken@0
   525
	VK_keymap[VK_APPS] = SDLK_MENU;
slouken@4162
   526
slouken@4162
   527
	Arrows_keymap[3] = 0x25;
slouken@4162
   528
	Arrows_keymap[2] = 0x26;
slouken@4162
   529
	Arrows_keymap[1] = 0x27;
slouken@4162
   530
	Arrows_keymap[0] = 0x28;
slouken@0
   531
}
slouken@0
   532
icculus@1852
   533
#define EXTKEYPAD(keypad) ((scancode & 0x100)?(mvke):(keypad))
icculus@1852
   534
icculus@1852
   535
static int SDL_MapVirtualKey(int scancode, int vkey)
icculus@1852
   536
{
slouken@3868
   537
#ifndef _WIN32_WCE
icculus@1852
   538
	int	mvke  = MapVirtualKeyEx(scancode & 0xFF, 1, hLayoutUS);
slouken@3868
   539
#else
slouken@3868
   540
	int	mvke  = MapVirtualKey(scancode & 0xFF, 1);
slouken@3868
   541
#endif
icculus@1852
   542
icculus@1852
   543
	switch(vkey) {
icculus@1852
   544
		/* These are always correct */
icculus@1852
   545
		case VK_DIVIDE:
icculus@1852
   546
		case VK_MULTIPLY:
icculus@1852
   547
		case VK_SUBTRACT:
icculus@1852
   548
		case VK_ADD:
icculus@1852
   549
		case VK_LWIN:
icculus@1852
   550
		case VK_RWIN:
icculus@1852
   551
		case VK_APPS:
icculus@1852
   552
		/* These are already handled */
icculus@1852
   553
		case VK_LCONTROL:
icculus@1852
   554
		case VK_RCONTROL:
icculus@1852
   555
		case VK_LSHIFT:
icculus@1852
   556
		case VK_RSHIFT:
icculus@1852
   557
		case VK_LMENU:
icculus@1852
   558
		case VK_RMENU:
icculus@1852
   559
		case VK_SNAPSHOT:
icculus@1852
   560
		case VK_PAUSE:
icculus@1852
   561
			return vkey;
icculus@1852
   562
	}	
icculus@1852
   563
	switch(mvke) {
icculus@1852
   564
		/* Distinguish between keypad and extended keys */
icculus@1852
   565
		case VK_INSERT: return EXTKEYPAD(VK_NUMPAD0);
icculus@1852
   566
		case VK_DELETE: return EXTKEYPAD(VK_DECIMAL);
icculus@1852
   567
		case VK_END:    return EXTKEYPAD(VK_NUMPAD1);
icculus@1852
   568
		case VK_DOWN:   return EXTKEYPAD(VK_NUMPAD2);
icculus@1852
   569
		case VK_NEXT:   return EXTKEYPAD(VK_NUMPAD3);
icculus@1852
   570
		case VK_LEFT:   return EXTKEYPAD(VK_NUMPAD4);
icculus@1852
   571
		case VK_CLEAR:  return EXTKEYPAD(VK_NUMPAD5);
icculus@1852
   572
		case VK_RIGHT:  return EXTKEYPAD(VK_NUMPAD6);
icculus@1852
   573
		case VK_HOME:   return EXTKEYPAD(VK_NUMPAD7);
icculus@1852
   574
		case VK_UP:     return EXTKEYPAD(VK_NUMPAD8);
icculus@1852
   575
		case VK_PRIOR:  return EXTKEYPAD(VK_NUMPAD9);
icculus@1852
   576
	}
icculus@1852
   577
	return mvke?mvke:vkey;
icculus@1852
   578
}
icculus@1852
   579
slouken@1472
   580
static SDL_keysym *TranslateKey(WPARAM vkey, UINT scancode, SDL_keysym *keysym, int pressed)
slouken@0
   581
{
slouken@0
   582
	/* Set the keysym information */
slouken@0
   583
	keysym->scancode = (unsigned char) scancode;
slouken@0
   584
	keysym->mod = KMOD_NONE;
slouken@0
   585
	keysym->unicode = 0;
slouken@4162
   586
	
slouken@4162
   587
	if ((vkey == VK_RETURN) && (scancode & 0x100)) {
slouken@4162
   588
		/* No VK_ code for the keypad enter key */
slouken@4162
   589
		keysym->sym = SDLK_KP_ENTER;
slouken@4162
   590
	}
slouken@4162
   591
	else {
slouken@4162
   592
		keysym->sym = VK_keymap[SDL_MapVirtualKey(scancode, vkey)];
slouken@4162
   593
	}
slouken@4162
   594
slouken@1253
   595
	if ( pressed && SDL_TranslateUNICODE ) {
slouken@0
   596
#ifdef NO_GETKEYBOARDSTATE
slouken@0
   597
		/* Uh oh, better hope the vkey is close enough.. */
slouken@4162
   598
		if((keysym->sym == vkey) || (vkey > 0x7f))
slouken@0
   599
		keysym->unicode = vkey;
slouken@0
   600
#else
slouken@1253
   601
		BYTE	keystate[256];
slouken@1253
   602
		Uint16	wchars[2];
slouken@0
   603
slouken@0
   604
		GetKeyboardState(keystate);
slouken@4170
   605
		/* Numlock isn't taken into account in ToUnicode,
slouken@4170
   606
		 * so we handle it as a special case here */
slouken@4170
   607
		if ((keystate[VK_NUMLOCK] & 1) && vkey >= VK_NUMPAD0 && vkey <= VK_NUMPAD9)
slouken@4170
   608
		{
slouken@4170
   609
			keysym->unicode = vkey - VK_NUMPAD0 + '0';
slouken@4170
   610
		}
slouken@4170
   611
		else if (SDL_ToUnicode((UINT)vkey, scancode, keystate, wchars, sizeof(wchars)/sizeof(wchars[0]), 0) > 0)
slouken@1253
   612
		{
slouken@1253
   613
			keysym->unicode = wchars[0];
slouken@0
   614
		}
slouken@0
   615
#endif /* NO_GETKEYBOARDSTATE */
slouken@0
   616
	}
icculus@1852
   617
icculus@1852
   618
#if 0
icculus@1852
   619
	{
icculus@1852
   620
		HKL     hLayoutCurrent = GetKeyboardLayout(0);
icculus@1852
   621
		int     sc = scancode & 0xFF;
icculus@1852
   622
icculus@1852
   623
		printf("SYM:%d, VK:0x%02X, SC:0x%04X, US:(1:0x%02X, 3:0x%02X), "
icculus@1852
   624
			"Current:(1:0x%02X, 3:0x%02X)\n",
icculus@1852
   625
			keysym->sym, vkey, scancode,
icculus@1852
   626
			MapVirtualKeyEx(sc, 1, hLayoutUS),
icculus@1852
   627
			MapVirtualKeyEx(sc, 3, hLayoutUS),
icculus@1852
   628
			MapVirtualKeyEx(sc, 1, hLayoutCurrent),
icculus@1852
   629
			MapVirtualKeyEx(sc, 3, hLayoutCurrent)
icculus@1852
   630
		);
icculus@1852
   631
	}
icculus@1852
   632
#endif
slouken@0
   633
	return(keysym);
slouken@0
   634
}
slouken@0
   635
slouken@0
   636
int DIB_CreateWindow(_THIS)
slouken@0
   637
{
slouken@4219
   638
	char *windowid;
icculus@1152
   639
slouken@1288
   640
	SDL_RegisterApp(NULL, 0, 0);
slouken@1280
   641
slouken@4219
   642
	windowid = SDL_getenv("SDL_WINDOWID");
slouken@1280
   643
	SDL_windowid = (windowid != NULL);
slouken@0
   644
	if ( SDL_windowid ) {
icculus@1152
   645
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
slouken@1280
   646
		/* wince 2.1 does not have strtol */
slouken@1336
   647
		wchar_t *windowid_t = SDL_malloc((SDL_strlen(windowid) + 1) * sizeof(wchar_t));
slouken@1336
   648
		MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, windowid, -1, windowid_t, SDL_strlen(windowid) + 1);
slouken@1280
   649
		SDL_Window = (HWND)wcstol(windowid_t, NULL, 0);
slouken@1336
   650
		SDL_free(windowid_t);
icculus@1152
   651
#else
slouken@1456
   652
		SDL_Window = (HWND)SDL_strtoull(windowid, NULL, 0);
icculus@1152
   653
#endif
slouken@974
   654
		if ( SDL_Window == NULL ) {
slouken@974
   655
			SDL_SetError("Couldn't get user specified window");
slouken@974
   656
			return(-1);
slouken@974
   657
		}
slouken@974
   658
slouken@974
   659
		/* DJM: we want all event's for the user specified
slouken@974
   660
			window to be handled by SDL.
slouken@974
   661
		 */
slouken@1472
   662
		userWindowProc = (WNDPROCTYPE)GetWindowLongPtr(SDL_Window, GWLP_WNDPROC);
slouken@1472
   663
		SetWindowLongPtr(SDL_Window, GWLP_WNDPROC, (LONG_PTR)WinMessage);
slouken@0
   664
	} else {
slouken@0
   665
		SDL_Window = CreateWindow(SDL_Appname, SDL_Appname,
slouken@0
   666
                        (WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX),
slouken@833
   667
                        CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, NULL, NULL, SDL_Instance, NULL);
slouken@0
   668
		if ( SDL_Window == NULL ) {
slouken@0
   669
			SDL_SetError("Couldn't create window");
slouken@0
   670
			return(-1);
slouken@0
   671
		}
slouken@0
   672
		ShowWindow(SDL_Window, SW_HIDE);
slouken@0
   673
	}
slouken@1523
   674
slouken@1523
   675
	/* JC 14 Mar 2006
slouken@1523
   676
		Flush the message loop or this can cause big problems later
slouken@1523
   677
		Especially if the user decides to use dialog boxes or assert()!
slouken@1523
   678
	*/
slouken@1523
   679
	WIN_FlushMessageQueue();
slouken@1523
   680
slouken@0
   681
	return(0);
slouken@0
   682
}
slouken@0
   683
slouken@0
   684
void DIB_DestroyWindow(_THIS)
slouken@0
   685
{
slouken@974
   686
	if ( SDL_windowid ) {
slouken@1472
   687
		SetWindowLongPtr(SDL_Window, GWLP_WNDPROC, (LONG_PTR)userWindowProc);
slouken@974
   688
	} else {
slouken@0
   689
		DestroyWindow(SDL_Window);
slouken@0
   690
	}
slouken@1288
   691
	SDL_UnregisterApp();
slouken@1523
   692
slouken@1523
   693
	/* JC 14 Mar 2006
slouken@1523
   694
		Flush the message loop or this can cause big problems later
slouken@1523
   695
		Especially if the user decides to use dialog boxes or assert()!
slouken@1523
   696
	*/
slouken@1523
   697
	WIN_FlushMessageQueue();
slouken@0
   698
}