Xinqi Bao's Git

battery: OS split
[slstatus.git] / components / OpenBSD / battery.c
1 /* See LICENSE file for copyright and license details. */
2 #include <errno.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <sys/ioctl.h>
6 #include <fcntl.h>
7 #include <unistd.h>
8 #include <machine/apmvar.h>
9
10 #include "../../util.h"
11
12 const char *
13 battery_perc(const char *null)
14 {
15 struct apm_power_info apm_info;
16 int fd;
17
18 fd = open("/dev/apm", O_RDONLY);
19 if (fd < 0) {
20 fprintf(stderr, "open '/dev/apm': %s\n", strerror(errno));
21 return NULL;
22 }
23
24 if (ioctl(fd, APM_IOC_GETPOWER, &apm_info) < 0) {
25 fprintf(stderr, "ioctl 'APM_IOC_GETPOWER': %s\n", strerror(errno));
26 close(fd);
27 return NULL;
28 }
29 close(fd);
30
31 return bprintf("%d", apm_info.battery_life);
32 }