src/video/windib/SDL_dibevents.c
author Ozkan Sezer <sezeroz@gmail.com>
Sat, 24 Mar 2018 23:38:50 +0300
branchSDL-1.2
changeset 11977 6cf3a08b42dc
parent 11974 b8aca1c1ad6e
permissions -rw-r--r--
backport bug #3332 (win32 deadkeys) patch:

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