xbattmon

simple battery monitor for X
git clone git://git.2f30.org/xbattmon
Log | Files | Refs | README | LICENSE

commit 48b2c54f609e276c53f724ef41710e91b38673b5
parent a8ddc6f0f269d07150a26195dfdfd5131d8d32fd
Author: Klemens Nanni <kn@openbsd.org>
Date:   Sun, 10 Feb 2019 18:57:54 +0100

Open apm(4) just once

On OpenBSD, no file I/O is required during runtime except for polling
the apm(4) device.

Instead of opening and closing the device on each poll, open it once on
startup with O_CLOEXEC.

Diffstat:
Mxbattmon.c | 18+++++++++++++-----
1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/xbattmon.c b/xbattmon.c @@ -246,20 +246,25 @@ redraw(void) #include <fcntl.h> #include <machine/apmvar.h> +int fd; + +void +openbat(void) +{ + fd = open(PATH_APM, O_RDONLY|O_CLOEXEC); + if (fd < 0) + err(1, "open %s", PATH_APM); +} + void pollbat(void) { struct apm_power_info info; int r; - int fd; - fd = open(PATH_APM, O_RDONLY); - if (fd < 0) - err(1, "open %s", PATH_APM); r = ioctl(fd, APM_IOC_GETPOWER, &info); if (r < 0) err(1, "APM_IOC_GETPOWER %s", PATH_APM); - close(fd); batcap = info.battery_life; if (batcap > maxcap) @@ -458,6 +463,9 @@ main(int argc, char *argv[]) usage(); setup(); +#ifdef __OpenBSD__ + openbat(); +#endif pollbat(); redraw(); loop();