commit d16c69ff78956934359795abf45eea2f5d2abe15
parent 82a1782238d4e3318dc15e89ae67d856e19e1c16
Author: Christoph Lohmann <20h@r-36.net>
Date: Sun, 30 Oct 2011 11:27:05 +0100
Merging both patches to be valid for Linux 3.0.
Diffstat:
3 files changed, 19 insertions(+), 65 deletions(-)
diff --git a/dwm.suckless.org/dwmstatus/even-newer-acpi-battery.c b/dwm.suckless.org/dwmstatus/even-newer-acpi-battery.c
@@ -1,55 +0,0 @@
-char *
-readfile(char *base, char *file)
-{
- char *path, line[513];
- FILE *fd;
-
- memset(line, 0, sizeof(line));
-
- path = smprintf("%s/%s", base, file);
- fd = fopen(path, "r");
- if (fd == NULL) {
- perror("fopen");
- exit(1);
- }
- free(path);
-
- if (fgets(line, sizeof(line)-1, fd) == NULL) {
- perror("fgets");
- exit(1);
- }
- fclose(fd);
-
- return smprintf("%s", line);
-}
-
-char *
-getbattery(char *base)
-{
- char *co;
- int descap, remcap;
-
- descap = -1;
- remcap = -1;
-
- co = readfile(base, "present");
- if (co[0] != '1') {
- free(co);
- return smprintf("not present");
- }
- free(co);
-
- co = readfile(base, "energy_full_design");
- sscanf(co, "%d", &descap);
- free(co);
-
- co = readfile(base, "energy_now");
- sscanf(co, "%d", &remcap);
- free(co);
-
- if (remcap < 0 || descap < 0)
- return smprintf("invalid");
-
- return smprintf("%.0f", ((float)remcap / (float)descap) * 100);
-}
-
diff --git a/dwm.suckless.org/dwmstatus/index.md b/dwm.suckless.org/dwmstatus/index.md
@@ -20,6 +20,5 @@ Helper functions
If you have simple C functions for gathering system information, please
add them here as file or as code example.
-* [Support for ACPI battery status Linux < 3.0](new-acpi-battery.c)
-* [Support for ACPI battery status Linux >= 3](even-newer-acpi-battery.c)
+* [Support for ACPI battery status Linux](new-acpi-battery.c)
diff --git a/dwm.suckless.org/dwmstatus/new-acpi-battery.c b/dwm.suckless.org/dwmstatus/new-acpi-battery.c
@@ -8,21 +8,21 @@ readfile(char *base, char *file)
path = smprintf("%s/%s", base, file);
fd = fopen(path, "r");
- if (fd == NULL) {
- perror("fopen");
- exit(1);
- }
+ if (fd == NULL)
+ return NULL;
free(path);
- if (fgets(line, sizeof(line)-1, fd) == NULL) {
- perror("fgets");
- exit(1);
- }
+ if (fgets(line, sizeof(line)-1, fd) == NULL)
+ return NULL;
fclose(fd);
return smprintf("%s", line);
}
+/*
+ * Linux seems to change the filenames after suspend/hibernate
+ * according to a random scheme. So just check for both possibilities.
+ */
char *
getbattery(char *base)
{
@@ -40,10 +40,20 @@ getbattery(char *base)
free(co);
co = readfile(base, "charge_full_design");
+ if (co == NULL) {
+ co = readfile(base, "energy_full_design");
+ if (co == NULL)
+ return smprintf("");
+ }
sscanf(co, "%d", &descap);
free(co);
co = readfile(base, "charge_now");
+ if (co == NULL) {
+ co = readfile(base, "energy_now");
+ if (co == NULL)
+ return smprintf("");
+ }
sscanf(co, "%d", &remcap);
free(co);