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
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Library General Public
     7     License as published by the Free Software Foundation; either
     8     version 2 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Library General Public License for more details.
    14 
    15     You should have received a copy of the GNU Library General Public
    16     License along with this library; if not, write to the Free
    17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    18 
    19     Sam Lantinga
    20     slouken@devolution.com
    21 */
    22 
    23 #ifdef SAVE_RCSID
    24 static char rcsid =
    25  "@(#) $Id$";
    26 #endif
    27 
    28 #include <stdlib.h>
    29 #include <stdio.h>
    30 #include <windows.h>
    31 
    32 #include "SDL_events.h"
    33 #include "SDL_error.h"
    34 #include "SDL_syswm.h"
    35 #include "SDL_sysevents.h"
    36 #include "SDL_events_c.h"
    37 #include "SDL_lowvideo.h"
    38 #include "SDL_dibvideo.h"
    39 #include "SDL_vkeys.h"
    40 
    41 #ifndef WM_APP
    42 #define WM_APP	0x8000
    43 #endif
    44 
    45 #ifdef _WIN32_WCE
    46 #define NO_GETKEYBOARDSTATE
    47 #endif
    48 
    49 /* The translation table from a Microsoft VK keysym to a SDL keysym */
    50 static SDLKey VK_keymap[SDLK_LAST];
    51 static SDL_keysym *TranslateKey(UINT vkey, UINT scancode, SDL_keysym *keysym, int pressed);
    52 
    53 /* Masks for processing the windows KEYDOWN and KEYUP messages */
    54 #define REPEATED_KEYMASK	(1<<30)
    55 #define EXTENDED_KEYMASK	(1<<24)
    56 
    57 /* The main Win32 event handler */
    58 LONG
    59  DIB_HandleMessage(_THIS, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    60 {
    61 	extern int posted;
    62 
    63 	switch (msg) {
    64 		case WM_SYSKEYDOWN:
    65 		case WM_KEYDOWN: {
    66 			SDL_keysym keysym;
    67 
    68 			/* Ignore repeated keys */
    69 			if ( lParam&REPEATED_KEYMASK ) {
    70 				return(0);
    71 			}
    72 			switch (wParam) {
    73 				case VK_CONTROL:
    74 					if ( lParam&EXTENDED_KEYMASK )
    75 						wParam = VK_RCONTROL;
    76 					else
    77 						wParam = VK_LCONTROL;
    78 					break;
    79 				case VK_SHIFT:
    80 					/* EXTENDED trick doesn't work here */
    81 					wParam = VK_LSHIFT;
    82 					break;
    83 				case VK_MENU:
    84 					if ( lParam&EXTENDED_KEYMASK )
    85 						wParam = VK_RMENU;
    86 					else
    87 						wParam = VK_LMENU;
    88 					break;
    89 			}
    90 			posted = SDL_PrivateKeyboard(SDL_PRESSED,
    91 				TranslateKey(wParam,HIWORD(lParam),&keysym,1));
    92 		}
    93 		return(0);
    94 
    95 		case WM_SYSKEYUP:
    96 		case WM_KEYUP: {
    97 			SDL_keysym keysym;
    98 
    99 			switch (wParam) {
   100 				case VK_CONTROL:
   101 					if ( lParam&EXTENDED_KEYMASK )
   102 						wParam = VK_RCONTROL;
   103 					else
   104 						wParam = VK_LCONTROL;
   105 					break;
   106 				case VK_SHIFT:
   107 					/* EXTENDED trick doesn't work here */
   108 					wParam = VK_LSHIFT;
   109 					break;
   110 				case VK_MENU:
   111 					if ( lParam&EXTENDED_KEYMASK )
   112 						wParam = VK_RMENU;
   113 					else
   114 						wParam = VK_LMENU;
   115 					break;
   116 			}
   117 			posted = SDL_PrivateKeyboard(SDL_RELEASED,
   118 				TranslateKey(wParam,HIWORD(lParam),&keysym,0));
   119 		}
   120 		return(0);
   121 
   122 		default: {
   123 			/* Only post the event if we're watching for it */
   124 			if ( SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE ) {
   125 			        SDL_SysWMmsg wmmsg;
   126 
   127 				SDL_VERSION(&wmmsg.version);
   128 				wmmsg.hwnd = hwnd;
   129 				wmmsg.msg = msg;
   130 				wmmsg.wParam = wParam;
   131 				wmmsg.lParam = lParam;
   132 				posted = SDL_PrivateSysWMEvent(&wmmsg);
   133 			}
   134 		}
   135 		break;
   136 	}
   137 	return(DefWindowProc(hwnd, msg, wParam, lParam));
   138 }
   139 
   140 void DIB_PumpEvents(_THIS)
   141 {
   142 	MSG msg;
   143 
   144 	while ( PeekMessage(&msg, NULL, 0, (WM_APP-1), PM_NOREMOVE) ) {
   145 		if ( GetMessage(&msg, NULL, 0, (WM_APP-1)) > 0 ) {
   146 			DispatchMessage(&msg);
   147 		}
   148 	}
   149 }
   150 
   151 void DIB_InitOSKeymap(_THIS)
   152 {
   153 	int i;
   154 
   155 	/* Map the VK keysyms */
   156 	for ( i=0; i<SDL_TABLESIZE(VK_keymap); ++i )
   157 		VK_keymap[i] = SDLK_UNKNOWN;
   158 
   159 	VK_keymap[VK_BACK] = SDLK_BACKSPACE;
   160 	VK_keymap[VK_TAB] = SDLK_TAB;
   161 	VK_keymap[VK_CLEAR] = SDLK_CLEAR;
   162 	VK_keymap[VK_RETURN] = SDLK_RETURN;
   163 	VK_keymap[VK_PAUSE] = SDLK_PAUSE;
   164 	VK_keymap[VK_ESCAPE] = SDLK_ESCAPE;
   165 	VK_keymap[VK_SPACE] = SDLK_SPACE;
   166 	VK_keymap[VK_APOSTROPHE] = SDLK_QUOTE;
   167 	VK_keymap[VK_COMMA] = SDLK_COMMA;
   168 	VK_keymap[VK_MINUS] = SDLK_MINUS;
   169 	VK_keymap[VK_PERIOD] = SDLK_PERIOD;
   170 	VK_keymap[VK_SLASH] = SDLK_SLASH;
   171 	VK_keymap[VK_0] = SDLK_0;
   172 	VK_keymap[VK_1] = SDLK_1;
   173 	VK_keymap[VK_2] = SDLK_2;
   174 	VK_keymap[VK_3] = SDLK_3;
   175 	VK_keymap[VK_4] = SDLK_4;
   176 	VK_keymap[VK_5] = SDLK_5;
   177 	VK_keymap[VK_6] = SDLK_6;
   178 	VK_keymap[VK_7] = SDLK_7;
   179 	VK_keymap[VK_8] = SDLK_8;
   180 	VK_keymap[VK_9] = SDLK_9;
   181 	VK_keymap[VK_SEMICOLON] = SDLK_SEMICOLON;
   182 	VK_keymap[VK_EQUALS] = SDLK_EQUALS;
   183 	VK_keymap[VK_LBRACKET] = SDLK_LEFTBRACKET;
   184 	VK_keymap[VK_BACKSLASH] = SDLK_BACKSLASH;
   185 	VK_keymap[VK_RBRACKET] = SDLK_RIGHTBRACKET;
   186 	VK_keymap[VK_GRAVE] = SDLK_BACKQUOTE;
   187 	VK_keymap[VK_A] = SDLK_a;
   188 	VK_keymap[VK_B] = SDLK_b;
   189 	VK_keymap[VK_C] = SDLK_c;
   190 	VK_keymap[VK_D] = SDLK_d;
   191 	VK_keymap[VK_E] = SDLK_e;
   192 	VK_keymap[VK_F] = SDLK_f;
   193 	VK_keymap[VK_G] = SDLK_g;
   194 	VK_keymap[VK_H] = SDLK_h;
   195 	VK_keymap[VK_I] = SDLK_i;
   196 	VK_keymap[VK_J] = SDLK_j;
   197 	VK_keymap[VK_K] = SDLK_k;
   198 	VK_keymap[VK_L] = SDLK_l;
   199 	VK_keymap[VK_M] = SDLK_m;
   200 	VK_keymap[VK_N] = SDLK_n;
   201 	VK_keymap[VK_O] = SDLK_o;
   202 	VK_keymap[VK_P] = SDLK_p;
   203 	VK_keymap[VK_Q] = SDLK_q;
   204 	VK_keymap[VK_R] = SDLK_r;
   205 	VK_keymap[VK_S] = SDLK_s;
   206 	VK_keymap[VK_T] = SDLK_t;
   207 	VK_keymap[VK_U] = SDLK_u;
   208 	VK_keymap[VK_V] = SDLK_v;
   209 	VK_keymap[VK_W] = SDLK_w;
   210 	VK_keymap[VK_X] = SDLK_x;
   211 	VK_keymap[VK_Y] = SDLK_y;
   212 	VK_keymap[VK_Z] = SDLK_z;
   213 	VK_keymap[VK_DELETE] = SDLK_DELETE;
   214 
   215 	VK_keymap[VK_NUMPAD0] = SDLK_KP0;
   216 	VK_keymap[VK_NUMPAD1] = SDLK_KP1;
   217 	VK_keymap[VK_NUMPAD2] = SDLK_KP2;
   218 	VK_keymap[VK_NUMPAD3] = SDLK_KP3;
   219 	VK_keymap[VK_NUMPAD4] = SDLK_KP4;
   220 	VK_keymap[VK_NUMPAD5] = SDLK_KP5;
   221 	VK_keymap[VK_NUMPAD6] = SDLK_KP6;
   222 	VK_keymap[VK_NUMPAD7] = SDLK_KP7;
   223 	VK_keymap[VK_NUMPAD8] = SDLK_KP8;
   224 	VK_keymap[VK_NUMPAD9] = SDLK_KP9;
   225 	VK_keymap[VK_DECIMAL] = SDLK_KP_PERIOD;
   226 	VK_keymap[VK_DIVIDE] = SDLK_KP_DIVIDE;
   227 	VK_keymap[VK_MULTIPLY] = SDLK_KP_MULTIPLY;
   228 	VK_keymap[VK_SUBTRACT] = SDLK_KP_MINUS;
   229 	VK_keymap[VK_ADD] = SDLK_KP_PLUS;
   230 
   231 	VK_keymap[VK_UP] = SDLK_UP;
   232 	VK_keymap[VK_DOWN] = SDLK_DOWN;
   233 	VK_keymap[VK_RIGHT] = SDLK_RIGHT;
   234 	VK_keymap[VK_LEFT] = SDLK_LEFT;
   235 	VK_keymap[VK_INSERT] = SDLK_INSERT;
   236 	VK_keymap[VK_HOME] = SDLK_HOME;
   237 	VK_keymap[VK_END] = SDLK_END;
   238 	VK_keymap[VK_PRIOR] = SDLK_PAGEUP;
   239 	VK_keymap[VK_NEXT] = SDLK_PAGEDOWN;
   240 
   241 	VK_keymap[VK_F1] = SDLK_F1;
   242 	VK_keymap[VK_F2] = SDLK_F2;
   243 	VK_keymap[VK_F3] = SDLK_F3;
   244 	VK_keymap[VK_F4] = SDLK_F4;
   245 	VK_keymap[VK_F5] = SDLK_F5;
   246 	VK_keymap[VK_F6] = SDLK_F6;
   247 	VK_keymap[VK_F7] = SDLK_F7;
   248 	VK_keymap[VK_F8] = SDLK_F8;
   249 	VK_keymap[VK_F9] = SDLK_F9;
   250 	VK_keymap[VK_F10] = SDLK_F10;
   251 	VK_keymap[VK_F11] = SDLK_F11;
   252 	VK_keymap[VK_F12] = SDLK_F12;
   253 	VK_keymap[VK_F13] = SDLK_F13;
   254 	VK_keymap[VK_F14] = SDLK_F14;
   255 	VK_keymap[VK_F15] = SDLK_F15;
   256 
   257 	VK_keymap[VK_NUMLOCK] = SDLK_NUMLOCK;
   258 	VK_keymap[VK_CAPITAL] = SDLK_CAPSLOCK;
   259 	VK_keymap[VK_SCROLL] = SDLK_SCROLLOCK;
   260 	VK_keymap[VK_RSHIFT] = SDLK_RSHIFT;
   261 	VK_keymap[VK_LSHIFT] = SDLK_LSHIFT;
   262 	VK_keymap[VK_RCONTROL] = SDLK_RCTRL;
   263 	VK_keymap[VK_LCONTROL] = SDLK_LCTRL;
   264 	VK_keymap[VK_RMENU] = SDLK_RALT;
   265 	VK_keymap[VK_LMENU] = SDLK_LALT;
   266 	VK_keymap[VK_RWIN] = SDLK_RSUPER;
   267 	VK_keymap[VK_LWIN] = SDLK_LSUPER;
   268 
   269 	VK_keymap[VK_HELP] = SDLK_HELP;
   270 #ifdef VK_PRINT
   271 	VK_keymap[VK_PRINT] = SDLK_PRINT;
   272 #endif
   273 	VK_keymap[VK_SNAPSHOT] = SDLK_PRINT;
   274 	VK_keymap[VK_CANCEL] = SDLK_BREAK;
   275 	VK_keymap[VK_APPS] = SDLK_MENU;
   276 }
   277 
   278 static SDL_keysym *TranslateKey(UINT vkey, UINT scancode, SDL_keysym *keysym, int pressed)
   279 {
   280 	/* Set the keysym information */
   281 	keysym->scancode = (unsigned char) scancode;
   282 	keysym->sym = VK_keymap[vkey];
   283 	keysym->mod = KMOD_NONE;
   284 	keysym->unicode = 0;
   285 	if ( pressed && SDL_TranslateUNICODE ) { /* Someday use ToUnicode() */
   286 #ifdef NO_GETKEYBOARDSTATE
   287 		/* Uh oh, better hope the vkey is close enough.. */
   288 		keysym->unicode = vkey;
   289 #else
   290 		BYTE keystate[256];
   291 		BYTE chars[2];
   292 
   293 		GetKeyboardState(keystate);
   294 		if ( ToAscii(vkey,scancode,keystate,(WORD *)chars,0) == 1 ) {
   295 			keysym->unicode = chars[0];
   296 		}
   297 #endif /* NO_GETKEYBOARDSTATE */
   298 	}
   299 	return(keysym);
   300 }
   301 
   302 int DIB_CreateWindow(_THIS)
   303 {
   304 #ifdef _WIN32_WCE
   305 	/* WinCE uses the UNICODE version */
   306 	int nLen = strlen(SDL_Appname)+1;
   307 	LPWSTR lpszW = alloca(nLen*2);
   308 	MultiByteToWideChar(CP_ACP, 0, SDL_Appname, -1, lpszW, nLen);
   309 
   310 	SDL_RegisterApp("SDL_app", 0, 0);
   311 	SDL_Window = CreateWindow(lpszW, lpszW, WS_VISIBLE,
   312                                   0, 0, 0, 0, NULL, NULL, SDL_Instance, NULL);
   313 	if ( SDL_Window == NULL ) {
   314 		SDL_SetError("Couldn't create window");
   315 		return(-1);
   316 	}
   317 	ShowWindow(SDL_Window, SW_HIDE);
   318 #else
   319 	SDL_RegisterApp("SDL_app", CS_BYTEALIGNCLIENT, 0);
   320 	if ( SDL_windowid ) {
   321 		SDL_Window = (HWND)strtol(SDL_windowid, NULL, 0);
   322 	} else {
   323 		SDL_Window = CreateWindow(SDL_Appname, SDL_Appname,
   324                         (WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX),
   325                                  0, 0, 0, 0, NULL, NULL, SDL_Instance, NULL);
   326 		if ( SDL_Window == NULL ) {
   327 			SDL_SetError("Couldn't create window");
   328 			return(-1);
   329 		}
   330 		ShowWindow(SDL_Window, SW_HIDE);
   331 	}
   332 #endif /* _WIN32_WCE */
   333 
   334 	return(0);
   335 }
   336 
   337 void DIB_DestroyWindow(_THIS)
   338 {
   339 	if ( SDL_windowid == NULL ) {
   340 		DestroyWindow(SDL_Window);
   341 	}
   342 }