battery.c (1218B)
1 /* See LICENSE for license details. */ 2 #include <fcntl.h> 3 #include <machine/apmvar.h> 4 #include <sys/ioctl.h> 5 6 struct bat_arg { 7 char *bat; /* BAT name (ignored on OpenBSD) */ 8 char *pre; /* prefix for percentages less than thres */ 9 char *suf; /* suffix for percentages less than thres */ 10 int thres; /* % threshold to consider low (-1 to disable) */ 11 }; 12 13 static size_t 14 batinfo(struct Block *b) 15 { 16 struct bat_arg ba = b->arg; 17 char *pre = ba->pre ? ba->pre : ""; 18 char *suf = ba->suf ? ba->suf : ""; 19 struct apm_power_info pi; 20 int fd, s; 21 22 if ((fd = open("/dev/apm", O_RDONLY)) < 0) 23 die("open\n"); 24 25 if ((ioctl(fd, APM_IOC_GETPOWER, &pi)) < 0) { 26 close(fd); 27 die("ioctl\n"); 28 } 29 close(fd); 30 31 s = pi.battery_life < ba->thres; 32 33 switch (pi.ac_state) { 34 case APM_AC_OFF: 35 snprintf(buf, sizeof(buf), "%s%d%% (%d:%02d)%s", s ? pre : "", 36 pi.battery_life, pi.minutes_left / 60, 37 pi.minutes_left % 60, s ? suf : ""); 38 case APM_AC_ON: 39 case APM_BATT_CHARGING: 40 snprintf(buf, sizeof(buf), "%d%% (ac)", pi.battery_life); 41 default: 42 snprintf(buf, sizeof(buf), "%s%d%% (unknown)%s", s ? pre : "", 43 pi.battery_life, s ? suf : ""); 44 } 45 return snprintf(b->curstr, LEN(b->curstr), b->fmt, buf); 46 }