spoon

set dwm status
git clone git://git.2f30.org/spoon
Log | Files | Refs | LICENSE

batt.c (1697B)


      1 #include <err.h>
      2 #include <stdio.h>
      3 
      4 #include "types.h"
      5 #include "util.h"
      6 
      7 char *crit[] = {
      8 	"[!!!!]=",
      9 	"       ",
     10 };
     11 
     12 void
     13 battprint(char *buf, size_t len, int acon, int life)
     14 {
     15 	char c;
     16 	static int frame = 0;
     17 
     18 	c = acon ? '>' : '<';
     19 	if (!acon && life <= 5)
     20 		snprintf(buf, len, "%s", crit[frame++ % LEN(crit)]);
     21 	else if (life >= 80)
     22 		snprintf(buf, len, "[////]=");
     23 	else if (life >= 60)
     24 		snprintf(buf, len, "[///%c]=", c);
     25 	else if (life >= 40)
     26 		snprintf(buf, len, "[//%c%c]=", c, c);
     27 	else if (life >= 20)
     28 		snprintf(buf, len, "[/%c%c%c]=", c, c, c);
     29 	else
     30 		snprintf(buf, len, "[%c%c%c%c]=", c, c, c, c);
     31 }
     32 
     33 #ifdef __OpenBSD__
     34 #include <sys/ioctl.h>
     35 
     36 #include <fcntl.h>
     37 #include <unistd.h>
     38 
     39 #include <machine/apmvar.h>
     40 
     41 int
     42 battread(void *arg, char *buf, size_t len)
     43 {
     44 	struct apm_power_info info;
     45 	int ret, fd;
     46 
     47 	fd = open("/dev/apm", O_RDONLY);
     48 	if (fd < 0) {
     49 		warn("open %s", "/dev/apm");
     50 		return -1;
     51 	}
     52 	ret = ioctl(fd, APM_IOC_GETPOWER, &info);
     53 	if (ret < 0) {
     54 		warn("APM_IOC_GETPOWER %s", "/dev/apm");
     55 		close(fd);
     56 		return -1;
     57 	}
     58 	close(fd);
     59 
     60 	if (info.battery_state == APM_BATTERY_ABSENT)
     61 		snprintf(buf, len, "[no batt]");
     62 	else
     63 		battprint(buf, len, info.ac_state == APM_AC_ON, info.battery_life);
     64 	return 0;
     65 }
     66 #elif __linux__
     67 int
     68 battread(void *arg, char *buf, size_t len)
     69 {
     70 	FILE *fp;
     71 	int acon;
     72 	int life;
     73 	struct battarg *battarg = arg;
     74 
     75 	fp = fopen(battarg->cap, "r");
     76 	if (fp == NULL) {
     77 		warn("fopen %s", battarg->cap);
     78 		return -1;
     79 	}
     80 	fscanf(fp, "%d", &life);
     81 	fclose(fp);
     82 	fp = fopen(battarg->ac, "r");
     83 	if (fp == NULL) {
     84 		warn("fopen %s", battarg->ac);
     85 		return -1;
     86 	}
     87 	fscanf(fp, "%d", &acon);
     88 	fclose(fp);
     89 	battprint(buf, len, acon, life);
     90 	return 0;
     91 }
     92 #endif