src/joystick/windows/SDL_xinputjoystick.c
changeset 9884 26b595dea221
parent 9660 b7188b6a6f2a
child 9918 31b7adf67756
     1.1 --- a/src/joystick/windows/SDL_xinputjoystick.c	Wed Sep 30 15:38:30 2015 -0700
     1.2 +++ b/src/joystick/windows/SDL_xinputjoystick.c	Wed Sep 30 15:39:30 2015 -0700
     1.3 @@ -221,8 +221,39 @@
     1.4      return 0;
     1.5  }
     1.6  
     1.7 +static void 
     1.8 +UpdateXInputJoystickBatteryInformation(SDL_Joystick * joystick, XINPUT_BATTERY_INFORMATION *pBatteryInformation)
     1.9 +{
    1.10 +    if ( pBatteryInformation->BatteryType != BATTERY_TYPE_UNKNOWN )
    1.11 +    {
    1.12 +        SDL_JoystickPowerLevel ePowerLevel = SDL_JOYSTICK_POWER_UNKNOWN;
    1.13 +        if (pBatteryInformation->BatteryType == BATTERY_TYPE_WIRED) {
    1.14 +            ePowerLevel = SDL_JOYSTICK_POWER_WIRED;
    1.15 +        } else {
    1.16 +            switch ( pBatteryInformation->BatteryLevel )
    1.17 +            {
    1.18 +            case BATTERY_LEVEL_EMPTY:
    1.19 +                ePowerLevel = SDL_JOYSTICK_POWER_EMPTY;
    1.20 +                break;
    1.21 +            case BATTERY_LEVEL_LOW:
    1.22 +                ePowerLevel = SDL_JOYSTICK_POWER_LOW;
    1.23 +                break;
    1.24 +            case BATTERY_LEVEL_MEDIUM:
    1.25 +                ePowerLevel = SDL_JOYSTICK_POWER_MEDIUM;
    1.26 +                break;
    1.27 +            default:
    1.28 +            case BATTERY_LEVEL_FULL:
    1.29 +                ePowerLevel = SDL_JOYSTICK_POWER_FULL;
    1.30 +                break;
    1.31 +            }
    1.32 +        }
    1.33 +
    1.34 +        SDL_PrivateJoystickBatteryLevel( joystick, ePowerLevel );
    1.35 +    }
    1.36 +}
    1.37 +
    1.38  static void
    1.39 -UpdateXInputJoystickState_OLD(SDL_Joystick * joystick, XINPUT_STATE_EX *pXInputState)
    1.40 +UpdateXInputJoystickState_OLD(SDL_Joystick * joystick, XINPUT_STATE_EX *pXInputState, XINPUT_BATTERY_INFORMATION *pBatteryInformation)
    1.41  {
    1.42      static WORD s_XInputButtons[] = {
    1.43          XINPUT_GAMEPAD_DPAD_UP, XINPUT_GAMEPAD_DPAD_DOWN, XINPUT_GAMEPAD_DPAD_LEFT, XINPUT_GAMEPAD_DPAD_RIGHT,
    1.44 @@ -244,10 +275,12 @@
    1.45      for (button = 0; button < SDL_arraysize(s_XInputButtons); ++button) {
    1.46          SDL_PrivateJoystickButton(joystick, button, (wButtons & s_XInputButtons[button]) ? SDL_PRESSED : SDL_RELEASED);
    1.47      }
    1.48 +
    1.49 +    UpdateXInputJoystickBatteryInformation( joystick, pBatteryInformation );
    1.50  }
    1.51  
    1.52  static void
    1.53 -UpdateXInputJoystickState(SDL_Joystick * joystick, XINPUT_STATE_EX *pXInputState)
    1.54 +UpdateXInputJoystickState(SDL_Joystick * joystick, XINPUT_STATE_EX *pXInputState, XINPUT_BATTERY_INFORMATION *pBatteryInformation)
    1.55  {
    1.56      static WORD s_XInputButtons[] = {
    1.57          XINPUT_GAMEPAD_A, XINPUT_GAMEPAD_B, XINPUT_GAMEPAD_X, XINPUT_GAMEPAD_Y,
    1.58 @@ -283,6 +316,8 @@
    1.59          hat |= SDL_HAT_RIGHT;
    1.60      }
    1.61      SDL_PrivateJoystickHat(joystick, 0, hat);
    1.62 +
    1.63 +    UpdateXInputJoystickBatteryInformation( joystick, pBatteryInformation );
    1.64  }
    1.65  
    1.66  void
    1.67 @@ -290,6 +325,7 @@
    1.68  {
    1.69      HRESULT result;
    1.70      XINPUT_STATE_EX XInputState;
    1.71 +    XINPUT_BATTERY_INFORMATION XBatteryInformation;
    1.72  
    1.73      if (!XINPUTGETSTATE)
    1.74          return;
    1.75 @@ -301,12 +337,18 @@
    1.76          return;
    1.77      }
    1.78  
    1.79 +    SDL_zero( XBatteryInformation );
    1.80 +    if ( XINPUTGETBATTERYINFORMATION )
    1.81 +    {
    1.82 +        result = XINPUTGETBATTERYINFORMATION( joystick->hwdata->userid, BATTERY_DEVTYPE_GAMEPAD, &XBatteryInformation );
    1.83 +    }
    1.84 +
    1.85      /* only fire events if the data changed from last time */
    1.86      if (XInputState.dwPacketNumber && XInputState.dwPacketNumber != joystick->hwdata->dwPacketNumber) {
    1.87          if (SDL_XInputUseOldJoystickMapping()) {
    1.88 -            UpdateXInputJoystickState_OLD(joystick, &XInputState);
    1.89 +            UpdateXInputJoystickState_OLD(joystick, &XInputState, &XBatteryInformation);
    1.90          } else {
    1.91 -            UpdateXInputJoystickState(joystick, &XInputState);
    1.92 +            UpdateXInputJoystickState(joystick, &XInputState, &XBatteryInformation);
    1.93          }
    1.94          joystick->hwdata->dwPacketNumber = XInputState.dwPacketNumber;
    1.95      }