src/video/windx5/SDL_dx5events.c
author Sam Lantinga
Tue, 14 Mar 2006 07:06:06 +0000
changeset 1526 89f90f32f6c0
parent 1523 21b1fbb53f4a
child 1658 e49147870aac
child 1852 eb2d5480ae95
permissions -rw-r--r--
Fixed bug #124
Print Screen key events weren't reported on Windows
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
/* CAUTION!!!!  If you modify this file, check ../windib/SDL_sysevents.c */
slouken@0
    25
slouken@0
    26
#include "directx.h"
slouken@0
    27
slouken@1358
    28
#include "SDL_main.h"
slouken@0
    29
#include "SDL_events.h"
slouken@0
    30
#include "SDL_video.h"
slouken@0
    31
#include "SDL_syswm.h"
slouken@1361
    32
#include "../../events/SDL_sysevents.h"
slouken@1361
    33
#include "../../events/SDL_events_c.h"
slouken@1361
    34
#include "../wincommon/SDL_lowvideo.h"
slouken@0
    35
#include "SDL_dx5video.h"
slouken@0
    36
slouken@0
    37
#ifndef WM_APP
slouken@0
    38
#define WM_APP	0x8000
slouken@0
    39
#endif
slouken@0
    40
slouken@453
    41
#ifdef _WIN32_WCE
slouken@453
    42
#define NO_GETKEYBOARDSTATE
slouken@453
    43
#endif
slouken@453
    44
slouken@0
    45
/* The keyboard and mouse device input */
slouken@0
    46
#define MAX_INPUTS	16		/* Maximum of 16-1 input devices */
slouken@536
    47
#define INPUT_QSIZE	512		/* Buffer up to 512 input messages */
slouken@0
    48
slouken@0
    49
static LPDIRECTINPUT dinput = NULL;
slouken@0
    50
static LPDIRECTINPUTDEVICE2 SDL_DIdev[MAX_INPUTS];
slouken@0
    51
static HANDLE               SDL_DIevt[MAX_INPUTS];
slouken@0
    52
static void (*SDL_DIfun[MAX_INPUTS])(const int, DIDEVICEOBJECTDATA *);
slouken@0
    53
static int SDL_DIndev = 0;
slouken@0
    54
static int mouse_lost;
slouken@0
    55
static int mouse_pressed;
slouken@491
    56
static int mouse_buttons_swapped = 0;
slouken@0
    57
slouken@0
    58
/* The translation table from a DirectInput scancode to an SDL keysym */
slouken@0
    59
static SDLKey DIK_keymap[256];
slouken@0
    60
static SDL_keysym *TranslateKey(UINT scancode, SDL_keysym *keysym, int pressed);
slouken@0
    61
slouken@145
    62
/* DJM: If the user setup the window for us, we want to save his window proc,
slouken@145
    63
   and give him a chance to handle some messages. */
slouken@1303
    64
#ifdef STRICT
slouken@1303
    65
#define WNDPROCTYPE	WNDPROC
slouken@1303
    66
#else
slouken@1303
    67
#define WNDPROCTYPE	FARPROC
slouken@1303
    68
#endif
slouken@1303
    69
static WNDPROCTYPE userWindowProc = NULL;
slouken@145
    70
icculus@1114
    71
static HWND GetTopLevelParent(HWND hWnd)
icculus@1114
    72
{
icculus@1114
    73
    HWND hParentWnd;
icculus@1114
    74
    while (1)
icculus@1114
    75
    {
icculus@1114
    76
        hParentWnd = GetParent(hWnd);
icculus@1114
    77
        if (hParentWnd == NULL)
icculus@1114
    78
            break;
icculus@1114
    79
        hWnd = hParentWnd;
icculus@1114
    80
    }
icculus@1114
    81
    return hWnd;
icculus@1114
    82
}
icculus@1114
    83
slouken@0
    84
/* Convert a DirectInput return code to a text message */
slouken@0
    85
static void SetDIerror(char *function, int code)
slouken@0
    86
{
slouken@0
    87
	static char *error;
slouken@453
    88
	static char  errbuf[1024];
slouken@0
    89
slouken@0
    90
	errbuf[0] = 0;
slouken@0
    91
	switch (code) {
slouken@0
    92
                case DIERR_GENERIC:
slouken@0
    93
                        error = "Undefined error!";
slouken@0
    94
                        break;
slouken@0
    95
		case DIERR_OLDDIRECTINPUTVERSION:
slouken@0
    96
			error = "Your version of DirectInput needs upgrading";
slouken@0
    97
			break;
slouken@0
    98
		case DIERR_INVALIDPARAM:
slouken@0
    99
                        error = "Invalid parameters";
slouken@0
   100
                        break;
slouken@0
   101
                case DIERR_OUTOFMEMORY:
slouken@0
   102
                        error = "Out of memory";
slouken@0
   103
                        break;
slouken@0
   104
		case DIERR_DEVICENOTREG:
slouken@0
   105
			error = "Device not registered";
slouken@0
   106
			break;
slouken@0
   107
		case DIERR_NOINTERFACE:
slouken@0
   108
			error = "Interface not supported";
slouken@0
   109
			break;
slouken@0
   110
		case DIERR_NOTINITIALIZED:
slouken@0
   111
			error = "Device not initialized";
slouken@0
   112
			break;
slouken@0
   113
		default:
slouken@1336
   114
			SDL_snprintf(errbuf, SDL_arraysize(errbuf),
slouken@1330
   115
			         "%s: Unknown DirectInput error: 0x%x",
slouken@0
   116
								function, code);
slouken@0
   117
			break;
slouken@0
   118
	}
slouken@0
   119
	if ( ! errbuf[0] ) {
slouken@1336
   120
		SDL_snprintf(errbuf, SDL_arraysize(errbuf), "%s: %s", function, error);
slouken@0
   121
	}
slouken@0
   122
	SDL_SetError("%s", errbuf);
slouken@0
   123
	return;
slouken@0
   124
}
slouken@0
   125
slouken@0
   126
/* Initialize DirectInput
slouken@0
   127
   Note:  If NONEXCLUSIVE access is requested for the devices, normal 
slouken@0
   128
          windows input messages will continue to be generated for that
slouken@0
   129
          input device, in addition to DirectInput messages.
slouken@0
   130
 */
slouken@0
   131
static void handle_keyboard(const int numevents, DIDEVICEOBJECTDATA *events);
slouken@0
   132
static void handle_mouse(const int numevents, DIDEVICEOBJECTDATA *events);
slouken@0
   133
struct {
slouken@0
   134
	char *name;
slouken@0
   135
	REFGUID guid;
slouken@0
   136
	LPCDIDATAFORMAT format;
slouken@0
   137
	DWORD win_level;
slouken@0
   138
	DWORD raw_level;
slouken@0
   139
	void (*fun)(const int numevents, DIDEVICEOBJECTDATA *events);
slouken@0
   140
} inputs[] = {
slouken@0
   141
	{ "keyboard",
slouken@0
   142
		&GUID_SysKeyboard, &c_dfDIKeyboard,
slouken@0
   143
		(DISCL_FOREGROUND|DISCL_NONEXCLUSIVE),
slouken@0
   144
		(DISCL_FOREGROUND|DISCL_NONEXCLUSIVE), handle_keyboard },
slouken@0
   145
	{ "mouse",
slouken@0
   146
		&GUID_SysMouse, &c_dfDIMouse,
slouken@0
   147
		(DISCL_FOREGROUND|DISCL_NONEXCLUSIVE),
slouken@0
   148
		(DISCL_FOREGROUND|DISCL_EXCLUSIVE), handle_mouse },
slouken@0
   149
	{ NULL, NULL, NULL, 0, 0, NULL }
slouken@0
   150
};
slouken@0
   151
	
slouken@0
   152
static int DX5_DInputInit(_THIS)
slouken@0
   153
{
slouken@0
   154
	int         i;
slouken@0
   155
	LPDIRECTINPUTDEVICE device;
slouken@0
   156
	HRESULT     result;
slouken@0
   157
	DIPROPDWORD dipdw;
slouken@970
   158
	HWND        topwnd;
slouken@0
   159
slouken@0
   160
	/* Create the DirectInput object */
slouken@0
   161
	result = DInputCreate(SDL_Instance, DIRECTINPUT_VERSION,
slouken@0
   162
							&dinput, NULL);
slouken@0
   163
	if ( result != DI_OK ) {
slouken@0
   164
		SetDIerror("DirectInputCreate", result);
slouken@0
   165
		return(-1);
slouken@0
   166
	}
slouken@0
   167
slouken@0
   168
	/* Create all of our registered input devices */
slouken@0
   169
	SDL_DIndev = 0;
slouken@0
   170
	for ( i=0; inputs[i].name; ++i ) {
slouken@0
   171
		/* Create the DirectInput device */
slouken@0
   172
		result = IDirectInput_CreateDevice(dinput, inputs[i].guid,
slouken@0
   173
								&device, NULL);
slouken@0
   174
		if ( result != DI_OK ) {
slouken@0
   175
			SetDIerror("DirectInput::CreateDevice", result);
slouken@0
   176
			return(-1);
slouken@0
   177
		}
slouken@0
   178
		result = IDirectInputDevice_QueryInterface(device,
slouken@0
   179
			&IID_IDirectInputDevice2, (LPVOID *)&SDL_DIdev[i]);
slouken@0
   180
		IDirectInputDevice_Release(device);
slouken@0
   181
		if ( result != DI_OK ) {
slouken@0
   182
			SetDIerror("DirectInputDevice::QueryInterface", result);
slouken@0
   183
			return(-1);
slouken@0
   184
		}
icculus@1115
   185
		topwnd =  GetTopLevelParent(SDL_Window);
slouken@0
   186
		result = IDirectInputDevice2_SetCooperativeLevel(SDL_DIdev[i],
slouken@970
   187
					topwnd, inputs[i].win_level);
slouken@0
   188
		if ( result != DI_OK ) {
slouken@0
   189
			SetDIerror("DirectInputDevice::SetCooperativeLevel",
slouken@0
   190
									result);
slouken@0
   191
			return(-1);
slouken@0
   192
		}
slouken@0
   193
		result = IDirectInputDevice2_SetDataFormat(SDL_DIdev[i],
slouken@0
   194
							inputs[i].format);
slouken@0
   195
		if ( result != DI_OK ) {
slouken@0
   196
			SetDIerror("DirectInputDevice::SetDataFormat", result);
slouken@0
   197
			return(-1);
slouken@0
   198
		}
slouken@0
   199
slouken@0
   200
		/* Set buffered input -- we aren't polling */
slouken@1336
   201
		SDL_memset(&dipdw, 0, sizeof(dipdw));
slouken@0
   202
		dipdw.diph.dwSize = sizeof(dipdw);
slouken@0
   203
		dipdw.diph.dwHeaderSize = sizeof(dipdw.diph);
slouken@0
   204
		dipdw.diph.dwObj = 0;
slouken@0
   205
		dipdw.diph.dwHow = DIPH_DEVICE;
slouken@0
   206
		dipdw.dwData = INPUT_QSIZE;
slouken@0
   207
		result = IDirectInputDevice2_SetProperty(SDL_DIdev[i],
slouken@0
   208
						DIPROP_BUFFERSIZE, &dipdw.diph);
slouken@0
   209
		if ( result != DI_OK ) {
slouken@0
   210
			SetDIerror("DirectInputDevice::SetProperty", result);
slouken@0
   211
			return(-1);
slouken@0
   212
		}
slouken@0
   213
slouken@0
   214
		/* Create an event to be signaled when input is ready */
slouken@0
   215
		SDL_DIevt[i] = CreateEvent(NULL, FALSE, FALSE, NULL);
slouken@0
   216
		if ( SDL_DIevt[i] == NULL ) {
slouken@0
   217
			SDL_SetError("Couldn't create DirectInput event");
slouken@0
   218
			return(-1);
slouken@0
   219
		}
slouken@0
   220
		result = IDirectInputDevice2_SetEventNotification(SDL_DIdev[i],
slouken@0
   221
								SDL_DIevt[i]);
slouken@0
   222
		if ( result != DI_OK ) {
slouken@0
   223
			SetDIerror("DirectInputDevice::SetEventNotification",
slouken@0
   224
									result);
slouken@0
   225
			return(-1);
slouken@0
   226
		}
slouken@0
   227
		SDL_DIfun[i] = inputs[i].fun;
slouken@0
   228
slouken@0
   229
		/* Acquire the device for input */
slouken@0
   230
		IDirectInputDevice2_Acquire(SDL_DIdev[i]);
slouken@0
   231
slouken@0
   232
		/* Increment the number of devices we have */
slouken@0
   233
		++SDL_DIndev;
slouken@0
   234
	}
slouken@0
   235
	mouse_pressed = 0;
slouken@491
   236
	mouse_buttons_swapped = GetSystemMetrics(SM_SWAPBUTTON);
slouken@0
   237
slouken@0
   238
	/* DirectInput is ready! */
slouken@0
   239
	return(0);
slouken@0
   240
}
slouken@0
   241
slouken@0
   242
/* Clean up DirectInput */
slouken@0
   243
static void DX5_DInputQuit(_THIS)
slouken@0
   244
{
slouken@0
   245
	int i;
slouken@0
   246
slouken@0
   247
	if ( dinput != NULL ) {
slouken@0
   248
		/* Close and release all DirectInput devices */
slouken@0
   249
		for ( i=0; i<MAX_INPUTS; ++i ) {
slouken@0
   250
			if ( SDL_DIdev[i] != NULL ) {
slouken@0
   251
				IDirectInputDevice2_Unacquire(SDL_DIdev[i]);
slouken@0
   252
				IDirectInputDevice2_SetEventNotification(
slouken@0
   253
							SDL_DIdev[i], NULL);
slouken@0
   254
				if ( SDL_DIevt[i] != NULL ) {
slouken@0
   255
					CloseHandle(SDL_DIevt[i]);
slouken@0
   256
					SDL_DIevt[i] = NULL;
slouken@0
   257
				}
slouken@0
   258
				IDirectInputDevice2_Release(SDL_DIdev[i]);
slouken@0
   259
				SDL_DIdev[i] = NULL;
slouken@0
   260
			}
slouken@0
   261
		}
slouken@0
   262
		/* Release DirectInput */
slouken@0
   263
		IDirectInput_Release(dinput);
slouken@0
   264
		dinput = NULL;
slouken@0
   265
	}
slouken@0
   266
}
slouken@0
   267
slouken@0
   268
/* Flag to tell SDL whether or not we queued an event */
slouken@0
   269
static int posted = 0;
slouken@0
   270
slouken@0
   271
/* Input event handler functions */
slouken@0
   272
static void handle_keyboard(const int numevents, DIDEVICEOBJECTDATA *keybuf)
slouken@0
   273
{
slouken@0
   274
	int i;
slouken@0
   275
	SDL_keysym keysym;
slouken@0
   276
slouken@0
   277
	/* Translate keyboard messages */
slouken@0
   278
	for ( i=0; i<numevents; ++i ) {
slouken@0
   279
		if ( keybuf[i].dwData & 0x80 ) {
slouken@0
   280
			posted = SDL_PrivateKeyboard(SDL_PRESSED,
slouken@0
   281
				    TranslateKey(keybuf[i].dwOfs, &keysym, 1));
slouken@0
   282
		} else {
slouken@0
   283
			posted = SDL_PrivateKeyboard(SDL_RELEASED,
slouken@0
   284
				    TranslateKey(keybuf[i].dwOfs, &keysym, 0));
slouken@0
   285
		}
slouken@0
   286
	}
slouken@0
   287
}
slouken@0
   288
static void handle_mouse(const int numevents, DIDEVICEOBJECTDATA *ptrbuf)
slouken@0
   289
{
slouken@0
   290
	int i;
slouken@0
   291
	Sint16 xrel, yrel;
slouken@0
   292
	Uint8 state;
slouken@0
   293
	Uint8 button;
slouken@536
   294
	DWORD timestamp = 0;
slouken@0
   295
icculus@717
   296
	/* Sanity check. Mailing list reports this being NULL unexpectedly. */
icculus@717
   297
	if (SDL_PublicSurface == NULL) {
icculus@717
   298
		return;
icculus@717
   299
	}
icculus@717
   300
slouken@0
   301
	/* If we are in windowed mode, Windows is taking care of the mouse */
slouken@325
   302
	if (  (SDL_PublicSurface->flags & SDL_OPENGL) ||
slouken@325
   303
	     !(SDL_PublicSurface->flags & SDL_FULLSCREEN) ) {
slouken@0
   304
		return;
slouken@0
   305
	}
slouken@0
   306
slouken@0
   307
	/* If the mouse was lost, regain some sense of mouse state */
slouken@0
   308
	if ( mouse_lost ) {
slouken@0
   309
		POINT mouse_pos;
slouken@0
   310
		Uint8 old_state;
slouken@0
   311
		Uint8 new_state;
slouken@0
   312
slouken@0
   313
		/* Set ourselves up with the current cursor position */
slouken@0
   314
		GetCursorPos(&mouse_pos);
slouken@0
   315
		ScreenToClient(SDL_Window, &mouse_pos);
slouken@0
   316
		posted = SDL_PrivateMouseMotion(0, 0,
slouken@0
   317
				(Sint16)mouse_pos.x, (Sint16)mouse_pos.y);
slouken@0
   318
slouken@0
   319
		/* Check for mouse button changes */
slouken@0
   320
		old_state = SDL_GetMouseState(NULL, NULL);
slouken@0
   321
		new_state = 0;
slouken@0
   322
		{ /* Get the new DirectInput button state for the mouse */
slouken@0
   323
			DIMOUSESTATE distate;
slouken@0
   324
			HRESULT result;
slouken@0
   325
slouken@0
   326
			result=IDirectInputDevice2_GetDeviceState(SDL_DIdev[1],
slouken@0
   327
						sizeof(distate), &distate);
slouken@0
   328
			if ( result != DI_OK ) {
slouken@0
   329
				/* Try again next time */
slouken@0
   330
				SetDIerror(
slouken@0
   331
				"IDirectInputDevice2::GetDeviceState", result);
slouken@0
   332
				return;
slouken@0
   333
			}
slouken@0
   334
			for ( i=3; i>=0; --i ) {
slouken@0
   335
				if ( (distate.rgbButtons[i]&0x80) == 0x80 ) {
slouken@0
   336
					new_state |= 0x01;
slouken@0
   337
				}
slouken@0
   338
				new_state <<= 1;
slouken@0
   339
			}
slouken@0
   340
		}
slouken@0
   341
		for ( i=0; i<8; ++i ) {
slouken@0
   342
			if ( (old_state&0x01) != (new_state&0x01) ) {
slouken@0
   343
				button = (Uint8)(i+1);
slouken@0
   344
				/* Button #2 on two button mice is button 3
slouken@0
   345
				   (the middle button is button 2)
slouken@0
   346
				 */
slouken@0
   347
				if ( button == 2 ) {
slouken@0
   348
					button = 3;
slouken@0
   349
				} else
slouken@0
   350
				if ( button == 3 ) {
slouken@0
   351
					button = 2;
slouken@0
   352
				}
slouken@0
   353
				if ( new_state & 0x01 ) {
slouken@0
   354
					/* Grab mouse so we get mouse-up */
slouken@0
   355
					if ( ++mouse_pressed > 0 ) {
slouken@0
   356
						SetCapture(SDL_Window);
slouken@0
   357
					}
slouken@0
   358
					state = SDL_PRESSED;
slouken@0
   359
				} else {
slouken@0
   360
					/* Release mouse after all mouse-ups */
slouken@0
   361
					if ( --mouse_pressed <= 0 ) {
slouken@0
   362
						ReleaseCapture();
slouken@0
   363
						mouse_pressed = 0;
slouken@0
   364
					}
slouken@0
   365
					state = SDL_RELEASED;
slouken@0
   366
				}
slouken@491
   367
				if ( mouse_buttons_swapped ) {
slouken@491
   368
					if ( button == 1 ) button = 3;
slouken@491
   369
					else
slouken@491
   370
					if ( button == 3 ) button = 1;
slouken@491
   371
				}
slouken@0
   372
				posted = SDL_PrivateMouseButton(state, button,
slouken@0
   373
									0, 0);
slouken@0
   374
			}
slouken@0
   375
			old_state >>= 1;
slouken@0
   376
			new_state >>= 1;
slouken@0
   377
		}
slouken@0
   378
		mouse_lost = 0;
slouken@0
   379
		return;
slouken@0
   380
	}
slouken@0
   381
slouken@0
   382
	/* Translate mouse messages */
slouken@0
   383
	xrel = 0;
slouken@0
   384
	yrel = 0;
slouken@0
   385
	for ( i=0; i<(int)numevents; ++i ) {
slouken@0
   386
		switch (ptrbuf[i].dwOfs) {
slouken@0
   387
			case DIMOFS_X:
slouken@536
   388
				if ( timestamp != ptrbuf[i].dwTimeStamp ) {
slouken@536
   389
					if ( xrel || yrel ) {
slouken@536
   390
						posted = SDL_PrivateMouseMotion(
slouken@536
   391
								0, 1, xrel, yrel);
slouken@536
   392
						xrel = 0;
slouken@536
   393
						yrel = 0;
slouken@536
   394
					}
slouken@536
   395
					timestamp = ptrbuf[i].dwTimeStamp;
slouken@536
   396
				}
slouken@0
   397
				xrel += (Sint16)ptrbuf[i].dwData;
slouken@0
   398
				break;
slouken@0
   399
			case DIMOFS_Y:
slouken@536
   400
				if ( timestamp != ptrbuf[i].dwTimeStamp ) {
slouken@536
   401
					if ( xrel || yrel ) {
slouken@536
   402
						posted = SDL_PrivateMouseMotion(
slouken@536
   403
								0, 1, xrel, yrel);
slouken@536
   404
						xrel = 0;
slouken@536
   405
						yrel = 0;
slouken@536
   406
					}
slouken@536
   407
					timestamp = ptrbuf[i].dwTimeStamp;
slouken@536
   408
				}
slouken@0
   409
				yrel += (Sint16)ptrbuf[i].dwData;
slouken@0
   410
				break;
slouken@61
   411
			case DIMOFS_Z:
slouken@61
   412
				if ( xrel || yrel ) {
slouken@61
   413
					posted = SDL_PrivateMouseMotion(
slouken@61
   414
							0, 1, xrel, yrel);
slouken@61
   415
					xrel = 0;
slouken@61
   416
					yrel = 0;
slouken@61
   417
				}
slouken@536
   418
				timestamp = 0;
slouken@61
   419
				if((int)ptrbuf[i].dwData > 0)
slouken@460
   420
					button = SDL_BUTTON_WHEELUP;
slouken@332
   421
				else
slouken@460
   422
					button = SDL_BUTTON_WHEELDOWN;
slouken@460
   423
				posted = SDL_PrivateMouseButton(
slouken@332
   424
						SDL_PRESSED, button, 0, 0);
slouken@332
   425
				posted |= SDL_PrivateMouseButton(
slouken@332
   426
						SDL_RELEASED, button, 0, 0);
slouken@61
   427
				break;
slouken@0
   428
			case DIMOFS_BUTTON0:
slouken@0
   429
			case DIMOFS_BUTTON1:
slouken@0
   430
			case DIMOFS_BUTTON2:
slouken@0
   431
			case DIMOFS_BUTTON3:
slouken@0
   432
				if ( xrel || yrel ) {
slouken@0
   433
					posted = SDL_PrivateMouseMotion(
slouken@0
   434
							0, 1, xrel, yrel);
slouken@0
   435
					xrel = 0;
slouken@0
   436
					yrel = 0;
slouken@0
   437
				}
slouken@536
   438
				timestamp = 0;
slouken@0
   439
				button = (Uint8)(ptrbuf[i].dwOfs-DIMOFS_BUTTON0)+1;
slouken@0
   440
				/* Button #2 on two button mice is button 3
slouken@0
   441
				   (the middle button is button 2)
slouken@0
   442
				 */
slouken@0
   443
				if ( button == 2 ) {
slouken@0
   444
					button = 3;
slouken@0
   445
				} else
slouken@0
   446
				if ( button == 3 ) {
slouken@0
   447
					button = 2;
slouken@0
   448
				}
slouken@0
   449
				if ( ptrbuf[i].dwData & 0x80 ) {
slouken@0
   450
					/* Grab mouse so we get mouse-up */
slouken@0
   451
					if ( ++mouse_pressed > 0 ) {
slouken@0
   452
						SetCapture(SDL_Window);
slouken@0
   453
					}
slouken@0
   454
					state = SDL_PRESSED;
slouken@0
   455
				} else {
slouken@0
   456
					/* Release mouse after all mouse-ups */
slouken@0
   457
					if ( --mouse_pressed <= 0 ) {
slouken@0
   458
						ReleaseCapture();
slouken@0
   459
						mouse_pressed = 0;
slouken@0
   460
					}
slouken@0
   461
					state = SDL_RELEASED;
slouken@0
   462
				}
slouken@491
   463
				if ( mouse_buttons_swapped ) {
slouken@491
   464
					if ( button == 1 ) button = 3;
slouken@491
   465
					else
slouken@491
   466
					if ( button == 3 ) button = 1;
slouken@491
   467
				}
slouken@0
   468
				posted = SDL_PrivateMouseButton(state, button,
slouken@0
   469
									0, 0);
slouken@0
   470
				break;
slouken@0
   471
		}
slouken@0
   472
	}
slouken@0
   473
	if ( xrel || yrel ) {
slouken@0
   474
		posted = SDL_PrivateMouseMotion( 0, 1, xrel, yrel);
slouken@0
   475
	}
slouken@0
   476
}
slouken@0
   477
slouken@0
   478
/* The main Win32 event handler */
slouken@1456
   479
LRESULT DX5_HandleMessage(_THIS, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
slouken@0
   480
{
slouken@0
   481
	switch (msg) {
slouken@453
   482
#ifdef WM_ACTIVATEAPP
slouken@0
   483
		case WM_ACTIVATEAPP: {
slouken@0
   484
			int i, active;
slouken@0
   485
slouken@0
   486
			active = (wParam && (GetForegroundWindow() == hwnd));
slouken@0
   487
			if ( active ) {
slouken@0
   488
				for ( i=0; SDL_DIdev[i]; ++i ) {
slouken@0
   489
					IDirectInputDevice2_Acquire(
slouken@0
   490
								SDL_DIdev[i]);
slouken@0
   491
				}
slouken@0
   492
			} else {
slouken@0
   493
				for ( i=0; SDL_DIdev[i]; ++i ) {
slouken@0
   494
					IDirectInputDevice2_Unacquire(
slouken@0
   495
								SDL_DIdev[i]);
slouken@0
   496
				}
slouken@0
   497
				mouse_lost = 1;
slouken@0
   498
			}
slouken@0
   499
		}
slouken@0
   500
		break;
slouken@453
   501
#endif /* WM_ACTIVATEAPP */
slouken@0
   502
slouken@453
   503
#ifdef WM_DISPLAYCHANGE
slouken@0
   504
		case WM_DISPLAYCHANGE: {
slouken@1456
   505
			WPARAM BitsPerPixel;
slouken@0
   506
			WORD SizeX, SizeY;
slouken@0
   507
slouken@0
   508
			/* Ack!  The display changed size and/or depth! */
slouken@0
   509
			SizeX = LOWORD(lParam);
slouken@0
   510
			SizeY = HIWORD(lParam);
slouken@0
   511
			BitsPerPixel = wParam;
slouken@0
   512
			/* We cause this message when we go fullscreen */
slouken@0
   513
		}
slouken@0
   514
		break;
slouken@453
   515
#endif /* WM_DISPLAYCHANGE */
slouken@0
   516
slouken@0
   517
		/* The keyboard is handled via DirectInput */
slouken@0
   518
		case WM_SYSKEYUP:
slouken@1348
   519
		case WM_SYSKEYDOWN: {
slouken@1348
   520
			/* Pass syskey to DefWindwoProc (ALT-F4, etc.) */
slouken@1348
   521
		}
slouken@1348
   522
		break;
slouken@0
   523
		case WM_KEYUP:
slouken@0
   524
		case WM_KEYDOWN: {
slouken@0
   525
			/* Ignore windows keyboard messages */;
slouken@0
   526
		}
slouken@0
   527
		return(0);
slouken@0
   528
slouken@453
   529
#if defined(SC_SCREENSAVE) || defined(SC_MONITORPOWER)
slouken@0
   530
		/* Don't allow screen savers or monitor power downs.
slouken@0
   531
		   This is because they quietly clear DirectX surfaces.
slouken@0
   532
		   It would be better to allow the application to
slouken@0
   533
		   decide whether or not to blow these off, but the
slouken@0
   534
		   semantics of SDL_PrivateSysWMEvent() don't allow
slouken@0
   535
		   the application that choice.
slouken@0
   536
		 */
slouken@0
   537
		case WM_SYSCOMMAND: {
slouken@0
   538
			if ((wParam&0xFFF0)==SC_SCREENSAVE || 
slouken@0
   539
			    (wParam&0xFFF0)==SC_MONITORPOWER)
slouken@0
   540
				return(0);
slouken@0
   541
		}
slouken@453
   542
		/* Fall through to default processing */
slouken@453
   543
slouken@453
   544
#endif /* SC_SCREENSAVE || SC_MONITORPOWER */
slouken@0
   545
slouken@0
   546
		default: {
slouken@0
   547
			/* Only post the event if we're watching for it */
slouken@0
   548
			if ( SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE ) {
slouken@0
   549
			        SDL_SysWMmsg wmmsg;
slouken@0
   550
slouken@0
   551
				SDL_VERSION(&wmmsg.version);
slouken@0
   552
				wmmsg.hwnd = hwnd;
slouken@0
   553
				wmmsg.msg = msg;
slouken@0
   554
				wmmsg.wParam = wParam;
slouken@0
   555
				wmmsg.lParam = lParam;
slouken@0
   556
				posted = SDL_PrivateSysWMEvent(&wmmsg);
slouken@145
   557
slouken@721
   558
			/* DJM: If the user isn't watching for private
slouken@721
   559
				messages in her SDL event loop, then pass it
slouken@721
   560
				along to any win32 specific window proc.
slouken@721
   561
			 */
slouken@721
   562
			} else if (userWindowProc) {
slouken@721
   563
				return CallWindowProc(userWindowProc, hwnd, msg, wParam, lParam);
slouken@721
   564
			}
slouken@0
   565
		}
slouken@0
   566
		break;
slouken@0
   567
	}
slouken@0
   568
	return(DefWindowProc(hwnd, msg, wParam, lParam));
slouken@0
   569
}
slouken@0
   570
slouken@0
   571
/* This function checks the windows message queue and DirectInput and returns
slouken@0
   572
   1 if there was input, 0 if there was no input, or -1 if the application has
slouken@0
   573
   posted a quit message.
slouken@0
   574
*/
slouken@460
   575
static int DX5_CheckInput(_THIS, int timeout, BOOL processInput)
slouken@0
   576
{
slouken@0
   577
	MSG msg;
slouken@0
   578
	int      i;
slouken@0
   579
	HRESULT  result;
slouken@0
   580
	DWORD    event;
slouken@0
   581
slouken@0
   582
	/* Check the normal windows queue (highest preference) */
slouken@0
   583
	posted = 0;
slouken@0
   584
	while ( ! posted &&
slouken@523
   585
	        PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) ) {
slouken@523
   586
		if ( GetMessage(&msg, NULL, 0, 0) > 0 ) {
slouken@0
   587
			DispatchMessage(&msg);
slouken@0
   588
		} else {
slouken@0
   589
			return(-1);
slouken@0
   590
		}
slouken@0
   591
	}
slouken@0
   592
	if ( posted ) {
slouken@0
   593
		return(1);
slouken@0
   594
	}
slouken@0
   595
slouken@0
   596
	/* Pump the DirectInput flow */
slouken@0
   597
	if ( SDL_GetAppState() & SDL_APPINPUTFOCUS ) {
slouken@0
   598
		for ( i=0; i<SDL_DIndev; ++i ) {
slouken@0
   599
			result = IDirectInputDevice2_Poll(SDL_DIdev[i]);
slouken@0
   600
			if ( (result == DIERR_INPUTLOST) ||
slouken@0
   601
					(result == DIERR_NOTACQUIRED) ) {
slouken@1336
   602
				if ( SDL_strcmp(inputs[i].name, "mouse") == 0 ) {
slouken@0
   603
					mouse_lost = 1;
slouken@0
   604
				}
slouken@0
   605
				IDirectInputDevice2_Acquire(SDL_DIdev[i]);
slouken@0
   606
				IDirectInputDevice2_Poll(SDL_DIdev[i]);
slouken@0
   607
			}
slouken@0
   608
		}
slouken@0
   609
	}
slouken@0
   610
slouken@0
   611
	/* Wait for messages and input events */
slouken@0
   612
	event = MsgWaitForMultipleObjects(SDL_DIndev, SDL_DIevt, FALSE,
slouken@0
   613
							timeout, QS_ALLEVENTS);
slouken@0
   614
	if ((event >= WAIT_OBJECT_0) && (event < (WAIT_OBJECT_0+SDL_DIndev))) {
slouken@0
   615
		DWORD numevents;
slouken@1330
   616
		static DIDEVICEOBJECTDATA evtbuf[INPUT_QSIZE];
slouken@0
   617
slouken@0
   618
		event -= WAIT_OBJECT_0;
slouken@0
   619
		numevents = INPUT_QSIZE;
slouken@0
   620
		result = IDirectInputDevice2_GetDeviceData(
slouken@0
   621
				SDL_DIdev[event], sizeof(DIDEVICEOBJECTDATA),
slouken@0
   622
							evtbuf, &numevents, 0);
slouken@0
   623
		if ( (result == DIERR_INPUTLOST) ||
slouken@0
   624
					(result == DIERR_NOTACQUIRED) ) {
slouken@1336
   625
			if ( SDL_strcmp(inputs[event].name, "mouse") == 0 ) {
slouken@0
   626
				mouse_lost = 1;
slouken@0
   627
			}
slouken@0
   628
			IDirectInputDevice2_Acquire(SDL_DIdev[event]);
slouken@0
   629
			result = IDirectInputDevice2_GetDeviceData(
slouken@0
   630
				SDL_DIdev[event], sizeof(DIDEVICEOBJECTDATA),
slouken@0
   631
							evtbuf, &numevents, 0);
slouken@0
   632
		}
slouken@0
   633
		/* Handle the events */
slouken@460
   634
		if ( result == DI_OK && processInput ) {
slouken@0
   635
			/* Note: This can post multiple events to event queue
slouken@0
   636
			 */
slouken@0
   637
			(*SDL_DIfun[event])((int)numevents, evtbuf);
slouken@0
   638
			return(1);
slouken@0
   639
		}
slouken@0
   640
	}
slouken@0
   641
	if ( event != WAIT_TIMEOUT ) {
slouken@0
   642
		/* Maybe there was a windows message? */
slouken@0
   643
		if ( PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) ) {
slouken@0
   644
			if ( GetMessage(&msg, NULL, 0, 0) > 0 ) {
slouken@0
   645
				DispatchMessage(&msg);
slouken@0
   646
			} else {
slouken@0
   647
				return(-1);
slouken@0
   648
			}
slouken@0
   649
			return(1);
slouken@0
   650
		}
slouken@0
   651
	}
slouken@0
   652
	return(0);
slouken@0
   653
}
slouken@0
   654
slouken@460
   655
/* Change cooperative level based on whether or not we are fullscreen */
slouken@460
   656
void DX5_DInputReset(_THIS, int fullscreen)
slouken@460
   657
{
slouken@460
   658
	DWORD level;
slouken@460
   659
	int i;
slouken@460
   660
	HRESULT result;
slouken@970
   661
	HWND topwnd;
slouken@460
   662
slouken@460
   663
	for ( i=0; i<MAX_INPUTS; ++i ) {
slouken@460
   664
		if ( SDL_DIdev[i] != NULL ) {
slouken@460
   665
			if ( fullscreen ) {
slouken@460
   666
				level = inputs[i].raw_level;
slouken@460
   667
			} else {
slouken@460
   668
				level = inputs[i].win_level;
slouken@460
   669
			}
slouken@460
   670
			IDirectInputDevice2_Unacquire(SDL_DIdev[i]);
icculus@1115
   671
			topwnd = GetTopLevelParent(SDL_Window);
slouken@460
   672
			result = IDirectInputDevice2_SetCooperativeLevel(
slouken@970
   673
					SDL_DIdev[i], topwnd, level);
slouken@460
   674
			IDirectInputDevice2_Acquire(SDL_DIdev[i]);
slouken@460
   675
			if ( result != DI_OK ) {
slouken@460
   676
				SetDIerror(
slouken@460
   677
			"DirectInputDevice::SetCooperativeLevel", result);
slouken@460
   678
			}
slouken@460
   679
		}
slouken@460
   680
	}
slouken@460
   681
	mouse_lost = 1;
slouken@460
   682
slouken@460
   683
	/* Flush pending input */
slouken@460
   684
	DX5_CheckInput(this, 0, FALSE);
slouken@460
   685
}
slouken@460
   686
slouken@0
   687
void DX5_PumpEvents(_THIS)
slouken@0
   688
{
slouken@0
   689
	/* Wait for messages and DirectInput */
slouken@460
   690
	while ( DX5_CheckInput(this, 0, TRUE) > 0 ) {
slouken@0
   691
		/* Loop and check again */;
slouken@0
   692
	}
slouken@0
   693
}
slouken@0
   694
slouken@0
   695
void DX5_InitOSKeymap(_THIS)
slouken@0
   696
{
slouken@275
   697
#ifndef DIK_PAUSE
slouken@275
   698
#define DIK_PAUSE	0xC5
slouken@275
   699
#endif
slouken@327
   700
#ifndef DIK_OEM_102
slouken@327
   701
#define DIK_OEM_102	0x56	/* < > | on UK/Germany keyboards */
slouken@327
   702
#endif
slouken@0
   703
	int i;
slouken@0
   704
slouken@0
   705
	/* Map the DIK scancodes to SDL keysyms */
slouken@1379
   706
	for ( i=0; i<SDL_arraysize(DIK_keymap); ++i )
slouken@0
   707
		DIK_keymap[i] = 0;
slouken@0
   708
slouken@0
   709
	/* Defined DIK_* constants */
slouken@0
   710
	DIK_keymap[DIK_ESCAPE] = SDLK_ESCAPE;
slouken@0
   711
	DIK_keymap[DIK_1] = SDLK_1;
slouken@0
   712
	DIK_keymap[DIK_2] = SDLK_2;
slouken@0
   713
	DIK_keymap[DIK_3] = SDLK_3;
slouken@0
   714
	DIK_keymap[DIK_4] = SDLK_4;
slouken@0
   715
	DIK_keymap[DIK_5] = SDLK_5;
slouken@0
   716
	DIK_keymap[DIK_6] = SDLK_6;
slouken@0
   717
	DIK_keymap[DIK_7] = SDLK_7;
slouken@0
   718
	DIK_keymap[DIK_8] = SDLK_8;
slouken@0
   719
	DIK_keymap[DIK_9] = SDLK_9;
slouken@0
   720
	DIK_keymap[DIK_0] = SDLK_0;
slouken@0
   721
	DIK_keymap[DIK_MINUS] = SDLK_MINUS;
slouken@0
   722
	DIK_keymap[DIK_EQUALS] = SDLK_EQUALS;
slouken@0
   723
	DIK_keymap[DIK_BACK] = SDLK_BACKSPACE;
slouken@0
   724
	DIK_keymap[DIK_TAB] = SDLK_TAB;
slouken@0
   725
	DIK_keymap[DIK_Q] = SDLK_q;
slouken@0
   726
	DIK_keymap[DIK_W] = SDLK_w;
slouken@0
   727
	DIK_keymap[DIK_E] = SDLK_e;
slouken@0
   728
	DIK_keymap[DIK_R] = SDLK_r;
slouken@0
   729
	DIK_keymap[DIK_T] = SDLK_t;
slouken@0
   730
	DIK_keymap[DIK_Y] = SDLK_y;
slouken@0
   731
	DIK_keymap[DIK_U] = SDLK_u;
slouken@0
   732
	DIK_keymap[DIK_I] = SDLK_i;
slouken@0
   733
	DIK_keymap[DIK_O] = SDLK_o;
slouken@0
   734
	DIK_keymap[DIK_P] = SDLK_p;
slouken@0
   735
	DIK_keymap[DIK_LBRACKET] = SDLK_LEFTBRACKET;
slouken@0
   736
	DIK_keymap[DIK_RBRACKET] = SDLK_RIGHTBRACKET;
slouken@0
   737
	DIK_keymap[DIK_RETURN] = SDLK_RETURN;
slouken@0
   738
	DIK_keymap[DIK_LCONTROL] = SDLK_LCTRL;
slouken@0
   739
	DIK_keymap[DIK_A] = SDLK_a;
slouken@0
   740
	DIK_keymap[DIK_S] = SDLK_s;
slouken@0
   741
	DIK_keymap[DIK_D] = SDLK_d;
slouken@0
   742
	DIK_keymap[DIK_F] = SDLK_f;
slouken@0
   743
	DIK_keymap[DIK_G] = SDLK_g;
slouken@0
   744
	DIK_keymap[DIK_H] = SDLK_h;
slouken@0
   745
	DIK_keymap[DIK_J] = SDLK_j;
slouken@0
   746
	DIK_keymap[DIK_K] = SDLK_k;
slouken@0
   747
	DIK_keymap[DIK_L] = SDLK_l;
slouken@0
   748
	DIK_keymap[DIK_SEMICOLON] = SDLK_SEMICOLON;
slouken@0
   749
	DIK_keymap[DIK_APOSTROPHE] = SDLK_QUOTE;
slouken@0
   750
	DIK_keymap[DIK_GRAVE] = SDLK_BACKQUOTE;
slouken@0
   751
	DIK_keymap[DIK_LSHIFT] = SDLK_LSHIFT;
slouken@0
   752
	DIK_keymap[DIK_BACKSLASH] = SDLK_BACKSLASH;
slouken@327
   753
	DIK_keymap[DIK_OEM_102] = SDLK_BACKSLASH;
slouken@0
   754
	DIK_keymap[DIK_Z] = SDLK_z;
slouken@0
   755
	DIK_keymap[DIK_X] = SDLK_x;
slouken@0
   756
	DIK_keymap[DIK_C] = SDLK_c;
slouken@0
   757
	DIK_keymap[DIK_V] = SDLK_v;
slouken@0
   758
	DIK_keymap[DIK_B] = SDLK_b;
slouken@0
   759
	DIK_keymap[DIK_N] = SDLK_n;
slouken@0
   760
	DIK_keymap[DIK_M] = SDLK_m;
slouken@0
   761
	DIK_keymap[DIK_COMMA] = SDLK_COMMA;
slouken@0
   762
	DIK_keymap[DIK_PERIOD] = SDLK_PERIOD;
slouken@0
   763
	DIK_keymap[DIK_SLASH] = SDLK_SLASH;
slouken@0
   764
	DIK_keymap[DIK_RSHIFT] = SDLK_RSHIFT;
slouken@0
   765
	DIK_keymap[DIK_MULTIPLY] = SDLK_KP_MULTIPLY;
slouken@0
   766
	DIK_keymap[DIK_LMENU] = SDLK_LALT;
slouken@0
   767
	DIK_keymap[DIK_SPACE] = SDLK_SPACE;
slouken@0
   768
	DIK_keymap[DIK_CAPITAL] = SDLK_CAPSLOCK;
slouken@0
   769
	DIK_keymap[DIK_F1] = SDLK_F1;
slouken@0
   770
	DIK_keymap[DIK_F2] = SDLK_F2;
slouken@0
   771
	DIK_keymap[DIK_F3] = SDLK_F3;
slouken@0
   772
	DIK_keymap[DIK_F4] = SDLK_F4;
slouken@0
   773
	DIK_keymap[DIK_F5] = SDLK_F5;
slouken@0
   774
	DIK_keymap[DIK_F6] = SDLK_F6;
slouken@0
   775
	DIK_keymap[DIK_F7] = SDLK_F7;
slouken@0
   776
	DIK_keymap[DIK_F8] = SDLK_F8;
slouken@0
   777
	DIK_keymap[DIK_F9] = SDLK_F9;
slouken@0
   778
	DIK_keymap[DIK_F10] = SDLK_F10;
slouken@0
   779
	DIK_keymap[DIK_NUMLOCK] = SDLK_NUMLOCK;
slouken@0
   780
	DIK_keymap[DIK_SCROLL] = SDLK_SCROLLOCK;
slouken@0
   781
	DIK_keymap[DIK_NUMPAD7] = SDLK_KP7;
slouken@0
   782
	DIK_keymap[DIK_NUMPAD8] = SDLK_KP8;
slouken@0
   783
	DIK_keymap[DIK_NUMPAD9] = SDLK_KP9;
slouken@0
   784
	DIK_keymap[DIK_SUBTRACT] = SDLK_KP_MINUS;
slouken@0
   785
	DIK_keymap[DIK_NUMPAD4] = SDLK_KP4;
slouken@0
   786
	DIK_keymap[DIK_NUMPAD5] = SDLK_KP5;
slouken@0
   787
	DIK_keymap[DIK_NUMPAD6] = SDLK_KP6;
slouken@0
   788
	DIK_keymap[DIK_ADD] = SDLK_KP_PLUS;
slouken@0
   789
	DIK_keymap[DIK_NUMPAD1] = SDLK_KP1;
slouken@0
   790
	DIK_keymap[DIK_NUMPAD2] = SDLK_KP2;
slouken@0
   791
	DIK_keymap[DIK_NUMPAD3] = SDLK_KP3;
slouken@0
   792
	DIK_keymap[DIK_NUMPAD0] = SDLK_KP0;
slouken@0
   793
	DIK_keymap[DIK_DECIMAL] = SDLK_KP_PERIOD;
slouken@0
   794
	DIK_keymap[DIK_F11] = SDLK_F11;
slouken@0
   795
	DIK_keymap[DIK_F12] = SDLK_F12;
slouken@0
   796
slouken@0
   797
	DIK_keymap[DIK_F13] = SDLK_F13;
slouken@0
   798
	DIK_keymap[DIK_F14] = SDLK_F14;
slouken@0
   799
	DIK_keymap[DIK_F15] = SDLK_F15;
slouken@0
   800
slouken@0
   801
	DIK_keymap[DIK_NUMPADEQUALS] = SDLK_KP_EQUALS;
slouken@0
   802
	DIK_keymap[DIK_NUMPADENTER] = SDLK_KP_ENTER;
slouken@0
   803
	DIK_keymap[DIK_RCONTROL] = SDLK_RCTRL;
slouken@0
   804
	DIK_keymap[DIK_DIVIDE] = SDLK_KP_DIVIDE;
slouken@1526
   805
	DIK_keymap[DIK_SYSRQ] = SDLK_PRINT;
slouken@0
   806
	DIK_keymap[DIK_RMENU] = SDLK_RALT;
slouken@290
   807
	DIK_keymap[DIK_PAUSE] = SDLK_PAUSE;
slouken@0
   808
	DIK_keymap[DIK_HOME] = SDLK_HOME;
slouken@0
   809
	DIK_keymap[DIK_UP] = SDLK_UP;
slouken@0
   810
	DIK_keymap[DIK_PRIOR] = SDLK_PAGEUP;
slouken@0
   811
	DIK_keymap[DIK_LEFT] = SDLK_LEFT;
slouken@0
   812
	DIK_keymap[DIK_RIGHT] = SDLK_RIGHT;
slouken@0
   813
	DIK_keymap[DIK_END] = SDLK_END;
slouken@0
   814
	DIK_keymap[DIK_DOWN] = SDLK_DOWN;
slouken@0
   815
	DIK_keymap[DIK_NEXT] = SDLK_PAGEDOWN;
slouken@0
   816
	DIK_keymap[DIK_INSERT] = SDLK_INSERT;
slouken@0
   817
	DIK_keymap[DIK_DELETE] = SDLK_DELETE;
slouken@0
   818
	DIK_keymap[DIK_LWIN] = SDLK_LMETA;
slouken@0
   819
	DIK_keymap[DIK_RWIN] = SDLK_RMETA;
slouken@0
   820
	DIK_keymap[DIK_APPS] = SDLK_MENU;
slouken@0
   821
}
slouken@0
   822
slouken@0
   823
static SDL_keysym *TranslateKey(UINT scancode, SDL_keysym *keysym, int pressed)
slouken@0
   824
{
slouken@0
   825
	/* Set the keysym information */
slouken@0
   826
	keysym->scancode = (unsigned char)scancode;
slouken@0
   827
	keysym->sym = DIK_keymap[scancode];
slouken@0
   828
	keysym->mod = KMOD_NONE;
slouken@0
   829
	keysym->unicode = 0;
slouken@1253
   830
	if ( pressed && SDL_TranslateUNICODE ) {
slouken@0
   831
		UINT vkey;
slouken@453
   832
#ifndef NO_GETKEYBOARDSTATE
slouken@1253
   833
		BYTE	keystate[256];
slouken@1253
   834
		Uint16	wchars[2];
slouken@453
   835
#endif
slouken@0
   836
slouken@0
   837
		vkey = MapVirtualKey(scancode, 1);
slouken@453
   838
#ifdef NO_GETKEYBOARDSTATE
slouken@453
   839
		/* Uh oh, better hope the vkey is close enough.. */
slouken@453
   840
		keysym->unicode = vkey;
slouken@453
   841
#else
slouken@0
   842
		GetKeyboardState(keystate);
slouken@1253
   843
		if (SDL_ToUnicode(vkey, scancode, keystate, wchars, sizeof(wchars)/sizeof(wchars[0]), 0) == 1)
slouken@1253
   844
		{
slouken@1253
   845
			keysym->unicode = wchars[0];
slouken@0
   846
		}
slouken@1253
   847
#endif /* NO_GETKEYBOARDSTATE */
slouken@0
   848
	}
slouken@0
   849
	return(keysym);
slouken@0
   850
}
slouken@0
   851
slouken@0
   852
int DX5_CreateWindow(_THIS)
slouken@0
   853
{
slouken@1336
   854
	char *windowid = SDL_getenv("SDL_WINDOWID");
slouken@0
   855
	int i;
slouken@0
   856
slouken@0
   857
	/* Clear out DirectInput variables in case we fail */
slouken@0
   858
	for ( i=0; i<MAX_INPUTS; ++i ) {
slouken@0
   859
		SDL_DIdev[i] = NULL;
slouken@0
   860
		SDL_DIevt[i] = NULL;
slouken@0
   861
		SDL_DIfun[i] = NULL;
slouken@0
   862
	}
slouken@0
   863
slouken@1288
   864
	SDL_RegisterApp(NULL, 0, 0);
slouken@1280
   865
slouken@1280
   866
	SDL_windowid = (windowid != NULL);
slouken@0
   867
	if ( SDL_windowid ) {
slouken@1456
   868
		SDL_Window = (HWND)SDL_strtoull(windowid, NULL, 0);
slouken@975
   869
		if ( SDL_Window == NULL ) {
slouken@975
   870
			SDL_SetError("Couldn't get user specified window");
slouken@975
   871
			return(-1);
slouken@975
   872
		}
slouken@145
   873
slouken@458
   874
		/* DJM: we want all event's for the user specified
slouken@975
   875
			window to be handled by SDL.
slouken@458
   876
		 */
slouken@1472
   877
		userWindowProc = (WNDPROCTYPE)GetWindowLongPtr(SDL_Window, GWLP_WNDPROC);
slouken@1472
   878
		SetWindowLongPtr(SDL_Window, GWLP_WNDPROC, (LONG_PTR)WinMessage);
slouken@0
   879
	} else {
slouken@0
   880
		SDL_Window = CreateWindow(SDL_Appname, SDL_Appname,
slouken@0
   881
                        (WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX),
slouken@833
   882
                        CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, NULL, NULL, SDL_Instance, NULL);
slouken@0
   883
		if ( SDL_Window == NULL ) {
slouken@0
   884
			SDL_SetError("Couldn't create window");
slouken@0
   885
			return(-1);
slouken@0
   886
		}
slouken@0
   887
		ShowWindow(SDL_Window, SW_HIDE);
slouken@0
   888
	}
slouken@0
   889
slouken@0
   890
	/* Initialize DirectInput */
slouken@0
   891
	if ( DX5_DInputInit(this) < 0 ) {
slouken@0
   892
		return(-1);
slouken@0
   893
	}
slouken@0
   894
slouken@1523
   895
	/* JC 14 Mar 2006
slouken@1523
   896
		Flush the message loop or this can cause big problems later
slouken@1523
   897
		Especially if the user decides to use dialog boxes or assert()!
slouken@1523
   898
	*/
slouken@1523
   899
	WIN_FlushMessageQueue();
slouken@1523
   900
slouken@0
   901
	/* Ready to roll */
slouken@0
   902
	return(0);
slouken@0
   903
}
slouken@0
   904
slouken@0
   905
void DX5_DestroyWindow(_THIS)
slouken@0
   906
{
slouken@0
   907
	/* Close down DirectInput */
slouken@0
   908
	DX5_DInputQuit(this);
slouken@0
   909
slouken@0
   910
	/* Destroy our window */
slouken@975
   911
	if ( SDL_windowid ) {
slouken@1472
   912
		SetWindowLongPtr(SDL_Window, GWLP_WNDPROC, (LONG_PTR)userWindowProc);
slouken@975
   913
	} else {
slouken@975
   914
		DestroyWindow(SDL_Window);
slouken@975
   915
	}
slouken@1288
   916
	SDL_UnregisterApp();
slouken@1523
   917
slouken@1523
   918
	/* JC 14 Mar 2006
slouken@1523
   919
		Flush the message loop or this can cause big problems later
slouken@1523
   920
		Especially if the user decides to use dialog boxes or assert()!
slouken@1523
   921
	*/
slouken@1523
   922
	WIN_FlushMessageQueue();
slouken@0
   923
}