support batteries on 3rd party controllers

(cherry picked from commit e610b85d1c)
This commit is contained in:
Sanjay Govind
2026-03-07 20:41:13 +13:00
committed by Sam Lantinga
parent 7280152549
commit 36ed4b75d1

View File

@@ -474,6 +474,10 @@ static bool HIDAPI_DriverPS5_InitDevice(SDL_HIDAPI_Device *device)
ctx->playerled_supported = true;
}
if (capabilities2 & 0x01) {
ctx->report_battery = true;
}
switch (device_type) {
case 0x00:
joystick_type = SDL_JOYSTICK_TYPE_GAMEPAD;
@@ -508,6 +512,7 @@ static bool HIDAPI_DriverPS5_InitDevice(SDL_HIDAPI_Device *device)
}
ctx->use_alternate_report = true;
ctx->report_battery = true;
if (device->vendor_id == USB_VENDOR_NACON_ALT &&
(device->product_id == USB_PRODUCT_NACON_REVOLUTION_5_PRO_PS5_WIRED ||
@@ -1465,6 +1470,36 @@ static void HIDAPI_DriverPS5_HandleStatePacketAlt(SDL_Joystick *joystick, SDL_hi
SDL_SendJoystickTouchpad(timestamp, joystick, 0, 1, touchpad_down, touchpad_x * TOUCHPAD_SCALEX, touchpad_y * TOUCHPAD_SCALEY, touchpad_down ? 1.0f : 0.0f);
}
if (ctx->report_battery) {
SDL_PowerState state;
int percent;
Uint8 status = (packet->ucBatteryLevel >> 4) & 0x0F;
Uint8 level = (packet->ucBatteryLevel & 0x0F);
// 0x0C means a controller isn't reporting battery levels
if (level != 0x0C) {
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);
}
}
HIDAPI_DriverPS5_HandleStatePacketCommon(joystick, dev, ctx, (PS5StatePacketCommon_t *)packet, timestamp);
if (ctx->guitar_whammy_supported) {