src/joystick/win32/SDL_dxjoystick.c
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1661 281d3f4870e5
child 1668 4da1ee79c9af
     1.1 --- a/src/joystick/win32/SDL_dxjoystick.c	Sun May 21 17:27:13 2006 +0000
     1.2 +++ b/src/joystick/win32/SDL_dxjoystick.c	Sun May 28 13:04:16 2006 +0000
     1.3 @@ -45,162 +45,171 @@
     1.4  #define DIRECTINPUT_VERSION 0x0500
     1.5  #include <dinput.h>
     1.6  
     1.7 -#define INPUT_QSIZE	32		/* Buffer up to 32 input messages */
     1.8 +#define INPUT_QSIZE	32      /* Buffer up to 32 input messages */
     1.9  
    1.10  extern HINSTANCE SDL_Instance;
    1.11 -extern int DX5_Load();
    1.12 -extern void DX5_Unload();
    1.13 -extern HRESULT (WINAPI *DInputCreate)(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUT *ppDI, LPUNKNOWN punkOuter);
    1.14 +extern int DX5_Load ();
    1.15 +extern void DX5_Unload ();
    1.16 +extern HRESULT (WINAPI * DInputCreate) (HINSTANCE hinst, DWORD dwVersion,
    1.17 +                                        LPDIRECTINPUT * ppDI,
    1.18 +                                        LPUNKNOWN punkOuter);
    1.19  
    1.20  static LPDIRECTINPUT dinput = NULL;
    1.21  
    1.22  #define MAX_JOYSTICKS	8
    1.23 -#define MAX_INPUTS	256	/* each joystick can have up to 256 inputs */
    1.24 +#define MAX_INPUTS	256     /* each joystick can have up to 256 inputs */
    1.25  #define AXIS_MIN	-32768  /* minimum value for axis coordinate */
    1.26  #define AXIS_MAX	32767   /* maximum value for axis coordinate */
    1.27 -#define JOY_AXIS_THRESHOLD	(((AXIS_MAX)-(AXIS_MIN))/100) /* 1% motion */
    1.28 +#define JOY_AXIS_THRESHOLD	(((AXIS_MAX)-(AXIS_MIN))/100)   /* 1% motion */
    1.29  
    1.30 -typedef enum Type { BUTTON, AXIS, HAT } Type;
    1.31 +typedef enum Type
    1.32 +{ BUTTON, AXIS, HAT } Type;
    1.33  
    1.34  /* array to hold joystick ID values */
    1.35  static DIDEVICEINSTANCE SYS_Joystick[MAX_JOYSTICKS];
    1.36 -static int	SYS_NumJoysticks;
    1.37 +static int SYS_NumJoysticks;
    1.38  
    1.39  extern HWND SDL_Window;
    1.40  
    1.41  typedef struct input_t
    1.42  {
    1.43 -	/* DirectInput offset for this input type: */
    1.44 -	DWORD ofs;
    1.45 +    /* DirectInput offset for this input type: */
    1.46 +    DWORD ofs;
    1.47  
    1.48 -	/* Button, axis or hat: */
    1.49 -	Type type;
    1.50 +    /* Button, axis or hat: */
    1.51 +    Type type;
    1.52  
    1.53 -	/* SDL input offset: */
    1.54 -	Uint8 num;
    1.55 +    /* SDL input offset: */
    1.56 +    Uint8 num;
    1.57  } input_t;
    1.58  
    1.59  /* The private structure used to keep track of a joystick */
    1.60  struct joystick_hwdata
    1.61  {
    1.62 -	LPDIRECTINPUTDEVICE2 InputDevice;
    1.63 -	int buffered;
    1.64 +    LPDIRECTINPUTDEVICE2 InputDevice;
    1.65 +    int buffered;
    1.66  
    1.67 -	input_t Inputs[MAX_INPUTS];
    1.68 -	int NumInputs;
    1.69 +    input_t Inputs[MAX_INPUTS];
    1.70 +    int NumInputs;
    1.71  };
    1.72  
    1.73  /* Convert a DirectInput return code to a text message */
    1.74 -static void SetDIerror(char *function, int code)
    1.75 +static void
    1.76 +SetDIerror (char *function, int code)
    1.77  {
    1.78 -	static char *error;
    1.79 -	static char  errbuf[1024];
    1.80 +    static char *error;
    1.81 +    static char errbuf[1024];
    1.82  
    1.83 -	errbuf[0] = 0;
    1.84 -	switch (code) {
    1.85 -                case DIERR_GENERIC:
    1.86 -                        error = "Undefined error!";
    1.87 -                        break;
    1.88 -		case DIERR_OLDDIRECTINPUTVERSION:
    1.89 -			error = "Your version of DirectInput needs upgrading";
    1.90 -			break;
    1.91 -		case DIERR_INVALIDPARAM:
    1.92 -                        error = "Invalid parameters";
    1.93 -                        break;
    1.94 -                case DIERR_OUTOFMEMORY:
    1.95 -                        error = "Out of memory";
    1.96 -                        break;
    1.97 -		case DIERR_DEVICENOTREG:
    1.98 -			error = "Device not registered";
    1.99 -			break;
   1.100 -		case DIERR_NOINTERFACE:
   1.101 -			error = "Interface not supported";
   1.102 -			break;
   1.103 -		case DIERR_NOTINITIALIZED:
   1.104 -			error = "Device not initialized";
   1.105 -			break;
   1.106 -		default:
   1.107 -			sprintf(errbuf, "%s: Unknown DirectInput error: 0x%x",
   1.108 -								function, code);
   1.109 -			break;
   1.110 -	}
   1.111 -	if ( ! errbuf[0] ) {
   1.112 -		sprintf(errbuf, "%s: %s", function, error);
   1.113 -	}
   1.114 -	SDL_SetError("%s", errbuf);
   1.115 -	return;
   1.116 +    errbuf[0] = 0;
   1.117 +    switch (code) {
   1.118 +    case DIERR_GENERIC:
   1.119 +        error = "Undefined error!";
   1.120 +        break;
   1.121 +    case DIERR_OLDDIRECTINPUTVERSION:
   1.122 +        error = "Your version of DirectInput needs upgrading";
   1.123 +        break;
   1.124 +    case DIERR_INVALIDPARAM:
   1.125 +        error = "Invalid parameters";
   1.126 +        break;
   1.127 +    case DIERR_OUTOFMEMORY:
   1.128 +        error = "Out of memory";
   1.129 +        break;
   1.130 +    case DIERR_DEVICENOTREG:
   1.131 +        error = "Device not registered";
   1.132 +        break;
   1.133 +    case DIERR_NOINTERFACE:
   1.134 +        error = "Interface not supported";
   1.135 +        break;
   1.136 +    case DIERR_NOTINITIALIZED:
   1.137 +        error = "Device not initialized";
   1.138 +        break;
   1.139 +    default:
   1.140 +        sprintf (errbuf, "%s: Unknown DirectInput error: 0x%x",
   1.141 +                 function, code);
   1.142 +        break;
   1.143 +    }
   1.144 +    if (!errbuf[0]) {
   1.145 +        sprintf (errbuf, "%s: %s", function, error);
   1.146 +    }
   1.147 +    SDL_SetError ("%s", errbuf);
   1.148 +    return;
   1.149  }
   1.150  
   1.151  
   1.152 -BOOL CALLBACK EnumJoysticksCallback( const DIDEVICEINSTANCE* pdidInstance,
   1.153 -				     VOID* pContext )
   1.154 +BOOL CALLBACK
   1.155 +EnumJoysticksCallback (const DIDEVICEINSTANCE * pdidInstance, VOID * pContext)
   1.156  {
   1.157 -	memcpy(&SYS_Joystick[SYS_NumJoysticks], pdidInstance, sizeof(DIDEVICEINSTANCE));
   1.158 -	SYS_NumJoysticks++;
   1.159 +    memcpy (&SYS_Joystick[SYS_NumJoysticks], pdidInstance,
   1.160 +            sizeof (DIDEVICEINSTANCE));
   1.161 +    SYS_NumJoysticks++;
   1.162  
   1.163 -	if( SYS_NumJoysticks >= MAX_JOYSTICKS )
   1.164 -	        return DIENUM_STOP;
   1.165 +    if (SYS_NumJoysticks >= MAX_JOYSTICKS)
   1.166 +        return DIENUM_STOP;
   1.167  
   1.168 -	return DIENUM_CONTINUE;
   1.169 +    return DIENUM_CONTINUE;
   1.170  }
   1.171  
   1.172 -static BOOL CALLBACK DIJoystick_EnumDevObjectsProc(LPCDIDEVICEOBJECTINSTANCE dev,
   1.173 -						    LPVOID pvRef)
   1.174 +static BOOL CALLBACK
   1.175 +DIJoystick_EnumDevObjectsProc (LPCDIDEVICEOBJECTINSTANCE dev, LPVOID pvRef)
   1.176  {
   1.177 -	SDL_Joystick *joystick = (SDL_Joystick*)pvRef;
   1.178 -	HRESULT result;
   1.179 -	input_t *in = &joystick->hwdata->Inputs[joystick->hwdata->NumInputs];
   1.180 -	const int SupportedMask = DIDFT_BUTTON | DIDFT_POV | DIDFT_AXIS;
   1.181 -	if(!(dev->dwType & SupportedMask))
   1.182 -	    return DIENUM_CONTINUE; /* unsupported */
   1.183 +    SDL_Joystick *joystick = (SDL_Joystick *) pvRef;
   1.184 +    HRESULT result;
   1.185 +    input_t *in = &joystick->hwdata->Inputs[joystick->hwdata->NumInputs];
   1.186 +    const int SupportedMask = DIDFT_BUTTON | DIDFT_POV | DIDFT_AXIS;
   1.187 +    if (!(dev->dwType & SupportedMask))
   1.188 +        return DIENUM_CONTINUE; /* unsupported */
   1.189  
   1.190 -	in->ofs = dev->dwOfs;
   1.191 +    in->ofs = dev->dwOfs;
   1.192  
   1.193 -	if(dev->dwType & DIDFT_BUTTON) {
   1.194 -		in->type = BUTTON;
   1.195 -		in->num = joystick->nbuttons;
   1.196 -		joystick->nbuttons++;
   1.197 -	} else if(dev->dwType & DIDFT_POV) {
   1.198 -		in->type = HAT;
   1.199 -		in->num = joystick->nhats;
   1.200 -		joystick->nhats++;
   1.201 -	} else { /* dev->dwType & DIDFT_AXIS */
   1.202 -		DIPROPRANGE diprg;
   1.203 -		DIPROPDWORD dilong;
   1.204 -		
   1.205 -		in->type = AXIS;
   1.206 -		in->num = joystick->naxes;
   1.207 -		
   1.208 -		diprg.diph.dwSize		= sizeof(diprg);
   1.209 -		diprg.diph.dwHeaderSize	= sizeof(diprg.diph);
   1.210 -		diprg.diph.dwObj		= dev->dwOfs;
   1.211 -		diprg.diph.dwHow		= DIPH_BYOFFSET;
   1.212 -		diprg.lMin			= AXIS_MIN;
   1.213 -		diprg.lMax			= AXIS_MAX;
   1.214 +    if (dev->dwType & DIDFT_BUTTON) {
   1.215 +        in->type = BUTTON;
   1.216 +        in->num = joystick->nbuttons;
   1.217 +        joystick->nbuttons++;
   1.218 +    } else if (dev->dwType & DIDFT_POV) {
   1.219 +        in->type = HAT;
   1.220 +        in->num = joystick->nhats;
   1.221 +        joystick->nhats++;
   1.222 +    } else {                    /* dev->dwType & DIDFT_AXIS */
   1.223 +        DIPROPRANGE diprg;
   1.224 +        DIPROPDWORD dilong;
   1.225  
   1.226 -		result = IDirectInputDevice2_SetProperty(joystick->hwdata->InputDevice, DIPROP_RANGE, &diprg.diph);
   1.227 -		if ( result != DI_OK )
   1.228 -			return DIENUM_CONTINUE; /* don't use this axis */
   1.229 -	
   1.230 -		/* Set dead zone to 0. */
   1.231 -		dilong.diph.dwSize		= sizeof(dilong);
   1.232 -		dilong.diph.dwHeaderSize	= sizeof(dilong.diph);
   1.233 -		dilong.diph.dwObj		= dev->dwOfs;
   1.234 -		dilong.diph.dwHow		= DIPH_BYOFFSET;
   1.235 -		dilong.dwData = 0;
   1.236 -		result = IDirectInputDevice2_SetProperty(joystick->hwdata->InputDevice, DIPROP_DEADZONE, &dilong.diph);
   1.237 -		if ( result != DI_OK )
   1.238 -			return DIENUM_CONTINUE; /* don't use this axis */
   1.239 +        in->type = AXIS;
   1.240 +        in->num = joystick->naxes;
   1.241  
   1.242 -		joystick->naxes++;
   1.243 -	}
   1.244 +        diprg.diph.dwSize = sizeof (diprg);
   1.245 +        diprg.diph.dwHeaderSize = sizeof (diprg.diph);
   1.246 +        diprg.diph.dwObj = dev->dwOfs;
   1.247 +        diprg.diph.dwHow = DIPH_BYOFFSET;
   1.248 +        diprg.lMin = AXIS_MIN;
   1.249 +        diprg.lMax = AXIS_MAX;
   1.250  
   1.251 -	joystick->hwdata->NumInputs++;
   1.252 +        result =
   1.253 +            IDirectInputDevice2_SetProperty (joystick->hwdata->InputDevice,
   1.254 +                                             DIPROP_RANGE, &diprg.diph);
   1.255 +        if (result != DI_OK)
   1.256 +            return DIENUM_CONTINUE;     /* don't use this axis */
   1.257  
   1.258 -	if(joystick->hwdata->NumInputs == MAX_INPUTS)
   1.259 -		return DIENUM_STOP; /* too many */
   1.260 +        /* Set dead zone to 0. */
   1.261 +        dilong.diph.dwSize = sizeof (dilong);
   1.262 +        dilong.diph.dwHeaderSize = sizeof (dilong.diph);
   1.263 +        dilong.diph.dwObj = dev->dwOfs;
   1.264 +        dilong.diph.dwHow = DIPH_BYOFFSET;
   1.265 +        dilong.dwData = 0;
   1.266 +        result =
   1.267 +            IDirectInputDevice2_SetProperty (joystick->hwdata->InputDevice,
   1.268 +                                             DIPROP_DEADZONE, &dilong.diph);
   1.269 +        if (result != DI_OK)
   1.270 +            return DIENUM_CONTINUE;     /* don't use this axis */
   1.271  
   1.272 -	return DIENUM_CONTINUE;
   1.273 +        joystick->naxes++;
   1.274 +    }
   1.275 +
   1.276 +    joystick->hwdata->NumInputs++;
   1.277 +
   1.278 +    if (joystick->hwdata->NumInputs == MAX_INPUTS)
   1.279 +        return DIENUM_STOP;     /* too many */
   1.280 +
   1.281 +    return DIENUM_CONTINUE;
   1.282  }
   1.283  
   1.284  /* Function to scan the system for joysticks.
   1.285 @@ -208,39 +217,39 @@
   1.286   * joysticks.  Joystick 0 should be the system default joystick.
   1.287   * It should return 0, or -1 on an unrecoverable fatal error.
   1.288   */
   1.289 -int SDL_SYS_JoystickInit(void)
   1.290 +int
   1.291 +SDL_SYS_JoystickInit (void)
   1.292  {
   1.293 -	HRESULT result;
   1.294 +    HRESULT result;
   1.295  
   1.296 -	SYS_NumJoysticks = 0;
   1.297 +    SYS_NumJoysticks = 0;
   1.298  
   1.299 -	/* Create the DirectInput object */
   1.300 -	if ( DX5_Load() < 0 ) {
   1.301 -		SDL_SetError("Couldn't load DirectInput");
   1.302 -		return(-1);
   1.303 -	}
   1.304 -	result = DInputCreate(SDL_Instance, DIRECTINPUT_VERSION,
   1.305 -							&dinput, NULL);
   1.306 -	if ( result != DI_OK ) {
   1.307 -		DX5_Unload();
   1.308 -		SetDIerror("DirectInputCreate", result);
   1.309 -		return(-1);
   1.310 -	}
   1.311 +    /* Create the DirectInput object */
   1.312 +    if (DX5_Load () < 0) {
   1.313 +        SDL_SetError ("Couldn't load DirectInput");
   1.314 +        return (-1);
   1.315 +    }
   1.316 +    result = DInputCreate (SDL_Instance, DIRECTINPUT_VERSION, &dinput, NULL);
   1.317 +    if (result != DI_OK) {
   1.318 +        DX5_Unload ();
   1.319 +        SetDIerror ("DirectInputCreate", result);
   1.320 +        return (-1);
   1.321 +    }
   1.322  
   1.323 -	result = IDirectInput_EnumDevices(dinput,
   1.324 -			DIDEVTYPE_JOYSTICK, 
   1.325 -			EnumJoysticksCallback,
   1.326 -   			NULL,
   1.327 -			DIEDFL_ATTACHEDONLY );
   1.328 +    result = IDirectInput_EnumDevices (dinput,
   1.329 +                                       DIDEVTYPE_JOYSTICK,
   1.330 +                                       EnumJoysticksCallback,
   1.331 +                                       NULL, DIEDFL_ATTACHEDONLY);
   1.332  
   1.333 -	return SYS_NumJoysticks;
   1.334 +    return SYS_NumJoysticks;
   1.335  }
   1.336  
   1.337  /* Function to get the device-dependent name of a joystick */
   1.338 -const char *SDL_SYS_JoystickName(int index)
   1.339 +const char *
   1.340 +SDL_SYS_JoystickName (int index)
   1.341  {
   1.342 -	/***-> test for invalid index ? */
   1.343 -	return(SYS_Joystick[index].tszProductName);
   1.344 +        /***-> test for invalid index ? */
   1.345 +    return (SYS_Joystick[index].tszProductName);
   1.346  }
   1.347  
   1.348  /* Function to open a joystick for use.
   1.349 @@ -248,127 +257,143 @@
   1.350     This should fill the nbuttons and naxes fields of the joystick structure.
   1.351     It returns 0, or -1 if there is an error.
   1.352   */
   1.353 -int SDL_SYS_JoystickOpen(SDL_Joystick *joystick)
   1.354 +int
   1.355 +SDL_SYS_JoystickOpen (SDL_Joystick * joystick)
   1.356  {
   1.357 -	HRESULT result;
   1.358 -	LPDIRECTINPUTDEVICE device;
   1.359 +    HRESULT result;
   1.360 +    LPDIRECTINPUTDEVICE device;
   1.361  
   1.362 -	/* allocate memory for system specific hardware data */
   1.363 -	joystick->hwdata = (struct joystick_hwdata *) malloc(sizeof(*joystick->hwdata));
   1.364 -	if (joystick->hwdata == NULL)
   1.365 -	{
   1.366 -		SDL_OutOfMemory();
   1.367 -		return(-1);
   1.368 -	}
   1.369 -	memset(joystick->hwdata, 0, sizeof(*joystick->hwdata));
   1.370 -	joystick->hwdata->buffered = 1;
   1.371 -	
   1.372 -	result = IDirectInput_CreateDevice(dinput, &SYS_Joystick[joystick->index].guidInstance,
   1.373 -			    &device, NULL);
   1.374 -	if ( result != DI_OK ) {
   1.375 -		SetDIerror("DirectInput::CreateDevice", result);
   1.376 -		return(-1);
   1.377 -	}
   1.378 +    /* allocate memory for system specific hardware data */
   1.379 +    joystick->hwdata =
   1.380 +        (struct joystick_hwdata *) malloc (sizeof (*joystick->hwdata));
   1.381 +    if (joystick->hwdata == NULL) {
   1.382 +        SDL_OutOfMemory ();
   1.383 +        return (-1);
   1.384 +    }
   1.385 +    memset (joystick->hwdata, 0, sizeof (*joystick->hwdata));
   1.386 +    joystick->hwdata->buffered = 1;
   1.387  
   1.388 -	result = IDirectInputDevice_QueryInterface(device,
   1.389 -		   	    &IID_IDirectInputDevice2, (LPVOID *)&joystick->hwdata->InputDevice);
   1.390 -	IDirectInputDevice_Release(device);
   1.391 -	if ( result != DI_OK ) {
   1.392 -		SetDIerror("DirectInputDevice::QueryInterface", result);
   1.393 -		return(-1);
   1.394 -	}
   1.395 +    result =
   1.396 +        IDirectInput_CreateDevice (dinput,
   1.397 +                                   &SYS_Joystick[joystick->index].
   1.398 +                                   guidInstance, &device, NULL);
   1.399 +    if (result != DI_OK) {
   1.400 +        SetDIerror ("DirectInput::CreateDevice", result);
   1.401 +        return (-1);
   1.402 +    }
   1.403  
   1.404 -	result = IDirectInputDevice2_SetCooperativeLevel(joystick->hwdata->InputDevice, SDL_Window,
   1.405 -			 DISCL_NONEXCLUSIVE | DISCL_BACKGROUND);
   1.406 -	if ( result != DI_OK ) {
   1.407 -		SetDIerror("DirectInputDevice::SetCooperativeLevel", result);
   1.408 -		return(-1);
   1.409 -	}
   1.410 +    result = IDirectInputDevice_QueryInterface (device,
   1.411 +                                                &IID_IDirectInputDevice2,
   1.412 +                                                (LPVOID *) & joystick->
   1.413 +                                                hwdata->InputDevice);
   1.414 +    IDirectInputDevice_Release (device);
   1.415 +    if (result != DI_OK) {
   1.416 +        SetDIerror ("DirectInputDevice::QueryInterface", result);
   1.417 +        return (-1);
   1.418 +    }
   1.419  
   1.420 -	result = IDirectInputDevice2_SetDataFormat(joystick->hwdata->InputDevice, &c_dfDIJoystick);
   1.421 -	if ( result != DI_OK ) {
   1.422 -		SetDIerror("DirectInputDevice::SetDataFormat", result);
   1.423 -		return(-1);
   1.424 -	}
   1.425 +    result =
   1.426 +        IDirectInputDevice2_SetCooperativeLevel (joystick->hwdata->
   1.427 +                                                 InputDevice, SDL_Window,
   1.428 +                                                 DISCL_NONEXCLUSIVE |
   1.429 +                                                 DISCL_BACKGROUND);
   1.430 +    if (result != DI_OK) {
   1.431 +        SetDIerror ("DirectInputDevice::SetCooperativeLevel", result);
   1.432 +        return (-1);
   1.433 +    }
   1.434  
   1.435 -	IDirectInputDevice2_EnumObjects(joystick->hwdata->InputDevice,
   1.436 -					DIJoystick_EnumDevObjectsProc,
   1.437 -					joystick,
   1.438 -					DIDFT_BUTTON | DIDFT_AXIS | DIDFT_POV);
   1.439 +    result =
   1.440 +        IDirectInputDevice2_SetDataFormat (joystick->hwdata->InputDevice,
   1.441 +                                           &c_dfDIJoystick);
   1.442 +    if (result != DI_OK) {
   1.443 +        SetDIerror ("DirectInputDevice::SetDataFormat", result);
   1.444 +        return (-1);
   1.445 +    }
   1.446  
   1.447 -	{
   1.448 -		DIPROPDWORD dipdw;
   1.449 -		memset(&dipdw, 0, sizeof(dipdw));
   1.450 -		dipdw.diph.dwSize = sizeof(dipdw);
   1.451 -		dipdw.diph.dwHeaderSize = sizeof(dipdw.diph);
   1.452 -		dipdw.diph.dwObj = 0;
   1.453 -		dipdw.diph.dwHow = DIPH_DEVICE;
   1.454 -		dipdw.dwData = INPUT_QSIZE;
   1.455 -		result = IDirectInputDevice2_SetProperty(joystick->hwdata->InputDevice,
   1.456 -						DIPROP_BUFFERSIZE, &dipdw.diph);
   1.457 +    IDirectInputDevice2_EnumObjects (joystick->hwdata->InputDevice,
   1.458 +                                     DIJoystick_EnumDevObjectsProc,
   1.459 +                                     joystick,
   1.460 +                                     DIDFT_BUTTON | DIDFT_AXIS | DIDFT_POV);
   1.461  
   1.462 -		if ( result == DI_POLLEDDEVICE )
   1.463 -		{
   1.464 -			/* This device doesn't support buffering, so we're forced
   1.465 -			 * to use less reliable polling. */
   1.466 -			joystick->hwdata->buffered = 0;
   1.467 -		} else if ( result != DI_OK ) {
   1.468 -			SetDIerror("DirectInputDevice::SetProperty", result);
   1.469 -			return(-1);
   1.470 -		}
   1.471 -	}
   1.472 +    {
   1.473 +        DIPROPDWORD dipdw;
   1.474 +        memset (&dipdw, 0, sizeof (dipdw));
   1.475 +        dipdw.diph.dwSize = sizeof (dipdw);
   1.476 +        dipdw.diph.dwHeaderSize = sizeof (dipdw.diph);
   1.477 +        dipdw.diph.dwObj = 0;
   1.478 +        dipdw.diph.dwHow = DIPH_DEVICE;
   1.479 +        dipdw.dwData = INPUT_QSIZE;
   1.480 +        result =
   1.481 +            IDirectInputDevice2_SetProperty (joystick->hwdata->InputDevice,
   1.482 +                                             DIPROP_BUFFERSIZE, &dipdw.diph);
   1.483  
   1.484 -	return(0);
   1.485 +        if (result == DI_POLLEDDEVICE) {
   1.486 +            /* This device doesn't support buffering, so we're forced
   1.487 +             * to use less reliable polling. */
   1.488 +            joystick->hwdata->buffered = 0;
   1.489 +        } else if (result != DI_OK) {
   1.490 +            SetDIerror ("DirectInputDevice::SetProperty", result);
   1.491 +            return (-1);
   1.492 +        }
   1.493 +    }
   1.494 +
   1.495 +    return (0);
   1.496  }
   1.497  
   1.498 -static Uint8 TranslatePOV(DWORD value)
   1.499 +static Uint8
   1.500 +TranslatePOV (DWORD value)
   1.501  {
   1.502 -	const int HAT_VALS[] = {
   1.503 -	    SDL_HAT_UP,
   1.504 -	    SDL_HAT_UP   | SDL_HAT_RIGHT,
   1.505 -	    SDL_HAT_RIGHT,
   1.506 -	    SDL_HAT_DOWN | SDL_HAT_RIGHT,
   1.507 -	    SDL_HAT_DOWN,
   1.508 -	    SDL_HAT_DOWN | SDL_HAT_LEFT,
   1.509 -	    SDL_HAT_LEFT,
   1.510 -	    SDL_HAT_UP   | SDL_HAT_LEFT
   1.511 -	};
   1.512 +    const int HAT_VALS[] = {
   1.513 +        SDL_HAT_UP,
   1.514 +        SDL_HAT_UP | SDL_HAT_RIGHT,
   1.515 +        SDL_HAT_RIGHT,
   1.516 +        SDL_HAT_DOWN | SDL_HAT_RIGHT,
   1.517 +        SDL_HAT_DOWN,
   1.518 +        SDL_HAT_DOWN | SDL_HAT_LEFT,
   1.519 +        SDL_HAT_LEFT,
   1.520 +        SDL_HAT_UP | SDL_HAT_LEFT
   1.521 +    };
   1.522  
   1.523 -	if(LOWORD(value) == 0xFFFF)
   1.524 -	    return SDL_HAT_CENTERED;
   1.525 +    if (LOWORD (value) == 0xFFFF)
   1.526 +        return SDL_HAT_CENTERED;
   1.527  
   1.528 -	/* Round the value up: */
   1.529 -	value += 4500 / 2;
   1.530 -	value %= 36000;
   1.531 -	value /= 4500;
   1.532 +    /* Round the value up: */
   1.533 +    value += 4500 / 2;
   1.534 +    value %= 36000;
   1.535 +    value /= 4500;
   1.536  
   1.537 -	if(value >= 8)
   1.538 -	    return SDL_HAT_CENTERED; /* shouldn't happen */
   1.539 -	
   1.540 -	return HAT_VALS[value];
   1.541 +    if (value >= 8)
   1.542 +        return SDL_HAT_CENTERED;        /* shouldn't happen */
   1.543 +
   1.544 +    return HAT_VALS[value];
   1.545  }
   1.546  
   1.547  /* SDL_PrivateJoystick* doesn't discard duplicate events, so we need to
   1.548   * do it. */
   1.549 -static int SDL_PrivateJoystickAxis_Int(SDL_Joystick *joystick, Uint8 axis, Sint16 value)
   1.550 +static int
   1.551 +SDL_PrivateJoystickAxis_Int (SDL_Joystick * joystick, Uint8 axis,
   1.552 +                             Sint16 value)
   1.553  {
   1.554 -	if(joystick->axes[axis] != value)
   1.555 -		return SDL_PrivateJoystickAxis(joystick, axis, value);
   1.556 -	return 0;
   1.557 +    if (joystick->axes[axis] != value)
   1.558 +        return SDL_PrivateJoystickAxis (joystick, axis, value);
   1.559 +    return 0;
   1.560  }
   1.561  
   1.562 -static int SDL_PrivateJoystickHat_Int(SDL_Joystick *joystick, Uint8 hat, Uint8 value)
   1.563 +static int
   1.564 +SDL_PrivateJoystickHat_Int (SDL_Joystick * joystick, Uint8 hat, Uint8 value)
   1.565  {
   1.566 -	if(joystick->hats[hat] != value)
   1.567 -		return SDL_PrivateJoystickHat(joystick, hat, value);
   1.568 -	return 0;
   1.569 +    if (joystick->hats[hat] != value)
   1.570 +        return SDL_PrivateJoystickHat (joystick, hat, value);
   1.571 +    return 0;
   1.572  }
   1.573  
   1.574 -static int SDL_PrivateJoystickButton_Int(SDL_Joystick *joystick, Uint8 button, Uint8 state)
   1.575 +static int
   1.576 +SDL_PrivateJoystickButton_Int (SDL_Joystick * joystick, Uint8 button,
   1.577 +                               Uint8 state)
   1.578  {
   1.579 -	if(joystick->buttons[button] != state)
   1.580 -		return SDL_PrivateJoystickButton(joystick, button, state);
   1.581 -	return 0;
   1.582 +    if (joystick->buttons[button] != state)
   1.583 +        return SDL_PrivateJoystickButton (joystick, button, state);
   1.584 +    return 0;
   1.585  }
   1.586  
   1.587  /* Function to update the state of a joystick - called as a device poll.
   1.588 @@ -376,138 +401,180 @@
   1.589   * but instead should call SDL_PrivateJoystick*() to deliver events
   1.590   * and update joystick device state.
   1.591   */
   1.592 -void SDL_SYS_JoystickUpdate_Polled(SDL_Joystick *joystick)
   1.593 +void
   1.594 +SDL_SYS_JoystickUpdate_Polled (SDL_Joystick * joystick)
   1.595  {
   1.596 -	DIJOYSTATE state;
   1.597 -	HRESULT  result;
   1.598 -	int i;
   1.599 +    DIJOYSTATE state;
   1.600 +    HRESULT result;
   1.601 +    int i;
   1.602  
   1.603 -	result = IDirectInputDevice2_GetDeviceState(joystick->hwdata->InputDevice, sizeof(state), &state);
   1.604 -	if ( result == DIERR_INPUTLOST || result == DIERR_NOTACQUIRED ) {
   1.605 -		IDirectInputDevice2_Acquire(joystick->hwdata->InputDevice);
   1.606 -		result = IDirectInputDevice2_GetDeviceState(joystick->hwdata->InputDevice, sizeof(state), &state);
   1.607 -	}
   1.608 +    result =
   1.609 +        IDirectInputDevice2_GetDeviceState (joystick->hwdata->InputDevice,
   1.610 +                                            sizeof (state), &state);
   1.611 +    if (result == DIERR_INPUTLOST || result == DIERR_NOTACQUIRED) {
   1.612 +        IDirectInputDevice2_Acquire (joystick->hwdata->InputDevice);
   1.613 +        result =
   1.614 +            IDirectInputDevice2_GetDeviceState (joystick->hwdata->
   1.615 +                                                InputDevice, sizeof (state),
   1.616 +                                                &state);
   1.617 +    }
   1.618  
   1.619 -	/* Set each known axis, button and POV. */
   1.620 -	for(i = 0; i < joystick->hwdata->NumInputs; ++i)
   1.621 -	{
   1.622 -		const input_t *in = &joystick->hwdata->Inputs[i];
   1.623 +    /* Set each known axis, button and POV. */
   1.624 +    for (i = 0; i < joystick->hwdata->NumInputs; ++i) {
   1.625 +        const input_t *in = &joystick->hwdata->Inputs[i];
   1.626  
   1.627 -		switch(in->type)
   1.628 -		{
   1.629 -		case AXIS:
   1.630 -			switch(in->ofs)
   1.631 -			{
   1.632 -			case DIJOFS_X: SDL_PrivateJoystickAxis_Int(joystick, in->num, (Sint16)state.lX); break;
   1.633 -			case DIJOFS_Y: SDL_PrivateJoystickAxis_Int(joystick, in->num, (Sint16)state.lY); break;
   1.634 -			case DIJOFS_Z: SDL_PrivateJoystickAxis_Int(joystick, in->num, (Sint16)state.lZ); break;
   1.635 -			case DIJOFS_RX: SDL_PrivateJoystickAxis_Int(joystick, in->num, (Sint16)state.lRx); break;
   1.636 -			case DIJOFS_RY: SDL_PrivateJoystickAxis_Int(joystick, in->num, (Sint16)state.lRy); break;
   1.637 -			case DIJOFS_RZ: SDL_PrivateJoystickAxis_Int(joystick, in->num, (Sint16)state.lRz); break;
   1.638 -			case DIJOFS_SLIDER(0): SDL_PrivateJoystickAxis_Int(joystick, in->num, (Sint16)state.rglSlider[0]); break;
   1.639 -			case DIJOFS_SLIDER(1): SDL_PrivateJoystickAxis_Int(joystick, in->num, (Sint16)state.rglSlider[0]); break;
   1.640 -			}
   1.641 +        switch (in->type) {
   1.642 +        case AXIS:
   1.643 +            switch (in->ofs) {
   1.644 +            case DIJOFS_X:
   1.645 +                SDL_PrivateJoystickAxis_Int (joystick, in->num,
   1.646 +                                             (Sint16) state.lX);
   1.647 +                break;
   1.648 +            case DIJOFS_Y:
   1.649 +                SDL_PrivateJoystickAxis_Int (joystick, in->num,
   1.650 +                                             (Sint16) state.lY);
   1.651 +                break;
   1.652 +            case DIJOFS_Z:
   1.653 +                SDL_PrivateJoystickAxis_Int (joystick, in->num,
   1.654 +                                             (Sint16) state.lZ);
   1.655 +                break;
   1.656 +            case DIJOFS_RX:
   1.657 +                SDL_PrivateJoystickAxis_Int (joystick, in->num,
   1.658 +                                             (Sint16) state.lRx);
   1.659 +                break;
   1.660 +            case DIJOFS_RY:
   1.661 +                SDL_PrivateJoystickAxis_Int (joystick, in->num,
   1.662 +                                             (Sint16) state.lRy);
   1.663 +                break;
   1.664 +            case DIJOFS_RZ:
   1.665 +                SDL_PrivateJoystickAxis_Int (joystick, in->num,
   1.666 +                                             (Sint16) state.lRz);
   1.667 +                break;
   1.668 +            case DIJOFS_SLIDER (0):
   1.669 +                SDL_PrivateJoystickAxis_Int (joystick, in->num,
   1.670 +                                             (Sint16) state.rglSlider[0]);
   1.671 +                break;
   1.672 +            case DIJOFS_SLIDER (1):
   1.673 +                SDL_PrivateJoystickAxis_Int (joystick, in->num,
   1.674 +                                             (Sint16) state.rglSlider[0]);
   1.675 +                break;
   1.676 +            }
   1.677  
   1.678 -			break;
   1.679 +            break;
   1.680  
   1.681 -		case BUTTON:
   1.682 -			SDL_PrivateJoystickButton_Int(joystick, in->num, (Uint8) (state.rgbButtons[in->ofs - DIJOFS_BUTTON0]?SDL_PRESSED:SDL_RELEASED));
   1.683 -			break;
   1.684 -		case HAT:
   1.685 -		    {
   1.686 -			Uint8 pos = TranslatePOV(state.rgdwPOV[in->ofs - DIJOFS_POV(0)]);
   1.687 -			SDL_PrivateJoystickHat_Int(joystick, in->num, pos);
   1.688 -			break;
   1.689 -		    }
   1.690 -		}
   1.691 -	}
   1.692 +        case BUTTON:
   1.693 +            SDL_PrivateJoystickButton_Int (joystick, in->num,
   1.694 +                                           (Uint8) (state.
   1.695 +                                                    rgbButtons[in->ofs -
   1.696 +                                                               DIJOFS_BUTTON0]
   1.697 +                                                    ? SDL_PRESSED :
   1.698 +                                                    SDL_RELEASED));
   1.699 +            break;
   1.700 +        case HAT:
   1.701 +            {
   1.702 +                Uint8 pos =
   1.703 +                    TranslatePOV (state.rgdwPOV[in->ofs - DIJOFS_POV (0)]);
   1.704 +                SDL_PrivateJoystickHat_Int (joystick, in->num, pos);
   1.705 +                break;
   1.706 +            }
   1.707 +        }
   1.708 +    }
   1.709  }
   1.710  
   1.711 -void SDL_SYS_JoystickUpdate_Buffered(SDL_Joystick *joystick)
   1.712 +void
   1.713 +SDL_SYS_JoystickUpdate_Buffered (SDL_Joystick * joystick)
   1.714  {
   1.715 -	int i;
   1.716 -	HRESULT  result;
   1.717 -	DWORD numevents;
   1.718 -	DIDEVICEOBJECTDATA evtbuf[INPUT_QSIZE];
   1.719 +    int i;
   1.720 +    HRESULT result;
   1.721 +    DWORD numevents;
   1.722 +    DIDEVICEOBJECTDATA evtbuf[INPUT_QSIZE];
   1.723  
   1.724 -	numevents = INPUT_QSIZE;
   1.725 -	result = IDirectInputDevice2_GetDeviceData(
   1.726 -			joystick->hwdata->InputDevice, sizeof(DIDEVICEOBJECTDATA),
   1.727 -						evtbuf, &numevents, 0);
   1.728 -	if ( result == DIERR_INPUTLOST || result == DIERR_NOTACQUIRED ) {
   1.729 -		IDirectInputDevice2_Acquire(joystick->hwdata->InputDevice);
   1.730 -		result = IDirectInputDevice2_GetDeviceData(
   1.731 -			joystick->hwdata->InputDevice, sizeof(DIDEVICEOBJECTDATA),
   1.732 -						evtbuf, &numevents, 0);
   1.733 -	}
   1.734 +    numevents = INPUT_QSIZE;
   1.735 +    result =
   1.736 +        IDirectInputDevice2_GetDeviceData (joystick->hwdata->InputDevice,
   1.737 +                                           sizeof (DIDEVICEOBJECTDATA),
   1.738 +                                           evtbuf, &numevents, 0);
   1.739 +    if (result == DIERR_INPUTLOST || result == DIERR_NOTACQUIRED) {
   1.740 +        IDirectInputDevice2_Acquire (joystick->hwdata->InputDevice);
   1.741 +        result =
   1.742 +            IDirectInputDevice2_GetDeviceData (joystick->hwdata->
   1.743 +                                               InputDevice,
   1.744 +                                               sizeof (DIDEVICEOBJECTDATA),
   1.745 +                                               evtbuf, &numevents, 0);
   1.746 +    }
   1.747  
   1.748 -	/* Handle the events */
   1.749 -	if ( result != DI_OK )
   1.750 -	    return;
   1.751 +    /* Handle the events */
   1.752 +    if (result != DI_OK)
   1.753 +        return;
   1.754  
   1.755 -	for(i = 0; i < (int) numevents; ++i)
   1.756 -	{
   1.757 -		int j;
   1.758 +    for (i = 0; i < (int) numevents; ++i) {
   1.759 +        int j;
   1.760  
   1.761 -		for(j = 0; j < joystick->hwdata->NumInputs; ++j)
   1.762 -		{
   1.763 -			const input_t *in = &joystick->hwdata->Inputs[j];
   1.764 +        for (j = 0; j < joystick->hwdata->NumInputs; ++j) {
   1.765 +            const input_t *in = &joystick->hwdata->Inputs[j];
   1.766  
   1.767 -			if(evtbuf[i].dwOfs != in->ofs)
   1.768 -				continue;
   1.769 -		
   1.770 -			switch(in->type)
   1.771 -			{
   1.772 -			case AXIS:
   1.773 -				SDL_PrivateJoystickAxis(joystick, in->num, (Sint16)evtbuf[i].dwData);
   1.774 -				break;
   1.775 -			case BUTTON:
   1.776 -				SDL_PrivateJoystickButton(joystick, in->num, (Uint8) (evtbuf[i].dwData?SDL_PRESSED:SDL_RELEASED));
   1.777 -				break;
   1.778 -			case HAT:
   1.779 -			    {
   1.780 -				Uint8 pos = TranslatePOV(evtbuf[i].dwData);
   1.781 -				SDL_PrivateJoystickHat(joystick, in->num, pos);
   1.782 -			    }
   1.783 -			}
   1.784 -		}
   1.785 -	}
   1.786 +            if (evtbuf[i].dwOfs != in->ofs)
   1.787 +                continue;
   1.788 +
   1.789 +            switch (in->type) {
   1.790 +            case AXIS:
   1.791 +                SDL_PrivateJoystickAxis (joystick, in->num,
   1.792 +                                         (Sint16) evtbuf[i].dwData);
   1.793 +                break;
   1.794 +            case BUTTON:
   1.795 +                SDL_PrivateJoystickButton (joystick, in->num,
   1.796 +                                           (Uint8) (evtbuf[i].
   1.797 +                                                    dwData ? SDL_PRESSED
   1.798 +                                                    : SDL_RELEASED));
   1.799 +                break;
   1.800 +            case HAT:
   1.801 +                {
   1.802 +                    Uint8 pos = TranslatePOV (evtbuf[i].dwData);
   1.803 +                    SDL_PrivateJoystickHat (joystick, in->num, pos);
   1.804 +                }
   1.805 +            }
   1.806 +        }
   1.807 +    }
   1.808  }
   1.809  
   1.810 -void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
   1.811 +void
   1.812 +SDL_SYS_JoystickUpdate (SDL_Joystick * joystick)
   1.813  {
   1.814 -	HRESULT  result;
   1.815 +    HRESULT result;
   1.816  
   1.817 -	result = IDirectInputDevice2_Poll(joystick->hwdata->InputDevice);
   1.818 -	if (result == DIERR_INPUTLOST || result == DIERR_NOTACQUIRED) {
   1.819 -		IDirectInputDevice2_Acquire(joystick->hwdata->InputDevice);
   1.820 -		IDirectInputDevice2_Poll(joystick->hwdata->InputDevice);
   1.821 -	}
   1.822 +    result = IDirectInputDevice2_Poll (joystick->hwdata->InputDevice);
   1.823 +    if (result == DIERR_INPUTLOST || result == DIERR_NOTACQUIRED) {
   1.824 +        IDirectInputDevice2_Acquire (joystick->hwdata->InputDevice);
   1.825 +        IDirectInputDevice2_Poll (joystick->hwdata->InputDevice);
   1.826 +    }
   1.827  
   1.828 -	if(joystick->hwdata->buffered)
   1.829 -		SDL_SYS_JoystickUpdate_Buffered(joystick);
   1.830 -	else
   1.831 -		SDL_SYS_JoystickUpdate_Polled(joystick);
   1.832 +    if (joystick->hwdata->buffered)
   1.833 +        SDL_SYS_JoystickUpdate_Buffered (joystick);
   1.834 +    else
   1.835 +        SDL_SYS_JoystickUpdate_Polled (joystick);
   1.836  }
   1.837  
   1.838  /* Function to close a joystick after use */
   1.839 -void SDL_SYS_JoystickClose(SDL_Joystick *joystick)
   1.840 +void
   1.841 +SDL_SYS_JoystickClose (SDL_Joystick * joystick)
   1.842  {
   1.843 -	IDirectInputDevice2_Unacquire(joystick->hwdata->InputDevice);
   1.844 -	IDirectInputDevice2_Release(joystick->hwdata->InputDevice);
   1.845 +    IDirectInputDevice2_Unacquire (joystick->hwdata->InputDevice);
   1.846 +    IDirectInputDevice2_Release (joystick->hwdata->InputDevice);
   1.847  
   1.848 -	if (joystick->hwdata != NULL) {
   1.849 -		/* free system specific hardware data */
   1.850 -		free(joystick->hwdata);
   1.851 -	}
   1.852 +    if (joystick->hwdata != NULL) {
   1.853 +        /* free system specific hardware data */
   1.854 +        free (joystick->hwdata);
   1.855 +    }
   1.856  }
   1.857  
   1.858  /* Function to perform any system-specific joystick related cleanup */
   1.859 -void SDL_SYS_JoystickQuit(void)
   1.860 +void
   1.861 +SDL_SYS_JoystickQuit (void)
   1.862  {
   1.863 -	IDirectInput_Release(dinput);
   1.864 -	dinput = NULL;
   1.865 -	DX5_Unload();
   1.866 +    IDirectInput_Release (dinput);
   1.867 +    dinput = NULL;
   1.868 +    DX5_Unload ();
   1.869  }
   1.870  
   1.871  #endif /* SDL_JOYSTICK_DINPUT */
   1.872 +/* vi: set ts=4 sw=4 expandtab: */