19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
247891dd35fa2439a70f43ab8572778a398365bf24David 'Digit' Turner#define WIN32_LEAN_AND_MEAN 1
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <windows.h>
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_main.h"
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_events.h"
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_syswm.h"
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../../events/SDL_sysevents.h"
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../../events/SDL_events_c.h"
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../wincommon/SDL_lowvideo.h"
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_gapidibvideo.h"
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_vkeys.h"
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef SDL_VIDEO_DRIVER_GAPI
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../gapi/SDL_gapivideo.h"
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef SDL_VIDEO_DRIVER_WINDIB
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_dibvideo.h"
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef WM_APP
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define WM_APP	0x8000
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef _WIN32_WCE
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define NO_GETKEYBOARDSTATE
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* The translation table from a Microsoft VK keysym to a SDL keysym */
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDLKey VK_keymap[SDLK_LAST];
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_keysym *TranslateKey(WPARAM vkey, UINT scancode, SDL_keysym *keysym, int pressed);
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDLKey Arrows_keymap[4];
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Masks for processing the windows KEYDOWN and KEYUP messages */
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define REPEATED_KEYMASK	(1<<30)
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define EXTENDED_KEYMASK	(1<<24)
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* DJM: If the user setup the window for us, we want to save his window proc,
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   and give him a chance to handle some messages. */
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef STRICT
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define WNDPROCTYPE	WNDPROC
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define WNDPROCTYPE	FARPROC
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic WNDPROCTYPE userWindowProc = NULL;
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef SDL_VIDEO_DRIVER_GAPI
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallWPARAM rotateKey(WPARAM key,int direction)
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if(direction ==0 ) return key;
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch (key) {
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case 0x26: /* up */
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return Arrows_keymap[(2 + direction) % 4];
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case 0x27: /* right */
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return Arrows_keymap[(1 + direction) % 4];
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case 0x28: /* down */
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return Arrows_keymap[direction % 4];
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case 0x25: /* left */
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return Arrows_keymap[(3 + direction) % 4];
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return key;
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void GapiTransform(GapiInfo *gapiInfo, LONG *x, LONG *y)
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if(gapiInfo->hiresFix)
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	*x *= 2;
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	*y *= 2;
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // 0 3 0
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if((!gapiInfo->userOrientation && gapiInfo->systemOrientation && !gapiInfo->gapiOrientation) ||
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // 3 0 3
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       (gapiInfo->userOrientation && !gapiInfo->systemOrientation && gapiInfo->gapiOrientation) ||
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // 3 0 0
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       (gapiInfo->userOrientation && !gapiInfo->systemOrientation && !gapiInfo->gapiOrientation))
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Sint16 temp = *x;
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *x = SDL_VideoSurface->w - *y;
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *y = temp;
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    else
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // 0 0 0
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if((!gapiInfo->userOrientation && !gapiInfo->systemOrientation && !gapiInfo->gapiOrientation) ||
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // 0 0 3
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      (!gapiInfo->userOrientation && !gapiInfo->systemOrientation && gapiInfo->gapiOrientation))
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	// without changes
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	// *x = *x;
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	// *y = *y;
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // default
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    else
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	// without changes
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	// *x = *x;
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	// *y = *y;
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* The main Win32 event handler */
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallLRESULT DIB_HandleMessage(_THIS, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	extern int posted;
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch (msg) {
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case WM_SYSKEYDOWN:
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case WM_KEYDOWN: {
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_keysym keysym;
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef SDL_VIDEO_DRIVER_GAPI
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if(this->hidden->gapiInfo)
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				// Drop GAPI artefacts
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if (wParam == 0x84 || wParam == 0x5B)
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					return 0;
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				wParam = rotateKey(wParam, this->hidden->gapiInfo->coordinateTransform);
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Ignore repeated keys */
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( lParam&REPEATED_KEYMASK ) {
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				return(0);
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			switch (wParam) {
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				case VK_CONTROL:
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					if ( lParam&EXTENDED_KEYMASK )
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						wParam = VK_RCONTROL;
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					else
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						wParam = VK_LCONTROL;
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					break;
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				case VK_SHIFT:
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					/* EXTENDED trick doesn't work here */
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					{
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					Uint8 *state = SDL_GetKeyState(NULL);
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					if (state[SDLK_LSHIFT] == SDL_RELEASED && (GetKeyState(VK_LSHIFT) & 0x8000)) {
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						wParam = VK_LSHIFT;
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					} else if (state[SDLK_RSHIFT] == SDL_RELEASED && (GetKeyState(VK_RSHIFT) & 0x8000)) {
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						wParam = VK_RSHIFT;
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					} else {
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						/* Win9x */
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						int sc = HIWORD(lParam) & 0xFF;
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						if (sc == 0x2A)
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							wParam = VK_LSHIFT;
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						else
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						if (sc == 0x36)
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							wParam = VK_RSHIFT;
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						else
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							wParam = VK_LSHIFT;
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					}
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					}
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					break;
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				case VK_MENU:
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					if ( lParam&EXTENDED_KEYMASK )
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						wParam = VK_RMENU;
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					else
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						wParam = VK_LMENU;
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					break;
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef NO_GETKEYBOARDSTATE
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* this is the workaround for the missing ToAscii() and ToUnicode() in CE (not necessary at KEYUP!) */
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( SDL_TranslateUNICODE ) {
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				MSG m;
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				m.hwnd = hwnd;
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				m.message = msg;
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				m.wParam = wParam;
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				m.lParam = lParam;
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				m.time = 0;
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( TranslateMessage(&m) && PeekMessage(&m, hwnd, 0, WM_USER, PM_NOREMOVE) && (m.message == WM_CHAR) ) {
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					GetMessage(&m, hwnd, 0, WM_USER);
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    		wParam = m.wParam;
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* NO_GETKEYBOARDSTATE */
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			posted = SDL_PrivateKeyboard(SDL_PRESSED,
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				TranslateKey(wParam,HIWORD(lParam),&keysym,1));
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(0);
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case WM_SYSKEYUP:
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case WM_KEYUP: {
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_keysym keysym;
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef SDL_VIDEO_DRIVER_GAPI
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if(this->hidden->gapiInfo)
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				// Drop GAPI artifacts
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if (wParam == 0x84 || wParam == 0x5B)
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					return 0;
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				wParam = rotateKey(wParam, this->hidden->gapiInfo->coordinateTransform);
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			switch (wParam) {
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				case VK_CONTROL:
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					if ( lParam&EXTENDED_KEYMASK )
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						wParam = VK_RCONTROL;
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					else
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						wParam = VK_LCONTROL;
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					break;
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				case VK_SHIFT:
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					/* EXTENDED trick doesn't work here */
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					{
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					Uint8 *state = SDL_GetKeyState(NULL);
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					if (state[SDLK_LSHIFT] == SDL_PRESSED && !(GetKeyState(VK_LSHIFT) & 0x8000)) {
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						wParam = VK_LSHIFT;
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					} else if (state[SDLK_RSHIFT] == SDL_PRESSED && !(GetKeyState(VK_RSHIFT) & 0x8000)) {
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						wParam = VK_RSHIFT;
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					} else {
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						/* Win9x */
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						int sc = HIWORD(lParam) & 0xFF;
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						if (sc == 0x2A)
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							wParam = VK_LSHIFT;
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						else
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						if (sc == 0x36)
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							wParam = VK_RSHIFT;
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						else
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							wParam = VK_LSHIFT;
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					}
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					}
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					break;
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				case VK_MENU:
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					if ( lParam&EXTENDED_KEYMASK )
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						wParam = VK_RMENU;
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					else
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						wParam = VK_LMENU;
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					break;
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Windows only reports keyup for print screen */
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( wParam == VK_SNAPSHOT && SDL_GetKeyState(NULL)[SDLK_PRINT] == SDL_RELEASED ) {
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				posted = SDL_PrivateKeyboard(SDL_PRESSED,
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					TranslateKey(wParam,HIWORD(lParam),&keysym,1));
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			posted = SDL_PrivateKeyboard(SDL_RELEASED,
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				TranslateKey(wParam,HIWORD(lParam),&keysym,0));
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(0);
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if defined(SC_SCREENSAVE) && defined(SC_MONITORPOWER)
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case WM_SYSCOMMAND: {
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			const DWORD val = (DWORD) (wParam & 0xFFF0);
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ((val == SC_SCREENSAVE) || (val == SC_MONITORPOWER)) {
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if (this->hidden->dibInfo && !allow_screensaver) {
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					/* Note that this doesn't stop anything on Vista
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					   if the screensaver has a password. */
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					return(0);
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Fall through to default processing */
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SC_SCREENSAVE && SC_MONITORPOWER */
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		default: {
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Only post the event if we're watching for it */
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE ) {
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			        SDL_SysWMmsg wmmsg;
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_VERSION(&wmmsg.version);
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				wmmsg.hwnd = hwnd;
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				wmmsg.msg = msg;
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				wmmsg.wParam = wParam;
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				wmmsg.lParam = lParam;
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				posted = SDL_PrivateSysWMEvent(&wmmsg);
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* DJM: If the user isn't watching for private
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				messages in her SDL event loop, then pass it
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				along to any win32 specific window proc.
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			 */
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			} else if (userWindowProc) {
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				return CallWindowProc(userWindowProc, hwnd, msg, wParam, lParam);
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(DefWindowProc(hwnd, msg, wParam, lParam));
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef _WIN32_WCE
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic BOOL GetLastStylusPos(POINT* ptLast)
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    BOOL bResult = FALSE;
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    UINT nRet;
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    GetMouseMovePoints(ptLast, 1, &nRet);
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( nRet == 1 ) {
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        ptLast->x /= 4;
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        ptLast->y /= 4;
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        bResult = TRUE;
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return bResult;
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DIB_GenerateMouseMotionEvent(_THIS)
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	extern int mouse_relative;
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	extern int posted;
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	POINT mouse;
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef _WIN32_WCE
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( !GetCursorPos(&mouse) && !GetLastStylusPos(&mouse) ) return;
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( !GetCursorPos(&mouse) ) return;
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( mouse_relative ) {
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		POINT center;
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		center.x = (SDL_VideoSurface->w/2);
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		center.y = (SDL_VideoSurface->h/2);
3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		ClientToScreen(SDL_Window, &center);
3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		mouse.x -= center.x;
3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		mouse.y -= center.y;
3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( mouse.x || mouse.y ) {
3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SetCursorPos(center.x, center.y);
3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			posted = SDL_PrivateMouseMotion(0, 1, (Sint16)mouse.x, (Sint16)mouse.y);
3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		ScreenToClient(SDL_Window, &mouse);
3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef SDL_VIDEO_DRIVER_GAPI
3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       if (SDL_VideoSurface && this->hidden->gapiInfo)
3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			GapiTransform(this->hidden->gapiInfo, &mouse.x, &mouse.y);
3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		posted = SDL_PrivateMouseMotion(0, 0, (Sint16)mouse.x, (Sint16)mouse.y);
3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid DIB_PumpEvents(_THIS)
3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MSG msg;
3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while ( PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) ) {
3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( GetMessage(&msg, NULL, 0, 0) > 0 ) {
3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			DispatchMessage(&msg);
3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_GetAppState() & SDL_APPMOUSEFOCUS ) {
3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		DIB_GenerateMouseMotionEvent( this );
3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic HKL hLayoutUS = NULL;
3769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid DIB_InitOSKeymap(_THIS)
3789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int	i;
3809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef _WIN32_WCE
3819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	char	current_layout[KL_NAMELENGTH];
3829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	GetKeyboardLayoutName(current_layout);
3849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	//printf("Initial Keyboard Layout Name: '%s'\n", current_layout);
3859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	hLayoutUS = LoadKeyboardLayout("00000409", KLF_NOTELLSHELL);
3879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (!hLayoutUS) {
3899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		//printf("Failed to load US keyboard layout. Using current.\n");
3909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		hLayoutUS = GetKeyboardLayout(0);
3919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	LoadKeyboardLayout(current_layout, KLF_ACTIVATE);
3939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
3949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if _WIN32_WCE >=420
3959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	TCHAR	current_layout[KL_NAMELENGTH];
3969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	GetKeyboardLayoutName(current_layout);
3989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	//printf("Initial Keyboard Layout Name: '%s'\n", current_layout);
3999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	hLayoutUS = LoadKeyboardLayout(L"00000409", 0);
4019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (!hLayoutUS) {
4039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		//printf("Failed to load US keyboard layout. Using current.\n");
4049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		hLayoutUS = GetKeyboardLayout(0);
4059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	LoadKeyboardLayout(current_layout, 0);
4079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif // _WIN32_WCE >=420
4089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Map the VK keysyms */
4109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i=0; i<SDL_arraysize(VK_keymap); ++i )
4119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		VK_keymap[i] = SDLK_UNKNOWN;
4129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_BACK] = SDLK_BACKSPACE;
4149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_TAB] = SDLK_TAB;
4159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_CLEAR] = SDLK_CLEAR;
4169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_RETURN] = SDLK_RETURN;
4179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_PAUSE] = SDLK_PAUSE;
4189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_ESCAPE] = SDLK_ESCAPE;
4199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_SPACE] = SDLK_SPACE;
4209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_APOSTROPHE] = SDLK_QUOTE;
4219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_COMMA] = SDLK_COMMA;
4229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_MINUS] = SDLK_MINUS;
4239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_PERIOD] = SDLK_PERIOD;
4249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_SLASH] = SDLK_SLASH;
4259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_0] = SDLK_0;
4269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_1] = SDLK_1;
4279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_2] = SDLK_2;
4289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_3] = SDLK_3;
4299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_4] = SDLK_4;
4309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_5] = SDLK_5;
4319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_6] = SDLK_6;
4329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_7] = SDLK_7;
4339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_8] = SDLK_8;
4349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_9] = SDLK_9;
4359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_SEMICOLON] = SDLK_SEMICOLON;
4369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_EQUALS] = SDLK_EQUALS;
4379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_LBRACKET] = SDLK_LEFTBRACKET;
4389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_BACKSLASH] = SDLK_BACKSLASH;
4399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_OEM_102] = SDLK_LESS;
4409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_RBRACKET] = SDLK_RIGHTBRACKET;
4419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_GRAVE] = SDLK_BACKQUOTE;
4429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_BACKTICK] = SDLK_BACKQUOTE;
4439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_A] = SDLK_a;
4449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_B] = SDLK_b;
4459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_C] = SDLK_c;
4469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_D] = SDLK_d;
4479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_E] = SDLK_e;
4489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_F] = SDLK_f;
4499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_G] = SDLK_g;
4509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_H] = SDLK_h;
4519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_I] = SDLK_i;
4529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_J] = SDLK_j;
4539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_K] = SDLK_k;
4549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_L] = SDLK_l;
4559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_M] = SDLK_m;
4569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_N] = SDLK_n;
4579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_O] = SDLK_o;
4589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_P] = SDLK_p;
4599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_Q] = SDLK_q;
4609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_R] = SDLK_r;
4619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_S] = SDLK_s;
4629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_T] = SDLK_t;
4639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_U] = SDLK_u;
4649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_V] = SDLK_v;
4659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_W] = SDLK_w;
4669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_X] = SDLK_x;
4679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_Y] = SDLK_y;
4689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_Z] = SDLK_z;
4699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_DELETE] = SDLK_DELETE;
4709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_NUMPAD0] = SDLK_KP0;
4729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_NUMPAD1] = SDLK_KP1;
4739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_NUMPAD2] = SDLK_KP2;
4749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_NUMPAD3] = SDLK_KP3;
4759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_NUMPAD4] = SDLK_KP4;
4769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_NUMPAD5] = SDLK_KP5;
4779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_NUMPAD6] = SDLK_KP6;
4789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_NUMPAD7] = SDLK_KP7;
4799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_NUMPAD8] = SDLK_KP8;
4809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_NUMPAD9] = SDLK_KP9;
4819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_DECIMAL] = SDLK_KP_PERIOD;
4829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_DIVIDE] = SDLK_KP_DIVIDE;
4839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_MULTIPLY] = SDLK_KP_MULTIPLY;
4849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_SUBTRACT] = SDLK_KP_MINUS;
4859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_ADD] = SDLK_KP_PLUS;
4869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_UP] = SDLK_UP;
4889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_DOWN] = SDLK_DOWN;
4899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_RIGHT] = SDLK_RIGHT;
4909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_LEFT] = SDLK_LEFT;
4919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_INSERT] = SDLK_INSERT;
4929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_HOME] = SDLK_HOME;
4939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_END] = SDLK_END;
4949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_PRIOR] = SDLK_PAGEUP;
4959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_NEXT] = SDLK_PAGEDOWN;
4969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_F1] = SDLK_F1;
4989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_F2] = SDLK_F2;
4999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_F3] = SDLK_F3;
5009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_F4] = SDLK_F4;
5019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_F5] = SDLK_F5;
5029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_F6] = SDLK_F6;
5039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_F7] = SDLK_F7;
5049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_F8] = SDLK_F8;
5059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_F9] = SDLK_F9;
5069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_F10] = SDLK_F10;
5079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_F11] = SDLK_F11;
5089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_F12] = SDLK_F12;
5099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_F13] = SDLK_F13;
5109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_F14] = SDLK_F14;
5119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_F15] = SDLK_F15;
5129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_NUMLOCK] = SDLK_NUMLOCK;
5149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_CAPITAL] = SDLK_CAPSLOCK;
5159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_SCROLL] = SDLK_SCROLLOCK;
5169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_RSHIFT] = SDLK_RSHIFT;
5179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_LSHIFT] = SDLK_LSHIFT;
5189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_RCONTROL] = SDLK_RCTRL;
5199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_LCONTROL] = SDLK_LCTRL;
5209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_RMENU] = SDLK_RALT;
5219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_LMENU] = SDLK_LALT;
5229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_RWIN] = SDLK_RSUPER;
5239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_LWIN] = SDLK_LSUPER;
5249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_HELP] = SDLK_HELP;
5269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef VK_PRINT
5279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_PRINT] = SDLK_PRINT;
5289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
5299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_SNAPSHOT] = SDLK_PRINT;
5309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_CANCEL] = SDLK_BREAK;
5319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VK_keymap[VK_APPS] = SDLK_MENU;
5329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Arrows_keymap[3] = 0x25;
5349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Arrows_keymap[2] = 0x26;
5359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Arrows_keymap[1] = 0x27;
5369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Arrows_keymap[0] = 0x28;
5379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define EXTKEYPAD(keypad) ((scancode & 0x100)?(mvke):(keypad))
5409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_MapVirtualKey(int scancode, int vkey)
5429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef _WIN32_WCE
5449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int	mvke  = MapVirtualKeyEx(scancode & 0xFF, 1, hLayoutUS);
5459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
5469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int	mvke  = MapVirtualKey(scancode & 0xFF, 1);
5479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
5489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch(vkey) {
5509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* These are always correct */
5519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case VK_DIVIDE:
5529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case VK_MULTIPLY:
5539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case VK_SUBTRACT:
5549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case VK_ADD:
5559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case VK_LWIN:
5569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case VK_RWIN:
5579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case VK_APPS:
5589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* These are already handled */
5599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case VK_LCONTROL:
5609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case VK_RCONTROL:
5619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case VK_LSHIFT:
5629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case VK_RSHIFT:
5639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case VK_LMENU:
5649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case VK_RMENU:
5659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case VK_SNAPSHOT:
5669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case VK_PAUSE:
5679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return vkey;
5689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch(mvke) {
5709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Distinguish between keypad and extended keys */
5719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case VK_INSERT: return EXTKEYPAD(VK_NUMPAD0);
5729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case VK_DELETE: return EXTKEYPAD(VK_DECIMAL);
5739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case VK_END:    return EXTKEYPAD(VK_NUMPAD1);
5749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case VK_DOWN:   return EXTKEYPAD(VK_NUMPAD2);
5759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case VK_NEXT:   return EXTKEYPAD(VK_NUMPAD3);
5769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case VK_LEFT:   return EXTKEYPAD(VK_NUMPAD4);
5779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case VK_CLEAR:  return EXTKEYPAD(VK_NUMPAD5);
5789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case VK_RIGHT:  return EXTKEYPAD(VK_NUMPAD6);
5799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case VK_HOME:   return EXTKEYPAD(VK_NUMPAD7);
5809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case VK_UP:     return EXTKEYPAD(VK_NUMPAD8);
5819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case VK_PRIOR:  return EXTKEYPAD(VK_NUMPAD9);
5829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return mvke?mvke:vkey;
5849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_keysym *TranslateKey(WPARAM vkey, UINT scancode, SDL_keysym *keysym, int pressed)
5879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set the keysym information */
5899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	keysym->scancode = (unsigned char) scancode;
5909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	keysym->mod = KMOD_NONE;
5919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	keysym->unicode = 0;
5929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ((vkey == VK_RETURN) && (scancode & 0x100)) {
5949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* No VK_ code for the keypad enter key */
5959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		keysym->sym = SDLK_KP_ENTER;
5969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	else {
5989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		keysym->sym = VK_keymap[SDL_MapVirtualKey(scancode, vkey)];
5999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( pressed && SDL_TranslateUNICODE ) {
6029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef NO_GETKEYBOARDSTATE
6039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Uh oh, better hope the vkey is close enough.. */
6049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if((keysym->sym == vkey) || (vkey > 0x7f))
6059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		keysym->unicode = vkey;
6069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
6079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		BYTE	keystate[256];
6089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Uint16	wchars[2];
6099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		GetKeyboardState(keystate);
6119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Numlock isn't taken into account in ToUnicode,
6129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		 * so we handle it as a special case here */
6139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ((keystate[VK_NUMLOCK] & 1) && vkey >= VK_NUMPAD0 && vkey <= VK_NUMPAD9)
6149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
6159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			keysym->unicode = vkey - VK_NUMPAD0 + '0';
6169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
6179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		else if (SDL_ToUnicode((UINT)vkey, scancode, keystate, wchars, sizeof(wchars)/sizeof(wchars[0]), 0) > 0)
6189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
6199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			keysym->unicode = wchars[0];
6209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
6219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* NO_GETKEYBOARDSTATE */
6229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if 0
6259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
6269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		HKL     hLayoutCurrent = GetKeyboardLayout(0);
6279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		int     sc = scancode & 0xFF;
6289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		printf("SYM:%d, VK:0x%02X, SC:0x%04X, US:(1:0x%02X, 3:0x%02X), "
6309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			"Current:(1:0x%02X, 3:0x%02X)\n",
6319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			keysym->sym, vkey, scancode,
6329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			MapVirtualKeyEx(sc, 1, hLayoutUS),
6339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			MapVirtualKeyEx(sc, 3, hLayoutUS),
6349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			MapVirtualKeyEx(sc, 1, hLayoutCurrent),
6359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			MapVirtualKeyEx(sc, 3, hLayoutCurrent)
6369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		);
6379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
6399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(keysym);
6409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint DIB_CreateWindow(_THIS)
6439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	char *windowid;
6459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_RegisterApp(NULL, 0, 0);
6479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	windowid = SDL_getenv("SDL_WINDOWID");
6499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_windowid = (windowid != NULL);
6509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_windowid ) {
6519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
6529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* wince 2.1 does not have strtol */
6539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		wchar_t *windowid_t = SDL_malloc((SDL_strlen(windowid) + 1) * sizeof(wchar_t));
6549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, windowid, -1, windowid_t, SDL_strlen(windowid) + 1);
6559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_Window = (HWND)wcstol(windowid_t, NULL, 0);
6569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_free(windowid_t);
6579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
6589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_Window = (HWND)((size_t)SDL_strtoull(windowid, NULL, 0));
6599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
6609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( SDL_Window == NULL ) {
6619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetError("Couldn't get user specified window");
6629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return(-1);
6639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
6649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* DJM: we want all event's for the user specified
6669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			window to be handled by SDL.
6679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		 */
6689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		userWindowProc = (WNDPROCTYPE)GetWindowLongPtr(SDL_Window, GWLP_WNDPROC);
6699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SetWindowLongPtr(SDL_Window, GWLP_WNDPROC, (LONG_PTR)WinMessage);
6709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
6719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_Window = CreateWindow(SDL_Appname, SDL_Appname,
6729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        (WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX),
6739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, NULL, NULL, SDL_Instance, NULL);
6749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( SDL_Window == NULL ) {
6759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetError("Couldn't create window");
6769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return(-1);
6779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
6789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		ShowWindow(SDL_Window, SW_HIDE);
6799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* JC 14 Mar 2006
6829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Flush the message loop or this can cause big problems later
6839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Especially if the user decides to use dialog boxes or assert()!
6849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	*/
6859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	WIN_FlushMessageQueue();
6869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
6889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid DIB_DestroyWindow(_THIS)
6919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_windowid ) {
6939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SetWindowLongPtr(SDL_Window, GWLP_WNDPROC, (LONG_PTR)userWindowProc);
6949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
6959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		DestroyWindow(SDL_Window);
6969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_UnregisterApp();
6989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* JC 14 Mar 2006
7009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Flush the message loop or this can cause big problems later
7019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Especially if the user decides to use dialog boxes or assert()!
7029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	*/
7039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	WIN_FlushMessageQueue();
7049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
705