aboutsummaryrefslogtreecommitdiff
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 @@
-#define _XOPEN_SOURCE 700
-#define _POSIX_C_SOURCE 200112L
+#define _POSIX_C_SOURCE 200809L
#include <getopt.h>
+#include <stdio.h>
#include <stdlib.h>
#include <wordexp.h>
#include "log.h"
#include "list.h"
-#include "readline.h"
#include "swaynag/swaynag.h"
#include "swaynag/types.h"
#include "util.h"
@@ -13,21 +12,19 @@
static char *read_from_stdin(void) {
char *buffer = NULL;
- while (!feof(stdin)) {
- char *line = read_line(stdin);
- if (!line) {
- continue;
- }
-
- size_t curlen = buffer ? strlen(buffer) : 0;
- buffer = realloc(buffer, curlen + strlen(line) + 2);
- snprintf(buffer + curlen, strlen(line) + 2, "%s\n", line);
-
- free(line);
+ size_t buffer_len = 0;
+ char *line = NULL;
+ size_t line_size = 0;
+ ssize_t nread;
+ while ((nread = getline(&line, &line_size, stdin)) != -1) {
+ buffer = realloc(buffer, buffer_len + nread);
+ snprintf(&buffer[buffer_len], nread + 1, "%s", line);
+ buffer_len += nread;
}
+ free(line);
- while (buffer && buffer[strlen(buffer) - 1] == '\n') {
- buffer[strlen(buffer) - 1] = '\0';
+ while (buffer && buffer[buffer_len - 1] == '\n') {
+ buffer[--buffer_len] = '\0';
}
return buffer;
@@ -53,6 +50,7 @@ int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag,
static struct option opts[] = {
{"button", required_argument, NULL, 'b'},
+ {"button-no-terminal", required_argument, NULL, 'B'},
{"config", required_argument, NULL, 'c'},
{"debug", no_argument, NULL, 'd'},
{"edge", required_argument, NULL, 'e'},
@@ -87,7 +85,10 @@ int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag,
"Usage: swaynag [options...]\n"
"\n"
" -b, --button <text> <action> Create a button with text that "
- "executes action when pressed. Multiple buttons can be defined.\n"
+ "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"
" -c, --config <path> Path to config file.\n"
" -d, --debug Enable debugging.\n"
" -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,
optind = 1;
while (1) {
- int c = getopt_long(argc, argv, "b:c:de:f:hlL:m:o:s:t:v", opts, NULL);
+ int c = getopt_long(argc, argv, "b:B:c:de:f:hlL:m:o:s:t:v", opts, NULL);
if (c == -1) {
break;
}
switch (c) {
case 'b': // Button
+ case 'B': // Button (No Terminal)
if (swaynag) {
if (optind >= argc) {
fprintf(stderr, "Missing action for button %s\n", optarg);
@@ -134,6 +136,7 @@ int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag,
button->text = strdup(optarg);
button->type = SWAYNAG_ACTION_COMMAND;
button->action = strdup(argv[optind]);
+ button->terminal = c == 'b';
list_add(swaynag->buttons, button);
}
optind++;
@@ -343,32 +346,24 @@ int swaynag_load_config(char *path, struct swaynag *swaynag, list_t *types) {
type->name = strdup("<config>");
list_add(types, type);
- char *line;
+ char *line = NULL;
+ size_t line_size = 0;
+ ssize_t nread;
int line_number = 0;
- while (!feof(config)) {
- line = read_line(config);
- if (!line) {
- continue;
- }
-
+ int result = 0;
+ while ((nread = getline(&line, &line_size, config)) != -1) {
line_number++;
- if (line[0] == '#') {
- free(line);
- continue;
- }
- if (strlen(line) == 0) {
- free(line);
+ if (!*line || line[0] == '\n' || line[0] == '#') {
continue;
}
if (line[0] == '[') {
char *close = strchr(line, ']');
if (!close) {
- free(line);
- fclose(config);
fprintf(stderr, "Closing bracket not found on line %d\n",
line_number);
- return 1;
+ result = 1;
+ break;
}
char *name = calloc(1, close - line);
strncat(name, line + 1, close - line - 1);
@@ -380,22 +375,17 @@ int swaynag_load_config(char *path, struct swaynag *swaynag, list_t *types) {
}
free(name);
} else {
- char flag[strlen(line) + 3];
+ char flag[nread + 3];
sprintf(flag, "--%s", line);
char *argv[] = {"swaynag", flag};
- int result;
result = swaynag_parse_options(2, argv, swaynag, types, type,
NULL, NULL);
if (result != 0) {
- free(line);
- fclose(config);
- return result;
+ break;
}
}
-
- free(line);
}
+ free(line);
fclose(config);
- return 0;
+ return result;
}
-
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 @@
-#define _XOPEN_SOURCE 500
+#define _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#include <signal.h>
#include "log.h"
@@ -130,4 +130,3 @@ cleanup:
swaynag_destroy(&swaynag);
return exit_code;
}
-
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...]
*-b, --button* <text> <action>
Create a button with the text _text_ that executes _action_ when pressed.
- Multiple buttons can be defined by providing the flag multiple times.
+ If the environment variable `TERMINAL` is set, _action_ will be run inside
+ the terminal. Otherwise, it will fallback to running directly. Multiple
+ buttons can be defined by providing the flag multiple times.
+
+*-B, --button-no-terminal* <text> <action>
+ Create a button with the text _text_ that executes _action_ when pressed.
+ _action_ will be run directly instead of in a terminal. Multiple buttons
+ can be defined by providing the flag multiple times.
*-c, --config* <path>
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 @@
-#define _XOPEN_SOURCE 500
+#define _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#include <assert.h>
#include <sys/stat.h>
@@ -49,14 +49,17 @@ static void swaynag_button_execute(struct swaynag *swaynag,
if (fork() == 0) {
// Child of the child. Will be reparented to the init process
char *terminal = getenv("TERMINAL");
- if (terminal && strlen(terminal)) {
+ if (button->terminal && terminal && strlen(terminal)) {
wlr_log(WLR_DEBUG, "Found $TERMINAL: %s", terminal);
if (!terminal_execute(terminal, button->action)) {
swaynag_destroy(swaynag);
exit(EXIT_FAILURE);
}
} else {
- wlr_log(WLR_DEBUG, "$TERMINAL not found. Running directly");
+ if (button->terminal) {
+ wlr_log(WLR_DEBUG,
+ "$TERMINAL not found. Running directly");
+ }
execl("/bin/sh", "/bin/sh", "-c", button->action, NULL);
}
}
@@ -126,6 +129,8 @@ static void update_cursor(struct swaynag *swaynag) {
pointer->cursor_surface,
pointer->cursor_image->hotspot_x / swaynag->scale,
pointer->cursor_image->hotspot_y / swaynag->scale);
+ wl_surface_damage_buffer(pointer->cursor_surface, 0, 0,
+ INT32_MAX, INT32_MAX);
wl_surface_commit(pointer->cursor_surface);
}
@@ -290,7 +295,7 @@ static void handle_global(void *data, struct wl_registry *registry,
struct swaynag *swaynag = data;
if (strcmp(interface, wl_compositor_interface.name) == 0) {
swaynag->compositor = wl_registry_bind(registry, name,
- &wl_compositor_interface, 3);
+ &wl_compositor_interface, 4);
} else if (strcmp(interface, wl_seat_interface.name) == 0) {
swaynag->seat = wl_registry_bind(registry, name, &wl_seat_interface, 1);
wl_seat_add_listener(swaynag->seat, &seat_listener, swaynag);
@@ -404,15 +409,13 @@ void swaynag_destroy(struct swaynag *swaynag) {
swaynag->run_display = false;
free(swaynag->message);
- while (swaynag->buttons->length) {
- struct swaynag_button *button = swaynag->buttons->items[0];
- list_del(swaynag->buttons, 0);
+ for (int i = 0; i < swaynag->buttons->length; ++i) {
+ struct swaynag_button *button = swaynag->buttons->items[i];
free(button->text);
free(button->action);
free(button);
}
list_free(swaynag->buttons);
- free(swaynag->details.button_details);
free(swaynag->details.message);
free(swaynag->details.button_up.text);
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 @@
-#define _XOPEN_SOURCE 500
+#define _POSIX_C_SOURCE 200809L
#include <getopt.h>
#include <stdbool.h>
#include <stdlib.h>
@@ -147,10 +147,8 @@ void swaynag_type_free(struct swaynag_type *type) {
}
void swaynag_types_free(list_t *types) {
- while (types->length) {
- struct swaynag_type *type = types->items[0];
- swaynag_type_free(type);
- list_del(types, 0);
+ for (int i = 0; i < types->length; ++i) {
+ swaynag_type_free(types->items[i]);
}
list_free(types);
}