Skip to content

Commit b46c716

Browse files
exec: export matched container id if present
Provides matched container id to each command execution via handler_context. If there is no matched node, the handler_context has the focused node. The idea is that this can simplify scripting. Relates to PR 6160 at i3
1 parent 0e19d85 commit b46c716

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

sway/commands/exec_always.c

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,22 @@ struct cmd_results *cmd_exec_validate(int argc, char **argv) {
2828
struct cmd_results *cmd_exec_process(int argc, char **argv) {
2929
struct cmd_results *error = NULL;
3030
char *cmd = NULL;
31+
bool matched_container_id = false;
3132
bool no_startup_id = false;
32-
if (strcmp(argv[0], "--no-startup-id") == 0) {
33-
no_startup_id = true;
34-
--argc; ++argv;
35-
if ((error = checkarg(argc, argv[-1], EXPECTED_AT_LEAST, 1))) {
36-
return error;
33+
int argv_counter=-1;
34+
while (argc > 0 && has_prefix(*argv, "--")) {
35+
if (strcmp(argv[0], "--matched-container-id") == 0) {
36+
matched_container_id = true;
37+
} else if (strcmp(argv[0], "--no-startup-id") == 0) {
38+
no_startup_id = true;
39+
} else {
40+
return cmd_results_new(CMD_INVALID, "Unrecognized argument '%s'", *argv);
3741
}
42+
--argc; ++argv; argv_counter--;
43+
}
44+
45+
if ((error = checkarg(argc, argv[argv_counter], EXPECTED_AT_LEAST, 1))) {
46+
return error;
3847
}
3948

4049
if (argc == 1 && (argv[0][0] == '\'' || argv[0][0] == '"')) {
@@ -61,6 +70,26 @@ struct cmd_results *cmd_exec_process(int argc, char **argv) {
6170
}
6271
}
6372

73+
if (matched_container_id && config->handler_context.node != NULL) {
74+
size_t con_id = config->handler_context.node->id;
75+
int con_id_len = snprintf(NULL, 0, "%zu", con_id);
76+
if (con_id_len < 0) {
77+
sway_log(SWAY_ERROR, "Unable to determine buffer length for SWAY_EXEC_CON_ID");
78+
goto no_con_id_export;
79+
}
80+
// accommodate \0
81+
con_id_len++;
82+
char* con_id_str = malloc(con_id_len);
83+
if (!con_id_str) {
84+
sway_log(SWAY_ERROR, "Unable to allocate buffer for SWAY_EXEC_CON_ID");
85+
goto no_con_id_export;
86+
}
87+
snprintf(con_id_str, con_id_len, "%zu", con_id);
88+
setenv("SWAY_EXEC_CON_ID", con_id_str, 1);
89+
free(con_id_str);
90+
}
91+
no_con_id_export:
92+
6493
execlp("sh", "sh", "-c", cmd, (void*)NULL);
6594
sway_log_errno(SWAY_ERROR, "execve failed");
6695
_exit(0); // Close child process

sway/sway.5.scd

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -697,10 +697,13 @@ The default colors are:
697697
windows that are spawned in floating mode, not windows that become floating
698698
afterwards.
699699

700-
*exec* <shell command>
701-
Executes _shell command_ with sh.
700+
*exec* [--matched-container-id] [--no-startup-id] <shell command>
701+
Executes _shell command_ with sh. The _--matched-container-id_ option
702+
exports matched container if following a criteria match or the focused
703+
node otherwise in SWAY_EXEC_CON_ID. The _--no_startup_id_ option prevents
704+
exporting of DESKTOP_STARTUP_ID.
702705

703-
*exec_always* <shell command>
706+
*exec_always* [--matched-container-id] [--no-startup-id] <shell command>
704707
Like *exec*, but the shell command will be executed _again_ after *reload*.
705708

706709
*floating_maximum_size* <width> x <height>

0 commit comments

Comments
 (0)