src/video/windib/SDL_dibevents.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 19 Jan 2006 08:43:00 +0000
changeset 1251 86d0d01290ea
parent 1152 51a8702d8ecd
child 1253 7c7ddaf195bf
permissions -rw-r--r--
Updated Windows CE/PocketPC support...adds GAPI driver, landscape mode,
updated project files, VS2005 support, VGA mode, more device support, etc,
etc, etc.

Fixes Bugzilla #47 and #28.

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