src/video/windib/SDL_dibevents.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 09 Aug 2001 05:51:34 +0000
changeset 140 3c35d8f160bd
parent 112 9ef74357a5fb
child 145 29a638dc26db
permissions -rw-r--r--
Fixed compiling on Windows CE
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@0
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001  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@0
    20
    slouken@devolution.com
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@0
    57
/* The main Win32 event handler */
slouken@0
    58
LONG
slouken@0
    59
 DIB_HandleMessage(_THIS, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
slouken@0
    60
{
slouken@0
    61
	extern int posted;
slouken@0
    62
slouken@0
    63
	switch (msg) {
slouken@0
    64
		case WM_SYSKEYDOWN:
slouken@0
    65
		case WM_KEYDOWN: {
slouken@0
    66
			SDL_keysym keysym;
slouken@0
    67
slouken@0
    68
			/* Ignore repeated keys */
slouken@0
    69
			if ( lParam&REPEATED_KEYMASK ) {
slouken@0
    70
				return(0);
slouken@0
    71
			}
slouken@0
    72
			switch (wParam) {
slouken@0
    73
				case VK_CONTROL:
slouken@0
    74
					if ( lParam&EXTENDED_KEYMASK )
slouken@0
    75
						wParam = VK_RCONTROL;
slouken@0
    76
					else
slouken@0
    77
						wParam = VK_LCONTROL;
slouken@0
    78
					break;
slouken@0
    79
				case VK_SHIFT:
slouken@0
    80
					/* EXTENDED trick doesn't work here */
slouken@0
    81
					wParam = VK_LSHIFT;
slouken@0
    82
					break;
slouken@0
    83
				case VK_MENU:
slouken@0
    84
					if ( lParam&EXTENDED_KEYMASK )
slouken@0
    85
						wParam = VK_RMENU;
slouken@0
    86
					else
slouken@0
    87
						wParam = VK_LMENU;
slouken@0
    88
					break;
slouken@0
    89
			}
slouken@112
    90
#ifdef NO_GETKEYBOARDSTATE
slouken@112
    91
			/* this is the workaround for the missing ToAscii() and ToUnicode() in CE (not necessary at KEYUP!) */
slouken@112
    92
			if ( SDL_TranslateUNICODE ) {
slouken@140
    93
				MSG m;
slouken@112
    94
slouken@140
    95
				m.hwnd = hwnd;
slouken@140
    96
				m.message = msg;
slouken@140
    97
				m.wParam = wParam;
slouken@140
    98
				m.lParam = lParam;
slouken@140
    99
				m.time = 0;
slouken@140
   100
				if ( TranslateMessage(&m) && PeekMessage(&m, hwnd, 0, WM_USER, PM_NOREMOVE) && (m.message == WM_CHAR) ) {
slouken@112
   101
					GetMessage(&m, hwnd, 0, WM_USER);
slouken@112
   102
			    		wParam = m.wParam;
slouken@112
   103
				} else {
slouken@112
   104
					wParam = 0;
slouken@112
   105
				}
slouken@112
   106
			} else {
slouken@112
   107
				wParam = 0;
slouken@112
   108
			}
slouken@112
   109
#endif /* NO_GETKEYBOARDSTATE */
slouken@0
   110
			posted = SDL_PrivateKeyboard(SDL_PRESSED,
slouken@0
   111
				TranslateKey(wParam,HIWORD(lParam),&keysym,1));
slouken@0
   112
		}
slouken@0
   113
		return(0);
slouken@0
   114
slouken@0
   115
		case WM_SYSKEYUP:
slouken@0
   116
		case WM_KEYUP: {
slouken@0
   117
			SDL_keysym keysym;
slouken@0
   118
slouken@0
   119
			switch (wParam) {
slouken@0
   120
				case VK_CONTROL:
slouken@0
   121
					if ( lParam&EXTENDED_KEYMASK )
slouken@0
   122
						wParam = VK_RCONTROL;
slouken@0
   123
					else
slouken@0
   124
						wParam = VK_LCONTROL;
slouken@0
   125
					break;
slouken@0
   126
				case VK_SHIFT:
slouken@0
   127
					/* EXTENDED trick doesn't work here */
slouken@0
   128
					wParam = VK_LSHIFT;
slouken@0
   129
					break;
slouken@0
   130
				case VK_MENU:
slouken@0
   131
					if ( lParam&EXTENDED_KEYMASK )
slouken@0
   132
						wParam = VK_RMENU;
slouken@0
   133
					else
slouken@0
   134
						wParam = VK_LMENU;
slouken@0
   135
					break;
slouken@0
   136
			}
slouken@0
   137
			posted = SDL_PrivateKeyboard(SDL_RELEASED,
slouken@0
   138
				TranslateKey(wParam,HIWORD(lParam),&keysym,0));
slouken@0
   139
		}
slouken@0
   140
		return(0);
slouken@0
   141
slouken@0
   142
		default: {
slouken@0
   143
			/* Only post the event if we're watching for it */
slouken@0
   144
			if ( SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE ) {
slouken@0
   145
			        SDL_SysWMmsg wmmsg;
slouken@0
   146
slouken@0
   147
				SDL_VERSION(&wmmsg.version);
slouken@0
   148
				wmmsg.hwnd = hwnd;
slouken@0
   149
				wmmsg.msg = msg;
slouken@0
   150
				wmmsg.wParam = wParam;
slouken@0
   151
				wmmsg.lParam = lParam;
slouken@0
   152
				posted = SDL_PrivateSysWMEvent(&wmmsg);
slouken@0
   153
			}
slouken@0
   154
		}
slouken@0
   155
		break;
slouken@0
   156
	}
slouken@0
   157
	return(DefWindowProc(hwnd, msg, wParam, lParam));
slouken@0
   158
}
slouken@0
   159
slouken@0
   160
void DIB_PumpEvents(_THIS)
slouken@0
   161
{
slouken@0
   162
	MSG msg;
slouken@0
   163
slouken@0
   164
	while ( PeekMessage(&msg, NULL, 0, (WM_APP-1), PM_NOREMOVE) ) {
slouken@0
   165
		if ( GetMessage(&msg, NULL, 0, (WM_APP-1)) > 0 ) {
slouken@0
   166
			DispatchMessage(&msg);
slouken@0
   167
		}
slouken@0
   168
	}
slouken@0
   169
}
slouken@0
   170
slouken@0
   171
void DIB_InitOSKeymap(_THIS)
slouken@0
   172
{
slouken@0
   173
	int i;
slouken@0
   174
slouken@0
   175
	/* Map the VK keysyms */
slouken@0
   176
	for ( i=0; i<SDL_TABLESIZE(VK_keymap); ++i )
slouken@0
   177
		VK_keymap[i] = SDLK_UNKNOWN;
slouken@0
   178
slouken@0
   179
	VK_keymap[VK_BACK] = SDLK_BACKSPACE;
slouken@0
   180
	VK_keymap[VK_TAB] = SDLK_TAB;
slouken@0
   181
	VK_keymap[VK_CLEAR] = SDLK_CLEAR;
slouken@0
   182
	VK_keymap[VK_RETURN] = SDLK_RETURN;
slouken@0
   183
	VK_keymap[VK_PAUSE] = SDLK_PAUSE;
slouken@0
   184
	VK_keymap[VK_ESCAPE] = SDLK_ESCAPE;
slouken@0
   185
	VK_keymap[VK_SPACE] = SDLK_SPACE;
slouken@0
   186
	VK_keymap[VK_APOSTROPHE] = SDLK_QUOTE;
slouken@0
   187
	VK_keymap[VK_COMMA] = SDLK_COMMA;
slouken@0
   188
	VK_keymap[VK_MINUS] = SDLK_MINUS;
slouken@0
   189
	VK_keymap[VK_PERIOD] = SDLK_PERIOD;
slouken@0
   190
	VK_keymap[VK_SLASH] = SDLK_SLASH;
slouken@0
   191
	VK_keymap[VK_0] = SDLK_0;
slouken@0
   192
	VK_keymap[VK_1] = SDLK_1;
slouken@0
   193
	VK_keymap[VK_2] = SDLK_2;
slouken@0
   194
	VK_keymap[VK_3] = SDLK_3;
slouken@0
   195
	VK_keymap[VK_4] = SDLK_4;
slouken@0
   196
	VK_keymap[VK_5] = SDLK_5;
slouken@0
   197
	VK_keymap[VK_6] = SDLK_6;
slouken@0
   198
	VK_keymap[VK_7] = SDLK_7;
slouken@0
   199
	VK_keymap[VK_8] = SDLK_8;
slouken@0
   200
	VK_keymap[VK_9] = SDLK_9;
slouken@0
   201
	VK_keymap[VK_SEMICOLON] = SDLK_SEMICOLON;
slouken@0
   202
	VK_keymap[VK_EQUALS] = SDLK_EQUALS;
slouken@0
   203
	VK_keymap[VK_LBRACKET] = SDLK_LEFTBRACKET;
slouken@0
   204
	VK_keymap[VK_BACKSLASH] = SDLK_BACKSLASH;
slouken@0
   205
	VK_keymap[VK_RBRACKET] = SDLK_RIGHTBRACKET;
slouken@0
   206
	VK_keymap[VK_GRAVE] = SDLK_BACKQUOTE;
slouken@0
   207
	VK_keymap[VK_A] = SDLK_a;
slouken@0
   208
	VK_keymap[VK_B] = SDLK_b;
slouken@0
   209
	VK_keymap[VK_C] = SDLK_c;
slouken@0
   210
	VK_keymap[VK_D] = SDLK_d;
slouken@0
   211
	VK_keymap[VK_E] = SDLK_e;
slouken@0
   212
	VK_keymap[VK_F] = SDLK_f;
slouken@0
   213
	VK_keymap[VK_G] = SDLK_g;
slouken@0
   214
	VK_keymap[VK_H] = SDLK_h;
slouken@0
   215
	VK_keymap[VK_I] = SDLK_i;
slouken@0
   216
	VK_keymap[VK_J] = SDLK_j;
slouken@0
   217
	VK_keymap[VK_K] = SDLK_k;
slouken@0
   218
	VK_keymap[VK_L] = SDLK_l;
slouken@0
   219
	VK_keymap[VK_M] = SDLK_m;
slouken@0
   220
	VK_keymap[VK_N] = SDLK_n;
slouken@0
   221
	VK_keymap[VK_O] = SDLK_o;
slouken@0
   222
	VK_keymap[VK_P] = SDLK_p;
slouken@0
   223
	VK_keymap[VK_Q] = SDLK_q;
slouken@0
   224
	VK_keymap[VK_R] = SDLK_r;
slouken@0
   225
	VK_keymap[VK_S] = SDLK_s;
slouken@0
   226
	VK_keymap[VK_T] = SDLK_t;
slouken@0
   227
	VK_keymap[VK_U] = SDLK_u;
slouken@0
   228
	VK_keymap[VK_V] = SDLK_v;
slouken@0
   229
	VK_keymap[VK_W] = SDLK_w;
slouken@0
   230
	VK_keymap[VK_X] = SDLK_x;
slouken@0
   231
	VK_keymap[VK_Y] = SDLK_y;
slouken@0
   232
	VK_keymap[VK_Z] = SDLK_z;
slouken@0
   233
	VK_keymap[VK_DELETE] = SDLK_DELETE;
slouken@0
   234
slouken@0
   235
	VK_keymap[VK_NUMPAD0] = SDLK_KP0;
slouken@0
   236
	VK_keymap[VK_NUMPAD1] = SDLK_KP1;
slouken@0
   237
	VK_keymap[VK_NUMPAD2] = SDLK_KP2;
slouken@0
   238
	VK_keymap[VK_NUMPAD3] = SDLK_KP3;
slouken@0
   239
	VK_keymap[VK_NUMPAD4] = SDLK_KP4;
slouken@0
   240
	VK_keymap[VK_NUMPAD5] = SDLK_KP5;
slouken@0
   241
	VK_keymap[VK_NUMPAD6] = SDLK_KP6;
slouken@0
   242
	VK_keymap[VK_NUMPAD7] = SDLK_KP7;
slouken@0
   243
	VK_keymap[VK_NUMPAD8] = SDLK_KP8;
slouken@0
   244
	VK_keymap[VK_NUMPAD9] = SDLK_KP9;
slouken@0
   245
	VK_keymap[VK_DECIMAL] = SDLK_KP_PERIOD;
slouken@0
   246
	VK_keymap[VK_DIVIDE] = SDLK_KP_DIVIDE;
slouken@0
   247
	VK_keymap[VK_MULTIPLY] = SDLK_KP_MULTIPLY;
slouken@0
   248
	VK_keymap[VK_SUBTRACT] = SDLK_KP_MINUS;
slouken@0
   249
	VK_keymap[VK_ADD] = SDLK_KP_PLUS;
slouken@0
   250
slouken@0
   251
	VK_keymap[VK_UP] = SDLK_UP;
slouken@0
   252
	VK_keymap[VK_DOWN] = SDLK_DOWN;
slouken@0
   253
	VK_keymap[VK_RIGHT] = SDLK_RIGHT;
slouken@0
   254
	VK_keymap[VK_LEFT] = SDLK_LEFT;
slouken@0
   255
	VK_keymap[VK_INSERT] = SDLK_INSERT;
slouken@0
   256
	VK_keymap[VK_HOME] = SDLK_HOME;
slouken@0
   257
	VK_keymap[VK_END] = SDLK_END;
slouken@0
   258
	VK_keymap[VK_PRIOR] = SDLK_PAGEUP;
slouken@0
   259
	VK_keymap[VK_NEXT] = SDLK_PAGEDOWN;
slouken@0
   260
slouken@0
   261
	VK_keymap[VK_F1] = SDLK_F1;
slouken@0
   262
	VK_keymap[VK_F2] = SDLK_F2;
slouken@0
   263
	VK_keymap[VK_F3] = SDLK_F3;
slouken@0
   264
	VK_keymap[VK_F4] = SDLK_F4;
slouken@0
   265
	VK_keymap[VK_F5] = SDLK_F5;
slouken@0
   266
	VK_keymap[VK_F6] = SDLK_F6;
slouken@0
   267
	VK_keymap[VK_F7] = SDLK_F7;
slouken@0
   268
	VK_keymap[VK_F8] = SDLK_F8;
slouken@0
   269
	VK_keymap[VK_F9] = SDLK_F9;
slouken@0
   270
	VK_keymap[VK_F10] = SDLK_F10;
slouken@0
   271
	VK_keymap[VK_F11] = SDLK_F11;
slouken@0
   272
	VK_keymap[VK_F12] = SDLK_F12;
slouken@0
   273
	VK_keymap[VK_F13] = SDLK_F13;
slouken@0
   274
	VK_keymap[VK_F14] = SDLK_F14;
slouken@0
   275
	VK_keymap[VK_F15] = SDLK_F15;
slouken@0
   276
slouken@0
   277
	VK_keymap[VK_NUMLOCK] = SDLK_NUMLOCK;
slouken@0
   278
	VK_keymap[VK_CAPITAL] = SDLK_CAPSLOCK;
slouken@0
   279
	VK_keymap[VK_SCROLL] = SDLK_SCROLLOCK;
slouken@0
   280
	VK_keymap[VK_RSHIFT] = SDLK_RSHIFT;
slouken@0
   281
	VK_keymap[VK_LSHIFT] = SDLK_LSHIFT;
slouken@0
   282
	VK_keymap[VK_RCONTROL] = SDLK_RCTRL;
slouken@0
   283
	VK_keymap[VK_LCONTROL] = SDLK_LCTRL;
slouken@0
   284
	VK_keymap[VK_RMENU] = SDLK_RALT;
slouken@0
   285
	VK_keymap[VK_LMENU] = SDLK_LALT;
slouken@0
   286
	VK_keymap[VK_RWIN] = SDLK_RSUPER;
slouken@0
   287
	VK_keymap[VK_LWIN] = SDLK_LSUPER;
slouken@0
   288
slouken@0
   289
	VK_keymap[VK_HELP] = SDLK_HELP;
slouken@0
   290
#ifdef VK_PRINT
slouken@0
   291
	VK_keymap[VK_PRINT] = SDLK_PRINT;
slouken@0
   292
#endif
slouken@0
   293
	VK_keymap[VK_SNAPSHOT] = SDLK_PRINT;
slouken@0
   294
	VK_keymap[VK_CANCEL] = SDLK_BREAK;
slouken@0
   295
	VK_keymap[VK_APPS] = SDLK_MENU;
slouken@0
   296
}
slouken@0
   297
slouken@0
   298
static SDL_keysym *TranslateKey(UINT vkey, UINT scancode, SDL_keysym *keysym, int pressed)
slouken@0
   299
{
slouken@0
   300
	/* Set the keysym information */
slouken@0
   301
	keysym->scancode = (unsigned char) scancode;
slouken@0
   302
	keysym->sym = VK_keymap[vkey];
slouken@0
   303
	keysym->mod = KMOD_NONE;
slouken@0
   304
	keysym->unicode = 0;
slouken@0
   305
	if ( pressed && SDL_TranslateUNICODE ) { /* Someday use ToUnicode() */
slouken@0
   306
#ifdef NO_GETKEYBOARDSTATE
slouken@0
   307
		/* Uh oh, better hope the vkey is close enough.. */
slouken@0
   308
		keysym->unicode = vkey;
slouken@0
   309
#else
slouken@0
   310
		BYTE keystate[256];
slouken@0
   311
		BYTE chars[2];
slouken@0
   312
slouken@0
   313
		GetKeyboardState(keystate);
slouken@0
   314
		if ( ToAscii(vkey,scancode,keystate,(WORD *)chars,0) == 1 ) {
slouken@0
   315
			keysym->unicode = chars[0];
slouken@0
   316
		}
slouken@0
   317
#endif /* NO_GETKEYBOARDSTATE */
slouken@0
   318
	}
slouken@0
   319
	return(keysym);
slouken@0
   320
}
slouken@0
   321
slouken@0
   322
int DIB_CreateWindow(_THIS)
slouken@0
   323
{
slouken@0
   324
#ifdef _WIN32_WCE
slouken@0
   325
	/* WinCE uses the UNICODE version */
slouken@36
   326
	int nLen = strlen(SDL_Appname)+1;
slouken@36
   327
	LPWSTR lpszW = alloca(nLen*2);
slouken@0
   328
	MultiByteToWideChar(CP_ACP, 0, SDL_Appname, -1, lpszW, nLen);
slouken@0
   329
slouken@0
   330
	SDL_RegisterApp("SDL_app", 0, 0);
slouken@36
   331
	SDL_Window = CreateWindow(lpszW, lpszW, WS_VISIBLE,
slouken@36
   332
                                  0, 0, 0, 0, NULL, NULL, SDL_Instance, NULL);
slouken@0
   333
	if ( SDL_Window == NULL ) {
slouken@0
   334
		SDL_SetError("Couldn't create window");
slouken@0
   335
		return(-1);
slouken@0
   336
	}
slouken@0
   337
	ShowWindow(SDL_Window, SW_HIDE);
slouken@0
   338
#else
slouken@0
   339
	SDL_RegisterApp("SDL_app", CS_BYTEALIGNCLIENT, 0);
slouken@0
   340
	if ( SDL_windowid ) {
slouken@0
   341
		SDL_Window = (HWND)strtol(SDL_windowid, NULL, 0);
slouken@0
   342
	} else {
slouken@0
   343
		SDL_Window = CreateWindow(SDL_Appname, SDL_Appname,
slouken@0
   344
                        (WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX),
slouken@0
   345
                                 0, 0, 0, 0, NULL, NULL, SDL_Instance, NULL);
slouken@0
   346
		if ( SDL_Window == NULL ) {
slouken@0
   347
			SDL_SetError("Couldn't create window");
slouken@0
   348
			return(-1);
slouken@0
   349
		}
slouken@0
   350
		ShowWindow(SDL_Window, SW_HIDE);
slouken@0
   351
	}
slouken@0
   352
#endif /* _WIN32_WCE */
slouken@0
   353
slouken@0
   354
	return(0);
slouken@0
   355
}
slouken@0
   356
slouken@0
   357
void DIB_DestroyWindow(_THIS)
slouken@0
   358
{
slouken@0
   359
	if ( SDL_windowid == NULL ) {
slouken@0
   360
		DestroyWindow(SDL_Window);
slouken@0
   361
	}
slouken@0
   362
}