wmsl

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

commit 25e671d35f2faec3fa59ac8354a5b895b5ef1eba
parent 0ddd6639021b2cc5d8ecce1bf36e0e2b0e3e328e
Author: Louis Burda <quent.burda@gmail.com>
Date:   Sun, 13 Sep 2020 01:30:08 +0200

Fix code style and fix block update

Diffstat:
MREADME | 4++++
Mwmsl.c | 77++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------
2 files changed, 60 insertions(+), 21 deletions(-)

diff --git a/README b/README @@ -35,3 +35,7 @@ Requirements ------------ Xlib header files for setting root window property WM_NAME + +Todo +---- +- add contrib patches for dwm (clickability, colors) diff --git a/wmsl.c b/wmsl.c @@ -32,6 +32,9 @@ 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 set_status_var(const char *str); +static void clean_status(); +static char* time_str(); static const char* pid_file = "/tmp/.wmsl-pid"; static char status_text[STATUSMAX]; @@ -64,7 +67,8 @@ signal_handler(int sig, siginfo_t *info, void *context) if (!block_id || info->si_pid == pid) return; if (verbose) - printf("received signal for block with id %i\n", block_id); + printf("[%s] received signal for block with id %i\n", + time_str(), block_id); for (i = 0; i < ARRSIZE(blocks); i++) { if (blocks[i].id == block_id) { @@ -77,7 +81,7 @@ signal_handler(int sig, siginfo_t *info, void *context) void concat_status(const char *text, size_t len) { - if (status_len + len + 1 > STATUSMAX) + if (status_len + len + 1 > STATUSMAX) die(EXIT_FAILURE, "wmsl: status line too large (>%i)\n", STATUSMAX); memcpy(&status_text[status_len], text, len); @@ -93,9 +97,9 @@ update_blocks() char tmp_output[OUTPUTMAX]; if (verbose) - printf("checking block output..\n"); + printf("[%s] checking block output..\n", time_str()); - memcpy(status_text, prefix, ARRSIZE(prefix) - 1); + memcpy(status_text, prefix, ARRSIZE(prefix) - 1); /* construct status string */ concat_status(prefix, ARRSIZE(prefix) - 1); @@ -120,13 +124,13 @@ update_blocks() pclose(cmd); - if (output_len == strlen(blocks[i].output) - && !strncmp(tmp_output, blocks[i].output, output_len)) { - continue; - } else { + if (output_len != strlen(blocks[i].output) + || strncmp(tmp_output, blocks[i].output, output_len)) { memcpy(blocks[i].output, tmp_output, output_len); + blocks[i].output[output_len] = '\0'; if (verbose) - printf("new output from cmd: %s\n", blocks[i].command); + printf("[%s] new output from cmd: %s\n", + time_str(), blocks[i].command); update = 1; } } else { @@ -140,12 +144,10 @@ update_blocks() } } if (!update) return; - concat_status(suffix, ARRSIZE(suffix) - 1); - status_text[status_len] = '\0'; + concat_status(suffix, ARRSIZE(suffix) - 1); - XStoreName(dpy, root, status_text); - XFlush(dpy); + set_status_var(status_text); } void @@ -154,12 +156,11 @@ sleep_till_next() int i, minsleep; last_sleep = 0; /* reset last_sleep in case we return early */ - for (i = minsleep = 0; i < ARRSIZE(blocks); i++) { - blocks[i].sleep_left -= last_sleep; + for (i = 0, minsleep = 0; i < ARRSIZE(blocks); i++) { if ((blocks[i].flags & READY) || blocks[i].sleep_max && blocks[i].sleep_left <= 0) return; - if (!minsleep || blocks[i].sleep_left < minsleep && blocks[i].sleep_max) + if (!minsleep || blocks[i].sleep_max && blocks[i].sleep_left < minsleep) minsleep = ceil(blocks[i].sleep_left); } @@ -167,14 +168,14 @@ sleep_till_next() minsleep = 1; if (verbose) - printf("sleeping for %i seconds\n", minsleep); + printf("[%s] sleeping for %i seconds\n", time_str(), minsleep); alarm(minsleep); pause(); last_sleep = get_elapsed(); if (verbose) - printf("slept for %f seconds\n", last_sleep); + printf("[%s] slept for %f seconds\n", time_str(), last_sleep); } float @@ -197,6 +198,33 @@ get_elapsed() return diff; } +void +set_status_var(const char *str) +{ + XStoreName(dpy, root, str); + XFlush(dpy); +} + +void +clean_status() +{ + set_status_var(""); + exit(EXIT_FAILURE); +} + +char* +time_str() +{ + static char buf[9]; + time_t epoch; + struct tm *tm; + + time(&epoch); + tm = localtime(&epoch); + strftime(buf, ARRSIZE(buf), "%T", tm); + return buf; +} + int main(int argc, const char **argv) { @@ -207,11 +235,12 @@ main(int argc, const char **argv) for (i = 1; i < argc; i++) { if (argv[i][0] == '-') switch (argv[i][1]) { - case 'h': - die(EXIT_SUCCESS, "USAGE: wmsl [-h] [-v]\n"); case 'v': verbose = 1; break; + case 'h': + default: + die(EXIT_SUCCESS, "USAGE: wmsl [-h] [-v]\n"); } } @@ -222,7 +251,7 @@ main(int argc, const char **argv) screen = DefaultScreen(dpy); root = RootWindow(dpy, screen); - /* setup signal handler */ + /* setup signal handlers */ sigemptyset(&sa.sa_mask); sa.sa_handler = NULL; sa.sa_sigaction = signal_handler; @@ -230,6 +259,9 @@ main(int argc, const char **argv) if (sigaction(SIGALRM, &sa, NULL) == -1) die(EXIT_FAILURE, "wmsl: Failed to setup signal handler (SIGALRM)\n"); + signal(SIGINT, clean_status); + signal(SIGTERM, clean_status); + /* save pid */ pid = getpid(); f = fopen(pid_file, "w+"); @@ -246,6 +278,9 @@ main(int argc, const char **argv) get_elapsed(); while (1) { sleep_till_next(); + for (i = 0; i < ARRSIZE(blocks); i++) + if (blocks[i].sleep_max) + blocks[i].sleep_left -= last_sleep; update_blocks(); } } else {