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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
|
#define _POSIX_C_SOURCE 200809L
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <wordexp.h>
#include <unistd.h>
#include "log.h"
#include "list.h"
#include "swaynag/swaynag.h"
#include "swaynag/types.h"
#include "util.h"
#include "wlr-layer-shell-unstable-v1-client-protocol.h"
static char *read_and_trim_stdin(void) {
char *buffer = NULL, *line = NULL;
size_t buffer_len = 0, line_size = 0;
while (1) {
ssize_t nread = getline(&line, &line_size, stdin);
if (nread == -1) {
if (feof(stdin)) {
break;
} else {
perror("getline");
goto freeline;
}
}
buffer = realloc(buffer, buffer_len + nread + 1);
if (!buffer) {
perror("realloc");
goto freebuf;
}
memcpy(&buffer[buffer_len], line, nread + 1);
buffer_len += nread;
}
free(line);
while (buffer_len && buffer[buffer_len - 1] == '\n') {
buffer[--buffer_len] = '\0';
}
return buffer;
freeline:
free(line);
freebuf:
free(buffer);
return NULL;
}
int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag,
list_t *types, struct swaynag_type *type, char **config, bool *debug) {
enum type_options {
TO_COLOR_BACKGROUND = 256,
TO_COLOR_BORDER,
TO_COLOR_BORDER_BOTTOM,
TO_COLOR_BUTTON,
TO_COLOR_DETAILS,
TO_COLOR_TEXT,
TO_COLOR_BUTTON_TEXT,
TO_THICK_BAR_BORDER,
TO_PADDING_MESSAGE,
TO_THICK_DET_BORDER,
TO_THICK_BTN_BORDER,
TO_GAP_BTN,
TO_GAP_BTN_DISMISS,
TO_MARGIN_BTN_RIGHT,
TO_PADDING_BTN,
};
static const struct option opts[] = {
{"button", required_argument, NULL, 'b'},
{"button-no-terminal", required_argument, NULL, 'B'},
{"button-dismiss", required_argument, NULL, 'z'},
{"button-dismiss-no-terminal", required_argument, NULL, 'Z'},
{"config", required_argument, NULL, 'c'},
{"debug", no_argument, NULL, 'd'},
{"edge", required_argument, NULL, 'e'},
{"layer", required_argument, NULL, 'y'},
{"font", required_argument, NULL, 'f'},
{"help", no_argument, NULL, 'h'},
{"detailed-message", no_argument, NULL, 'l'},
{"detailed-button", required_argument, NULL, 'L'},
{"message", required_argument, NULL, 'm'},
{"output", required_argument, NULL, 'o'},
{"dismiss-button", required_argument, NULL, 's'},
{"type", required_argument, NULL, 't'},
{"version", no_argument, NULL, 'v'},
{"background", required_argument, NULL, TO_COLOR_BACKGROUND},
{"border", required_argument, NULL, TO_COLOR_BORDER},
{"border-bottom", required_argument, NULL, TO_COLOR_BORDER_BOTTOM},
{"button-background", required_argument, NULL, TO_COLOR_BUTTON},
{"text", required_argument, NULL, TO_COLOR_TEXT},
{"button-text", required_argument, NULL, TO_COLOR_BUTTON_TEXT},
{"border-bottom-size", required_argument, NULL, TO_THICK_BAR_BORDER},
{"message-padding", required_argument, NULL, TO_PADDING_MESSAGE},
{"details-border-size", required_argument, NULL, TO_THICK_DET_BORDER},
{"details-background", required_argument, NULL, TO_COLOR_DETAILS},
{"button-border-size", required_argument, NULL, TO_THICK_BTN_BORDER},
{"button-gap", required_argument, NULL, TO_GAP_BTN},
{"button-dismiss-gap", required_argument, NULL, TO_GAP_BTN_DISMISS},
{"button-margin-right", required_argument, NULL, TO_MARGIN_BTN_RIGHT},
{"button-padding", required_argument, NULL, TO_PADDING_BTN},
{0, 0, 0, 0}
};
const char *usage =
"Usage: swaynag [options...]\n"
"\n"
" -b, --button <text> <action> Create a button with text that "
"executes action in a terminal when pressed. Multiple buttons can "
"be defined.\n"
" -B, --button-no-terminal <text> <action> Like --button, but does"
"not run the action in a terminal.\n"
" -z, --button-dismiss <text> <action> Create a button with text that "
"dismisses swaynag, and executes action in a terminal when pressed. "
"Multiple buttons can be defined.\n"
" -Z, --button-dismiss-no-terminal <text> <action> Like "
"--button-dismiss, but does not run the action in a terminal.\n"
" -c, --config <path> Path to config file.\n"
" -d, --debug Enable debugging.\n"
" -e, --edge top|bottom Set the edge to use.\n"
" -y, --layer overlay|top|bottom|background\n"
" Set the layer to use.\n"
" -f, --font <font> Set the font to use.\n"
" -h, --help Show help message and quit.\n"
" -l, --detailed-message Read a detailed message from stdin.\n"
" -L, --detailed-button <text> Set the text of the detail button.\n"
" -m, --message <msg> Set the message text.\n"
" -o, --output <output> Set the output to use.\n"
" -s, --dismiss-button <text> Set the dismiss button text.\n"
" -t, --type <type> Set the message type.\n"
" -v, --version Show the version number and quit.\n"
"\n"
"The following appearance options can also be given:\n"
" --background RRGGBB[AA] Background color.\n"
" --border RRGGBB[AA] Border color.\n"
" --border-bottom RRGGBB[AA] Bottom border color.\n"
" --button-background RRGGBB[AA] Button background color.\n"
" --text RRGGBB[AA] Text color.\n"
" --button-text RRGGBB[AA] Button text color.\n"
" --border-bottom-size size Thickness of the bar border.\n"
" --message-padding padding Padding for the message.\n"
" --details-border-size size Thickness for the details border.\n"
" --details-background RRGGBB[AA] Details background color.\n"
" --button-border-size size Thickness for the button border.\n"
" --button-gap gap Size of the gap between buttons\n"
" --button-dismiss-gap gap Size of the gap for dismiss button.\n"
" --button-margin-right margin Margin from dismiss button to edge.\n"
" --button-padding padding Padding for the button text.\n";
optind = 1;
while (1) {
int c = getopt_long(argc, argv, "b:B:z:Z:c:de:y:f:hlL:m:o:s:t:v", opts, NULL);
if (c == -1) {
break;
}
switch (c) {
case 'b': // Button
case 'B': // Button (No Terminal)
case 'z': // Button (Dismiss)
case 'Z': // Button (Dismiss, No Terminal)
if (swaynag) {
if (optind >= argc) {
fprintf(stderr, "Missing action for button %s\n", optarg);
return EXIT_FAILURE;
}
struct swaynag_button *button = calloc(1, sizeof(struct swaynag_button));
if (!button) {
perror("calloc");
return EXIT_FAILURE;
}
button->text = strdup(optarg);
button->type = SWAYNAG_ACTION_COMMAND;
button->action = strdup(argv[optind]);
button->terminal = c == 'b';
button->dismiss = c == 'z' || c == 'Z';
list_add(swaynag->buttons, button);
}
optind++;
break;
case 'c': // Config
if (config) {
*config = strdup(optarg);
}
break;
case 'd': // Debug
if (debug) {
*debug = true;
}
break;
case 'e': // Edge
if (type) {
if (strcmp(optarg, "top") == 0) {
type->anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP
| ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT
| ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT;
} else if (strcmp(optarg, "bottom") == 0) {
type->anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM
| ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT
| ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT;
} else {
fprintf(stderr, "Invalid edge: %s\n", optarg);
return EXIT_FAILURE;
}
}
break;
case 'y': // Layer
if (type) {
if (strcmp(optarg, "background") == 0) {
type->layer = ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND;
} else if (strcmp(optarg, "bottom") == 0) {
type->layer = ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM;
} else if (strcmp(optarg, "top") == 0) {
type->layer = ZWLR_LAYER_SHELL_V1_LAYER_TOP;
} else if (strcmp(optarg, "overlay") == 0) {
type->layer = ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY;
} else {
fprintf(stderr, "Invalid layer: %s\n"
"Usage: --layer overlay|top|bottom|background\n",
optarg);
return EXIT_FAILURE;
}
}
break;
case 'f': // Font
if (type) {
pango_font_description_free(type->font_description);
type->font_description = pango_font_description_from_string(optarg);
}
break;
case 'l': // Detailed Message
if (swaynag) {
free(swaynag->details.message);
swaynag->details.message = read_and_trim_stdin();
if (!swaynag->details.message) {
return EXIT_FAILURE;
}
swaynag->details.button_up.text = strdup("▲");
swaynag->details.button_down.text = strdup("▼");
}
break;
case 'L': // Detailed Button Text
if (swaynag) {
free(swaynag->details.details_text);
swaynag->details.details_text = strdup(optarg);
}
break;
case 'm': // Message
if (swaynag) {
free(swaynag->message);
swaynag->message = strdup(optarg);
}
break;
case 'o': // Output
if (type) {
free(type->output);
type->output = strdup(optarg);
}
break;
case 's': // Dismiss Button Text
if (swaynag) {
struct swaynag_button *button_close = swaynag->buttons->items[0];
free(button_close->text);
button_close->text = strdup(optarg);
}
break;
case 't': // Type
if (swaynag) {
swaynag->type = swaynag_type_get(types, optarg);
if (!swaynag->type) {
fprintf(stderr, "Unknown type %s\n", optarg);
return EXIT_FAILURE;
}
}
break;
case 'v': // Version
printf("swaynag version " SWAY_VERSION "\n");
return -1;
case TO_COLOR_BACKGROUND: // Background color
if (type && !parse_color(optarg, &type->background)) {
fprintf(stderr, "Invalid background color: %s", optarg);
}
break;
case TO_COLOR_BORDER: // Border color
if (type && !parse_color(optarg, &type->border)) {
fprintf(stderr, "Invalid border color: %s", optarg);
}
break;
case TO_COLOR_BORDER_BOTTOM: // Bottom border color
if (type && !parse_color(optarg, &type->border_bottom)) {
fprintf(stderr, "Invalid border bottom color: %s", optarg);
}
break;
case TO_COLOR_BUTTON: // Button background color
if (type && !parse_color(optarg, &type->button_background)) {
fprintf(stderr, "Invalid button background color: %s", optarg);
}
break;
case TO_COLOR_DETAILS: // Details background color
if (type && !parse_color(optarg, &type->details_background)) {
fprintf(stderr, "Invalid details background color: %s", optarg);
}
break;
case TO_COLOR_TEXT: // Text color
if (type && !parse_color(optarg, &type->text)) {
fprintf(stderr, "Invalid text color: %s", optarg);
}
break;
case TO_COLOR_BUTTON_TEXT: // Button text color
if (type && !parse_color(optarg, &type->button_text)) {
fprintf(stderr, "Invalid button text color: %s", optarg);
}
break;
case TO_THICK_BAR_BORDER: // Bottom border thickness
if (type) {
type->bar_border_thickness = strtol(optarg, NULL, 0);
}
break;
case TO_PADDING_MESSAGE: // Message padding
if (type) {
type->message_padding = strtol(optarg, NULL, 0);
}
break;
case TO_THICK_DET_BORDER: // Details border thickness
if (type) {
type->details_border_thickness = strtol(optarg, NULL, 0);
}
break;
case TO_THICK_BTN_BORDER: // Button border thickness
if (type) {
type->button_border_thickness = strtol(optarg, NULL, 0);
}
break;
case TO_GAP_BTN: // Gap between buttons
if (type) {
type->button_gap = strtol(optarg, NULL, 0);
}
break;
case TO_GAP_BTN_DISMISS: // Gap between dismiss button
if (type) {
type->button_gap_close = strtol(optarg, NULL, 0);
}
break;
case TO_MARGIN_BTN_RIGHT: // Margin on the right side of button area
if (type) {
type->button_margin_right = strtol(optarg, NULL, 0);
}
break;
case TO_PADDING_BTN: // Padding for the button text
if (type) {
type->button_padding = strtol(optarg, NULL, 0);
}
break;
default: // Help or unknown flag
fprintf(c == 'h' ? stdout : stderr, "%s", usage);
return -1;
}
}
return 0;
}
static bool file_exists(const char *path) {
return path && access(path, R_OK) != -1;
}
char *swaynag_get_config_path(void) {
static const char *config_paths[] = {
"$HOME/.swaynag/config",
"$XDG_CONFIG_HOME/swaynag/config",
SYSCONFDIR "/swaynag/config",
};
char *config_home = getenv("XDG_CONFIG_HOME");
if (!config_home || config_home[0] == '\0') {
config_paths[1] = "$HOME/.config/swaynag/config";
}
wordexp_t p;
for (size_t i = 0; i < sizeof(config_paths) / sizeof(char *); ++i) {
if (wordexp(config_paths[i], &p, 0) == 0) {
char *path = strdup(p.we_wordv[0]);
wordfree(&p);
if (file_exists(path)) {
return path;
}
free(path);
}
}
return NULL;
}
int swaynag_load_config(char *path, struct swaynag *swaynag, list_t *types) {
FILE *config = fopen(path, "r");
if (!config) {
fprintf(stderr, "Failed to read config. Running without it.\n");
return 0;
}
struct swaynag_type *type = swaynag_type_new("<config>");
list_add(types, type);
char *line = NULL;
size_t line_size = 0;
ssize_t nread;
int line_number = 0;
int result = 0;
while ((nread = getline(&line, &line_size, config)) != -1) {
line_number++;
if (!*line || line[0] == '\n' || line[0] == '#') {
continue;
}
if (line[nread - 1] == '\n') {
line[nread - 1] = '\0';
}
if (line[0] == '[') {
char *close = strchr(line, ']');
if (!close || close != &line[nread - 2] || nread <= 3) {
fprintf(stderr, "Line %d is malformed\n", line_number);
result = 1;
break;
}
*close = '\0';
type = swaynag_type_get(types, &line[1]);
if (!type) {
type = swaynag_type_new(&line[1]);
list_add(types, type);
}
} else {
char *flag = malloc(nread + 3);
if (!flag) {
perror("calloc");
return EXIT_FAILURE;
}
snprintf(flag, nread + 3, "--%s", line);
char *argv[] = {"swaynag", flag};
result = swaynag_parse_options(2, argv, swaynag, types, type,
NULL, NULL);
free(flag);
if (result != 0) {
break;
}
}
}
free(line);
fclose(config);
return result;
}
|