Separate joystick power state into battery status and percentage

This allows you to see battery percentage while the controller is charging
This commit is contained in:
Sam Lantinga committed 2024-04-01 13:59:00 -07:00
1 parent 5e624c2083
commit 8847b35244
42 files changed
+596 -1001

No files matched your search

+4 -2
View File
@@ -232,6 +232,7 @@ SDL3_0.0.0 {
SDL_GetGamepadButtonFromString;
SDL_GetGamepadButtonLabel;
SDL_GetGamepadButtonLabelForType;
SDL_GetGamepadConnectionState;
SDL_GetGamepadFirmwareVersion;
SDL_GetGamepadFromInstanceID;
SDL_GetGamepadFromPlayerIndex;
@@ -252,7 +253,7 @@ SDL3_0.0.0 {
SDL_GetGamepadName;
SDL_GetGamepadPath;
SDL_GetGamepadPlayerIndex;
SDL_GetGamepadPowerLevel;
SDL_GetGamepadPowerInfo;
SDL_GetGamepadProduct;
SDL_GetGamepadProductVersion;
SDL_GetGamepadProperties;
@@ -287,6 +288,7 @@ SDL3_0.0.0 {
SDL_GetJoystickAxisInitialState;
SDL_GetJoystickBall;
SDL_GetJoystickButton;
SDL_GetJoystickConnectionState;
SDL_GetJoystickFirmwareVersion;
SDL_GetJoystickFromInstanceID;
SDL_GetJoystickFromPlayerIndex;
@@ -307,7 +309,7 @@ SDL3_0.0.0 {
SDL_GetJoystickName;
SDL_GetJoystickPath;
SDL_GetJoystickPlayerIndex;
SDL_GetJoystickPowerLevel;
SDL_GetJoystickPowerInfo;
SDL_GetJoystickProduct;
SDL_GetJoystickProductVersion;
SDL_GetJoystickProperties;
+4 -2
View File
@@ -257,6 +257,7 @@
#define SDL_GetGamepadButtonFromString SDL_GetGamepadButtonFromString_REAL
#define SDL_GetGamepadButtonLabel SDL_GetGamepadButtonLabel_REAL
#define SDL_GetGamepadButtonLabelForType SDL_GetGamepadButtonLabelForType_REAL
#define SDL_GetGamepadConnectionState SDL_GetGamepadConnectionState_REAL
#define SDL_GetGamepadFirmwareVersion SDL_GetGamepadFirmwareVersion_REAL
#define SDL_GetGamepadFromInstanceID SDL_GetGamepadFromInstanceID_REAL
#define SDL_GetGamepadFromPlayerIndex SDL_GetGamepadFromPlayerIndex_REAL
@@ -277,7 +278,7 @@
#define SDL_GetGamepadName SDL_GetGamepadName_REAL
#define SDL_GetGamepadPath SDL_GetGamepadPath_REAL
#define SDL_GetGamepadPlayerIndex SDL_GetGamepadPlayerIndex_REAL
#define SDL_GetGamepadPowerLevel SDL_GetGamepadPowerLevel_REAL
#define SDL_GetGamepadPowerInfo SDL_GetGamepadPowerInfo_REAL
#define SDL_GetGamepadProduct SDL_GetGamepadProduct_REAL
#define SDL_GetGamepadProductVersion SDL_GetGamepadProductVersion_REAL
#define SDL_GetGamepadProperties SDL_GetGamepadProperties_REAL
@@ -312,6 +313,7 @@
#define SDL_GetJoystickAxisInitialState SDL_GetJoystickAxisInitialState_REAL
#define SDL_GetJoystickBall SDL_GetJoystickBall_REAL
#define SDL_GetJoystickButton SDL_GetJoystickButton_REAL
#define SDL_GetJoystickConnectionState SDL_GetJoystickConnectionState_REAL
#define SDL_GetJoystickFirmwareVersion SDL_GetJoystickFirmwareVersion_REAL
#define SDL_GetJoystickFromInstanceID SDL_GetJoystickFromInstanceID_REAL
#define SDL_GetJoystickFromPlayerIndex SDL_GetJoystickFromPlayerIndex_REAL
@@ -332,7 +334,7 @@
#define SDL_GetJoystickName SDL_GetJoystickName_REAL
#define SDL_GetJoystickPath SDL_GetJoystickPath_REAL
#define SDL_GetJoystickPlayerIndex SDL_GetJoystickPlayerIndex_REAL
#define SDL_GetJoystickPowerLevel SDL_GetJoystickPowerLevel_REAL
#define SDL_GetJoystickPowerInfo SDL_GetJoystickPowerInfo_REAL
#define SDL_GetJoystickProduct SDL_GetJoystickProduct_REAL
#define SDL_GetJoystickProductVersion SDL_GetJoystickProductVersion_REAL
#define SDL_GetJoystickProperties SDL_GetJoystickProperties_REAL
+4 -2
View File
@@ -295,6 +295,7 @@ SDL_DYNAPI_PROC(Uint8,SDL_GetGamepadButton,(SDL_Gamepad *a, SDL_GamepadButton b)
SDL_DYNAPI_PROC(SDL_GamepadButton,SDL_GetGamepadButtonFromString,(const char *a),(a),return)
SDL_DYNAPI_PROC(SDL_GamepadButtonLabel,SDL_GetGamepadButtonLabel,(SDL_Gamepad *a, SDL_GamepadButton b),(a,b),return)
SDL_DYNAPI_PROC(SDL_GamepadButtonLabel,SDL_GetGamepadButtonLabelForType,(SDL_GamepadType a, SDL_GamepadButton b),(a,b),return)
SDL_DYNAPI_PROC(SDL_JoystickConnectionState,SDL_GetGamepadConnectionState,(SDL_Gamepad *a),(a),return)
SDL_DYNAPI_PROC(Uint16,SDL_GetGamepadFirmwareVersion,(SDL_Gamepad *a),(a),return)
SDL_DYNAPI_PROC(SDL_Gamepad*,SDL_GetGamepadFromInstanceID,(SDL_JoystickID a),(a),return)
SDL_DYNAPI_PROC(SDL_Gamepad*,SDL_GetGamepadFromPlayerIndex,(int a),(a),return)
@@ -315,7 +316,7 @@ SDL_DYNAPI_PROC(char**,SDL_GetGamepadMappings,(int *a),(a),return)
SDL_DYNAPI_PROC(const char*,SDL_GetGamepadName,(SDL_Gamepad *a),(a),return)
SDL_DYNAPI_PROC(const char*,SDL_GetGamepadPath,(SDL_Gamepad *a),(a),return)
SDL_DYNAPI_PROC(int,SDL_GetGamepadPlayerIndex,(SDL_Gamepad *a),(a),return)
SDL_DYNAPI_PROC(SDL_JoystickPowerLevel,SDL_GetGamepadPowerLevel,(SDL_Gamepad *a),(a),return)
SDL_DYNAPI_PROC(SDL_PowerState,SDL_GetGamepadPowerInfo,(SDL_Gamepad *a, int *b),(a,b),return)
SDL_DYNAPI_PROC(Uint16,SDL_GetGamepadProduct,(SDL_Gamepad *a),(a),return)
SDL_DYNAPI_PROC(Uint16,SDL_GetGamepadProductVersion,(SDL_Gamepad *a),(a),return)
SDL_DYNAPI_PROC(SDL_PropertiesID,SDL_GetGamepadProperties,(SDL_Gamepad *a),(a),return)
@@ -350,6 +351,7 @@ SDL_DYNAPI_PROC(Sint16,SDL_GetJoystickAxis,(SDL_Joystick *a, int b),(a,b),return
SDL_DYNAPI_PROC(SDL_bool,SDL_GetJoystickAxisInitialState,(SDL_Joystick *a, int b, Sint16 *c),(a,b,c),return)
SDL_DYNAPI_PROC(int,SDL_GetJoystickBall,(SDL_Joystick *a, int b, int *c, int *d),(a,b,c,d),return)
SDL_DYNAPI_PROC(Uint8,SDL_GetJoystickButton,(SDL_Joystick *a, int b),(a,b),return)
SDL_DYNAPI_PROC(SDL_JoystickConnectionState,SDL_GetJoystickConnectionState,(SDL_Joystick *a),(a),return)
SDL_DYNAPI_PROC(Uint16,SDL_GetJoystickFirmwareVersion,(SDL_Joystick *a),(a),return)
SDL_DYNAPI_PROC(SDL_Joystick*,SDL_GetJoystickFromInstanceID,(SDL_JoystickID a),(a),return)
SDL_DYNAPI_PROC(SDL_Joystick*,SDL_GetJoystickFromPlayerIndex,(int a),(a),return)
@@ -370,7 +372,7 @@ SDL_DYNAPI_PROC(Uint16,SDL_GetJoystickInstanceVendor,(SDL_JoystickID a),(a),retu
SDL_DYNAPI_PROC(const char*,SDL_GetJoystickName,(SDL_Joystick *a),(a),return)
SDL_DYNAPI_PROC(const char*,SDL_GetJoystickPath,(SDL_Joystick *a),(a),return)
SDL_DYNAPI_PROC(int,SDL_GetJoystickPlayerIndex,(SDL_Joystick *a),(a),return)
SDL_DYNAPI_PROC(SDL_JoystickPowerLevel,SDL_GetJoystickPowerLevel,(SDL_Joystick *a),(a),return)
SDL_DYNAPI_PROC(SDL_PowerState,SDL_GetJoystickPowerInfo,(SDL_Joystick *a, int *b),(a,b),return)
SDL_DYNAPI_PROC(Uint16,SDL_GetJoystickProduct,(SDL_Joystick *a),(a),return)
SDL_DYNAPI_PROC(Uint16,SDL_GetJoystickProductVersion,(SDL_Joystick *a),(a),return)
SDL_DYNAPI_PROC(SDL_PropertiesID,SDL_GetJoystickProperties,(SDL_Joystick *a),(a),return)
+16 -3
View File
@@ -3427,14 +3427,27 @@ Uint64 SDL_GetGamepadSteamHandle(SDL_Gamepad *gamepad)
return handle;
}
SDL_JoystickPowerLevel SDL_GetGamepadPowerLevel(SDL_Gamepad *gamepad)
SDL_JoystickConnectionState SDL_GetGamepadConnectionState(SDL_Gamepad *gamepad)
{
SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
if (!joystick) {
return SDL_JOYSTICK_POWER_UNKNOWN;
return SDL_JOYSTICK_CONNECTION_INVALID;
}
return SDL_GetJoystickPowerLevel(joystick);
return SDL_GetJoystickConnectionState(joystick);
}
SDL_PowerState SDL_GetGamepadPowerInfo(SDL_Gamepad *gamepad, int *percent)
{
SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
if (percent) {
*percent = -1;
}
if (!joystick) {
return SDL_POWERSTATE_ERROR;
}
return SDL_GetJoystickPowerInfo(joystick, percent);
}
/*
+35 -18
View File
@@ -1054,7 +1054,6 @@ SDL_Joystick *SDL_OpenJoystick(SDL_JoystickID instance_id)
SDL_Joystick *joysticklist;
const char *joystickname = NULL;
const char *joystickpath = NULL;
SDL_JoystickPowerLevel initial_power_level;
SDL_bool invert_sensors = SDL_FALSE;
const SDL_SteamVirtualGamepadInfo *info;
@@ -1089,8 +1088,8 @@ SDL_Joystick *SDL_OpenJoystick(SDL_JoystickID instance_id)
joystick->driver = driver;
joystick->instance_id = instance_id;
joystick->attached = SDL_TRUE;
joystick->epowerlevel = SDL_JOYSTICK_POWER_UNKNOWN;
joystick->led_expiration = SDL_GetTicks();
joystick->battery_percent = -1;
if (driver->Open(joystick, device_index) < 0) {
SDL_free(joystick);
@@ -1159,11 +1158,6 @@ SDL_Joystick *SDL_OpenJoystick(SDL_JoystickID instance_id)
joystick->next = SDL_joysticks;
SDL_joysticks = joystick;
/* send initial battery event */
initial_power_level = joystick->epowerlevel;
joystick->epowerlevel = SDL_JOYSTICK_POWER_UNKNOWN;
SDL_SendJoystickBatteryLevel(joystick, initial_power_level);
driver->Update(joystick);
SDL_UnlockJoysticks();
@@ -3410,35 +3404,58 @@ SDL_JoystickGUID SDL_GetJoystickGUIDFromString(const char *pchGUID)
return SDL_GUIDFromString(pchGUID);
}
/* update the power level for this joystick */
void SDL_SendJoystickBatteryLevel(SDL_Joystick *joystick, SDL_JoystickPowerLevel ePowerLevel)
void SDL_SendJoystickPowerInfo(SDL_Joystick *joystick, SDL_PowerState state, int percent)
{
SDL_AssertJoysticksLocked();
SDL_assert(joystick->ref_count); /* make sure we are calling this only for update, not for initialization */
if (ePowerLevel != joystick->epowerlevel) {
if (state != joystick->battery_state || percent != joystick->battery_percent) {
joystick->battery_state = state;
joystick->battery_percent = percent;
if (SDL_EventEnabled(SDL_EVENT_JOYSTICK_BATTERY_UPDATED)) {
SDL_Event event;
event.type = SDL_EVENT_JOYSTICK_BATTERY_UPDATED;
event.common.timestamp = 0;
event.jbattery.which = joystick->instance_id;
event.jbattery.level = ePowerLevel;
event.jbattery.state = state;
event.jbattery.percent = percent;
SDL_PushEvent(&event);
}
joystick->epowerlevel = ePowerLevel;
}
}
/* return its power level */
SDL_JoystickPowerLevel SDL_GetJoystickPowerLevel(SDL_Joystick *joystick)
SDL_JoystickConnectionState SDL_GetJoystickConnectionState(SDL_Joystick *joystick)
{
SDL_JoystickPowerLevel retval;
SDL_JoystickConnectionState retval;
SDL_LockJoysticks();
{
CHECK_JOYSTICK_MAGIC(joystick, SDL_JOYSTICK_POWER_UNKNOWN);
CHECK_JOYSTICK_MAGIC(joystick, SDL_JOYSTICK_CONNECTION_INVALID);
retval = joystick->epowerlevel;
retval = joystick->connection_state;
}
SDL_UnlockJoysticks();
return retval;
}
SDL_PowerState SDL_GetJoystickPowerInfo(SDL_Joystick *joystick, int *percent)
{
SDL_PowerState retval;
if (percent) {
*percent = -1;
}
SDL_LockJoysticks();
{
CHECK_JOYSTICK_MAGIC(joystick, SDL_POWERSTATE_ERROR);
retval = joystick->battery_state;
if (percent) {
*percent = joystick->battery_percent;
}
}
SDL_UnlockJoysticks();
+1 -1
View File
@@ -167,7 +167,7 @@ extern int SDL_SendJoystickHat(Uint64 timestamp, SDL_Joystick *joystick, Uint8 h
extern int SDL_SendJoystickButton(Uint64 timestamp, SDL_Joystick *joystick, Uint8 button, Uint8 state);
extern int SDL_SendJoystickTouchpad(Uint64 timestamp, SDL_Joystick *joystick, int touchpad, int finger, Uint8 state, float x, float y, float pressure);
extern int SDL_SendJoystickSensor(Uint64 timestamp, SDL_Joystick *joystick, SDL_SensorType type, Uint64 sensor_timestamp, const float *data, int num_values);
extern void SDL_SendJoystickBatteryLevel(SDL_Joystick *joystick, SDL_JoystickPowerLevel ePowerLevel);
extern void SDL_SendJoystickPowerInfo(SDL_Joystick *joystick, SDL_PowerState state, int percent);
/* Function to get the Steam virtual gamepad info for a joystick */
extern const struct SDL_SteamVirtualGamepadInfo *SDL_GetJoystickInstanceVirtualGamepadInfo(SDL_JoystickID instance_id);
+4 -1
View File
@@ -121,8 +121,11 @@ struct SDL_Joystick
SDL_bool attached _guarded;
SDL_bool is_gamepad _guarded;
SDL_JoystickConnectionState connection_state _guarded;
SDL_PowerState battery_state _guarded;
int battery_percent _guarded;
SDL_bool delayed_guide_button _guarded; /* SDL_TRUE if this device has the guide button event delayed */
SDL_JoystickPowerLevel epowerlevel _guarded; /* power level of this joystick, SDL_JOYSTICK_POWER_UNKNOWN if not supported */
SDL_SensorID accel_sensor _guarded;
SDL_Sensor *accel _guarded;
+7 -16
View File
@@ -1296,33 +1296,24 @@ static void IOS_MFIJoystickUpdate(SDL_Joystick *joystick)
if (@available(macOS 10.16, iOS 14.0, tvOS 14.0, *)) {
GCDeviceBattery *battery = controller.battery;
if (battery) {
SDL_JoystickPowerLevel ePowerLevel = SDL_JOYSTICK_POWER_UNKNOWN;
SDL_PowerState state = SDL_POWERSTATE_UNKNOWN;
int percent = (int)SDL_roundf(battery.batteryLevel * 100.0f);
switch (battery.batteryState) {
case GCDeviceBatteryStateDischarging:
{
float power_level = battery.batteryLevel;
if (power_level <= 0.05f) {
ePowerLevel = SDL_JOYSTICK_POWER_EMPTY;
} else if (power_level <= 0.20f) {
ePowerLevel = SDL_JOYSTICK_POWER_LOW;
} else if (power_level <= 0.70f) {
ePowerLevel = SDL_JOYSTICK_POWER_MEDIUM;
} else {
ePowerLevel = SDL_JOYSTICK_POWER_FULL;
}
} break;
state = SDL_POWERSTATE_ON_BATTERY;
break;
case GCDeviceBatteryStateCharging:
ePowerLevel = SDL_JOYSTICK_POWER_WIRED;
state = SDL_POWERSTATE_CHARGING;
break;
case GCDeviceBatteryStateFull:
ePowerLevel = SDL_JOYSTICK_POWER_FULL;
state = SDL_POWERSTATE_CHARGED;
break;
default:
break;
}
SDL_SendJoystickBatteryLevel(joystick, ePowerLevel);
SDL_SendJoystickPowerInfo(joystick, state, percent);
}
}
#endif /* ENABLE_MFI_BATTERY */
+27 -10
View File
@@ -341,17 +341,36 @@ static SDL_JoystickID GAMEINPUT_JoystickGetDeviceInstanceID(int device_index)
return GAMEINPUT_InternalFindByIndex(device_index)->device_instance;
}
static SDL_JoystickPowerLevel GAMEINPUT_InternalGetPowerLevel(IGameInputDevice *device)
static void GAMEINPUT_UpdatePowerInfo(SDL_Joystick *joystick, IGameInputDevice *device)
{
GameInputBatteryState battery_state;
SDL_PowerState state;
int percent = 0;
SDL_zero(battery_state);
IGameInputDevice_GetBatteryState(device, &battery_state);
if (battery_state.status == GameInputBatteryDischarging) {
/* FIXME: What are the units for remainingCapacity? */
switch (battery_state.status) {
case GameInputBatteryNotPresent:
state = SDL_POWERSTATE_NO_BATTERY;
break;
case GameInputBatteryDischarging:
state = SDL_POWERSTATE_ON_BATTERY;
break;
case GameInputBatteryIdle:
state = SDL_POWERSTATE_CHARGED;
break;
case GameInputBatteryCharging:
state = SDL_POWERSTATE_CHARGING;
break;
default:
state = SDL_POWERSTATE_UNKNOWN;
break;
}
return SDL_JOYSTICK_POWER_UNKNOWN;
if (battery_state.fullChargeCapacity > 0.0f) {
percent = (int)SDL_roundf((battery_state.remainingCapacity / battery_state.fullChargeCapacity) * 100.0f);
}
SDL_SendJoystickPowerInfo(joystick, state, percent);
}
#if 0
@@ -417,9 +436,9 @@ static int GAMEINPUT_JoystickOpen(SDL_Joystick *joystick, int device_index)
}
if (info->capabilities & GameInputDeviceCapabilityWireless) {
joystick->epowerlevel = GAMEINPUT_InternalGetPowerLevel(elem->device);
joystick->connection_state = SDL_JOYSTICK_CONNECTION_WIRELESS;
} else {
joystick->epowerlevel = SDL_JOYSTICK_POWER_WIRED;
joystick->connection_state = SDL_JOYSTICK_CONNECTION_WIRED;
}
return 0;
}
@@ -580,10 +599,8 @@ static void GAMEINPUT_JoystickUpdate(SDL_Joystick *joystick)
IGameInputReading_Release(reading);
if (joystick->epowerlevel != SDL_JOYSTICK_POWER_WIRED) {
/* FIXME: We can poll this at a much lower rate */
SDL_SendJoystickBatteryLevel(joystick, GAMEINPUT_InternalGetPowerLevel(device));
}
/* FIXME: We can poll this at a much lower rate */
GAMEINPUT_UpdatePowerInfo(joystick, device);
}
static void GAMEINPUT_JoystickClose(SDL_Joystick* joystick)
+5 -1
View File
@@ -392,7 +392,11 @@ static SDL_bool HIDAPI_DriverGameCube_OpenJoystick(SDL_HIDAPI_Device *device, SD
if (joystick->instance_id == ctx->joysticks[i]) {
joystick->nbuttons = 12;
joystick->naxes = SDL_GAMEPAD_AXIS_MAX;
joystick->epowerlevel = ctx->wireless[i] ? SDL_JOYSTICK_POWER_UNKNOWN : SDL_JOYSTICK_POWER_WIRED;
if (ctx->wireless[i]) {
joystick->connection_state = SDL_JOYSTICK_CONNECTION_WIRELESS;
} else {
joystick->connection_state = SDL_JOYSTICK_CONNECTION_WIRED;
}
return SDL_TRUE;
}
}
+2 -12
View File
@@ -103,7 +103,6 @@ static SDL_bool HIDAPI_DriverLuna_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Jo
joystick->nbuttons = SDL_GAMEPAD_NUM_LUNA_BUTTONS;
joystick->naxes = SDL_GAMEPAD_AXIS_MAX;
joystick->nhats = 1;
joystick->epowerlevel = SDL_JOYSTICK_POWER_FULL;
return SDL_TRUE;
}
@@ -259,17 +258,8 @@ static void HIDAPI_DriverLuna_HandleBluetoothStatePacket(SDL_Joystick *joystick,
if (size >= 2 && data[0] == 0x04) {
/* Battery level report */
int level = data[1] * 100 / 0xFF;
if (level == 0) {
SDL_SendJoystickBatteryLevel(joystick, SDL_JOYSTICK_POWER_EMPTY);
} else if (level <= 20) {
SDL_SendJoystickBatteryLevel(joystick, SDL_JOYSTICK_POWER_LOW);
} else if (level <= 70) {
SDL_SendJoystickBatteryLevel(joystick, SDL_JOYSTICK_POWER_MEDIUM);
} else {
SDL_SendJoystickBatteryLevel(joystick, SDL_JOYSTICK_POWER_FULL);
}
int percent = (int)SDL_roundf((data[1] / 255.0f) * 100.0f);
SDL_SendJoystickPowerInfo(joystick, SDL_POWERSTATE_ON_BATTERY, percent);
return;
}
-3
View File
@@ -279,7 +279,6 @@ static SDL_bool HIDAPI_DriverPS3_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joy
joystick->nbuttons = 11;
joystick->naxes = 16;
joystick->nhats = 1;
joystick->epowerlevel = SDL_JOYSTICK_POWER_WIRED;
SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_ACCEL, 100.0f);
@@ -682,7 +681,6 @@ static SDL_bool HIDAPI_DriverPS3ThirdParty_OpenJoystick(SDL_HIDAPI_Device *devic
joystick->nbuttons = 11;
joystick->naxes = 16;
joystick->nhats = 1;
joystick->epowerlevel = SDL_JOYSTICK_POWER_WIRED;
return SDL_TRUE;
}
@@ -1127,7 +1125,6 @@ static SDL_bool HIDAPI_DriverPS3SonySixaxis_OpenJoystick(SDL_HIDAPI_Device *devi
joystick->nbuttons = 11;
joystick->naxes = 16;
joystick->nhats = 1;
joystick->epowerlevel = SDL_JOYSTICK_POWER_WIRED;
SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_ACCEL, 100.0f);
+19 -16
View File
@@ -720,7 +720,7 @@ static void HIDAPI_DriverPS4_SetEnhancedModeAvailable(SDL_DriverPS4_Context *ctx
SDL_PrivateJoystickAddSensor(ctx->joystick, SDL_SENSOR_ACCEL, 250.0f);
}
if (ctx->device->is_bluetooth && ctx->official_controller) {
if (ctx->official_controller) {
ctx->report_battery = SDL_TRUE;
}
@@ -833,12 +833,6 @@ static SDL_bool HIDAPI_DriverPS4_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joy
}
joystick->naxes = SDL_GAMEPAD_AXIS_MAX;
joystick->nhats = 1;
if (device->is_bluetooth) {
/* We'll update this once we're in enhanced mode */
joystick->epowerlevel = SDL_JOYSTICK_POWER_UNKNOWN;
} else {
joystick->epowerlevel = SDL_JOYSTICK_POWER_WIRED;
}
SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_PS4_RUMBLE,
SDL_PS4RumbleHintChanged, ctx);
@@ -1073,17 +1067,26 @@ static void HIDAPI_DriverPS4_HandleStatePacket(SDL_Joystick *joystick, SDL_hid_d
SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, axis);
if (size > 9 && ctx->report_battery && ctx->enhanced_reports) {
/* Battery level ranges from 0 to 10 */
int level = (packet->ucBatteryLevel & 0xF);
if (level == 0) {
SDL_SendJoystickBatteryLevel(joystick, SDL_JOYSTICK_POWER_EMPTY);
} else if (level <= 2) {
SDL_SendJoystickBatteryLevel(joystick, SDL_JOYSTICK_POWER_LOW);
} else if (level <= 7) {
SDL_SendJoystickBatteryLevel(joystick, SDL_JOYSTICK_POWER_MEDIUM);
SDL_PowerState state;
int percent;
Uint8 level = (packet->ucBatteryLevel & 0x0F);
if (packet->ucBatteryLevel & 0x10) {
if (level <= 10) {
state = SDL_POWERSTATE_CHARGING;
percent = SDL_min(level * 10 + 5, 100);
} else if (level == 11) {
state = SDL_POWERSTATE_CHARGED;
percent = 100;
} else {
state = SDL_POWERSTATE_UNKNOWN;
percent = 0;
}
} else {
SDL_SendJoystickBatteryLevel(joystick, SDL_JOYSTICK_POWER_FULL);
state = SDL_POWERSTATE_ON_BATTERY;
percent = SDL_min(level * 10 + 5, 100);
}
SDL_SendJoystickPowerInfo(joystick, state, percent);
}
if (size > 9 && ctx->report_touchpad && ctx->enhanced_reports) {
+24 -19
View File
@@ -815,9 +815,7 @@ static void HIDAPI_DriverPS5_SetEnhancedModeAvailable(SDL_DriverPS5_Context *ctx
}
}
if (ctx->device->is_bluetooth) {
ctx->report_battery = SDL_TRUE;
}
ctx->report_battery = SDL_TRUE;
HIDAPI_UpdateDeviceProperties(ctx->device);
}
@@ -956,12 +954,6 @@ static SDL_bool HIDAPI_DriverPS5_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joy
}
joystick->naxes = SDL_GAMEPAD_AXIS_MAX;
joystick->nhats = 1;
if (device->is_bluetooth) {
/* We'll update this once we're in enhanced mode */
joystick->epowerlevel = SDL_JOYSTICK_POWER_UNKNOWN;
} else {
joystick->epowerlevel = SDL_JOYSTICK_POWER_WIRED;
}
joystick->firmware_version = ctx->firmware_version;
SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_PS5_RUMBLE,
@@ -1387,17 +1379,30 @@ static void HIDAPI_DriverPS5_HandleStatePacket(SDL_Joystick *joystick, SDL_hid_d
}
if (ctx->report_battery) {
/* Battery level ranges from 0 to 10 */
int level = (packet->ucBatteryLevel & 0xF);
if (level == 0) {
SDL_SendJoystickBatteryLevel(joystick, SDL_JOYSTICK_POWER_EMPTY);
} else if (level <= 2) {
SDL_SendJoystickBatteryLevel(joystick, SDL_JOYSTICK_POWER_LOW);
} else if (level <= 7) {
SDL_SendJoystickBatteryLevel(joystick, SDL_JOYSTICK_POWER_MEDIUM);
} else {
SDL_SendJoystickBatteryLevel(joystick, SDL_JOYSTICK_POWER_FULL);
SDL_PowerState state;
int percent;
Uint8 status = (packet->ucBatteryLevel >> 4) & 0x0F;
Uint8 level = (packet->ucBatteryLevel & 0x0F);
switch (status) {
case 0:
state = SDL_POWERSTATE_ON_BATTERY;
percent = SDL_min(level * 10 + 5, 100);
break;
case 1:
state = SDL_POWERSTATE_CHARGING;
percent = SDL_min(level * 10 + 5, 100);
break;
case 2:
state = SDL_POWERSTATE_CHARGED;
percent = 100;
break;
default:
state = SDL_POWERSTATE_UNKNOWN;
percent = 0;
break;
}
SDL_SendJoystickPowerInfo(joystick, state, percent);
}
SDL_memcpy(&ctx->last_state, packet, sizeof(ctx->last_state));
+21 -30
View File
@@ -77,8 +77,10 @@ typedef struct
{
Uint8 seq_num;
SDL_JoystickPowerLevel battery_level;
SDL_bool charging;
SDL_bool has_charging;
Uint8 charging;
SDL_bool has_battery_level;
Uint8 battery_level;
Uint64 last_battery_query_time;
SDL_bool rumble_report_pending;
@@ -184,14 +186,12 @@ static SDL_bool HIDAPI_DriverShield_OpenJoystick(SDL_HIDAPI_Device *device, SDL_
joystick->nbuttons = SDL_GAMEPAD_NUM_SHIELD_V103_BUTTONS;
joystick->naxes = SDL_GAMEPAD_AXIS_MAX;
joystick->nhats = 1;
joystick->epowerlevel = SDL_JOYSTICK_POWER_WIRED;
SDL_PrivateJoystickAddTouchpad(joystick, 1);
} else {
joystick->nbuttons = SDL_GAMEPAD_NUM_SHIELD_V104_BUTTONS;
joystick->naxes = SDL_GAMEPAD_AXIS_MAX;
joystick->nhats = 1;
joystick->epowerlevel = SDL_JOYSTICK_POWER_UNKNOWN;
}
/* Request battery and charging info */
@@ -451,6 +451,17 @@ static void HIDAPI_DriverShield_HandleStatePacketV104(SDL_Joystick *joystick, SD
SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
}
static void HIDAPI_DriverShield_UpdatePowerInfo(SDL_Joystick *joystick, SDL_DriverShield_Context *ctx)
{
if (!ctx->has_charging || !ctx->has_battery_level) {
return;
}
SDL_PowerState state = ctx->charging ? SDL_POWERSTATE_CHARGING : SDL_POWERSTATE_ON_BATTERY;
int percent = ctx->battery_level * 20;
SDL_SendJoystickPowerInfo(joystick, state, percent);
}
static SDL_bool HIDAPI_DriverShield_UpdateDevice(SDL_HIDAPI_Device *device)
{
SDL_DriverShield_Context *ctx = (SDL_DriverShield_Context *)device->context;
@@ -496,34 +507,14 @@ static SDL_bool HIDAPI_DriverShield_UpdateDevice(SDL_HIDAPI_Device *device)
HIDAPI_DriverShield_SendNextRumble(device);
break;
case CMD_CHARGE_STATE:
ctx->charging = cmd_resp_report->payload[0] != 0;
if (joystick) {
SDL_SendJoystickBatteryLevel(joystick, ctx->charging ? SDL_JOYSTICK_POWER_WIRED : ctx->battery_level);
}
ctx->has_charging = SDL_TRUE;
ctx->charging = cmd_resp_report->payload[0];
HIDAPI_DriverShield_UpdatePowerInfo(joystick, ctx);
break;
case CMD_BATTERY_STATE:
switch (cmd_resp_report->payload[2]) {
case 0:
ctx->battery_level = SDL_JOYSTICK_POWER_EMPTY;
break;
case 1:
ctx->battery_level = SDL_JOYSTICK_POWER_LOW;
break;
case 2: /* 40% */
case 3: /* 60% */
case 4: /* 80% */
ctx->battery_level = SDL_JOYSTICK_POWER_MEDIUM;
break;
case 5:
ctx->battery_level = SDL_JOYSTICK_POWER_FULL;
break;
default:
ctx->battery_level = SDL_JOYSTICK_POWER_UNKNOWN;
break;
}
if (joystick) {
SDL_SendJoystickBatteryLevel(joystick, ctx->charging ? SDL_JOYSTICK_POWER_WIRED : ctx->battery_level);
}
ctx->has_battery_level = SDL_TRUE;
ctx->battery_level = cmd_resp_report->payload[2];
HIDAPI_DriverShield_UpdatePowerInfo(joystick, ctx);
break;
}
break;
-1
View File
@@ -109,7 +109,6 @@ static SDL_bool HIDAPI_DriverStadia_OpenJoystick(SDL_HIDAPI_Device *device, SDL_
joystick->nbuttons = SDL_GAMEPAD_NUM_STADIA_BUTTONS;
joystick->naxes = SDL_GAMEPAD_AXIS_MAX;
joystick->nhats = 1;
joystick->epowerlevel = SDL_JOYSTICK_POWER_WIRED;
return SDL_TRUE;
}
+25 -22
View File
@@ -1447,7 +1447,6 @@ static SDL_bool HIDAPI_DriverSwitch_OpenJoystick(SDL_HIDAPI_Device *device, SDL_
joystick->nbuttons = SDL_GAMEPAD_NUM_SWITCH_BUTTONS;
joystick->naxes = SDL_GAMEPAD_AXIS_MAX;
joystick->nhats = 1;
joystick->epowerlevel = device->is_bluetooth ? SDL_JOYSTICK_POWER_UNKNOWN : SDL_JOYSTICK_POWER_WIRED;
/* Set up for input */
ctx->m_bSyncWrite = SDL_FALSE;
@@ -2095,29 +2094,33 @@ static void HandleFullControllerState(SDL_Joystick *joystick, SDL_DriverSwitch_C
SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, ~axis);
}
if (ctx->device->is_bluetooth) {
/* High nibble of battery/connection byte is battery level, low nibble is connection status
* LSB of connection nibble is USB/Switch connection status
*/
if (packet->controllerState.ucBatteryAndConnection & 0x1) {
SDL_SendJoystickBatteryLevel(joystick, SDL_JOYSTICK_POWER_WIRED);
} else {
/* LSB of the battery nibble is used to report charging.
* The battery level is reported from 0(empty)-8(full)
*/
int level = (packet->controllerState.ucBatteryAndConnection & 0xE0) >> 4;
if (level == 0) {
SDL_SendJoystickBatteryLevel(joystick, SDL_JOYSTICK_POWER_EMPTY);
} else if (level <= 2) {
SDL_SendJoystickBatteryLevel(joystick, SDL_JOYSTICK_POWER_LOW);
} else if (level <= 6) {
SDL_SendJoystickBatteryLevel(joystick, SDL_JOYSTICK_POWER_MEDIUM);
} else {
SDL_SendJoystickBatteryLevel(joystick, SDL_JOYSTICK_POWER_FULL);
}
}
/* High nibble of battery/connection byte is battery level, low nibble is connection status
* LSB of connection nibble is USB/Switch connection status
* LSB of the battery nibble is used to report charging.
* The battery level is reported from 0(empty)-8(full)
*/
SDL_PowerState state;
int charging = (packet->controllerState.ucBatteryAndConnection & 0x10);
int level = (packet->controllerState.ucBatteryAndConnection & 0xE0) >> 4;
int percent = (int)SDL_roundf((level / 8.0f) * 100.0f);
if (packet->controllerState.ucBatteryAndConnection & 0x01) {
joystick->connection_state = SDL_JOYSTICK_CONNECTION_WIRED;
} else {
joystick->connection_state = SDL_JOYSTICK_CONNECTION_WIRELESS;
}
if (charging) {
if (level == 8) {
state = SDL_POWERSTATE_CHARGED;
} else {
state = SDL_POWERSTATE_CHARGING;
}
} else {
state = SDL_POWERSTATE_ON_BATTERY;
}
SDL_SendJoystickPowerInfo(joystick, state, percent);
if (ctx->m_bReportSensors) {
SDL_bool bHasSensorData = (packet->imuState[0].sAccelZ != 0 ||
packet->imuState[0].sAccelY != 0 ||
+31 -13
View File
@@ -493,15 +493,17 @@ static void DeactivateMotionPlus(SDL_DriverWii_Context *ctx)
static void UpdatePowerLevelWii(SDL_Joystick *joystick, Uint8 batteryLevelByte)
{
int percent;
if (batteryLevelByte > 178) {
SDL_SendJoystickBatteryLevel(joystick, SDL_JOYSTICK_POWER_FULL);
percent = 100;
} else if (batteryLevelByte > 51) {
SDL_SendJoystickBatteryLevel(joystick, SDL_JOYSTICK_POWER_MEDIUM);
percent = 70;
} else if (batteryLevelByte > 13) {
SDL_SendJoystickBatteryLevel(joystick, SDL_JOYSTICK_POWER_LOW);
percent = 20;
} else {
SDL_SendJoystickBatteryLevel(joystick, SDL_JOYSTICK_POWER_EMPTY);
percent = 5;
}
SDL_SendJoystickPowerInfo(joystick, SDL_POWERSTATE_ON_BATTERY, percent);
}
static void UpdatePowerLevelWiiU(SDL_Joystick *joystick, Uint8 extensionBatteryByte)
@@ -510,23 +512,39 @@ static void UpdatePowerLevelWiiU(SDL_Joystick *joystick, Uint8 extensionBatteryB
SDL_bool pluggedIn = !(extensionBatteryByte & 0x04);
Uint8 batteryLevel = extensionBatteryByte >> 4;
if (pluggedIn) {
joystick->connection_state = SDL_JOYSTICK_CONNECTION_WIRED;
} else {
joystick->connection_state = SDL_JOYSTICK_CONNECTION_WIRELESS;
}
/* Not sure if all Wii U Pro controllers act like this, but on mine
* 4, 3, and 2 are held for about 20 hours each
* 1 is held for about 6 hours
* 0 is held for about 2 hours
* No value above 4 has been observed.
*/
if (pluggedIn && !charging) {
SDL_SendJoystickBatteryLevel(joystick, SDL_JOYSTICK_POWER_WIRED);
} else if (batteryLevel >= 4) {
SDL_SendJoystickBatteryLevel(joystick, SDL_JOYSTICK_POWER_FULL);
} else if (batteryLevel > 1) {
SDL_SendJoystickBatteryLevel(joystick, SDL_JOYSTICK_POWER_MEDIUM);
} else if (batteryLevel == 1) {
SDL_SendJoystickBatteryLevel(joystick, SDL_JOYSTICK_POWER_LOW);
SDL_PowerState state;
int percent;
if (charging) {
state = SDL_POWERSTATE_CHARGING;
} else if (pluggedIn) {
state = SDL_POWERSTATE_CHARGED;
} else {
SDL_SendJoystickBatteryLevel(joystick, SDL_JOYSTICK_POWER_EMPTY);
state = SDL_POWERSTATE_ON_BATTERY;
}
if (batteryLevel >= 4) {
percent = 100;
} else if (batteryLevel == 3) {
percent = 70;
} else if (batteryLevel == 2) {
percent = 40;
} else if (batteryLevel == 1) {
percent = 10;
} else {
percent = 3;
}
SDL_SendJoystickPowerInfo(joystick, state, percent);
}
static EWiiInputReportIDs GetButtonPacketType(SDL_DriverWii_Context *ctx)
-1
View File
@@ -191,7 +191,6 @@ static SDL_bool HIDAPI_DriverXbox360_OpenJoystick(SDL_HIDAPI_Device *device, SDL
joystick->nbuttons = 11;
joystick->naxes = SDL_GAMEPAD_AXIS_MAX;
joystick->nhats = 1;
joystick->epowerlevel = SDL_JOYSTICK_POWER_WIRED;
return SDL_TRUE;
}
+2 -12
View File
@@ -109,17 +109,8 @@ static void SDLCALL SDL_PlayerLEDHintChanged(void *userdata, const char *name, c
static void UpdatePowerLevel(SDL_Joystick *joystick, Uint8 level)
{
float normalized_level = (float)level / 255.0f;
if (normalized_level <= 0.05f) {
SDL_SendJoystickBatteryLevel(joystick, SDL_JOYSTICK_POWER_EMPTY);
} else if (normalized_level <= 0.20f) {
SDL_SendJoystickBatteryLevel(joystick, SDL_JOYSTICK_POWER_LOW);
} else if (normalized_level <= 0.70f) {
SDL_SendJoystickBatteryLevel(joystick, SDL_JOYSTICK_POWER_MEDIUM);
} else {
SDL_SendJoystickBatteryLevel(joystick, SDL_JOYSTICK_POWER_FULL);
}
int percent = (int)SDL_roundf((level / 255.0f) * 100.0f);
SDL_SendJoystickPowerInfo(joystick, SDL_POWERSTATE_ON_BATTERY, percent);
}
static SDL_bool HIDAPI_DriverXbox360W_InitDevice(SDL_HIDAPI_Device *device)
@@ -186,7 +177,6 @@ static SDL_bool HIDAPI_DriverXbox360W_OpenJoystick(SDL_HIDAPI_Device *device, SD
/* Initialize the joystick capabilities */
joystick->nbuttons = 15;
joystick->naxes = SDL_GAMEPAD_AXIS_MAX;
joystick->epowerlevel = SDL_JOYSTICK_POWER_UNKNOWN;
return SDL_TRUE;
}
+23 -19
View File
@@ -432,10 +432,6 @@ static SDL_bool HIDAPI_DriverXboxOne_OpenJoystick(SDL_HIDAPI_Device *device, SDL
joystick->naxes = SDL_GAMEPAD_AXIS_MAX;
joystick->nhats = 1;
if (!device->is_bluetooth) {
joystick->epowerlevel = SDL_JOYSTICK_POWER_WIRED;
}
SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_XBOX_ONE_HOME_LED,
SDL_HomeLEDHintChanged, ctx);
return SDL_TRUE;
@@ -1068,23 +1064,31 @@ static void HIDAPI_DriverXboxOneBluetooth_HandleBatteryPacket(SDL_Joystick *joys
{
Uint8 flags = data[1];
SDL_bool on_usb = (((flags & 0x0C) >> 2) == 0);
SDL_PowerState state;
int percent;
if (on_usb) {
/* Does this ever happen? */
SDL_SendJoystickBatteryLevel(joystick, SDL_JOYSTICK_POWER_WIRED);
} else {
switch ((flags & 0x03)) {
case 0:
SDL_SendJoystickBatteryLevel(joystick, SDL_JOYSTICK_POWER_LOW);
break;
case 1:
SDL_SendJoystickBatteryLevel(joystick, SDL_JOYSTICK_POWER_MEDIUM);
break;
default: /* 2, 3 */
SDL_SendJoystickBatteryLevel(joystick, SDL_JOYSTICK_POWER_FULL);
break;
}
// Mapped percentage value from:
// https://learn.microsoft.com/en-us/gaming/gdk/_content/gc/reference/input/gameinput/interfaces/igameinputdevice/methods/igameinputdevice_getbatterystate
switch (flags & 0x03) {
case 0:
percent = 10;
break;
case 1:
percent = 40;
break;
case 2:
percent = 70;
break;
case 3:
percent = 100;
break;
}
if (on_usb) {
state = SDL_POWERSTATE_CHARGING;
} else {
state = SDL_POWERSTATE_ON_BATTERY;
}
SDL_SendJoystickPowerInfo(joystick, state, percent);
}
static void HIDAPI_DriverXboxOne_HandleSerialIDPacket(SDL_DriverXboxOne_Context *ctx, const Uint8 *data, int size)
+7
View File
@@ -1517,6 +1517,13 @@ static int HIDAPI_JoystickOpen(SDL_Joystick *joystick, int device_index)
return SDL_SetError("HIDAPI device disconnected while opening");
}
/* Set the default connection state, can be overridden below */
if (device->is_bluetooth) {
joystick->connection_state = SDL_JOYSTICK_CONNECTION_WIRELESS;
} else {
joystick->connection_state = SDL_JOYSTICK_CONNECTION_WIRED;
}
if (!device->driver->OpenJoystick(device, joystick)) {
/* The open failed, mark this device as disconnected and update devices */
HIDAPI_JoystickDisconnected(device, joystickID);
+29 -25
View File
@@ -1426,8 +1426,6 @@ static int RAWINPUT_JoystickOpen(SDL_Joystick *joystick, int device_index)
}
}
joystick->epowerlevel = SDL_JOYSTICK_POWER_UNKNOWN;
#ifdef SDL_JOYSTICK_RAWINPUT_XINPUT
if (ctx->is_xinput) {
SDL_SetBooleanProperty(SDL_GetJoystickProperties(joystick), SDL_PROP_JOYSTICK_CAP_RUMBLE_BOOLEAN, SDL_TRUE);
@@ -1961,30 +1959,36 @@ static void RAWINPUT_UpdateOtherAPIs(SDL_Joystick *joystick)
}
has_trigger_data = SDL_TRUE;
if (battery_info->BatteryType != BATTERY_TYPE_UNKNOWN &&
battery_info->BatteryType != BATTERY_TYPE_DISCONNECTED) {
SDL_JoystickPowerLevel ePowerLevel = SDL_JOYSTICK_POWER_UNKNOWN;
if (battery_info->BatteryType == BATTERY_TYPE_WIRED) {
ePowerLevel = SDL_JOYSTICK_POWER_WIRED;
} else {
switch (battery_info->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_SendJoystickBatteryLevel(joystick, ePowerLevel);
SDL_PowerState state;
int percent;
switch (battery_info->BatteryType) {
case BATTERY_TYPE_WIRED:
state = SDL_POWERSTATE_CHARGING;
break;
case BATTERY_TYPE_UNKNOWN:
case BATTERY_TYPE_DISCONNECTED:
state = SDL_POWERSTATE_UNKNOWN;
break;
default:
state = SDL_POWERSTATE_ON_BATTERY;
break;
}
switch (battery_info->BatteryLevel) {
case BATTERY_LEVEL_EMPTY:
percent = 10;
break;
case BATTERY_LEVEL_LOW:
percent = 40;
break;
case BATTERY_LEVEL_MEDIUM:
percent = 70;
break;
default:
case BATTERY_LEVEL_FULL:
percent = 100;
break;
}
SDL_SendJoystickPowerInfo(joystick, state, percent);
}
}
#endif /* SDL_JOYSTICK_RAWINPUT_XINPUT */
+34 -13
View File
@@ -739,7 +739,11 @@ static int WGI_JoystickOpen(SDL_Joystick *joystick, int device_index)
}
/* Initialize the joystick capabilities */
joystick->epowerlevel = wireless ? SDL_JOYSTICK_POWER_UNKNOWN : SDL_JOYSTICK_POWER_WIRED;
if (wireless) {
joystick->connection_state = SDL_JOYSTICK_CONNECTION_WIRELESS;
} else {
joystick->connection_state = SDL_JOYSTICK_CONNECTION_WIRED;
}
__x_ABI_CWindows_CGaming_CInput_CIRawGameController_get_ButtonCount(hwdata->controller, &joystick->nbuttons);
__x_ABI_CWindows_CGaming_CInput_CIRawGameController_get_AxisCount(hwdata->controller, &joystick->naxes);
__x_ABI_CWindows_CGaming_CInput_CIRawGameController_get_SwitchCount(hwdata->controller, &joystick->nhats);
@@ -893,14 +897,38 @@ static void WGI_JoystickUpdate(SDL_Joystick *joystick)
SDL_stack_free(hats);
SDL_stack_free(axes);
if (joystick->epowerlevel != SDL_JOYSTICK_POWER_WIRED && hwdata->battery) {
if (hwdata->battery) {
__x_ABI_CWindows_CDevices_CPower_CIBatteryReport *report = NULL;
hr = __x_ABI_CWindows_CGaming_CInput_CIGameControllerBatteryInfo_TryGetBatteryReport(hwdata->battery, &report);
if (SUCCEEDED(hr) && report) {
SDL_PowerState state = SDL_POWERSTATE_UNKNOWN;
int percent = 0;
__x_ABI_CWindows_CSystem_CPower_CBatteryStatus status;
int full_capacity = 0, curr_capacity = 0;
__FIReference_1_int *full_capacityP, *curr_capacityP;
hr = __x_ABI_CWindows_CDevices_CPower_CIBatteryReport_get_Status(report, &status);
if (SUCCEEDED(hr)) {
switch (status) {
case BatteryStatus_NotPresent:
state = SDL_POWERSTATE_NO_BATTERY;
break;
case BatteryStatus_Discharging:
state = SDL_POWERSTATE_ON_BATTERY;
break;
case BatteryStatus_Idle:
state = SDL_POWERSTATE_CHARGED;
break;
case BatteryStatus_Charging:
state = SDL_POWERSTATE_CHARGING;
break;
default:
state = SDL_POWERSTATE_UNKNOWN;
break;
}
}
hr = __x_ABI_CWindows_CDevices_CPower_CIBatteryReport_get_FullChargeCapacityInMilliwattHours(report, &full_capacityP);
if (SUCCEEDED(hr)) {
__FIReference_1_int_get_Value(full_capacityP, &full_capacity);
@@ -914,18 +942,11 @@ static void WGI_JoystickUpdate(SDL_Joystick *joystick)
}
if (full_capacity > 0) {
float ratio = (float)curr_capacity / full_capacity;
if (ratio <= 0.05f) {
joystick->epowerlevel = SDL_JOYSTICK_POWER_EMPTY;
} else if (ratio <= 0.20f) {
joystick->epowerlevel = SDL_JOYSTICK_POWER_LOW;
} else if (ratio <= 0.70f) {
joystick->epowerlevel = SDL_JOYSTICK_POWER_MEDIUM;
} else {
joystick->epowerlevel = SDL_JOYSTICK_POWER_FULL;
}
percent = (int)SDL_roundf(((float)curr_capacity / full_capacity) * 100.0f);
}
SDL_SendJoystickPowerInfo(joystick, state, percent);
__x_ABI_CWindows_CDevices_CPower_CIBatteryReport_Release(report);
}
}
+29 -23
View File
@@ -289,30 +289,36 @@ int SDL_XINPUT_JoystickOpen(SDL_Joystick *joystick, JoyStick_DeviceData *joystic
static void UpdateXInputJoystickBatteryInformation(SDL_Joystick *joystick, XINPUT_BATTERY_INFORMATION_EX *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_SendJoystickBatteryLevel(joystick, ePowerLevel);
SDL_PowerState state;
int percent;
switch (pBatteryInformation->BatteryType) {
case BATTERY_TYPE_WIRED:
state = SDL_POWERSTATE_CHARGING;
break;
case BATTERY_TYPE_UNKNOWN:
case BATTERY_TYPE_DISCONNECTED:
state = SDL_POWERSTATE_UNKNOWN;
break;
default:
state = SDL_POWERSTATE_ON_BATTERY;
break;
}
switch (pBatteryInformation->BatteryLevel) {
case BATTERY_LEVEL_EMPTY:
percent = 10;
break;
case BATTERY_LEVEL_LOW:
percent = 40;
break;
case BATTERY_LEVEL_MEDIUM:
percent = 70;
break;
default:
case BATTERY_LEVEL_FULL:
percent = 100;
break;
}
SDL_SendJoystickPowerInfo(joystick, state, percent);
}
static void UpdateXInputJoystickState(SDL_Joystick *joystick, XINPUT_STATE *pXInputState, XINPUT_BATTERY_INFORMATION_EX *pBatteryInformation)