Commit 6e56c0177449e9e01b4c918f917fc32172b1fdbf
1 parent
a3b73ec8
Exists in
master
fix freebsd version
Showing
1 changed file
with
29 additions
and
29 deletions
Show diff stats
battery.c
@@ -4,17 +4,9 @@ | @@ -4,17 +4,9 @@ | ||
4 | #include <sys/types.h> | 4 | #include <sys/types.h> |
5 | 5 | ||
6 | const char *icon[] = { | 6 | const char *icon[] = { |
7 | - "\uf58d", /* 0% */ | ||
8 | - "\uf579", | ||
9 | - "\uf57a", | ||
10 | - "\uf57b", | ||
11 | - "\uf57c", | ||
12 | - "\uf57d", /* 50% */ | ||
13 | - "\uf57e", | ||
14 | - "\uf57f", | ||
15 | - "\uf580", | ||
16 | - "\uf581", | ||
17 | - "\uf578", /* 100% */ | 7 | + "\uf58d", /* 0% */ |
8 | + "\uf579", "\uf57a", "\uf57b", "\uf57c", "\uf57d", /* 50% */ | ||
9 | + "\uf57e", "\uf57f", "\uf580", "\uf581", "\uf578", /* 100% */ | ||
18 | }; | 10 | }; |
19 | 11 | ||
20 | typedef enum { | 12 | typedef enum { |
@@ -22,19 +14,20 @@ typedef enum { | @@ -22,19 +14,20 @@ typedef enum { | ||
22 | DISCHARGING = 1, | 14 | DISCHARGING = 1, |
23 | CHARGING = 2, | 15 | CHARGING = 2, |
24 | DISCONNECTED = 7, | 16 | DISCONNECTED = 7, |
17 | + UNKNOWN = -1 | ||
25 | } State; | 18 | } State; |
26 | 19 | ||
27 | -#if defined (__linux__) | 20 | +#if defined(__linux__) |
28 | void battery_state(int *nbats, int bat[], State state[]) { | 21 | void battery_state(int *nbats, int bat[], State state[]) { |
29 | FILE *fd; | 22 | FILE *fd; |
30 | - int i=0; | 23 | + int i = 0; |
31 | do { | 24 | do { |
32 | char filename[50], status[50]; | 25 | char filename[50], status[50]; |
33 | - | 26 | + |
34 | // get current capacity | 27 | // get current capacity |
35 | sprintf(filename, "/sys/class/power_supply/BAT%d/capacity", i); | 28 | sprintf(filename, "/sys/class/power_supply/BAT%d/capacity", i); |
36 | fd = fopen(filename, "rb"); | 29 | fd = fopen(filename, "rb"); |
37 | - if (fd == NULL) | 30 | + if (fd == NULL) |
38 | break; | 31 | break; |
39 | fscanf(fd, "%d", &bat[i]); | 32 | fscanf(fd, "%d", &bat[i]); |
40 | fclose(fd); | 33 | fclose(fd); |
@@ -56,19 +49,22 @@ void battery_state(int *nbats, int bat[], State state[]) { | @@ -56,19 +49,22 @@ void battery_state(int *nbats, int bat[], State state[]) { | ||
56 | } while (1); | 49 | } while (1); |
57 | *nbats = i; | 50 | *nbats = i; |
58 | } | 51 | } |
59 | -#elif defined (__FreeBSD__) | 52 | +#elif defined(__FreeBSD__) |
60 | #include <sys/sysctl.h> | 53 | #include <sys/sysctl.h> |
61 | void battery_state(int *nbats, int bat[], State state[]) { | 54 | void battery_state(int *nbats, int bat[], State state[]) { |
62 | size_t len = sizeof(int); | 55 | size_t len = sizeof(int); |
63 | - int state = -1, life = 0; | 56 | + int status, life = 0; |
64 | 57 | ||
65 | - if (sysctlbyname("hw.acpi.battery.state", &state, &len, NULL, 0) || | ||
66 | - sysctlbyname("hw.acpi.battery.life", &life, &len, NULL, 0)) | ||
67 | - state[0] = UNKNOWN; | 58 | + *nbats = 1; // FIXME: deal with multiple batteries |
68 | 59 | ||
69 | - *nbats = 1; | ||
70 | - bat[0] = life; | ||
71 | - state[0] = state; | 60 | + if (sysctlbyname("hw.acpi.battery.state", &status, &len, NULL, 0) || |
61 | + sysctlbyname("hw.acpi.battery.life", &life, &len, NULL, 0)) { | ||
62 | + state[0] = UNKNOWN; | ||
63 | + bat[0] = 0; | ||
64 | + } else { | ||
65 | + bat[0] = life; | ||
66 | + state[0] = status; | ||
67 | + } | ||
72 | } | 68 | } |
73 | #endif | 69 | #endif |
74 | 70 | ||
@@ -78,18 +74,22 @@ int main() { | @@ -78,18 +74,22 @@ int main() { | ||
78 | State state[2]; | 74 | State state[2]; |
79 | 75 | ||
80 | battery_state(&nbats, life, state); | 76 | battery_state(&nbats, life, state); |
81 | - for (int i=0; i < nbats; i++) { | 77 | + for (int i = 0; i < nbats; i++) { |
82 | 78 | ||
83 | - if (life[i] >= 50) color = "colour028"; /* green */ | ||
84 | - else if (life[i] >= 25) color = "colour172"; /* yellow */ | ||
85 | - else color = "colour088"; /* red */ | 79 | + if (life[i] >= 50) |
80 | + color = "colour028"; /* green */ | ||
81 | + else if (life[i] >= 25) | ||
82 | + color = "colour172"; /* yellow */ | ||
83 | + else | ||
84 | + color = "colour088"; /* red */ | ||
86 | 85 | ||
87 | switch (state[i]) { | 86 | switch (state[i]) { |
88 | case CHARGED: /* fully charged */ | 87 | case CHARGED: /* fully charged */ |
89 | - printf("%s %d%% ", icon[life[i]/10], life[i]); | 88 | + printf("%s %d%% ", icon[life[i] / 10], life[i]); |
90 | break; | 89 | break; |
91 | case DISCHARGING: /* discharging */ | 90 | case DISCHARGING: /* discharging */ |
92 | - printf("#[fg=%s]%s#[fg=default] %d%% ", color, icon[life[i]/10], life[i]); | 91 | + printf("#[fg=%s]%s#[fg=default] %d%% ", color, icon[life[i] / 10], |
92 | + life[i]); | ||
93 | break; | 93 | break; |
94 | case CHARGING: /* charging */ | 94 | case CHARGING: /* charging */ |
95 | printf("#[fg=yellow]\uf583#[fg=default] %d%%\n ", life[i]); | 95 | printf("#[fg=yellow]\uf583#[fg=default] %d%%\n ", life[i]); |