From 6e56c0177449e9e01b4c918f917fc32172b1fdbf Mon Sep 17 00:00:00 2001 From: Miguel BarĂ£o Date: Tue, 30 Jan 2024 18:09:22 +0000 Subject: [PATCH] fix freebsd version --- battery.c | 58 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/battery.c b/battery.c index 186812e..7f3720c 100644 --- a/battery.c +++ b/battery.c @@ -4,17 +4,9 @@ #include const char *icon[] = { - "\uf58d", /* 0% */ - "\uf579", - "\uf57a", - "\uf57b", - "\uf57c", - "\uf57d", /* 50% */ - "\uf57e", - "\uf57f", - "\uf580", - "\uf581", - "\uf578", /* 100% */ + "\uf58d", /* 0% */ + "\uf579", "\uf57a", "\uf57b", "\uf57c", "\uf57d", /* 50% */ + "\uf57e", "\uf57f", "\uf580", "\uf581", "\uf578", /* 100% */ }; typedef enum { @@ -22,19 +14,20 @@ typedef enum { DISCHARGING = 1, CHARGING = 2, DISCONNECTED = 7, + UNKNOWN = -1 } State; -#if defined (__linux__) +#if defined(__linux__) void battery_state(int *nbats, int bat[], State state[]) { FILE *fd; - int i=0; + int i = 0; do { char filename[50], status[50]; - + // get current capacity sprintf(filename, "/sys/class/power_supply/BAT%d/capacity", i); fd = fopen(filename, "rb"); - if (fd == NULL) + if (fd == NULL) break; fscanf(fd, "%d", &bat[i]); fclose(fd); @@ -56,19 +49,22 @@ void battery_state(int *nbats, int bat[], State state[]) { } while (1); *nbats = i; } -#elif defined (__FreeBSD__) +#elif defined(__FreeBSD__) #include void battery_state(int *nbats, int bat[], State state[]) { size_t len = sizeof(int); - int state = -1, life = 0; + int status, life = 0; - if (sysctlbyname("hw.acpi.battery.state", &state, &len, NULL, 0) || - sysctlbyname("hw.acpi.battery.life", &life, &len, NULL, 0)) - state[0] = UNKNOWN; + *nbats = 1; // FIXME: deal with multiple batteries - *nbats = 1; - bat[0] = life; - state[0] = state; + if (sysctlbyname("hw.acpi.battery.state", &status, &len, NULL, 0) || + sysctlbyname("hw.acpi.battery.life", &life, &len, NULL, 0)) { + state[0] = UNKNOWN; + bat[0] = 0; + } else { + bat[0] = life; + state[0] = status; + } } #endif @@ -78,18 +74,22 @@ int main() { State state[2]; battery_state(&nbats, life, state); - for (int i=0; i < nbats; i++) { + for (int i = 0; i < nbats; i++) { - if (life[i] >= 50) color = "colour028"; /* green */ - else if (life[i] >= 25) color = "colour172"; /* yellow */ - else color = "colour088"; /* red */ + if (life[i] >= 50) + color = "colour028"; /* green */ + else if (life[i] >= 25) + color = "colour172"; /* yellow */ + else + color = "colour088"; /* red */ switch (state[i]) { case CHARGED: /* fully charged */ - printf("%s %d%% ", icon[life[i]/10], life[i]); + printf("%s %d%% ", icon[life[i] / 10], life[i]); break; case DISCHARGING: /* discharging */ - printf("#[fg=%s]%s#[fg=default] %d%% ", color, icon[life[i]/10], life[i]); + printf("#[fg=%s]%s#[fg=default] %d%% ", color, icon[life[i] / 10], + life[i]); break; case CHARGING: /* charging */ printf("#[fg=yellow]\uf583#[fg=default] %d%%\n ", life[i]); -- libgit2 0.21.2