Commit: 5b20775ed74c339deb9dcd609f28861c5bfaf158
Parent: 47d29691cb6f94158edd08fff204172a081cb057
Author: opask
Date: Wed, 14 Nov 2018 23:18:12 -0700
add batteryinfo() for openbsd
Diffstat:
2 files changed, 34 insertions(+), 0 deletions(-)
diff --git a/openbsd.c b/openbsd.c
@@ -1,3 +1,8 @@
+#include <fcntl.h>
+#include <machine/apmvar.h>
+#include <sys/ioctl.h>
+#include <unistd.h>
+
#include "status.h"
#if defined(__OpenBSD__)
@@ -8,4 +13,32 @@ getvol(const char *card, const char *output)
return 0;
}
+char *
+batinfo(void)
+{
+ struct apm_power_info pi;
+ int fd;
+
+ if ((fd = open("/dev/apm", O_RDONLY)) < 0)
+ die("open\n");
+
+ if ((ioctl(fd, APM_IOC_GETPOWER, &pi)) < 0) {
+ close(fd);
+ die("ioctl\n");
+ }
+ close(fd);
+
+ switch (pi.ac_state) {
+ case APM_AC_OFF:
+ return smprintf("%d%% (%d:%02d)", pi.battery_life,
+ pi.minutes_left / 60, pi.minutes_left % 60);
+ case APM_BATT_CHARGING:
+ return smprintf("%d%% (charging)", pi.battery_life);
+ case APM_AC_ON:
+ return smprintf("%d%% (external)", pi.battery_life);
+ default:
+ return smprintf("%d%% (unknown)", pi.battery_life);
+ }
+}
+
#endif
diff --git a/status.h b/status.h
@@ -2,3 +2,4 @@ void die(const char *errstr, ...);
char *smprintf(const char *fmt, ...);
int getvol(const char *card, const char *output);
+char *batinfo(void);