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

In `i3 4.16`, `i3-nagbar` introduces the flags `-B/--button-no-terminal`
to run the action directly instead of inside a terminal. This implements
the flags for swaynag for compatibility.

Since swaynag does not use an equivalent to `i3-sensible-terminal`, the
flags `-b/--button` only uses a terminal when the environment variable
`TERMINAL` is set, otherwise it acts the same as these new flags.
This commit is contained in:
Brian Ashworth 2018-11-27 23:27:44 -05:00
parent dbf8e1cead
commit 673da83260
4 changed files with 22 additions and 5 deletions

View file

@ -44,6 +44,7 @@ struct swaynag_button {
int y; int y;
int width; int width;
int height; int height;
bool terminal;
}; };
struct swaynag_details { struct swaynag_details {

View file

@ -52,6 +52,7 @@ int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag,
static struct option opts[] = { static struct option opts[] = {
{"button", required_argument, NULL, 'b'}, {"button", required_argument, NULL, 'b'},
{"button-no-terminal", required_argument, NULL, 'B'},
{"config", required_argument, NULL, 'c'}, {"config", required_argument, NULL, 'c'},
{"debug", no_argument, NULL, 'd'}, {"debug", no_argument, NULL, 'd'},
{"edge", required_argument, NULL, 'e'}, {"edge", required_argument, NULL, 'e'},
@ -86,7 +87,10 @@ int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag,
"Usage: swaynag [options...]\n" "Usage: swaynag [options...]\n"
"\n" "\n"
" -b, --button <text> <action> Create a button with text that " " -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" " -c, --config <path> Path to config file.\n"
" -d, --debug Enable debugging.\n" " -d, --debug Enable debugging.\n"
" -e, --edge top|bottom Set the edge to use.\n" " -e, --edge top|bottom Set the edge to use.\n"
@ -117,12 +121,13 @@ int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag,
optind = 1; optind = 1;
while (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) { if (c == -1) {
break; break;
} }
switch (c) { switch (c) {
case 'b': // Button case 'b': // Button
case 'B': // Button (No Terminal)
if (swaynag) { if (swaynag) {
if (optind >= argc) { if (optind >= argc) {
fprintf(stderr, "Missing action for button %s\n", optarg); fprintf(stderr, "Missing action for button %s\n", optarg);
@ -133,6 +138,7 @@ int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag,
button->text = strdup(optarg); button->text = strdup(optarg);
button->type = SWAYNAG_ACTION_COMMAND; button->type = SWAYNAG_ACTION_COMMAND;
button->action = strdup(argv[optind]); button->action = strdup(argv[optind]);
button->terminal = c == 'b';
list_add(swaynag->buttons, button); list_add(swaynag->buttons, button);
} }
optind++; optind++;

View file

@ -12,7 +12,14 @@ _swaynag_ [options...]
*-b, --button* <text> <action> *-b, --button* <text> <action>
Create a button with the text _text_ that executes _action_ when pressed. 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> *-c, --config* <path>
The config file to use. By default, the following paths are checked: The config file to use. By default, the following paths are checked:

View file

@ -49,14 +49,17 @@ static void swaynag_button_execute(struct swaynag *swaynag,
if (fork() == 0) { if (fork() == 0) {
// Child of the child. Will be reparented to the init process // Child of the child. Will be reparented to the init process
char *terminal = getenv("TERMINAL"); char *terminal = getenv("TERMINAL");
if (terminal && strlen(terminal)) { if (button->terminal && terminal && strlen(terminal)) {
wlr_log(WLR_DEBUG, "Found $TERMINAL: %s", terminal); wlr_log(WLR_DEBUG, "Found $TERMINAL: %s", terminal);
if (!terminal_execute(terminal, button->action)) { if (!terminal_execute(terminal, button->action)) {
swaynag_destroy(swaynag); swaynag_destroy(swaynag);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} else { } 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); execl("/bin/sh", "/bin/sh", "-c", button->action, NULL);
} }
} }