Moved DirectInput joystick code to 1.3 branch SDL-1.3
authorSam Lantinga <slouken@libsdl.org>
Sun, 21 May 2006 17:27:13 +0000
branchSDL-1.3
changeset 1661281d3f4870e5
parent 1660 8b9d79e7eacf
child 1662 782fd950bd46
Moved DirectInput joystick code to 1.3 branch
VisualC.zip
configure.in
include/SDL_config.h.in
include/SDL_config_win32.h
src/joystick/win32/SDL_dxjoystick.c
src/video/windx5/SDL_dx5video.c
test/testjoystick.c
     1.1 Binary file VisualC.zip has changed
     2.1 --- a/configure.in	Sat May 20 04:35:58 2006 +0000
     2.2 +++ b/configure.in	Sun May 21 17:27:13 2006 +0000
     2.3 @@ -2287,8 +2287,13 @@
     2.4          fi
     2.5          # Set up files for the joystick library
     2.6          if test x$enable_joystick = xyes; then
     2.7 -            AC_DEFINE(SDL_JOYSTICK_WINMM)
     2.8 -            SOURCES="$SOURCES $srcdir/src/joystick/win32/*.c"
     2.9 +            if test x$have_directx = xyes; then
    2.10 +                AC_DEFINE(SDL_JOYSTICK_DINPUT)
    2.11 +                SOURCES="$SOURCES $srcdir/src/joystick/win32/SDL_dxjoystick.c"
    2.12 +            else
    2.13 +                AC_DEFINE(SDL_JOYSTICK_WINMM)
    2.14 +                SOURCES="$SOURCES $srcdir/src/joystick/win32/SDL_mmjoystick.c"
    2.15 +            fi
    2.16              have_joystick=yes
    2.17          fi
    2.18          # Set up files for the cdrom library
     3.1 --- a/include/SDL_config.h.in	Sat May 20 04:35:58 2006 +0000
     3.2 +++ b/include/SDL_config.h.in	Sun May 21 17:27:13 2006 +0000
     3.3 @@ -198,6 +198,7 @@
     3.4  #undef SDL_JOYSTICK_AMIGA
     3.5  #undef SDL_JOYSTICK_BEOS
     3.6  #undef SDL_JOYSTICK_DC
     3.7 +#undef SDL_JOYSTICK_DINPUT
     3.8  #undef SDL_JOYSTICK_DUMMY
     3.9  #undef SDL_JOYSTICK_IOKIT
    3.10  #undef SDL_JOYSTICK_LINUX
     4.1 --- a/include/SDL_config_win32.h	Sat May 20 04:35:58 2006 +0000
     4.2 +++ b/include/SDL_config_win32.h	Sun May 21 17:27:13 2006 +0000
     4.3 @@ -131,7 +131,7 @@
     4.4  #ifdef _WIN32_WCE
     4.5  #define SDL_JOYSTICK_DISABLED   1
     4.6  #else
     4.7 -#define SDL_JOYSTICK_WINMM	1
     4.8 +#define SDL_JOYSTICK_DINPUT	1
     4.9  #endif
    4.10  
    4.11  /* Enable various shared object loading systems */
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/src/joystick/win32/SDL_dxjoystick.c	Sun May 21 17:27:13 2006 +0000
     5.3 @@ -0,0 +1,513 @@
     5.4 +/*
     5.5 +    SDL - Simple DirectMedia Layer
     5.6 +    Copyright (C) 1997-2006 Sam Lantinga
     5.7 +
     5.8 +    This library is free software; you can redistribute it and/or
     5.9 +    modify it under the terms of the GNU Lesser General Public
    5.10 +    License as published by the Free Software Foundation; either
    5.11 +    version 2.1 of the License, or (at your option) any later version.
    5.12 +
    5.13 +    This library is distributed in the hope that it will be useful,
    5.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    5.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    5.16 +    Lesser General Public License for more details.
    5.17 +
    5.18 +    You should have received a copy of the GNU Lesser General Public
    5.19 +    License along with this library; if not, write to the Free Software
    5.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    5.21 +
    5.22 +    Sam Lantinga
    5.23 +    slouken@libsdl.org
    5.24 +*/
    5.25 +#include "SDL_config.h"
    5.26 +
    5.27 +#ifdef SDL_JOYSTICK_DINPUT
    5.28 +
    5.29 +/* DirectInput joystick driver; written by Glenn Maynard, based on Andrei de
    5.30 + * A. Formiga's WINMM driver. 
    5.31 + *
    5.32 + * Hats and sliders are completely untested; the app I'm writing this for mostly
    5.33 + * doesn't use them and I don't own any joysticks with them. 
    5.34 + *
    5.35 + * We don't bother to use event notification here.  It doesn't seem to work
    5.36 + * with polled devices, and it's fine to call IDirectInputDevice2_GetDeviceData and
    5.37 + * let it return 0 events. */
    5.38 +
    5.39 +#include "SDL_error.h"
    5.40 +#include "SDL_events.h"
    5.41 +#include "SDL_joystick.h"
    5.42 +#include "../SDL_sysjoystick.h"
    5.43 +#include "../SDL_joystick_c.h"
    5.44 +
    5.45 +#define WIN32_LEAN_AND_MEAN
    5.46 +#include <windows.h>
    5.47 +
    5.48 +#define DIRECTINPUT_VERSION 0x0500
    5.49 +#include <dinput.h>
    5.50 +
    5.51 +#define INPUT_QSIZE	32		/* Buffer up to 32 input messages */
    5.52 +
    5.53 +extern HINSTANCE SDL_Instance;
    5.54 +extern int DX5_Load();
    5.55 +extern void DX5_Unload();
    5.56 +extern HRESULT (WINAPI *DInputCreate)(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUT *ppDI, LPUNKNOWN punkOuter);
    5.57 +
    5.58 +static LPDIRECTINPUT dinput = NULL;
    5.59 +
    5.60 +#define MAX_JOYSTICKS	8
    5.61 +#define MAX_INPUTS	256	/* each joystick can have up to 256 inputs */
    5.62 +#define AXIS_MIN	-32768  /* minimum value for axis coordinate */
    5.63 +#define AXIS_MAX	32767   /* maximum value for axis coordinate */
    5.64 +#define JOY_AXIS_THRESHOLD	(((AXIS_MAX)-(AXIS_MIN))/100) /* 1% motion */
    5.65 +
    5.66 +typedef enum Type { BUTTON, AXIS, HAT } Type;
    5.67 +
    5.68 +/* array to hold joystick ID values */
    5.69 +static DIDEVICEINSTANCE SYS_Joystick[MAX_JOYSTICKS];
    5.70 +static int	SYS_NumJoysticks;
    5.71 +
    5.72 +extern HWND SDL_Window;
    5.73 +
    5.74 +typedef struct input_t
    5.75 +{
    5.76 +	/* DirectInput offset for this input type: */
    5.77 +	DWORD ofs;
    5.78 +
    5.79 +	/* Button, axis or hat: */
    5.80 +	Type type;
    5.81 +
    5.82 +	/* SDL input offset: */
    5.83 +	Uint8 num;
    5.84 +} input_t;
    5.85 +
    5.86 +/* The private structure used to keep track of a joystick */
    5.87 +struct joystick_hwdata
    5.88 +{
    5.89 +	LPDIRECTINPUTDEVICE2 InputDevice;
    5.90 +	int buffered;
    5.91 +
    5.92 +	input_t Inputs[MAX_INPUTS];
    5.93 +	int NumInputs;
    5.94 +};
    5.95 +
    5.96 +/* Convert a DirectInput return code to a text message */
    5.97 +static void SetDIerror(char *function, int code)
    5.98 +{
    5.99 +	static char *error;
   5.100 +	static char  errbuf[1024];
   5.101 +
   5.102 +	errbuf[0] = 0;
   5.103 +	switch (code) {
   5.104 +                case DIERR_GENERIC:
   5.105 +                        error = "Undefined error!";
   5.106 +                        break;
   5.107 +		case DIERR_OLDDIRECTINPUTVERSION:
   5.108 +			error = "Your version of DirectInput needs upgrading";
   5.109 +			break;
   5.110 +		case DIERR_INVALIDPARAM:
   5.111 +                        error = "Invalid parameters";
   5.112 +                        break;
   5.113 +                case DIERR_OUTOFMEMORY:
   5.114 +                        error = "Out of memory";
   5.115 +                        break;
   5.116 +		case DIERR_DEVICENOTREG:
   5.117 +			error = "Device not registered";
   5.118 +			break;
   5.119 +		case DIERR_NOINTERFACE:
   5.120 +			error = "Interface not supported";
   5.121 +			break;
   5.122 +		case DIERR_NOTINITIALIZED:
   5.123 +			error = "Device not initialized";
   5.124 +			break;
   5.125 +		default:
   5.126 +			sprintf(errbuf, "%s: Unknown DirectInput error: 0x%x",
   5.127 +								function, code);
   5.128 +			break;
   5.129 +	}
   5.130 +	if ( ! errbuf[0] ) {
   5.131 +		sprintf(errbuf, "%s: %s", function, error);
   5.132 +	}
   5.133 +	SDL_SetError("%s", errbuf);
   5.134 +	return;
   5.135 +}
   5.136 +
   5.137 +
   5.138 +BOOL CALLBACK EnumJoysticksCallback( const DIDEVICEINSTANCE* pdidInstance,
   5.139 +				     VOID* pContext )
   5.140 +{
   5.141 +	memcpy(&SYS_Joystick[SYS_NumJoysticks], pdidInstance, sizeof(DIDEVICEINSTANCE));
   5.142 +	SYS_NumJoysticks++;
   5.143 +
   5.144 +	if( SYS_NumJoysticks >= MAX_JOYSTICKS )
   5.145 +	        return DIENUM_STOP;
   5.146 +
   5.147 +	return DIENUM_CONTINUE;
   5.148 +}
   5.149 +
   5.150 +static BOOL CALLBACK DIJoystick_EnumDevObjectsProc(LPCDIDEVICEOBJECTINSTANCE dev,
   5.151 +						    LPVOID pvRef)
   5.152 +{
   5.153 +	SDL_Joystick *joystick = (SDL_Joystick*)pvRef;
   5.154 +	HRESULT result;
   5.155 +	input_t *in = &joystick->hwdata->Inputs[joystick->hwdata->NumInputs];
   5.156 +	const int SupportedMask = DIDFT_BUTTON | DIDFT_POV | DIDFT_AXIS;
   5.157 +	if(!(dev->dwType & SupportedMask))
   5.158 +	    return DIENUM_CONTINUE; /* unsupported */
   5.159 +
   5.160 +	in->ofs = dev->dwOfs;
   5.161 +
   5.162 +	if(dev->dwType & DIDFT_BUTTON) {
   5.163 +		in->type = BUTTON;
   5.164 +		in->num = joystick->nbuttons;
   5.165 +		joystick->nbuttons++;
   5.166 +	} else if(dev->dwType & DIDFT_POV) {
   5.167 +		in->type = HAT;
   5.168 +		in->num = joystick->nhats;
   5.169 +		joystick->nhats++;
   5.170 +	} else { /* dev->dwType & DIDFT_AXIS */
   5.171 +		DIPROPRANGE diprg;
   5.172 +		DIPROPDWORD dilong;
   5.173 +		
   5.174 +		in->type = AXIS;
   5.175 +		in->num = joystick->naxes;
   5.176 +		
   5.177 +		diprg.diph.dwSize		= sizeof(diprg);
   5.178 +		diprg.diph.dwHeaderSize	= sizeof(diprg.diph);
   5.179 +		diprg.diph.dwObj		= dev->dwOfs;
   5.180 +		diprg.diph.dwHow		= DIPH_BYOFFSET;
   5.181 +		diprg.lMin			= AXIS_MIN;
   5.182 +		diprg.lMax			= AXIS_MAX;
   5.183 +
   5.184 +		result = IDirectInputDevice2_SetProperty(joystick->hwdata->InputDevice, DIPROP_RANGE, &diprg.diph);
   5.185 +		if ( result != DI_OK )
   5.186 +			return DIENUM_CONTINUE; /* don't use this axis */
   5.187 +	
   5.188 +		/* Set dead zone to 0. */
   5.189 +		dilong.diph.dwSize		= sizeof(dilong);
   5.190 +		dilong.diph.dwHeaderSize	= sizeof(dilong.diph);
   5.191 +		dilong.diph.dwObj		= dev->dwOfs;
   5.192 +		dilong.diph.dwHow		= DIPH_BYOFFSET;
   5.193 +		dilong.dwData = 0;
   5.194 +		result = IDirectInputDevice2_SetProperty(joystick->hwdata->InputDevice, DIPROP_DEADZONE, &dilong.diph);
   5.195 +		if ( result != DI_OK )
   5.196 +			return DIENUM_CONTINUE; /* don't use this axis */
   5.197 +
   5.198 +		joystick->naxes++;
   5.199 +	}
   5.200 +
   5.201 +	joystick->hwdata->NumInputs++;
   5.202 +
   5.203 +	if(joystick->hwdata->NumInputs == MAX_INPUTS)
   5.204 +		return DIENUM_STOP; /* too many */
   5.205 +
   5.206 +	return DIENUM_CONTINUE;
   5.207 +}
   5.208 +
   5.209 +/* Function to scan the system for joysticks.
   5.210 + * This function should set SDL_numjoysticks to the number of available
   5.211 + * joysticks.  Joystick 0 should be the system default joystick.
   5.212 + * It should return 0, or -1 on an unrecoverable fatal error.
   5.213 + */
   5.214 +int SDL_SYS_JoystickInit(void)
   5.215 +{
   5.216 +	HRESULT result;
   5.217 +
   5.218 +	SYS_NumJoysticks = 0;
   5.219 +
   5.220 +	/* Create the DirectInput object */
   5.221 +	if ( DX5_Load() < 0 ) {
   5.222 +		SDL_SetError("Couldn't load DirectInput");
   5.223 +		return(-1);
   5.224 +	}
   5.225 +	result = DInputCreate(SDL_Instance, DIRECTINPUT_VERSION,
   5.226 +							&dinput, NULL);
   5.227 +	if ( result != DI_OK ) {
   5.228 +		DX5_Unload();
   5.229 +		SetDIerror("DirectInputCreate", result);
   5.230 +		return(-1);
   5.231 +	}
   5.232 +
   5.233 +	result = IDirectInput_EnumDevices(dinput,
   5.234 +			DIDEVTYPE_JOYSTICK, 
   5.235 +			EnumJoysticksCallback,
   5.236 +   			NULL,
   5.237 +			DIEDFL_ATTACHEDONLY );
   5.238 +
   5.239 +	return SYS_NumJoysticks;
   5.240 +}
   5.241 +
   5.242 +/* Function to get the device-dependent name of a joystick */
   5.243 +const char *SDL_SYS_JoystickName(int index)
   5.244 +{
   5.245 +	/***-> test for invalid index ? */
   5.246 +	return(SYS_Joystick[index].tszProductName);
   5.247 +}
   5.248 +
   5.249 +/* Function to open a joystick for use.
   5.250 +   The joystick to open is specified by the index field of the joystick.
   5.251 +   This should fill the nbuttons and naxes fields of the joystick structure.
   5.252 +   It returns 0, or -1 if there is an error.
   5.253 + */
   5.254 +int SDL_SYS_JoystickOpen(SDL_Joystick *joystick)
   5.255 +{
   5.256 +	HRESULT result;
   5.257 +	LPDIRECTINPUTDEVICE device;
   5.258 +
   5.259 +	/* allocate memory for system specific hardware data */
   5.260 +	joystick->hwdata = (struct joystick_hwdata *) malloc(sizeof(*joystick->hwdata));
   5.261 +	if (joystick->hwdata == NULL)
   5.262 +	{
   5.263 +		SDL_OutOfMemory();
   5.264 +		return(-1);
   5.265 +	}
   5.266 +	memset(joystick->hwdata, 0, sizeof(*joystick->hwdata));
   5.267 +	joystick->hwdata->buffered = 1;
   5.268 +	
   5.269 +	result = IDirectInput_CreateDevice(dinput, &SYS_Joystick[joystick->index].guidInstance,
   5.270 +			    &device, NULL);
   5.271 +	if ( result != DI_OK ) {
   5.272 +		SetDIerror("DirectInput::CreateDevice", result);
   5.273 +		return(-1);
   5.274 +	}
   5.275 +
   5.276 +	result = IDirectInputDevice_QueryInterface(device,
   5.277 +		   	    &IID_IDirectInputDevice2, (LPVOID *)&joystick->hwdata->InputDevice);
   5.278 +	IDirectInputDevice_Release(device);
   5.279 +	if ( result != DI_OK ) {
   5.280 +		SetDIerror("DirectInputDevice::QueryInterface", result);
   5.281 +		return(-1);
   5.282 +	}
   5.283 +
   5.284 +	result = IDirectInputDevice2_SetCooperativeLevel(joystick->hwdata->InputDevice, SDL_Window,
   5.285 +			 DISCL_NONEXCLUSIVE | DISCL_BACKGROUND);
   5.286 +	if ( result != DI_OK ) {
   5.287 +		SetDIerror("DirectInputDevice::SetCooperativeLevel", result);
   5.288 +		return(-1);
   5.289 +	}
   5.290 +
   5.291 +	result = IDirectInputDevice2_SetDataFormat(joystick->hwdata->InputDevice, &c_dfDIJoystick);
   5.292 +	if ( result != DI_OK ) {
   5.293 +		SetDIerror("DirectInputDevice::SetDataFormat", result);
   5.294 +		return(-1);
   5.295 +	}
   5.296 +
   5.297 +	IDirectInputDevice2_EnumObjects(joystick->hwdata->InputDevice,
   5.298 +					DIJoystick_EnumDevObjectsProc,
   5.299 +					joystick,
   5.300 +					DIDFT_BUTTON | DIDFT_AXIS | DIDFT_POV);
   5.301 +
   5.302 +	{
   5.303 +		DIPROPDWORD dipdw;
   5.304 +		memset(&dipdw, 0, sizeof(dipdw));
   5.305 +		dipdw.diph.dwSize = sizeof(dipdw);
   5.306 +		dipdw.diph.dwHeaderSize = sizeof(dipdw.diph);
   5.307 +		dipdw.diph.dwObj = 0;
   5.308 +		dipdw.diph.dwHow = DIPH_DEVICE;
   5.309 +		dipdw.dwData = INPUT_QSIZE;
   5.310 +		result = IDirectInputDevice2_SetProperty(joystick->hwdata->InputDevice,
   5.311 +						DIPROP_BUFFERSIZE, &dipdw.diph);
   5.312 +
   5.313 +		if ( result == DI_POLLEDDEVICE )
   5.314 +		{
   5.315 +			/* This device doesn't support buffering, so we're forced
   5.316 +			 * to use less reliable polling. */
   5.317 +			joystick->hwdata->buffered = 0;
   5.318 +		} else if ( result != DI_OK ) {
   5.319 +			SetDIerror("DirectInputDevice::SetProperty", result);
   5.320 +			return(-1);
   5.321 +		}
   5.322 +	}
   5.323 +
   5.324 +	return(0);
   5.325 +}
   5.326 +
   5.327 +static Uint8 TranslatePOV(DWORD value)
   5.328 +{
   5.329 +	const int HAT_VALS[] = {
   5.330 +	    SDL_HAT_UP,
   5.331 +	    SDL_HAT_UP   | SDL_HAT_RIGHT,
   5.332 +	    SDL_HAT_RIGHT,
   5.333 +	    SDL_HAT_DOWN | SDL_HAT_RIGHT,
   5.334 +	    SDL_HAT_DOWN,
   5.335 +	    SDL_HAT_DOWN | SDL_HAT_LEFT,
   5.336 +	    SDL_HAT_LEFT,
   5.337 +	    SDL_HAT_UP   | SDL_HAT_LEFT
   5.338 +	};
   5.339 +
   5.340 +	if(LOWORD(value) == 0xFFFF)
   5.341 +	    return SDL_HAT_CENTERED;
   5.342 +
   5.343 +	/* Round the value up: */
   5.344 +	value += 4500 / 2;
   5.345 +	value %= 36000;
   5.346 +	value /= 4500;
   5.347 +
   5.348 +	if(value >= 8)
   5.349 +	    return SDL_HAT_CENTERED; /* shouldn't happen */
   5.350 +	
   5.351 +	return HAT_VALS[value];
   5.352 +}
   5.353 +
   5.354 +/* SDL_PrivateJoystick* doesn't discard duplicate events, so we need to
   5.355 + * do it. */
   5.356 +static int SDL_PrivateJoystickAxis_Int(SDL_Joystick *joystick, Uint8 axis, Sint16 value)
   5.357 +{
   5.358 +	if(joystick->axes[axis] != value)
   5.359 +		return SDL_PrivateJoystickAxis(joystick, axis, value);
   5.360 +	return 0;
   5.361 +}
   5.362 +
   5.363 +static int SDL_PrivateJoystickHat_Int(SDL_Joystick *joystick, Uint8 hat, Uint8 value)
   5.364 +{
   5.365 +	if(joystick->hats[hat] != value)
   5.366 +		return SDL_PrivateJoystickHat(joystick, hat, value);
   5.367 +	return 0;
   5.368 +}
   5.369 +
   5.370 +static int SDL_PrivateJoystickButton_Int(SDL_Joystick *joystick, Uint8 button, Uint8 state)
   5.371 +{
   5.372 +	if(joystick->buttons[button] != state)
   5.373 +		return SDL_PrivateJoystickButton(joystick, button, state);
   5.374 +	return 0;
   5.375 +}
   5.376 +
   5.377 +/* Function to update the state of a joystick - called as a device poll.
   5.378 + * This function shouldn't update the joystick structure directly,
   5.379 + * but instead should call SDL_PrivateJoystick*() to deliver events
   5.380 + * and update joystick device state.
   5.381 + */
   5.382 +void SDL_SYS_JoystickUpdate_Polled(SDL_Joystick *joystick)
   5.383 +{
   5.384 +	DIJOYSTATE state;
   5.385 +	HRESULT  result;
   5.386 +	int i;
   5.387 +
   5.388 +	result = IDirectInputDevice2_GetDeviceState(joystick->hwdata->InputDevice, sizeof(state), &state);
   5.389 +	if ( result == DIERR_INPUTLOST || result == DIERR_NOTACQUIRED ) {
   5.390 +		IDirectInputDevice2_Acquire(joystick->hwdata->InputDevice);
   5.391 +		result = IDirectInputDevice2_GetDeviceState(joystick->hwdata->InputDevice, sizeof(state), &state);
   5.392 +	}
   5.393 +
   5.394 +	/* Set each known axis, button and POV. */
   5.395 +	for(i = 0; i < joystick->hwdata->NumInputs; ++i)
   5.396 +	{
   5.397 +		const input_t *in = &joystick->hwdata->Inputs[i];
   5.398 +
   5.399 +		switch(in->type)
   5.400 +		{
   5.401 +		case AXIS:
   5.402 +			switch(in->ofs)
   5.403 +			{
   5.404 +			case DIJOFS_X: SDL_PrivateJoystickAxis_Int(joystick, in->num, (Sint16)state.lX); break;
   5.405 +			case DIJOFS_Y: SDL_PrivateJoystickAxis_Int(joystick, in->num, (Sint16)state.lY); break;
   5.406 +			case DIJOFS_Z: SDL_PrivateJoystickAxis_Int(joystick, in->num, (Sint16)state.lZ); break;
   5.407 +			case DIJOFS_RX: SDL_PrivateJoystickAxis_Int(joystick, in->num, (Sint16)state.lRx); break;
   5.408 +			case DIJOFS_RY: SDL_PrivateJoystickAxis_Int(joystick, in->num, (Sint16)state.lRy); break;
   5.409 +			case DIJOFS_RZ: SDL_PrivateJoystickAxis_Int(joystick, in->num, (Sint16)state.lRz); break;
   5.410 +			case DIJOFS_SLIDER(0): SDL_PrivateJoystickAxis_Int(joystick, in->num, (Sint16)state.rglSlider[0]); break;
   5.411 +			case DIJOFS_SLIDER(1): SDL_PrivateJoystickAxis_Int(joystick, in->num, (Sint16)state.rglSlider[0]); break;
   5.412 +			}
   5.413 +
   5.414 +			break;
   5.415 +
   5.416 +		case BUTTON:
   5.417 +			SDL_PrivateJoystickButton_Int(joystick, in->num, (Uint8) (state.rgbButtons[in->ofs - DIJOFS_BUTTON0]?SDL_PRESSED:SDL_RELEASED));
   5.418 +			break;
   5.419 +		case HAT:
   5.420 +		    {
   5.421 +			Uint8 pos = TranslatePOV(state.rgdwPOV[in->ofs - DIJOFS_POV(0)]);
   5.422 +			SDL_PrivateJoystickHat_Int(joystick, in->num, pos);
   5.423 +			break;
   5.424 +		    }
   5.425 +		}
   5.426 +	}
   5.427 +}
   5.428 +
   5.429 +void SDL_SYS_JoystickUpdate_Buffered(SDL_Joystick *joystick)
   5.430 +{
   5.431 +	int i;
   5.432 +	HRESULT  result;
   5.433 +	DWORD numevents;
   5.434 +	DIDEVICEOBJECTDATA evtbuf[INPUT_QSIZE];
   5.435 +
   5.436 +	numevents = INPUT_QSIZE;
   5.437 +	result = IDirectInputDevice2_GetDeviceData(
   5.438 +			joystick->hwdata->InputDevice, sizeof(DIDEVICEOBJECTDATA),
   5.439 +						evtbuf, &numevents, 0);
   5.440 +	if ( result == DIERR_INPUTLOST || result == DIERR_NOTACQUIRED ) {
   5.441 +		IDirectInputDevice2_Acquire(joystick->hwdata->InputDevice);
   5.442 +		result = IDirectInputDevice2_GetDeviceData(
   5.443 +			joystick->hwdata->InputDevice, sizeof(DIDEVICEOBJECTDATA),
   5.444 +						evtbuf, &numevents, 0);
   5.445 +	}
   5.446 +
   5.447 +	/* Handle the events */
   5.448 +	if ( result != DI_OK )
   5.449 +	    return;
   5.450 +
   5.451 +	for(i = 0; i < (int) numevents; ++i)
   5.452 +	{
   5.453 +		int j;
   5.454 +
   5.455 +		for(j = 0; j < joystick->hwdata->NumInputs; ++j)
   5.456 +		{
   5.457 +			const input_t *in = &joystick->hwdata->Inputs[j];
   5.458 +
   5.459 +			if(evtbuf[i].dwOfs != in->ofs)
   5.460 +				continue;
   5.461 +		
   5.462 +			switch(in->type)
   5.463 +			{
   5.464 +			case AXIS:
   5.465 +				SDL_PrivateJoystickAxis(joystick, in->num, (Sint16)evtbuf[i].dwData);
   5.466 +				break;
   5.467 +			case BUTTON:
   5.468 +				SDL_PrivateJoystickButton(joystick, in->num, (Uint8) (evtbuf[i].dwData?SDL_PRESSED:SDL_RELEASED));
   5.469 +				break;
   5.470 +			case HAT:
   5.471 +			    {
   5.472 +				Uint8 pos = TranslatePOV(evtbuf[i].dwData);
   5.473 +				SDL_PrivateJoystickHat(joystick, in->num, pos);
   5.474 +			    }
   5.475 +			}
   5.476 +		}
   5.477 +	}
   5.478 +}
   5.479 +
   5.480 +void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
   5.481 +{
   5.482 +	HRESULT  result;
   5.483 +
   5.484 +	result = IDirectInputDevice2_Poll(joystick->hwdata->InputDevice);
   5.485 +	if (result == DIERR_INPUTLOST || result == DIERR_NOTACQUIRED) {
   5.486 +		IDirectInputDevice2_Acquire(joystick->hwdata->InputDevice);
   5.487 +		IDirectInputDevice2_Poll(joystick->hwdata->InputDevice);
   5.488 +	}
   5.489 +
   5.490 +	if(joystick->hwdata->buffered)
   5.491 +		SDL_SYS_JoystickUpdate_Buffered(joystick);
   5.492 +	else
   5.493 +		SDL_SYS_JoystickUpdate_Polled(joystick);
   5.494 +}
   5.495 +
   5.496 +/* Function to close a joystick after use */
   5.497 +void SDL_SYS_JoystickClose(SDL_Joystick *joystick)
   5.498 +{
   5.499 +	IDirectInputDevice2_Unacquire(joystick->hwdata->InputDevice);
   5.500 +	IDirectInputDevice2_Release(joystick->hwdata->InputDevice);
   5.501 +
   5.502 +	if (joystick->hwdata != NULL) {
   5.503 +		/* free system specific hardware data */
   5.504 +		free(joystick->hwdata);
   5.505 +	}
   5.506 +}
   5.507 +
   5.508 +/* Function to perform any system-specific joystick related cleanup */
   5.509 +void SDL_SYS_JoystickQuit(void)
   5.510 +{
   5.511 +	IDirectInput_Release(dinput);
   5.512 +	dinput = NULL;
   5.513 +	DX5_Unload();
   5.514 +}
   5.515 +
   5.516 +#endif /* SDL_JOYSTICK_DINPUT */
     6.1 --- a/src/video/windx5/SDL_dx5video.c	Sat May 20 04:35:58 2006 +0000
     6.2 +++ b/src/video/windx5/SDL_dx5video.c	Sun May 21 17:27:13 2006 +0000
     6.3 @@ -436,102 +436,98 @@
     6.4  extern int DIB_GetGammaRamp(_THIS, Uint16 *ramp);
     6.5  extern void DIB_QuitGamma(_THIS);
     6.6  
     6.7 +/* Functions for loading the DirectX functions dynamically */
     6.8 +static int DX5_loaded = 0;
     6.9 +static HINSTANCE DDrawDLL = NULL;
    6.10 +static HINSTANCE DInputDLL = NULL;
    6.11 +
    6.12 +void DX5_Unload(void)
    6.13 +{
    6.14 +	if ( --DX5_loaded == 0 ) {
    6.15 +		if ( DDrawDLL != NULL ) {
    6.16 +			FreeLibrary(DDrawDLL);
    6.17 +			DDrawCreate = NULL;
    6.18 +			DDrawDLL = NULL;
    6.19 +		}
    6.20 +		if ( DInputDLL != NULL ) {
    6.21 +			FreeLibrary(DInputDLL);
    6.22 +			DInputCreate = NULL;
    6.23 +			DInputDLL = NULL;
    6.24 +		}
    6.25 +	}
    6.26 +}
    6.27 +int DX5_Load(void)
    6.28 +{
    6.29 +	int status = 0;
    6.30 +
    6.31 +	if ( ++DX5_loaded == 1 ) {
    6.32 +		DDrawDLL = LoadLibrary(TEXT("DDRAW.DLL"));
    6.33 +		if ( DDrawDLL != NULL ) {
    6.34 +			DDrawCreate = (void *)GetProcAddress(DDrawDLL,
    6.35 +						TEXT("DirectDrawCreate"));
    6.36 +		}
    6.37 +		DInputDLL = LoadLibrary(TEXT("DINPUT.DLL"));
    6.38 +		if ( DInputDLL != NULL ) {
    6.39 +			DInputCreate = (void *)GetProcAddress(DInputDLL,
    6.40 +						TEXT("DirectInputCreateA"));
    6.41 +		}
    6.42 +		if ( DDrawDLL && DDrawCreate && DInputDLL && DInputCreate ) {
    6.43 +			status = 0;
    6.44 +		} else {
    6.45 +			DX5_Unload();
    6.46 +			status = -1;
    6.47 +		}
    6.48 +	}
    6.49 +	return status;
    6.50 +}
    6.51 +
    6.52  /* DX5 driver bootstrap functions */
    6.53  
    6.54  static int DX5_Available(void)
    6.55  {
    6.56 -	HINSTANCE DInputDLL;
    6.57 -	HINSTANCE DDrawDLL;
    6.58 -	int dinput_ok;
    6.59 -	int ddraw_ok;
    6.60 +	int ddraw_ok = 0;
    6.61 +	HRESULT (WINAPI *DDrawCreate)(GUID *,LPDIRECTDRAW *,IUnknown *);
    6.62 +	LPDIRECTDRAW DDraw;
    6.63  
    6.64  	/* Version check DINPUT.DLL and DDRAW.DLL (Is DirectX okay?) */
    6.65 -	dinput_ok = 0;
    6.66 -	DInputDLL = LoadLibrary(TEXT("DINPUT.DLL"));
    6.67 -	if ( DInputDLL != NULL ) {
    6.68 -		dinput_ok = 1;
    6.69 -	  	FreeLibrary(DInputDLL);
    6.70 +	if ( DX5_Load() < 0 ) {
    6.71 +		return -1;
    6.72  	}
    6.73 -	ddraw_ok = 0;
    6.74 -	DDrawDLL = LoadLibrary(TEXT("DDRAW.DLL"));
    6.75 -	if ( DDrawDLL != NULL ) {
    6.76 -	  HRESULT (WINAPI *DDrawCreate)(GUID *,LPDIRECTDRAW *,IUnknown *);
    6.77 -	  LPDIRECTDRAW DDraw;
    6.78  
    6.79 -	  /* Try to create a valid DirectDraw object */
    6.80 -	  DDrawCreate = (void *)GetProcAddress(DDrawDLL, TEXT("DirectDrawCreate"));
    6.81 -	  if ( (DDrawCreate != NULL)
    6.82 +	/* Try to create a valid DirectDraw object */
    6.83 +	DDrawCreate = (void *)GetProcAddress(DDrawDLL, TEXT("DirectDrawCreate"));
    6.84 +	if ( (DDrawCreate != NULL)
    6.85  			&& !FAILED(DDrawCreate(NULL, &DDraw, NULL)) ) {
    6.86 -	    if ( !FAILED(IDirectDraw_SetCooperativeLevel(DDraw,
    6.87 +	  if ( !FAILED(IDirectDraw_SetCooperativeLevel(DDraw,
    6.88  							NULL, DDSCL_NORMAL)) ) {
    6.89 -	      DDSURFACEDESC desc;
    6.90 -	      LPDIRECTDRAWSURFACE  DDrawSurf;
    6.91 -	      LPDIRECTDRAWSURFACE3 DDrawSurf3;
    6.92 +	    DDSURFACEDESC desc;
    6.93 +	    LPDIRECTDRAWSURFACE  DDrawSurf;
    6.94 +	    LPDIRECTDRAWSURFACE3 DDrawSurf3;
    6.95  
    6.96 -	      /* Try to create a DirectDrawSurface3 object */
    6.97 -	      SDL_memset(&desc, 0, sizeof(desc));
    6.98 -	      desc.dwSize = sizeof(desc);
    6.99 -	      desc.dwFlags = DDSD_CAPS;
   6.100 -	      desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE|DDSCAPS_VIDEOMEMORY;
   6.101 -	      if ( !FAILED(IDirectDraw_CreateSurface(DDraw, &desc,
   6.102 +	    /* Try to create a DirectDrawSurface3 object */
   6.103 +	    SDL_memset(&desc, 0, sizeof(desc));
   6.104 +	    desc.dwSize = sizeof(desc);
   6.105 +	    desc.dwFlags = DDSD_CAPS;
   6.106 +	    desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE|DDSCAPS_VIDEOMEMORY;
   6.107 +	    if ( !FAILED(IDirectDraw_CreateSurface(DDraw, &desc,
   6.108  							&DDrawSurf, NULL)) ) {
   6.109 -	        if ( !FAILED(IDirectDrawSurface_QueryInterface(DDrawSurf,
   6.110 +	      if ( !FAILED(IDirectDrawSurface_QueryInterface(DDrawSurf,
   6.111  			&IID_IDirectDrawSurface3, (LPVOID *)&DDrawSurf3)) ) {
   6.112 -	          /* Yay! */
   6.113 -		  ddraw_ok = 1;
   6.114 +	        /* Yay! */
   6.115 +		ddraw_ok = 1;
   6.116  
   6.117 -	          /* Clean up.. */
   6.118 -	          IDirectDrawSurface3_Release(DDrawSurf3);
   6.119 -	        }
   6.120 -	        IDirectDrawSurface_Release(DDrawSurf);
   6.121 +	        /* Clean up.. */
   6.122 +	        IDirectDrawSurface3_Release(DDrawSurf3);
   6.123  	      }
   6.124 +	      IDirectDrawSurface_Release(DDrawSurf);
   6.125  	    }
   6.126 -	    IDirectDraw_Release(DDraw);
   6.127  	  }
   6.128 -	  FreeLibrary(DDrawDLL);
   6.129 +	  IDirectDraw_Release(DDraw);
   6.130  	}
   6.131 -	return(dinput_ok && ddraw_ok);
   6.132 -}
   6.133 -
   6.134 -/* Functions for loading the DirectX functions dynamically */
   6.135 -static HINSTANCE DDrawDLL = NULL;
   6.136 -static HINSTANCE DInputDLL = NULL;
   6.137 -
   6.138 -static void DX5_Unload(void)
   6.139 -{
   6.140 -	if ( DDrawDLL != NULL ) {
   6.141 -		FreeLibrary(DDrawDLL);
   6.142 -		DDrawCreate = NULL;
   6.143 -		DDrawDLL = NULL;
   6.144 -	}
   6.145 -	if ( DInputDLL != NULL ) {
   6.146 -		FreeLibrary(DInputDLL);
   6.147 -		DInputCreate = NULL;
   6.148 -		DInputDLL = NULL;
   6.149 -	}
   6.150 -}
   6.151 -static int DX5_Load(void)
   6.152 -{
   6.153 -	int status;
   6.154  
   6.155  	DX5_Unload();
   6.156 -	DDrawDLL = LoadLibrary(TEXT("DDRAW.DLL"));
   6.157 -	if ( DDrawDLL != NULL ) {
   6.158 -		DDrawCreate = (void *)GetProcAddress(DDrawDLL,
   6.159 -					TEXT("DirectDrawCreate"));
   6.160 -	}
   6.161 -	DInputDLL = LoadLibrary(TEXT("DINPUT.DLL"));
   6.162 -	if ( DInputDLL != NULL ) {
   6.163 -		DInputCreate = (void *)GetProcAddress(DInputDLL,
   6.164 -					TEXT("DirectInputCreateA"));
   6.165 -	}
   6.166 -	if ( DDrawDLL && DDrawCreate && DInputDLL && DInputCreate ) {
   6.167 -		status = 0;
   6.168 -	} else {
   6.169 -		DX5_Unload();
   6.170 -		status = -1;
   6.171 -	}
   6.172 -	return status;
   6.173 +
   6.174 +	return ddraw_ok;
   6.175  }
   6.176  
   6.177  static void DX5_DeleteDevice(SDL_VideoDevice *this)
   6.178 @@ -541,6 +537,7 @@
   6.179  		IDirectDraw2_Release(ddraw2);
   6.180  	}
   6.181  	DX5_Unload();
   6.182 +
   6.183  	if ( this ) {
   6.184  		if ( this->hidden ) {
   6.185  			SDL_free(this->hidden);
     7.1 --- a/test/testjoystick.c	Sat May 20 04:35:58 2006 +0000
     7.2 +++ b/test/testjoystick.c	Sun May 21 17:27:13 2006 +0000
     7.3 @@ -17,7 +17,7 @@
     7.4  	int i, done;
     7.5  	SDL_Event event;
     7.6  	int x, y, draw;
     7.7 -	SDL_Rect axis_area[2];
     7.8 +	SDL_Rect axis_area[6][2];
     7.9  
    7.10  	/* Set a video mode to display joystick axis position */
    7.11  	screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 16, 0);
    7.12 @@ -110,36 +110,38 @@
    7.13  			SDL_UpdateRects(screen, 1, &area);
    7.14  		}
    7.15  
    7.16 -		/* Erase previous axes */
    7.17 -		SDL_FillRect(screen, &axis_area[draw], 0x0000);
    7.18 +		for ( i=0; i<SDL_JoystickNumAxes(joystick)/2 && i < SDL_arraysize(axis_area); ++i ) {
    7.19 +			/* Erase previous axes */
    7.20 +			SDL_FillRect(screen, &axis_area[i][draw], 0x0000);
    7.21  
    7.22 -		/* Draw the X/Y axis */
    7.23 -		draw = !draw;
    7.24 -		x = (((int)SDL_JoystickGetAxis(joystick, 0))+32768);
    7.25 -		x *= SCREEN_WIDTH;
    7.26 -		x /= 65535;
    7.27 -		if ( x < 0 ) {
    7.28 -			x = 0;
    7.29 -		} else
    7.30 -		if ( x > (SCREEN_WIDTH-16) ) {
    7.31 -			x = SCREEN_WIDTH-16;
    7.32 +			/* Draw the X/Y axis */
    7.33 +			draw = !draw;
    7.34 +			x = (((int)SDL_JoystickGetAxis(joystick, i*2+0))+32768);
    7.35 +			x *= SCREEN_WIDTH;
    7.36 +			x /= 65535;
    7.37 +			if ( x < 0 ) {
    7.38 +				x = 0;
    7.39 +			} else
    7.40 +			if ( x > (SCREEN_WIDTH-16) ) {
    7.41 +				x = SCREEN_WIDTH-16;
    7.42 +			}
    7.43 +			y = (((int)SDL_JoystickGetAxis(joystick, i*2+1))+32768);
    7.44 +			y *= SCREEN_HEIGHT;
    7.45 +			y /= 65535;
    7.46 +			if ( y < 0 ) {
    7.47 +				y = 0;
    7.48 +			} else
    7.49 +			if ( y > (SCREEN_HEIGHT-16) ) {
    7.50 +				y = SCREEN_HEIGHT-16;
    7.51 +			}
    7.52 +			axis_area[i][draw].x = (Sint16)x;
    7.53 +			axis_area[i][draw].y = (Sint16)y;
    7.54 +			axis_area[i][draw].w = 16;
    7.55 +			axis_area[i][draw].h = 16;
    7.56 +			SDL_FillRect(screen, &axis_area[i][draw], 0xFFFF);
    7.57 +
    7.58 +			SDL_UpdateRects(screen, 2, axis_area[i]);
    7.59  		}
    7.60 -		y = (((int)SDL_JoystickGetAxis(joystick, 1))+32768);
    7.61 -		y *= SCREEN_HEIGHT;
    7.62 -		y /= 65535;
    7.63 -		if ( y < 0 ) {
    7.64 -			y = 0;
    7.65 -		} else
    7.66 -		if ( y > (SCREEN_HEIGHT-16) ) {
    7.67 -			y = SCREEN_HEIGHT-16;
    7.68 -		}
    7.69 -		axis_area[draw].x = (Sint16)x;
    7.70 -		axis_area[draw].y = (Sint16)y;
    7.71 -		axis_area[draw].w = 16;
    7.72 -		axis_area[draw].h = 16;
    7.73 -		SDL_FillRect(screen, &axis_area[draw], 0xFFFF);
    7.74 -
    7.75 -		SDL_UpdateRects(screen, 2, axis_area);
    7.76  	}
    7.77  }
    7.78