client: Basic detection of how much capacity a host's battery has on Android and Windows.

This commit is contained in:
Rom Walton 2013-02-07 16:06:47 -05:00 committed by Oliver Bock
parent 3453ed7814
commit 759b3b8078
3 changed files with 49 additions and 6 deletions

View File

@ -444,6 +444,37 @@ bool HOST_INFO::host_is_running_on_batteries() {
#endif
}
// Returns the percent of which the battery is charged
//
int HOST_INFO::host_battery_charge() {
#if defined(ANDROID)
// using /sys/class/power_supply/battery/capacity
char capacitypath[256];
int capacity = 0;
snprintf(capacitypath, sizeof(capacitypath), "/sys/class/power_supply/battery/capacity");
FILE *battery_capacity_file = fopen(acpath, "r");
if(battery_capacity_file) {
fscanf(battery_capacity_file, "%d", &capacity);
fclose(battery_capacity_file);
}
if (capacity) {
char msg[1024];
snprintf(msg, sizeof(msg),
"battery capacity at: %d%% charge",
capacity
);
LOGD(msg);
return capacity;
}
#endif
return 0;
}
#if LINUX_LIKE_SYSTEM
static void parse_meminfo_linux(HOST_INFO& host) {
char buf[256];

View File

@ -1279,9 +1279,9 @@ int HOST_INFO::get_host_info() {
}
bool HOST_INFO::host_is_running_on_batteries() {
SYSTEM_POWER_STATUS pStatus;
ZeroMemory(&pStatus, sizeof(SYSTEM_POWER_STATUS));
if (!GetSystemPowerStatus(&pStatus)) {
SYSTEM_POWER_STATUS Status;
ZeroMemory(&Status, sizeof(SYSTEM_POWER_STATUS));
if (!GetSystemPowerStatus(&Status)) {
return false;
}
@ -1289,13 +1289,24 @@ bool HOST_INFO::host_is_running_on_batteries() {
// undocumented value, so lets check to see if the
// battery is charging or missing and make that part
// of the decision.
bool bIsOnBatteryPower = (pStatus.ACLineStatus != 1);
bool bIsBatteryCharging = ((pStatus.BatteryFlag & 8) == 8);
bool bIsBatteryMissing = ((pStatus.BatteryFlag & 128) == 128);
bool bIsOnBatteryPower = (Status.ACLineStatus != 1);
bool bIsBatteryCharging = ((Status.BatteryFlag & 8) == 8);
bool bIsBatteryMissing = ((Status.BatteryFlag & 128) == 128);
return (bIsOnBatteryPower && !bIsBatteryCharging && !bIsBatteryMissing);
}
int HOST_INFO::host_battery_charge() {
SYSTEM_POWER_STATUS Status;
ZeroMemory(&Status, sizeof(SYSTEM_POWER_STATUS));
if (!GetSystemPowerStatus(&Status)) {
return false;
}
if (((int)Status.BatteryLifePercent) == 255) return 0;
return ((int)Status.BatteryLifePercent);
}
bool HOST_INFO::users_idle(bool /*check_all_logins*/, double idle_time_to_run) {
double seconds_idle = get_idle_tick_count() / 1000;
double seconds_time_to_run = 60 * idle_time_to_run;

View File

@ -75,6 +75,7 @@ public:
void print();
bool host_is_running_on_batteries();
int host_battery_charge();
#ifdef __APPLE__
bool users_idle(bool check_all_logins, double idle_time_to_run, double *actual_idle_time=NULL);
#else