Added Windows Xbox controller state packet handling, for completeness.
authorSam Lantinga <slouken@libsdl.org>
Wed, 15 Aug 2018 19:53:36 -0700
changeset 12116b65fb56dcf88
parent 12115 0d9277d27e2d
child 12117 8cbdc9b8b055
Added Windows Xbox controller state packet handling, for completeness.
src/joystick/hidapi/SDL_hidapi_xbox360.c
     1.1 --- a/src/joystick/hidapi/SDL_hidapi_xbox360.c	Wed Aug 15 19:53:34 2018 -0700
     1.2 +++ b/src/joystick/hidapi/SDL_hidapi_xbox360.c	Wed Aug 15 19:53:36 2018 -0700
     1.3 @@ -261,6 +261,99 @@
     1.4      return 0;
     1.5  }
     1.6  
     1.7 +#if 0
     1.8 + /* This is the packet format for Xbox 360 and Xbox One controllers on Windows,
     1.9 +    however with this interface there is no rumble support, no guide button,
    1.10 +    and the left and right triggers are tied together as a single axis.
    1.11 +  */
    1.12 +static void
    1.13 +HIDAPI_DriverXboxOne_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXbox360_Context *ctx, Uint8 *data, int size )
    1.14 +{
    1.15 +    Sint16 axis;
    1.16 +
    1.17 +    if (ctx->last_state[10] != data[10]) {
    1.18 +        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_A, (data[10] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
    1.19 +        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_B, (data[10] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
    1.20 +        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_X, (data[10] & 0x04) ? SDL_PRESSED : SDL_RELEASED);
    1.21 +        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_Y, (data[10] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
    1.22 +        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, (data[10] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
    1.23 +        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, (data[10] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
    1.24 +        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_BACK, (data[10] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
    1.25 +        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_START, (data[10] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
    1.26 +    }
    1.27 +
    1.28 +    if (ctx->last_state[11] != data[11]) {
    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 +        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSTICK, (data[11] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
    1.35 +        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSTICK, (data[11] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
    1.36 +
    1.37 +        switch (data[11] & 0x3C) {
    1.38 +        case 4:
    1.39 +            dpad_up = SDL_TRUE;
    1.40 +            break;
    1.41 +        case 8:
    1.42 +            dpad_up = SDL_TRUE;
    1.43 +            dpad_right = SDL_TRUE;
    1.44 +            break;
    1.45 +        case 12:
    1.46 +            dpad_right = SDL_TRUE;
    1.47 +            break;
    1.48 +        case 16:
    1.49 +            dpad_right = SDL_TRUE;
    1.50 +            dpad_down = SDL_TRUE;
    1.51 +            break;
    1.52 +        case 20:
    1.53 +            dpad_down = SDL_TRUE;
    1.54 +            break;
    1.55 +        case 24:
    1.56 +            dpad_left = SDL_TRUE;
    1.57 +            dpad_down = SDL_TRUE;
    1.58 +            break;
    1.59 +        case 28:
    1.60 +            dpad_left = SDL_TRUE;
    1.61 +            break;
    1.62 +        case 32:
    1.63 +            dpad_up = SDL_TRUE;
    1.64 +            dpad_left = SDL_TRUE;
    1.65 +            break;
    1.66 +        default:
    1.67 +            break;
    1.68 +        }
    1.69 +        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_DOWN, dpad_down);
    1.70 +        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_UP, dpad_up);
    1.71 +        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, dpad_right);
    1.72 +        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT, dpad_left);
    1.73 +    }
    1.74 +
    1.75 +    axis = (int)*(Uint16*)(&data[0]) - 0x8000;
    1.76 +    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
    1.77 +    axis = (int)*(Uint16*)(&data[2]) - 0x8000;
    1.78 +    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, axis);
    1.79 +    axis = (int)*(Uint16*)(&data[4]) - 0x8000;
    1.80 +    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
    1.81 +    axis = (int)*(Uint16*)(&data[6]) - 0x8000;
    1.82 +    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, axis);
    1.83 +
    1.84 +    axis = (data[9] * 257) - 32768;
    1.85 +    if (data[9] < 0x80) {
    1.86 +        axis = -axis * 2 - 32769;
    1.87 +        SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
    1.88 +    } else if (data[9] > 0x80) {
    1.89 +        axis = axis * 2 - 32767;
    1.90 +        SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
    1.91 +    } else {
    1.92 +        SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, SDL_MIN_SINT16);
    1.93 +        SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, SDL_MIN_SINT16);
    1.94 +    }
    1.95 +
    1.96 +    SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
    1.97 +}
    1.98 +#endif /* 0 */
    1.99 +
   1.100  static void
   1.101  HIDAPI_DriverXbox360_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXbox360_Context *ctx, Uint8 *data, int size)
   1.102  {