From 9ea61f69071b84d458d77fad09d273b130673bc0 Mon Sep 17 00:00:00 2001 From: Przemek Grondek Date: Mon, 25 Jul 2022 23:05:07 +0200 Subject: [PATCH] Add brightness --- dwmstatus.c | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/dwmstatus.c b/dwmstatus.c index 1b63143..61f0a73 100644 --- a/dwmstatus.c +++ b/dwmstatus.c @@ -165,6 +165,36 @@ getbattery(char *base) return smprintf("%s %.0f%%", status, ((float)remcap / (float)descap) * 100); } +char * +getBrightness(char *base) { + char *co; + int brightness, max_brightness; + char *status; + + co = readfile(base, "brightness"); + if (co == NULL) + return smprintf(""); + + sscanf(co, "%d", &brightness); + free(co); + + co = readfile(base, "max_brightness"); + if (co == NULL) + return smprintf(""); + sscanf(co, "%d", &max_brightness); + free(co); + + int percentage = (((float)brightness / (float)max_brightness) * 100); + + if(percentage < 50) { + status = "🔅"; + } else { + status = "☀"; + } + + return smprintf("%s %d%% ", status, percentage); +} + char * gettemperature(char *base, char *sensor) { @@ -184,20 +214,22 @@ main(void) char *tmbln; char *t0; char *bat; + char *brightness; if (!(dpy = XOpenDisplay(NULL))) { fprintf(stderr, "dwmstatus: cannot open display.\n"); return 1; } - +// 🖱 for (;;sleep(60)) { 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"); - status = smprintf("%s 🌡 %s 📈 %s 📅 %s", - bat, t0, avgs, tmbln); + status = smprintf("%s %s🌡 %s 📈 %s 📅 %s", + bat, brightness, t0, avgs, tmbln); setstatus(status); free(t0); @@ -205,6 +237,7 @@ main(void) free(avgs); free(tmbln); free(status); + free(brightness); } XCloseDisplay(dpy);