commit 12b21d085d2e8d56034a7047dc1bcf60f49d46e8
parent aa689092b9b76e041c60d5a70fd18d10ef0742b3
Author: sin <sin@2f30.org>
Date: Sat, 14 May 2016 17:52:37 +0100
Add battery plugin
Diffstat:
M | spoon.c | | | 50 | ++++++++++++++++++++++++++++++++++++++++---------- |
1 file changed, 40 insertions(+), 10 deletions(-)
diff --git a/spoon.c b/spoon.c
@@ -1,15 +1,20 @@
#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
+#include <machine/apmvar.h>
+
#define LEN(x) (sizeof (x) / sizeof *(x))
+int dummyread(char *buf, size_t len);
int mpdread(char *buf, size_t len);
+int battread(char *buf, size_t len);
int dateread(char *buf, size_t len);
-int dummyread(char *buf, size_t len);
struct ent {
char *fmt;
@@ -17,10 +22,19 @@ struct ent {
} ents[] = {
{ .fmt = "[%s]", .read = mpdread },
{ .fmt = " ", .read = dummyread },
+ { .fmt = "%s%%", .read = battread },
+ { .fmt = " ", .read = dummyread },
{ .fmt = "%s", .read = dateread },
};
int
+dummyread(char *buf, size_t len)
+{
+ buf[0] = '\0';
+ return 0;
+}
+
+int
mpdread(char *buf, size_t len)
{
strlcpy(buf, "mpd", len);
@@ -28,6 +42,30 @@ mpdread(char *buf, size_t len)
}
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);
+
+ snprintf(buf, len, "%d", info.battery_life);
+ return 0;
+}
+
+int
dateread(char *buf, size_t len)
{
struct tm *now;
@@ -41,21 +79,13 @@ dateread(char *buf, size_t len)
return 0;
}
-int
-dummyread(char *buf, size_t len)
-{
- buf[0] = '\0';
- return 0;
-}
-
void
entcat(char *line, size_t len)
{
char buf[BUFSIZ];
char *s, *e;
struct ent *ent;
- int ret;
- int i;
+ int ret, i;
s = line;
e = line + len;