src/video/windib/SDL_dibevents.c
changeset 0 74212992fb08
child 36 13ee9f4834ea
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/video/windib/SDL_dibevents.c	Thu Apr 26 16:45:43 2001 +0000
     1.3 @@ -0,0 +1,343 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Library General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Library General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Library General Public
    1.19 +    License along with this library; if not, write to the Free
    1.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@devolution.com
    1.24 +*/
    1.25 +
    1.26 +#ifdef SAVE_RCSID
    1.27 +static char rcsid =
    1.28 + "@(#) $Id$";
    1.29 +#endif
    1.30 +
    1.31 +#include <stdlib.h>
    1.32 +#include <stdio.h>
    1.33 +#include <windows.h>
    1.34 +
    1.35 +#include "SDL_events.h"
    1.36 +#include "SDL_error.h"
    1.37 +#include "SDL_syswm.h"
    1.38 +#include "SDL_sysevents.h"
    1.39 +#include "SDL_events_c.h"
    1.40 +#include "SDL_lowvideo.h"
    1.41 +#include "SDL_dibvideo.h"
    1.42 +#include "SDL_vkeys.h"
    1.43 +
    1.44 +#ifndef WM_APP
    1.45 +#define WM_APP	0x8000
    1.46 +#endif
    1.47 +
    1.48 +#ifdef _WIN32_WCE
    1.49 +#define NO_GETKEYBOARDSTATE
    1.50 +#endif
    1.51 +
    1.52 +/* The translation table from a Microsoft VK keysym to a SDL keysym */
    1.53 +static SDLKey VK_keymap[SDLK_LAST];
    1.54 +static SDL_keysym *TranslateKey(UINT vkey, UINT scancode, SDL_keysym *keysym, int pressed);
    1.55 +
    1.56 +/* Masks for processing the windows KEYDOWN and KEYUP messages */
    1.57 +#define REPEATED_KEYMASK	(1<<30)
    1.58 +#define EXTENDED_KEYMASK	(1<<24)
    1.59 +
    1.60 +/* The main Win32 event handler */
    1.61 +LONG
    1.62 + DIB_HandleMessage(_THIS, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    1.63 +{
    1.64 +	extern int posted;
    1.65 +
    1.66 +	switch (msg) {
    1.67 +		case WM_SYSKEYDOWN:
    1.68 +		case WM_KEYDOWN: {
    1.69 +			SDL_keysym keysym;
    1.70 +
    1.71 +			/* Ignore repeated keys */
    1.72 +			if ( lParam&REPEATED_KEYMASK ) {
    1.73 +				return(0);
    1.74 +			}
    1.75 +			switch (wParam) {
    1.76 +				case VK_CONTROL:
    1.77 +					if ( lParam&EXTENDED_KEYMASK )
    1.78 +						wParam = VK_RCONTROL;
    1.79 +					else
    1.80 +						wParam = VK_LCONTROL;
    1.81 +					break;
    1.82 +				case VK_SHIFT:
    1.83 +					/* EXTENDED trick doesn't work here */
    1.84 +					wParam = VK_LSHIFT;
    1.85 +					break;
    1.86 +				case VK_MENU:
    1.87 +					if ( lParam&EXTENDED_KEYMASK )
    1.88 +						wParam = VK_RMENU;
    1.89 +					else
    1.90 +						wParam = VK_LMENU;
    1.91 +					break;
    1.92 +			}
    1.93 +			posted = SDL_PrivateKeyboard(SDL_PRESSED,
    1.94 +				TranslateKey(wParam,HIWORD(lParam),&keysym,1));
    1.95 +		}
    1.96 +		return(0);
    1.97 +
    1.98 +		case WM_SYSKEYUP:
    1.99 +		case WM_KEYUP: {
   1.100 +			SDL_keysym keysym;
   1.101 +
   1.102 +			switch (wParam) {
   1.103 +				case VK_CONTROL:
   1.104 +					if ( lParam&EXTENDED_KEYMASK )
   1.105 +						wParam = VK_RCONTROL;
   1.106 +					else
   1.107 +						wParam = VK_LCONTROL;
   1.108 +					break;
   1.109 +				case VK_SHIFT:
   1.110 +					/* EXTENDED trick doesn't work here */
   1.111 +					wParam = VK_LSHIFT;
   1.112 +					break;
   1.113 +				case VK_MENU:
   1.114 +					if ( lParam&EXTENDED_KEYMASK )
   1.115 +						wParam = VK_RMENU;
   1.116 +					else
   1.117 +						wParam = VK_LMENU;
   1.118 +					break;
   1.119 +			}
   1.120 +			posted = SDL_PrivateKeyboard(SDL_RELEASED,
   1.121 +				TranslateKey(wParam,HIWORD(lParam),&keysym,0));
   1.122 +		}
   1.123 +		return(0);
   1.124 +
   1.125 +		default: {
   1.126 +			/* Only post the event if we're watching for it */
   1.127 +			if ( SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE ) {
   1.128 +			        SDL_SysWMmsg wmmsg;
   1.129 +
   1.130 +				SDL_VERSION(&wmmsg.version);
   1.131 +				wmmsg.hwnd = hwnd;
   1.132 +				wmmsg.msg = msg;
   1.133 +				wmmsg.wParam = wParam;
   1.134 +				wmmsg.lParam = lParam;
   1.135 +				posted = SDL_PrivateSysWMEvent(&wmmsg);
   1.136 +			}
   1.137 +		}
   1.138 +		break;
   1.139 +	}
   1.140 +	return(DefWindowProc(hwnd, msg, wParam, lParam));
   1.141 +}
   1.142 +
   1.143 +void DIB_PumpEvents(_THIS)
   1.144 +{
   1.145 +	MSG msg;
   1.146 +
   1.147 +	while ( PeekMessage(&msg, NULL, 0, (WM_APP-1), PM_NOREMOVE) ) {
   1.148 +		if ( GetMessage(&msg, NULL, 0, (WM_APP-1)) > 0 ) {
   1.149 +			DispatchMessage(&msg);
   1.150 +		}
   1.151 +	}
   1.152 +}
   1.153 +
   1.154 +void DIB_InitOSKeymap(_THIS)
   1.155 +{
   1.156 +	int i;
   1.157 +
   1.158 +	/* Map the VK keysyms */
   1.159 +	for ( i=0; i<SDL_TABLESIZE(VK_keymap); ++i )
   1.160 +		VK_keymap[i] = SDLK_UNKNOWN;
   1.161 +
   1.162 +	VK_keymap[VK_BACK] = SDLK_BACKSPACE;
   1.163 +	VK_keymap[VK_TAB] = SDLK_TAB;
   1.164 +	VK_keymap[VK_CLEAR] = SDLK_CLEAR;
   1.165 +	VK_keymap[VK_RETURN] = SDLK_RETURN;
   1.166 +	VK_keymap[VK_PAUSE] = SDLK_PAUSE;
   1.167 +	VK_keymap[VK_ESCAPE] = SDLK_ESCAPE;
   1.168 +	VK_keymap[VK_SPACE] = SDLK_SPACE;
   1.169 +	VK_keymap[VK_APOSTROPHE] = SDLK_QUOTE;
   1.170 +	VK_keymap[VK_COMMA] = SDLK_COMMA;
   1.171 +	VK_keymap[VK_MINUS] = SDLK_MINUS;
   1.172 +	VK_keymap[VK_PERIOD] = SDLK_PERIOD;
   1.173 +	VK_keymap[VK_SLASH] = SDLK_SLASH;
   1.174 +	VK_keymap[VK_0] = SDLK_0;
   1.175 +	VK_keymap[VK_1] = SDLK_1;
   1.176 +	VK_keymap[VK_2] = SDLK_2;
   1.177 +	VK_keymap[VK_3] = SDLK_3;
   1.178 +	VK_keymap[VK_4] = SDLK_4;
   1.179 +	VK_keymap[VK_5] = SDLK_5;
   1.180 +	VK_keymap[VK_6] = SDLK_6;
   1.181 +	VK_keymap[VK_7] = SDLK_7;
   1.182 +	VK_keymap[VK_8] = SDLK_8;
   1.183 +	VK_keymap[VK_9] = SDLK_9;
   1.184 +	VK_keymap[VK_SEMICOLON] = SDLK_SEMICOLON;
   1.185 +	VK_keymap[VK_EQUALS] = SDLK_EQUALS;
   1.186 +	VK_keymap[VK_LBRACKET] = SDLK_LEFTBRACKET;
   1.187 +	VK_keymap[VK_BACKSLASH] = SDLK_BACKSLASH;
   1.188 +	VK_keymap[VK_RBRACKET] = SDLK_RIGHTBRACKET;
   1.189 +	VK_keymap[VK_GRAVE] = SDLK_BACKQUOTE;
   1.190 +	VK_keymap[VK_A] = SDLK_a;
   1.191 +	VK_keymap[VK_B] = SDLK_b;
   1.192 +	VK_keymap[VK_C] = SDLK_c;
   1.193 +	VK_keymap[VK_D] = SDLK_d;
   1.194 +	VK_keymap[VK_E] = SDLK_e;
   1.195 +	VK_keymap[VK_F] = SDLK_f;
   1.196 +	VK_keymap[VK_G] = SDLK_g;
   1.197 +	VK_keymap[VK_H] = SDLK_h;
   1.198 +	VK_keymap[VK_I] = SDLK_i;
   1.199 +	VK_keymap[VK_J] = SDLK_j;
   1.200 +	VK_keymap[VK_K] = SDLK_k;
   1.201 +	VK_keymap[VK_L] = SDLK_l;
   1.202 +	VK_keymap[VK_M] = SDLK_m;
   1.203 +	VK_keymap[VK_N] = SDLK_n;
   1.204 +	VK_keymap[VK_O] = SDLK_o;
   1.205 +	VK_keymap[VK_P] = SDLK_p;
   1.206 +	VK_keymap[VK_Q] = SDLK_q;
   1.207 +	VK_keymap[VK_R] = SDLK_r;
   1.208 +	VK_keymap[VK_S] = SDLK_s;
   1.209 +	VK_keymap[VK_T] = SDLK_t;
   1.210 +	VK_keymap[VK_U] = SDLK_u;
   1.211 +	VK_keymap[VK_V] = SDLK_v;
   1.212 +	VK_keymap[VK_W] = SDLK_w;
   1.213 +	VK_keymap[VK_X] = SDLK_x;
   1.214 +	VK_keymap[VK_Y] = SDLK_y;
   1.215 +	VK_keymap[VK_Z] = SDLK_z;
   1.216 +	VK_keymap[VK_DELETE] = SDLK_DELETE;
   1.217 +
   1.218 +	VK_keymap[VK_NUMPAD0] = SDLK_KP0;
   1.219 +	VK_keymap[VK_NUMPAD1] = SDLK_KP1;
   1.220 +	VK_keymap[VK_NUMPAD2] = SDLK_KP2;
   1.221 +	VK_keymap[VK_NUMPAD3] = SDLK_KP3;
   1.222 +	VK_keymap[VK_NUMPAD4] = SDLK_KP4;
   1.223 +	VK_keymap[VK_NUMPAD5] = SDLK_KP5;
   1.224 +	VK_keymap[VK_NUMPAD6] = SDLK_KP6;
   1.225 +	VK_keymap[VK_NUMPAD7] = SDLK_KP7;
   1.226 +	VK_keymap[VK_NUMPAD8] = SDLK_KP8;
   1.227 +	VK_keymap[VK_NUMPAD9] = SDLK_KP9;
   1.228 +	VK_keymap[VK_DECIMAL] = SDLK_KP_PERIOD;
   1.229 +	VK_keymap[VK_DIVIDE] = SDLK_KP_DIVIDE;
   1.230 +	VK_keymap[VK_MULTIPLY] = SDLK_KP_MULTIPLY;
   1.231 +	VK_keymap[VK_SUBTRACT] = SDLK_KP_MINUS;
   1.232 +	VK_keymap[VK_ADD] = SDLK_KP_PLUS;
   1.233 +
   1.234 +	VK_keymap[VK_UP] = SDLK_UP;
   1.235 +	VK_keymap[VK_DOWN] = SDLK_DOWN;
   1.236 +	VK_keymap[VK_RIGHT] = SDLK_RIGHT;
   1.237 +	VK_keymap[VK_LEFT] = SDLK_LEFT;
   1.238 +	VK_keymap[VK_INSERT] = SDLK_INSERT;
   1.239 +	VK_keymap[VK_HOME] = SDLK_HOME;
   1.240 +	VK_keymap[VK_END] = SDLK_END;
   1.241 +	VK_keymap[VK_PRIOR] = SDLK_PAGEUP;
   1.242 +	VK_keymap[VK_NEXT] = SDLK_PAGEDOWN;
   1.243 +
   1.244 +	VK_keymap[VK_F1] = SDLK_F1;
   1.245 +	VK_keymap[VK_F2] = SDLK_F2;
   1.246 +	VK_keymap[VK_F3] = SDLK_F3;
   1.247 +	VK_keymap[VK_F4] = SDLK_F4;
   1.248 +	VK_keymap[VK_F5] = SDLK_F5;
   1.249 +	VK_keymap[VK_F6] = SDLK_F6;
   1.250 +	VK_keymap[VK_F7] = SDLK_F7;
   1.251 +	VK_keymap[VK_F8] = SDLK_F8;
   1.252 +	VK_keymap[VK_F9] = SDLK_F9;
   1.253 +	VK_keymap[VK_F10] = SDLK_F10;
   1.254 +	VK_keymap[VK_F11] = SDLK_F11;
   1.255 +	VK_keymap[VK_F12] = SDLK_F12;
   1.256 +	VK_keymap[VK_F13] = SDLK_F13;
   1.257 +	VK_keymap[VK_F14] = SDLK_F14;
   1.258 +	VK_keymap[VK_F15] = SDLK_F15;
   1.259 +
   1.260 +	VK_keymap[VK_NUMLOCK] = SDLK_NUMLOCK;
   1.261 +	VK_keymap[VK_CAPITAL] = SDLK_CAPSLOCK;
   1.262 +	VK_keymap[VK_SCROLL] = SDLK_SCROLLOCK;
   1.263 +	VK_keymap[VK_RSHIFT] = SDLK_RSHIFT;
   1.264 +	VK_keymap[VK_LSHIFT] = SDLK_LSHIFT;
   1.265 +	VK_keymap[VK_RCONTROL] = SDLK_RCTRL;
   1.266 +	VK_keymap[VK_LCONTROL] = SDLK_LCTRL;
   1.267 +	VK_keymap[VK_RMENU] = SDLK_RALT;
   1.268 +	VK_keymap[VK_LMENU] = SDLK_LALT;
   1.269 +	VK_keymap[VK_RWIN] = SDLK_RSUPER;
   1.270 +	VK_keymap[VK_LWIN] = SDLK_LSUPER;
   1.271 +
   1.272 +	VK_keymap[VK_HELP] = SDLK_HELP;
   1.273 +#ifdef VK_PRINT
   1.274 +	VK_keymap[VK_PRINT] = SDLK_PRINT;
   1.275 +#endif
   1.276 +	VK_keymap[VK_SNAPSHOT] = SDLK_PRINT;
   1.277 +	VK_keymap[VK_CANCEL] = SDLK_BREAK;
   1.278 +	VK_keymap[VK_APPS] = SDLK_MENU;
   1.279 +}
   1.280 +
   1.281 +static SDL_keysym *TranslateKey(UINT vkey, UINT scancode, SDL_keysym *keysym, int pressed)
   1.282 +{
   1.283 +	/* Set the keysym information */
   1.284 +	keysym->scancode = (unsigned char) scancode;
   1.285 +	keysym->sym = VK_keymap[vkey];
   1.286 +	keysym->mod = KMOD_NONE;
   1.287 +	keysym->unicode = 0;
   1.288 +	if ( pressed && SDL_TranslateUNICODE ) { /* Someday use ToUnicode() */
   1.289 +#ifdef NO_GETKEYBOARDSTATE
   1.290 +		/* Uh oh, better hope the vkey is close enough.. */
   1.291 +		keysym->unicode = vkey;
   1.292 +#else
   1.293 +		BYTE keystate[256];
   1.294 +		BYTE chars[2];
   1.295 +
   1.296 +		GetKeyboardState(keystate);
   1.297 +		if ( ToAscii(vkey,scancode,keystate,(WORD *)chars,0) == 1 ) {
   1.298 +			keysym->unicode = chars[0];
   1.299 +		}
   1.300 +#endif /* NO_GETKEYBOARDSTATE */
   1.301 +	}
   1.302 +	return(keysym);
   1.303 +}
   1.304 +
   1.305 +int DIB_CreateWindow(_THIS)
   1.306 +{
   1.307 +#ifdef _WIN32_WCE
   1.308 +	/* WinCE uses the UNICODE version */
   1.309 +	int nLen = strlen(SDL_Appname);
   1.310 +	LPWSTR lpszW = alloca((nLen+1)*2);
   1.311 +	MultiByteToWideChar(CP_ACP, 0, SDL_Appname, -1, lpszW, nLen);
   1.312 +
   1.313 +	SDL_RegisterApp("SDL_app", 0, 0);
   1.314 +	SDL_Window = CreateWindow(lpszW, lpszW,
   1.315 +                        (WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX),
   1.316 +                                 0, 0, 0, 0, NULL, NULL, SDL_Instance, NULL);
   1.317 +	if ( SDL_Window == NULL ) {
   1.318 +		SDL_SetError("Couldn't create window");
   1.319 +		return(-1);
   1.320 +	}
   1.321 +	ShowWindow(SDL_Window, SW_HIDE);
   1.322 +#else
   1.323 +	SDL_RegisterApp("SDL_app", CS_BYTEALIGNCLIENT, 0);
   1.324 +	if ( SDL_windowid ) {
   1.325 +		SDL_Window = (HWND)strtol(SDL_windowid, NULL, 0);
   1.326 +	} else {
   1.327 +		SDL_Window = CreateWindow(SDL_Appname, SDL_Appname,
   1.328 +                        (WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX),
   1.329 +                                 0, 0, 0, 0, NULL, NULL, SDL_Instance, NULL);
   1.330 +		if ( SDL_Window == NULL ) {
   1.331 +			SDL_SetError("Couldn't create window");
   1.332 +			return(-1);
   1.333 +		}
   1.334 +		ShowWindow(SDL_Window, SW_HIDE);
   1.335 +	}
   1.336 +#endif /* _WIN32_WCE */
   1.337 +
   1.338 +	return(0);
   1.339 +}
   1.340 +
   1.341 +void DIB_DestroyWindow(_THIS)
   1.342 +{
   1.343 +	if ( SDL_windowid == NULL ) {
   1.344 +		DestroyWindow(SDL_Window);
   1.345 +	}
   1.346 +}