aboutsummaryrefslogtreecommitdiffstats
path: root/src/app.c
diff options
context:
space:
mode:
authoruser <user@clank>2026-09-29 14:26:06 +0200
committeruser <user@clank>2026-09-29 14:26:06 +0200
commite779c1fafa556330752b2c5f43c56415f655ff24 (patch)
treeb87d335587fbfb68696569c5a41d912435d67140 /src/app.c
parentmouse: encode pixel reports against the cell size we advertise (diff)
downloadslosh-e779c1fafa556330752b2c5f43c56415f655ff24.tar.gz
slosh-e779c1fafa556330752b2c5f43c56415f655ff24.tar.xz
slosh-e779c1fafa556330752b2c5f43c56415f655ff24.zip
panes: $SLOSH_PANE, so a program knows which pane it is in
A program in a pane could name every pane but its own. `panes` says which one is *focused*, and focus belongs to the human, so anything that inferred "where do I live" from it did its work in whichever tab somebody else happened to be looking at -- and lost a race with anybody else asking the same question. So export the id into the child, the way tmux has $TMUX_PANE and zellij $ZELLIJ_PANE_ID. The id is taken before the pane is made, because pane_new spawns the program and assigning it afterwards is one instruction too late; it lives on the pane rather than the layout, so a rerun keeps it -- an identity that changed under a rerun would be no identity at all. Unset, not zero, when there is none: a stale value inherited from an outer session would read as this one's. `panes` also reports each pane's pid now, which is the same question answered for anything that cannot read its own environment: walk your parents up until one of them is on that list.
Diffstat (limited to 'src/app.c')
-rw-r--r--src/app.c15
1 files changed, 13 insertions, 2 deletions
diff --git a/src/app.c b/src/app.c
index 385c5f9..2031e09 100644
--- a/src/app.c
+++ b/src/app.c
@@ -830,13 +830,18 @@ const char *app_clipboard(const app_t *a) { return a->clipboard; }
node_t *leaf_new_ex(app_t *a, const char *const argv[], const char *cwd,
bool suspended, const char *label) {
- pane_t *p = pane_new_ex(argv, 1, 1, cwd, suspended, label);
+ /* The id is taken *before* the pane is made, because the pane's program is
+ * spawned by pane_new and has to be told which pane it is ($SLOSH_PANE).
+ * Assigning it afterwards, as this used to, is one instruction too late:
+ * the child is already running without it. */
+ uint32_t id = ++a->next_id;
+ pane_t *p = pane_new_ex(argv, 1, 1, cwd, suspended, label, id);
if (!p) return NULL;
node_t *n = calloc(1, sizeof *n);
n->kind = NODE_LEAF;
n->weight = WEIGHT_UNIT;
n->pane = p;
- n->id = ++a->next_id;
+ n->id = id;
pane_set_osc_handler(p, on_pane_osc, a);
pane_set_clipboard_handler(p, on_pane_clipboard, a);
pane_set_notify_handler(p, on_pane_notify, a);
@@ -2137,6 +2142,12 @@ static void panes_cb(node_t *n, void *ud) {
* false with a status is one whose program is over and which is waiting to
* be re-run or closed. `exit_code` is -1 when there is no status to give. */
json_bool(j, "alive", pane_alive(n->pane));
+ /* The pid of the program in the pane, so a process can work out which pane
+ * it is in by walking its own parents up to one of these -- the answer for
+ * anything that cannot read $SLOSH_PANE from its environment, which is
+ * every tool run with a scrubbed env and every process with no controlling
+ * tty. -1 when there is nothing running (a suspended or dead pane). */
+ json_int(j, "pid", (long long)pane_pid(n->pane));
{
int code = 0;
bool sig = false;