ztatus

Status bar for dwm, and simple notification daemon.
git clone git://git.noxz.tech/ztatus
Log | Files | Refs | LICENSE

commit 7935a9de1bc0b06000d6712d675f78a27b0b5d5a
parent 757467be124d0a86dc5d8709c565448b1afea434
Author: Chris Noxz <chris@noxz.tech>
Date:   Fri,  3 May 2019 20:15:34 +0200

Generalization of element rendering

Diffstat:
Mconfig.def.h | 40++++++++++++++++++++++++----------------
Mztatus.c | 146+++++++++++++++++++++++++++++--------------------------------------------------
2 files changed, 78 insertions(+), 108 deletions(-)

diff --git a/config.def.h b/config.def.h @@ -18,46 +18,53 @@ static Limit limit_volume[] = { {1, "\x08"}, - {0, "\x07"} + {0, "\x07"}, + {-1} }; static Limit limit_power[] = { {75, "\ue011"}, {50, "\ue012"}, {25, "\ue013"}, {0, "\ue014"}, - {0, "\ue00b"} + {0, "\ue00b"}, + {-1} }; static Limit limit_temp[] = { {60, "\x09"}, - {0, "\x07"} + {0, "\x07"}, + {-1} }; static Limit limit_cpu[] = { {75, "\x09"}, - {0, "\x07"} + {0, "\x07"}, + {-1} }; static Limit limit_mem[] = { {80, "\x09"}, - {0, "\x07"} + {0, "\x07"}, + {-1} }; static Limit limit_mail[] = { {1, "\x08"}, - {0, "\x07"} + {0, "\x07"}, + {-1} }; static Limit limit_updates[] = { {1, "\x08"}, - {0, "\x07"} + {0, "\x07"}, + {-1} }; -static Element elements[ELEMENT_COUNT] = { +static Element elements[] = { /* handler renderer format val/vis arguments */ - [ElmVolume] = { get_volume, render_volume, FRMT_VOLUME, -1,1, {0} }, - [ElmPower] = { get_power, render_power, FRMT_POWER, -1,1, {.d = 1} }, - [ElmTemperature] = { get_temp, render_temp, FRMT_TEMP, -1,1, {.d = 10} }, - [ElmCPU] = { get_cpu, render_cpu, FRMT_CPU, -1,1, {.d = 1} }, - [ElmMemory] = { get_memory, render_mem, FRMT_MEM, -1,1, {.d = 1} }, - [ElmMail0] = { get_mail, render_mail, FRMT_MAIL, -1,1, {.t = MAIL_DIR_0} }, - [ElmMail1] = { get_mail, render_mail, FRMT_MAIL, -1,1, {.t = MAIL_DIR_1} }, - [ElmUpdates] = { get_updates, render_updates, FRMT_UPDATES, -1,1, {0} }, + [ElmVolume] = { get_volume, render_generic, FRMT_VOLUME, -1,1, {.l = limit_volume} }, + [ElmPower] = { get_power, render_power, FRMT_POWER, -1,1, {.l = limit_power, .d = 10} }, + [ElmTemperature] = { get_temp, render_generic, FRMT_TEMP, -1,1, {.l = limit_temp, .d = 10} }, + [ElmCPU] = { get_cpu, render_generic, FRMT_CPU, -1,1, {.l = limit_cpu, .d = 1} }, + [ElmMemory] = { get_memory, render_generic, FRMT_MEM, -1,1, {.l = limit_mem, .d = 1} }, + [ElmMail0] = { get_mail, render_generic, FRMT_MAIL, -1,1, {.l = limit_mail, .t = MAIL_DIR_0} }, + [ElmMail1] = { get_mail, render_generic, FRMT_MAIL, -1,1, {.l = limit_mail, .t = MAIL_DIR_1} }, + [ElmUpdates] = { get_updates, render_generic, FRMT_UPDATES, -1,1, {.l = limit_updates} }, [ElmDate] = { get_date, render_date, FRMT_DATE, -1,0, {.d = -60} }, [ElmTime] = { get_time, render_time, FRMT_TIME, -1,0, {.d = -60} }, [ElmIcon] = { NULL, NULL, FRMT_ICON, -1,1, {0} }, @@ -82,6 +89,7 @@ static Command commands[] = { { "update mail0", update_element, {.v = &elements[ElmMail0]} }, { "update mail1", update_element, {.v = &elements[ElmMail1]} }, { "update updates", update_element, {.v = &elements[ElmUpdates]} }, + { "update power", update_element, {.v = &elements[ElmPower]} }, /* --- miscellaneous ----------------------------------------------------*/ { "notify ...", notify, {0} }, diff --git a/ztatus.c b/ztatus.c @@ -15,7 +15,6 @@ #include <alsa/mixer.h> #define PNAME "ztatus" -#define ELEMENT_COUNT 11 #define LIMIT(x, a, b) ((x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)) #define MAX(a, b) ((a) > (b) ? (a) : (b)) #define LENGTH(x) (sizeof (x) / sizeof (x[0])) @@ -36,8 +35,14 @@ enum { }; typedef struct { + int threshold; + const char *symbol; +} Limit; + +typedef struct { int d; /* time delay */ const char *t; /* text */ + const Limit *l; /* limit */ } Attr; typedef struct { @@ -47,7 +52,7 @@ typedef struct { typedef struct { int (*handler)(int*, const Attr*); - void (*renderer)(char*, const char*, int); + void (*renderer)(char*, void*); const char *format; int value; int visible; @@ -55,11 +60,6 @@ typedef struct { } Element; typedef struct { - int threshold; - const char *symbol; -} Limit; - -typedef struct { const char *name; void (*func)(const Arg *, const char*, char*); const Arg arg; @@ -75,7 +75,8 @@ static int self_pid = 0; static char *status_line = NULL; static int fifofd; -/* functions referenced in config */ +/* --- functions referenced in config --- */ +/* get function, to gather data */ static int get_volume(int*, const Attr*); static int get_power(int*, const Attr*); static int get_temp(int*, const Attr*); @@ -85,17 +86,12 @@ static int get_mail(int*, const Attr*); static int get_updates(int*, const Attr*); static int get_date(int*, const Attr*); static int get_time(int*, const Attr*); - -static void render_volume(char*, const char*, int); -static void render_power(char*, const char*, int); -static void render_temp(char*, const char*, int); -static void render_cpu(char*, const char*, int); -static void render_mem(char*, const char*, int); -static void render_mail(char*, const char*, int); -static void render_updates(char*, const char*, int); -static void render_date(char*, const char*, int); -static void render_time(char*, const char*, int); - +/* render functions, to render elements */ +static void render_generic(char*, void*); +static void render_power(char*, void*); +static void render_date(char*, void*); +static void render_time(char*, void*); +/* miscellaneous functions */ static void toggle_element(const Arg*, const char*, char*); static void update_element(const Arg*, const char*, char*); static void notify(const Arg*, const char*, char*); @@ -109,8 +105,7 @@ static int daemonize(void); static int expand_tilde(const char*, char**); static int get_int(const char*, int*); static int get_pid(void); -static void render_threshold(char*, const char*, int, Limit[], int); -static void render_threshold_start(char*, const char*, int, Limit[], int, int); +static void render_threshold(char*, const char*, int, const Limit[], int); static int run(const char*); static void set_status(char*); static void sigint_handler(int); @@ -250,10 +245,7 @@ daemonize(void) if (!elements[i].visible) continue; if (elements[i].renderer) - elements[i].renderer( - status_line, - elements[i].format, - elements[i].value); + elements[i].renderer(status_line, &elements[i]); else strcpy( status_line + strlen(status_line), @@ -447,10 +439,12 @@ int get_power(int *value, const Attr *attr) { FILE *fp; - int energy_now, energy_full, voltage_now; - char status; - int old_value = *value; - int old_state = *value >= 1000; + int energy_now, energy_full, voltage_now, status; + int old_value; + int old_state; + + old_value = *value; + old_state = *value >= 1000; fp = fopen("/sys/class/power_supply/BAT0/energy_now", "r"); if (!fp) @@ -470,15 +464,15 @@ get_power(int *value, const Attr *attr) fscanf(fp, "%d", &voltage_now); fclose(fp); - fp = fopen("/sys/class/power_supply/BAT0/status", "r"); + fp = fopen("/sys/class/power_supply/AC/online", "r"); if (!fp) return 0; - fscanf(fp, "%c", &status); + fscanf(fp, "%d", &status); fclose(fp); *value = ((float)energy_now * 1000 / (float)voltage_now) * 100 /\ ((float)energy_full * 1000 / (float)voltage_now); - *value += status != 'D' ? 1000 : 0; + *value += (status == 1 ? 1000 : 0); if (*value != old_value || (*value >= 1000) != old_state) return 1; @@ -659,16 +653,18 @@ get_time(int *value, const Attr *attr) } void -render_threshold_start(char *str, const char *format, int value, Limit l[], int start, int count) +render_threshold(char *str, const char *format, int value, const Limit limits[], int start) { + const Limit *limit = (limits + start); char *tmp = NULL; - int i; - for (i = start; i < (start + count); i++) { - if (value >= l[i].threshold) { + + while (limit->threshold >= 0) { + if (value >= limit->threshold) { tmp = malloc(16); - snprintf(tmp, 16, format, l[i].symbol, value); + snprintf(tmp, 16, format, limit->symbol, value); break; } + limit++; } if (tmp) { @@ -678,87 +674,54 @@ render_threshold_start(char *str, const char *format, int value, Limit l[], int } void -render_threshold(char *str, const char *format, int value, Limit l[], int count) +render_generic(char *str, void *element) { - render_threshold_start(str, format, value, l, 0, count); + Element *e = ((Element *)element); + render_threshold(str, e->format, e->value, e->attr.l, 0); } void -render_volume(char *str, const char *format, int value) +render_power(char *str, void *element) { - render_threshold(str, format, value, limit_volume, 2); -} - -void -render_power(char *str, const char *format, int value) -{ - char *nformat = malloc(strlen(format) + 1); + Element *e = ((Element *)element); + char *nformat = malloc(strlen(e->format) + 1); int start = 0; - int count = 4; + int v = e->value; - strcpy(nformat, format); - if (value >= 1000) { - value -= 1000; - count = 1; + strcpy(nformat, e->format); + if (v >= 1000) { + v -= 1000; start = 4; nformat[0] = '\x08'; } - render_threshold_start(str, nformat, value, limit_power, start, count); + render_threshold(str, nformat, v, e->attr.l, start); if (nformat) free(nformat); } void -render_temp(char *str, const char *format, int value) -{ - render_threshold(str, format, value, limit_temp, 2); -} - -void -render_cpu(char *str, const char *format, int value) -{ - render_threshold(str, format, value, limit_cpu, 2); -} - -void -render_mem(char *str, const char *format, int value) -{ - render_threshold(str, format, value, limit_mem, 2); -} - -void -render_mail(char *str, const char *format, int value) -{ - render_threshold(str, format, value, limit_mail, 2); -} - -void -render_updates(char *str, const char *format, int value) -{ - render_threshold(str, format, value, limit_updates, 2); -} - -void -render_date(char *str, const char *format, int value) +render_date(char *str, void *element) { + Element *e = ((Element *)element); char *tmp = malloc(20); - snprintf(tmp, 20, format - ,value / 10000 - ,(value % 10000) / 100 - ,(value % 10000) % 100); + snprintf(tmp, 20, e->format + ,e->value / 10000 + ,(e->value % 10000) / 100 + ,(e->value % 10000) % 100); strcpy(str + strlen(str), tmp); free(tmp); } void -render_time(char *str, const char *format, int value) +render_time(char *str, void *element) { + Element *e = ((Element *)element); char *tmp = malloc(16); - snprintf(tmp, 16, format, value / 60, value % 60); + snprintf(tmp, 16, e->format, e->value / 60, e->value % 60); strcpy(str + strlen(str), tmp); free(tmp); } @@ -770,8 +733,7 @@ run(const char* cmd) char rval[8]; int val; - fp = popen(cmd, "r"); - if (fp == NULL) + if ((fp = popen(cmd, "r")) == NULL) return -1; if (fgets(rval, sizeof(rval), fp) != NULL)