spoon

[fork] customized build of spoon, the dwm status utility
git clone git://src.adamsgaard.dk/spoon # fast
git clone https://src.adamsgaard.dk/spoon.git # slow
Log | Files | Refs | LICENSE Back to index

commit 7365d31d3c34ce9d6ae191a4e38a9817199cf22c
parent 568c3949f6922f2a250c0bf108ae6afedae319a9
Author: Anders Damsgaard <anders@adamsgaard.dk>
Date:   Sat,  2 Apr 2022 16:35:46 +0200

calculate remaining battery time on linux

Diffstat:
Mbatt.c | 20+++++++++++++++++---
Mconfig.h | 2+-
Mtypes.h | 2++
3 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/batt.c b/batt.c @@ -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"); @@ -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); @@ -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 diff --git a/config.h b/config.h @@ -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 } }, diff --git a/types.h b/types.h @@ -1,6 +1,8 @@ struct battarg { char *cap; char *ac; + char *en; + char *pow; }; struct mpdarg {