src/joystick/win32/SDL_dxjoystick.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 21 May 2006 17:27:13 +0000
branchSDL-1.3
changeset 1661 281d3f4870e5
child 1662 782fd950bd46
permissions -rw-r--r--
Moved DirectInput joystick code to 1.3 branch
slouken@1661
     1
/*
slouken@1661
     2
    SDL - Simple DirectMedia Layer
slouken@1661
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@1661
     4
slouken@1661
     5
    This library is free software; you can redistribute it and/or
slouken@1661
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1661
     7
    License as published by the Free Software Foundation; either
slouken@1661
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1661
     9
slouken@1661
    10
    This library is distributed in the hope that it will be useful,
slouken@1661
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1661
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1661
    13
    Lesser General Public License for more details.
slouken@1661
    14
slouken@1661
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1661
    16
    License along with this library; if not, write to the Free Software
slouken@1661
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1661
    18
slouken@1661
    19
    Sam Lantinga
slouken@1661
    20
    slouken@libsdl.org
slouken@1661
    21
*/
slouken@1661
    22
#include "SDL_config.h"
slouken@1661
    23
slouken@1661
    24
#ifdef SDL_JOYSTICK_DINPUT
slouken@1661
    25
slouken@1661
    26
/* DirectInput joystick driver; written by Glenn Maynard, based on Andrei de
slouken@1661
    27
 * A. Formiga's WINMM driver. 
slouken@1661
    28
 *
slouken@1661
    29
 * Hats and sliders are completely untested; the app I'm writing this for mostly
slouken@1661
    30
 * doesn't use them and I don't own any joysticks with them. 
slouken@1661
    31
 *
slouken@1661
    32
 * We don't bother to use event notification here.  It doesn't seem to work
slouken@1661
    33
 * with polled devices, and it's fine to call IDirectInputDevice2_GetDeviceData and
slouken@1661
    34
 * let it return 0 events. */
slouken@1661
    35
slouken@1661
    36
#include "SDL_error.h"
slouken@1661
    37
#include "SDL_events.h"
slouken@1661
    38
#include "SDL_joystick.h"
slouken@1661
    39
#include "../SDL_sysjoystick.h"
slouken@1661
    40
#include "../SDL_joystick_c.h"
slouken@1661
    41
slouken@1661
    42
#define WIN32_LEAN_AND_MEAN
slouken@1661
    43
#include <windows.h>
slouken@1661
    44
slouken@1661
    45
#define DIRECTINPUT_VERSION 0x0500
slouken@1661
    46
#include <dinput.h>
slouken@1661
    47
slouken@1661
    48
#define INPUT_QSIZE	32		/* Buffer up to 32 input messages */
slouken@1661
    49
slouken@1661
    50
extern HINSTANCE SDL_Instance;
slouken@1661
    51
extern int DX5_Load();
slouken@1661
    52
extern void DX5_Unload();
slouken@1661
    53
extern HRESULT (WINAPI *DInputCreate)(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUT *ppDI, LPUNKNOWN punkOuter);
slouken@1661
    54
slouken@1661
    55
static LPDIRECTINPUT dinput = NULL;
slouken@1661
    56
slouken@1661
    57
#define MAX_JOYSTICKS	8
slouken@1661
    58
#define MAX_INPUTS	256	/* each joystick can have up to 256 inputs */
slouken@1661
    59
#define AXIS_MIN	-32768  /* minimum value for axis coordinate */
slouken@1661
    60
#define AXIS_MAX	32767   /* maximum value for axis coordinate */
slouken@1661
    61
#define JOY_AXIS_THRESHOLD	(((AXIS_MAX)-(AXIS_MIN))/100) /* 1% motion */
slouken@1661
    62
slouken@1661
    63
typedef enum Type { BUTTON, AXIS, HAT } Type;
slouken@1661
    64
slouken@1661
    65
/* array to hold joystick ID values */
slouken@1661
    66
static DIDEVICEINSTANCE SYS_Joystick[MAX_JOYSTICKS];
slouken@1661
    67
static int	SYS_NumJoysticks;
slouken@1661
    68
slouken@1661
    69
extern HWND SDL_Window;
slouken@1661
    70
slouken@1661
    71
typedef struct input_t
slouken@1661
    72
{
slouken@1661
    73
	/* DirectInput offset for this input type: */
slouken@1661
    74
	DWORD ofs;
slouken@1661
    75
slouken@1661
    76
	/* Button, axis or hat: */
slouken@1661
    77
	Type type;
slouken@1661
    78
slouken@1661
    79
	/* SDL input offset: */
slouken@1661
    80
	Uint8 num;
slouken@1661
    81
} input_t;
slouken@1661
    82
slouken@1661
    83
/* The private structure used to keep track of a joystick */
slouken@1661
    84
struct joystick_hwdata
slouken@1661
    85
{
slouken@1661
    86
	LPDIRECTINPUTDEVICE2 InputDevice;
slouken@1661
    87
	int buffered;
slouken@1661
    88
slouken@1661
    89
	input_t Inputs[MAX_INPUTS];
slouken@1661
    90
	int NumInputs;
slouken@1661
    91
};
slouken@1661
    92
slouken@1661
    93
/* Convert a DirectInput return code to a text message */
slouken@1661
    94
static void SetDIerror(char *function, int code)
slouken@1661
    95
{
slouken@1661
    96
	static char *error;
slouken@1661
    97
	static char  errbuf[1024];
slouken@1661
    98
slouken@1661
    99
	errbuf[0] = 0;
slouken@1661
   100
	switch (code) {
slouken@1661
   101
                case DIERR_GENERIC:
slouken@1661
   102
                        error = "Undefined error!";
slouken@1661
   103
                        break;
slouken@1661
   104
		case DIERR_OLDDIRECTINPUTVERSION:
slouken@1661
   105
			error = "Your version of DirectInput needs upgrading";
slouken@1661
   106
			break;
slouken@1661
   107
		case DIERR_INVALIDPARAM:
slouken@1661
   108
                        error = "Invalid parameters";
slouken@1661
   109
                        break;
slouken@1661
   110
                case DIERR_OUTOFMEMORY:
slouken@1661
   111
                        error = "Out of memory";
slouken@1661
   112
                        break;
slouken@1661
   113
		case DIERR_DEVICENOTREG:
slouken@1661
   114
			error = "Device not registered";
slouken@1661
   115
			break;
slouken@1661
   116
		case DIERR_NOINTERFACE:
slouken@1661
   117
			error = "Interface not supported";
slouken@1661
   118
			break;
slouken@1661
   119
		case DIERR_NOTINITIALIZED:
slouken@1661
   120
			error = "Device not initialized";
slouken@1661
   121
			break;
slouken@1661
   122
		default:
slouken@1661
   123
			sprintf(errbuf, "%s: Unknown DirectInput error: 0x%x",
slouken@1661
   124
								function, code);
slouken@1661
   125
			break;
slouken@1661
   126
	}
slouken@1661
   127
	if ( ! errbuf[0] ) {
slouken@1661
   128
		sprintf(errbuf, "%s: %s", function, error);
slouken@1661
   129
	}
slouken@1661
   130
	SDL_SetError("%s", errbuf);
slouken@1661
   131
	return;
slouken@1661
   132
}
slouken@1661
   133
slouken@1661
   134
slouken@1661
   135
BOOL CALLBACK EnumJoysticksCallback( const DIDEVICEINSTANCE* pdidInstance,
slouken@1661
   136
				     VOID* pContext )
slouken@1661
   137
{
slouken@1661
   138
	memcpy(&SYS_Joystick[SYS_NumJoysticks], pdidInstance, sizeof(DIDEVICEINSTANCE));
slouken@1661
   139
	SYS_NumJoysticks++;
slouken@1661
   140
slouken@1661
   141
	if( SYS_NumJoysticks >= MAX_JOYSTICKS )
slouken@1661
   142
	        return DIENUM_STOP;
slouken@1661
   143
slouken@1661
   144
	return DIENUM_CONTINUE;
slouken@1661
   145
}
slouken@1661
   146
slouken@1661
   147
static BOOL CALLBACK DIJoystick_EnumDevObjectsProc(LPCDIDEVICEOBJECTINSTANCE dev,
slouken@1661
   148
						    LPVOID pvRef)
slouken@1661
   149
{
slouken@1661
   150
	SDL_Joystick *joystick = (SDL_Joystick*)pvRef;
slouken@1661
   151
	HRESULT result;
slouken@1661
   152
	input_t *in = &joystick->hwdata->Inputs[joystick->hwdata->NumInputs];
slouken@1661
   153
	const int SupportedMask = DIDFT_BUTTON | DIDFT_POV | DIDFT_AXIS;
slouken@1661
   154
	if(!(dev->dwType & SupportedMask))
slouken@1661
   155
	    return DIENUM_CONTINUE; /* unsupported */
slouken@1661
   156
slouken@1661
   157
	in->ofs = dev->dwOfs;
slouken@1661
   158
slouken@1661
   159
	if(dev->dwType & DIDFT_BUTTON) {
slouken@1661
   160
		in->type = BUTTON;
slouken@1661
   161
		in->num = joystick->nbuttons;
slouken@1661
   162
		joystick->nbuttons++;
slouken@1661
   163
	} else if(dev->dwType & DIDFT_POV) {
slouken@1661
   164
		in->type = HAT;
slouken@1661
   165
		in->num = joystick->nhats;
slouken@1661
   166
		joystick->nhats++;
slouken@1661
   167
	} else { /* dev->dwType & DIDFT_AXIS */
slouken@1661
   168
		DIPROPRANGE diprg;
slouken@1661
   169
		DIPROPDWORD dilong;
slouken@1661
   170
		
slouken@1661
   171
		in->type = AXIS;
slouken@1661
   172
		in->num = joystick->naxes;
slouken@1661
   173
		
slouken@1661
   174
		diprg.diph.dwSize		= sizeof(diprg);
slouken@1661
   175
		diprg.diph.dwHeaderSize	= sizeof(diprg.diph);
slouken@1661
   176
		diprg.diph.dwObj		= dev->dwOfs;
slouken@1661
   177
		diprg.diph.dwHow		= DIPH_BYOFFSET;
slouken@1661
   178
		diprg.lMin			= AXIS_MIN;
slouken@1661
   179
		diprg.lMax			= AXIS_MAX;
slouken@1661
   180
slouken@1661
   181
		result = IDirectInputDevice2_SetProperty(joystick->hwdata->InputDevice, DIPROP_RANGE, &diprg.diph);
slouken@1661
   182
		if ( result != DI_OK )
slouken@1661
   183
			return DIENUM_CONTINUE; /* don't use this axis */
slouken@1661
   184
	
slouken@1661
   185
		/* Set dead zone to 0. */
slouken@1661
   186
		dilong.diph.dwSize		= sizeof(dilong);
slouken@1661
   187
		dilong.diph.dwHeaderSize	= sizeof(dilong.diph);
slouken@1661
   188
		dilong.diph.dwObj		= dev->dwOfs;
slouken@1661
   189
		dilong.diph.dwHow		= DIPH_BYOFFSET;
slouken@1661
   190
		dilong.dwData = 0;
slouken@1661
   191
		result = IDirectInputDevice2_SetProperty(joystick->hwdata->InputDevice, DIPROP_DEADZONE, &dilong.diph);
slouken@1661
   192
		if ( result != DI_OK )
slouken@1661
   193
			return DIENUM_CONTINUE; /* don't use this axis */
slouken@1661
   194
slouken@1661
   195
		joystick->naxes++;
slouken@1661
   196
	}
slouken@1661
   197
slouken@1661
   198
	joystick->hwdata->NumInputs++;
slouken@1661
   199
slouken@1661
   200
	if(joystick->hwdata->NumInputs == MAX_INPUTS)
slouken@1661
   201
		return DIENUM_STOP; /* too many */
slouken@1661
   202
slouken@1661
   203
	return DIENUM_CONTINUE;
slouken@1661
   204
}
slouken@1661
   205
slouken@1661
   206
/* Function to scan the system for joysticks.
slouken@1661
   207
 * This function should set SDL_numjoysticks to the number of available
slouken@1661
   208
 * joysticks.  Joystick 0 should be the system default joystick.
slouken@1661
   209
 * It should return 0, or -1 on an unrecoverable fatal error.
slouken@1661
   210
 */
slouken@1661
   211
int SDL_SYS_JoystickInit(void)
slouken@1661
   212
{
slouken@1661
   213
	HRESULT result;
slouken@1661
   214
slouken@1661
   215
	SYS_NumJoysticks = 0;
slouken@1661
   216
slouken@1661
   217
	/* Create the DirectInput object */
slouken@1661
   218
	if ( DX5_Load() < 0 ) {
slouken@1661
   219
		SDL_SetError("Couldn't load DirectInput");
slouken@1661
   220
		return(-1);
slouken@1661
   221
	}
slouken@1661
   222
	result = DInputCreate(SDL_Instance, DIRECTINPUT_VERSION,
slouken@1661
   223
							&dinput, NULL);
slouken@1661
   224
	if ( result != DI_OK ) {
slouken@1661
   225
		DX5_Unload();
slouken@1661
   226
		SetDIerror("DirectInputCreate", result);
slouken@1661
   227
		return(-1);
slouken@1661
   228
	}
slouken@1661
   229
slouken@1661
   230
	result = IDirectInput_EnumDevices(dinput,
slouken@1661
   231
			DIDEVTYPE_JOYSTICK, 
slouken@1661
   232
			EnumJoysticksCallback,
slouken@1661
   233
   			NULL,
slouken@1661
   234
			DIEDFL_ATTACHEDONLY );
slouken@1661
   235
slouken@1661
   236
	return SYS_NumJoysticks;
slouken@1661
   237
}
slouken@1661
   238
slouken@1661
   239
/* Function to get the device-dependent name of a joystick */
slouken@1661
   240
const char *SDL_SYS_JoystickName(int index)
slouken@1661
   241
{
slouken@1661
   242
	/***-> test for invalid index ? */
slouken@1661
   243
	return(SYS_Joystick[index].tszProductName);
slouken@1661
   244
}
slouken@1661
   245
slouken@1661
   246
/* Function to open a joystick for use.
slouken@1661
   247
   The joystick to open is specified by the index field of the joystick.
slouken@1661
   248
   This should fill the nbuttons and naxes fields of the joystick structure.
slouken@1661
   249
   It returns 0, or -1 if there is an error.
slouken@1661
   250
 */
slouken@1661
   251
int SDL_SYS_JoystickOpen(SDL_Joystick *joystick)
slouken@1661
   252
{
slouken@1661
   253
	HRESULT result;
slouken@1661
   254
	LPDIRECTINPUTDEVICE device;
slouken@1661
   255
slouken@1661
   256
	/* allocate memory for system specific hardware data */
slouken@1661
   257
	joystick->hwdata = (struct joystick_hwdata *) malloc(sizeof(*joystick->hwdata));
slouken@1661
   258
	if (joystick->hwdata == NULL)
slouken@1661
   259
	{
slouken@1661
   260
		SDL_OutOfMemory();
slouken@1661
   261
		return(-1);
slouken@1661
   262
	}
slouken@1661
   263
	memset(joystick->hwdata, 0, sizeof(*joystick->hwdata));
slouken@1661
   264
	joystick->hwdata->buffered = 1;
slouken@1661
   265
	
slouken@1661
   266
	result = IDirectInput_CreateDevice(dinput, &SYS_Joystick[joystick->index].guidInstance,
slouken@1661
   267
			    &device, NULL);
slouken@1661
   268
	if ( result != DI_OK ) {
slouken@1661
   269
		SetDIerror("DirectInput::CreateDevice", result);
slouken@1661
   270
		return(-1);
slouken@1661
   271
	}
slouken@1661
   272
slouken@1661
   273
	result = IDirectInputDevice_QueryInterface(device,
slouken@1661
   274
		   	    &IID_IDirectInputDevice2, (LPVOID *)&joystick->hwdata->InputDevice);
slouken@1661
   275
	IDirectInputDevice_Release(device);
slouken@1661
   276
	if ( result != DI_OK ) {
slouken@1661
   277
		SetDIerror("DirectInputDevice::QueryInterface", result);
slouken@1661
   278
		return(-1);
slouken@1661
   279
	}
slouken@1661
   280
slouken@1661
   281
	result = IDirectInputDevice2_SetCooperativeLevel(joystick->hwdata->InputDevice, SDL_Window,
slouken@1661
   282
			 DISCL_NONEXCLUSIVE | DISCL_BACKGROUND);
slouken@1661
   283
	if ( result != DI_OK ) {
slouken@1661
   284
		SetDIerror("DirectInputDevice::SetCooperativeLevel", result);
slouken@1661
   285
		return(-1);
slouken@1661
   286
	}
slouken@1661
   287
slouken@1661
   288
	result = IDirectInputDevice2_SetDataFormat(joystick->hwdata->InputDevice, &c_dfDIJoystick);
slouken@1661
   289
	if ( result != DI_OK ) {
slouken@1661
   290
		SetDIerror("DirectInputDevice::SetDataFormat", result);
slouken@1661
   291
		return(-1);
slouken@1661
   292
	}
slouken@1661
   293
slouken@1661
   294
	IDirectInputDevice2_EnumObjects(joystick->hwdata->InputDevice,
slouken@1661
   295
					DIJoystick_EnumDevObjectsProc,
slouken@1661
   296
					joystick,
slouken@1661
   297
					DIDFT_BUTTON | DIDFT_AXIS | DIDFT_POV);
slouken@1661
   298
slouken@1661
   299
	{
slouken@1661
   300
		DIPROPDWORD dipdw;
slouken@1661
   301
		memset(&dipdw, 0, sizeof(dipdw));
slouken@1661
   302
		dipdw.diph.dwSize = sizeof(dipdw);
slouken@1661
   303
		dipdw.diph.dwHeaderSize = sizeof(dipdw.diph);
slouken@1661
   304
		dipdw.diph.dwObj = 0;
slouken@1661
   305
		dipdw.diph.dwHow = DIPH_DEVICE;
slouken@1661
   306
		dipdw.dwData = INPUT_QSIZE;
slouken@1661
   307
		result = IDirectInputDevice2_SetProperty(joystick->hwdata->InputDevice,
slouken@1661
   308
						DIPROP_BUFFERSIZE, &dipdw.diph);
slouken@1661
   309
slouken@1661
   310
		if ( result == DI_POLLEDDEVICE )
slouken@1661
   311
		{
slouken@1661
   312
			/* This device doesn't support buffering, so we're forced
slouken@1661
   313
			 * to use less reliable polling. */
slouken@1661
   314
			joystick->hwdata->buffered = 0;
slouken@1661
   315
		} else if ( result != DI_OK ) {
slouken@1661
   316
			SetDIerror("DirectInputDevice::SetProperty", result);
slouken@1661
   317
			return(-1);
slouken@1661
   318
		}
slouken@1661
   319
	}
slouken@1661
   320
slouken@1661
   321
	return(0);
slouken@1661
   322
}
slouken@1661
   323
slouken@1661
   324
static Uint8 TranslatePOV(DWORD value)
slouken@1661
   325
{
slouken@1661
   326
	const int HAT_VALS[] = {
slouken@1661
   327
	    SDL_HAT_UP,
slouken@1661
   328
	    SDL_HAT_UP   | SDL_HAT_RIGHT,
slouken@1661
   329
	    SDL_HAT_RIGHT,
slouken@1661
   330
	    SDL_HAT_DOWN | SDL_HAT_RIGHT,
slouken@1661
   331
	    SDL_HAT_DOWN,
slouken@1661
   332
	    SDL_HAT_DOWN | SDL_HAT_LEFT,
slouken@1661
   333
	    SDL_HAT_LEFT,
slouken@1661
   334
	    SDL_HAT_UP   | SDL_HAT_LEFT
slouken@1661
   335
	};
slouken@1661
   336
slouken@1661
   337
	if(LOWORD(value) == 0xFFFF)
slouken@1661
   338
	    return SDL_HAT_CENTERED;
slouken@1661
   339
slouken@1661
   340
	/* Round the value up: */
slouken@1661
   341
	value += 4500 / 2;
slouken@1661
   342
	value %= 36000;
slouken@1661
   343
	value /= 4500;
slouken@1661
   344
slouken@1661
   345
	if(value >= 8)
slouken@1661
   346
	    return SDL_HAT_CENTERED; /* shouldn't happen */
slouken@1661
   347
	
slouken@1661
   348
	return HAT_VALS[value];
slouken@1661
   349
}
slouken@1661
   350
slouken@1661
   351
/* SDL_PrivateJoystick* doesn't discard duplicate events, so we need to
slouken@1661
   352
 * do it. */
slouken@1661
   353
static int SDL_PrivateJoystickAxis_Int(SDL_Joystick *joystick, Uint8 axis, Sint16 value)
slouken@1661
   354
{
slouken@1661
   355
	if(joystick->axes[axis] != value)
slouken@1661
   356
		return SDL_PrivateJoystickAxis(joystick, axis, value);
slouken@1661
   357
	return 0;
slouken@1661
   358
}
slouken@1661
   359
slouken@1661
   360
static int SDL_PrivateJoystickHat_Int(SDL_Joystick *joystick, Uint8 hat, Uint8 value)
slouken@1661
   361
{
slouken@1661
   362
	if(joystick->hats[hat] != value)
slouken@1661
   363
		return SDL_PrivateJoystickHat(joystick, hat, value);
slouken@1661
   364
	return 0;
slouken@1661
   365
}
slouken@1661
   366
slouken@1661
   367
static int SDL_PrivateJoystickButton_Int(SDL_Joystick *joystick, Uint8 button, Uint8 state)
slouken@1661
   368
{
slouken@1661
   369
	if(joystick->buttons[button] != state)
slouken@1661
   370
		return SDL_PrivateJoystickButton(joystick, button, state);
slouken@1661
   371
	return 0;
slouken@1661
   372
}
slouken@1661
   373
slouken@1661
   374
/* Function to update the state of a joystick - called as a device poll.
slouken@1661
   375
 * This function shouldn't update the joystick structure directly,
slouken@1661
   376
 * but instead should call SDL_PrivateJoystick*() to deliver events
slouken@1661
   377
 * and update joystick device state.
slouken@1661
   378
 */
slouken@1661
   379
void SDL_SYS_JoystickUpdate_Polled(SDL_Joystick *joystick)
slouken@1661
   380
{
slouken@1661
   381
	DIJOYSTATE state;
slouken@1661
   382
	HRESULT  result;
slouken@1661
   383
	int i;
slouken@1661
   384
slouken@1661
   385
	result = IDirectInputDevice2_GetDeviceState(joystick->hwdata->InputDevice, sizeof(state), &state);
slouken@1661
   386
	if ( result == DIERR_INPUTLOST || result == DIERR_NOTACQUIRED ) {
slouken@1661
   387
		IDirectInputDevice2_Acquire(joystick->hwdata->InputDevice);
slouken@1661
   388
		result = IDirectInputDevice2_GetDeviceState(joystick->hwdata->InputDevice, sizeof(state), &state);
slouken@1661
   389
	}
slouken@1661
   390
slouken@1661
   391
	/* Set each known axis, button and POV. */
slouken@1661
   392
	for(i = 0; i < joystick->hwdata->NumInputs; ++i)
slouken@1661
   393
	{
slouken@1661
   394
		const input_t *in = &joystick->hwdata->Inputs[i];
slouken@1661
   395
slouken@1661
   396
		switch(in->type)
slouken@1661
   397
		{
slouken@1661
   398
		case AXIS:
slouken@1661
   399
			switch(in->ofs)
slouken@1661
   400
			{
slouken@1661
   401
			case DIJOFS_X: SDL_PrivateJoystickAxis_Int(joystick, in->num, (Sint16)state.lX); break;
slouken@1661
   402
			case DIJOFS_Y: SDL_PrivateJoystickAxis_Int(joystick, in->num, (Sint16)state.lY); break;
slouken@1661
   403
			case DIJOFS_Z: SDL_PrivateJoystickAxis_Int(joystick, in->num, (Sint16)state.lZ); break;
slouken@1661
   404
			case DIJOFS_RX: SDL_PrivateJoystickAxis_Int(joystick, in->num, (Sint16)state.lRx); break;
slouken@1661
   405
			case DIJOFS_RY: SDL_PrivateJoystickAxis_Int(joystick, in->num, (Sint16)state.lRy); break;
slouken@1661
   406
			case DIJOFS_RZ: SDL_PrivateJoystickAxis_Int(joystick, in->num, (Sint16)state.lRz); break;
slouken@1661
   407
			case DIJOFS_SLIDER(0): SDL_PrivateJoystickAxis_Int(joystick, in->num, (Sint16)state.rglSlider[0]); break;
slouken@1661
   408
			case DIJOFS_SLIDER(1): SDL_PrivateJoystickAxis_Int(joystick, in->num, (Sint16)state.rglSlider[0]); break;
slouken@1661
   409
			}
slouken@1661
   410
slouken@1661
   411
			break;
slouken@1661
   412
slouken@1661
   413
		case BUTTON:
slouken@1661
   414
			SDL_PrivateJoystickButton_Int(joystick, in->num, (Uint8) (state.rgbButtons[in->ofs - DIJOFS_BUTTON0]?SDL_PRESSED:SDL_RELEASED));
slouken@1661
   415
			break;
slouken@1661
   416
		case HAT:
slouken@1661
   417
		    {
slouken@1661
   418
			Uint8 pos = TranslatePOV(state.rgdwPOV[in->ofs - DIJOFS_POV(0)]);
slouken@1661
   419
			SDL_PrivateJoystickHat_Int(joystick, in->num, pos);
slouken@1661
   420
			break;
slouken@1661
   421
		    }
slouken@1661
   422
		}
slouken@1661
   423
	}
slouken@1661
   424
}
slouken@1661
   425
slouken@1661
   426
void SDL_SYS_JoystickUpdate_Buffered(SDL_Joystick *joystick)
slouken@1661
   427
{
slouken@1661
   428
	int i;
slouken@1661
   429
	HRESULT  result;
slouken@1661
   430
	DWORD numevents;
slouken@1661
   431
	DIDEVICEOBJECTDATA evtbuf[INPUT_QSIZE];
slouken@1661
   432
slouken@1661
   433
	numevents = INPUT_QSIZE;
slouken@1661
   434
	result = IDirectInputDevice2_GetDeviceData(
slouken@1661
   435
			joystick->hwdata->InputDevice, sizeof(DIDEVICEOBJECTDATA),
slouken@1661
   436
						evtbuf, &numevents, 0);
slouken@1661
   437
	if ( result == DIERR_INPUTLOST || result == DIERR_NOTACQUIRED ) {
slouken@1661
   438
		IDirectInputDevice2_Acquire(joystick->hwdata->InputDevice);
slouken@1661
   439
		result = IDirectInputDevice2_GetDeviceData(
slouken@1661
   440
			joystick->hwdata->InputDevice, sizeof(DIDEVICEOBJECTDATA),
slouken@1661
   441
						evtbuf, &numevents, 0);
slouken@1661
   442
	}
slouken@1661
   443
slouken@1661
   444
	/* Handle the events */
slouken@1661
   445
	if ( result != DI_OK )
slouken@1661
   446
	    return;
slouken@1661
   447
slouken@1661
   448
	for(i = 0; i < (int) numevents; ++i)
slouken@1661
   449
	{
slouken@1661
   450
		int j;
slouken@1661
   451
slouken@1661
   452
		for(j = 0; j < joystick->hwdata->NumInputs; ++j)
slouken@1661
   453
		{
slouken@1661
   454
			const input_t *in = &joystick->hwdata->Inputs[j];
slouken@1661
   455
slouken@1661
   456
			if(evtbuf[i].dwOfs != in->ofs)
slouken@1661
   457
				continue;
slouken@1661
   458
		
slouken@1661
   459
			switch(in->type)
slouken@1661
   460
			{
slouken@1661
   461
			case AXIS:
slouken@1661
   462
				SDL_PrivateJoystickAxis(joystick, in->num, (Sint16)evtbuf[i].dwData);
slouken@1661
   463
				break;
slouken@1661
   464
			case BUTTON:
slouken@1661
   465
				SDL_PrivateJoystickButton(joystick, in->num, (Uint8) (evtbuf[i].dwData?SDL_PRESSED:SDL_RELEASED));
slouken@1661
   466
				break;
slouken@1661
   467
			case HAT:
slouken@1661
   468
			    {
slouken@1661
   469
				Uint8 pos = TranslatePOV(evtbuf[i].dwData);
slouken@1661
   470
				SDL_PrivateJoystickHat(joystick, in->num, pos);
slouken@1661
   471
			    }
slouken@1661
   472
			}
slouken@1661
   473
		}
slouken@1661
   474
	}
slouken@1661
   475
}
slouken@1661
   476
slouken@1661
   477
void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
slouken@1661
   478
{
slouken@1661
   479
	HRESULT  result;
slouken@1661
   480
slouken@1661
   481
	result = IDirectInputDevice2_Poll(joystick->hwdata->InputDevice);
slouken@1661
   482
	if (result == DIERR_INPUTLOST || result == DIERR_NOTACQUIRED) {
slouken@1661
   483
		IDirectInputDevice2_Acquire(joystick->hwdata->InputDevice);
slouken@1661
   484
		IDirectInputDevice2_Poll(joystick->hwdata->InputDevice);
slouken@1661
   485
	}
slouken@1661
   486
slouken@1661
   487
	if(joystick->hwdata->buffered)
slouken@1661
   488
		SDL_SYS_JoystickUpdate_Buffered(joystick);
slouken@1661
   489
	else
slouken@1661
   490
		SDL_SYS_JoystickUpdate_Polled(joystick);
slouken@1661
   491
}
slouken@1661
   492
slouken@1661
   493
/* Function to close a joystick after use */
slouken@1661
   494
void SDL_SYS_JoystickClose(SDL_Joystick *joystick)
slouken@1661
   495
{
slouken@1661
   496
	IDirectInputDevice2_Unacquire(joystick->hwdata->InputDevice);
slouken@1661
   497
	IDirectInputDevice2_Release(joystick->hwdata->InputDevice);
slouken@1661
   498
slouken@1661
   499
	if (joystick->hwdata != NULL) {
slouken@1661
   500
		/* free system specific hardware data */
slouken@1661
   501
		free(joystick->hwdata);
slouken@1661
   502
	}
slouken@1661
   503
}
slouken@1661
   504
slouken@1661
   505
/* Function to perform any system-specific joystick related cleanup */
slouken@1661
   506
void SDL_SYS_JoystickQuit(void)
slouken@1661
   507
{
slouken@1661
   508
	IDirectInput_Release(dinput);
slouken@1661
   509
	dinput = NULL;
slouken@1661
   510
	DX5_Unload();
slouken@1661
   511
}
slouken@1661
   512
slouken@1661
   513
#endif /* SDL_JOYSTICK_DINPUT */