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