wmsl

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

commit e5b3a31498323c834340e105c07bbc34015d82d2
parent 0ea9cf0946928745b0a9a7c0ada8fa4f1bfeccdf
Author: Louis Burda <quent.burda@gmail.com>
Date:   Tue, 29 Sep 2020 12:24:09 +0200

Remove patch original from tree

Diffstat:
Dwmsl.c.orig | 301-------------------------------------------------------------------------------
1 file changed, 0 insertions(+), 301 deletions(-)

diff --git a/wmsl.c.orig b/wmsl.c.orig @@ -1,301 +0,0 @@ -#include <stdlib.h> -#include <stdio.h> -#include <math.h> -#include <string.h> -#include <time.h> -#include <unistd.h> -#include <stdarg.h> -#include <signal.h> - -#include <X11/Xlib.h> - -#define ARRSIZE(x) (sizeof(x)/sizeof(x[0])) -#define OUTPUTMAX 256 -#define STATUSMAX 1024 - -enum block_state { - IDLE, - READY -}; - -struct block { - const char *command; - unsigned int id, sleep_max; - int flags; - const char *prefix, *suffix; - float sleep_left; - char output[OUTPUTMAX]; -}; - -static void die(int rc, const char *errstr, ...); -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 set_status_var(const char *str); -static void clean_status(); -static void debug(const char *fmtstr, ...); - -static const char* pid_file = "/tmp/.wmsl-pid"; -static char status_text[STATUSMAX]; -static float last_sleep; -static Display* dpy; -static Window root; -static size_t status_len; -static int verbose; -static pid_t pid; - -#include "blocks.h" - -void -die(int rc, const char *errstr, ...) -{ - va_list ap; - - va_start(ap, errstr); - vfprintf(stderr, errstr, ap); - va_end(ap); - exit(rc); -} - -void -signal_handler(int sig, siginfo_t *info, void *context) -{ - int i, block_id; - - block_id = info->si_value.sival_int; - if (!block_id || info->si_pid == pid) return; - - debug("received signal for block with id %i\n", block_id); - - for (i = 0; i < ARRSIZE(blocks); i++) { - if (blocks[i].id == block_id) { - blocks[i].flags |= READY; - break; - } - } -} - -void -concat_status(const char *text, size_t len) -{ - if (status_len + len + 1 > STATUSMAX) - die(EXIT_FAILURE, "wmsl: status line too large (>%i)\n", - STATUSMAX); - memcpy(&status_text[status_len], text, len); - status_len += len; - status_text[status_len] = '\0'; -} - -void -update_blocks() -{ - FILE *cmd; - int i, output_len, update; - char tmp_output[OUTPUTMAX]; - - debug("checking block output..\n"); - - memcpy(status_text, prefix, ARRSIZE(prefix) - 1); - - /* construct status string */ - 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_left = blocks[i].sleep_max; - blocks[i].flags = IDLE; - - /* get command output */ - cmd = popen(blocks[i].command, "r"); - if (!cmd) - continue; - - if (!fgets(tmp_output, OUTPUTMAX, cmd)) { - output_len = 0; - } else { - output_len = strlen(tmp_output); - if (tmp_output[output_len - 1] == '\n') - output_len -= 1; - } - - pclose(cmd); - - 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'; - debug("new output from cmd: %s\n", blocks[i].command); - update = 1; - } - } else { - output_len = strlen(blocks[i].output); - } - - if (output_len) { - if (status_len > 0) - concat_status(delim, ARRSIZE(delim) - 1); - if (blocks[i].prefix) - concat_status(blocks[i].prefix, strlen(blocks[i].prefix)); - concat_status(blocks[i].output, output_len); - if (blocks[i].suffix) - concat_status(blocks[i].suffix, strlen(blocks[i].suffix)); - } - } - if (!update) return; - - concat_status(suffix, ARRSIZE(suffix) - 1); - - set_status_var(status_text); -} - -void -sleep_till_next() -{ - int i, minsleep; - - last_sleep = 0; /* reset last_sleep in case we return early */ - 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_max && blocks[i].sleep_left < minsleep) - minsleep = ceil(blocks[i].sleep_left); - } - - if (minsleep < 1) - minsleep = 1; - - debug("sleeping for %i seconds\n", minsleep); - - alarm(minsleep); - pause(); - - last_sleep = get_elapsed(); - debug("slept for %f seconds\n", last_sleep); -} - -float -get_elapsed() -{ - 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"); - - if (last.tv_nsec == -1) - diff = 0; - else - diff = cur.tv_sec - last.tv_sec - + (cur.tv_nsec - last.tv_nsec) / 1000000000.f; - - last = cur; - return diff; -} - -void -set_status_var(const char *str) -{ - XStoreName(dpy, root, str); - XFlush(dpy); -} - -void -clean_status() -{ - set_status_var(""); - exit(EXIT_FAILURE); -} - -void -debug(const char *fmtstr, ...) -{ - char buf[9], *fmtalloc; - time_t epoch; - struct tm *tm; - va_list ap; - int len; - - if (!verbose) - return; - - time(&epoch); - tm = localtime(&epoch); - - va_start(ap, fmtstr); - fmtalloc = malloc(strlen(fmtstr) + 12); - strftime(fmtalloc, 12, "[%T] ", tm); - strcpy(fmtalloc + 11, fmtstr); - vprintf(fmtalloc, ap); - va_end(ap); - free(fmtalloc); -} - -int -main(int argc, const char **argv) -{ - struct sigaction sa; - int i, screen, sleep; - FILE* f; - - for (i = 1; i < argc; i++) { - if (argv[i][0] == '-') - switch (argv[i][1]) { - case 'v': - verbose = 1; - break; - case 'h': - default: - die(EXIT_SUCCESS, "USAGE: wmsl [-h] [-v]\n"); - } - } - - /* get root window */ - dpy = XOpenDisplay(NULL); - if (!dpy) - die(EXIT_FAILURE, "wmsl: Failed to open display\n"); - screen = DefaultScreen(dpy); - root = RootWindow(dpy, screen); - - /* setup signal handlers */ - sigemptyset(&sa.sa_mask); - sa.sa_handler = NULL; - 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"); - - signal(SIGINT, clean_status); - signal(SIGTERM, clean_status); - - /* save pid */ - pid = getpid(); - f = fopen(pid_file, "w+"); - if (!f) - die(EXIT_FAILURE, "wmsl: Failed to open pid file %s\n", pid_file); - fprintf(f, "%i", pid); - fclose(f); - - /* check if any blocks require timers */ - for (i = 0; i < ARRSIZE(blocks); i++) - if (blocks[i].sleep_max != 0) break; - - if (i != ARRSIZE(blocks)) { - 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 { - while (1) { - pause(); - update_blocks(); - } - } -}