summaryrefslogtreecommitdiff
path: root/components
diff options
context:
space:
mode:
authorAaron Marcher <me@drkhsh.at>2018-05-01 20:45:29 +0200
committerAaron Marcher <me@drkhsh.at>2018-05-01 20:45:29 +0200
commit41deabf2ec1ad5f10851421cfbaf17023e7de05f (patch)
tree6bebe479dac500f1b74745fef300cb93a8def78d /components
parent19343ff34385db441b5efc2e1a57cdd7fd95ec26 (diff)
battery_perc: Port to OpenBSD
Diffstat (limited to 'components')
-rw-r--r--components/battery.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/components/battery.c b/components/battery.c
index 04dfa47..327d576 100644
--- a/components/battery.c
+++ b/components/battery.c
@@ -88,4 +88,41 @@
return bprintf("%d", apm_info.battery_life);
}
+
+ const char *
+ battery_state(const char *bat)
+ {
+ int fd;
+ size_t i;
+ struct apm_power_info apm_info;
+ struct {
+ unsigned int state;
+ char *symbol;
+ } map[] = {
+ { APM_AC_ON, "+" },
+ { APM_AC_OFF, "-" },
+ { APM_AC_UNKNOWN, "/" },
+ };
+
+ fd = open("/dev/apm", O_RDONLY);
+ if (fd < 0) {
+ fprintf(stderr, "open '/dev/apm': %s\n", strerror(errno));
+ return NULL;
+ }
+
+ if (ioctl(fd, APM_IOC_GETPOWER, &apm_info) < 0) {
+ fprintf(stderr, "ioctl 'APM_IOC_GETPOWER': %s\n",
+ strerror(errno));
+ close(fd);
+ return NULL;
+ }
+ close(fd);
+
+ for (i = 0; i < LEN(map); i++) {
+ if (map[i].state == apm_info.ac_state) {
+ break;
+ }
+ }
+ return (i == LEN(map)) ? "?" : map[i].symbol;
+ }
#endif