Skip to content

Instantly share code, notes, and snippets.

@torrinfail
Created August 12, 2020 03:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save torrinfail/fb423345f0f9c340db25181c8159cdd2 to your computer and use it in GitHub Desktop.
Save torrinfail/fb423345f0f9c340db25181c8159cdd2 to your computer and use it in GitHub Desktop.
diff --git a/dwmblocks.c b/dwmblocks.c
index 2f3b774..ba7dbb5 100644
--- a/dwmblocks.c
+++ b/dwmblocks.c
@@ -14,6 +14,7 @@ typedef struct {
unsigned int signal;
} Block;
void sighandler(int num);
+void buttonhandler(int sig, siginfo_t *si, void *ucontext);
void getcmds(int time);
#ifndef __OpenBSD__
void getsigcmds(int signal);
@@ -33,15 +34,32 @@ static int screen;
static Window root;
static char statusbar[LENGTH(blocks)][CMDLENGTH] = {0};
static char statusstr[2][256];
+static char button[] = "\0";
static int statusContinue = 1;
static void (*writestatus) () = setroot;
//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);
char *cmd = block->command;
- FILE *cmdf = popen(cmd,"r");
+ FILE *cmdf;
+ if (*button)
+ {
+ setenv("BUTTON", button, 1);
+ cmdf = popen(cmd,"r");
+ *button = '\0';
+ unsetenv("BUTTON");
+ }
+ else
+ {
+ cmdf = popen(cmd,"r");
+ }
if (!cmdf)
return;
char c;
@@ -79,12 +97,18 @@ void getsigcmds(int signal)
void setupsignals()
{
+ struct sigaction sa;
for(int i = 0; i < LENGTH(blocks); i++)
{
if (blocks[i].signal > 0)
+ {
signal(SIGRTMIN+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);
}
#endif
@@ -143,6 +167,13 @@ void sighandler(int signum)
getsigcmds(signum-SIGRTMIN);
writestatus();
}
+
+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 termhandler(int signum)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment