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