src/video/windib/SDL_dibevents.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 16 Feb 2009 22:32:34 +0000
branchSDL-1.2
changeset 4162 3b7fc3416601
parent 4159 a1b03ba2fcd0
child 4167 a6f635e5eaa6
permissions -rw-r--r--
GAPI fixes from Stefan Klug
     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 #endif 
    92 
    93 
    94 /* The main Win32 event handler */
    95 LRESULT DIB_HandleMessage(_THIS, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    96 {
    97 	extern int posted;
    98 
    99 	switch (msg) {
   100 		case WM_SYSKEYDOWN:
   101 		case WM_KEYDOWN: {
   102 			SDL_keysym keysym;
   103 
   104 #ifdef SDL_VIDEO_DRIVER_GAPI
   105 			if(this->hidden->gapiInfo)
   106 			{
   107 				// Drop GAPI artefacts
   108 				if (wParam == 0x84 || wParam == 0x5B)
   109 					return 0;
   110 
   111 				wParam = rotateKey(wParam, this->hidden->gapiInfo->coordinateTransform);
   112 			}
   113 #endif 
   114 			/* Ignore repeated keys */
   115 			if ( lParam&REPEATED_KEYMASK ) {
   116 				return(0);
   117 			}
   118 			switch (wParam) {
   119 				case VK_CONTROL:
   120 					if ( lParam&EXTENDED_KEYMASK )
   121 						wParam = VK_RCONTROL;
   122 					else
   123 						wParam = VK_LCONTROL;
   124 					break;
   125 				case VK_SHIFT:
   126 					/* EXTENDED trick doesn't work here */
   127 					{
   128 					Uint8 *state = SDL_GetKeyState(NULL);
   129 					if (state[SDLK_LSHIFT] == SDL_RELEASED && (GetKeyState(VK_LSHIFT) & 0x8000)) {
   130 						wParam = VK_LSHIFT;
   131 					} else if (state[SDLK_RSHIFT] == SDL_RELEASED && (GetKeyState(VK_RSHIFT) & 0x8000)) {
   132 						wParam = VK_RSHIFT;
   133 					} else {
   134 						/* Win9x */
   135 						int sc = HIWORD(lParam) & 0xFF;
   136 
   137 						if (sc == 0x2A)
   138 							wParam = VK_LSHIFT;
   139 						else
   140 						if (sc == 0x36)
   141 							wParam = VK_RSHIFT;
   142 						else
   143 							wParam = VK_LSHIFT;
   144 					}
   145 					}
   146 					break;
   147 				case VK_MENU:
   148 					if ( lParam&EXTENDED_KEYMASK )
   149 						wParam = VK_RMENU;
   150 					else
   151 						wParam = VK_LMENU;
   152 					break;
   153 			}
   154 #ifdef NO_GETKEYBOARDSTATE
   155 			/* this is the workaround for the missing ToAscii() and ToUnicode() in CE (not necessary at KEYUP!) */
   156 			if ( SDL_TranslateUNICODE ) {
   157 				MSG m;
   158 
   159 				m.hwnd = hwnd;
   160 				m.message = msg;
   161 				m.wParam = wParam;
   162 				m.lParam = lParam;
   163 				m.time = 0;
   164 				if ( TranslateMessage(&m) && PeekMessage(&m, hwnd, 0, WM_USER, PM_NOREMOVE) && (m.message == WM_CHAR) ) {
   165 					GetMessage(&m, hwnd, 0, WM_USER);
   166 			    		wParam = m.wParam;
   167 				}
   168 			}
   169 #endif /* NO_GETKEYBOARDSTATE */
   170 			posted = SDL_PrivateKeyboard(SDL_PRESSED,
   171 				TranslateKey(wParam,HIWORD(lParam),&keysym,1));
   172 		}
   173 		return(0);
   174 
   175 		case WM_SYSKEYUP:
   176 		case WM_KEYUP: {
   177 			SDL_keysym keysym;
   178 
   179 #ifdef SDL_VIDEO_DRIVER_GAPI
   180 			if(this->hidden->gapiInfo)
   181 			{
   182 				// Drop GAPI artifacts
   183 				if (wParam == 0x84 || wParam == 0x5B)
   184 					return 0;
   185 	
   186 				wParam = rotateKey(wParam, this->hidden->gapiInfo->coordinateTransform);
   187 			}
   188 #endif
   189 
   190 			switch (wParam) {
   191 				case VK_CONTROL:
   192 					if ( lParam&EXTENDED_KEYMASK )
   193 						wParam = VK_RCONTROL;
   194 					else
   195 						wParam = VK_LCONTROL;
   196 					break;
   197 				case VK_SHIFT:
   198 					/* EXTENDED trick doesn't work here */
   199 					{
   200 					Uint8 *state = SDL_GetKeyState(NULL);
   201 					if (state[SDLK_LSHIFT] == SDL_PRESSED && !(GetKeyState(VK_LSHIFT) & 0x8000)) {
   202 						wParam = VK_LSHIFT;
   203 					} else if (state[SDLK_RSHIFT] == SDL_PRESSED && !(GetKeyState(VK_RSHIFT) & 0x8000)) {
   204 						wParam = VK_RSHIFT;
   205 					} else {
   206 						/* Win9x */
   207 						int sc = HIWORD(lParam) & 0xFF;
   208 
   209 						if (sc == 0x2A)
   210 							wParam = VK_LSHIFT;
   211 						else
   212 						if (sc == 0x36)
   213 							wParam = VK_RSHIFT;
   214 						else
   215 							wParam = VK_LSHIFT;
   216 					}
   217 					}
   218 					break;
   219 				case VK_MENU:
   220 					if ( lParam&EXTENDED_KEYMASK )
   221 						wParam = VK_RMENU;
   222 					else
   223 						wParam = VK_LMENU;
   224 					break;
   225 			}
   226 			/* Windows only reports keyup for print screen */
   227 			if ( wParam == VK_SNAPSHOT && SDL_GetKeyState(NULL)[SDLK_PRINT] == SDL_RELEASED ) {
   228 				posted = SDL_PrivateKeyboard(SDL_PRESSED,
   229 					TranslateKey(wParam,HIWORD(lParam),&keysym,1));
   230 			}
   231 			posted = SDL_PrivateKeyboard(SDL_RELEASED,
   232 				TranslateKey(wParam,HIWORD(lParam),&keysym,0));
   233 		}
   234 		return(0);
   235 #if defined(SC_SCREENSAVE) && defined(SC_MONITORPOWER)
   236 		case WM_SYSCOMMAND: {
   237 			const DWORD val = (DWORD) (wParam & 0xFFF0);
   238 			if ((val == SC_SCREENSAVE) || (val == SC_MONITORPOWER)) {
   239 				if (this->hidden->dibInfo && !allow_screensaver) {
   240 					/* Note that this doesn't stop anything on Vista
   241 					   if the screensaver has a password. */
   242 					return(0);
   243 				}
   244 			}
   245 		}
   246 		/* Fall through to default processing */
   247 #endif /* SC_SCREENSAVE && SC_MONITORPOWER */
   248 
   249 		default: {
   250 			/* Only post the event if we're watching for it */
   251 			if ( SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE ) {
   252 			        SDL_SysWMmsg wmmsg;
   253 
   254 				SDL_VERSION(&wmmsg.version);
   255 				wmmsg.hwnd = hwnd;
   256 				wmmsg.msg = msg;
   257 				wmmsg.wParam = wParam;
   258 				wmmsg.lParam = lParam;
   259 				posted = SDL_PrivateSysWMEvent(&wmmsg);
   260 
   261 			/* DJM: If the user isn't watching for private
   262 				messages in her SDL event loop, then pass it
   263 				along to any win32 specific window proc.
   264 			 */
   265 			} else if (userWindowProc) {
   266 				return CallWindowProc(userWindowProc, hwnd, msg, wParam, lParam);
   267 			}
   268 		}
   269 		break;
   270 	}
   271 	return(DefWindowProc(hwnd, msg, wParam, lParam));
   272 }
   273 
   274 void DIB_PumpEvents(_THIS)
   275 {
   276 	MSG msg;
   277 
   278 	while ( PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) ) {
   279 		if ( GetMessage(&msg, NULL, 0, 0) > 0 ) {
   280 			DispatchMessage(&msg);
   281 		}
   282 	}
   283 }
   284 
   285 static HKL hLayoutUS = NULL;
   286 
   287 void DIB_InitOSKeymap(_THIS)
   288 {
   289 	int	i;
   290 #ifndef _WIN32_WCE
   291 	char	current_layout[KL_NAMELENGTH];
   292 
   293 	GetKeyboardLayoutName(current_layout);
   294 	//printf("Initial Keyboard Layout Name: '%s'\n", current_layout);
   295 
   296 	hLayoutUS = LoadKeyboardLayout("00000409", KLF_NOTELLSHELL);
   297 
   298 	if (!hLayoutUS) {
   299 		//printf("Failed to load US keyboard layout. Using current.\n");
   300 		hLayoutUS = GetKeyboardLayout(0);
   301 	}
   302 	LoadKeyboardLayout(current_layout, KLF_ACTIVATE);
   303 #else
   304 #if _WIN32_WCE >=420
   305 	TCHAR	current_layout[KL_NAMELENGTH];
   306 
   307 	GetKeyboardLayoutName(current_layout);
   308 	//printf("Initial Keyboard Layout Name: '%s'\n", current_layout);
   309 
   310 	hLayoutUS = LoadKeyboardLayout(L"00000409", 0);
   311 
   312 	if (!hLayoutUS) {
   313 		//printf("Failed to load US keyboard layout. Using current.\n");
   314 		hLayoutUS = GetKeyboardLayout(0);
   315 	}
   316 	LoadKeyboardLayout(current_layout, 0);
   317 #endif // _WIN32_WCE >=420
   318 #endif
   319 	/* Map the VK keysyms */
   320 	for ( i=0; i<SDL_arraysize(VK_keymap); ++i )
   321 		VK_keymap[i] = SDLK_UNKNOWN;
   322 
   323 	VK_keymap[VK_BACK] = SDLK_BACKSPACE;
   324 	VK_keymap[VK_TAB] = SDLK_TAB;
   325 	VK_keymap[VK_CLEAR] = SDLK_CLEAR;
   326 	VK_keymap[VK_RETURN] = SDLK_RETURN;
   327 	VK_keymap[VK_PAUSE] = SDLK_PAUSE;
   328 	VK_keymap[VK_ESCAPE] = SDLK_ESCAPE;
   329 	VK_keymap[VK_SPACE] = SDLK_SPACE;
   330 	VK_keymap[VK_APOSTROPHE] = SDLK_QUOTE;
   331 	VK_keymap[VK_COMMA] = SDLK_COMMA;
   332 	VK_keymap[VK_MINUS] = SDLK_MINUS;
   333 	VK_keymap[VK_PERIOD] = SDLK_PERIOD;
   334 	VK_keymap[VK_SLASH] = SDLK_SLASH;
   335 	VK_keymap[VK_0] = SDLK_0;
   336 	VK_keymap[VK_1] = SDLK_1;
   337 	VK_keymap[VK_2] = SDLK_2;
   338 	VK_keymap[VK_3] = SDLK_3;
   339 	VK_keymap[VK_4] = SDLK_4;
   340 	VK_keymap[VK_5] = SDLK_5;
   341 	VK_keymap[VK_6] = SDLK_6;
   342 	VK_keymap[VK_7] = SDLK_7;
   343 	VK_keymap[VK_8] = SDLK_8;
   344 	VK_keymap[VK_9] = SDLK_9;
   345 	VK_keymap[VK_SEMICOLON] = SDLK_SEMICOLON;
   346 	VK_keymap[VK_EQUALS] = SDLK_EQUALS;
   347 	VK_keymap[VK_LBRACKET] = SDLK_LEFTBRACKET;
   348 	VK_keymap[VK_BACKSLASH] = SDLK_BACKSLASH;
   349 	VK_keymap[VK_OEM_102] = SDLK_LESS;
   350 	VK_keymap[VK_RBRACKET] = SDLK_RIGHTBRACKET;
   351 	VK_keymap[VK_GRAVE] = SDLK_BACKQUOTE;
   352 	VK_keymap[VK_BACKTICK] = SDLK_BACKQUOTE;
   353 	VK_keymap[VK_A] = SDLK_a;
   354 	VK_keymap[VK_B] = SDLK_b;
   355 	VK_keymap[VK_C] = SDLK_c;
   356 	VK_keymap[VK_D] = SDLK_d;
   357 	VK_keymap[VK_E] = SDLK_e;
   358 	VK_keymap[VK_F] = SDLK_f;
   359 	VK_keymap[VK_G] = SDLK_g;
   360 	VK_keymap[VK_H] = SDLK_h;
   361 	VK_keymap[VK_I] = SDLK_i;
   362 	VK_keymap[VK_J] = SDLK_j;
   363 	VK_keymap[VK_K] = SDLK_k;
   364 	VK_keymap[VK_L] = SDLK_l;
   365 	VK_keymap[VK_M] = SDLK_m;
   366 	VK_keymap[VK_N] = SDLK_n;
   367 	VK_keymap[VK_O] = SDLK_o;
   368 	VK_keymap[VK_P] = SDLK_p;
   369 	VK_keymap[VK_Q] = SDLK_q;
   370 	VK_keymap[VK_R] = SDLK_r;
   371 	VK_keymap[VK_S] = SDLK_s;
   372 	VK_keymap[VK_T] = SDLK_t;
   373 	VK_keymap[VK_U] = SDLK_u;
   374 	VK_keymap[VK_V] = SDLK_v;
   375 	VK_keymap[VK_W] = SDLK_w;
   376 	VK_keymap[VK_X] = SDLK_x;
   377 	VK_keymap[VK_Y] = SDLK_y;
   378 	VK_keymap[VK_Z] = SDLK_z;
   379 	VK_keymap[VK_DELETE] = SDLK_DELETE;
   380 
   381 	VK_keymap[VK_NUMPAD0] = SDLK_KP0;
   382 	VK_keymap[VK_NUMPAD1] = SDLK_KP1;
   383 	VK_keymap[VK_NUMPAD2] = SDLK_KP2;
   384 	VK_keymap[VK_NUMPAD3] = SDLK_KP3;
   385 	VK_keymap[VK_NUMPAD4] = SDLK_KP4;
   386 	VK_keymap[VK_NUMPAD5] = SDLK_KP5;
   387 	VK_keymap[VK_NUMPAD6] = SDLK_KP6;
   388 	VK_keymap[VK_NUMPAD7] = SDLK_KP7;
   389 	VK_keymap[VK_NUMPAD8] = SDLK_KP8;
   390 	VK_keymap[VK_NUMPAD9] = SDLK_KP9;
   391 	VK_keymap[VK_DECIMAL] = SDLK_KP_PERIOD;
   392 	VK_keymap[VK_DIVIDE] = SDLK_KP_DIVIDE;
   393 	VK_keymap[VK_MULTIPLY] = SDLK_KP_MULTIPLY;
   394 	VK_keymap[VK_SUBTRACT] = SDLK_KP_MINUS;
   395 	VK_keymap[VK_ADD] = SDLK_KP_PLUS;
   396 
   397 	VK_keymap[VK_UP] = SDLK_UP;
   398 	VK_keymap[VK_DOWN] = SDLK_DOWN;
   399 	VK_keymap[VK_RIGHT] = SDLK_RIGHT;
   400 	VK_keymap[VK_LEFT] = SDLK_LEFT;
   401 	VK_keymap[VK_INSERT] = SDLK_INSERT;
   402 	VK_keymap[VK_HOME] = SDLK_HOME;
   403 	VK_keymap[VK_END] = SDLK_END;
   404 	VK_keymap[VK_PRIOR] = SDLK_PAGEUP;
   405 	VK_keymap[VK_NEXT] = SDLK_PAGEDOWN;
   406 
   407 	VK_keymap[VK_F1] = SDLK_F1;
   408 	VK_keymap[VK_F2] = SDLK_F2;
   409 	VK_keymap[VK_F3] = SDLK_F3;
   410 	VK_keymap[VK_F4] = SDLK_F4;
   411 	VK_keymap[VK_F5] = SDLK_F5;
   412 	VK_keymap[VK_F6] = SDLK_F6;
   413 	VK_keymap[VK_F7] = SDLK_F7;
   414 	VK_keymap[VK_F8] = SDLK_F8;
   415 	VK_keymap[VK_F9] = SDLK_F9;
   416 	VK_keymap[VK_F10] = SDLK_F10;
   417 	VK_keymap[VK_F11] = SDLK_F11;
   418 	VK_keymap[VK_F12] = SDLK_F12;
   419 	VK_keymap[VK_F13] = SDLK_F13;
   420 	VK_keymap[VK_F14] = SDLK_F14;
   421 	VK_keymap[VK_F15] = SDLK_F15;
   422 
   423 	VK_keymap[VK_NUMLOCK] = SDLK_NUMLOCK;
   424 	VK_keymap[VK_CAPITAL] = SDLK_CAPSLOCK;
   425 	VK_keymap[VK_SCROLL] = SDLK_SCROLLOCK;
   426 	VK_keymap[VK_RSHIFT] = SDLK_RSHIFT;
   427 	VK_keymap[VK_LSHIFT] = SDLK_LSHIFT;
   428 	VK_keymap[VK_RCONTROL] = SDLK_RCTRL;
   429 	VK_keymap[VK_LCONTROL] = SDLK_LCTRL;
   430 	VK_keymap[VK_RMENU] = SDLK_RALT;
   431 	VK_keymap[VK_LMENU] = SDLK_LALT;
   432 	VK_keymap[VK_RWIN] = SDLK_RSUPER;
   433 	VK_keymap[VK_LWIN] = SDLK_LSUPER;
   434 
   435 	VK_keymap[VK_HELP] = SDLK_HELP;
   436 #ifdef VK_PRINT
   437 	VK_keymap[VK_PRINT] = SDLK_PRINT;
   438 #endif
   439 	VK_keymap[VK_SNAPSHOT] = SDLK_PRINT;
   440 	VK_keymap[VK_CANCEL] = SDLK_BREAK;
   441 	VK_keymap[VK_APPS] = SDLK_MENU;
   442 
   443 	Arrows_keymap[3] = 0x25;
   444 	Arrows_keymap[2] = 0x26;
   445 	Arrows_keymap[1] = 0x27;
   446 	Arrows_keymap[0] = 0x28;
   447 }
   448 
   449 #define EXTKEYPAD(keypad) ((scancode & 0x100)?(mvke):(keypad))
   450 
   451 static int SDL_MapVirtualKey(int scancode, int vkey)
   452 {
   453 #ifndef _WIN32_WCE
   454 	int	mvke  = MapVirtualKeyEx(scancode & 0xFF, 1, hLayoutUS);
   455 #else
   456 	int	mvke  = MapVirtualKey(scancode & 0xFF, 1);
   457 #endif
   458 
   459 	switch(vkey) {
   460 		/* These are always correct */
   461 		case VK_DIVIDE:
   462 		case VK_MULTIPLY:
   463 		case VK_SUBTRACT:
   464 		case VK_ADD:
   465 		case VK_LWIN:
   466 		case VK_RWIN:
   467 		case VK_APPS:
   468 		/* These are already handled */
   469 		case VK_LCONTROL:
   470 		case VK_RCONTROL:
   471 		case VK_LSHIFT:
   472 		case VK_RSHIFT:
   473 		case VK_LMENU:
   474 		case VK_RMENU:
   475 		case VK_SNAPSHOT:
   476 		case VK_PAUSE:
   477 			return vkey;
   478 	}	
   479 	switch(mvke) {
   480 		/* Distinguish between keypad and extended keys */
   481 		case VK_INSERT: return EXTKEYPAD(VK_NUMPAD0);
   482 		case VK_DELETE: return EXTKEYPAD(VK_DECIMAL);
   483 		case VK_END:    return EXTKEYPAD(VK_NUMPAD1);
   484 		case VK_DOWN:   return EXTKEYPAD(VK_NUMPAD2);
   485 		case VK_NEXT:   return EXTKEYPAD(VK_NUMPAD3);
   486 		case VK_LEFT:   return EXTKEYPAD(VK_NUMPAD4);
   487 		case VK_CLEAR:  return EXTKEYPAD(VK_NUMPAD5);
   488 		case VK_RIGHT:  return EXTKEYPAD(VK_NUMPAD6);
   489 		case VK_HOME:   return EXTKEYPAD(VK_NUMPAD7);
   490 		case VK_UP:     return EXTKEYPAD(VK_NUMPAD8);
   491 		case VK_PRIOR:  return EXTKEYPAD(VK_NUMPAD9);
   492 	}
   493 	return mvke?mvke:vkey;
   494 }
   495 
   496 static SDL_keysym *TranslateKey(WPARAM vkey, UINT scancode, SDL_keysym *keysym, int pressed)
   497 {
   498 	/* Set the keysym information */
   499 	keysym->scancode = (unsigned char) scancode;
   500 	keysym->mod = KMOD_NONE;
   501 	keysym->unicode = 0;
   502 	
   503 	if ((vkey == VK_RETURN) && (scancode & 0x100)) {
   504 		/* No VK_ code for the keypad enter key */
   505 		keysym->sym = SDLK_KP_ENTER;
   506 	}
   507 	else {
   508 		keysym->sym = VK_keymap[SDL_MapVirtualKey(scancode, vkey)];
   509 	}
   510 
   511 	if ( pressed && SDL_TranslateUNICODE ) {
   512 #ifdef NO_GETKEYBOARDSTATE
   513 		/* Uh oh, better hope the vkey is close enough.. */
   514 		if((keysym->sym == vkey) || (vkey > 0x7f))
   515 		keysym->unicode = vkey;
   516 #else
   517 		BYTE	keystate[256];
   518 		Uint16	wchars[2];
   519 
   520 		GetKeyboardState(keystate);
   521 		if (SDL_ToUnicode((UINT)vkey, scancode, keystate, wchars, sizeof(wchars)/sizeof(wchars[0]), 0) == 1)
   522 		{
   523 			keysym->unicode = wchars[0];
   524 		}
   525 #endif /* NO_GETKEYBOARDSTATE */
   526 	}
   527 
   528 #if 0
   529 	{
   530 		HKL     hLayoutCurrent = GetKeyboardLayout(0);
   531 		int     sc = scancode & 0xFF;
   532 
   533 		printf("SYM:%d, VK:0x%02X, SC:0x%04X, US:(1:0x%02X, 3:0x%02X), "
   534 			"Current:(1:0x%02X, 3:0x%02X)\n",
   535 			keysym->sym, vkey, scancode,
   536 			MapVirtualKeyEx(sc, 1, hLayoutUS),
   537 			MapVirtualKeyEx(sc, 3, hLayoutUS),
   538 			MapVirtualKeyEx(sc, 1, hLayoutCurrent),
   539 			MapVirtualKeyEx(sc, 3, hLayoutCurrent)
   540 		);
   541 	}
   542 #endif
   543 	return(keysym);
   544 }
   545 
   546 int DIB_CreateWindow(_THIS)
   547 {
   548 	char *windowid = SDL_getenv("SDL_WINDOWID");
   549 
   550 	SDL_RegisterApp(NULL, 0, 0);
   551 
   552 	SDL_windowid = (windowid != NULL);
   553 	if ( SDL_windowid ) {
   554 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
   555 		/* wince 2.1 does not have strtol */
   556 		wchar_t *windowid_t = SDL_malloc((SDL_strlen(windowid) + 1) * sizeof(wchar_t));
   557 		MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, windowid, -1, windowid_t, SDL_strlen(windowid) + 1);
   558 		SDL_Window = (HWND)wcstol(windowid_t, NULL, 0);
   559 		SDL_free(windowid_t);
   560 #else
   561 		SDL_Window = (HWND)SDL_strtoull(windowid, NULL, 0);
   562 #endif
   563 		if ( SDL_Window == NULL ) {
   564 			SDL_SetError("Couldn't get user specified window");
   565 			return(-1);
   566 		}
   567 
   568 		/* DJM: we want all event's for the user specified
   569 			window to be handled by SDL.
   570 		 */
   571 		userWindowProc = (WNDPROCTYPE)GetWindowLongPtr(SDL_Window, GWLP_WNDPROC);
   572 		SetWindowLongPtr(SDL_Window, GWLP_WNDPROC, (LONG_PTR)WinMessage);
   573 	} else {
   574 		SDL_Window = CreateWindow(SDL_Appname, SDL_Appname,
   575                         (WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX),
   576                         CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, NULL, NULL, SDL_Instance, NULL);
   577 		if ( SDL_Window == NULL ) {
   578 			SDL_SetError("Couldn't create window");
   579 			return(-1);
   580 		}
   581 		ShowWindow(SDL_Window, SW_HIDE);
   582 	}
   583 
   584 	/* JC 14 Mar 2006
   585 		Flush the message loop or this can cause big problems later
   586 		Especially if the user decides to use dialog boxes or assert()!
   587 	*/
   588 	WIN_FlushMessageQueue();
   589 
   590 	return(0);
   591 }
   592 
   593 void DIB_DestroyWindow(_THIS)
   594 {
   595 	if ( SDL_windowid ) {
   596 		SetWindowLongPtr(SDL_Window, GWLP_WNDPROC, (LONG_PTR)userWindowProc);
   597 	} else {
   598 		DestroyWindow(SDL_Window);
   599 	}
   600 	SDL_UnregisterApp();
   601 
   602 	/* JC 14 Mar 2006
   603 		Flush the message loop or this can cause big problems later
   604 		Especially if the user decides to use dialog boxes or assert()!
   605 	*/
   606 	WIN_FlushMessageQueue();
   607 }