src/video/windib/SDL_dibevents.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 16 Feb 2006 10:11:48 +0000
changeset 1361 19418e4422cb
parent 1358 c71e05b4dc2e
child 1379 c0a74f199ecf
permissions -rw-r--r--
New configure-based build system. Still work in progress, but much improved
     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 
    23 #include "SDL_windows.h"
    24 
    25 #include "SDL_main.h"
    26 #include "SDL_events.h"
    27 #include "SDL_syswm.h"
    28 #include "../../events/SDL_sysevents.h"
    29 #include "../../events/SDL_events_c.h"
    30 #include "../wincommon/SDL_lowvideo.h"
    31 #include "SDL_dibvideo.h"
    32 #include "SDL_vkeys.h"
    33 
    34 #ifndef WM_APP
    35 #define WM_APP	0x8000
    36 #endif
    37 
    38 #ifdef _WIN32_WCE
    39 #define NO_GETKEYBOARDSTATE
    40 #endif
    41 
    42 /* The translation table from a Microsoft VK keysym to a SDL keysym */
    43 static SDLKey VK_keymap[SDLK_LAST];
    44 static SDL_keysym *TranslateKey(UINT vkey, UINT scancode, SDL_keysym *keysym, int pressed);
    45 
    46 /* Masks for processing the windows KEYDOWN and KEYUP messages */
    47 #define REPEATED_KEYMASK	(1<<30)
    48 #define EXTENDED_KEYMASK	(1<<24)
    49 
    50 /* DJM: If the user setup the window for us, we want to save his window proc,
    51    and give him a chance to handle some messages. */
    52 #ifdef STRICT
    53 #define WNDPROCTYPE	WNDPROC
    54 #else
    55 #define WNDPROCTYPE	FARPROC
    56 #endif
    57 static WNDPROCTYPE userWindowProc = NULL;
    58 
    59 
    60 #ifdef _WIN32_WCE
    61 
    62 WPARAM rotateKey(WPARAM key,SDL_ScreenOrientation direction) 
    63 {
    64 	if (direction != SDL_ORIENTATION_LEFT)
    65 		return key;
    66 
    67 	switch (key) {
    68 		case 0x26: /* up */
    69 			return 0x27;
    70 		case 0x27: /* right */
    71 			return 0x28;
    72 		case 0x28: /* down */
    73 			return 0x25;
    74 		case 0x25: /* left */
    75 			return 0x26;
    76 	}
    77 
    78 	return key;
    79 }
    80 
    81 #endif 
    82 
    83 
    84 /* The main Win32 event handler */
    85 LONG
    86  DIB_HandleMessage(_THIS, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    87 {
    88 	extern int posted;
    89 
    90 	switch (msg) {
    91 		case WM_SYSKEYDOWN:
    92 		case WM_KEYDOWN: {
    93 			SDL_keysym keysym;
    94 
    95 #ifdef _WIN32_WCE
    96 			// Drop GAPI artefacts
    97 			if (wParam == 0x84 || wParam == 0x5B)
    98 				return 0;
    99 
   100 			// Rotate key if necessary
   101 			if (this->hidden->orientation != SDL_ORIENTATION_UP)
   102 				wParam = rotateKey(wParam, this->hidden->orientation);	
   103 #endif 
   104 			/* Ignore repeated keys */
   105 			if ( lParam&REPEATED_KEYMASK ) {
   106 				return(0);
   107 			}
   108 			switch (wParam) {
   109 				case VK_CONTROL:
   110 					if ( lParam&EXTENDED_KEYMASK )
   111 						wParam = VK_RCONTROL;
   112 					else
   113 						wParam = VK_LCONTROL;
   114 					break;
   115 				case VK_SHIFT:
   116 					/* EXTENDED trick doesn't work here */
   117 					{
   118 					Uint8 *state = SDL_GetKeyState(NULL);
   119 					if (state[SDLK_LSHIFT] == SDL_RELEASED && (GetKeyState(VK_LSHIFT) & 0x8000)) {
   120 						wParam = VK_LSHIFT;
   121 					} else if (state[SDLK_RSHIFT] == SDL_RELEASED && (GetKeyState(VK_RSHIFT) & 0x8000)) {
   122 						wParam = VK_RSHIFT;
   123 					} else {
   124 						/* Probably a key repeat */
   125 						return(0);
   126 					}
   127 					}
   128 					break;
   129 				case VK_MENU:
   130 					if ( lParam&EXTENDED_KEYMASK )
   131 						wParam = VK_RMENU;
   132 					else
   133 						wParam = VK_LMENU;
   134 					break;
   135 			}
   136 #ifdef NO_GETKEYBOARDSTATE
   137 			/* this is the workaround for the missing ToAscii() and ToUnicode() in CE (not necessary at KEYUP!) */
   138 			if ( SDL_TranslateUNICODE ) {
   139 				MSG m;
   140 
   141 				m.hwnd = hwnd;
   142 				m.message = msg;
   143 				m.wParam = wParam;
   144 				m.lParam = lParam;
   145 				m.time = 0;
   146 				if ( TranslateMessage(&m) && PeekMessage(&m, hwnd, 0, WM_USER, PM_NOREMOVE) && (m.message == WM_CHAR) ) {
   147 					GetMessage(&m, hwnd, 0, WM_USER);
   148 			    		wParam = m.wParam;
   149 				}
   150 			}
   151 #endif /* NO_GETKEYBOARDSTATE */
   152 			posted = SDL_PrivateKeyboard(SDL_PRESSED,
   153 				TranslateKey(wParam,HIWORD(lParam),&keysym,1));
   154 		}
   155 		return(0);
   156 
   157 		case WM_SYSKEYUP:
   158 		case WM_KEYUP: {
   159 			SDL_keysym keysym;
   160 
   161 #ifdef _WIN32_WCE
   162 			// Drop GAPI artefacts
   163 			if (wParam == 0x84 || wParam == 0x5B)
   164 				return 0;
   165 
   166 			// Rotate key if necessary
   167 			if (this->hidden->orientation != SDL_ORIENTATION_UP)
   168 				wParam = rotateKey(wParam, this->hidden->orientation);	
   169 #endif
   170 
   171 			switch (wParam) {
   172 				case VK_CONTROL:
   173 					if ( lParam&EXTENDED_KEYMASK )
   174 						wParam = VK_RCONTROL;
   175 					else
   176 						wParam = VK_LCONTROL;
   177 					break;
   178 				case VK_SHIFT:
   179 					/* EXTENDED trick doesn't work here */
   180 					{
   181 					Uint8 *state = SDL_GetKeyState(NULL);
   182 					if (state[SDLK_LSHIFT] == SDL_PRESSED && !(GetKeyState(VK_LSHIFT) & 0x8000)) {
   183 						wParam = VK_LSHIFT;
   184 					} else if (state[SDLK_RSHIFT] == SDL_PRESSED && !(GetKeyState(VK_RSHIFT) & 0x8000)) {
   185 						wParam = VK_RSHIFT;
   186 					} else {
   187 						/* Probably a key repeat */
   188 						return(0);
   189 					}
   190 					}
   191 					break;
   192 				case VK_MENU:
   193 					if ( lParam&EXTENDED_KEYMASK )
   194 						wParam = VK_RMENU;
   195 					else
   196 						wParam = VK_LMENU;
   197 					break;
   198 			}
   199 			posted = SDL_PrivateKeyboard(SDL_RELEASED,
   200 				TranslateKey(wParam,HIWORD(lParam),&keysym,0));
   201 		}
   202 		return(0);
   203 
   204 #if defined(SC_SCREENSAVE) && defined(SC_MONITORPOWER)
   205 		case WM_SYSCOMMAND: {
   206 			if ((wParam&0xFFF0)==SC_SCREENSAVE ||
   207 				(wParam&0xFFF0)==SC_MONITORPOWER)
   208 					return(0);
   209 		}
   210 		/* Fall through to default processing */
   211 #endif /* SC_SCREENSAVE && SC_MONITORPOWER */
   212 
   213 		default: {
   214 			/* Only post the event if we're watching for it */
   215 			if ( SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE ) {
   216 			        SDL_SysWMmsg wmmsg;
   217 
   218 				SDL_VERSION(&wmmsg.version);
   219 				wmmsg.hwnd = hwnd;
   220 				wmmsg.msg = msg;
   221 				wmmsg.wParam = wParam;
   222 				wmmsg.lParam = lParam;
   223 				posted = SDL_PrivateSysWMEvent(&wmmsg);
   224 
   225 			/* DJM: If the user isn't watching for private
   226 				messages in her SDL event loop, then pass it
   227 				along to any win32 specific window proc.
   228 			 */
   229 			} else if (userWindowProc) {
   230 				return CallWindowProc(userWindowProc, hwnd, msg, wParam, lParam);
   231 			}
   232 		}
   233 		break;
   234 	}
   235 	return(DefWindowProc(hwnd, msg, wParam, lParam));
   236 }
   237 
   238 void DIB_PumpEvents(_THIS)
   239 {
   240 	MSG msg;
   241 
   242 	while ( PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) ) {
   243 		if ( GetMessage(&msg, NULL, 0, 0) > 0 ) {
   244 			DispatchMessage(&msg);
   245 		}
   246 	}
   247 }
   248 
   249 void DIB_InitOSKeymap(_THIS)
   250 {
   251 	int i;
   252 
   253 	/* Map the VK keysyms */
   254 	for ( i=0; i<SDL_TABLESIZE(VK_keymap); ++i )
   255 		VK_keymap[i] = SDLK_UNKNOWN;
   256 
   257 	VK_keymap[VK_BACK] = SDLK_BACKSPACE;
   258 	VK_keymap[VK_TAB] = SDLK_TAB;
   259 	VK_keymap[VK_CLEAR] = SDLK_CLEAR;
   260 	VK_keymap[VK_RETURN] = SDLK_RETURN;
   261 	VK_keymap[VK_PAUSE] = SDLK_PAUSE;
   262 	VK_keymap[VK_ESCAPE] = SDLK_ESCAPE;
   263 	VK_keymap[VK_SPACE] = SDLK_SPACE;
   264 	VK_keymap[VK_APOSTROPHE] = SDLK_QUOTE;
   265 	VK_keymap[VK_COMMA] = SDLK_COMMA;
   266 	VK_keymap[VK_MINUS] = SDLK_MINUS;
   267 	VK_keymap[VK_PERIOD] = SDLK_PERIOD;
   268 	VK_keymap[VK_SLASH] = SDLK_SLASH;
   269 	VK_keymap[VK_0] = SDLK_0;
   270 	VK_keymap[VK_1] = SDLK_1;
   271 	VK_keymap[VK_2] = SDLK_2;
   272 	VK_keymap[VK_3] = SDLK_3;
   273 	VK_keymap[VK_4] = SDLK_4;
   274 	VK_keymap[VK_5] = SDLK_5;
   275 	VK_keymap[VK_6] = SDLK_6;
   276 	VK_keymap[VK_7] = SDLK_7;
   277 	VK_keymap[VK_8] = SDLK_8;
   278 	VK_keymap[VK_9] = SDLK_9;
   279 	VK_keymap[VK_SEMICOLON] = SDLK_SEMICOLON;
   280 	VK_keymap[VK_EQUALS] = SDLK_EQUALS;
   281 	VK_keymap[VK_LBRACKET] = SDLK_LEFTBRACKET;
   282 	VK_keymap[VK_BACKSLASH] = SDLK_BACKSLASH;
   283 	VK_keymap[VK_OEM_102] = SDLK_LESS;
   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 
   378 static SDL_keysym *TranslateKey(UINT vkey, UINT scancode, SDL_keysym *keysym, int pressed)
   379 {
   380 	/* Set the keysym information */
   381 	keysym->scancode = (unsigned char) scancode;
   382 	keysym->sym = VK_keymap[vkey];
   383 	keysym->mod = KMOD_NONE;
   384 	keysym->unicode = 0;
   385 	if ( pressed && SDL_TranslateUNICODE ) {
   386 #ifdef NO_GETKEYBOARDSTATE
   387 		/* Uh oh, better hope the vkey is close enough.. */
   388 		keysym->unicode = vkey;
   389 #else
   390 		BYTE	keystate[256];
   391 		Uint16	wchars[2];
   392 
   393 		GetKeyboardState(keystate);
   394 		if (SDL_ToUnicode(vkey, scancode, keystate, wchars, sizeof(wchars)/sizeof(wchars[0]), 0) == 1)
   395 		{
   396 			keysym->unicode = wchars[0];
   397 		}
   398 #endif /* NO_GETKEYBOARDSTATE */
   399 	}
   400 	return(keysym);
   401 }
   402 
   403 int DIB_CreateWindow(_THIS)
   404 {
   405 	char *windowid = SDL_getenv("SDL_WINDOWID");
   406 
   407 	SDL_RegisterApp(NULL, 0, 0);
   408 
   409 	SDL_windowid = (windowid != NULL);
   410 	if ( SDL_windowid ) {
   411 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
   412 		/* wince 2.1 does not have strtol */
   413 		wchar_t *windowid_t = SDL_malloc((SDL_strlen(windowid) + 1) * sizeof(wchar_t));
   414 		MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, windowid, -1, windowid_t, SDL_strlen(windowid) + 1);
   415 		SDL_Window = (HWND)wcstol(windowid_t, NULL, 0);
   416 		SDL_free(windowid_t);
   417 #else
   418 		SDL_Window = (HWND)SDL_strtol(windowid, NULL, 0);
   419 #endif
   420 		if ( SDL_Window == NULL ) {
   421 			SDL_SetError("Couldn't get user specified window");
   422 			return(-1);
   423 		}
   424 
   425 		/* DJM: we want all event's for the user specified
   426 			window to be handled by SDL.
   427 		 */
   428 		userWindowProc = (WNDPROCTYPE)GetWindowLong(SDL_Window, GWL_WNDPROC);
   429 		SetWindowLong(SDL_Window, GWL_WNDPROC, (LONG)WinMessage);
   430 	} else {
   431 		SDL_Window = CreateWindow(SDL_Appname, SDL_Appname,
   432                         (WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX),
   433                         CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, NULL, NULL, SDL_Instance, NULL);
   434 		if ( SDL_Window == NULL ) {
   435 			SDL_SetError("Couldn't create window");
   436 			return(-1);
   437 		}
   438 		ShowWindow(SDL_Window, SW_HIDE);
   439 	}
   440 	return(0);
   441 }
   442 
   443 void DIB_DestroyWindow(_THIS)
   444 {
   445 	if ( SDL_windowid ) {
   446 		SetWindowLong(SDL_Window, GWL_WNDPROC, (LONG)userWindowProc);
   447 	} else {
   448 		DestroyWindow(SDL_Window);
   449 	}
   450 	SDL_UnregisterApp();
   451 }