src/video/windib/SDL_dibevents.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 29 Jan 2006 07:57:13 +0000
changeset 1282 217f5d5a49e5
parent 1280 f61f045343d3
child 1288 ea3888b472bf
permissions -rw-r--r--
Date: Sat, 15 Jan 2005 02:01:51 +0000 (UTC)
From: jimrandomh
Subject: [SDL] Re: Modifier keys pressed during initialization stick

I wrote a simple test program which initializes SDL, prints the SDL
version number, then prints any keydown and keyup events with their
modifiers. (Source code below). Compilation was done using Visual
Studio 6, release mode.

My test sequence was:
Start a command prompt. Type the name of the test program.
shift down
enter down (program starts)
Wait for window to appear
enter up
shift up
spacebar down
spacebar up

Under Windows 98, the output was correct:
SDL 1.2.8
left shift down
shift-return down
shift-return up
left shift up
space down
space up

Under Windows 2000 and under Windows XP, the output was:
SDL 1.2.8
shift-space down
shift-space up

Since shift was not held at the time space was pressed, this is
incorrect. Similar results were observed with launching in different
ways (including double-clicking in Windows Explorer), so it does not
depend on the launching terminal.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@769
     3
    Copyright (C) 1997-2004 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@0
     6
    modify it under the terms of the GNU Library General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@0
     8
    version 2 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@0
    13
    Library General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU Library General Public
slouken@0
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
#ifdef SAVE_RCSID
slouken@0
    24
static char rcsid =
slouken@0
    25
 "@(#) $Id$";
slouken@0
    26
#endif
slouken@0
    27
slouken@0
    28
#include <stdlib.h>
slouken@0
    29
#include <stdio.h>
slouken@0
    30
#include <windows.h>
slouken@0
    31
slouken@0
    32
#include "SDL_events.h"
slouken@0
    33
#include "SDL_error.h"
slouken@0
    34
#include "SDL_syswm.h"
slouken@0
    35
#include "SDL_sysevents.h"
slouken@0
    36
#include "SDL_events_c.h"
slouken@0
    37
#include "SDL_lowvideo.h"
slouken@0
    38
#include "SDL_dibvideo.h"
slouken@0
    39
#include "SDL_vkeys.h"
slouken@0
    40
slouken@0
    41
#ifndef WM_APP
slouken@0
    42
#define WM_APP	0x8000
slouken@0
    43
#endif
slouken@0
    44
slouken@0
    45
#ifdef _WIN32_WCE
slouken@0
    46
#define NO_GETKEYBOARDSTATE
slouken@0
    47
#endif
slouken@0
    48
slouken@0
    49
/* The translation table from a Microsoft VK keysym to a SDL keysym */
slouken@0
    50
static SDLKey VK_keymap[SDLK_LAST];
slouken@0
    51
static SDL_keysym *TranslateKey(UINT vkey, UINT scancode, SDL_keysym *keysym, int pressed);
slouken@0
    52
slouken@0
    53
/* Masks for processing the windows KEYDOWN and KEYUP messages */
slouken@0
    54
#define REPEATED_KEYMASK	(1<<30)
slouken@0
    55
#define EXTENDED_KEYMASK	(1<<24)
slouken@0
    56
slouken@145
    57
/* DJM: If the user setup the window for us, we want to save his window proc,
slouken@145
    58
   and give him a chance to handle some messages. */
slouken@145
    59
static WNDPROC userWindowProc = NULL;
slouken@145
    60
icculus@1251
    61
icculus@1251
    62
#ifdef _WIN32_WCE
icculus@1251
    63
icculus@1251
    64
WPARAM rotateKey(WPARAM key,SDL_ScreenOrientation direction) 
icculus@1251
    65
{
icculus@1251
    66
	if (direction != SDL_ORIENTATION_LEFT)
icculus@1251
    67
		return key;
icculus@1251
    68
icculus@1251
    69
	switch (key) {
icculus@1251
    70
		case 0x26: /* up */
icculus@1251
    71
			return 0x27;
icculus@1251
    72
		case 0x27: /* right */
icculus@1251
    73
			return 0x28;
icculus@1251
    74
		case 0x28: /* down */
icculus@1251
    75
			return 0x25;
icculus@1251
    76
		case 0x25: /* left */
icculus@1251
    77
			return 0x26;
icculus@1251
    78
	}
icculus@1251
    79
icculus@1251
    80
	return key;
icculus@1251
    81
}
icculus@1251
    82
icculus@1251
    83
#endif 
icculus@1251
    84
icculus@1251
    85
slouken@0
    86
/* The main Win32 event handler */
slouken@0
    87
LONG
slouken@0
    88
 DIB_HandleMessage(_THIS, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
slouken@0
    89
{
slouken@0
    90
	extern int posted;
slouken@0
    91
slouken@0
    92
	switch (msg) {
slouken@0
    93
		case WM_SYSKEYDOWN:
slouken@0
    94
		case WM_KEYDOWN: {
slouken@0
    95
			SDL_keysym keysym;
slouken@0
    96
icculus@1251
    97
#ifdef _WIN32_WCE
icculus@1251
    98
			// Drop GAPI artefacts
icculus@1251
    99
			if (wParam == 0x84 || wParam == 0x5B)
icculus@1251
   100
				return 0;
icculus@1251
   101
icculus@1251
   102
			// Rotate key if necessary
icculus@1251
   103
			if (this->hidden->orientation != SDL_ORIENTATION_UP)
icculus@1251
   104
				wParam = rotateKey(wParam, this->hidden->orientation);	
icculus@1251
   105
#endif 
slouken@0
   106
			/* Ignore repeated keys */
slouken@0
   107
			if ( lParam&REPEATED_KEYMASK ) {
slouken@0
   108
				return(0);
slouken@0
   109
			}
slouken@0
   110
			switch (wParam) {
slouken@0
   111
				case VK_CONTROL:
slouken@0
   112
					if ( lParam&EXTENDED_KEYMASK )
slouken@0
   113
						wParam = VK_RCONTROL;
slouken@0
   114
					else
slouken@0
   115
						wParam = VK_LCONTROL;
slouken@0
   116
					break;
slouken@0
   117
				case VK_SHIFT:
slouken@0
   118
					/* EXTENDED trick doesn't work here */
slouken@1282
   119
					{
slouken@1282
   120
					Uint8 *state = SDL_GetKeyState(NULL);
slouken@1282
   121
					if (state[SDLK_LSHIFT] == SDL_RELEASED && (GetKeyState(VK_LSHIFT) & 0x8000)) {
slouken@558
   122
						wParam = VK_LSHIFT;
slouken@1282
   123
					} else if (state[SDLK_RSHIFT] == SDL_RELEASED && (GetKeyState(VK_RSHIFT) & 0x8000)) {
slouken@558
   124
						wParam = VK_RSHIFT;
slouken@558
   125
					} else {
slouken@1282
   126
						/* Probably a key repeat */
slouken@1282
   127
						return(0);
slouken@1282
   128
					}
slouken@558
   129
					}
slouken@0
   130
					break;
slouken@0
   131
				case VK_MENU:
slouken@0
   132
					if ( lParam&EXTENDED_KEYMASK )
slouken@0
   133
						wParam = VK_RMENU;
slouken@0
   134
					else
slouken@0
   135
						wParam = VK_LMENU;
slouken@0
   136
					break;
slouken@0
   137
			}
slouken@112
   138
#ifdef NO_GETKEYBOARDSTATE
slouken@112
   139
			/* this is the workaround for the missing ToAscii() and ToUnicode() in CE (not necessary at KEYUP!) */
slouken@112
   140
			if ( SDL_TranslateUNICODE ) {
slouken@140
   141
				MSG m;
slouken@112
   142
slouken@140
   143
				m.hwnd = hwnd;
slouken@140
   144
				m.message = msg;
slouken@140
   145
				m.wParam = wParam;
slouken@140
   146
				m.lParam = lParam;
slouken@140
   147
				m.time = 0;
slouken@140
   148
				if ( TranslateMessage(&m) && PeekMessage(&m, hwnd, 0, WM_USER, PM_NOREMOVE) && (m.message == WM_CHAR) ) {
slouken@112
   149
					GetMessage(&m, hwnd, 0, WM_USER);
slouken@112
   150
			    		wParam = m.wParam;
slouken@112
   151
				}
slouken@112
   152
			}
slouken@112
   153
#endif /* NO_GETKEYBOARDSTATE */
slouken@0
   154
			posted = SDL_PrivateKeyboard(SDL_PRESSED,
slouken@0
   155
				TranslateKey(wParam,HIWORD(lParam),&keysym,1));
slouken@0
   156
		}
slouken@0
   157
		return(0);
slouken@0
   158
slouken@0
   159
		case WM_SYSKEYUP:
slouken@0
   160
		case WM_KEYUP: {
slouken@0
   161
			SDL_keysym keysym;
slouken@0
   162
icculus@1251
   163
#ifdef _WIN32_WCE
icculus@1251
   164
			// Drop GAPI artefacts
icculus@1251
   165
			if (wParam == 0x84 || wParam == 0x5B)
icculus@1251
   166
				return 0;
icculus@1251
   167
icculus@1251
   168
			// Rotate key if necessary
icculus@1251
   169
			if (this->hidden->orientation != SDL_ORIENTATION_UP)
icculus@1251
   170
				wParam = rotateKey(wParam, this->hidden->orientation);	
icculus@1251
   171
#endif
icculus@1251
   172
slouken@0
   173
			switch (wParam) {
slouken@0
   174
				case VK_CONTROL:
slouken@0
   175
					if ( lParam&EXTENDED_KEYMASK )
slouken@0
   176
						wParam = VK_RCONTROL;
slouken@0
   177
					else
slouken@0
   178
						wParam = VK_LCONTROL;
slouken@0
   179
					break;
slouken@0
   180
				case VK_SHIFT:
slouken@0
   181
					/* EXTENDED trick doesn't work here */
slouken@1282
   182
					{
slouken@1282
   183
					Uint8 *state = SDL_GetKeyState(NULL);
slouken@1282
   184
					if (state[SDLK_LSHIFT] == SDL_PRESSED && !(GetKeyState(VK_LSHIFT) & 0x8000)) {
slouken@572
   185
						wParam = VK_LSHIFT;
slouken@1282
   186
					} else if (state[SDLK_RSHIFT] == SDL_PRESSED && !(GetKeyState(VK_RSHIFT) & 0x8000)) {
slouken@572
   187
						wParam = VK_RSHIFT;
slouken@572
   188
					} else {
slouken@1282
   189
						/* Probably a key repeat */
slouken@1282
   190
						return(0);
slouken@1282
   191
					}
slouken@572
   192
					}
slouken@0
   193
					break;
slouken@0
   194
				case VK_MENU:
slouken@0
   195
					if ( lParam&EXTENDED_KEYMASK )
slouken@0
   196
						wParam = VK_RMENU;
slouken@0
   197
					else
slouken@0
   198
						wParam = VK_LMENU;
slouken@0
   199
					break;
slouken@0
   200
			}
slouken@0
   201
			posted = SDL_PrivateKeyboard(SDL_RELEASED,
slouken@0
   202
				TranslateKey(wParam,HIWORD(lParam),&keysym,0));
slouken@0
   203
		}
slouken@0
   204
		return(0);
slouken@0
   205
icculus@726
   206
#if defined(SC_SCREENSAVE) && defined(SC_MONITORPOWER)
icculus@726
   207
		case WM_SYSCOMMAND: {
icculus@726
   208
			if ((wParam&0xFFF0)==SC_SCREENSAVE ||
icculus@726
   209
				(wParam&0xFFF0)==SC_MONITORPOWER)
icculus@726
   210
					return(0);
icculus@726
   211
		}
icculus@726
   212
		/* Fall through to default processing */
icculus@726
   213
#endif /* SC_SCREENSAVE && SC_MONITORPOWER */
icculus@726
   214
slouken@0
   215
		default: {
slouken@0
   216
			/* Only post the event if we're watching for it */
slouken@0
   217
			if ( SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE ) {
slouken@0
   218
			        SDL_SysWMmsg wmmsg;
slouken@0
   219
slouken@0
   220
				SDL_VERSION(&wmmsg.version);
slouken@0
   221
				wmmsg.hwnd = hwnd;
slouken@0
   222
				wmmsg.msg = msg;
slouken@0
   223
				wmmsg.wParam = wParam;
slouken@0
   224
				wmmsg.lParam = lParam;
slouken@0
   225
				posted = SDL_PrivateSysWMEvent(&wmmsg);
slouken@145
   226
slouken@145
   227
			/* DJM: If the user isn't watching for private
slouken@145
   228
				messages in her SDL event loop, then pass it
slouken@145
   229
				along to any win32 specific window proc.
slouken@145
   230
			 */
slouken@145
   231
			} else if (userWindowProc) {
slouken@721
   232
				return CallWindowProc(userWindowProc, hwnd, msg, wParam, lParam);
slouken@0
   233
			}
slouken@0
   234
		}
slouken@0
   235
		break;
slouken@0
   236
	}
slouken@0
   237
	return(DefWindowProc(hwnd, msg, wParam, lParam));
slouken@0
   238
}
slouken@0
   239
slouken@0
   240
void DIB_PumpEvents(_THIS)
slouken@0
   241
{
slouken@0
   242
	MSG msg;
slouken@0
   243
slouken@523
   244
	while ( PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) ) {
slouken@523
   245
		if ( GetMessage(&msg, NULL, 0, 0) > 0 ) {
slouken@0
   246
			DispatchMessage(&msg);
slouken@0
   247
		}
slouken@0
   248
	}
slouken@0
   249
}
slouken@0
   250
slouken@0
   251
void DIB_InitOSKeymap(_THIS)
slouken@0
   252
{
slouken@0
   253
	int i;
slouken@0
   254
slouken@0
   255
	/* Map the VK keysyms */
slouken@0
   256
	for ( i=0; i<SDL_TABLESIZE(VK_keymap); ++i )
slouken@0
   257
		VK_keymap[i] = SDLK_UNKNOWN;
slouken@0
   258
slouken@0
   259
	VK_keymap[VK_BACK] = SDLK_BACKSPACE;
slouken@0
   260
	VK_keymap[VK_TAB] = SDLK_TAB;
slouken@0
   261
	VK_keymap[VK_CLEAR] = SDLK_CLEAR;
slouken@0
   262
	VK_keymap[VK_RETURN] = SDLK_RETURN;
slouken@0
   263
	VK_keymap[VK_PAUSE] = SDLK_PAUSE;
slouken@0
   264
	VK_keymap[VK_ESCAPE] = SDLK_ESCAPE;
slouken@0
   265
	VK_keymap[VK_SPACE] = SDLK_SPACE;
slouken@0
   266
	VK_keymap[VK_APOSTROPHE] = SDLK_QUOTE;
slouken@0
   267
	VK_keymap[VK_COMMA] = SDLK_COMMA;
slouken@0
   268
	VK_keymap[VK_MINUS] = SDLK_MINUS;
slouken@0
   269
	VK_keymap[VK_PERIOD] = SDLK_PERIOD;
slouken@0
   270
	VK_keymap[VK_SLASH] = SDLK_SLASH;
slouken@0
   271
	VK_keymap[VK_0] = SDLK_0;
slouken@0
   272
	VK_keymap[VK_1] = SDLK_1;
slouken@0
   273
	VK_keymap[VK_2] = SDLK_2;
slouken@0
   274
	VK_keymap[VK_3] = SDLK_3;
slouken@0
   275
	VK_keymap[VK_4] = SDLK_4;
slouken@0
   276
	VK_keymap[VK_5] = SDLK_5;
slouken@0
   277
	VK_keymap[VK_6] = SDLK_6;
slouken@0
   278
	VK_keymap[VK_7] = SDLK_7;
slouken@0
   279
	VK_keymap[VK_8] = SDLK_8;
slouken@0
   280
	VK_keymap[VK_9] = SDLK_9;
slouken@0
   281
	VK_keymap[VK_SEMICOLON] = SDLK_SEMICOLON;
slouken@0
   282
	VK_keymap[VK_EQUALS] = SDLK_EQUALS;
slouken@0
   283
	VK_keymap[VK_LBRACKET] = SDLK_LEFTBRACKET;
slouken@0
   284
	VK_keymap[VK_BACKSLASH] = SDLK_BACKSLASH;
slouken@1253
   285
	VK_keymap[VK_OEM_102] = SDLK_LESS;
slouken@0
   286
	VK_keymap[VK_RBRACKET] = SDLK_RIGHTBRACKET;
slouken@0
   287
	VK_keymap[VK_GRAVE] = SDLK_BACKQUOTE;
slouken@327
   288
	VK_keymap[VK_BACKTICK] = SDLK_BACKQUOTE;
slouken@0
   289
	VK_keymap[VK_A] = SDLK_a;
slouken@0
   290
	VK_keymap[VK_B] = SDLK_b;
slouken@0
   291
	VK_keymap[VK_C] = SDLK_c;
slouken@0
   292
	VK_keymap[VK_D] = SDLK_d;
slouken@0
   293
	VK_keymap[VK_E] = SDLK_e;
slouken@0
   294
	VK_keymap[VK_F] = SDLK_f;
slouken@0
   295
	VK_keymap[VK_G] = SDLK_g;
slouken@0
   296
	VK_keymap[VK_H] = SDLK_h;
slouken@0
   297
	VK_keymap[VK_I] = SDLK_i;
slouken@0
   298
	VK_keymap[VK_J] = SDLK_j;
slouken@0
   299
	VK_keymap[VK_K] = SDLK_k;
slouken@0
   300
	VK_keymap[VK_L] = SDLK_l;
slouken@0
   301
	VK_keymap[VK_M] = SDLK_m;
slouken@0
   302
	VK_keymap[VK_N] = SDLK_n;
slouken@0
   303
	VK_keymap[VK_O] = SDLK_o;
slouken@0
   304
	VK_keymap[VK_P] = SDLK_p;
slouken@0
   305
	VK_keymap[VK_Q] = SDLK_q;
slouken@0
   306
	VK_keymap[VK_R] = SDLK_r;
slouken@0
   307
	VK_keymap[VK_S] = SDLK_s;
slouken@0
   308
	VK_keymap[VK_T] = SDLK_t;
slouken@0
   309
	VK_keymap[VK_U] = SDLK_u;
slouken@0
   310
	VK_keymap[VK_V] = SDLK_v;
slouken@0
   311
	VK_keymap[VK_W] = SDLK_w;
slouken@0
   312
	VK_keymap[VK_X] = SDLK_x;
slouken@0
   313
	VK_keymap[VK_Y] = SDLK_y;
slouken@0
   314
	VK_keymap[VK_Z] = SDLK_z;
slouken@0
   315
	VK_keymap[VK_DELETE] = SDLK_DELETE;
slouken@0
   316
slouken@0
   317
	VK_keymap[VK_NUMPAD0] = SDLK_KP0;
slouken@0
   318
	VK_keymap[VK_NUMPAD1] = SDLK_KP1;
slouken@0
   319
	VK_keymap[VK_NUMPAD2] = SDLK_KP2;
slouken@0
   320
	VK_keymap[VK_NUMPAD3] = SDLK_KP3;
slouken@0
   321
	VK_keymap[VK_NUMPAD4] = SDLK_KP4;
slouken@0
   322
	VK_keymap[VK_NUMPAD5] = SDLK_KP5;
slouken@0
   323
	VK_keymap[VK_NUMPAD6] = SDLK_KP6;
slouken@0
   324
	VK_keymap[VK_NUMPAD7] = SDLK_KP7;
slouken@0
   325
	VK_keymap[VK_NUMPAD8] = SDLK_KP8;
slouken@0
   326
	VK_keymap[VK_NUMPAD9] = SDLK_KP9;
slouken@0
   327
	VK_keymap[VK_DECIMAL] = SDLK_KP_PERIOD;
slouken@0
   328
	VK_keymap[VK_DIVIDE] = SDLK_KP_DIVIDE;
slouken@0
   329
	VK_keymap[VK_MULTIPLY] = SDLK_KP_MULTIPLY;
slouken@0
   330
	VK_keymap[VK_SUBTRACT] = SDLK_KP_MINUS;
slouken@0
   331
	VK_keymap[VK_ADD] = SDLK_KP_PLUS;
slouken@0
   332
slouken@0
   333
	VK_keymap[VK_UP] = SDLK_UP;
slouken@0
   334
	VK_keymap[VK_DOWN] = SDLK_DOWN;
slouken@0
   335
	VK_keymap[VK_RIGHT] = SDLK_RIGHT;
slouken@0
   336
	VK_keymap[VK_LEFT] = SDLK_LEFT;
slouken@0
   337
	VK_keymap[VK_INSERT] = SDLK_INSERT;
slouken@0
   338
	VK_keymap[VK_HOME] = SDLK_HOME;
slouken@0
   339
	VK_keymap[VK_END] = SDLK_END;
slouken@0
   340
	VK_keymap[VK_PRIOR] = SDLK_PAGEUP;
slouken@0
   341
	VK_keymap[VK_NEXT] = SDLK_PAGEDOWN;
slouken@0
   342
slouken@0
   343
	VK_keymap[VK_F1] = SDLK_F1;
slouken@0
   344
	VK_keymap[VK_F2] = SDLK_F2;
slouken@0
   345
	VK_keymap[VK_F3] = SDLK_F3;
slouken@0
   346
	VK_keymap[VK_F4] = SDLK_F4;
slouken@0
   347
	VK_keymap[VK_F5] = SDLK_F5;
slouken@0
   348
	VK_keymap[VK_F6] = SDLK_F6;
slouken@0
   349
	VK_keymap[VK_F7] = SDLK_F7;
slouken@0
   350
	VK_keymap[VK_F8] = SDLK_F8;
slouken@0
   351
	VK_keymap[VK_F9] = SDLK_F9;
slouken@0
   352
	VK_keymap[VK_F10] = SDLK_F10;
slouken@0
   353
	VK_keymap[VK_F11] = SDLK_F11;
slouken@0
   354
	VK_keymap[VK_F12] = SDLK_F12;
slouken@0
   355
	VK_keymap[VK_F13] = SDLK_F13;
slouken@0
   356
	VK_keymap[VK_F14] = SDLK_F14;
slouken@0
   357
	VK_keymap[VK_F15] = SDLK_F15;
slouken@0
   358
slouken@0
   359
	VK_keymap[VK_NUMLOCK] = SDLK_NUMLOCK;
slouken@0
   360
	VK_keymap[VK_CAPITAL] = SDLK_CAPSLOCK;
slouken@0
   361
	VK_keymap[VK_SCROLL] = SDLK_SCROLLOCK;
slouken@0
   362
	VK_keymap[VK_RSHIFT] = SDLK_RSHIFT;
slouken@0
   363
	VK_keymap[VK_LSHIFT] = SDLK_LSHIFT;
slouken@0
   364
	VK_keymap[VK_RCONTROL] = SDLK_RCTRL;
slouken@0
   365
	VK_keymap[VK_LCONTROL] = SDLK_LCTRL;
slouken@0
   366
	VK_keymap[VK_RMENU] = SDLK_RALT;
slouken@0
   367
	VK_keymap[VK_LMENU] = SDLK_LALT;
slouken@0
   368
	VK_keymap[VK_RWIN] = SDLK_RSUPER;
slouken@0
   369
	VK_keymap[VK_LWIN] = SDLK_LSUPER;
slouken@0
   370
slouken@0
   371
	VK_keymap[VK_HELP] = SDLK_HELP;
slouken@0
   372
#ifdef VK_PRINT
slouken@0
   373
	VK_keymap[VK_PRINT] = SDLK_PRINT;
slouken@0
   374
#endif
slouken@0
   375
	VK_keymap[VK_SNAPSHOT] = SDLK_PRINT;
slouken@0
   376
	VK_keymap[VK_CANCEL] = SDLK_BREAK;
slouken@0
   377
	VK_keymap[VK_APPS] = SDLK_MENU;
slouken@0
   378
}
slouken@0
   379
slouken@0
   380
static SDL_keysym *TranslateKey(UINT vkey, UINT scancode, SDL_keysym *keysym, int pressed)
slouken@0
   381
{
slouken@0
   382
	/* Set the keysym information */
slouken@0
   383
	keysym->scancode = (unsigned char) scancode;
slouken@0
   384
	keysym->sym = VK_keymap[vkey];
slouken@0
   385
	keysym->mod = KMOD_NONE;
slouken@0
   386
	keysym->unicode = 0;
slouken@1253
   387
	if ( pressed && SDL_TranslateUNICODE ) {
slouken@0
   388
#ifdef NO_GETKEYBOARDSTATE
slouken@0
   389
		/* Uh oh, better hope the vkey is close enough.. */
slouken@0
   390
		keysym->unicode = vkey;
slouken@0
   391
#else
slouken@1253
   392
		BYTE	keystate[256];
slouken@1253
   393
		Uint16	wchars[2];
slouken@0
   394
slouken@0
   395
		GetKeyboardState(keystate);
slouken@1253
   396
		if (SDL_ToUnicode(vkey, scancode, keystate, wchars, sizeof(wchars)/sizeof(wchars[0]), 0) == 1)
slouken@1253
   397
		{
slouken@1253
   398
			keysym->unicode = wchars[0];
slouken@0
   399
		}
slouken@0
   400
#endif /* NO_GETKEYBOARDSTATE */
slouken@0
   401
	}
slouken@0
   402
	return(keysym);
slouken@0
   403
}
slouken@0
   404
slouken@0
   405
int DIB_CreateWindow(_THIS)
slouken@0
   406
{
slouken@1280
   407
	char *windowid = getenv("SDL_WINDOWID");
icculus@1152
   408
slouken@453
   409
#ifndef CS_BYTEALIGNCLIENT
slouken@453
   410
#define CS_BYTEALIGNCLIENT	0
slouken@453
   411
#endif
slouken@0
   412
	SDL_RegisterApp("SDL_app", CS_BYTEALIGNCLIENT, 0);
slouken@1280
   413
slouken@1280
   414
	SDL_windowid = (windowid != NULL);
slouken@0
   415
	if ( SDL_windowid ) {
icculus@1152
   416
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
slouken@1280
   417
		/* wince 2.1 does not have strtol */
slouken@1280
   418
		wchar_t *windowid_t = malloc((strlen(windowid) + 1) * sizeof(wchar_t));
slouken@1280
   419
		MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, windowid, -1, windowid_t, strlen(windowid) + 1);
slouken@1280
   420
		SDL_Window = (HWND)wcstol(windowid_t, NULL, 0);
slouken@1280
   421
		free(windowid_t);
icculus@1152
   422
#else
slouken@1280
   423
		SDL_Window = (HWND)strtol(windowid, NULL, 0);
icculus@1152
   424
#endif
slouken@974
   425
		if ( SDL_Window == NULL ) {
slouken@974
   426
			SDL_SetError("Couldn't get user specified window");
slouken@974
   427
			return(-1);
slouken@974
   428
		}
slouken@974
   429
slouken@974
   430
		/* DJM: we want all event's for the user specified
slouken@974
   431
			window to be handled by SDL.
slouken@974
   432
		 */
slouken@974
   433
		userWindowProc = (WNDPROC)GetWindowLong(SDL_Window, GWL_WNDPROC);
slouken@974
   434
		SetWindowLong(SDL_Window, GWL_WNDPROC, (LONG)WinMessage);
slouken@0
   435
	} else {
slouken@0
   436
		SDL_Window = CreateWindow(SDL_Appname, SDL_Appname,
slouken@0
   437
                        (WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX),
slouken@833
   438
                        CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, NULL, NULL, SDL_Instance, NULL);
slouken@0
   439
		if ( SDL_Window == NULL ) {
slouken@0
   440
			SDL_SetError("Couldn't create window");
slouken@0
   441
			return(-1);
slouken@0
   442
		}
slouken@0
   443
		ShowWindow(SDL_Window, SW_HIDE);
slouken@0
   444
	}
slouken@0
   445
	return(0);
slouken@0
   446
}
slouken@0
   447
slouken@0
   448
void DIB_DestroyWindow(_THIS)
slouken@0
   449
{
slouken@974
   450
	if ( SDL_windowid ) {
slouken@974
   451
		SetWindowLong(SDL_Window, GWL_WNDPROC, (LONG)userWindowProc);
slouken@974
   452
	} else {
slouken@0
   453
		DestroyWindow(SDL_Window);
slouken@0
   454
	}
slouken@0
   455
}