Added support for the Xbox One Elite Series 2 controller in wired mode
authorSam Lantinga <slouken@libsdl.org>
Mon, 18 Nov 2019 15:46:13 -0800
changeset 132589d38f48052c6
parent 13257 0a2ce5c4d86f
child 13259 5122b8bc57f6
Added support for the Xbox One Elite Series 2 controller in wired mode
src/joystick/hidapi/SDL_hidapi_xboxone.c
     1.1 --- a/src/joystick/hidapi/SDL_hidapi_xboxone.c	Mon Nov 18 14:08:05 2019 -0800
     1.2 +++ b/src/joystick/hidapi/SDL_hidapi_xboxone.c	Mon Nov 18 15:46:13 2019 -0800
     1.3 @@ -37,6 +37,13 @@
     1.4  #define USB_PACKET_LENGTH   64
     1.5  
     1.6  /*
     1.7 + * This packet is required for Xbox One Elite Series 2 pad
     1.8 + */
     1.9 +static const Uint8 xboxone_elite_series2_init[] = {
    1.10 +    0x04, 0x20, 0x01, 0x00
    1.11 +};
    1.12 +
    1.13 +/*
    1.14   * This packet is required for all Xbox One pads with 2015
    1.15   * or later firmware installed (or present from the factory).
    1.16   */
    1.17 @@ -108,6 +115,7 @@
    1.18  static const SDL_DriverXboxOne_InitPacket xboxone_init_packets[] = {
    1.19      { 0x0e6f, 0x0165, xboxone_hori_init, sizeof(xboxone_hori_init) },
    1.20      { 0x0f0d, 0x0067, xboxone_hori_init, sizeof(xboxone_hori_init) },
    1.21 +    { 0x045e, 0x0b00, xboxone_elite_series2_init, sizeof(xboxone_elite_series2_init) },
    1.22      { 0x0000, 0x0000, xboxone_fw2015_init, sizeof(xboxone_fw2015_init) },
    1.23      { 0x0e6f, 0x0246, xboxone_pdp_init1, sizeof(xboxone_pdp_init1) },
    1.24      { 0x0e6f, 0x0246, xboxone_pdp_init2, sizeof(xboxone_pdp_init2) },
    1.25 @@ -283,100 +291,6 @@
    1.26      SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (data[4] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
    1.27  }
    1.28  
    1.29 -static void
    1.30 -HIDAPI_DriverXboxOne_HandleBluetoothStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXboxOne_Context *ctx, Uint8 *data, int size)
    1.31 -{
    1.32 -    Sint16 axis;
    1.33 -
    1.34 -    if (ctx->last_state[14] != data[14]) {
    1.35 -        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_A, (data[14] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
    1.36 -        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_B, (data[14] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
    1.37 -        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_X, (data[14] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
    1.38 -        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_Y, (data[14] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
    1.39 -        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, (data[14] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
    1.40 -        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, (data[14] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
    1.41 -    }
    1.42 -    if (ctx->last_state[15] != data[15]) {
    1.43 -        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_START, (data[15] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
    1.44 -        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSTICK, (data[15] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
    1.45 -        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSTICK, (data[15] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
    1.46 -    }
    1.47 -    if (ctx->last_state[16] != data[16]) {
    1.48 -        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_BACK, (data[16] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
    1.49 -    }
    1.50 -
    1.51 -    if (ctx->last_state[13] != data[13]) {
    1.52 -        SDL_bool dpad_up = SDL_FALSE;
    1.53 -        SDL_bool dpad_down = SDL_FALSE;
    1.54 -        SDL_bool dpad_left = SDL_FALSE;
    1.55 -        SDL_bool dpad_right = SDL_FALSE;
    1.56 -
    1.57 -        switch (data[13]) {
    1.58 -        case 1:
    1.59 -            dpad_up = SDL_TRUE;
    1.60 -            break;
    1.61 -        case 2:
    1.62 -            dpad_up = SDL_TRUE;
    1.63 -            dpad_right = SDL_TRUE;
    1.64 -            break;
    1.65 -        case 3:
    1.66 -            dpad_right = SDL_TRUE;
    1.67 -            break;
    1.68 -        case 4:
    1.69 -            dpad_right = SDL_TRUE;
    1.70 -            dpad_down = SDL_TRUE;
    1.71 -            break;
    1.72 -        case 5:
    1.73 -            dpad_down = SDL_TRUE;
    1.74 -            break;
    1.75 -        case 6:
    1.76 -            dpad_left = SDL_TRUE;
    1.77 -            dpad_down = SDL_TRUE;
    1.78 -            break;
    1.79 -        case 7:
    1.80 -            dpad_left = SDL_TRUE;
    1.81 -            break;
    1.82 -        case 8:
    1.83 -            dpad_up = SDL_TRUE;
    1.84 -            dpad_left = SDL_TRUE;
    1.85 -            break;
    1.86 -        default:
    1.87 -            break;
    1.88 -        }
    1.89 -        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_DOWN, dpad_down);
    1.90 -        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_UP, dpad_up);
    1.91 -        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, dpad_right);
    1.92 -        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT, dpad_left);
    1.93 -    }
    1.94 -
    1.95 -    axis = ((int)*(Sint16*)(&data[9]) * 64) - 32768;
    1.96 -    if (axis == 32704) {
    1.97 -        axis = 32767;
    1.98 -    }
    1.99 -    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
   1.100 -    axis = ((int)*(Sint16*)(&data[11]) * 64) - 32768;
   1.101 -    if (axis == 32704) {
   1.102 -        axis = 32767;
   1.103 -    }
   1.104 -    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
   1.105 -    axis = *(Uint16*)(&data[1]) - 32768;
   1.106 -    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
   1.107 -    axis = *(Uint16*)(&data[3]) - 32768;
   1.108 -    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, axis);
   1.109 -    axis = *(Uint16*)(&data[5]) - 32768;
   1.110 -    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
   1.111 -    axis = *(Uint16*)(&data[7]) - 32768;
   1.112 -    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, axis);
   1.113 -
   1.114 -    SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
   1.115 -}
   1.116 -
   1.117 -static void
   1.118 -HIDAPI_DriverXboxOne_HandleBluetoothModePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXboxOne_Context *ctx, Uint8 *data, int size)
   1.119 -{
   1.120 -    SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (data[1] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
   1.121 -}
   1.122 -
   1.123  static SDL_bool
   1.124  HIDAPI_DriverXboxOne_Update(SDL_Joystick *joystick, hid_device *dev, void *context)
   1.125  {
   1.126 @@ -396,12 +310,6 @@
   1.127                      data[16], data[17], data[18], data[19]);
   1.128  #endif
   1.129          switch (data[0]) {
   1.130 -        case 0x01:
   1.131 -            HIDAPI_DriverXboxOne_HandleBluetoothStatePacket(joystick, dev, ctx, data, size);
   1.132 -            break;
   1.133 -        case 0x02:
   1.134 -            HIDAPI_DriverXboxOne_HandleBluetoothModePacket(joystick, dev, ctx, data, size);
   1.135 -            break;
   1.136          case 0x20:
   1.137              HIDAPI_DriverXboxOne_HandleStatePacket(joystick, dev, ctx, data, size);
   1.138              break;