X-Git-Url: https://git.xinqibao.xyz/slstatus.git/blobdiff_plain/103945e7bb8d3a4198cf175b8f6be09fe53ab9e1..d76b1a21fc014395cf9f7bd4e14180a3a0945e9c:/components/battery.c diff --git a/components/battery.c b/components/battery.c index 6105a37..31c7d48 100644 --- a/components/battery.c +++ b/components/battery.c @@ -1,19 +1,14 @@ /* See LICENSE file for copyright and license details. */ -#include #include #include #include "../util.h" #if defined(__linux__) + #include #include #include - #define CHARGE_NOW "/sys/class/power_supply/%s/charge_now" - #define ENERGY_NOW "/sys/class/power_supply/%s/energy_now" - #define CURRENT_NOW "/sys/class/power_supply/%s/current_now" - #define POWER_NOW "/sys/class/power_supply/%s/power_now" - static const char * pick(const char *bat, const char *f1, const char *f2, char *path, size_t length) @@ -82,8 +77,8 @@ const char * battery_remaining(const char *bat) { - int charge_now, current_now, m, h; - float timeleft; + uint64_t charge_now, current_now, m, h; + double timeleft; char path[PATH_MAX], state[12]; if (esnprintf(path, sizeof(path), @@ -95,23 +90,30 @@ return NULL; } - if (!pick(bat, CHARGE_NOW, ENERGY_NOW, path, sizeof(path)) || - pscanf(path, "%d", &charge_now) < 0) { + if (!pick(bat, "/sys/class/power_supply/%s/charge_now", + "/sys/class/power_supply/%s/energy_now", + path, sizeof(path)) || + pscanf(path, "%" PRIu64, &charge_now) < 0) { return NULL; } if (!strcmp(state, "Discharging")) { - if (!pick(bat, CURRENT_NOW, POWER_NOW, path, + if (!pick(bat, "/sys/class/power_supply/%s/current_now", + "/sys/class/power_supply/%s/power_now", path, sizeof(path)) || - pscanf(path, "%d", ¤t_now) < 0) { + pscanf(path, "%" PRIu64, ¤t_now) < 0) { + return NULL; + } + + if (current_now == 0) { return NULL; } - timeleft = (float)charge_now / (float)current_now; + timeleft = (double)charge_now / (double)current_now; h = timeleft; - m = (timeleft - (float)h) * 60; + m = (timeleft - (double)h) * 60; - return bprintf("%dh %dm", h, m); + return bprintf("%" PRIu64 "h %" PRIu64 "m", h, m); } return "";