src/joystick/windows/SDL_dxjoystick.c
changeset 6220 c36934808194
parent 6138 4c64952a58fb
child 6403 8d34c6248f4a
equal deleted inserted replaced
6219:8e94af244f2c 6220:c36934808194
    72 static void SetDIerror(const char *function, HRESULT code);
    72 static void SetDIerror(const char *function, HRESULT code);
    73 static BOOL CALLBACK EnumJoysticksCallback(const DIDEVICEINSTANCE *
    73 static BOOL CALLBACK EnumJoysticksCallback(const DIDEVICEINSTANCE *
    74                                            pdidInstance, VOID * pContext);
    74                                            pdidInstance, VOID * pContext);
    75 static BOOL CALLBACK EnumDevObjectsCallback(LPCDIDEVICEOBJECTINSTANCE dev,
    75 static BOOL CALLBACK EnumDevObjectsCallback(LPCDIDEVICEOBJECTINSTANCE dev,
    76                                             LPVOID pvRef);
    76                                             LPVOID pvRef);
       
    77 static void SortDevObjects(SDL_Joystick *joystick);
    77 static Uint8 TranslatePOV(DWORD value);
    78 static Uint8 TranslatePOV(DWORD value);
    78 static int SDL_PrivateJoystickAxis_Int(SDL_Joystick * joystick, Uint8 axis,
    79 static int SDL_PrivateJoystickAxis_Int(SDL_Joystick * joystick, Uint8 axis,
    79                                        Sint16 value);
    80                                        Sint16 value);
    80 static int SDL_PrivateJoystickHat_Int(SDL_Joystick * joystick, Uint8 hat,
    81 static int SDL_PrivateJoystickHat_Int(SDL_Joystick * joystick, Uint8 hat,
    81                                       Uint8 value);
    82                                       Uint8 value);
   481     /* What buttons and axes does it have? */
   482     /* What buttons and axes does it have? */
   482     IDirectInputDevice2_EnumObjects(joystick->hwdata->InputDevice,
   483     IDirectInputDevice2_EnumObjects(joystick->hwdata->InputDevice,
   483                                     EnumDevObjectsCallback, joystick,
   484                                     EnumDevObjectsCallback, joystick,
   484                                     DIDFT_BUTTON | DIDFT_AXIS | DIDFT_POV);
   485                                     DIDFT_BUTTON | DIDFT_AXIS | DIDFT_POV);
   485 
   486 
       
   487 	/* Reorder the input objects. Some devices do not report the X axis as
       
   488 	 * the first axis, for example. */
       
   489 	SortDevObjects(joystick);
       
   490 
   486     dipdw.diph.dwObj = 0;
   491     dipdw.diph.dwObj = 0;
   487     dipdw.diph.dwHow = DIPH_DEVICE;
   492     dipdw.diph.dwHow = DIPH_DEVICE;
   488     dipdw.dwData = INPUT_QSIZE;
   493     dipdw.dwData = INPUT_QSIZE;
   489 
   494 
   490     /* Set the buffer size */
   495     /* Set the buffer size */
   500         SetDIerror("IDirectInputDevice2::SetProperty", result);
   505         SetDIerror("IDirectInputDevice2::SetProperty", result);
   501         return (-1);
   506         return (-1);
   502     }
   507     }
   503 
   508 
   504     return (0);
   509     return (0);
       
   510 }
       
   511 
       
   512 /* Sort using the data offset into the DInput struct.
       
   513  * This gives a reasonable ordering for the inputs. */
       
   514 static int
       
   515 SortDevFunc(const void *a, const void *b)
       
   516 {
       
   517 	const input_t *inputA = (const input_t*)a;
       
   518 	const input_t *inputB = (const input_t*)b;
       
   519 
       
   520 	if (inputA->ofs < inputB->ofs)
       
   521 		return -1;
       
   522 	if (inputA->ofs > inputB->ofs)
       
   523 		return 1;
       
   524 	return 0;
       
   525 }
       
   526 
       
   527 /* Sort the input objects and recalculate the indices for each input. */
       
   528 static void
       
   529 SortDevObjects(SDL_Joystick *joystick)
       
   530 {
       
   531 	input_t *inputs = joystick->hwdata->Inputs;
       
   532 	int nButtons = 0;
       
   533 	int nHats = 0;
       
   534 	int nAxis = 0;
       
   535 	int n;
       
   536 
       
   537 	SDL_qsort(inputs, joystick->hwdata->NumInputs, sizeof(input_t), SortDevFunc);
       
   538 
       
   539 	for (n = 0; n < joystick->hwdata->NumInputs; n++)
       
   540 	{
       
   541 		switch (inputs[n].type)
       
   542 		{
       
   543 		case BUTTON:
       
   544 			inputs[n].num = nButtons;
       
   545 			nButtons++;
       
   546 			break;
       
   547 
       
   548 		case HAT:
       
   549 			inputs[n].num = nHats;
       
   550 			nHats++;
       
   551 			break;
       
   552 
       
   553 		case AXIS:
       
   554 			inputs[n].num = nAxis;
       
   555 			nAxis++;
       
   556 			break;
       
   557 		}
       
   558 	}
   505 }
   559 }
   506 
   560 
   507 static BOOL CALLBACK
   561 static BOOL CALLBACK
   508 EnumDevObjectsCallback(LPCDIDEVICEOBJECTINSTANCE dev, LPVOID pvRef)
   562 EnumDevObjectsCallback(LPCDIDEVICEOBJECTINSTANCE dev, LPVOID pvRef)
   509 {
   563 {