tbatt.c - spoon - [fork] customized build of spoon, the dwm status utility
 (HTM) git clone git://src.adamsgaard.dk/spoon
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) LICENSE
       ---
       tbatt.c (1913B)
       ---
            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, int remain, int power)
           14 {
           15         char c;
           16         static int frame = 0;
           17 
           18         c = acon ? '=' : ' ';
           19         if (c == ' ' && remain != 0)
           20                 snprintf(buf, len, " %d%% %d:%02d (%.2g W) |",
           21                          life, remain/60, remain%60, (float)power/1000000);
           22         else
           23                 snprintf(buf, len, " %d%% (%.2g W) |", life, (float)power/1000000);
           24 
           25 }
           26 
           27 #ifdef __OpenBSD__
           28 #include <sys/ioctl.h>
           29 
           30 #include <fcntl.h>
           31 #include <unistd.h>
           32 
           33 #include <machine/apmvar.h>
           34 
           35 int
           36 battread(void *arg, char *buf, size_t len)
           37 {
           38         struct apm_power_info info;
           39         int ret, fd;
           40 
           41         fd = open("/dev/apm", O_RDONLY);
           42         if (fd < 0) {
           43                 warn("open %s", "/dev/apm");
           44                 return -1;
           45         }
           46         ret = ioctl(fd, APM_IOC_GETPOWER, &info);
           47         if (ret < 0) {
           48                 warn("APM_IOC_GETPOWER %s", "/dev/apm");
           49                 close(fd);
           50                 return -1;
           51         }
           52         close(fd);
           53 
           54         if (info.battery_state == APM_BATTERY_ABSENT)
           55                 snprintf(buf, len, "");
           56         else
           57                 battprint(buf, len, info.ac_state == APM_AC_ON,
           58                           info.battery_life, info.minutes_left, 0);
           59         return 0;
           60 }
           61 #elif __linux__
           62 int
           63 battread(void *arg, char *buf, size_t len)
           64 {
           65         FILE *fp;
           66         int acon, life, energy, power;
           67         struct battarg *battarg = arg;
           68 
           69         fp = fopen(battarg->cap, "r");
           70         if (fp == NULL) {
           71                 warn("fopen %s", battarg->cap);
           72                 return -1;
           73         }
           74         fscanf(fp, "%d", &life);
           75         fclose(fp);
           76 
           77         fp = fopen(battarg->ac, "r");
           78         if (fp == NULL) {
           79                 warn("fopen %s", battarg->ac);
           80                 return -1;
           81         }
           82         fscanf(fp, "%d", &acon);
           83         fclose(fp);
           84         fp = fopen(battarg->en, "r");
           85         if (fp == NULL) {
           86                 warn("fopen %s", battarg->en);
           87                 return -1;
           88         }
           89         fscanf(fp, "%d", &energy);
           90         fclose(fp);
           91         fp = fopen(battarg->pow, "r");
           92         if (fp == NULL) {
           93                 warn("fopen %s", battarg->pow);
           94                 return -1;
           95         }
           96         fscanf(fp, "%d", &power);
           97         fclose(fp);
           98         battprint(buf, len, acon, life, (float)energy / power * 60.0, power);
           99         return 0;
          100 }
          101 #endif