diff --git a/src/core/hle/service/ptm/ptm.cpp b/src/core/hle/service/ptm/ptm.cpp index e373ed47a..f1b4d1bf2 100644 --- a/src/core/hle/service/ptm/ptm.cpp +++ b/src/core/hle/service/ptm/ptm.cpp @@ -13,6 +13,7 @@ #include "core/hle/service/ptm/ptm_u.h" #include "core/hle/service/service.h" #include "core/settings.h" +#include namespace Service { namespace PTM { @@ -49,22 +50,38 @@ void GetShellState(Service::Interface* self) { void GetBatteryLevel(Service::Interface* self) { u32* cmd_buff = Kernel::GetCommandBuffer(); - // TODO(purpasmart96): This function is only a stub, - // it returns a valid result without implementing full functionality. + int* battery_percentage; + SDL_PowerState power_status = SDL_GetPowerInfo(NULL, battery_percentage); cmd_buff[1] = RESULT_SUCCESS.raw; - cmd_buff[2] = - static_cast(ChargeLevels::CompletelyFull); // Set to a completely full battery - LOG_WARNING(Service_PTM, "(STUBBED) called"); + ChargeLevels PowerPercentageToChargeLevel = [](int* percentage) { + if (percentage <= 100 && percentage >= 61) return ChargeLevels::CompletelyFull; + if (percentage <= 60 && percentage >= 31) return ChargeLevels::MostlyFull; + if (percentage <= 30 && percentage >= 11) return ChargeLevels::HalfFull; + if (percentage <= 10 && percentage >= 5) return ChargeLevels::LowBattery; + if (percentage <= 5 && percentage >= 1) return ChargeLevels::CriticalBattery; + if (percentage == 0) return ChargeLevels::EmptyBattery; + + return ChargeLevels::CompletelyFull; + // Return full battery since it's obvious the battery is NOT empty. + } + + // Deal with exceptional cases + if (power_status == SDL_POWERSTATE_CHARGED || + power_status == SDL_POWERSTATE_NO_BATTERY || + power_status == SDL_POWERSTATE_UNKNOWN) { + + cmd_buff[2] = ChargeLevels::CompletelyFull; + return; + } + + cmd_buff[2] = PowerPercentageToChargeLevel(battery_percentage); } void GetBatteryChargeState(Service::Interface* self) { u32* cmd_buff = Kernel::GetCommandBuffer(); - // TODO(purpasmart96): This function is only a stub, - // it returns a valid result without implementing full functionality. - cmd_buff[1] = RESULT_SUCCESS.raw; cmd_buff[2] = battery_is_charging ? 1 : 0;