Compare View
Commits (2)
Showing
1 changed file
Show diff stats
battery.c
1 | -#ifndef __FreeBSD__ | |
2 | - #error Only works on FreeBSD | |
3 | -#endif | |
4 | - | |
5 | 1 | #include <stdio.h> |
6 | 2 | #include <stdlib.h> |
3 | +#include <string.h> | |
7 | 4 | #include <sys/types.h> |
8 | -#include <sys/sysctl.h> | |
9 | 5 | |
10 | -const char *bat_icons1[] = { | |
11 | - "", /* 0% */ | |
12 | - "", | |
13 | - "", | |
14 | - "", | |
15 | - "", | |
16 | - "", /* 50% */ | |
17 | - "", | |
18 | - "", | |
19 | - "", | |
20 | - "", | |
21 | - "", /* 100% */ | |
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% */ | |
22 | 18 | }; |
23 | 19 | |
24 | -int main() { | |
25 | - int life, state; | |
20 | +typedef enum { | |
21 | + CHARGED = 0, | |
22 | + DISCHARGING = 1, | |
23 | + CHARGING = 2, | |
24 | + DISCONNECTED = 7, | |
25 | +} State; | |
26 | + | |
27 | +#if defined (__linux__) | |
28 | +void battery_state(int *nbats, int bat[], State state[]) { | |
29 | + FILE *fd; | |
30 | + int i=0; | |
31 | + do { | |
32 | + char filename[50], status[50]; | |
33 | + | |
34 | + // get current capacity | |
35 | + sprintf(filename, "/sys/class/power_supply/BAT%d/capacity", i); | |
36 | + fd = fopen(filename, "rb"); | |
37 | + if (fd == NULL) | |
38 | + break; | |
39 | + fscanf(fd, "%d", &bat[i]); | |
40 | + fclose(fd); | |
41 | + // battery status (charging...) | |
42 | + sprintf(filename, "/sys/class/power_supply/BAT%d/status", i); | |
43 | + fd = fopen(filename, "rb"); | |
44 | + fgets(status, 50, fd); | |
45 | + // puts(status); | |
46 | + fclose(fd); | |
47 | + if (!strcmp(status, "Not charging\n")) | |
48 | + state[i] = CHARGED; | |
49 | + else if (!strcmp(status, "Discharging\n")) | |
50 | + state[i] = DISCHARGING; | |
51 | + else if (!strcmp(status, "Charging\n")) | |
52 | + state[i] = CHARGING; | |
53 | + | |
54 | + // next | |
55 | + i++; | |
56 | + } while (1); | |
57 | + *nbats = i; | |
58 | +} | |
59 | +#elif defined (__FreeBSD__) | |
60 | +#include <sys/sysctl.h> | |
61 | +void battery_state(int *nbats, int bat[], State state[]) { | |
26 | 62 | size_t len = sizeof(int); |
63 | + int state = -1, life = 0; | |
27 | 64 | |
28 | 65 | if (sysctlbyname("hw.acpi.battery.state", &state, &len, NULL, 0) || |
29 | - sysctlbyname("hw.acpi.battery.life", &life, &len, NULL, 0)) { | |
30 | - puts("(no battery)"); | |
31 | - exit(EXIT_FAILURE); | |
32 | - } | |
66 | + sysctlbyname("hw.acpi.battery.life", &life, &len, NULL, 0)) | |
67 | + state[0] = UNKNOWN; | |
68 | + | |
69 | + *nbats = 1; | |
70 | + bat[0] = life; | |
71 | + state[0] = state; | |
72 | +} | |
73 | +#endif | |
74 | + | |
75 | +int main() { | |
76 | + char *color; | |
77 | + int life[2], nbats; | |
78 | + State state[2]; | |
79 | + | |
80 | + battery_state(&nbats, life, state); | |
81 | + for (int i=0; i < nbats; i++) { | |
82 | + | |
83 | + if (life[i] >= 50) color = "colour028"; /* green */ | |
84 | + else if (life[i] >= 25) color = "colour172"; /* yellow */ | |
85 | + else color = "colour088"; /* red */ | |
33 | 86 | |
34 | - if (life > 100) | |
35 | - life = 100; | |
36 | - else if (life < 0) | |
37 | - life = 0; | |
38 | - | |
39 | - switch (state) { | |
40 | - case 0: /* fully charged */ | |
41 | - printf(" Full"); | |
42 | - break; | |
43 | - case 1: /* discharging */ | |
44 | - printf("%s %d%%\n", bat_icons1[life / 10], life); | |
45 | - break; | |
46 | - case 2: /* charging */ | |
47 | - printf(" %d%%\n", life); | |
48 | - break; | |
49 | - case 7: /* disconnected */ | |
50 | - printf(""); | |
51 | - break; | |
52 | - default: /* unknown code */ | |
53 | - printf(" unknown battery code"); | |
87 | + switch (state[i]) { | |
88 | + case CHARGED: /* fully charged */ | |
89 | + printf("%s %d%% ", icon[life[i]/10], life[i]); | |
90 | + break; | |
91 | + case DISCHARGING: /* discharging */ | |
92 | + printf("#[fg=%s]%s#[fg=default] %d%% ", color, icon[life[i]/10], life[i]); | |
93 | + break; | |
94 | + case CHARGING: /* charging */ | |
95 | + printf("#[fg=yellow]\uf583#[fg=default] %d%%\n ", life[i]); | |
96 | + break; | |
97 | + case DISCONNECTED: /* disconnected */ | |
98 | + printf("\uf492 "); | |
99 | + break; | |
100 | + default: /* unknown code */ | |
101 | + printf("\uf590 "); | |
102 | + } | |
54 | 103 | } |
55 | 104 | |
56 | 105 | return EXIT_SUCCESS; | ... | ... |