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