tcalculate remaining battery time on linux - 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
       ---
 (DIR) commit 7365d31d3c34ce9d6ae191a4e38a9817199cf22c
 (DIR) parent 568c3949f6922f2a250c0bf108ae6afedae319a9
 (HTM) Author: Anders Damsgaard <anders@adamsgaard.dk>
       Date:   Sat,  2 Apr 2022 16:35:46 +0200
       
       calculate remaining battery time on linux
       
       Diffstat:
         M batt.c                              |      20 +++++++++++++++++---
         M config.h                            |       2 +-
         M types.h                             |       2 ++
       
       3 files changed, 20 insertions(+), 4 deletions(-)
       ---
 (DIR) diff --git a/batt.c b/batt.c
       t@@ -62,8 +62,7 @@ int
        battread(void *arg, char *buf, size_t len)
        {
                FILE *fp;
       -        int acon;
       -        int life;
       +        int acon, life, energy, power;
                struct battarg *battarg = arg;
        
                fp = fopen(battarg->cap, "r");
       t@@ -73,6 +72,7 @@ battread(void *arg, char *buf, size_t len)
                }
                fscanf(fp, "%d", &life);
                fclose(fp);
       +
                fp = fopen(battarg->ac, "r");
                if (fp == NULL) {
                        warn("fopen %s", battarg->ac);
       t@@ -80,7 +80,21 @@ battread(void *arg, char *buf, size_t len)
                }
                fscanf(fp, "%d", &acon);
                fclose(fp);
       -        battprint(buf, len, acon, life, 0);
       +        fp = fopen(battarg->en, "r");
       +        if (fp == NULL) {
       +                warn("fopen %s", battarg->en);
       +                return -1;
       +        }
       +        fscanf(fp, "%d", &energy);
       +        fclose(fp);
       +        fp = fopen(battarg->pow, "r");
       +        if (fp == NULL) {
       +                warn("fopen %s", battarg->pow);
       +                return -1;
       +        }
       +        fscanf(fp, "%d", &power);
       +        fclose(fp);
       +        battprint(buf, len, acon, life, (float)energy / power * 60.0);
                return 0;
        }
        #endif
 (DIR) diff --git a/config.h b/config.h
       t@@ -15,7 +15,7 @@ struct ent ents[] = {
                /*{ .fmt = "[%s] ",        .read = cpuread,        .arg = "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq" },*/
                { .fmt = " %s°C",        .read = tempread,        .arg = "/sys/class/hwmon/hwmon1/temp1_input" },
                { .fmt = " %s "SEP,        .read = loadread,        .arg = NULL },
       -        { .fmt = "%s",                .read = battread,        .arg = &(struct battarg){ .cap = "/sys/class/power_supply/BAT0/capacity", .ac = "/sys/class/power_supply/AC/online" } },
       +        { .fmt = "%s",                .read = battread,        .arg = &(struct battarg){ .cap = "/sys/class/power_supply/BAT0/capacity", .ac = "/sys/class/power_supply/AC/online", .en = "/sys/class/power_supply/BAT0/energy_now", .pow = "/sys/class/power_supply/BAT0/power_now" } },
                /*{ .fmt = "%s ",                .read = fileread,        .arg = "/etc/myname" },*/
                { .fmt = " %s "SEP,                .read = wifiread,        .arg = NULL },
                { .fmt = " %s",                .read = dateread,        .arg = &(struct datearg){ .fmt = "%Y-%m-%d %a %H:%M:%S %Z", .tz = NULL } },
 (DIR) diff --git a/types.h b/types.h
       t@@ -1,6 +1,8 @@
        struct battarg {
                char *cap;
                char *ac;
       +        char *en;
       +        char *pow;
        };
        
        struct mpdarg {