wmsl

Block-based window manager status line
git clone https://git.sinitax.com/sinitax/wmsl
Log | Files | Refs | README | LICENSE | sfeed.txt

commit fb3db89d1481cf2b191141dd17a3575b250d308c
parent 56ed910a7897a47352e9232e357b0f4305a762db
Author: Louis Burda <quent.burda@gmail.com>
Date:   Sun,  2 Jan 2022 13:54:27 +0100

Refactor for readability

Diffstat:
MMakefile | 11+++++++----
Mcontrib/event.diff | 4++--
Mcontrib/prefix-suffix.diff | 4++--
Msigwmsl.c | 84++++++++++++++++++++++++++++++++++++++++---------------------------------------
Mwmsl.c | 80++++++++++++++++++++++++++++++++++++++++---------------------------------------
5 files changed, 95 insertions(+), 88 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,15 +1,18 @@ CFLAGS = -I. LDLIBS = -lX11 -lm -DESTDIR ?= / -BINDIR ?= /usr/bin +DESTDIR ?= +BINDIR ?= usr/bin + +.PHONY: all install uninstall all: wmsl sigwmsl install: - cp sigwmsl wmsl $(DESTDIR)/$(BINDIR) + install -m 755 sigwmsl wmsl "$(DESTDIR)/$(BINDIR)" uninstall: - rm $(DESTDIR)/$(BINDIR) + rm "$(DESTDIR)/$(BINDIR)/wmsl" + rm "$(DESTDIR)/$(BINDIR)/sigwmsl" blocks.h: blocks.def.h cp blocks.def.h blocks.h diff --git a/contrib/event.diff b/contrib/event.diff @@ -26,9 +26,9 @@ break; } } -@@ -104,6 +107,10 @@ update_blocks() +@@ -104,6 +107,10 @@ update_blocks(void) if ((blocks[i].flags & READY) || - blocks[i].sleep_max && blocks[i].sleep_left <= 0.5) { + blocks[i].sleep_max && blocks[i].sleep_left <= 0.2) { blocks[i].sleep_left = blocks[i].sleep_max; + if (blocks[i].flags & EVENT) + setenv("WMSLEVENT", "1", 1); diff --git a/contrib/prefix-suffix.diff b/contrib/prefix-suffix.diff @@ -4,11 +4,11 @@ const char *command; unsigned int id, sleep_max; int flags; -+ const char *prefix, *suffix; ++ const char *prefix, *suffix; float sleep_left; char output[OUTPUTMAX]; }; -@@ -135,7 +136,11 @@ update_blocks() +@@ -135,7 +136,11 @@ update_blocks(void) if (output_len) { if (status_len > 0) concat_status(delim, ARRSIZE(delim) - 1); diff --git a/sigwmsl.c b/sigwmsl.c @@ -4,55 +4,57 @@ #include <stdlib.h> #include <errno.h> -void die(int rc, const char *errstr, ...); +void die(const char *errstr, ...); -const char* pid_file = "/tmp/.wmsl-pid"; +const char* pid_file = "/tmp/.wmsl.pid"; void -die(int rc, const char *errstr, ...) +die(const char *errstr, ...) { - va_list ap; + va_list ap; - va_start(ap, errstr); - vprintf(errstr, ap); - va_end(ap); - exit(rc); + va_start(ap, errstr); + vprintf(errstr, ap); + va_end(ap); + + exit(1); } int main(int argc, const char **argv) { - FILE *f; - char buffer[64]; - pid_t pid; - sigval_t sigval; - int block_id; - - if (argc == 1) - die(EXIT_SUCCESS, "USAGE: sigwmsl <block-id>\n"); - - block_id = atoi(argv[1]); - if (!block_id) - die(EXIT_FAILURE, "sigwmsl: invalid block id\n"); - - f = fopen(pid_file, "r"); - if (!f) - die(EXIT_FAILURE, "sigwmsl: Failed to open pid file %s\n", pid_file); - - if (!fgets(buffer, 64, f)) - die(EXIT_FAILURE, "sigwmsl: PID file is empty\n"); - - pid = atoi(buffer); - - sigval.sival_int = block_id; - if (sigqueue(pid, SIGALRM, sigval) == -1) { - switch (errno) { - case EINVAL: - die(EXIT_FAILURE, "sigwmsl: invalid signal value\n"); - case ESRCH: - die(EXIT_FAILURE, "sigwmsl: wmsl is not running\n"); - } - } - - fclose(f); + FILE *f; + char buffer[64]; + pid_t pid; + sigval_t sigval; + int block_id; + + if (argc != 2) { + printf("USAGE: sigwmsl <block-id>\n"); + return EXIT_SUCCESS; + } + + block_id = atoi(argv[1]); + if (!block_id) + die("sigwmsl: invalid block id\n"); + + f = fopen(pid_file, "r"); + if (!f) die("sigwmsl: Failed to open pid file %s\n", pid_file); + + if (!fgets(buffer, 64, f)) + die("sigwmsl: PID file is empty\n"); + + pid = atoi(buffer); + + sigval.sival_int = block_id; + if (sigqueue(pid, SIGALRM, sigval) == -1) { + switch (errno) { + case EINVAL: + die("sigwmsl: invalid signal value\n"); + case ESRCH: + die("sigwmsl: wmsl is not running\n"); + } + } + + fclose(f); } diff --git a/wmsl.c b/wmsl.c @@ -26,20 +26,22 @@ struct block { char output[OUTPUTMAX]; }; -static void die(int rc, const char *errstr, ...); +static void die(const char *fmtstr, ...); static void signal_handler(int sig, siginfo_t *info, void *context); static void concat_status(const char *text, size_t len); -static void update_blocks(); -static void sleep_till_next(); -static float get_elapsed(); +static void update_blocks(void); +static void sleep_till_next(void); +static float get_elapsed(void); static void set_status_var(const char *str); -static void clean_status(); +static void clean_status(void); static void debug(const char *fmtstr, ...); -static const char* pid_file = "/tmp/.wmsl-pid"; +static const char *usage = "USAGE: wmsl [-h] [-v]\n"; +static const char *pid_file = "/tmp/.wmsl.pid"; + static char status_text[STATUSMAX]; static float last_sleep; -static Display* dpy; +static Display *dpy; static Window root; static size_t status_len; static int verbose; @@ -48,14 +50,15 @@ static pid_t pid; #include "blocks.h" void -die(int rc, const char *errstr, ...) +die(const char *fmtstr, ...) { va_list ap; - va_start(ap, errstr); - vfprintf(stderr, errstr, ap); + va_start(ap, fmtstr); + vfprintf(stderr, fmtstr, ap); va_end(ap); - exit(rc); + + exit(EXIT_FAILURE); } void @@ -89,7 +92,7 @@ concat_status(const char *text, size_t len) } void -update_blocks() +update_blocks(void) { FILE *cmd; int i, output_len, update; @@ -103,14 +106,13 @@ update_blocks() concat_status(prefix, ARRSIZE(prefix) - 1); for (status_len = update = i = 0; i < ARRSIZE(blocks); i++) { if ((blocks[i].flags & READY) || - blocks[i].sleep_max && blocks[i].sleep_left <= 0.5) { + blocks[i].sleep_max && blocks[i].sleep_left <= 0.2) { blocks[i].sleep_left = blocks[i].sleep_max; blocks[i].flags = IDLE; /* get command output */ cmd = popen(blocks[i].command, "r"); - if (!cmd) - continue; + if (!cmd) continue; if (!fgets(tmp_output, OUTPUTMAX, cmd)) { output_len = 0; @@ -147,7 +149,7 @@ update_blocks() } void -sleep_till_next() +sleep_till_next(void) { int i, minsleep; @@ -173,14 +175,14 @@ sleep_till_next() } float -get_elapsed() +get_elapsed(void) { static struct timespec last = { .tv_nsec = -1 }; struct timespec cur; float diff; if (clock_gettime(CLOCK_MONOTONIC, &cur) == -1) - die(EXIT_FAILURE, "wmsl: failed to get time via clock()\n"); + die("wmsl: failed to get time via clock()\n"); if (last.tv_nsec == -1) diff = 0; @@ -200,34 +202,32 @@ set_status_var(const char *str) } void -clean_status() +clean_status(void) { set_status_var(""); - exit(EXIT_FAILURE); } void debug(const char *fmtstr, ...) { - char buf[9], *fmtalloc; - time_t epoch; + char buf[12]; struct tm *tm; + time_t sec; va_list ap; - int len; + int len; if (!verbose) return; - time(&epoch); - tm = localtime(&epoch); + time(&sec); + tm = localtime(&sec); + + strftime(buf, sizeof(buf), "[%T] ", tm); + printf("%s", buf); va_start(ap, fmtstr); - fmtalloc = malloc(strlen(fmtstr) + 12); - strftime(fmtalloc, 12, "[%T] ", tm); - strcpy(fmtalloc + 11, fmtstr); - vprintf(fmtalloc, ap); + vprintf(fmtstr, ap); va_end(ap); - free(fmtalloc); } int @@ -235,7 +235,7 @@ main(int argc, const char **argv) { struct sigaction sa; int i, screen, sleep; - FILE* f; + FILE *f; for (i = 1; i < argc; i++) { if (argv[i][0] == '-') @@ -244,15 +244,16 @@ main(int argc, const char **argv) verbose = 1; break; case 'h': + printf("%s", usage); + return 0; default: - die(EXIT_SUCCESS, "USAGE: wmsl [-h] [-v]\n"); + die("%s", usage); } } /* get root window */ dpy = XOpenDisplay(NULL); - if (!dpy) - die(EXIT_FAILURE, "wmsl: Failed to open display\n"); + if (!dpy) die("wmsl: Failed to open display\n"); screen = DefaultScreen(dpy); root = RootWindow(dpy, screen); @@ -262,16 +263,16 @@ main(int argc, const char **argv) sa.sa_sigaction = signal_handler; sa.sa_flags = SA_RESTART | SA_SIGINFO; if (sigaction(SIGALRM, &sa, NULL) == -1) - die(EXIT_FAILURE, "wmsl: Failed to setup signal handler (SIGALRM)\n"); + die("wmsl: Failed to setup signal handler (SIGALRM)\n"); - signal(SIGINT, clean_status); - signal(SIGTERM, clean_status); + atexit(clean_status); + signal(SIGINT, exit); + signal(SIGTERM, exit); /* save pid */ pid = getpid(); f = fopen(pid_file, "w+"); - if (!f) - die(EXIT_FAILURE, "wmsl: Failed to open pid file %s\n", pid_file); + if (!f) die("wmsl: Failed to open pid file %s\n", pid_file); fprintf(f, "%i", pid); fclose(f); @@ -295,3 +296,4 @@ main(int argc, const char **argv) } } } +