Introduction
Introduction Statistics Contact Development Disclaimer Help
new-acpi-battery.c - sites - public wiki contents of suckless.org
git clone git://git.suckless.org/sites
Log
Files
Refs
---
new-acpi-battery.c (1208B)
---
1 char *
2 readfile(char *base, char *file)
3 {
4 char *path, line[513];
5 FILE *fd;
6
7 memset(line, 0, sizeof(line));
8
9 path = smprintf("%s/%s", base, file);
10 fd = fopen(path, "r");
11 if (fd == NULL)
12 return NULL;
13 free(path);
14
15 if (fgets(line, sizeof(line)-1, fd) == NULL)
16 return NULL;
17 fclose(fd);
18
19 return smprintf("%s", line);
20 }
21
22 /*
23 * Linux seems to change the filenames after suspend/hibernate
24 * according to a random scheme. So just check for both possibilities.
25 */
26 char *
27 getbattery(char *base)
28 {
29 char *co;
30 int descap, remcap;
31
32 descap = -1;
33 remcap = -1;
34
35 co = readfile(base, "present");
36 if (co == NULL || co[0] != '1') {
37 if (co != NULL) free(co);
38 return smprintf("not present");
39 }
40 free(co);
41
42 co = readfile(base, "charge_full_design");
43 if (co == NULL) {
44 co = readfile(base, "energy_full_design");
45 if (co == NULL)
46 return smprintf("");
47 }
48 sscanf(co, "%d", &descap);
49 free(co);
50
51 co = readfile(base, "charge_now");
52 if (co == NULL) {
53 co = readfile(base, "energy_now");
54 if (co == NULL)
55 return smprintf("");
56 }
57 sscanf(co, "%d", &remcap);
58 free(co);
59
60 if (remcap < 0 || descap < 0)
61 return smprintf("invalid");
62
63 return smprintf("%.0f", ((float)remcap / (float)descap) * 100);
64 }
65
You are viewing proxied material from suckless.org. The copyright of proxied material belongs to its original authors. Any comments or complaints in relation to proxied material should be directed to the original authors of the content concerned. Please see the disclaimer for more details.