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