-
Notifications
You must be signed in to change notification settings - Fork 1
/
particle.c
329 lines (273 loc) · 9.53 KB
/
particle.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
#include "particle.h"
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <errno.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#define LOG_MODULE "particle"
#define LOG_ENABLE_DBG 0
#include "log.h"
#include "bar/bar.h"
void
particle_default_destroy(struct particle *particle)
{
if (particle->deco != NULL)
particle->deco->destroy(particle->deco);
fcft_destroy(particle->font);
for (size_t i = 0; i < MOUSE_BTN_COUNT; i++)
free(particle->on_click_templates[i]);
free(particle);
}
struct particle *
particle_common_new(int left_margin, int right_margin,
const char **on_click_templates,
struct fcft_font *font, pixman_color_t foreground,
struct deco *deco)
{
struct particle *p = calloc(1, sizeof(*p));
p->left_margin = left_margin;
p->right_margin = right_margin;
p->foreground = foreground;
p->font = font;
p->deco = deco;
if (on_click_templates != NULL) {
for (size_t i = 0; i < MOUSE_BTN_COUNT; i++) {
if (on_click_templates[i] != NULL) {
p->have_on_click_template = true;
p->on_click_templates[i] = strdup(on_click_templates[i]);
}
}
}
return p;
}
void
exposable_default_destroy(struct exposable *exposable)
{
for (size_t i = 0; i < MOUSE_BTN_COUNT; i++)
free(exposable->on_click[i]);
free(exposable);
}
void
exposable_render_deco(const struct exposable *exposable,
pixman_image_t *pix, int x, int y, int height)
{
const struct deco *deco = exposable->particle->deco;
if (deco != NULL)
deco->expose(deco, pix, x, y, exposable->width, height);
}
static bool
push_argv(char ***argv, size_t *size, char *arg, size_t *argc)
{
if (arg != NULL && arg[0] == '%')
return true;
if (*argc >= *size) {
size_t new_size = *size > 0 ? 2 * *size : 10;
char **new_argv = realloc(*argv, new_size * sizeof(new_argv[0]));
if (new_argv == NULL)
return false;
*argv = new_argv;
*size = new_size;
}
(*argv)[(*argc)++] = arg;
return true;
}
static bool
tokenize_cmdline(char *cmdline, char ***argv)
{
*argv = NULL;
size_t argv_size = 0;
bool first_token_is_quoted = cmdline[0] == '"' || cmdline[0] == '\'';
char delim = first_token_is_quoted ? cmdline[0] : ' ';
char *p = first_token_is_quoted ? &cmdline[1] : &cmdline[0];
size_t idx = 0;
while (*p != '\0') {
char *end = strchr(p, delim);
if (end == NULL) {
if (delim != ' ') {
LOG_ERR("unterminated %s quote\n", delim == '"' ? "double" : "single");
free(*argv);
return false;
}
if (!push_argv(argv, &argv_size, p, &idx) ||
!push_argv(argv, &argv_size, NULL, &idx))
{
goto err;
} else
return true;
}
*end = '\0';
if (!push_argv(argv, &argv_size, p, &idx))
goto err;
p = end + 1;
while (*p == delim)
p++;
while (*p == ' ')
p++;
if (*p == '"' || *p == '\'') {
delim = *p;
p++;
} else
delim = ' ';
}
if (!push_argv(argv, &argv_size, NULL, &idx))
goto err;
return true;
err:
free(*argv);
return false;
}
void
exposable_default_on_mouse(struct exposable *exposable, struct bar *bar,
enum mouse_event event, enum mouse_button btn,
int x, int y)
{
#if defined(LOG_ENABLE_DBG) && LOG_ENABLE_DBG
static const char *button_name[] = {
[MOUSE_BTN_NONE] = "none",
[MOUSE_BTN_LEFT] = "left",
[MOUSE_BTN_MIDDLE] = "middle",
[MOUSE_BTN_RIGHT] = "right",
[MOUSE_BTN_COUNT] = "count",
[MOUSE_BTN_WHEEL_UP] = "wheel-up",
[MOUSE_BTN_WHEEL_DOWN] = "wheel-down",
};
LOG_DBG("on_mouse: exposable=%p, event=%s, btn=%s, x=%d, y=%d (on-click=%s)",
exposable, event == ON_MOUSE_MOTION ? "motion" : "click",
button_name[btn], x, y, exposable->on_click[btn]);
#endif
/* If we have a handler, change cursor to a hand */
const char *cursor =
(exposable->particle != NULL &&
exposable->particle->have_on_click_template)
? "hand2"
: "left_ptr";
bar->set_cursor(bar, cursor);
/* If this is a mouse click, and we have a handler, execute it */
if (exposable->on_click[btn] != NULL && event == ON_MOUSE_CLICK) {
/* Need a writeable copy, whose scope *we* control */
char *cmd = strdup(exposable->on_click[btn]);
LOG_DBG("cmd = \"%s\"", exposable->on_click[btn]);
char **argv;
if (!tokenize_cmdline(cmd, &argv)) {
free(cmd);
return;
}
pid_t pid = fork();
if (pid == -1)
LOG_ERRNO("failed to run on_click handler (fork)");
else if (pid > 0) {
/* Parent */
free(cmd);
free(argv);
int wstatus;
if (waitpid(pid, &wstatus, 0) == -1)
LOG_ERRNO("%s: failed to wait for on_click handler", exposable->on_click[btn]);
if (WIFEXITED(wstatus)) {
if (WEXITSTATUS(wstatus) != 0)
LOG_ERRNO_P(WEXITSTATUS(wstatus), "%s: failed to execute", exposable->on_click[btn]);
} else
LOG_ERR("%s: did not exit normally", exposable->on_click[btn]);
LOG_DBG("%s: launched", exposable->on_click[btn]);
} else {
/*
* Use a pipe with O_CLOEXEC to communicate exec() failure
* to parent process.
*
* If the child succeeds with exec(), the pipe is simply
* closed. If it fails, we write the fail reason (errno)
* to the pipe. The parent reads the pipe; if it receives
* data, the child failed, else it succeeded.
*/
int pipe_fds[2];
if (pipe2(pipe_fds, O_CLOEXEC) == -1) {
LOG_ERRNO("%s: failed to create pipe", cmd);
free(cmd);
return;
}
LOG_DBG("ARGV:");
for (size_t i = 0; argv[i] != NULL; i++)
LOG_DBG(" #%zu: \"%s\" ", i, argv[i]);
LOG_DBG("double forking");
switch (fork()) {
case -1:
close(pipe_fds[0]);
close(pipe_fds[1]);
LOG_ERRNO("failed to double fork");
_exit(errno);
break;
case 0:
/* Child */
close(pipe_fds[0]); /* Close read end */
LOG_DBG("executing on-click handler: %s", cmd);
sigset_t mask;
sigemptyset(&mask);
const struct sigaction sa = {.sa_handler = SIG_DFL};
if (sigaction(SIGINT, &sa, NULL) < 0 ||
sigaction(SIGTERM, &sa, NULL) < 0 ||
sigaction(SIGCHLD, &sa, NULL) < 0 ||
sigprocmask(SIG_SETMASK, &mask, NULL) < 0)
{
goto fail;
}
/* Redirect stdin/stdout/stderr to /dev/null */
int dev_null_r = open("/dev/null", O_RDONLY | O_CLOEXEC);
int dev_null_w = open("/dev/null", O_WRONLY | O_CLOEXEC);
if (dev_null_r == -1 || dev_null_w == -1) {
LOG_ERRNO("/dev/null: failed to open");
goto fail;
}
if (dup2(dev_null_r, STDIN_FILENO) == -1 ||
dup2(dev_null_w, STDOUT_FILENO) == -1 ||
dup2(dev_null_w, STDERR_FILENO) == -1)
{
LOG_ERRNO("failed to redirect stdin/stdout/stderr");
goto fail;
}
/* Close *all* other FDs (e.g. script modules' FDs) */
for (int i = STDERR_FILENO + 1; i < 65536; i++)
if (i != pipe_fds[1])
close(i);
execvp(argv[0], argv);
fail:
/* Signal failure to parent process */
(void)!write(pipe_fds[1], &errno, sizeof(errno));
close(pipe_fds[1]);
_exit(errno);
break;
default:
/* Parent */
close(pipe_fds[1]); /* Close write end */
int _errno = 0;
ssize_t ret = read(pipe_fds[0], &_errno, sizeof(_errno));
close(pipe_fds[0]);
if (ret == 0) {
/* Pipe was closed - child succeeded with exec() */
_exit(0);
}
LOG_DBG("second pipe failed: %s (%d)", strerror(_errno), _errno);
_exit(_errno);
break;
}
}
}
}
struct exposable *
exposable_common_new(const struct particle *particle, const struct tag_set *tags)
{
struct exposable *exposable = calloc(1, sizeof(*exposable));
exposable->particle = particle;
if (particle != NULL && particle->have_on_click_template) {
tags_expand_templates(
exposable->on_click,
(const char **)particle->on_click_templates,
MOUSE_BTN_COUNT, tags);
}
exposable->destroy = &exposable_default_destroy;
exposable->on_mouse = &exposable_default_on_mouse;
return exposable;
}