Xbox One controller rumble doesn't need synchronization if you use a packet counter of 0
authorSam Lantinga <slouken@libsdl.org>
Thu, 27 Feb 2020 09:33:32 -0800
changeset 135570e9384077170
parent 13556 3d49344c6988
child 13558 ed7c27865ea7
Xbox One controller rumble doesn't need synchronization if you use a packet counter of 0
src/joystick/hidapi/SDL_hidapi_xboxone.c
     1.1 --- a/src/joystick/hidapi/SDL_hidapi_xboxone.c	Thu Feb 27 09:32:03 2020 -0800
     1.2 +++ b/src/joystick/hidapi/SDL_hidapi_xboxone.c	Thu Feb 27 09:33:32 2020 -0800
     1.3 @@ -74,11 +74,6 @@
     1.4      0x00, 0x00, 0xFF, 0x00, 0xEB
     1.5  };
     1.6  
     1.7 -static const Uint8 xboxone_rumble_reset[] = {
     1.8 -    0x09, 0x00, 0x00, 0x09, 0x00, 0x0F, 0x00, 0x00,
     1.9 -    0x00, 0x00, 0x00, 0x00, 0x00
    1.10 -};
    1.11 -
    1.12  /*
    1.13   * This specifies the selection of init packets that a gamepad
    1.14   * will be sent on init *and* the order in which they will be
    1.15 @@ -117,8 +112,6 @@
    1.16      SDL_bool initialized;
    1.17      Uint8 sequence;
    1.18      Uint8 last_state[USB_PACKET_LENGTH];
    1.19 -    SDL_bool rumble_synchronized;
    1.20 -    SDL_bool rumble_synchronization_complete;
    1.21      SDL_bool has_paddles;
    1.22  } SDL_DriverXboxOne_Context;
    1.23  
    1.24 @@ -168,47 +161,6 @@
    1.25      return SDL_FALSE;
    1.26  }
    1.27  
    1.28 -static SDL_bool
    1.29 -ControllerNeedsRumbleSequenceSynchronized(Uint16 vendor_id, Uint16 product_id)
    1.30 -{
    1.31 -    if (vendor_id == USB_VENDOR_MICROSOFT) {
    1.32 -        /* All Xbox One controllers, from model 1537 through Elite Series 2, appear to need this */
    1.33 -        return SDL_TRUE;
    1.34 -    }
    1.35 -    return SDL_FALSE;
    1.36 -}
    1.37 -
    1.38 -static SDL_bool
    1.39 -SynchronizeRumbleSequence(SDL_HIDAPI_Device *device, SDL_DriverXboxOne_Context *ctx)
    1.40 -{
    1.41 -    Uint16 vendor_id = ctx->vendor_id;
    1.42 -    Uint16 product_id = ctx->product_id;
    1.43 -
    1.44 -    if (ctx->rumble_synchronized) {
    1.45 -        return SDL_TRUE;
    1.46 -    }
    1.47 -
    1.48 -    if (ControllerNeedsRumbleSequenceSynchronized(vendor_id, product_id)) {
    1.49 -        int i;
    1.50 -        Uint8 init_packet[USB_PACKET_LENGTH];
    1.51 -
    1.52 -        SDL_memcpy(init_packet, xboxone_rumble_reset, sizeof(xboxone_rumble_reset));
    1.53 -        for (i = 0; i < 255; ++i) {
    1.54 -            init_packet[2] = ((ctx->sequence + i) % 255);
    1.55 -            if (SDL_HIDAPI_LockRumble() < 0) {
    1.56 -                return SDL_FALSE;
    1.57 -            }
    1.58 -            if (SDL_HIDAPI_SendRumbleAndUnlock(device, init_packet, sizeof(xboxone_rumble_reset)) != sizeof(xboxone_rumble_reset)) {
    1.59 -                SDL_SetError("Couldn't write Xbox One initialization packet");
    1.60 -                return SDL_FALSE;
    1.61 -            }
    1.62 -        }
    1.63 -    }
    1.64 -    ctx->rumble_synchronized = SDL_TRUE;
    1.65 -
    1.66 -    return SDL_TRUE;
    1.67 -}
    1.68 -
    1.69  /* Return true if this controller sends the 0x02 "waiting for init" packet */
    1.70  static SDL_bool
    1.71  ControllerSendsWaitingForInit(Uint16 vendor_id, Uint16 product_id)
    1.72 @@ -293,8 +245,6 @@
    1.73          }
    1.74      }
    1.75  
    1.76 -    SynchronizeRumbleSequence(device, ctx);
    1.77 -
    1.78      return SDL_TRUE;
    1.79  }
    1.80  
    1.81 @@ -373,41 +323,14 @@
    1.82  static int
    1.83  HIDAPI_DriverXboxOne_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
    1.84  {
    1.85 -    SDL_DriverXboxOne_Context *ctx = (SDL_DriverXboxOne_Context *)device->context;
    1.86      Uint8 rumble_packet[] = { 0x09, 0x00, 0x00, 0x09, 0x00, 0x0F, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF };
    1.87 -    Uint8 *pending_rumble;
    1.88 -    int *pending_size;
    1.89 -    int maximum_size;
    1.90 -
    1.91 -    SynchronizeRumbleSequence(device, ctx);
    1.92 -
    1.93 -    if (SDL_HIDAPI_LockRumble() < 0) {
    1.94 -        return -1;
    1.95 -    }
    1.96  
    1.97 -    if (!ctx->rumble_synchronization_complete) {
    1.98 -        if (!SDL_HIDAPI_GetPendingRumbleLocked(device, &pending_rumble, &pending_size, &maximum_size)) {
    1.99 -            /* Rumble synchronization has drained */
   1.100 -            ctx->rumble_synchronization_complete = SDL_TRUE;
   1.101 -        }
   1.102 -    }
   1.103 +    /* Magnitude is 1..100 so scale the 16-bit input here */
   1.104 +    rumble_packet[8] = low_frequency_rumble / 655;
   1.105 +    rumble_packet[9] = high_frequency_rumble / 655;
   1.106  
   1.107 -    /* Try to overwrite any pending rumble with the new value */
   1.108 -    if (ctx->rumble_synchronization_complete && 
   1.109 -        SDL_HIDAPI_GetPendingRumbleLocked(device, &pending_rumble, &pending_size, &maximum_size)) {
   1.110 -        /* Magnitude is 1..100 so scale the 16-bit input here */
   1.111 -        pending_rumble[8] = low_frequency_rumble / 655;
   1.112 -        pending_rumble[9] = high_frequency_rumble / 655;
   1.113 -        SDL_HIDAPI_UnlockRumble();
   1.114 -    } else {
   1.115 -        /* Magnitude is 1..100 so scale the 16-bit input here */
   1.116 -        rumble_packet[2] = ctx->sequence++;
   1.117 -        rumble_packet[8] = low_frequency_rumble / 655;
   1.118 -        rumble_packet[9] = high_frequency_rumble / 655;
   1.119 -
   1.120 -        if (SDL_HIDAPI_SendRumbleAndUnlock(device, rumble_packet, sizeof(rumble_packet)) != sizeof(rumble_packet)) {
   1.121 -            return SDL_SetError("Couldn't send rumble packet");
   1.122 -        }
   1.123 +    if (SDL_HIDAPI_SendRumble(device, rumble_packet, sizeof(rumble_packet)) != sizeof(rumble_packet)) {
   1.124 +        return SDL_SetError("Couldn't send rumble packet");
   1.125      }
   1.126      return 0;
   1.127  }