From cd487dcf76559cb95bd2f5595f1b4d9d3c702373 Mon Sep 17 00:00:00 2001 From: Przemek Grondek Date: Fri, 14 Oct 2022 00:05:19 +0200 Subject: [PATCH] Add power consumption --- dwmstatus.c | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/dwmstatus.c b/dwmstatus.c index dd5f136..5c511ac 100644 --- a/dwmstatus.c +++ b/dwmstatus.c @@ -100,7 +100,11 @@ readfile(char *base, char *file) memset(line, 0, sizeof(line)); - path = smprintf("%s/%s", base, file); + if (file != NULL) + path = smprintf("%s/%s", base, file); + else { + path = smprintf("%s", base); + } fd = fopen(path, "r"); free(path); if (fd == NULL) @@ -194,6 +198,26 @@ getBrightness(char *base) { return smprintf("%s %d%% ", status, percentage); } +char * +getPowerConsumption(char *base) { + char *co; + long usageRead; + float usage; + + co = readfile(base, NULL); + if (co == NULL) + return smprintf(""); + + sscanf(co, "%ld", &usageRead); + free(co); + + if (usage == 0) + return smprintf(""); + + usage = usageRead/ 1e6; + + return smprintf("%.2lf W ", usage); +} char * gettemperature(char *base, char *sensor) @@ -213,15 +237,17 @@ void update() { char *t0; char *bat; char *brightness; + char *powerConsumption; avgs = loadavg(); tmbln = mktimes("%H:%M %d.%m.%Y", tzwarsaw); t0 = gettemperature("/sys/class//hwmon/hwmon1", "temp1_input"); bat = getbattery("/sys/class/power_supply/BAT0"); brightness = getBrightness("/sys/class/backlight/amdgpu_bl0"); + powerConsumption = getPowerConsumption("/sys/class/power_supply/BAT0/power_now"); - status = smprintf("%s%s%s%s%s", - bat, brightness, t0, avgs, tmbln); + status = smprintf("%s%s%s%s%s%s", + powerConsumption, bat, brightness, t0, avgs, tmbln); setstatus(status); free(t0);