src/video/windib/SDL_dibevents.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 31 Jan 2006 15:30:42 +0000
changeset 1303 52b5afd7ecee
parent 1288 ea3888b472bf
child 1312 c9b51268668f
permissions -rw-r--r--
Date: Tue, 05 Jul 2005 21:43:26 +1000
From: Sean Childs
Subject: [SDL] Compiling SDL 1.2.8 with the free Borland compiler

When compiling SDL 1.2.8 with the free Borland compiler, I received this
error (there is a similar error that occurs in
src\video\windx5\sdl_dx5events.c):
Error E2342 ..\..\src\video\windib\sdl_dibevents.c 189: Type mismatch in
parameter 'lpPrevWndFunc' (wanted 'int (__stdcall *)()', got 'long
(__stdcall *)(void *,unsigned int,unsigned int,long)') in function
DIB_HandleMessage


I checked the MSDN library at:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/
windowsuserinterface/windowing/windowprocedures/windowprocedurereference/wind
owprocedurefunctions/callwindowproc.asp

and it had this to say:
If STRICT is not defined, the lpPrevWndFunc parameter has the data type
FARPROC. The FARPROC type is declared as follows:

int (FAR WINAPI * FARPROC) ()

In C, the FARPROC declaration indicates a callback function that has an
unspecified parameter list. In C++, however, the empty parameter list in
the declaration indicates that a function has no parameters. This subtle
distinction can break careless code. Following is one way to handle this
situation:

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