diff --git a/BatteryMeter.c b/BatteryMeter.c index 859df9d7..0ae2834c 100644 --- a/BatteryMeter.c +++ b/BatteryMeter.c @@ -15,6 +15,7 @@ This meter written by Ian P. Hands (iphands@gmail.com, ihands@redhat.com). #include "StringUtils.h" #include "Platform.h" +#include #include #include @@ -29,8 +30,8 @@ static void BatteryMeter_updateValues(Meter * this, char *buffer, int len) { Battery_getData(&percent, &isOnAC); - if (percent == -1) { - this->values[0] = 0; + if (isnan(percent)) { + this->values[0] = NAN; xSnprintf(buffer, len, "n/a"); return; } diff --git a/darwin/Battery.c b/darwin/Battery.c index d52a5954..51d49c6d 100644 --- a/darwin/Battery.c +++ b/darwin/Battery.c @@ -1,6 +1,8 @@ #include "BatteryMeter.h" +#include + #include #include #include @@ -9,7 +11,7 @@ void Battery_getData(double* level, ACPresence* isOnAC) { CFTypeRef power_sources = IOPSCopyPowerSourcesInfo(); - *level = -1; + *level = NAN; *isOnAC = AC_ERROR; if(NULL == power_sources) { diff --git a/dragonflybsd/Battery.c b/dragonflybsd/Battery.c index f17efb5d..58960429 100644 --- a/dragonflybsd/Battery.c +++ b/dragonflybsd/Battery.c @@ -7,13 +7,14 @@ in the source distribution for its full text. */ #include "BatteryMeter.h" +#include #include void Battery_getData(double* level, ACPresence* isOnAC) { int life; size_t life_len = sizeof(life); if (sysctlbyname("hw.acpi.battery.life", &life, &life_len, NULL, 0) == -1) - *level = -1; + *level = NAN; else *level = life; diff --git a/freebsd/Battery.c b/freebsd/Battery.c index b8c5e312..009f7abe 100644 --- a/freebsd/Battery.c +++ b/freebsd/Battery.c @@ -6,13 +6,14 @@ in the source distribution for its full text. */ #include "BatteryMeter.h" +#include #include void Battery_getData(double* level, ACPresence* isOnAC) { int life; size_t life_len = sizeof(life); if (sysctlbyname("hw.acpi.battery.life", &life, &life_len, NULL, 0) == -1) - *level = -1; + *level = NAN; else *level = life; diff --git a/linux/Battery.c b/linux/Battery.c index 1930880d..f92a3176 100644 --- a/linux/Battery.c +++ b/linux/Battery.c @@ -17,6 +17,7 @@ Linux battery readings written by Ian P. Hands (iphands@gmail.com, ihands@redhat #include #include #include +#include #include "BatteryMeter.h" #include "StringUtils.h" @@ -140,18 +141,18 @@ static ACPresence procAcpiCheck(void) { static double Battery_getProcBatData(void) { const unsigned long int totalFull = parseBatInfo("info", 3, 4); if (totalFull == 0) - return 0; + return NAN; const unsigned long int totalRemain = parseBatInfo("state", 5, 3); if (totalRemain == 0) - return 0; + return NAN; return totalRemain * 100.0 / (double) totalFull; } static void Battery_getProcData(double* level, ACPresence* isOnAC) { - *level = Battery_getProcBatData(); *isOnAC = procAcpiCheck(); + *level = AC_ERROR != *isOnAC ? Battery_getProcBatData() : NAN; } // ---------------------------------------- @@ -176,7 +177,7 @@ static inline ssize_t xread(int fd, void *buf, size_t count) { static void Battery_getSysData(double* level, ACPresence* isOnAC) { - *level = 0; + *level = NAN; *isOnAC = AC_ERROR; DIR *dir = opendir(SYS_POWERSUPPLY_DIR); @@ -279,13 +280,14 @@ static void Battery_getSysData(double* level, ACPresence* isOnAC) { } } closedir(dir); - *level = totalFull > 0 ? ((double) totalRemain * 100) / (double) totalFull : 0; + + *level = totalFull > 0 ? ((double) totalRemain * 100.0) / (double) totalFull : NAN; } static enum { BAT_PROC, BAT_SYS, BAT_ERR } Battery_method = BAT_PROC; static time_t Battery_cacheTime = 0; -static double Battery_cacheLevel = 0; +static double Battery_cacheLevel = NAN; static ACPresence Battery_cacheIsOnAC = 0; void Battery_getData(double* level, ACPresence* isOnAC) { @@ -299,22 +301,21 @@ void Battery_getData(double* level, ACPresence* isOnAC) { if (Battery_method == BAT_PROC) { Battery_getProcData(level, isOnAC); - if (*level == 0) { + if (isnan(*level)) { Battery_method = BAT_SYS; } } if (Battery_method == BAT_SYS) { Battery_getSysData(level, isOnAC); - if (*level == 0) { + if (isnan(*level)) { Battery_method = BAT_ERR; } } if (Battery_method == BAT_ERR) { - *level = -1; + *level = NAN; *isOnAC = AC_ERROR; - } - if (*level > 100.0) { - *level = 100.0; + } else { + CLAMP(*level, 0.0, 100.0); } Battery_cacheLevel = *level; Battery_cacheIsOnAC = *isOnAC; diff --git a/openbsd/Battery.c b/openbsd/Battery.c index c215e418..66e4b631 100644 --- a/openbsd/Battery.c +++ b/openbsd/Battery.c @@ -10,6 +10,7 @@ in the source distribution for its full text. #include #include #include +#include #include static bool findDevice(const char* name, int* mib, struct sensordev* snsrdev, size_t* sdlen) { @@ -36,7 +37,7 @@ void Battery_getData(double* level, ACPresence* isOnAC) { bool found = findDevice("acpibat0", mib, &snsrdev, &sdlen); - *level = -1; + *level = NAN; if (found) { /* last full capacity */ mib[3] = 7; diff --git a/solaris/Battery.c b/solaris/Battery.c index 080cf540..121d2e49 100644 --- a/solaris/Battery.c +++ b/solaris/Battery.c @@ -1,7 +1,8 @@ +#include #include "BatteryMeter.h" void Battery_getData(double* level, ACPresence* isOnAC) { - *level = -1; + *level = NAN; *isOnAC = AC_ERROR; } diff --git a/unsupported/Battery.c b/unsupported/Battery.c index 080cf540..121d2e49 100644 --- a/unsupported/Battery.c +++ b/unsupported/Battery.c @@ -1,7 +1,8 @@ +#include #include "BatteryMeter.h" void Battery_getData(double* level, ACPresence* isOnAC) { - *level = -1; + *level = NAN; *isOnAC = AC_ERROR; }