s2dblocks

statusbar blocks for dwm
Log | Files | Refs

commit 7c540a7eafb4d024976c9e24649a002528fbdc1f
parent ee9b27abb3b937e83021cd445eaa847839de84f6
Author: hhvn <dev@hhvn.uk>
Date:   Thu, 14 Apr 2022 21:33:15 +0100

Simplify organization and makefile

Diffstat:
A.gitignore | 8++++++++
AMakefile | 25+++++++++++++++++++++++++
Abat.c | 116+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aconfig.mk | 5+++++
Acpu.c | 148+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Adate.c | 32++++++++++++++++++++++++++++++++
Adf.c | 69+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ddwmbar.h | 28----------------------------
Dhandlers/bat.c | 108-------------------------------------------------------------------------------
Dhandlers/cpu.c | 146-------------------------------------------------------------------------------
Dhandlers/date.c | 32--------------------------------
Dhandlers/mem.c | 53-----------------------------------------------------
Dhandlers/time.c | 14--------------
Dhandlers/vol.c | 102-------------------------------------------------------------------------------
Amem.c | 53+++++++++++++++++++++++++++++++++++++++++++++++++++++
Dmisc.c | 24------------------------
Mstatus2d.c | 2+-
Astatus2d.h | 21+++++++++++++++++++++
Atime.c | 14++++++++++++++
Avol.c | 102+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
20 files changed, 594 insertions(+), 508 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1,8 @@ +*.o +bat +cpu +date +df +mem +time +vol diff --git a/Makefile b/Makefile @@ -0,0 +1,25 @@ +OBJ = status2d.o +BINS = bat cpu date df mem time vol + +include config.mk + +all: $(BINS) +$(BINS): $(OBJ) status2d.h + +.c: + $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< $(OBJ) + +clean: + rm -f $(OBJ) $(BINS) + +install: + for f in $(BINS); do \ + install -m0755 $$f ~/.scripts/dwmbar/$$f; \ + done + +uninstall: + for f in $(BINS); do \ + rm ~/.scripts/dwmbar/$$f; \ + done + +.PHONY: clean install uninstall diff --git a/bat.c b/bat.c @@ -0,0 +1,116 @@ +#include <stdio.h> +#include <stdlib.h> +#include <assert.h> +#include <dirent.h> +#include <string.h> +#include <assert.h> +#include <sys/stat.h> +#include "status2d.h" + +int +main(void) { + enum { s_none, s_charge, s_dcharge }; + int percent, status = s_none, w, iw, ih; +#ifdef __linux__ +#define BATDIR "/sys/class/power_supply" + struct dirent **dirent; + struct stat st; + char buf[BUFSIZ]; + char *dir = NULL; + char *sfile = NULL; + char *cfile = NULL; + size_t len; + FILE *f; + int nbats, ndir, total; + int i; + + if ((ndir = scandir(BATDIR, &dirent, 0, alphasort)) < 0) { + return 1; + } + + for (i = total = nbats = 0; i < ndir; i++) { + if (strncmp(dirent[i]->d_name, "BAT", strlen("BAT")) == 0) { + free(dir); + len = strlen(BATDIR) + strlen(dirent[i]->d_name) + 2; /* / + \0 */ + dir = malloc(len); + assert(dir); + snprintf(dir, len, "%s/%s", BATDIR, dirent[i]->d_name); + + if (stat(dir, &st) == -1 || !S_ISDIR(st.st_mode)) + continue; + + free(sfile); + len = strlen(dir) + 8; /* /status\0 */ + sfile = malloc(len); + assert(sfile); + snprintf(sfile, len, "%s/status", dir); + if ((f = fopen(sfile, "r")) != NULL) { + memset(buf, '\0', sizeof(buf)); + fread(buf, sizeof(char), sizeof(buf), f); + fclose(f); + if (buf[strlen(buf) - 1] == '\n') + buf[strlen(buf) - 1] = '\0'; + /* XXX: some batteries charging, some discharging? */ + if (strcmp(buf, "Charging") == 0 && s_charge > status) + status = s_charge; + else if (strcmp(buf, "Discharging") == 0 && s_dcharge > status) + status = s_dcharge; + } + + free(cfile); + len = strlen(dir) + 10; /* /capacity\0 */ + cfile = malloc(len); + assert(cfile); + snprintf(cfile, len, "%s/capacity", dir); + if ((f = fopen(cfile, "r")) != NULL) { + memset(buf, '\0', sizeof(buf)); + fread(buf, sizeof(char), sizeof(buf), f); + fclose(f); + if (buf[strlen(buf) - 1] == '\n') + buf[strlen(buf) - 1] = '\0'; + nbats++; + total += atoi(buf); + } + } + } + + free(dir); + free(sfile); + free(cfile); + + percent = total / nbats; +#else + s2d_print("no handler for this OS"); + s2d_finish(); + return 1; +#endif + iw = 18; + ih = bar_height - 8; + w = iw * percent / 100; + + /* colour border+nose for charge indicator */ + if (status == s_dcharge) + s2d_fg(RED); + else if (status == s_charge) + s2d_fg(GREEN); + + /* draw nose */ + s2d_rect(2, (bar_height - 5) / 2, 1, 5); + + /* draw border for battery */ + s2d_border(3, 3, iw + 2, ih + 2, 1); + + /* fill battery */ + s2d_fg(BRBG); + s2d_rect(4, 4, iw, ih); + if (percent < 25) + s2d_fg(RED); + else + s2d_reset(1, 0); + s2d_rect(4 + iw - w, 4, w, ih); + + /* pad + finish up */ + s2d_forward(2); + s2d_finish(); + return 0; +} diff --git a/config.mk b/config.mk @@ -0,0 +1,5 @@ +# Debug +# CFLAGS += -g3 -O0 + +# Linux +vol: LDFLAGS += -lasound diff --git a/cpu.c b/cpu.c @@ -0,0 +1,148 @@ +#include <stdio.h> +#include <errno.h> +#include <ctype.h> +#include <string.h> +#include <stdlib.h> +#include <unistd.h> +#include <sys/stat.h> +#include "status2d.h" + +#define MAXCORES 1024 /* moore's law, man */ + +#ifdef __linux__ +#define CPUTOKS 11 /* grep '^cpu' < /proc/stat | awk '{print NF}' */ +static FILE * +statopen(void) { + FILE *ret; + if ((ret = fopen("/proc/stat", "r")) == NULL) { + s2d_print("%s: %s", strerror(errno)); + s2d_finish(); + exit(EXIT_FAILURE); + } + return ret; +} +#endif /* __linux__ */ + +int +main(void) { + int percent[MAXCORES]; + int avgtemp; + int cores = 0; + char *col; + memset(percent, 0, sizeof(percent)); + +#ifdef __linux__ + long long unsigned total[MAXCORES]; + long long unsigned idle[MAXCORES]; + long long unsigned ltotal, lidle, used, diff; + struct stat st; + FILE *f; + char buf[BUFSIZ]; + char *toks[CPUTOKS]; + char *p; + int i, j; + int temptotal, tempn; + + memset(total, 0, sizeof(total)); + memset(idle, 0, sizeof(idle)); + + f = statopen(); + while ((fgets(buf, sizeof(buf), f)) != NULL) { + buf[strlen(buf) - 1] = '\0'; /* remove \n */ + if (strncmp(buf, "cpu", 3) == 0 && isdigit(*(buf+3))) { + for (j = 0; j < CPUTOKS; j++) + toks[j] = strtok(j ? NULL : buf, " "); + p = toks[0]; + p += 3; /* "cpu" */ + i = strtoll(p, NULL, 10); + if (i >= MAXCORES) { + s2d_print("MAXCORES exceeded"); + s2d_finish(); + return 1; + } + if (i + 1 > cores) + cores = i + 1; + for (j = 1; j < CPUTOKS; j++) + total[i] += strtoll(toks[j], NULL, 10); + idle[i] = strtoll(toks[4], NULL, 10); + } + } + fclose(f); + + sleep(1); + + f = statopen(); + while ((fgets(buf, sizeof(buf), f)) != NULL) { + buf[strlen(buf) - 1] = '\0'; + if (strncmp(buf, "cpu", 3) == 0 && isdigit(*(buf+3))) { + for (j = 0; j < CPUTOKS; j++) + toks[j] = strtok(j ? NULL : buf, " "); + p = toks[0]; + p += 3; /* "cpu" */ + i = strtoll(p, NULL, 10); + if (i >= MAXCORES) { + s2d_print("MAXCORES exceeded"); + s2d_finish(); + return 1; + } + if (i + 1 > cores) + cores = i + 1; + for (j = 1, ltotal = 0; j < CPUTOKS; j++) + ltotal += strtoll(toks[j], NULL, 10); + diff = ltotal - total[i]; + lidle = strtoll(toks[4], NULL, 10) - idle[i]; + used = 100 * (diff - lidle); + percent[i] = used / diff; + } + } + fclose(f); + + 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; + if (!S_ISREG(st.st_mode)) + continue; + if ((f = fopen(buf, "r")) == NULL) + continue; + fread(buf, sizeof(char), BUFSIZ, f); + buf[5] = '\0'; + 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++) { + s2d_bg(BRBG); + if (percent[i] >= 86) + s2d_fg(RED); + else if (percent[i] >= 63) + s2d_fg(ORANGE); + else if (percent[i] >= 50) + s2d_fg(YELLOW); + else + s2d_fg(GREEN); + 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 >= 65) + s2d_fg(ORANGE); + else if (avgtemp >= 60) + s2d_fg(YELLOW); + else + s2d_reset(1, 1); + s2d_print("%d°", avgtemp); + } + s2d_finish(); +} diff --git a/date.c b/date.c @@ -0,0 +1,32 @@ +#include <stdio.h> +#include <ctype.h> +#include <time.h> +#include "status2d.h" + +int +main(void) { + char buf[BUFSIZ], *p; + time_t t = time(NULL); + struct tm *tm = localtime(&t); + + strftime(buf, sizeof(buf), "%a", tm); + for (p = buf; *p; p++) + *p = toupper(*p); + if (tm->tm_wday == 0 || tm->tm_wday == 1) /* weekend */ + s2d_fg(GREEN); + else + s2d_fg(GREY); + s2d_print("%s", buf); + + strftime(buf, sizeof(buf), "%d/%m", tm); + if (tm->tm_mon >= 3 && tm->tm_mon <= 5) + s2d_fg(YELLOW); /* spring */ + else if (tm->tm_mon >= 6 && tm->tm_mon <= 7) + s2d_fg(GREEN); /* summer */ + else if (tm->tm_mon >= 8 && tm->tm_mon <= 11) + s2d_fg(ORANGE); /* autumn */ + else + s2d_fg(WHITE); /* winter */ + s2d_print("%s", buf); + s2d_finish(); +} diff --git a/df.c b/df.c @@ -0,0 +1,69 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <assert.h> +#ifdef __linux__ +#include <mntent.h> +#include <sys/statvfs.h> +#endif /* __linux__ */ +#include "status2d.h" + +#define MAXDISKS 64 + +int +main(void) { + char *names[MAXDISKS]; + int percent[MAXDISKS]; + int i; + + memset(names, 0, sizeof(names)); + memset(percent, 0, sizeof(percent)); +#ifdef __linux__ + unsigned long long used, total; + struct mntent *mnt; + struct statvfs fst; + FILE *f; + char *tmp, *p; + int j; + + f = setmntent("/etc/mtab", "r"); + i = 0; + while ((mnt = getmntent(f))) { + if (strchr(mnt->mnt_fsname, '/')) { + if (statvfs(mnt->mnt_dir, &fst) == -1) + continue; + used = fst.f_blocks - fst.f_bfree; + total = used + fst.f_bavail; + percent[i] = 100 * used / total; + tmp = mnt->mnt_fsname; + if (*tmp == '/') + tmp++; + while (p = strchr(tmp, '/')) + tmp = p + 1; + names[i] = strdup(tmp); + i++; + } + } + endmntent(f); +#else + s2d_print("no handler for this OS"); + s2d_finish(); +#endif + for (i = 0; names[i] && i < MAXDISKS; i++) { + s2d_bg(BRBG); + if (percent[i] >= 99) + s2d_fg(RED); + else if (percent[i] >= 90) + s2d_fg(ORANGE); + else if (percent[i] >= 80) + s2d_fg(YELLOW); + else + s2d_fg(GREEN); + s2d_bar(0, 2, 3, bar_height - 4, 1, percent[i]); + s2d_reset(1, 1); + s2d_forward(-1); + s2d_forward(2); + s2d_print("%s%s", names[i], names[i+1] ? " " : ""); + free(names[i]); + } +} diff --git a/dwmbar.h b/dwmbar.h @@ -1,28 +0,0 @@ -#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" -#define BLACK "#000000" -#define GREY "#888888" -#define YELLOW "#AAAA00" -#define WHITE "#CCCCCC" - -/* 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(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(unsigned x, unsigned y, unsigned w, unsigned h, unsigned px, unsigned percent); -void s2d_divider(void); -void s2d_fg(char *fg); -void s2d_bg(char *bg); -void s2d_forward(int px); /* negative to advance past drawn objects */ -void s2d_print(char *fmt, ...); diff --git a/handlers/bat.c b/handlers/bat.c @@ -1,108 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <assert.h> -#include <dirent.h> -#include <string.h> -#include <sys/stat.h> -#include "../dwmbar.h" - -int -main(void) { - enum { s_none, s_charge, s_dcharge }; - int percent, status = s_none, w, iw, ih; -#ifdef __linux__ -#define BATDIR "/sys/class/power_supply" - struct dirent **dirent; - struct stat st; - char buf[BUFSIZ]; - char *dir = NULL; - char *sfile = NULL; - char *cfile = NULL; - size_t len; - FILE *f; - int nbats, ndir, total; - int i; - - if ((ndir = scandir(BATDIR, &dirent, 0, alphasort)) < 0) { - return 1; - } - - for (i = total = nbats = 0; i < ndir; i++) { - if (strncmp(dirent[i]->d_name, "BAT", strlen("BAT")) == 0) { - free(dir); - len = strlen(BATDIR) + strlen(dirent[i]->d_name) + 2; /* / + \0 */ - dir = emalloc(len); - snprintf(dir, len, "%s/%s", BATDIR, dirent[i]->d_name); - - if (stat(dir, &st) == -1 || !S_ISDIR(st.st_mode)) - continue; - - free(sfile); - len = strlen(dir) + 8; /* /status\0 */ - sfile = emalloc(len); - snprintf(sfile, len, "%s/status", dir); - if ((f = fopen(sfile, "r")) != NULL) { - memset(buf, '\0', sizeof(buf)); - fread(buf, sizeof(char), sizeof(buf), f); - fclose(f); - if (buf[strlen(buf) - 1] == '\n') - buf[strlen(buf) - 1] = '\0'; - /* XXX: some batteries charging, some discharging? */ - if (strcmp(buf, "Charging") == 0 && s_charge > status) - status = s_charge; - else if (strcmp(buf, "Discharging") == 0 && s_dcharge > status) - status = s_dcharge; - } - - free(cfile); - len = strlen(dir) + 10; /* /capacity\0 */ - cfile = emalloc(len); - snprintf(cfile, len, "%s/capacity", dir); - if ((f = fopen(cfile, "r")) != NULL) { - memset(buf, '\0', sizeof(buf)); - fread(buf, sizeof(char), sizeof(buf), f); - fclose(f); - if (buf[strlen(buf) - 1] == '\n') - buf[strlen(buf) - 1] = '\0'; - nbats++; - total += atoi(buf); - } - } - } - - percent = total / nbats; -#else - s2d_print("no handler for this OS"); - s2d_finish(); - return 1; -#endif - iw = 18; - ih = bar_height - 8; - w = iw * percent / 100; - - /* colour border+nose for charge indicator */ - if (status == s_dcharge) - s2d_fg(RED); - else if (status == s_charge) - s2d_fg(GREEN); - - /* draw nose */ - s2d_rect(2, (bar_height - 5) / 2, 1, 5); - - /* draw border for battery */ - s2d_border(3, 3, iw + 2, ih + 2, 1); - - /* fill battery */ - s2d_fg(BRBG); - s2d_rect(4, 4, iw, ih); - if (percent < 25) - s2d_fg(RED); - else - s2d_reset(1, 0); - s2d_rect(4 + iw - w, 4, w, ih); - - /* pad + finish up */ - s2d_forward(2); - s2d_finish(); - return 0; -} diff --git a/handlers/cpu.c b/handlers/cpu.c @@ -1,146 +0,0 @@ -#include <stdio.h> -#include <errno.h> -#include <ctype.h> -#include <string.h> -#include <stdlib.h> -#include <unistd.h> -#include <sys/stat.h> -#include "../dwmbar.h" - -#define MAXCORES 1024 /* moore's law, man */ - -#ifdef __linux__ -#define CPUTOKS 11 /* grep '^cpu' < /proc/stat | awk '{print NF}' */ -static FILE * -statopen(void) { - FILE *ret; - if ((ret = fopen("/proc/stat", "r")) == NULL) { - s2d_print("%s: %s", strerror(errno)); - s2d_finish(); - exit(EXIT_FAILURE); - } - return ret; -} -#endif /* __linux__ */ - -int -main(void) { - int percent[MAXCORES]; - int avgtemp; - int cores = 0; - char *col; - memset(percent, 0, sizeof(percent)); - -#ifdef __linux__ - long long unsigned total[MAXCORES]; - long long unsigned idle[MAXCORES]; - long long unsigned ltotal, lidle, used, diff; - struct stat st; - FILE *f; - char buf[BUFSIZ]; - char *toks[CPUTOKS]; - char *p; - int i, j; - int temptotal, tempn; - - memset(total, 0, sizeof(total)); - memset(idle, 0, sizeof(idle)); - - f = statopen(); - while ((fgets(buf, sizeof(buf), f)) != NULL) { - buf[strlen(buf) - 1] = '\0'; /* remove \n */ - if (strncmp(buf, "cpu", 3) == 0 && isdigit(*(buf+3))) { - for (j = 0; j < CPUTOKS; j++) - toks[j] = strtok(j ? NULL : buf, " "); - p = toks[0]; - p += 3; /* "cpu" */ - i = strtoll(p, NULL, 10); - if (i >= MAXCORES) { - s2d_print("MAXCORES exceeded"); - s2d_finish(); - return 1; - } - if (i + 1 > cores) - cores = i + 1; - for (j = 1; j < CPUTOKS; j++) - total[i] += strtoll(toks[j], NULL, 10); - idle[i] = strtoll(toks[4], NULL, 10); - } - } - fclose(f); - - sleep(1); - - f = statopen(); - while ((fgets(buf, sizeof(buf), f)) != NULL) { - buf[strlen(buf) - 1] = '\0'; - if (strncmp(buf, "cpu", 3) == 0 && isdigit(*(buf+3))) { - for (j = 0; j < CPUTOKS; j++) - toks[j] = strtok(j ? NULL : buf, " "); - p = toks[0]; - p += 3; /* "cpu" */ - i = strtoll(p, NULL, 10); - if (i >= MAXCORES) { - s2d_print("MAXCORES exceeded"); - s2d_finish(); - return 1; - } - if (i + 1 > cores) - cores = i + 1; - for (j = 1, ltotal = 0; j < CPUTOKS; j++) - ltotal += strtoll(toks[j], NULL, 10); - diff = ltotal - total[i]; - lidle = strtoll(toks[4], NULL, 10) - idle[i]; - used = 100 * (diff - lidle); - percent[i] = used / diff; - } - } - fclose(f); - - 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; - if (!S_ISREG(st.st_mode)) - continue; - if ((f = fopen(buf, "r")) == NULL) - continue; - fread(buf, sizeof(char), BUFSIZ, f); - buf[5] = '\0'; - 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++) { - s2d_bg(BRBG); - if (percent[i] >= 80) - s2d_fg(RED); - else if (percent[i] >= 65) - s2d_fg(ORANGE); - else if (percent[i] >= 50) - s2d_fg(YELLOW); - else - s2d_fg(GREEN); - 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 - s2d_reset(1, 1); - s2d_print("%d°", avgtemp); - } - s2d_finish(); -} diff --git a/handlers/date.c b/handlers/date.c @@ -1,32 +0,0 @@ -#include <stdio.h> -#include <ctype.h> -#include <time.h> -#include "../dwmbar.h" - -int -main(void) { - char buf[BUFSIZ], *p; - time_t t = time(NULL); - struct tm *tm = localtime(&t); - - strftime(buf, sizeof(buf), "%a", tm); - for (p = buf; *p; p++) - *p = toupper(*p); - if (tm->tm_wday == 0 || tm->tm_wday == 1) /* weekend */ - s2d_fg(GREEN); - else - s2d_fg(GREY); - s2d_print("%s", buf); - - strftime(buf, sizeof(buf), "%d/%m", tm); - if (tm->tm_mon >= 3 && tm->tm_mon <= 5) - s2d_fg(YELLOW); /* spring */ - else if (tm->tm_mon >= 6 && tm->tm_mon <= 7) - s2d_fg(GREEN); /* summer */ - else if (tm->tm_mon >= 8 && tm->tm_mon <= 11) - s2d_fg(ORANGE); /* autumn */ - else - s2d_fg(WHITE); /* winter */ - s2d_print("%s", buf); - s2d_finish(); -} diff --git a/handlers/mem.c b/handlers/mem.c @@ -1,53 +0,0 @@ -#include <stdio.h> -#include <ctype.h> -#include <string.h> -#include <stdlib.h> -#include "../dwmbar.h" - -#define CONSTLEN(str) ((size_t)((sizeof(str) - sizeof(str[0])) / sizeof(str[0]))) - -int -main(void) { - long double total = 0, used = 0, percent = 0; -#ifdef __linux__ - long double avail = 0; - char buf[1024], *p; - FILE *f; - - if (!(f = fopen("/proc/meminfo", "r"))) { - perror("fopen()"); - return 1; - } - - while (fgets(buf, sizeof(buf), f) && (!avail || !total)) { - for (p = buf; !isdigit(*p) && *p; p++); - if (strncmp(buf, "MemTotal:", CONSTLEN("MemTotal:")) == 0) { - total = strtold(p, NULL); - } else if (strncmp(buf, "MemAvailable:", CONSTLEN("MemAvailable:")) == 0) { - avail = strtold(p, NULL); - } - } - fclose(f); - - used = total - avail; -#else - s2d_print("no handler for this OS"); - s2d_finish(); -#endif - percent = (used / total) * 100; - s2d_bg(BRBG); - if (percent >= 75) - s2d_fg(RED); - else if (percent >= 60) - s2d_fg(ORANGE); - else if (percent >= 50) - s2d_fg(YELLOW); - else - s2d_fg(GREEN); - s2d_bar(0, 2, 3, bar_height - 4, 1, percent); - s2d_reset(0, 1); - s2d_forward(-1); - s2d_forward(2); - s2d_print("%.0LF%%", percent); - s2d_finish(); -} diff --git a/handlers/time.c b/handlers/time.c @@ -1,14 +0,0 @@ -#include <stdio.h> -#include <time.h> -#include "../dwmbar.h" - -int -main(void) { - char buf[BUFSIZ]; - time_t t = time(NULL); - - strftime(buf, sizeof(buf), "%H:%M:%S", localtime(&t)); - s2d_print("%s", buf); - s2d_finish(); - return 0; -} diff --git a/handlers/vol.c b/handlers/vol.c @@ -1,102 +0,0 @@ -#include <stddef.h> -#ifdef __linux__ -#include <alsa/asoundlib.h> -#endif /* __linux__ */ -#include "../dwmbar.h" - -void -draw_on(void) { - s2d_rect(11, 3, 1, 3); - s2d_rect(2, 4, 3, 1); - s2d_rect(9, 4, 1, 2); - s2d_rect(1, 5, 1, 5); - s2d_rect(5, 5, 1, 5); - s2d_rect(7, 5, 1, 1); - s2d_rect(8, 6, 1, 3); - s2d_rect(10, 6, 1, 3); - s2d_rect(12, 6, 1, 3); - s2d_rect(7, 9, 1, 1); - s2d_rect(9, 9, 1, 2); - s2d_rect(11, 9, 1, 3); - s2d_rect(2, 10, 4, 1); -}; - -void -draw_off(void) { - s2d_rect(2, 4, 4, 1); - s2d_rect(1, 5, 1, 5); - s2d_rect(5, 5, 1, 5); - s2d_rect(7, 5, 1, 1); - s2d_rect(11, 5, 1, 1); - s2d_rect(8, 6, 1, 1); - s2d_rect(10, 6, 1, 1); - s2d_rect(9, 7, 1, 1); - s2d_rect(8, 8, 1, 1); - s2d_rect(10, 8, 1, 1); - s2d_rect(7, 9, 1, 1); - s2d_rect(11, 9, 1, 1); - s2d_rect(2, 10, 4, 1); -} - -int -main(void) { - int on = 0, percent = 0; - int ret; -#ifdef __linux__ - long min, max, vol; - snd_mixer_t *handle; - snd_mixer_elem_t *elem; - snd_mixer_selem_id_t *sid; - - if ((ret = snd_mixer_open(&handle, 0)) < 0) { - s2d_print("could not open mixer: %s\n", snd_strerror(ret)); - s2d_finish(); - return 1; - } - if ((ret = snd_mixer_attach(handle, "default")) < 0) { - s2d_print("could not attach mixer: %s\n", snd_strerror(ret)); - s2d_finish(); - snd_mixer_close(handle); - return 1; - } - if ((ret = snd_mixer_selem_register(handle, NULL, NULL)) < 0) { - s2d_print("could not register element: %s\n", snd_strerror(ret)); - s2d_finish(); - snd_mixer_close(handle); - return 1; - } - if ((ret = snd_mixer_load(handle)) < 0) { - s2d_print("could not load mixer: %s\n", snd_strerror(ret)); - s2d_finish(); - snd_mixer_close(handle); - return 1; - } - snd_mixer_selem_id_malloc(&sid); - snd_mixer_selem_id_set_index(sid, 0); - snd_mixer_selem_id_set_name(sid, "Master"); - if (!(elem = snd_mixer_find_selem(handle, sid))) { - s2d_print("could not find element 'Master'"); - s2d_finish(); - snd_mixer_close(handle); - return 1; - } - snd_mixer_selem_get_playback_volume_range(elem, &min, &max); - snd_mixer_selem_get_playback_volume(elem, 0, &vol); - snd_mixer_selem_get_playback_switch(elem, 0, &on); - percent = 100 * vol / max; - snd_mixer_selem_id_free(sid); - snd_mixer_close(handle); -#else - s2d_print("no handler for this OS"); - s2d_finish(); -#endif - if (on) - draw_on(); - else - draw_off(); - s2d_forward(-1); - s2d_forward(2); - s2d_bg(BRBG); - s2d_bar(0, 2, 3, bar_height - 4, 1, percent); - s2d_finish(); -} diff --git a/mem.c b/mem.c @@ -0,0 +1,53 @@ +#include <stdio.h> +#include <ctype.h> +#include <string.h> +#include <stdlib.h> +#include "status2d.h" + +#define CONSTLEN(str) ((size_t)((sizeof(str) - sizeof(str[0])) / sizeof(str[0]))) + +int +main(void) { + long double total = 0, used = 0, percent = 0; +#ifdef __linux__ + long double avail = 0; + char buf[1024], *p; + FILE *f; + + if (!(f = fopen("/proc/meminfo", "r"))) { + perror("fopen()"); + return 1; + } + + while (fgets(buf, sizeof(buf), f) && (!avail || !total)) { + for (p = buf; !isdigit(*p) && *p; p++); + if (strncmp(buf, "MemTotal:", CONSTLEN("MemTotal:")) == 0) { + total = strtold(p, NULL); + } else if (strncmp(buf, "MemAvailable:", CONSTLEN("MemAvailable:")) == 0) { + avail = strtold(p, NULL); + } + } + fclose(f); + + used = total - avail; +#else + s2d_print("no handler for this OS"); + s2d_finish(); +#endif + percent = (used / total) * 100; + s2d_bg(BRBG); + if (percent >= 75) + s2d_fg(RED); + else if (percent >= 60) + s2d_fg(ORANGE); + else if (percent >= 50) + s2d_fg(YELLOW); + else + s2d_fg(GREEN); + s2d_bar(0, 2, 3, bar_height - 4, 1, percent); + s2d_reset(0, 1); + s2d_forward(-1); + s2d_forward(2); + s2d_print("%.0LF%%", percent); + s2d_finish(); +} diff --git a/misc.c b/misc.c @@ -1,24 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include "dwmbar.h" - -void -bar_assert_(const char *aval, const char *file, int line, const char *func) { - printf("assertion failed (%s() at %s:%d): '%s'", func, file, line, aval); - exit(EXIT_FAILURE); -} - -void * -emalloc(size_t size) { - void *mem; - mem = malloc(size); - bar_assert(mem); - return mem; -} - -void * -erealloc(void *ptr, size_t size) { - ptr = realloc(ptr, size); - bar_assert(ptr); - return ptr; -} diff --git a/status2d.c b/status2d.c @@ -4,7 +4,7 @@ #include <stdarg.h> #include <stdlib.h> #include <unistd.h> -#include "dwmbar.h" +#include "status2d.h" static int needforwarding = 0; static int initialized = 0; diff --git a/status2d.h b/status2d.h @@ -0,0 +1,21 @@ +#define bar_height 15 + +#define RED "#AA2222" +#define ORANGE "#AA7700" +#define GREEN "#00AA00" +#define BRBG "#1A413A" +#define BLACK "#000000" +#define GREY "#888888" +#define YELLOW "#AAAA00" +#define WHITE "#CCCCCC" + +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(unsigned x, unsigned y, unsigned w, unsigned h, unsigned px, unsigned percent); +void s2d_divider(void); +void s2d_fg(char *fg); +void s2d_bg(char *bg); +void s2d_forward(int px); /* negative to advance past drawn objects */ +void s2d_print(char *fmt, ...); diff --git a/time.c b/time.c @@ -0,0 +1,14 @@ +#include <stdio.h> +#include <time.h> +#include "status2d.h" + +int +main(void) { + char buf[BUFSIZ]; + time_t t = time(NULL); + + strftime(buf, sizeof(buf), "%H:%M:%S", localtime(&t)); + s2d_print("%s", buf); + s2d_finish(); + return 0; +} diff --git a/vol.c b/vol.c @@ -0,0 +1,102 @@ +#include <stddef.h> +#ifdef __linux__ +#include <alsa/asoundlib.h> +#endif /* __linux__ */ +#include "status2d.h" + +void +draw_on(void) { + s2d_rect(11, 3, 1, 3); + s2d_rect(2, 4, 3, 1); + s2d_rect(9, 4, 1, 2); + s2d_rect(1, 5, 1, 5); + s2d_rect(5, 5, 1, 5); + s2d_rect(7, 5, 1, 1); + s2d_rect(8, 6, 1, 3); + s2d_rect(10, 6, 1, 3); + s2d_rect(12, 6, 1, 3); + s2d_rect(7, 9, 1, 1); + s2d_rect(9, 9, 1, 2); + s2d_rect(11, 9, 1, 3); + s2d_rect(2, 10, 4, 1); +}; + +void +draw_off(void) { + s2d_rect(2, 4, 4, 1); + s2d_rect(1, 5, 1, 5); + s2d_rect(5, 5, 1, 5); + s2d_rect(7, 5, 1, 1); + s2d_rect(11, 5, 1, 1); + s2d_rect(8, 6, 1, 1); + s2d_rect(10, 6, 1, 1); + s2d_rect(9, 7, 1, 1); + s2d_rect(8, 8, 1, 1); + s2d_rect(10, 8, 1, 1); + s2d_rect(7, 9, 1, 1); + s2d_rect(11, 9, 1, 1); + s2d_rect(2, 10, 4, 1); +} + +int +main(void) { + int on = 0, percent = 0; + int ret; +#ifdef __linux__ + long min, max, vol; + snd_mixer_t *handle; + snd_mixer_elem_t *elem; + snd_mixer_selem_id_t *sid; + + if ((ret = snd_mixer_open(&handle, 0)) < 0) { + s2d_print("could not open mixer: %s\n", snd_strerror(ret)); + s2d_finish(); + return 1; + } + if ((ret = snd_mixer_attach(handle, "default")) < 0) { + s2d_print("could not attach mixer: %s\n", snd_strerror(ret)); + s2d_finish(); + snd_mixer_close(handle); + return 1; + } + if ((ret = snd_mixer_selem_register(handle, NULL, NULL)) < 0) { + s2d_print("could not register element: %s\n", snd_strerror(ret)); + s2d_finish(); + snd_mixer_close(handle); + return 1; + } + if ((ret = snd_mixer_load(handle)) < 0) { + s2d_print("could not load mixer: %s\n", snd_strerror(ret)); + s2d_finish(); + snd_mixer_close(handle); + return 1; + } + snd_mixer_selem_id_malloc(&sid); + snd_mixer_selem_id_set_index(sid, 0); + snd_mixer_selem_id_set_name(sid, "Master"); + if (!(elem = snd_mixer_find_selem(handle, sid))) { + s2d_print("could not find element 'Master'"); + s2d_finish(); + snd_mixer_close(handle); + return 1; + } + snd_mixer_selem_get_playback_volume_range(elem, &min, &max); + snd_mixer_selem_get_playback_volume(elem, 0, &vol); + snd_mixer_selem_get_playback_switch(elem, 0, &on); + percent = 100 * vol / max; + snd_mixer_selem_id_free(sid); + snd_mixer_close(handle); +#else + s2d_print("no handler for this OS"); + s2d_finish(); +#endif + if (on) + draw_on(); + else + draw_off(); + s2d_forward(-1); + s2d_forward(2); + s2d_bg(BRBG); + s2d_bar(0, 2, 3, bar_height - 4, 1, percent); + s2d_finish(); +}