src/video/windib/SDL_dibevents.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 14 Mar 2006 07:06:06 +0000
changeset 1526 89f90f32f6c0
parent 1523 21b1fbb53f4a
child 1662 782fd950bd46
child 1852 eb2d5480ae95
permissions -rw-r--r--
Fixed bug #124
Print Screen key events weren't reported on Windows
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 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     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 #define WIN32_LEAN_AND_MEAN
    25 #include <windows.h>
    26 
    27 #include "SDL_main.h"
    28 #include "SDL_events.h"
    29 #include "SDL_syswm.h"
    30 #include "../../events/SDL_sysevents.h"
    31 #include "../../events/SDL_events_c.h"
    32 #include "../wincommon/SDL_lowvideo.h"
    33 #include "SDL_dibvideo.h"
    34 #include "SDL_vkeys.h"
    35 
    36 #ifndef WM_APP
    37 #define WM_APP	0x8000
    38 #endif
    39 
    40 #ifdef _WIN32_WCE
    41 #define NO_GETKEYBOARDSTATE
    42 #endif
    43 
    44 /* The translation table from a Microsoft VK keysym to a SDL keysym */
    45 static SDLKey VK_keymap[SDLK_LAST];
    46 static SDL_keysym *TranslateKey(WPARAM vkey, UINT scancode, SDL_keysym *keysym, int pressed);
    47 
    48 /* Masks for processing the windows KEYDOWN and KEYUP messages */
    49 #define REPEATED_KEYMASK	(1<<30)
    50 #define EXTENDED_KEYMASK	(1<<24)
    51 
    52 /* DJM: If the user setup the window for us, we want to save his window proc,
    53    and give him a chance to handle some messages. */
    54 #ifdef STRICT
    55 #define WNDPROCTYPE	WNDPROC
    56 #else
    57 #define WNDPROCTYPE	FARPROC
    58 #endif
    59 static WNDPROCTYPE userWindowProc = NULL;
    60 
    61 
    62 #ifdef _WIN32_WCE
    63 
    64 WPARAM rotateKey(WPARAM key,SDL_ScreenOrientation direction) 
    65 {
    66 	if (direction != SDL_ORIENTATION_LEFT)
    67 		return key;
    68 
    69 	switch (key) {
    70 		case 0x26: /* up */
    71 			return 0x27;
    72 		case 0x27: /* right */
    73 			return 0x28;
    74 		case 0x28: /* down */
    75 			return 0x25;
    76 		case 0x25: /* left */
    77 			return 0x26;
    78 	}
    79 
    80 	return key;
    81 }
    82 
    83 #endif 
    84 
    85 
    86 /* The main Win32 event handler */
    87 LRESULT DIB_HandleMessage(_THIS, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    88 {
    89 	extern int posted;
    90 
    91 	switch (msg) {
    92 		case WM_SYSKEYDOWN:
    93 		case WM_KEYDOWN: {
    94 			SDL_keysym keysym;
    95 
    96 #ifdef _WIN32_WCE
    97 			// Drop GAPI artefacts
    98 			if (wParam == 0x84 || wParam == 0x5B)
    99 				return 0;
   100 
   101 			// Rotate key if necessary
   102 			if (this->hidden->orientation != SDL_ORIENTATION_UP)
   103 				wParam = rotateKey(wParam, this->hidden->orientation);	
   104 #endif 
   105 			/* Ignore repeated keys */
   106 			if ( lParam&REPEATED_KEYMASK ) {
   107 				return(0);
   108 			}
   109 			switch (wParam) {
   110 				case VK_CONTROL:
   111 					if ( lParam&EXTENDED_KEYMASK )
   112 						wParam = VK_RCONTROL;
   113 					else
   114 						wParam = VK_LCONTROL;
   115 					break;
   116 				case VK_SHIFT:
   117 					/* EXTENDED trick doesn't work here */
   118 					{
   119 					Uint8 *state = SDL_GetKeyState(NULL);
   120 					if (state[SDLK_LSHIFT] == SDL_RELEASED && (GetKeyState(VK_LSHIFT) & 0x8000)) {
   121 						wParam = VK_LSHIFT;
   122 					} else if (state[SDLK_RSHIFT] == SDL_RELEASED && (GetKeyState(VK_RSHIFT) & 0x8000)) {
   123 						wParam = VK_RSHIFT;
   124 					} else {
   125 						/* Probably a key repeat */
   126 						return(0);
   127 					}
   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 				}
   151 			}
   152 #endif /* NO_GETKEYBOARDSTATE */
   153 			posted = SDL_PrivateKeyboard(SDL_PRESSED,
   154 				TranslateKey(wParam,HIWORD(lParam),&keysym,1));
   155 		}
   156 		return(0);
   157 
   158 		case WM_SYSKEYUP:
   159 		case WM_KEYUP: {
   160 			SDL_keysym keysym;
   161 
   162 #ifdef _WIN32_WCE
   163 			// Drop GAPI artifacts
   164 			if (wParam == 0x84 || wParam == 0x5B)
   165 				return 0;
   166 
   167 			// Rotate key if necessary
   168 			if (this->hidden->orientation != SDL_ORIENTATION_UP)
   169 				wParam = rotateKey(wParam, this->hidden->orientation);	
   170 #endif
   171 
   172 			switch (wParam) {
   173 				case VK_CONTROL:
   174 					if ( lParam&EXTENDED_KEYMASK )
   175 						wParam = VK_RCONTROL;
   176 					else
   177 						wParam = VK_LCONTROL;
   178 					break;
   179 				case VK_SHIFT:
   180 					/* EXTENDED trick doesn't work here */
   181 					{
   182 					Uint8 *state = SDL_GetKeyState(NULL);
   183 					if (state[SDLK_LSHIFT] == SDL_PRESSED && !(GetKeyState(VK_LSHIFT) & 0x8000)) {
   184 						wParam = VK_LSHIFT;
   185 					} else if (state[SDLK_RSHIFT] == SDL_PRESSED && !(GetKeyState(VK_RSHIFT) & 0x8000)) {
   186 						wParam = VK_RSHIFT;
   187 					} else {
   188 						/* Probably a key repeat */
   189 						return(0);
   190 					}
   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 			/* Windows only reports keyup for print screen */
   201 			if ( wParam == VK_SNAPSHOT && SDL_GetKeyState(NULL)[SDLK_PRINT] == SDL_RELEASED ) {
   202 				posted = SDL_PrivateKeyboard(SDL_PRESSED,
   203 					TranslateKey(wParam,HIWORD(lParam),&keysym,1));
   204 			}
   205 			posted = SDL_PrivateKeyboard(SDL_RELEASED,
   206 				TranslateKey(wParam,HIWORD(lParam),&keysym,0));
   207 		}
   208 		return(0);
   209 
   210 #if defined(SC_SCREENSAVE) && defined(SC_MONITORPOWER)
   211 		case WM_SYSCOMMAND: {
   212 			if ((wParam&0xFFF0)==SC_SCREENSAVE ||
   213 				(wParam&0xFFF0)==SC_MONITORPOWER)
   214 					return(0);
   215 		}
   216 		/* Fall through to default processing */
   217 #endif /* SC_SCREENSAVE && SC_MONITORPOWER */
   218 
   219 		default: {
   220 			/* Only post the event if we're watching for it */
   221 			if ( SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE ) {
   222 			        SDL_SysWMmsg wmmsg;
   223 
   224 				SDL_VERSION(&wmmsg.version);
   225 				wmmsg.hwnd = hwnd;
   226 				wmmsg.msg = msg;
   227 				wmmsg.wParam = wParam;
   228 				wmmsg.lParam = lParam;
   229 				posted = SDL_PrivateSysWMEvent(&wmmsg);
   230 
   231 			/* DJM: If the user isn't watching for private
   232 				messages in her SDL event loop, then pass it
   233 				along to any win32 specific window proc.
   234 			 */
   235 			} else if (userWindowProc) {
   236 				return CallWindowProc(userWindowProc, hwnd, msg, wParam, lParam);
   237 			}
   238 		}
   239 		break;
   240 	}
   241 	return(DefWindowProc(hwnd, msg, wParam, lParam));
   242 }
   243 
   244 void DIB_PumpEvents(_THIS)
   245 {
   246 	MSG msg;
   247 
   248 	while ( PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) ) {
   249 		if ( GetMessage(&msg, NULL, 0, 0) > 0 ) {
   250 			DispatchMessage(&msg);
   251 		}
   252 	}
   253 }
   254 
   255 void DIB_InitOSKeymap(_THIS)
   256 {
   257 	int i;
   258 
   259 	/* Map the VK keysyms */
   260 	for ( i=0; i<SDL_arraysize(VK_keymap); ++i )
   261 		VK_keymap[i] = SDLK_UNKNOWN;
   262 
   263 	VK_keymap[VK_BACK] = SDLK_BACKSPACE;
   264 	VK_keymap[VK_TAB] = SDLK_TAB;
   265 	VK_keymap[VK_CLEAR] = SDLK_CLEAR;
   266 	VK_keymap[VK_RETURN] = SDLK_RETURN;
   267 	VK_keymap[VK_PAUSE] = SDLK_PAUSE;
   268 	VK_keymap[VK_ESCAPE] = SDLK_ESCAPE;
   269 	VK_keymap[VK_SPACE] = SDLK_SPACE;
   270 	VK_keymap[VK_APOSTROPHE] = SDLK_QUOTE;
   271 	VK_keymap[VK_COMMA] = SDLK_COMMA;
   272 	VK_keymap[VK_MINUS] = SDLK_MINUS;
   273 	VK_keymap[VK_PERIOD] = SDLK_PERIOD;
   274 	VK_keymap[VK_SLASH] = SDLK_SLASH;
   275 	VK_keymap[VK_0] = SDLK_0;
   276 	VK_keymap[VK_1] = SDLK_1;
   277 	VK_keymap[VK_2] = SDLK_2;
   278 	VK_keymap[VK_3] = SDLK_3;
   279 	VK_keymap[VK_4] = SDLK_4;
   280 	VK_keymap[VK_5] = SDLK_5;
   281 	VK_keymap[VK_6] = SDLK_6;
   282 	VK_keymap[VK_7] = SDLK_7;
   283 	VK_keymap[VK_8] = SDLK_8;
   284 	VK_keymap[VK_9] = SDLK_9;
   285 	VK_keymap[VK_SEMICOLON] = SDLK_SEMICOLON;
   286 	VK_keymap[VK_EQUALS] = SDLK_EQUALS;
   287 	VK_keymap[VK_LBRACKET] = SDLK_LEFTBRACKET;
   288 	VK_keymap[VK_BACKSLASH] = SDLK_BACKSLASH;
   289 	VK_keymap[VK_OEM_102] = SDLK_LESS;
   290 	VK_keymap[VK_RBRACKET] = SDLK_RIGHTBRACKET;
   291 	VK_keymap[VK_GRAVE] = SDLK_BACKQUOTE;
   292 	VK_keymap[VK_BACKTICK] = SDLK_BACKQUOTE;
   293 	VK_keymap[VK_A] = SDLK_a;
   294 	VK_keymap[VK_B] = SDLK_b;
   295 	VK_keymap[VK_C] = SDLK_c;
   296 	VK_keymap[VK_D] = SDLK_d;
   297 	VK_keymap[VK_E] = SDLK_e;
   298 	VK_keymap[VK_F] = SDLK_f;
   299 	VK_keymap[VK_G] = SDLK_g;
   300 	VK_keymap[VK_H] = SDLK_h;
   301 	VK_keymap[VK_I] = SDLK_i;
   302 	VK_keymap[VK_J] = SDLK_j;
   303 	VK_keymap[VK_K] = SDLK_k;
   304 	VK_keymap[VK_L] = SDLK_l;
   305 	VK_keymap[VK_M] = SDLK_m;
   306 	VK_keymap[VK_N] = SDLK_n;
   307 	VK_keymap[VK_O] = SDLK_o;
   308 	VK_keymap[VK_P] = SDLK_p;
   309 	VK_keymap[VK_Q] = SDLK_q;
   310 	VK_keymap[VK_R] = SDLK_r;
   311 	VK_keymap[VK_S] = SDLK_s;
   312 	VK_keymap[VK_T] = SDLK_t;
   313 	VK_keymap[VK_U] = SDLK_u;
   314 	VK_keymap[VK_V] = SDLK_v;
   315 	VK_keymap[VK_W] = SDLK_w;
   316 	VK_keymap[VK_X] = SDLK_x;
   317 	VK_keymap[VK_Y] = SDLK_y;
   318 	VK_keymap[VK_Z] = SDLK_z;
   319 	VK_keymap[VK_DELETE] = SDLK_DELETE;
   320 
   321 	VK_keymap[VK_NUMPAD0] = SDLK_KP0;
   322 	VK_keymap[VK_NUMPAD1] = SDLK_KP1;
   323 	VK_keymap[VK_NUMPAD2] = SDLK_KP2;
   324 	VK_keymap[VK_NUMPAD3] = SDLK_KP3;
   325 	VK_keymap[VK_NUMPAD4] = SDLK_KP4;
   326 	VK_keymap[VK_NUMPAD5] = SDLK_KP5;
   327 	VK_keymap[VK_NUMPAD6] = SDLK_KP6;
   328 	VK_keymap[VK_NUMPAD7] = SDLK_KP7;
   329 	VK_keymap[VK_NUMPAD8] = SDLK_KP8;
   330 	VK_keymap[VK_NUMPAD9] = SDLK_KP9;
   331 	VK_keymap[VK_DECIMAL] = SDLK_KP_PERIOD;
   332 	VK_keymap[VK_DIVIDE] = SDLK_KP_DIVIDE;
   333 	VK_keymap[VK_MULTIPLY] = SDLK_KP_MULTIPLY;
   334 	VK_keymap[VK_SUBTRACT] = SDLK_KP_MINUS;
   335 	VK_keymap[VK_ADD] = SDLK_KP_PLUS;
   336 
   337 	VK_keymap[VK_UP] = SDLK_UP;
   338 	VK_keymap[VK_DOWN] = SDLK_DOWN;
   339 	VK_keymap[VK_RIGHT] = SDLK_RIGHT;
   340 	VK_keymap[VK_LEFT] = SDLK_LEFT;
   341 	VK_keymap[VK_INSERT] = SDLK_INSERT;
   342 	VK_keymap[VK_HOME] = SDLK_HOME;
   343 	VK_keymap[VK_END] = SDLK_END;
   344 	VK_keymap[VK_PRIOR] = SDLK_PAGEUP;
   345 	VK_keymap[VK_NEXT] = SDLK_PAGEDOWN;
   346 
   347 	VK_keymap[VK_F1] = SDLK_F1;
   348 	VK_keymap[VK_F2] = SDLK_F2;
   349 	VK_keymap[VK_F3] = SDLK_F3;
   350 	VK_keymap[VK_F4] = SDLK_F4;
   351 	VK_keymap[VK_F5] = SDLK_F5;
   352 	VK_keymap[VK_F6] = SDLK_F6;
   353 	VK_keymap[VK_F7] = SDLK_F7;
   354 	VK_keymap[VK_F8] = SDLK_F8;
   355 	VK_keymap[VK_F9] = SDLK_F9;
   356 	VK_keymap[VK_F10] = SDLK_F10;
   357 	VK_keymap[VK_F11] = SDLK_F11;
   358 	VK_keymap[VK_F12] = SDLK_F12;
   359 	VK_keymap[VK_F13] = SDLK_F13;
   360 	VK_keymap[VK_F14] = SDLK_F14;
   361 	VK_keymap[VK_F15] = SDLK_F15;
   362 
   363 	VK_keymap[VK_NUMLOCK] = SDLK_NUMLOCK;
   364 	VK_keymap[VK_CAPITAL] = SDLK_CAPSLOCK;
   365 	VK_keymap[VK_SCROLL] = SDLK_SCROLLOCK;
   366 	VK_keymap[VK_RSHIFT] = SDLK_RSHIFT;
   367 	VK_keymap[VK_LSHIFT] = SDLK_LSHIFT;
   368 	VK_keymap[VK_RCONTROL] = SDLK_RCTRL;
   369 	VK_keymap[VK_LCONTROL] = SDLK_LCTRL;
   370 	VK_keymap[VK_RMENU] = SDLK_RALT;
   371 	VK_keymap[VK_LMENU] = SDLK_LALT;
   372 	VK_keymap[VK_RWIN] = SDLK_RSUPER;
   373 	VK_keymap[VK_LWIN] = SDLK_LSUPER;
   374 
   375 	VK_keymap[VK_HELP] = SDLK_HELP;
   376 #ifdef VK_PRINT
   377 	VK_keymap[VK_PRINT] = SDLK_PRINT;
   378 #endif
   379 	VK_keymap[VK_SNAPSHOT] = SDLK_PRINT;
   380 	VK_keymap[VK_CANCEL] = SDLK_BREAK;
   381 	VK_keymap[VK_APPS] = SDLK_MENU;
   382 }
   383 
   384 static SDL_keysym *TranslateKey(WPARAM vkey, UINT scancode, SDL_keysym *keysym, int pressed)
   385 {
   386 	/* Set the keysym information */
   387 	keysym->scancode = (unsigned char) scancode;
   388 	keysym->sym = VK_keymap[vkey];
   389 	keysym->mod = KMOD_NONE;
   390 	keysym->unicode = 0;
   391 	if ( pressed && SDL_TranslateUNICODE ) {
   392 #ifdef NO_GETKEYBOARDSTATE
   393 		/* Uh oh, better hope the vkey is close enough.. */
   394 		keysym->unicode = vkey;
   395 #else
   396 		BYTE	keystate[256];
   397 		Uint16	wchars[2];
   398 
   399 		GetKeyboardState(keystate);
   400 		if (SDL_ToUnicode((UINT)vkey, scancode, keystate, wchars, sizeof(wchars)/sizeof(wchars[0]), 0) == 1)
   401 		{
   402 			keysym->unicode = wchars[0];
   403 		}
   404 #endif /* NO_GETKEYBOARDSTATE */
   405 	}
   406 	return(keysym);
   407 }
   408 
   409 int DIB_CreateWindow(_THIS)
   410 {
   411 	char *windowid = SDL_getenv("SDL_WINDOWID");
   412 
   413 	SDL_RegisterApp(NULL, 0, 0);
   414 
   415 	SDL_windowid = (windowid != NULL);
   416 	if ( SDL_windowid ) {
   417 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
   418 		/* wince 2.1 does not have strtol */
   419 		wchar_t *windowid_t = SDL_malloc((SDL_strlen(windowid) + 1) * sizeof(wchar_t));
   420 		MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, windowid, -1, windowid_t, SDL_strlen(windowid) + 1);
   421 		SDL_Window = (HWND)wcstol(windowid_t, NULL, 0);
   422 		SDL_free(windowid_t);
   423 #else
   424 		SDL_Window = (HWND)SDL_strtoull(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 = (WNDPROCTYPE)GetWindowLongPtr(SDL_Window, GWLP_WNDPROC);
   435 		SetWindowLongPtr(SDL_Window, GWLP_WNDPROC, (LONG_PTR)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 
   447 	/* JC 14 Mar 2006
   448 		Flush the message loop or this can cause big problems later
   449 		Especially if the user decides to use dialog boxes or assert()!
   450 	*/
   451 	WIN_FlushMessageQueue();
   452 
   453 	return(0);
   454 }
   455 
   456 void DIB_DestroyWindow(_THIS)
   457 {
   458 	if ( SDL_windowid ) {
   459 		SetWindowLongPtr(SDL_Window, GWLP_WNDPROC, (LONG_PTR)userWindowProc);
   460 	} else {
   461 		DestroyWindow(SDL_Window);
   462 	}
   463 	SDL_UnregisterApp();
   464 
   465 	/* JC 14 Mar 2006
   466 		Flush the message loop or this can cause big problems later
   467 		Especially if the user decides to use dialog boxes or assert()!
   468 	*/
   469 	WIN_FlushMessageQueue();
   470 }