src/joystick/win32/SDL_dxjoystick.c
branchSDL-1.3
changeset 1661 281d3f4870e5
child 1662 782fd950bd46
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/joystick/win32/SDL_dxjoystick.c	Sun May 21 17:27:13 2006 +0000
     1.3 @@ -0,0 +1,513 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997-2006 Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Lesser General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2.1 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Lesser General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Lesser General Public
    1.19 +    License along with this library; if not, write to the Free Software
    1.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@libsdl.org
    1.24 +*/
    1.25 +#include "SDL_config.h"
    1.26 +
    1.27 +#ifdef SDL_JOYSTICK_DINPUT
    1.28 +
    1.29 +/* DirectInput joystick driver; written by Glenn Maynard, based on Andrei de
    1.30 + * A. Formiga's WINMM driver. 
    1.31 + *
    1.32 + * Hats and sliders are completely untested; the app I'm writing this for mostly
    1.33 + * doesn't use them and I don't own any joysticks with them. 
    1.34 + *
    1.35 + * We don't bother to use event notification here.  It doesn't seem to work
    1.36 + * with polled devices, and it's fine to call IDirectInputDevice2_GetDeviceData and
    1.37 + * let it return 0 events. */
    1.38 +
    1.39 +#include "SDL_error.h"
    1.40 +#include "SDL_events.h"
    1.41 +#include "SDL_joystick.h"
    1.42 +#include "../SDL_sysjoystick.h"
    1.43 +#include "../SDL_joystick_c.h"
    1.44 +
    1.45 +#define WIN32_LEAN_AND_MEAN
    1.46 +#include <windows.h>
    1.47 +
    1.48 +#define DIRECTINPUT_VERSION 0x0500
    1.49 +#include <dinput.h>
    1.50 +
    1.51 +#define INPUT_QSIZE	32		/* Buffer up to 32 input messages */
    1.52 +
    1.53 +extern HINSTANCE SDL_Instance;
    1.54 +extern int DX5_Load();
    1.55 +extern void DX5_Unload();
    1.56 +extern HRESULT (WINAPI *DInputCreate)(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUT *ppDI, LPUNKNOWN punkOuter);
    1.57 +
    1.58 +static LPDIRECTINPUT dinput = NULL;
    1.59 +
    1.60 +#define MAX_JOYSTICKS	8
    1.61 +#define MAX_INPUTS	256	/* each joystick can have up to 256 inputs */
    1.62 +#define AXIS_MIN	-32768  /* minimum value for axis coordinate */
    1.63 +#define AXIS_MAX	32767   /* maximum value for axis coordinate */
    1.64 +#define JOY_AXIS_THRESHOLD	(((AXIS_MAX)-(AXIS_MIN))/100) /* 1% motion */
    1.65 +
    1.66 +typedef enum Type { BUTTON, AXIS, HAT } Type;
    1.67 +
    1.68 +/* array to hold joystick ID values */
    1.69 +static DIDEVICEINSTANCE SYS_Joystick[MAX_JOYSTICKS];
    1.70 +static int	SYS_NumJoysticks;
    1.71 +
    1.72 +extern HWND SDL_Window;
    1.73 +
    1.74 +typedef struct input_t
    1.75 +{
    1.76 +	/* DirectInput offset for this input type: */
    1.77 +	DWORD ofs;
    1.78 +
    1.79 +	/* Button, axis or hat: */
    1.80 +	Type type;
    1.81 +
    1.82 +	/* SDL input offset: */
    1.83 +	Uint8 num;
    1.84 +} input_t;
    1.85 +
    1.86 +/* The private structure used to keep track of a joystick */
    1.87 +struct joystick_hwdata
    1.88 +{
    1.89 +	LPDIRECTINPUTDEVICE2 InputDevice;
    1.90 +	int buffered;
    1.91 +
    1.92 +	input_t Inputs[MAX_INPUTS];
    1.93 +	int NumInputs;
    1.94 +};
    1.95 +
    1.96 +/* Convert a DirectInput return code to a text message */
    1.97 +static void SetDIerror(char *function, int code)
    1.98 +{
    1.99 +	static char *error;
   1.100 +	static char  errbuf[1024];
   1.101 +
   1.102 +	errbuf[0] = 0;
   1.103 +	switch (code) {
   1.104 +                case DIERR_GENERIC:
   1.105 +                        error = "Undefined error!";
   1.106 +                        break;
   1.107 +		case DIERR_OLDDIRECTINPUTVERSION:
   1.108 +			error = "Your version of DirectInput needs upgrading";
   1.109 +			break;
   1.110 +		case DIERR_INVALIDPARAM:
   1.111 +                        error = "Invalid parameters";
   1.112 +                        break;
   1.113 +                case DIERR_OUTOFMEMORY:
   1.114 +                        error = "Out of memory";
   1.115 +                        break;
   1.116 +		case DIERR_DEVICENOTREG:
   1.117 +			error = "Device not registered";
   1.118 +			break;
   1.119 +		case DIERR_NOINTERFACE:
   1.120 +			error = "Interface not supported";
   1.121 +			break;
   1.122 +		case DIERR_NOTINITIALIZED:
   1.123 +			error = "Device not initialized";
   1.124 +			break;
   1.125 +		default:
   1.126 +			sprintf(errbuf, "%s: Unknown DirectInput error: 0x%x",
   1.127 +								function, code);
   1.128 +			break;
   1.129 +	}
   1.130 +	if ( ! errbuf[0] ) {
   1.131 +		sprintf(errbuf, "%s: %s", function, error);
   1.132 +	}
   1.133 +	SDL_SetError("%s", errbuf);
   1.134 +	return;
   1.135 +}
   1.136 +
   1.137 +
   1.138 +BOOL CALLBACK EnumJoysticksCallback( const DIDEVICEINSTANCE* pdidInstance,
   1.139 +				     VOID* pContext )
   1.140 +{
   1.141 +	memcpy(&SYS_Joystick[SYS_NumJoysticks], pdidInstance, sizeof(DIDEVICEINSTANCE));
   1.142 +	SYS_NumJoysticks++;
   1.143 +
   1.144 +	if( SYS_NumJoysticks >= MAX_JOYSTICKS )
   1.145 +	        return DIENUM_STOP;
   1.146 +
   1.147 +	return DIENUM_CONTINUE;
   1.148 +}
   1.149 +
   1.150 +static BOOL CALLBACK DIJoystick_EnumDevObjectsProc(LPCDIDEVICEOBJECTINSTANCE dev,
   1.151 +						    LPVOID pvRef)
   1.152 +{
   1.153 +	SDL_Joystick *joystick = (SDL_Joystick*)pvRef;
   1.154 +	HRESULT result;
   1.155 +	input_t *in = &joystick->hwdata->Inputs[joystick->hwdata->NumInputs];
   1.156 +	const int SupportedMask = DIDFT_BUTTON | DIDFT_POV | DIDFT_AXIS;
   1.157 +	if(!(dev->dwType & SupportedMask))
   1.158 +	    return DIENUM_CONTINUE; /* unsupported */
   1.159 +
   1.160 +	in->ofs = dev->dwOfs;
   1.161 +
   1.162 +	if(dev->dwType & DIDFT_BUTTON) {
   1.163 +		in->type = BUTTON;
   1.164 +		in->num = joystick->nbuttons;
   1.165 +		joystick->nbuttons++;
   1.166 +	} else if(dev->dwType & DIDFT_POV) {
   1.167 +		in->type = HAT;
   1.168 +		in->num = joystick->nhats;
   1.169 +		joystick->nhats++;
   1.170 +	} else { /* dev->dwType & DIDFT_AXIS */
   1.171 +		DIPROPRANGE diprg;
   1.172 +		DIPROPDWORD dilong;
   1.173 +		
   1.174 +		in->type = AXIS;
   1.175 +		in->num = joystick->naxes;
   1.176 +		
   1.177 +		diprg.diph.dwSize		= sizeof(diprg);
   1.178 +		diprg.diph.dwHeaderSize	= sizeof(diprg.diph);
   1.179 +		diprg.diph.dwObj		= dev->dwOfs;
   1.180 +		diprg.diph.dwHow		= DIPH_BYOFFSET;
   1.181 +		diprg.lMin			= AXIS_MIN;
   1.182 +		diprg.lMax			= AXIS_MAX;
   1.183 +
   1.184 +		result = IDirectInputDevice2_SetProperty(joystick->hwdata->InputDevice, DIPROP_RANGE, &diprg.diph);
   1.185 +		if ( result != DI_OK )
   1.186 +			return DIENUM_CONTINUE; /* don't use this axis */
   1.187 +	
   1.188 +		/* Set dead zone to 0. */
   1.189 +		dilong.diph.dwSize		= sizeof(dilong);
   1.190 +		dilong.diph.dwHeaderSize	= sizeof(dilong.diph);
   1.191 +		dilong.diph.dwObj		= dev->dwOfs;
   1.192 +		dilong.diph.dwHow		= DIPH_BYOFFSET;
   1.193 +		dilong.dwData = 0;
   1.194 +		result = IDirectInputDevice2_SetProperty(joystick->hwdata->InputDevice, DIPROP_DEADZONE, &dilong.diph);
   1.195 +		if ( result != DI_OK )
   1.196 +			return DIENUM_CONTINUE; /* don't use this axis */
   1.197 +
   1.198 +		joystick->naxes++;
   1.199 +	}
   1.200 +
   1.201 +	joystick->hwdata->NumInputs++;
   1.202 +
   1.203 +	if(joystick->hwdata->NumInputs == MAX_INPUTS)
   1.204 +		return DIENUM_STOP; /* too many */
   1.205 +
   1.206 +	return DIENUM_CONTINUE;
   1.207 +}
   1.208 +
   1.209 +/* Function to scan the system for joysticks.
   1.210 + * This function should set SDL_numjoysticks to the number of available
   1.211 + * joysticks.  Joystick 0 should be the system default joystick.
   1.212 + * It should return 0, or -1 on an unrecoverable fatal error.
   1.213 + */
   1.214 +int SDL_SYS_JoystickInit(void)
   1.215 +{
   1.216 +	HRESULT result;
   1.217 +
   1.218 +	SYS_NumJoysticks = 0;
   1.219 +
   1.220 +	/* Create the DirectInput object */
   1.221 +	if ( DX5_Load() < 0 ) {
   1.222 +		SDL_SetError("Couldn't load DirectInput");
   1.223 +		return(-1);
   1.224 +	}
   1.225 +	result = DInputCreate(SDL_Instance, DIRECTINPUT_VERSION,
   1.226 +							&dinput, NULL);
   1.227 +	if ( result != DI_OK ) {
   1.228 +		DX5_Unload();
   1.229 +		SetDIerror("DirectInputCreate", result);
   1.230 +		return(-1);
   1.231 +	}
   1.232 +
   1.233 +	result = IDirectInput_EnumDevices(dinput,
   1.234 +			DIDEVTYPE_JOYSTICK, 
   1.235 +			EnumJoysticksCallback,
   1.236 +   			NULL,
   1.237 +			DIEDFL_ATTACHEDONLY );
   1.238 +
   1.239 +	return SYS_NumJoysticks;
   1.240 +}
   1.241 +
   1.242 +/* Function to get the device-dependent name of a joystick */
   1.243 +const char *SDL_SYS_JoystickName(int index)
   1.244 +{
   1.245 +	/***-> test for invalid index ? */
   1.246 +	return(SYS_Joystick[index].tszProductName);
   1.247 +}
   1.248 +
   1.249 +/* Function to open a joystick for use.
   1.250 +   The joystick to open is specified by the index field of the joystick.
   1.251 +   This should fill the nbuttons and naxes fields of the joystick structure.
   1.252 +   It returns 0, or -1 if there is an error.
   1.253 + */
   1.254 +int SDL_SYS_JoystickOpen(SDL_Joystick *joystick)
   1.255 +{
   1.256 +	HRESULT result;
   1.257 +	LPDIRECTINPUTDEVICE device;
   1.258 +
   1.259 +	/* allocate memory for system specific hardware data */
   1.260 +	joystick->hwdata = (struct joystick_hwdata *) malloc(sizeof(*joystick->hwdata));
   1.261 +	if (joystick->hwdata == NULL)
   1.262 +	{
   1.263 +		SDL_OutOfMemory();
   1.264 +		return(-1);
   1.265 +	}
   1.266 +	memset(joystick->hwdata, 0, sizeof(*joystick->hwdata));
   1.267 +	joystick->hwdata->buffered = 1;
   1.268 +	
   1.269 +	result = IDirectInput_CreateDevice(dinput, &SYS_Joystick[joystick->index].guidInstance,
   1.270 +			    &device, NULL);
   1.271 +	if ( result != DI_OK ) {
   1.272 +		SetDIerror("DirectInput::CreateDevice", result);
   1.273 +		return(-1);
   1.274 +	}
   1.275 +
   1.276 +	result = IDirectInputDevice_QueryInterface(device,
   1.277 +		   	    &IID_IDirectInputDevice2, (LPVOID *)&joystick->hwdata->InputDevice);
   1.278 +	IDirectInputDevice_Release(device);
   1.279 +	if ( result != DI_OK ) {
   1.280 +		SetDIerror("DirectInputDevice::QueryInterface", result);
   1.281 +		return(-1);
   1.282 +	}
   1.283 +
   1.284 +	result = IDirectInputDevice2_SetCooperativeLevel(joystick->hwdata->InputDevice, SDL_Window,
   1.285 +			 DISCL_NONEXCLUSIVE | DISCL_BACKGROUND);
   1.286 +	if ( result != DI_OK ) {
   1.287 +		SetDIerror("DirectInputDevice::SetCooperativeLevel", result);
   1.288 +		return(-1);
   1.289 +	}
   1.290 +
   1.291 +	result = IDirectInputDevice2_SetDataFormat(joystick->hwdata->InputDevice, &c_dfDIJoystick);
   1.292 +	if ( result != DI_OK ) {
   1.293 +		SetDIerror("DirectInputDevice::SetDataFormat", result);
   1.294 +		return(-1);
   1.295 +	}
   1.296 +
   1.297 +	IDirectInputDevice2_EnumObjects(joystick->hwdata->InputDevice,
   1.298 +					DIJoystick_EnumDevObjectsProc,
   1.299 +					joystick,
   1.300 +					DIDFT_BUTTON | DIDFT_AXIS | DIDFT_POV);
   1.301 +
   1.302 +	{
   1.303 +		DIPROPDWORD dipdw;
   1.304 +		memset(&dipdw, 0, sizeof(dipdw));
   1.305 +		dipdw.diph.dwSize = sizeof(dipdw);
   1.306 +		dipdw.diph.dwHeaderSize = sizeof(dipdw.diph);
   1.307 +		dipdw.diph.dwObj = 0;
   1.308 +		dipdw.diph.dwHow = DIPH_DEVICE;
   1.309 +		dipdw.dwData = INPUT_QSIZE;
   1.310 +		result = IDirectInputDevice2_SetProperty(joystick->hwdata->InputDevice,
   1.311 +						DIPROP_BUFFERSIZE, &dipdw.diph);
   1.312 +
   1.313 +		if ( result == DI_POLLEDDEVICE )
   1.314 +		{
   1.315 +			/* This device doesn't support buffering, so we're forced
   1.316 +			 * to use less reliable polling. */
   1.317 +			joystick->hwdata->buffered = 0;
   1.318 +		} else if ( result != DI_OK ) {
   1.319 +			SetDIerror("DirectInputDevice::SetProperty", result);
   1.320 +			return(-1);
   1.321 +		}
   1.322 +	}
   1.323 +
   1.324 +	return(0);
   1.325 +}
   1.326 +
   1.327 +static Uint8 TranslatePOV(DWORD value)
   1.328 +{
   1.329 +	const int HAT_VALS[] = {
   1.330 +	    SDL_HAT_UP,
   1.331 +	    SDL_HAT_UP   | SDL_HAT_RIGHT,
   1.332 +	    SDL_HAT_RIGHT,
   1.333 +	    SDL_HAT_DOWN | SDL_HAT_RIGHT,
   1.334 +	    SDL_HAT_DOWN,
   1.335 +	    SDL_HAT_DOWN | SDL_HAT_LEFT,
   1.336 +	    SDL_HAT_LEFT,
   1.337 +	    SDL_HAT_UP   | SDL_HAT_LEFT
   1.338 +	};
   1.339 +
   1.340 +	if(LOWORD(value) == 0xFFFF)
   1.341 +	    return SDL_HAT_CENTERED;
   1.342 +
   1.343 +	/* Round the value up: */
   1.344 +	value += 4500 / 2;
   1.345 +	value %= 36000;
   1.346 +	value /= 4500;
   1.347 +
   1.348 +	if(value >= 8)
   1.349 +	    return SDL_HAT_CENTERED; /* shouldn't happen */
   1.350 +	
   1.351 +	return HAT_VALS[value];
   1.352 +}
   1.353 +
   1.354 +/* SDL_PrivateJoystick* doesn't discard duplicate events, so we need to
   1.355 + * do it. */
   1.356 +static int SDL_PrivateJoystickAxis_Int(SDL_Joystick *joystick, Uint8 axis, Sint16 value)
   1.357 +{
   1.358 +	if(joystick->axes[axis] != value)
   1.359 +		return SDL_PrivateJoystickAxis(joystick, axis, value);
   1.360 +	return 0;
   1.361 +}
   1.362 +
   1.363 +static int SDL_PrivateJoystickHat_Int(SDL_Joystick *joystick, Uint8 hat, Uint8 value)
   1.364 +{
   1.365 +	if(joystick->hats[hat] != value)
   1.366 +		return SDL_PrivateJoystickHat(joystick, hat, value);
   1.367 +	return 0;
   1.368 +}
   1.369 +
   1.370 +static int SDL_PrivateJoystickButton_Int(SDL_Joystick *joystick, Uint8 button, Uint8 state)
   1.371 +{
   1.372 +	if(joystick->buttons[button] != state)
   1.373 +		return SDL_PrivateJoystickButton(joystick, button, state);
   1.374 +	return 0;
   1.375 +}
   1.376 +
   1.377 +/* Function to update the state of a joystick - called as a device poll.
   1.378 + * This function shouldn't update the joystick structure directly,
   1.379 + * but instead should call SDL_PrivateJoystick*() to deliver events
   1.380 + * and update joystick device state.
   1.381 + */
   1.382 +void SDL_SYS_JoystickUpdate_Polled(SDL_Joystick *joystick)
   1.383 +{
   1.384 +	DIJOYSTATE state;
   1.385 +	HRESULT  result;
   1.386 +	int i;
   1.387 +
   1.388 +	result = IDirectInputDevice2_GetDeviceState(joystick->hwdata->InputDevice, sizeof(state), &state);
   1.389 +	if ( result == DIERR_INPUTLOST || result == DIERR_NOTACQUIRED ) {
   1.390 +		IDirectInputDevice2_Acquire(joystick->hwdata->InputDevice);
   1.391 +		result = IDirectInputDevice2_GetDeviceState(joystick->hwdata->InputDevice, sizeof(state), &state);
   1.392 +	}
   1.393 +
   1.394 +	/* Set each known axis, button and POV. */
   1.395 +	for(i = 0; i < joystick->hwdata->NumInputs; ++i)
   1.396 +	{
   1.397 +		const input_t *in = &joystick->hwdata->Inputs[i];
   1.398 +
   1.399 +		switch(in->type)
   1.400 +		{
   1.401 +		case AXIS:
   1.402 +			switch(in->ofs)
   1.403 +			{
   1.404 +			case DIJOFS_X: SDL_PrivateJoystickAxis_Int(joystick, in->num, (Sint16)state.lX); break;
   1.405 +			case DIJOFS_Y: SDL_PrivateJoystickAxis_Int(joystick, in->num, (Sint16)state.lY); break;
   1.406 +			case DIJOFS_Z: SDL_PrivateJoystickAxis_Int(joystick, in->num, (Sint16)state.lZ); break;
   1.407 +			case DIJOFS_RX: SDL_PrivateJoystickAxis_Int(joystick, in->num, (Sint16)state.lRx); break;
   1.408 +			case DIJOFS_RY: SDL_PrivateJoystickAxis_Int(joystick, in->num, (Sint16)state.lRy); break;
   1.409 +			case DIJOFS_RZ: SDL_PrivateJoystickAxis_Int(joystick, in->num, (Sint16)state.lRz); break;
   1.410 +			case DIJOFS_SLIDER(0): SDL_PrivateJoystickAxis_Int(joystick, in->num, (Sint16)state.rglSlider[0]); break;
   1.411 +			case DIJOFS_SLIDER(1): SDL_PrivateJoystickAxis_Int(joystick, in->num, (Sint16)state.rglSlider[0]); break;
   1.412 +			}
   1.413 +
   1.414 +			break;
   1.415 +
   1.416 +		case BUTTON:
   1.417 +			SDL_PrivateJoystickButton_Int(joystick, in->num, (Uint8) (state.rgbButtons[in->ofs - DIJOFS_BUTTON0]?SDL_PRESSED:SDL_RELEASED));
   1.418 +			break;
   1.419 +		case HAT:
   1.420 +		    {
   1.421 +			Uint8 pos = TranslatePOV(state.rgdwPOV[in->ofs - DIJOFS_POV(0)]);
   1.422 +			SDL_PrivateJoystickHat_Int(joystick, in->num, pos);
   1.423 +			break;
   1.424 +		    }
   1.425 +		}
   1.426 +	}
   1.427 +}
   1.428 +
   1.429 +void SDL_SYS_JoystickUpdate_Buffered(SDL_Joystick *joystick)
   1.430 +{
   1.431 +	int i;
   1.432 +	HRESULT  result;
   1.433 +	DWORD numevents;
   1.434 +	DIDEVICEOBJECTDATA evtbuf[INPUT_QSIZE];
   1.435 +
   1.436 +	numevents = INPUT_QSIZE;
   1.437 +	result = IDirectInputDevice2_GetDeviceData(
   1.438 +			joystick->hwdata->InputDevice, sizeof(DIDEVICEOBJECTDATA),
   1.439 +						evtbuf, &numevents, 0);
   1.440 +	if ( result == DIERR_INPUTLOST || result == DIERR_NOTACQUIRED ) {
   1.441 +		IDirectInputDevice2_Acquire(joystick->hwdata->InputDevice);
   1.442 +		result = IDirectInputDevice2_GetDeviceData(
   1.443 +			joystick->hwdata->InputDevice, sizeof(DIDEVICEOBJECTDATA),
   1.444 +						evtbuf, &numevents, 0);
   1.445 +	}
   1.446 +
   1.447 +	/* Handle the events */
   1.448 +	if ( result != DI_OK )
   1.449 +	    return;
   1.450 +
   1.451 +	for(i = 0; i < (int) numevents; ++i)
   1.452 +	{
   1.453 +		int j;
   1.454 +
   1.455 +		for(j = 0; j < joystick->hwdata->NumInputs; ++j)
   1.456 +		{
   1.457 +			const input_t *in = &joystick->hwdata->Inputs[j];
   1.458 +
   1.459 +			if(evtbuf[i].dwOfs != in->ofs)
   1.460 +				continue;
   1.461 +		
   1.462 +			switch(in->type)
   1.463 +			{
   1.464 +			case AXIS:
   1.465 +				SDL_PrivateJoystickAxis(joystick, in->num, (Sint16)evtbuf[i].dwData);
   1.466 +				break;
   1.467 +			case BUTTON:
   1.468 +				SDL_PrivateJoystickButton(joystick, in->num, (Uint8) (evtbuf[i].dwData?SDL_PRESSED:SDL_RELEASED));
   1.469 +				break;
   1.470 +			case HAT:
   1.471 +			    {
   1.472 +				Uint8 pos = TranslatePOV(evtbuf[i].dwData);
   1.473 +				SDL_PrivateJoystickHat(joystick, in->num, pos);
   1.474 +			    }
   1.475 +			}
   1.476 +		}
   1.477 +	}
   1.478 +}
   1.479 +
   1.480 +void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
   1.481 +{
   1.482 +	HRESULT  result;
   1.483 +
   1.484 +	result = IDirectInputDevice2_Poll(joystick->hwdata->InputDevice);
   1.485 +	if (result == DIERR_INPUTLOST || result == DIERR_NOTACQUIRED) {
   1.486 +		IDirectInputDevice2_Acquire(joystick->hwdata->InputDevice);
   1.487 +		IDirectInputDevice2_Poll(joystick->hwdata->InputDevice);
   1.488 +	}
   1.489 +
   1.490 +	if(joystick->hwdata->buffered)
   1.491 +		SDL_SYS_JoystickUpdate_Buffered(joystick);
   1.492 +	else
   1.493 +		SDL_SYS_JoystickUpdate_Polled(joystick);
   1.494 +}
   1.495 +
   1.496 +/* Function to close a joystick after use */
   1.497 +void SDL_SYS_JoystickClose(SDL_Joystick *joystick)
   1.498 +{
   1.499 +	IDirectInputDevice2_Unacquire(joystick->hwdata->InputDevice);
   1.500 +	IDirectInputDevice2_Release(joystick->hwdata->InputDevice);
   1.501 +
   1.502 +	if (joystick->hwdata != NULL) {
   1.503 +		/* free system specific hardware data */
   1.504 +		free(joystick->hwdata);
   1.505 +	}
   1.506 +}
   1.507 +
   1.508 +/* Function to perform any system-specific joystick related cleanup */
   1.509 +void SDL_SYS_JoystickQuit(void)
   1.510 +{
   1.511 +	IDirectInput_Release(dinput);
   1.512 +	dinput = NULL;
   1.513 +	DX5_Unload();
   1.514 +}
   1.515 +
   1.516 +#endif /* SDL_JOYSTICK_DINPUT */