src/joystick/windows/SDL_dxjoystick.c
changeset 6220 c36934808194
parent 6138 4c64952a58fb
child 6403 8d34c6248f4a
     1.1 --- a/src/joystick/windows/SDL_dxjoystick.c	Sun Jan 15 03:34:14 2012 -0500
     1.2 +++ b/src/joystick/windows/SDL_dxjoystick.c	Sun Jan 15 15:48:27 2012 -0500
     1.3 @@ -74,6 +74,7 @@
     1.4                                             pdidInstance, VOID * pContext);
     1.5  static BOOL CALLBACK EnumDevObjectsCallback(LPCDIDEVICEOBJECTINSTANCE dev,
     1.6                                              LPVOID pvRef);
     1.7 +static void SortDevObjects(SDL_Joystick *joystick);
     1.8  static Uint8 TranslatePOV(DWORD value);
     1.9  static int SDL_PrivateJoystickAxis_Int(SDL_Joystick * joystick, Uint8 axis,
    1.10                                         Sint16 value);
    1.11 @@ -483,6 +484,10 @@
    1.12                                      EnumDevObjectsCallback, joystick,
    1.13                                      DIDFT_BUTTON | DIDFT_AXIS | DIDFT_POV);
    1.14  
    1.15 +	/* Reorder the input objects. Some devices do not report the X axis as
    1.16 +	 * the first axis, for example. */
    1.17 +	SortDevObjects(joystick);
    1.18 +
    1.19      dipdw.diph.dwObj = 0;
    1.20      dipdw.diph.dwHow = DIPH_DEVICE;
    1.21      dipdw.dwData = INPUT_QSIZE;
    1.22 @@ -504,6 +509,55 @@
    1.23      return (0);
    1.24  }
    1.25  
    1.26 +/* Sort using the data offset into the DInput struct.
    1.27 + * This gives a reasonable ordering for the inputs. */
    1.28 +static int
    1.29 +SortDevFunc(const void *a, const void *b)
    1.30 +{
    1.31 +	const input_t *inputA = (const input_t*)a;
    1.32 +	const input_t *inputB = (const input_t*)b;
    1.33 +
    1.34 +	if (inputA->ofs < inputB->ofs)
    1.35 +		return -1;
    1.36 +	if (inputA->ofs > inputB->ofs)
    1.37 +		return 1;
    1.38 +	return 0;
    1.39 +}
    1.40 +
    1.41 +/* Sort the input objects and recalculate the indices for each input. */
    1.42 +static void
    1.43 +SortDevObjects(SDL_Joystick *joystick)
    1.44 +{
    1.45 +	input_t *inputs = joystick->hwdata->Inputs;
    1.46 +	int nButtons = 0;
    1.47 +	int nHats = 0;
    1.48 +	int nAxis = 0;
    1.49 +	int n;
    1.50 +
    1.51 +	SDL_qsort(inputs, joystick->hwdata->NumInputs, sizeof(input_t), SortDevFunc);
    1.52 +
    1.53 +	for (n = 0; n < joystick->hwdata->NumInputs; n++)
    1.54 +	{
    1.55 +		switch (inputs[n].type)
    1.56 +		{
    1.57 +		case BUTTON:
    1.58 +			inputs[n].num = nButtons;
    1.59 +			nButtons++;
    1.60 +			break;
    1.61 +
    1.62 +		case HAT:
    1.63 +			inputs[n].num = nHats;
    1.64 +			nHats++;
    1.65 +			break;
    1.66 +
    1.67 +		case AXIS:
    1.68 +			inputs[n].num = nAxis;
    1.69 +			nAxis++;
    1.70 +			break;
    1.71 +		}
    1.72 +	}
    1.73 +}
    1.74 +
    1.75  static BOOL CALLBACK
    1.76  EnumDevObjectsCallback(LPCDIDEVICEOBJECTINSTANCE dev, LPVOID pvRef)
    1.77  {