dwmblocks

My build of dwmblocks.
git clone git://git.alex.balgavy.eu/dwmblocks.git
Log | Files | Refs | README | LICENSE

commit 232d90a40a762ec0f7118ee46bbf2983e5543225
parent 96cbb453e5373c05372fd4bf3faacfa53e409067
Author: Alex Balgavy <alex@balgavy.eu>
Date:   Tue, 20 Apr 2021 14:39:02 +0200

Clickable patch

Diffstat:
M.gitignore | 3---
Ablocks.h | 10++++++++++
Mdwmblocks.c | 48+++++++++++++++++++++++++++++++++++++++++-------
3 files changed, 51 insertions(+), 10 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1,6 +1,3 @@ -# Custom blocks file -blocks.h - # Prerequisites *.d diff --git a/blocks.h b/blocks.h @@ -0,0 +1,10 @@ +//Modify this file to change what commands output to your statusbar, and recompile using the make command. +static const Block blocks[] = { + /*Icon*/ /*Command*/ /*Update Interval*/ /*Update Signal*/ + {"🔋 ", "printf '%s%%' $(battery | head -n 1)", 60, 0}, + {"", "date '+%b %d (%a) %I:%M%p'", 60, 0}, +}; + +//sets delimeter between status commands. NULL character ('\0') means no delimeter. +static char delim[] = " | "; +static unsigned int delimLen = 5; diff --git a/dwmblocks.c b/dwmblocks.c @@ -3,6 +3,7 @@ #include<string.h> #include<unistd.h> #include<signal.h> +#include<sys/wait.h> #ifndef NO_X #include<X11/Xlib.h> #endif @@ -27,14 +28,14 @@ typedef struct { #ifndef __OpenBSD__ void dummysighandler(int num); #endif -void sighandler(int num); void getcmds(int time); void getsigcmds(unsigned int signal); void setupsignals(); -void sighandler(int signum); +void sighandler(int signum, siginfo_t *si, void *ucontext); int getstatus(char *str, char *last); void statusloop(); void termhandler(); +void chldhandler(); void pstdout(); #ifndef NO_X void setroot(); @@ -58,6 +59,8 @@ static int returnStatus = 0; //opens process *cmd and stores output in *output void getcmd(const Block *block, char *output) { + if (block->signal) + *output++ = block->signal; strcpy(output, block->icon); FILE *cmdf = popen(block->command, "r"); if (!cmdf) @@ -102,15 +105,18 @@ void getsigcmds(unsigned int signal) void setupsignals() { + struct sigaction sa = { .sa_sigaction = sighandler, .sa_flags = SA_SIGINFO }; #ifndef __OpenBSD__ /* initialize all real time signals with dummy handler */ - for (int i = SIGRTMIN; i <= SIGRTMAX; i++) + for (int i = SIGRTMIN; i <= SIGRTMAX; i++) { signal(i, dummysighandler); + sigaddset(&sa.sa_mask, i); + } #endif for (unsigned int i = 0; i < LENGTH(blocks); i++) { if (blocks[i].signal > 0) - signal(SIGMINUS+blocks[i].signal, sighandler); + sigaction(SIGMINUS+blocks[i].signal, &sa, NULL); } } @@ -178,10 +184,32 @@ void dummysighandler(int signum) } #endif -void sighandler(int signum) +void sighandler(int signum, siginfo_t *si, void *ucontext) { - getsigcmds(signum-SIGPLUS); - writestatus(); + if (si->si_value.sival_int) { + pid_t parent = getpid(); + if (fork() == 0) { +#ifndef NO_X + if (dpy) + close(ConnectionNumber(dpy)); +#endif + int i; + for (i = 0; i < LENGTH(blocks) && blocks[i].signal != signum-SIGRTMIN; i++); + + char shcmd[1024]; + sprintf(shcmd, "%s; kill -%d %d", blocks[i].command, SIGRTMIN+blocks[i].signal, parent); + char *cmd[] = { "/bin/sh", "-c", shcmd, NULL }; + char button[2] = { '0' + si->si_value.sival_int, '\0' }; + setenv("BUTTON", button, 1); + setsid(); + execvp(cmd[0], cmd); + perror(cmd[0]); + exit(EXIT_SUCCESS); + } + } else { + getsigcmds(signum-SIGPLUS); + writestatus(); + } } void termhandler() @@ -189,6 +217,11 @@ void termhandler() statusContinue = 0; } +void chldhandler() +{ + while (0 < waitpid(-1, NULL, WNOHANG)); +} + int main(int argc, char** argv) { for (int i = 0; i < argc; i++) {//Handle command line arguments @@ -205,6 +238,7 @@ int main(int argc, char** argv) delim[delimLen++] = '\0'; signal(SIGTERM, termhandler); signal(SIGINT, termhandler); + signal(SIGCHLD, chldhandler); statusloop(); #ifndef NO_X XCloseDisplay(dpy);