src/joystick/hidapi/SDL_hidapi_xboxone.c
changeset 13620 317366b2c172
parent 13606 6b0a65d1e16f
child 13633 340324c76848
     1.1 --- a/src/joystick/hidapi/SDL_hidapi_xboxone.c	Fri Mar 13 19:00:22 2020 -0700
     1.2 +++ b/src/joystick/hidapi/SDL_hidapi_xboxone.c	Fri Mar 13 19:00:24 2020 -0700
     1.3 @@ -478,7 +478,96 @@
     1.4  }
     1.5  
     1.6  static void
     1.7 -HIDAPI_DriverXboxOneBluetooth_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXboxOne_Context *ctx, Uint8 *data, int size)
     1.8 +HIDAPI_DriverXboxOneBluetooth_HandleStatePacketV1(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXboxOne_Context *ctx, Uint8 *data, int size)
     1.9 +{
    1.10 +    Sint16 axis;
    1.11 +
    1.12 +    if (ctx->last_state[14] != data[14]) {
    1.13 +        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_A, (data[14] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
    1.14 +        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_B, (data[14] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
    1.15 +        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_X, (data[14] & 0x04) ? SDL_PRESSED : SDL_RELEASED);
    1.16 +        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_Y, (data[14] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
    1.17 +        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, (data[14] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
    1.18 +        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, (data[14] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
    1.19 +        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_BACK, (data[14] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
    1.20 +        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_START, (data[14] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
    1.21 +    }
    1.22 +
    1.23 +    if (ctx->last_state[15] != data[15]) {
    1.24 +        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSTICK, (data[15] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
    1.25 +        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSTICK, (data[15] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
    1.26 +    }
    1.27 +
    1.28 +    if (ctx->last_state[13] != data[13]) {
    1.29 +        SDL_bool dpad_up = SDL_FALSE;
    1.30 +        SDL_bool dpad_down = SDL_FALSE;
    1.31 +        SDL_bool dpad_left = SDL_FALSE;
    1.32 +        SDL_bool dpad_right = SDL_FALSE;
    1.33 +
    1.34 +        switch (data[13]) {
    1.35 +        case 1:
    1.36 +            dpad_up = SDL_TRUE;
    1.37 +            break;
    1.38 +        case 2:
    1.39 +            dpad_up = SDL_TRUE;
    1.40 +            dpad_right = SDL_TRUE;
    1.41 +            break;
    1.42 +        case 3:
    1.43 +            dpad_right = SDL_TRUE;
    1.44 +            break;
    1.45 +        case 4:
    1.46 +            dpad_right = SDL_TRUE;
    1.47 +            dpad_down = SDL_TRUE;
    1.48 +            break;
    1.49 +        case 5:
    1.50 +            dpad_down = SDL_TRUE;
    1.51 +            break;
    1.52 +        case 6:
    1.53 +            dpad_left = SDL_TRUE;
    1.54 +            dpad_down = SDL_TRUE;
    1.55 +            break;
    1.56 +        case 7:
    1.57 +            dpad_left = SDL_TRUE;
    1.58 +            break;
    1.59 +        case 8:
    1.60 +            dpad_up = SDL_TRUE;
    1.61 +            dpad_left = SDL_TRUE;
    1.62 +            break;
    1.63 +        default:
    1.64 +            break;
    1.65 +        }
    1.66 +        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_DOWN, dpad_down);
    1.67 +        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_UP, dpad_up);
    1.68 +        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, dpad_right);
    1.69 +        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT, dpad_left);
    1.70 +    }
    1.71 +
    1.72 +    axis = (int)*(Uint16*)(&data[1]) - 0x8000;
    1.73 +    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
    1.74 +    axis = (int)*(Uint16*)(&data[3]) - 0x8000;
    1.75 +    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, axis);
    1.76 +    axis = (int)*(Uint16*)(&data[5]) - 0x8000;
    1.77 +    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
    1.78 +    axis = (int)*(Uint16*)(&data[7]) - 0x8000;
    1.79 +    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, axis);
    1.80 +
    1.81 +    axis = ((int)*(Sint16*)(&data[9]) * 64) - 32768;
    1.82 +    if (axis == 32704) {
    1.83 +        axis = 32767;
    1.84 +    }
    1.85 +    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
    1.86 +
    1.87 +    axis = ((int)*(Sint16*)(&data[11]) * 64) - 32768;
    1.88 +    if (axis == 32704) {
    1.89 +        axis = 32767;
    1.90 +    }
    1.91 +    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
    1.92 +
    1.93 +    SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
    1.94 +}
    1.95 +
    1.96 +static void
    1.97 +HIDAPI_DriverXboxOneBluetooth_HandleStatePacketV2(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXboxOne_Context *ctx, Uint8 *data, int size)
    1.98  {
    1.99      Sint16 axis;
   1.100  
   1.101 @@ -619,7 +708,16 @@
   1.102          if (ctx->bluetooth) {
   1.103              switch (data[0]) {
   1.104              case 0x01:
   1.105 -                HIDAPI_DriverXboxOneBluetooth_HandleStatePacket(joystick, device->dev, ctx, data, size);
   1.106 +                switch (size) {
   1.107 +                case 16:
   1.108 +                    HIDAPI_DriverXboxOneBluetooth_HandleStatePacketV1(joystick, device->dev, ctx, data, size);
   1.109 +                    break;
   1.110 +                case 17:
   1.111 +                    HIDAPI_DriverXboxOneBluetooth_HandleStatePacketV2(joystick, device->dev, ctx, data, size);
   1.112 +                    break;
   1.113 +                default:
   1.114 +                    break;
   1.115 +                }
   1.116                  break;
   1.117              case 0x02:
   1.118                  HIDAPI_DriverXboxOneBluetooth_HandleGuidePacket(joystick, device->dev, ctx, data, size);