summaryrefslogtreecommitdiff
path: root/dwm/patches/dwmblocks-statuscmd-20210402-96cbb45.diff
blob: 982f93a722522b0022fc374bb5bba4371b731cdd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# From 1669878c08607f481e3f879d6914fc4d3c9d7206 Mon Sep 17 00:00:00 2001
# From: Daniel Bylinka <daniel.bylinka@gmail.com>
# Date: Fri, 2 Apr 2021 19:20:17 +0200
# Subject: [PATCH] [statuscmd] Format status text and handle button signals
#
# ---
#  dwmblocks.c | 48 +++++++++++++++++++++++++++++++++++++++++-------
#  1 file changed, 41 insertions(+), 7 deletions(-)

diff --git a/dwmblocks.c b/dwmblocks.c
index ded717c..78fdeb5 100644
--- 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);
--
2.31.0