X-Git-Url: https://git.xinqibao.xyz/slstatus.git/blobdiff_plain/b0401b13bb78bacde9d10b00766830cbeb2c90f6..c2492c3f99d9d434a6f935dea8e97fa27cdd2adc:/components/battery.c?ds=inline diff --git a/components/battery.c b/components/battery.c index 99e09d5..ae31dd6 100644 --- a/components/battery.c +++ b/components/battery.c @@ -5,14 +5,10 @@ #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) @@ -81,7 +77,7 @@ const char * battery_remaining(const char *bat) { - int charge_now, current_now, m, h; + uintmax_t charge_now, current_now, m, h; double timeleft; char path[PATH_MAX], state[12]; @@ -94,15 +90,18 @@ 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, "%" PRIuMAX, &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, "%" PRIuMAX, ¤t_now) < 0) { return NULL; } @@ -114,7 +113,7 @@ h = timeleft; m = (timeleft - (double)h) * 60; - return bprintf("%dh %dm", h, m); + return bprintf("%" PRIuMAX "h %" PRIuMAX "m", h, m); } return "";