mirror of
https://github.com/encounter/SDL.git
synced 2025-12-09 13:37:56 +00:00
SDL - added new SDL_JoystickCurrentPowerLevel() API that returns the battery level of the selected joystick. Currently only implemented for XInput devices, other platforms are a TODO.
CR: Sam
This commit is contained in:
@@ -178,6 +178,7 @@ SDL_JoystickOpen(int device_index)
|
||||
if (joystick->buttons) {
|
||||
SDL_memset(joystick->buttons, 0, joystick->nbuttons * sizeof(Uint8));
|
||||
}
|
||||
joystick->epowerlevel = SDL_JOYSTICK_POWER_UNKNOWN;
|
||||
|
||||
/* Add joystick to list */
|
||||
++joystick->ref_count;
|
||||
@@ -619,10 +620,10 @@ SDL_PrivateJoystickButton(SDL_Joystick * joystick, Uint8 button, Uint8 state)
|
||||
/* Make sure we're not getting garbage or duplicate events */
|
||||
if (button >= joystick->nbuttons) {
|
||||
return 0;
|
||||
}
|
||||
if (state == joystick->buttons[button]) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if (state == joystick->buttons[button]) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* We ignore events if we don't have keyboard focus, except for button
|
||||
* release. */
|
||||
@@ -825,4 +826,21 @@ SDL_JoystickGUID SDL_JoystickGetGUIDFromString(const char *pchGUID)
|
||||
}
|
||||
|
||||
|
||||
/* update the power level for this joystick */
|
||||
void SDL_PrivateJoystickBatteryLevel(SDL_Joystick * joystick, SDL_JoystickPowerLevel ePowerLevel)
|
||||
{
|
||||
joystick->epowerlevel = ePowerLevel;
|
||||
}
|
||||
|
||||
|
||||
/* return its power level */
|
||||
SDL_JoystickPowerLevel SDL_JoystickCurrentPowerLevel(SDL_Joystick * joystick)
|
||||
{
|
||||
if (!SDL_PrivateJoystickValid(joystick)) {
|
||||
return (SDL_JOYSTICK_POWER_UNKNOWN);
|
||||
}
|
||||
return joystick->epowerlevel;
|
||||
}
|
||||
|
||||
|
||||
/* vi: set ts=4 sw=4 expandtab: */
|
||||
|
||||
@@ -41,6 +41,8 @@ extern int SDL_PrivateJoystickHat(SDL_Joystick * joystick,
|
||||
Uint8 hat, Uint8 value);
|
||||
extern int SDL_PrivateJoystickButton(SDL_Joystick * joystick,
|
||||
Uint8 button, Uint8 state);
|
||||
extern void SDL_PrivateJoystickBatteryLevel( SDL_Joystick * joystick,
|
||||
SDL_JoystickPowerLevel ePowerLevel );
|
||||
|
||||
/* Internal sanity checking functions */
|
||||
extern int SDL_PrivateJoystickValid(SDL_Joystick * joystick);
|
||||
|
||||
@@ -54,6 +54,7 @@ struct _SDL_Joystick
|
||||
int ref_count; /* Reference count for multiple opens */
|
||||
|
||||
SDL_bool force_recentering; /* SDL_TRUE if this device needs to have its state reset to 0 */
|
||||
SDL_JoystickPowerLevel epowerlevel; /* power level of this joystick, SDL_JOYSTICK_POWER_UNKNOWN if not supported */
|
||||
struct _SDL_Joystick *next; /* pointer to next joystick we have allocated */
|
||||
};
|
||||
|
||||
|
||||
@@ -221,8 +221,39 @@ SDL_XINPUT_JoystickOpen(SDL_Joystick * joystick, JoyStick_DeviceData *joystickde
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
UpdateXInputJoystickBatteryInformation(SDL_Joystick * joystick, XINPUT_BATTERY_INFORMATION *pBatteryInformation)
|
||||
{
|
||||
if ( pBatteryInformation->BatteryType != BATTERY_TYPE_UNKNOWN )
|
||||
{
|
||||
SDL_JoystickPowerLevel ePowerLevel = SDL_JOYSTICK_POWER_UNKNOWN;
|
||||
if (pBatteryInformation->BatteryType == BATTERY_TYPE_WIRED) {
|
||||
ePowerLevel = SDL_JOYSTICK_POWER_WIRED;
|
||||
} else {
|
||||
switch ( pBatteryInformation->BatteryLevel )
|
||||
{
|
||||
case BATTERY_LEVEL_EMPTY:
|
||||
ePowerLevel = SDL_JOYSTICK_POWER_EMPTY;
|
||||
break;
|
||||
case BATTERY_LEVEL_LOW:
|
||||
ePowerLevel = SDL_JOYSTICK_POWER_LOW;
|
||||
break;
|
||||
case BATTERY_LEVEL_MEDIUM:
|
||||
ePowerLevel = SDL_JOYSTICK_POWER_MEDIUM;
|
||||
break;
|
||||
default:
|
||||
case BATTERY_LEVEL_FULL:
|
||||
ePowerLevel = SDL_JOYSTICK_POWER_FULL;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
SDL_PrivateJoystickBatteryLevel( joystick, ePowerLevel );
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
UpdateXInputJoystickState_OLD(SDL_Joystick * joystick, XINPUT_STATE_EX *pXInputState)
|
||||
UpdateXInputJoystickState_OLD(SDL_Joystick * joystick, XINPUT_STATE_EX *pXInputState, XINPUT_BATTERY_INFORMATION *pBatteryInformation)
|
||||
{
|
||||
static WORD s_XInputButtons[] = {
|
||||
XINPUT_GAMEPAD_DPAD_UP, XINPUT_GAMEPAD_DPAD_DOWN, XINPUT_GAMEPAD_DPAD_LEFT, XINPUT_GAMEPAD_DPAD_RIGHT,
|
||||
@@ -244,10 +275,12 @@ UpdateXInputJoystickState_OLD(SDL_Joystick * joystick, XINPUT_STATE_EX *pXInputS
|
||||
for (button = 0; button < SDL_arraysize(s_XInputButtons); ++button) {
|
||||
SDL_PrivateJoystickButton(joystick, button, (wButtons & s_XInputButtons[button]) ? SDL_PRESSED : SDL_RELEASED);
|
||||
}
|
||||
|
||||
UpdateXInputJoystickBatteryInformation( joystick, pBatteryInformation );
|
||||
}
|
||||
|
||||
static void
|
||||
UpdateXInputJoystickState(SDL_Joystick * joystick, XINPUT_STATE_EX *pXInputState)
|
||||
UpdateXInputJoystickState(SDL_Joystick * joystick, XINPUT_STATE_EX *pXInputState, XINPUT_BATTERY_INFORMATION *pBatteryInformation)
|
||||
{
|
||||
static WORD s_XInputButtons[] = {
|
||||
XINPUT_GAMEPAD_A, XINPUT_GAMEPAD_B, XINPUT_GAMEPAD_X, XINPUT_GAMEPAD_Y,
|
||||
@@ -283,6 +316,8 @@ UpdateXInputJoystickState(SDL_Joystick * joystick, XINPUT_STATE_EX *pXInputState
|
||||
hat |= SDL_HAT_RIGHT;
|
||||
}
|
||||
SDL_PrivateJoystickHat(joystick, 0, hat);
|
||||
|
||||
UpdateXInputJoystickBatteryInformation( joystick, pBatteryInformation );
|
||||
}
|
||||
|
||||
void
|
||||
@@ -290,6 +325,7 @@ SDL_XINPUT_JoystickUpdate(SDL_Joystick * joystick)
|
||||
{
|
||||
HRESULT result;
|
||||
XINPUT_STATE_EX XInputState;
|
||||
XINPUT_BATTERY_INFORMATION XBatteryInformation;
|
||||
|
||||
if (!XINPUTGETSTATE)
|
||||
return;
|
||||
@@ -301,12 +337,18 @@ SDL_XINPUT_JoystickUpdate(SDL_Joystick * joystick)
|
||||
return;
|
||||
}
|
||||
|
||||
SDL_zero( XBatteryInformation );
|
||||
if ( XINPUTGETBATTERYINFORMATION )
|
||||
{
|
||||
result = XINPUTGETBATTERYINFORMATION( joystick->hwdata->userid, BATTERY_DEVTYPE_GAMEPAD, &XBatteryInformation );
|
||||
}
|
||||
|
||||
/* only fire events if the data changed from last time */
|
||||
if (XInputState.dwPacketNumber && XInputState.dwPacketNumber != joystick->hwdata->dwPacketNumber) {
|
||||
if (SDL_XInputUseOldJoystickMapping()) {
|
||||
UpdateXInputJoystickState_OLD(joystick, &XInputState);
|
||||
UpdateXInputJoystickState_OLD(joystick, &XInputState, &XBatteryInformation);
|
||||
} else {
|
||||
UpdateXInputJoystickState(joystick, &XInputState);
|
||||
UpdateXInputJoystickState(joystick, &XInputState, &XBatteryInformation);
|
||||
}
|
||||
joystick->hwdata->dwPacketNumber = XInputState.dwPacketNumber;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user