Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
diff -up a/dwmblocks.c b/dwmblocks.c
--- a/dwmblocks.c 2020-12-22 14:25:05.289247774 +0200
+++ b/dwmblocks.c 2020-12-22 14:29:39.744943965 +0200
@@ -28,6 +28,7 @@ typedef struct {
void dummysighandler(int num);
#endif
void sighandler(int num);
+void buttonhandler(int sig, siginfo_t *si, void *ucontext);
void getcmds(int time);
void getsigcmds(unsigned int signal);
void setupsignals();
@@ -52,14 +53,31 @@ static void (*writestatus) () = pstdout;
static char statusbar[LENGTH(blocks)][CMDLENGTH] = {0};
static char statusstr[2][STATUSLENGTH];
+static char button[] = "\0";
static int statusContinue = 1;
static int returnStatus = 0;
//opens process *cmd and stores output in *output
void getcmd(const Block *block, char *output)
{
+ if (block->signal)
+ {
+ output[0] = block->signal;
+ output++;
+ }
strcpy(output, block->icon);
- FILE *cmdf = popen(block->command, "r");
+ FILE *cmdf;
+ if (*button)
+ {
+ setenv("BUTTON", button, 1);
+ cmdf = popen(block->command,"r");
+ *button = '\0';
+ unsetenv("BUTTON");
+ }
+ else
+ {
+ cmdf = popen(block->command,"r");
+ }
if (!cmdf)
return;
int i = strlen(block->icon);
@@ -105,9 +123,15 @@ void setupsignals()
signal(i, dummysighandler);
#endif
+ struct sigaction sa;
for (unsigned int i = 0; i < LENGTH(blocks); i++) {
- if (blocks[i].signal > 0)
+ if (blocks[i].signal > 0) {
signal(SIGMINUS+blocks[i].signal, sighandler);
+ sigaddset(&sa.sa_mask, SIGRTMIN+blocks[i].signal); // ignore signal when handling SIGUSR1
+ }
+ sa.sa_sigaction = buttonhandler;
+ sa.sa_flags = SA_SIGINFO;
+ sigaction(SIGUSR1, &sa, NULL);
}
}
@@ -173,6 +197,13 @@ void dummysighandler(int signum)
{
return;
}
+
+void buttonhandler(int sig, siginfo_t *si, void *ucontext)
+{
+ *button = '0' + si->si_value.sival_int & 0xff;
+ getsigcmds(si->si_value.sival_int >> 8);
+ writestatus();
+}
#endif
void sighandler(int signum)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment