Fixed Xbox One S Bluetooth support on Mac OS X
authorSam Lantinga <slouken@libsdl.org>
Wed, 29 Aug 2018 18:56:54 -0700
changeset 1216112a877a0ccb5
parent 12160 c63903ab7150
child 12162 03a4f7b4ec77
Fixed Xbox One S Bluetooth support on Mac OS X
src/joystick/hidapi/SDL_hidapi_xbox360.c
src/joystick/hidapi/SDL_hidapi_xboxone.c
     1.1 --- a/src/joystick/hidapi/SDL_hidapi_xbox360.c	Wed Aug 29 11:04:02 2018 +0300
     1.2 +++ b/src/joystick/hidapi/SDL_hidapi_xbox360.c	Wed Aug 29 18:56:54 2018 -0700
     1.3 @@ -394,6 +394,106 @@
     1.4  }
     1.5  #endif /* __WIN32__ */
     1.6  
     1.7 +#ifdef __MACOSX__
     1.8 +static void
     1.9 +HIDAPI_DriverXboxOneS_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXbox360_Context *ctx, Uint8 *data, int size)
    1.10 +{
    1.11 +    Sint16 axis;
    1.12 +
    1.13 +    if (ctx->last_state[14] != data[14]) {
    1.14 +        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_A, (data[14] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
    1.15 +        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_B, (data[14] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
    1.16 +        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_X, (data[14] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
    1.17 +        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_Y, (data[14] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
    1.18 +        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, (data[14] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
    1.19 +        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, (data[14] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
    1.20 +    }
    1.21 +
    1.22 +    if (ctx->last_state[15] != data[15]) {
    1.23 +        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_START, (data[15] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
    1.24 +        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSTICK, (data[15] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
    1.25 +        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSTICK, (data[15] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
    1.26 +    }
    1.27 +
    1.28 +    if (ctx->last_state[16] != data[16]) {
    1.29 +        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_BACK, (data[16] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
    1.30 +    }
    1.31 +
    1.32 +    if (ctx->last_state[13] != data[13]) {
    1.33 +        SDL_bool dpad_up = SDL_FALSE;
    1.34 +        SDL_bool dpad_down = SDL_FALSE;
    1.35 +        SDL_bool dpad_left = SDL_FALSE;
    1.36 +        SDL_bool dpad_right = SDL_FALSE;
    1.37 +
    1.38 +        switch (data[13]) {
    1.39 +        case 1:
    1.40 +            dpad_up = SDL_TRUE;
    1.41 +            break;
    1.42 +        case 2:
    1.43 +            dpad_up = SDL_TRUE;
    1.44 +            dpad_right = SDL_TRUE;
    1.45 +            break;
    1.46 +        case 3:
    1.47 +            dpad_right = SDL_TRUE;
    1.48 +            break;
    1.49 +        case 4:
    1.50 +            dpad_right = SDL_TRUE;
    1.51 +            dpad_down = SDL_TRUE;
    1.52 +            break;
    1.53 +        case 5:
    1.54 +            dpad_down = SDL_TRUE;
    1.55 +            break;
    1.56 +        case 6:
    1.57 +            dpad_left = SDL_TRUE;
    1.58 +            dpad_down = SDL_TRUE;
    1.59 +            break;
    1.60 +        case 7:
    1.61 +            dpad_left = SDL_TRUE;
    1.62 +            break;
    1.63 +        case 8:
    1.64 +            dpad_up = SDL_TRUE;
    1.65 +            dpad_left = SDL_TRUE;
    1.66 +            break;
    1.67 +        default:
    1.68 +            break;
    1.69 +        }
    1.70 +        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_DOWN, dpad_down);
    1.71 +        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_UP, dpad_up);
    1.72 +        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, dpad_right);
    1.73 +        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT, dpad_left);
    1.74 +    }
    1.75 +
    1.76 +    axis = (int)*(Uint16*)(&data[1]) - 0x8000;
    1.77 +    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
    1.78 +    axis = (int)*(Uint16*)(&data[3]) - 0x8000;
    1.79 +    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, axis);
    1.80 +    axis = (int)*(Uint16*)(&data[5]) - 0x8000;
    1.81 +    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
    1.82 +    axis = (int)*(Uint16*)(&data[7]) - 0x8000;
    1.83 +    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, axis);
    1.84 +
    1.85 +    axis = ((int)*(Sint16*)(&data[9]) * 64) - 32768;
    1.86 +    if (axis == 32704) {
    1.87 +        axis = 32767;
    1.88 +    }
    1.89 +    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
    1.90 +
    1.91 +    axis = ((int)*(Sint16*)(&data[11]) * 64) - 32768;
    1.92 +    if (axis == 32704) {
    1.93 +        axis = 32767;
    1.94 +    }
    1.95 +    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
    1.96 +
    1.97 +    SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
    1.98 +}
    1.99 +
   1.100 +static void
   1.101 +HIDAPI_DriverXboxOneS_HandleGuidePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXbox360_Context *ctx, Uint8 *data, int size)
   1.102 +{
   1.103 +    SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (data[1] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
   1.104 +}
   1.105 +#endif /* __MACOSX__ */
   1.106 +
   1.107  static SDL_bool
   1.108  HIDAPI_DriverXbox360_Update(SDL_Joystick *joystick, hid_device *dev, void *context)
   1.109  {
   1.110 @@ -409,9 +509,20 @@
   1.111          case 0x00:
   1.112              HIDAPI_DriverXbox360_HandleStatePacket(joystick, dev, ctx, data, size);
   1.113              break;
   1.114 +#ifdef __MACOSX__
   1.115 +        case 0x01:
   1.116 +            HIDAPI_DriverXboxOneS_HandleStatePacket(joystick, dev, ctx, data, size);
   1.117 +            break;
   1.118 +        case 0x02:
   1.119 +            HIDAPI_DriverXboxOneS_HandleGuidePacket(joystick, dev, ctx, data, size);
   1.120 +            break;
   1.121 +#endif
   1.122          default:
   1.123  #ifdef DEBUG_JOYSTICK
   1.124 -            SDL_Log("Unknown Xbox 360 packet: 0x%.2x\n", data[0]);
   1.125 +            SDL_Log("Unknown Xbox 360 packet, size = %d\n", size);
   1.126 +            SDL_Log("%.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x\n",
   1.127 +                data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7],
   1.128 +                data[8], data[9], data[10], data[11], data[12], data[13], data[14], data[15], data[16]);
   1.129  #endif
   1.130              break;
   1.131          }
     2.1 --- a/src/joystick/hidapi/SDL_hidapi_xboxone.c	Wed Aug 29 11:04:02 2018 +0300
     2.2 +++ b/src/joystick/hidapi/SDL_hidapi_xboxone.c	Wed Aug 29 18:56:54 2018 -0700
     2.3 @@ -232,8 +232,14 @@
     2.4      }
     2.5  
     2.6      axis = ((int)*(Sint16*)(&data[6]) * 64) - 32768;
     2.7 +    if (axis == 32704) {
     2.8 +        axis = 32767;
     2.9 +    }
    2.10      SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
    2.11      axis = ((int)*(Sint16*)(&data[8]) * 64) - 32768;
    2.12 +    if (axis == 32704) {
    2.13 +        axis = 32767;
    2.14 +    }
    2.15      SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
    2.16      axis = *(Sint16*)(&data[10]);
    2.17      SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);