Skip to content

Commit 77b11fd

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 0153bc9 commit 77b11fd

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

sway/commands/exec_always.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,26 @@ struct cmd_results *cmd_exec_process(int argc, char **argv) {
6363
}
6464
}
6565

66+
if (config->handler_context.node != NULL) {
67+
size_t con_id = config->handler_context.node->id;
68+
int con_id_len = snprintf(NULL, 0, "%zu", con_id);
69+
if (con_id_len < 0) {
70+
sway_log(SWAY_ERROR, "Unable to determine buffer length for SWAY_EXEC_CON_ID");
71+
goto no_con_id_export;
72+
}
73+
// accommodate \0
74+
con_id_len++;
75+
char* con_id_str = malloc(con_id_len);
76+
if (!con_id_str) {
77+
sway_log(SWAY_ERROR, "Unable to allocate buffer for SWAY_EXEC_CON_ID");
78+
goto no_con_id_export;
79+
}
80+
snprintf(con_id_str, con_id_len, "%zu", con_id);
81+
setenv("SWAY_EXEC_CON_ID", con_id_str, 1);
82+
free(con_id_str);
83+
}
84+
no_con_id_export:
85+
6686
execlp("sh", "sh", "-c", cmd, (void*)NULL);
6787
sway_log_errno(SWAY_ERROR, "execve failed");
6888
_exit(0); // Close child process

0 commit comments

Comments
 (0)