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