commit f8af5822d0e12e3f7ce990160b9642692918c982
parent a7749ea8f6e8768be2eb7fcf04fdc4f96a17dda0
Author: lostd <lostd@2f30.org>
Date: Sun, 25 Sep 2016 16:57:25 +0100
Battery life in separate file and linux support
Diffstat:
M | Makefile | | | 6 | +++++- |
A | batt.c | | | 83 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
M | spoon.c | | | 42 | ------------------------------------------ |
3 files changed, 88 insertions(+), 43 deletions(-)
diff --git a/Makefile b/Makefile
@@ -4,9 +4,13 @@ CPPFLAGS = -I/usr/X11R6/include -I/usr/local/include
LDFLAGS = -L/usr/X11R6/lib -L/usr/local/lib
LDLIBS = -lxkbfile -lX11 -lmpdclient
DISTFILES = spoon.c config.def.h Makefile LICENSE
-OBJ = spoon.o strlcpy.o strlcat.o
+OBJ = spoon.o batt.o strlcpy.o strlcat.o
BIN = spoon
+# Linux
+#CPPFLAGS += -DPATH_BAT_CAP=\"/sys/class/power_supply/BAT0/capacity\"
+#CPPFLAGS += -DPATH_AC_ONLINE=\"/sys/class/power_supply/AC/online\"
+
all: $(BIN)
spoon.o: config.h
diff --git a/batt.c b/batt.c
@@ -0,0 +1,83 @@
+#include <stddef.h>
+#include <stdio.h>
+
+static void
+battprint(char *buf, size_t len, int acon , int life)
+{
+ char c;
+
+ c = acon ? '>' : '<';
+ if (life == 100)
+ snprintf(buf, len, "[////]=");
+ else if (life >= 75)
+ snprintf(buf, len, "[///%c]=", c);
+ else if (life >= 50)
+ snprintf(buf, len, "[//%c%c]=", c, c);
+ else if (life >= 25)
+ snprintf(buf, len, "[/%c%c%c]=", c, c, c);
+ else
+ snprintf(buf, len, "[%c%c%c%c]=", c, c, c, c);
+}
+
+#ifdef __OpenBSD__
+#include <sys/ioctl.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include <machine/apmvar.h>
+
+int
+battread(char *buf, size_t len)
+{
+ struct apm_power_info info;
+ int ret, fd;
+
+ fd = open("/dev/apm", O_RDONLY);
+ if (fd < 0) {
+ warn("open %s", "/dev/apm");
+ return -1;
+ }
+ ret = ioctl(fd, APM_IOC_GETPOWER, &info);
+ if (ret < 0) {
+ warn("APM_IOC_GETPOWER %s", "/dev/apm");
+ close(fd);
+ return -1;
+ }
+ close(fd);
+ battprint(buf, len, info.ac_state == APM_AC_ON, info.battery_life);
+ return 0;
+}
+#elif __linux__
+int
+battread(char *buf, size_t len)
+{
+ FILE *fp;
+ int acon;
+ int life;
+
+ fp = fopen(PATH_BAT_CAP, "r");
+ if (fp == NULL) {
+ warn("fopen %s", PATH_BAT_CAP);
+ return -1;
+ }
+ fscanf(fp, "%d", &life);
+ fclose(fp);
+ fp = fopen(PATH_AC_ONLINE, "r");
+ if (fp == NULL) {
+ warn("fopen %s", PATH_AC_ONLINE);
+ return -1;
+ }
+ fscanf(fp, "%d", &acon);
+ fclose(fp);
+ battprint(buf, len, acon, life);
+ return 0;
+}
+#else
+int
+battread(char *buf, size_t len)
+{
+ return -1;
+}
+#endif
diff --git a/spoon.c b/spoon.c
@@ -91,8 +91,6 @@ out:
#include <ifaddrs.h>
#include <limits.h>
-#include <machine/apmvar.h>
-
int
cpuread(char *buf, size_t len)
{
@@ -184,40 +182,6 @@ out:
}
int
-battread(char *buf, size_t len)
-{
- struct apm_power_info info;
- int ret, fd;
- char *icon;
- char c;
-
- fd = open("/dev/apm", O_RDONLY);
- if (fd < 0) {
- warn("open %s", "/dev/apm");
- return -1;
- }
- ret = ioctl(fd, APM_IOC_GETPOWER, &info);
- if (ret < 0) {
- warn("APM_IOC_GETPOWER %s", "/dev/apm");
- close(fd);
- return -1;
- }
- close(fd);
- c = info.ac_state == APM_AC_ON ? '>' : '<';
- if (info.battery_life == 100)
- snprintf(buf, len, "[////]=");
- else if (info.battery_life >= 75)
- snprintf(buf, len, "[///%c]=", c);
- else if (info.battery_life >= 50)
- snprintf(buf, len, "[//%c%c]=", c, c);
- else if (info.battery_life >= 25)
- snprintf(buf, len, "[/%c%c%c]=", c, c, c);
- else
- snprintf(buf, len, "[%c%c%c%c]=", c, c, c, c);
- return 0;
-}
-
-int
wifiread(char *buf, size_t len)
{
struct ifaddrs *ifa, *ifas;
@@ -316,12 +280,6 @@ mixread(char *buf, size_t len)
}
int
-battread(char *buf, size_t len)
-{
- return -1;
-}
-
-int
wifiread(char *buf, size_t len)
{
return -1;