s2dblocks

statusbar blocks for dwm
Log | Files | Refs

commit f9c4388924a9c3d1bd113e7e2fa0db3c92a3543e
parent b3883e5aa0d29fb37555765d66d54cac87f856ea
Author: hhvn <dev@hhvn.uk>
Date:   Wed, 13 Apr 2022 23:48:17 +0100

Collapse temps into average

Diffstat:
Mdwmbar.h | 10+++++++---
Mhandlers/bat.c | 14++++----------
Mhandlers/cpu.c | 47++++++++++++++++++++++-------------------------
Mstatus2d.c | 68+++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------
4 files changed, 88 insertions(+), 51 deletions(-)

diff --git a/dwmbar.h b/dwmbar.h @@ -1,18 +1,22 @@ #define bar_assert(expr) ((void)((expr) || (bar_assert_(#expr, __FILE__, __LINE__, __func__),0))) #define bar_height 15 +#define RED "#AA2222" +#define ORANGE "#AA7700" +#define GREEN "#00AA00" +#define BRBG "#1A413A" + /* misc.c */ void bar_assert_(const char *aval, const char *file, int line, const char *func); void *emalloc(size_t size); void *erealloc(void *ptr, size_t size); /* status2d.c */ -void s2d_reset(void); +void s2d_reset(int fg, int bg); void s2d_finish(void); void s2d_rect(unsigned x, unsigned y, unsigned w, unsigned h); void s2d_border(unsigned x, unsigned y, unsigned w, unsigned h, unsigned px); -void s2d_bar(char *bordercol, unsigned x, unsigned y, unsigned w, unsigned h, unsigned px, - char *incolour, unsigned percent); +void s2d_bar(unsigned x, unsigned y, unsigned w, unsigned h, unsigned px, unsigned percent); void s2d_fg(char *fg); void s2d_bg(char *bg); void s2d_forward(int px); /* negative to advance past drawn objects */ diff --git a/handlers/bat.c b/handlers/bat.c @@ -6,10 +6,6 @@ #include <sys/stat.h> #include "../dwmbar.h" -#define RED "#AA2222" -#define GREEN "#00AA00" -#define GREY "#888888" - int main(void) { enum { s_none, s_charge, s_dcharge }; @@ -89,8 +85,6 @@ main(void) { s2d_fg(RED); else if (status == s_charge) s2d_fg(GREEN); - else - s2d_fg(GREY); /* draw nose */ s2d_rect(2, (bar_height - 5) / 2, 1, 5); @@ -99,12 +93,12 @@ main(void) { s2d_border(3, 3, iw + 2, ih + 2, 1); /* fill battery */ - if (percent > 95 && status != s_dcharge) - s2d_fg(GREEN); - else if (percent < 25) + s2d_fg(BRBG); + s2d_rect(4, 4, iw, ih); + if (percent < 25) s2d_fg(RED); else - s2d_fg(GREY); + s2d_reset(1, 0); s2d_rect(4 + iw - w, 4, w, ih); /* pad + finish up */ diff --git a/handlers/cpu.c b/handlers/cpu.c @@ -8,9 +8,6 @@ #include "../dwmbar.h" #define MAXCORES 1024 /* moore's law, man */ -#define RED "#AA2222" -#define ORANGE "#AA7700" -#define GREEN "#00AA00" #ifdef __linux__ #define CPUTOKS 11 /* grep '^cpu' < /proc/stat | awk '{print NF}' */ @@ -29,7 +26,7 @@ statopen(void) { int main(void) { int percent[MAXCORES]; - int temp[MAXCORES]; + int avgtemp; int cores = 0; char *col; memset(percent, 0, sizeof(percent)); @@ -44,6 +41,7 @@ main(void) { char *toks[CPUTOKS]; char *p; int i, j; + int temptotal, tempn; memset(total, 0, sizeof(total)); memset(idle, 0, sizeof(idle)); @@ -99,7 +97,7 @@ main(void) { } fclose(f); - for (i = 0; i < MAXCORES && i < cores; i++) { + for (temptotal = tempn = i = 0; i < MAXCORES && i < cores; i++) { snprintf(buf, sizeof(buf), "/sys/class/thermal/thermal_zone%d/temp", i); if (stat(buf, &st) == -1) continue; @@ -109,34 +107,33 @@ main(void) { continue; fread(buf, sizeof(char), BUFSIZ, f); buf[5] = '\0'; - temp[i] = strtol(buf, NULL, 10) / 1000; + tempn++; + temptotal += strtol(buf, NULL, 10) / 1000; fclose(f); } + + avgtemp = temptotal / tempn; #else s2d_print("no handler for this OS"); s2d_finish(); return 1; #endif for (i = 0; percent[i] && i < MAXCORES; i++) { - if (percent[i] > 80) - col = RED: - else if (percent[i] > 50) - col = ORANGE; + s2d_bg(BRBG); + s2d_reset(1, 0); + s2d_bar(0, 2, 3, bar_height - 4, 1, percent[i]); + s2d_reset(0, 1); + s2d_forward(-1); + s2d_forward(2); + } + if (avgtemp) { + if (avgtemp >= 70) + s2d_fg(RED); + else if (avgtemp >= 60) + s2d_fg(ORANGE); else - col = GREEN; - s2d_bar("#000000", 0, 1, 5, bar_height - 2, 1, col, percent[i]); - if (temp[i]) { - if (temp[i] >= 70) - col = RED; - else if (temp[i] >= 60) - col = ORANGE; - else - col = GREEN; - s2d_fg(col); - s2d_print("%d°", temp[i]); - } else { - s2d_forward(-1); - s2d_forward(2); - } + s2d_reset(1, 1); + s2d_print("%d°", avgtemp); } + s2d_finish(); } diff --git a/status2d.c b/status2d.c @@ -2,27 +2,40 @@ #include <ctype.h> #include <string.h> #include <stdarg.h> +#include <stdlib.h> #include "dwmbar.h" static int needforwarding = 0; static int initialized = 0; +static char *curfg = NULL; +static char *curbg = NULL; #define s2d_init() (initialized ? s2d_init_() : ((void)0)) static void s2d_init_(void) { - s2d_reset(); + s2d_reset(1, 1); } void -s2d_reset(void) { +s2d_reset(int fg, int bg) { printf("^d^"); + + if (fg) + curfg = NULL; + else if (curfg) + s2d_fg(curfg); + + if (bg) + curbg = NULL; + else if (curbg) + s2d_bg(curbg); } void s2d_finish(void) { s2d_forward(-1); - s2d_reset(); + s2d_reset(1, 1); } void @@ -52,21 +65,42 @@ s2d_border(unsigned x, unsigned y, unsigned w, unsigned h, unsigned px) { } void -s2d_bar(char *bordercol, unsigned x, unsigned y, unsigned w, unsigned h, unsigned px, - char *incol, unsigned percent) { +s2d_bar(unsigned x, unsigned y, unsigned w, unsigned h, unsigned px, unsigned percent) { int maxpx = x + w; - int iw = w - 2 * px, ih = h - 2 * px; - int fh = ih * percent / 100; + int fh = h * percent / 100; + int needrestore = 0; + char *sfg = curfg ? strdup(curfg) : NULL; + char *sbg = curbg ? strdup(curbg) : NULL; s2d_init(); if (!w || !h) return; if (maxpx > needforwarding) needforwarding = maxpx; - s2d_fg(bordercol); - s2d_border(x, y, w, h, px); - s2d_fg(incol); - s2d_rect(x + px, y + px + ih - fh, iw, fh); + /* Hijack fg/bg: + * - use current bg for first rectangle + * - use current fg for second rectangle */ + + if (sbg) + s2d_fg(sbg); + else + s2d_reset(1, 1); + s2d_rect(x, y, w, h); + + if (sfg) + s2d_fg(sfg); + else + s2d_reset(1, 1); + s2d_rect(x, y + h - fh, w, fh); + + /* Restore old fg/bg */ + s2d_reset(1, 1); + if (sfg) + s2d_fg(sfg); + if (sbg) + s2d_bg(sbg); + free(sfg); + free(sbg); } static char * @@ -91,16 +125,24 @@ void s2d_fg(char *fg) { s2d_init(); fg = verifyhex(fg); - if (fg) + if (fg) { printf("^c#%s^", fg); + fg = strdup(fg); + free(curfg); + curfg = fg; + } } void s2d_bg(char *bg) { s2d_init(); bg = verifyhex(bg); - if (bg) + if (bg) { printf("^b#%s^", bg); + bg = strdup(bg); + free(curbg); + curbg = bg; + } } void