aboutsummaryrefslogtreecommitdiffstats
path: root/swaynag
diff options
context:
space:
mode:
Diffstat (limited to 'swaynag')
-rw-r--r--swaynag/config.c76
-rw-r--r--swaynag/main.c3
-rw-r--r--swaynag/swaynag.1.scd9
-rw-r--r--swaynag/swaynag.c19
-rw-r--r--swaynag/types.c8
5 files changed, 56 insertions, 59 deletions
diff --git a/swaynag/config.c b/swaynag/config.c
index cd34dcc2..85aa380a 100644
--- a/swaynag/config.c
+++ b/swaynag/config.c
@@ -1,11 +1,10 @@
1#define _XOPEN_SOURCE 700 1#define _POSIX_C_SOURCE 200809L
2#define _POSIX_C_SOURCE 200112L
3#include <getopt.h> 2#include <getopt.h>
3#include <stdio.h>
4#include <stdlib.h> 4#include <stdlib.h>
5#include <wordexp.h> 5#include <wordexp.h>
6#include "log.h" 6#include "log.h"
7#include "list.h" 7#include "list.h"
8#include "readline.h"
9#include "swaynag/swaynag.h" 8#include "swaynag/swaynag.h"
10#include "swaynag/types.h" 9#include "swaynag/types.h"
11#include "util.h" 10#include "util.h"
@@ -13,21 +12,19 @@
13 12
14static char *read_from_stdin(void) { 13static char *read_from_stdin(void) {
15 char *buffer = NULL; 14 char *buffer = NULL;
16 while (!feof(stdin)) { 15 size_t buffer_len = 0;
17 char *line = read_line(stdin); 16 char *line = NULL;
18 if (!line) { 17 size_t line_size = 0;
19 continue; 18 ssize_t nread;
20 } 19 while ((nread = getline(&line, &line_size, stdin)) != -1) {
21 20 buffer = realloc(buffer, buffer_len + nread);
22 size_t curlen = buffer ? strlen(buffer) : 0; 21 snprintf(&buffer[buffer_len], nread + 1, "%s", line);
23 buffer = realloc(buffer, curlen + strlen(line) + 2); 22 buffer_len += nread;
24 snprintf(buffer + curlen, strlen(line) + 2, "%s\n", line);
25
26 free(line);
27 } 23 }
24 free(line);
28 25
29 while (buffer && buffer[strlen(buffer) - 1] == '\n') { 26 while (buffer && buffer[buffer_len - 1] == '\n') {
30 buffer[strlen(buffer) - 1] = '\0'; 27 buffer[--buffer_len] = '\0';
31 } 28 }
32 29
33 return buffer; 30 return buffer;
@@ -53,6 +50,7 @@ int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag,
53 50
54 static struct option opts[] = { 51 static struct option opts[] = {
55 {"button", required_argument, NULL, 'b'}, 52 {"button", required_argument, NULL, 'b'},
53 {"button-no-terminal", required_argument, NULL, 'B'},
56 {"config", required_argument, NULL, 'c'}, 54 {"config", required_argument, NULL, 'c'},
57 {"debug", no_argument, NULL, 'd'}, 55 {"debug", no_argument, NULL, 'd'},
58 {"edge", required_argument, NULL, 'e'}, 56 {"edge", required_argument, NULL, 'e'},
@@ -87,7 +85,10 @@ int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag,
87 "Usage: swaynag [options...]\n" 85 "Usage: swaynag [options...]\n"
88 "\n" 86 "\n"
89 " -b, --button <text> <action> Create a button with text that " 87 " -b, --button <text> <action> Create a button with text that "
90 "executes action when pressed. Multiple buttons can be defined.\n" 88 "executes action in a terminal when pressed. Multiple buttons can "
89 "be defined.\n"
90 " -B, --button-no-terminal <text> <action> Like --button, but does"
91 "not run the action in a terminal.\n"
91 " -c, --config <path> Path to config file.\n" 92 " -c, --config <path> Path to config file.\n"
92 " -d, --debug Enable debugging.\n" 93 " -d, --debug Enable debugging.\n"
93 " -e, --edge top|bottom Set the edge to use.\n" 94 " -e, --edge top|bottom Set the edge to use.\n"
@@ -118,12 +119,13 @@ int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag,
118 119
119 optind = 1; 120 optind = 1;
120 while (1) { 121 while (1) {
121 int c = getopt_long(argc, argv, "b:c:de:f:hlL:m:o:s:t:v", opts, NULL); 122 int c = getopt_long(argc, argv, "b:B:c:de:f:hlL:m:o:s:t:v", opts, NULL);
122 if (c == -1) { 123 if (c == -1) {
123 break; 124 break;
124 } 125 }
125 switch (c) { 126 switch (c) {
126 case 'b': // Button 127 case 'b': // Button
128 case 'B': // Button (No Terminal)
127 if (swaynag) { 129 if (swaynag) {
128 if (optind >= argc) { 130 if (optind >= argc) {
129 fprintf(stderr, "Missing action for button %s\n", optarg); 131 fprintf(stderr, "Missing action for button %s\n", optarg);
@@ -134,6 +136,7 @@ int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag,
134 button->text = strdup(optarg); 136 button->text = strdup(optarg);
135 button->type = SWAYNAG_ACTION_COMMAND; 137 button->type = SWAYNAG_ACTION_COMMAND;
136 button->action = strdup(argv[optind]); 138 button->action = strdup(argv[optind]);
139 button->terminal = c == 'b';
137 list_add(swaynag->buttons, button); 140 list_add(swaynag->buttons, button);
138 } 141 }
139 optind++; 142 optind++;
@@ -343,32 +346,24 @@ int swaynag_load_config(char *path, struct swaynag *swaynag, list_t *types) {
343 type->name = strdup("<config>"); 346 type->name = strdup("<config>");
344 list_add(types, type); 347 list_add(types, type);
345 348
346 char *line; 349 char *line = NULL;
350 size_t line_size = 0;
351 ssize_t nread;
347 int line_number = 0; 352 int line_number = 0;
348 while (!feof(config)) { 353 int result = 0;
349 line = read_line(config); 354 while ((nread = getline(&line, &line_size, config)) != -1) {
350 if (!line) {
351 continue;
352 }
353
354 line_number++; 355 line_number++;
355 if (line[0] == '#') { 356 if (!*line || line[0] == '\n' || line[0] == '#') {
356 free(line);
357 continue;
358 }
359 if (strlen(line) == 0) {
360 free(line);
361 continue; 357 continue;
362 } 358 }
363 359
364 if (line[0] == '[') { 360 if (line[0] == '[') {
365 char *close = strchr(line, ']'); 361 char *close = strchr(line, ']');
366 if (!close) { 362 if (!close) {
367 free(line);
368 fclose(config);
369 fprintf(stderr, "Closing bracket not found on line %d\n", 363 fprintf(stderr, "Closing bracket not found on line %d\n",
370 line_number); 364 line_number);
371 return 1; 365 result = 1;
366 break;
372 } 367 }
373 char *name = calloc(1, close - line); 368 char *name = calloc(1, close - line);
374 strncat(name, line + 1, close - line - 1); 369 strncat(name, line + 1, close - line - 1);
@@ -380,22 +375,17 @@ int swaynag_load_config(char *path, struct swaynag *swaynag, list_t *types) {
380 } 375 }
381 free(name); 376 free(name);
382 } else { 377 } else {
383 char flag[strlen(line) + 3]; 378 char flag[nread + 3];
384 sprintf(flag, "--%s", line); 379 sprintf(flag, "--%s", line);
385 char *argv[] = {"swaynag", flag}; 380 char *argv[] = {"swaynag", flag};
386 int result;
387 result = swaynag_parse_options(2, argv, swaynag, types, type, 381 result = swaynag_parse_options(2, argv, swaynag, types, type,
388 NULL, NULL); 382 NULL, NULL);
389 if (result != 0) { 383 if (result != 0) {
390 free(line); 384 break;
391 fclose(config);
392 return result;
393 } 385 }
394 } 386 }
395
396 free(line);
397 } 387 }
388 free(line);
398 fclose(config); 389 fclose(config);
399 return 0; 390 return result;
400} 391}
401
diff --git a/swaynag/main.c b/swaynag/main.c
index bae3c0e2..9f00ac7e 100644
--- a/swaynag/main.c
+++ b/swaynag/main.c
@@ -1,4 +1,4 @@
1#define _XOPEN_SOURCE 500 1#define _POSIX_C_SOURCE 200809L
2#include <stdlib.h> 2#include <stdlib.h>
3#include <signal.h> 3#include <signal.h>
4#include "log.h" 4#include "log.h"
@@ -130,4 +130,3 @@ cleanup:
130 swaynag_destroy(&swaynag); 130 swaynag_destroy(&swaynag);
131 return exit_code; 131 return exit_code;
132} 132}
133
diff --git a/swaynag/swaynag.1.scd b/swaynag/swaynag.1.scd
index bb69e47d..b25568a0 100644
--- a/swaynag/swaynag.1.scd
+++ b/swaynag/swaynag.1.scd
@@ -12,7 +12,14 @@ _swaynag_ [options...]
12 12
13*-b, --button* <text> <action> 13*-b, --button* <text> <action>
14 Create a button with the text _text_ that executes _action_ when pressed. 14 Create a button with the text _text_ that executes _action_ when pressed.
15 Multiple buttons can be defined by providing the flag multiple times. 15 If the environment variable `TERMINAL` is set, _action_ will be run inside
16 the terminal. Otherwise, it will fallback to running directly. Multiple
17 buttons can be defined by providing the flag multiple times.
18
19*-B, --button-no-terminal* <text> <action>
20 Create a button with the text _text_ that executes _action_ when pressed.
21 _action_ will be run directly instead of in a terminal. Multiple buttons
22 can be defined by providing the flag multiple times.
16 23
17*-c, --config* <path> 24*-c, --config* <path>
18 The config file to use. By default, the following paths are checked: 25 The config file to use. By default, the following paths are checked:
diff --git a/swaynag/swaynag.c b/swaynag/swaynag.c
index 06185f20..674c24b5 100644
--- a/swaynag/swaynag.c
+++ b/swaynag/swaynag.c
@@ -1,4 +1,4 @@
1#define _XOPEN_SOURCE 500 1#define _POSIX_C_SOURCE 200809L
2#include <stdlib.h> 2#include <stdlib.h>
3#include <assert.h> 3#include <assert.h>
4#include <sys/stat.h> 4#include <sys/stat.h>
@@ -49,14 +49,17 @@ static void swaynag_button_execute(struct swaynag *swaynag,
49 if (fork() == 0) { 49 if (fork() == 0) {
50 // Child of the child. Will be reparented to the init process 50 // Child of the child. Will be reparented to the init process
51 char *terminal = getenv("TERMINAL"); 51 char *terminal = getenv("TERMINAL");
52 if (terminal && strlen(terminal)) { 52 if (button->terminal && terminal && strlen(terminal)) {
53 wlr_log(WLR_DEBUG, "Found $TERMINAL: %s", terminal); 53 wlr_log(WLR_DEBUG, "Found $TERMINAL: %s", terminal);
54 if (!terminal_execute(terminal, button->action)) { 54 if (!terminal_execute(terminal, button->action)) {
55 swaynag_destroy(swaynag); 55 swaynag_destroy(swaynag);
56 exit(EXIT_FAILURE); 56 exit(EXIT_FAILURE);
57 } 57 }
58 } else { 58 } else {
59 wlr_log(WLR_DEBUG, "$TERMINAL not found. Running directly"); 59 if (button->terminal) {
60 wlr_log(WLR_DEBUG,
61 "$TERMINAL not found. Running directly");
62 }
60 execl("/bin/sh", "/bin/sh", "-c", button->action, NULL); 63 execl("/bin/sh", "/bin/sh", "-c", button->action, NULL);
61 } 64 }
62 } 65 }
@@ -126,6 +129,8 @@ static void update_cursor(struct swaynag *swaynag) {
126 pointer->cursor_surface, 129 pointer->cursor_surface,
127 pointer->cursor_image->hotspot_x / swaynag->scale, 130 pointer->cursor_image->hotspot_x / swaynag->scale,
128 pointer->cursor_image->hotspot_y / swaynag->scale); 131 pointer->cursor_image->hotspot_y / swaynag->scale);
132 wl_surface_damage_buffer(pointer->cursor_surface, 0, 0,
133 INT32_MAX, INT32_MAX);
129 wl_surface_commit(pointer->cursor_surface); 134 wl_surface_commit(pointer->cursor_surface);
130} 135}
131 136
@@ -290,7 +295,7 @@ static void handle_global(void *data, struct wl_registry *registry,
290 struct swaynag *swaynag = data; 295 struct swaynag *swaynag = data;
291 if (strcmp(interface, wl_compositor_interface.name) == 0) { 296 if (strcmp(interface, wl_compositor_interface.name) == 0) {
292 swaynag->compositor = wl_registry_bind(registry, name, 297 swaynag->compositor = wl_registry_bind(registry, name,
293 &wl_compositor_interface, 3); 298 &wl_compositor_interface, 4);
294 } else if (strcmp(interface, wl_seat_interface.name) == 0) { 299 } else if (strcmp(interface, wl_seat_interface.name) == 0) {
295 swaynag->seat = wl_registry_bind(registry, name, &wl_seat_interface, 1); 300 swaynag->seat = wl_registry_bind(registry, name, &wl_seat_interface, 1);
296 wl_seat_add_listener(swaynag->seat, &seat_listener, swaynag); 301 wl_seat_add_listener(swaynag->seat, &seat_listener, swaynag);
@@ -404,15 +409,13 @@ void swaynag_destroy(struct swaynag *swaynag) {
404 swaynag->run_display = false; 409 swaynag->run_display = false;
405 410
406 free(swaynag->message); 411 free(swaynag->message);
407 while (swaynag->buttons->length) { 412 for (int i = 0; i < swaynag->buttons->length; ++i) {
408 struct swaynag_button *button = swaynag->buttons->items[0]; 413 struct swaynag_button *button = swaynag->buttons->items[i];
409 list_del(swaynag->buttons, 0);
410 free(button->text); 414 free(button->text);
411 free(button->action); 415 free(button->action);
412 free(button); 416 free(button);
413 } 417 }
414 list_free(swaynag->buttons); 418 list_free(swaynag->buttons);
415 free(swaynag->details.button_details);
416 free(swaynag->details.message); 419 free(swaynag->details.message);
417 free(swaynag->details.button_up.text); 420 free(swaynag->details.button_up.text);
418 free(swaynag->details.button_down.text); 421 free(swaynag->details.button_down.text);
diff --git a/swaynag/types.c b/swaynag/types.c
index 1e0a138b..bc17bd33 100644
--- a/swaynag/types.c
+++ b/swaynag/types.c
@@ -1,4 +1,4 @@
1#define _XOPEN_SOURCE 500 1#define _POSIX_C_SOURCE 200809L
2#include <getopt.h> 2#include <getopt.h>
3#include <stdbool.h> 3#include <stdbool.h>
4#include <stdlib.h> 4#include <stdlib.h>
@@ -147,10 +147,8 @@ void swaynag_type_free(struct swaynag_type *type) {
147} 147}
148 148
149void swaynag_types_free(list_t *types) { 149void swaynag_types_free(list_t *types) {
150 while (types->length) { 150 for (int i = 0; i < types->length; ++i) {
151 struct swaynag_type *type = types->items[0]; 151 swaynag_type_free(types->items[i]);
152 swaynag_type_free(type);
153 list_del(types, 0);
154 } 152 }
155 list_free(types); 153 list_free(types);
156} 154}