src/joystick/hidapi/SDL_hidapi_xbox360.c
changeset 12641 64597a7e8771
parent 12503 806492103856
child 12769 096f2f3c4bdc
     1.1 --- a/src/joystick/hidapi/SDL_hidapi_xbox360.c	Sat Mar 16 17:47:59 2019 -0700
     1.2 +++ b/src/joystick/hidapi/SDL_hidapi_xbox360.c	Tue Mar 12 20:27:54 2019 -0400
     1.3 @@ -54,6 +54,7 @@
     1.4  
     1.5  
     1.6  typedef struct {
     1.7 +    SDL_JoystickID joystickID;
     1.8      Uint8 last_state[USB_PACKET_LENGTH];
     1.9      Uint32 rumble_expiration;
    1.10  #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
    1.11 @@ -277,7 +278,7 @@
    1.12  }
    1.13  
    1.14  static SDL_bool
    1.15 -HIDAPI_DriverXbox360_Init(SDL_Joystick *joystick, hid_device *dev, Uint16 vendor_id, Uint16 product_id, void **context)
    1.16 +HIDAPI_DriverXbox360_InitDriver(SDL_HIDAPI_DriverData *context, Uint16 vendor_id, Uint16 product_id, int *num_joysticks)
    1.17  {
    1.18      SDL_DriverXbox360_Context *ctx;
    1.19  
    1.20 @@ -296,10 +297,20 @@
    1.21  #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
    1.22      HIDAPI_DriverXbox360_InitWindowsGamingInput(ctx);
    1.23  #endif
    1.24 -    *context = ctx;
    1.25 +    context->context = ctx;
    1.26  
    1.27 +    ctx->joystickID = SDL_GetNextJoystickInstanceID();
    1.28 +    *num_joysticks += 1;
    1.29 +    SDL_PrivateJoystickAdded(ctx->joystickID);
    1.30 +
    1.31 +    return SDL_TRUE;
    1.32 +}
    1.33 +
    1.34 +static SDL_bool
    1.35 +HIDAPI_DriverXbox360_OpenJoystick(SDL_HIDAPI_DriverData *context, SDL_Joystick *joystick)
    1.36 +{
    1.37      /* Set the controller LED */
    1.38 -    SetSlotLED(dev, (joystick->instance_id % 4));
    1.39 +    SetSlotLED(context->device, (joystick->instance_id % 4));
    1.40  
    1.41      /* Initialize the joystick capabilities */
    1.42      joystick->nbuttons = SDL_CONTROLLER_BUTTON_MAX;
    1.43 @@ -310,9 +321,22 @@
    1.44  }
    1.45  
    1.46  static int
    1.47 -HIDAPI_DriverXbox360_Rumble(SDL_Joystick *joystick, hid_device *dev, void *context, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)
    1.48 +HIDAPI_DriverXbox360_NumJoysticks(SDL_HIDAPI_DriverData *context)
    1.49  {
    1.50 -    SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)context;
    1.51 +    return 1;
    1.52 +}
    1.53 +
    1.54 +static SDL_JoystickID
    1.55 +HIDAPI_DriverXbox360_InstanceIDForIndex(SDL_HIDAPI_DriverData *context, int index)
    1.56 +{
    1.57 +    SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)context->context;
    1.58 +    return ctx->joystickID;
    1.59 +}
    1.60 +
    1.61 +static int
    1.62 +HIDAPI_DriverXbox360_Rumble(SDL_HIDAPI_DriverData *context, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)
    1.63 +{
    1.64 +    SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)context->context;
    1.65  
    1.66  #ifdef __WIN32__
    1.67      SDL_bool rumbled = SDL_FALSE;
    1.68 @@ -365,7 +389,7 @@
    1.69      rumble_packet[4] = (high_frequency_rumble >> 8);
    1.70  #endif
    1.71  
    1.72 -    if (hid_write(dev, rumble_packet, sizeof(rumble_packet)) != sizeof(rumble_packet)) {
    1.73 +    if (hid_write(context->device, rumble_packet, sizeof(rumble_packet)) != sizeof(rumble_packet)) {
    1.74          return SDL_SetError("Couldn't send rumble packet");
    1.75      }
    1.76  #endif /* __WIN32__ */
    1.77 @@ -705,26 +729,31 @@
    1.78  #endif /* __MACOSX__ */
    1.79  
    1.80  static SDL_bool
    1.81 -HIDAPI_DriverXbox360_Update(SDL_Joystick *joystick, hid_device *dev, void *context)
    1.82 +HIDAPI_DriverXbox360_UpdateDriver(SDL_HIDAPI_DriverData *context, int *num_joysticks)
    1.83  {
    1.84 -    SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)context;
    1.85 +    SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)context->context;
    1.86 +    SDL_Joystick *joystick = SDL_JoystickFromInstanceID(ctx->joystickID);
    1.87      Uint8 data[USB_PACKET_LENGTH];
    1.88      int size;
    1.89  
    1.90 -    while ((size = hid_read_timeout(dev, data, sizeof(data), 0)) > 0) {
    1.91 +    if (joystick == NULL) {
    1.92 +        return SDL_TRUE; /* Nothing to do right now! */
    1.93 +    }
    1.94 +
    1.95 +    while ((size = hid_read_timeout(context->device, data, sizeof(data), 0)) > 0) {
    1.96  #ifdef __WIN32__
    1.97 -        HIDAPI_DriverXbox360_HandleStatePacket(joystick, dev, ctx, data, size);
    1.98 +        HIDAPI_DriverXbox360_HandleStatePacket(joystick, context->device, ctx, data, size);
    1.99  #else
   1.100          switch (data[0]) {
   1.101          case 0x00:
   1.102 -            HIDAPI_DriverXbox360_HandleStatePacket(joystick, dev, ctx, data, size);
   1.103 +            HIDAPI_DriverXbox360_HandleStatePacket(joystick, context->device, ctx, data, size);
   1.104              break;
   1.105  #ifdef __MACOSX__
   1.106          case 0x01:
   1.107 -            HIDAPI_DriverXboxOneS_HandleStatePacket(joystick, dev, ctx, data, size);
   1.108 +            HIDAPI_DriverXboxOneS_HandleStatePacket(joystick, context->device, ctx, data, size);
   1.109              break;
   1.110          case 0x02:
   1.111 -            HIDAPI_DriverXboxOneS_HandleGuidePacket(joystick, dev, ctx, data, size);
   1.112 +            HIDAPI_DriverXboxOneS_HandleGuidePacket(joystick, context->device, ctx, data, size);
   1.113              break;
   1.114  #endif
   1.115          default:
   1.116 @@ -742,7 +771,7 @@
   1.117      if (ctx->rumble_expiration) {
   1.118          Uint32 now = SDL_GetTicks();
   1.119          if (SDL_TICKS_PASSED(now, ctx->rumble_expiration)) {
   1.120 -            HIDAPI_DriverXbox360_Rumble(joystick, dev, context, 0, 0, 0);
   1.121 +            HIDAPI_DriverXbox360_Rumble(context, joystick, 0, 0, 0);
   1.122          }
   1.123      }
   1.124  
   1.125 @@ -750,11 +779,9 @@
   1.126  }
   1.127  
   1.128  static void
   1.129 -HIDAPI_DriverXbox360_Quit(SDL_Joystick *joystick, hid_device *dev, void *context)
   1.130 +HIDAPI_DriverXbox360_QuitDriver(SDL_HIDAPI_DriverData *context, SDL_bool send_event, int *num_joysticks)
   1.131  {
   1.132 -#if defined(SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT) || defined(SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT)
   1.133 -    SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)context;
   1.134 -#endif
   1.135 +    SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)context->context;
   1.136  
   1.137  #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
   1.138      if (ctx->xinput_enabled) {
   1.139 @@ -765,7 +792,11 @@
   1.140  #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
   1.141      HIDAPI_DriverXbox360_InitWindowsGamingInput(ctx);
   1.142  #endif
   1.143 -    SDL_free(context);
   1.144 +    *num_joysticks -= 1;
   1.145 +    if (send_event) {
   1.146 +        SDL_PrivateJoystickRemoved(ctx->joystickID);
   1.147 +    }
   1.148 +    SDL_free(context->context);
   1.149  }
   1.150  
   1.151  SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXbox360 =
   1.152 @@ -774,10 +805,13 @@
   1.153      SDL_TRUE,
   1.154      HIDAPI_DriverXbox360_IsSupportedDevice,
   1.155      HIDAPI_DriverXbox360_GetDeviceName,
   1.156 -    HIDAPI_DriverXbox360_Init,
   1.157 -    HIDAPI_DriverXbox360_Rumble,
   1.158 -    HIDAPI_DriverXbox360_Update,
   1.159 -    HIDAPI_DriverXbox360_Quit
   1.160 +    HIDAPI_DriverXbox360_InitDriver,
   1.161 +    HIDAPI_DriverXbox360_QuitDriver,
   1.162 +    HIDAPI_DriverXbox360_UpdateDriver,
   1.163 +    HIDAPI_DriverXbox360_NumJoysticks,
   1.164 +    HIDAPI_DriverXbox360_InstanceIDForIndex,
   1.165 +    HIDAPI_DriverXbox360_OpenJoystick,
   1.166 +    HIDAPI_DriverXbox360_Rumble
   1.167  };
   1.168  
   1.169  #endif /* SDL_JOYSTICK_HIDAPI_XBOX360 */