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