Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement swaynag -B/--button-no-terminal #3207

Merged
merged 1 commit into from
Nov 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/swaynag/swaynag.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ struct swaynag_button {
int y;
int width;
int height;
bool terminal;
};

struct swaynag_details {
Expand Down
10 changes: 8 additions & 2 deletions swaynag/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,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'},
Expand Down Expand Up @@ -86,7 +87,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"
Expand Down Expand Up @@ -117,12 +121,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);
Expand All @@ -133,6 +138,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++;
Expand Down
9 changes: 8 additions & 1 deletion swaynag/swaynag.1.scd
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 5 additions & 2 deletions swaynag/swaynag.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down