forked from ashish-yadav11/dwmblocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dwmblocks.c
363 lines (333 loc) · 11.6 KB
/
dwmblocks.c
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <X11/Xlib.h>
#define CMDLENGTH 50
#define STTLENGTH 256
#define NILL INT_MIN
#define LOCKFILE "/tmp/dwmblocks.pid"
typedef struct {
char *pathu;
char *pathc;
const int interval;
const int signal;
char cmdoutcur[CMDLENGTH];
char cmdoutprv[CMDLENGTH];
} Block;
#include "blocks.h"
static void buttonhandler(int signal, siginfo_t *si, void *ucontext);
static void cleanup();
static void getcmd(Block *block, int sigval);
static void setroot();
static void setupsignals();
static void sighandler(int signal, siginfo_t *si, void *ucontext);
static void statusloop();
static void termhandler(int signum);
static int updatestatus();
static void writepid();
static char statustext[STTLENGTH];
static size_t delimlength;
static Display *dpy;
static sigset_t blocksigmask;
void
buttonhandler(int signal, siginfo_t *si, void *ucontext)
{
signal = si->si_value.sival_int >> 8;
for (Block *block = blocks; block->pathu; block++)
if (block->signal == signal)
switch (fork()) {
case -1:
perror("buttonhandler - fork");
break;
case 0:
{
char button[] = { '0' + (si->si_value.sival_int & 0xff), '\0' };
char *arg[] = { block->pathc, button, NULL };
close(ConnectionNumber(dpy));
setsid();
execv(arg[0], arg);
perror("buttonhandler - child - execv");
_exit(127);
}
}
}
void
cleanup()
{
unlink(LOCKFILE);
XStoreName(dpy, DefaultRootWindow(dpy), "");
XCloseDisplay(dpy);
}
void
getcmd(Block *block, int sigval)
{
int fd[2];
if (pipe(fd) == -1) {
perror("getcmd - pipe");
exit(1);
}
switch (fork()) {
case -1:
perror("getcmd - fork");
exit(1);
case 0:
close(ConnectionNumber(dpy));
close(fd[0]);
if (fd[1] != STDOUT_FILENO) {
if (dup2(fd[1], STDOUT_FILENO) != STDOUT_FILENO) {
perror("getcmd - child - dup2");
exit(1);
}
close(fd[1]);
}
if (sigval == NILL) {
char *arg[] = { block->pathu, NULL };
execv(arg[0], arg);
} else {
char buf[12];
char *arg[] = { block->pathu, buf, NULL };
snprintf(buf, sizeof buf, "%d", sigval);
execv(arg[0], arg);
}
perror("getcmd - child - execv");
_exit(127);
default:
close(fd[1]);
if (read(fd[0], block->cmdoutcur, CMDLENGTH) == -1) {
perror("getcmd - read");
exit(1);
}
close(fd[0]);
}
}
void
setroot()
{
if (updatestatus()) {
XStoreName(dpy, DefaultRootWindow(dpy), statustext);
XSync(dpy, False);
}
}
void
setupsignals()
{
struct sigaction sa;
/* populate blocksigmask */
sigemptyset(&blocksigmask);
sigaddset(&blocksigmask, SIGHUP);
sigaddset(&blocksigmask, SIGINT);
sigaddset(&blocksigmask, SIGTERM);
for (Block *block = blocks; block->pathu; block++)
if (block->signal > 0)
sigaddset(&blocksigmask, SIGRTMIN + block->signal);
/* setup signal handlers */
/* to handle HUP, INT and TERM */
sa.sa_flags = SA_RESTART;
sigemptyset(&sa.sa_mask);
sa.sa_handler = termhandler;
sigaction(SIGHUP, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
/* to ignore unused realtime signals */
// sa.sa_flags = SA_RESTART;
// sigemptyset(&sa.sa_mask);
sa.sa_handler = SIG_IGN;
for (int i = SIGRTMIN + 1; i <= SIGRTMAX; i++)
sigaction(i, &sa, NULL);
/* to prevent forked children from becoming zombies */
sa.sa_flags = SA_NOCLDSTOP | SA_NOCLDWAIT | SA_RESTART;
// sigemptyset(&sa.sa_mask);
sa.sa_handler = SIG_DFL;
sigaction(SIGCHLD, &sa, NULL);
/* to handle signals generated by dwm on click events */
sa.sa_flags = SA_RESTART | SA_SIGINFO;
// sigemptyset(&sa.sa_mask);
sa.sa_sigaction = buttonhandler;
sigaction(SIGRTMIN, &sa, NULL);
/* to handle update signals for individual blocks */
sa.sa_flags |= SA_NODEFER;
sa.sa_mask = blocksigmask;
sa.sa_sigaction = sighandler;
for (Block *block = blocks; block->pathu; block++)
if (block->signal > 0)
sigaction(SIGRTMIN + block->signal, &sa, NULL);
}
void
sighandler(int signal, siginfo_t *si, void *ucontext)
{
signal -= SIGRTMIN;
for (Block *block = blocks; block->pathu; block++)
if (block->signal == signal)
getcmd(block, si->si_value.sival_int);
setroot();
}
void
statusloop()
{
int i;
/* first run */
sigprocmask(SIG_BLOCK, &blocksigmask, NULL);
for (Block *block = blocks; block->pathu; block++)
if (block->interval >= 0)
getcmd(block, NILL);
setroot();
sigprocmask(SIG_UNBLOCK, &blocksigmask, NULL);
sleep(SLEEPINTERVAL);
i = SLEEPINTERVAL;
/* main loop */
for (;; i += SLEEPINTERVAL) {
sigprocmask(SIG_BLOCK, &blocksigmask, NULL);
for (Block *block = blocks; block->pathu; block++)
if (block->interval > 0 && i % block->interval == 0)
getcmd(block, NILL);
setroot();
sigprocmask(SIG_UNBLOCK, &blocksigmask, NULL);
sleep(SLEEPINTERVAL);
}
}
void
termhandler(int signum)
{
cleanup();
exit(0);
}
/* returns whether block outputs have changed and updates statustext if they have */
int
updatestatus()
{
char *s = statustext;
char *c, *p; /* for cmdoutcur and cmdoutprv */
const char *d; /* for delimiter */
Block *block = blocks;
/* checking half of the function */
/* find the first non-empty block */
for (;; block++) {
/* all blocks are empty */
if (!block->pathu)
return 0;
/* contents of the block changed */
if (*block->cmdoutcur != *block->cmdoutprv)
goto update0;
/* skip delimiter handler for the first non-empty block */
if (*block->cmdoutcur != '\n' && *block->cmdoutcur != '\0')
goto skipdelimc;
}
/* main loop */
for (; block->pathu; block++) {
/* contents of the block changed */
if (*block->cmdoutcur != *block->cmdoutprv)
goto update1;
/* delimiter handler */
if (*block->cmdoutcur != '\n' && *block->cmdoutcur != '\0')
s += delimlength;
/* skip over empty blocks */
else
continue;
skipdelimc:
/* checking for the first byte has been done */
c = block->cmdoutcur + 1, p = block->cmdoutprv + 1;
for (; *c != '\n' && *c != '\0'; c++, p++)
/* contents of the block changed */
if (*c != *p) {
s += c - block->cmdoutcur;
goto update2;
}
s += c - block->cmdoutcur;
/* byte containing info about signal number for the block */
if (block->pathc && block->signal)
s++;
}
return 0;
/* updating half of the function */
/* find the first non-empty block */
for (;; block++) {
/* all blocks are empty */
if (!block->pathu)
return 1;
update0:
/* don't add delimiter before the first non-empty block */
if (*block->cmdoutcur != '\n' && *block->cmdoutcur != '\0')
goto skipdelimu;
*block->cmdoutprv = *block->cmdoutcur;
}
/* main loop */
for (; block->pathu; block++) {
update1:
/* delimiter handler */
if (*block->cmdoutcur != '\n' && *block->cmdoutcur != '\0') {
d = delim;
while (*d != '\0')
*(s++) = *(d++);
*(s++) = '\n'; /* to mark the end of delimiter */
/* skip over empty blocks */
} else {
*block->cmdoutprv = *block->cmdoutcur;
continue;
}
skipdelimu:
c = block->cmdoutcur, p = block->cmdoutprv;
update2:
do {
*(s++) = *c;
*p = *c;
c++, p++;
} while (*c != '\n' && *c != '\0');
if (block->pathc && block->signal)
*(s++) = block->signal;
}
*s = '\0';
return 1;
}
void
writepid()
{
int fd;
struct flock fl;
fd = open(LOCKFILE, O_RDWR|O_CREAT, 0644);
if (fd == -1) {
perror("writepid - open");
exit(1);
}
fl.l_type = F_WRLCK;
fl.l_start = 0;
fl.l_whence = SEEK_SET;
fl.l_len = 0;
if (fcntl(fd, F_SETLK, &fl) == -1) {
if (errno == EACCES || errno == EAGAIN) {
fputs("Error: another instance of dwmblocks is already running.\n", stderr);
exit(2);
}
perror("writepid - fcntl");
exit(1);
}
if (ftruncate(fd, 0) == -1) {
perror("writepid - ftruncate");
exit(1);
}
if (dprintf(fd, "%ld", (long)getpid()) < 0) {
perror("writepid - dprintf");
exit(1);
}
}
int
main(int argc, char *argv[])
{
writepid();
if (argc > 2)
if (strcmp(argv[1], "-d") == 0)
delim = argv[2];
delimlength = strlen(delim) + 1;
if (!(dpy = XOpenDisplay(NULL))) {
fputs("Error: could not open display.\n", stderr);
return 1;
}
setupsignals();
statusloop();
cleanup();
return 0;
}