battery.c
1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#ifndef __FreeBSD__
#error Only works on FreeBSD
#endif
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/sysctl.h>
const char *bat_icons1[] = {
"", /* 0% */
"",
"",
"",
"",
"", /* 50% */
"",
"",
"",
"",
"", /* 100% */
};
int main() {
int life, state;
size_t len = sizeof(int);
if (sysctlbyname("hw.acpi.battery.state", &state, &len, NULL, 0) ||
sysctlbyname("hw.acpi.battery.life", &life, &len, NULL, 0)) {
puts("(no battery)");
exit(EXIT_FAILURE);
}
if (life > 100)
life = 100;
else if (life < 0)
life = 0;
switch (state) {
case 0: /* fully charged */
printf(" Full");
break;
case 1: /* discharging */
printf("%s %d%%\n", bat_icons1[life / 10], life);
break;
case 2: /* charging */
printf(" %d%%\n", life);
break;
case 7: /* disconnected */
printf("");
break;
default: /* unknown code */
printf(" unknown battery code");
}
return EXIT_SUCCESS;
}