This is an automated email from the git hooks/post-receive script.

git pushed a commit to branch tymux-10-quiet-head
in repository terminology.

View the commit online.

commit 781d2e3e647e9f1608eb1baa73aa7adacd74b170
Author: [email protected] <[email protected]>
AuthorDate: Fri Sep 25 12:21:47 2026 -0600

    termio: keep the terminal when leaving a tymux session
    
    Detaching from a native session, or the session ending, closed the
    terminal, even one that tymux attach had switched into the session
    from a local shell. That shell is gone, it got a SIGHUP, so a fresh
    one now takes its place, started in the directory the old one was in
    when the terminal was switched.
    
    A terminal opened directly on a session has nothing to go back to. It
    gets a plain shell when it detaches, so the window stays usable, and it
    closes like any terminal whose shell exited when the session ends.
    termtymux reports which of the two happened.
    
    termio_add(), the hand-off and this path now share the code wiring a
    new pty's callbacks.
---
 man/tymux.1               |  17 ++++++
 src/bin/termio.c          | 149 ++++++++++++++++++++++++++++++++++------------
 src/bin/termiointernals.h |   8 +++
 src/bin/termtymux.c       |  11 +++-
 src/bin/termtymux.h       |  17 ++++++
 5 files changed, 164 insertions(+), 38 deletions(-)

diff --git a/man/tymux.1 b/man/tymux.1
index 05f259d0..7f323845 100644
--- a/man/tymux.1
+++ b/man/tymux.1
@@ -175,6 +175,23 @@ in its own key input, the same chord the inline proxy uses (see
 Ctrl+b followed by any other key, including a second Ctrl+b, is sent to the
 session untouched. Pasted or dropped text is never scanned for the chord,
 even if it contains a literal Ctrl+b: only an actual key press can detach.
+.PP
+Leaving the session, whether by that chord, by
+.BR "tymux kill" ,
+or because its shell exited, never closes a terminal that was switched into
+it from a local shell: a fresh shell replaces the session right there, in
+the directory the terminal was in at the time of the hand-off, the same way
+detaching in
+.B tmux
+returns you to your own shell. A terminal opened directly on a session
+(\fBterminology \-\-tymux\fP=\fINAME\fP, or a new tab from \fB\-\-new\-tab\fP)
+has no earlier local shell to return to: detaching still replaces it with a
+plain shell, so the window stays usable, but the session's own shell
+exiting (typing
+.B exit ,
+or
+.BR "tymux kill" )
+closes the terminal, like any other terminal whose shell exited.
 .SH BUGS
 A terminal a native hand-off (see \fBNATIVE HAND\-OFF\fP) attaches to has
 its own scrollback: the daemon streams every line as it scrolls off its
diff --git a/src/bin/termio.c b/src/bin/termio.c
index 9f15b3eb..37a16615 100644
--- a/src/bin/termio.c
+++ b/src/bin/termio.c
@@ -41,6 +41,7 @@ static Eina_List *terms = NULL;
 
 static void _smart_apply(Evas_Object *obj);
 static void _smart_size(Evas_Object *obj, int w, int h, Eina_Bool force);
+static void _termio_pty_cb_wire(Evas_Object *obj, Termio *sd);
 static void _smart_calculate(Evas_Object *obj);
 static Eina_Bool _mouse_in_selection(Termio *sd, int cx, int cy);
 
@@ -3940,6 +3941,7 @@ _smart_del(Evas_Object *obj)
    eina_stringshare_del(sd->tymux_pending);
    if (sd->tymux_newtab_job) ecore_job_del(sd->tymux_newtab_job);
    eina_stringshare_del(sd->tymux_newtab_pending);
+   eina_stringshare_del(sd->tymux_launch_cwd);
    if (sd->tymux) termtymux_free(sd->tymux); /* also frees sd->pty */
    else if (sd->pty) termpty_free(sd->pty);
 #else
@@ -4116,13 +4118,80 @@ _smart_pty_cancel_sel(void *data)
      }
 }
 
+#ifdef HAVE_TYMUX
+/* Replaces sd->tymux's shadow pty with a fresh local shell in the same
+ * Termio, instead of closing the terminal: called instead of term_close()
+ * from _smart_pty_exited() when leaving the session should keep the
+ * terminal around (see there for which cases those are). Reuses
+ * termio_add()'s own plain-pty creation and callback wiring
+ * (_termio_pty_cb_wire()) rather than duplicating it. */
+static void
+_termio_tymux_leave(Evas_Object *obj, Termio *sd)
+{
+   const char *cwd = sd->tymux_switched_in ? sd->tymux_launch_cwd : NULL;
+   Ecore_Window window_id = elm_win_window_id_get(sd->win);
+
+   termtymux_free(sd->tymux); /* also frees the shadow pty */
+   sd->tymux = NULL;
+   sd->pty = NULL;
+
+   sd->pty = termpty_new(NULL, EINA_TRUE, cwd, sd->grid.w, sd->grid.h,
+                         sd->config, NULL, window_id);
+   if (!sd->pty)
+     {
+        ERR(_("Could not allocate termpty"));
+        term_close(sd->win, sd->self, EINA_TRUE);
+        return;
+     }
+   _termio_pty_cb_wire(obj, sd);
+
+   eina_stringshare_replace(&sd->tymux_launch_cwd, NULL);
+   sd->tymux_switched_in = EINA_FALSE;
+
+   termio_remove_links(sd);
+   if (sd->have_sel)
+     {
+        eina_stringshare_del(sd->sel_str);
+        sd->sel_str = NULL;
+        termio_sel_set(sd, EINA_FALSE);
+        sd->have_sel = EINA_FALSE;
+     }
+   sd->scroll = 0;
+
+   term_tymux_pause_apply(obj); /* clears the pause overlay */
+   _smart_apply(obj);
+}
+#endif
+
 static void
 _smart_pty_exited(void *data)
 {
-   Termio *sd = evas_object_smart_data_get(data);
+   Evas_Object *obj = data;
+   Termio *sd = evas_object_smart_data_get(obj);
 
    EINA_SAFETY_ON_NULL_RETURN(sd);
 
+#ifdef HAVE_TYMUX
+   if (sd->tymux)
+     {
+        /* Detaching (our own native chord, tymux.c's, or the daemon going
+         * away) always leaves the terminal running with a fresh local
+         * shell, the same way tmux returns you to your shell on detach.
+         * When the session's own shell exits instead, a terminal switched
+         * in from a local shell (the escape hand-off) gets that shell
+         * back, in its original directory; one created directly on the
+         * session (termio_add()'s tymux_name, a new tab) has no such
+         * shell to return to, so it closes like any other terminal whose
+         * shell exited. */
+        if (termtymux_end_reason_get(sd->tymux) == TERMTYMUX_END_DETACHED ||
+            sd->tymux_switched_in)
+          {
+             _termio_tymux_leave(obj, sd);
+             return;
+          }
+     }
+#endif
+
    if (sd->event)
      {
         evas_object_event_callback_del(sd->event, EVAS_CALLBACK_MOUSE_DOWN,
@@ -4330,7 +4399,12 @@ _termio_tymux_switch_job(void *data)
    /* Only now drop the pty being replaced. If it is a plain shell (the
     * one that ran the 'tymux attach' helper which emitted this escape),
     * closing its pty master sends it SIGHUP like any other terminal exit;
-    * nothing needs to be kept alive for it. */
+    * nothing needs to be kept alive for it, beyond its cwd: leaving the
+    * session later starts a fresh shell right back where this one was
+    * (_termio_tymux_leave()). A hand-off straight from one session to
+    * another leaves that cwd, and tymux_switched_in, as they already are:
+    * this terminal's local shell, if it has one to go back to, is further
+    * back than the session being replaced. */
    if (sd->tymux)
      {
         termtymux_free(sd->tymux); /* also frees the old sd->pty */
@@ -4339,28 +4413,18 @@ _termio_tymux_switch_job(void *data)
      }
    else if (sd->pty)
      {
+        char cwd[PATH_MAX];
+
+        if (termio_cwd_get(obj, cwd, sizeof(cwd)))
+          eina_stringshare_replace(&sd->tymux_launch_cwd, cwd);
+        sd->tymux_switched_in = EINA_TRUE;
         termpty_free(sd->pty);
         sd->pty = NULL;
      }
 
    sd->pty = spty;
-   sd->pty->obj = obj;
    sd->tymux = tymux;
-
-   sd->pty->cb.change.func = _smart_pty_change;
-   sd->pty->cb.change.data = ""
-   sd->pty->cb.set_title.func = _smart_pty_title;
-   sd->pty->cb.set_title.data = ""
-   sd->pty->cb.set_icon.func = _smart_pty_icon;
-   sd->pty->cb.set_icon.data = ""
-   sd->pty->cb.cancel_sel.func = _smart_pty_cancel_sel;
-   sd->pty->cb.cancel_sel.data = ""
-   sd->pty->cb.exited.func = _smart_pty_exited;
-   sd->pty->cb.exited.data = ""
-   sd->pty->cb.bell.func = _smart_pty_bell;
-   sd->pty->cb.bell.data = ""
-   sd->pty->cb.command.func = _smart_pty_command;
-   sd->pty->cb.command.data = ""
+   _termio_pty_cb_wire(obj, sd);
    sd->tymux->cb_pause.func = _termio_tymux_pause;
    sd->tymux->cb_pause.data = ""
 
@@ -4819,6 +4883,35 @@ _smart_pty_command(void *data)
    evas_object_smart_callback_call(obj, "command", (void *)ty->cur_cmd);
 }
 
+/* Wires sd->pty's callbacks the same way for every pty termio ever owns:
+ * the one termio_add() starts with, one a tymux hand-off switches sd->pty
+ * to (_termio_tymux_switch_job()), and the local shell that replaces a
+ * tymux shadow pty when its session is detached or left
+ * (_termio_tymux_leave()). */
+static void
+_termio_pty_cb_wire(Evas_Object *obj, Termio *sd)
+{
+   sd->pty->obj = obj;
+   sd->pty->cb.change.func = _smart_pty_change;
+   sd->pty->cb.change.data = ""
+   sd->pty->cb.set_title.func = _smart_pty_title;
+   sd->pty->cb.set_title.data = ""
+   sd->pty->cb.set_icon.func = _smart_pty_icon;
+   sd->pty->cb.set_icon.data = ""
+   sd->pty->cb.cancel_sel.func = _smart_pty_cancel_sel;
+   sd->pty->cb.cancel_sel.data = ""
+   sd->pty->cb.exited.func = _smart_pty_exited;
+   sd->pty->cb.exited.data = ""
+   sd->pty->cb.bell.func = _smart_pty_bell;
+   sd->pty->cb.bell.data = ""
+   sd->pty->cb.command.func = _smart_pty_command;
+   sd->pty->cb.command.data = ""
+   sd->pty->cb.progress.func = _smart_pty_progress;
+   sd->pty->cb.progress.data = ""
+   sd->pty->cb.program_status.func = _smart_pty_program_status;
+   sd->pty->cb.program_status.data = ""
+}
+
 static void
 _smart_cb_drag_enter(void *_data EINA_UNUSED, Evas_Object *_o EINA_UNUSED)
 {
@@ -5041,25 +5134,7 @@ termio_add(Evas_Object *win, Config *config,
         evas_object_del(obj);
         return NULL;
      }
-   sd->pty->obj = obj;
-   sd->pty->cb.change.func = _smart_pty_change;
-   sd->pty->cb.change.data = ""
-   sd->pty->cb.set_title.func = _smart_pty_title;
-   sd->pty->cb.set_title.data = ""
-   sd->pty->cb.set_icon.func = _smart_pty_icon;
-   sd->pty->cb.set_icon.data = ""
-   sd->pty->cb.cancel_sel.func = _smart_pty_cancel_sel;
-   sd->pty->cb.cancel_sel.data = ""
-   sd->pty->cb.exited.func = _smart_pty_exited;
-   sd->pty->cb.exited.data = ""
-   sd->pty->cb.bell.func = _smart_pty_bell;
-   sd->pty->cb.bell.data = ""
-   sd->pty->cb.command.func = _smart_pty_command;
-   sd->pty->cb.command.data = ""
-   sd->pty->cb.progress.func = _smart_pty_progress;
-   sd->pty->cb.progress.data = ""
-   sd->pty->cb.program_status.func = _smart_pty_program_status;
-   sd->pty->cb.program_status.data = ""
+   _termio_pty_cb_wire(obj, sd);
    _smart_size(obj, w, h, EINA_TRUE);
    return obj;
 }
diff --git a/src/bin/termiointernals.h b/src/bin/termiointernals.h
index 6126f23d..252e33c7 100644
--- a/src/bin/termiointernals.h
+++ b/src/bin/termiointernals.h
@@ -128,6 +128,14 @@ struct tag_Termio
    const char *tymux_newtab_pending; /* eina_stringshare; deferred new-tab target */
    TermSrvAttachMode tymux_newtab_pending_mode;
    Ecore_Job *tymux_newtab_job;
+   /* Set only while sd->tymux replaced a local shell (the escape hand-off,
+    * _termio_tymux_switch_job()): tymux_launch_cwd is that shell's cwd,
+    * snapshotted before its pty was freed, so leaving the session can
+    * start a fresh shell right back where the user was. Unset for a
+    * terminal created directly on a session (termio_add()'s tymux_name,
+    * or a new tab), which has no such shell to return to. */
+   Eina_Bool tymux_switched_in;
+   const char *tymux_launch_cwd; /* eina_stringshare */
 #endif
    Ecore_Animator *anim;
    Ecore_Timer *delayed_size_timer;
diff --git a/src/bin/termtymux.c b/src/bin/termtymux.c
index c5556d25..ce1a3713 100644
--- a/src/bin/termtymux.c
+++ b/src/bin/termtymux.c
@@ -289,7 +289,8 @@ _write_hook(void *data, Termpty *ty, const char *input, int len)
           * client once it sees DETACH (termd_tymux.c
           * _client_msg_handle()) instead of answering, so the detach is
           * only complete once the socket closes: _cb_fd() then sees EOF
-          * and fires cb.exited. */
+          * and fires cb.exited with end_reason left at its default,
+          * TERMTYMUX_END_DETACHED. */
          termsrv_msg_send(ts->sock_fd, TSRV_MSG_DETACH, NULL, 0);
          break;
       case TERMTYMUX_INPUT_DROP:
@@ -359,6 +360,7 @@ _msg_handle(TermTymux *ts, int type, void *payload, uint32_t size)
         break;
       case TSRV_MSG_EXIT:
         ty->pid = -1;
+        ts->end_reason = TERMTYMUX_END_EXITED;
         if (size == sizeof(TermSrvMsgExit))
           ty->exit_code = ((TermSrvMsgExit *)payload)->exit_code;
         return EINA_TRUE;
@@ -583,6 +585,13 @@ termtymux_paused_get(const TermTymux *ts)
    return ts->paused;
 }
 
+TermTymuxEndReason
+termtymux_end_reason_get(const TermTymux *ts)
+{
+   if (!ts) return TERMTYMUX_END_DETACHED;
+   return ts->end_reason;
+}
+
 /* arg is everything after "at" in ty->cur_cmd, which is also where the
  * pre-existing 'at[on/true/yes/off/false/no]' temporary-alpha command
  * (win.c _cb_command(), values "on"/"true"/"yes"/"off"/"false"/"no")
diff --git a/src/bin/termtymux.h b/src/bin/termtymux.h
index 9f26e018..b61e94f5 100644
--- a/src/bin/termtymux.h
+++ b/src/bin/termtymux.h
@@ -16,6 +16,18 @@
 
 typedef struct _TermTymux TermTymux;
 
+/* Why the session stopped being mirrored, as far as the caller of
+ * termtymux_new()'s ty_out->cb.exited can tell: no TSRV_MSG_EXIT ever
+ * distinguishes "we asked to detach" from "the daemon went away", both of
+ * which look like the socket closing with nothing more to read; either way
+ * the session itself is assumed to still be running, unlike an actual
+ * exit. */
+typedef enum
+{
+   TERMTYMUX_END_DETACHED, /* our own chord, or the daemon/connection gone */
+   TERMTYMUX_END_EXITED    /* TSRV_MSG_EXIT: the session's shell exited */
+} TermTymuxEndReason;
+
 struct _TermTymux
 {
    int               sock_fd;
@@ -30,6 +42,7 @@ struct _TermTymux
    Eina_Bool         paused;      /* another client hijacked the session */
    TermSrvChord      chord;       /* native Ctrl+b d detach chord state */
    Eina_Binbuf      *chord_out;   /* reused across _write_hook() calls */
+   TermTymuxEndReason end_reason; /* valid once ty_out->cb.exited fires */
    struct
      {
         void (*func) (void *data, Eina_Bool paused);
@@ -95,6 +108,10 @@ pid_t termtymux_shell_pid_get(const TermTymux *ts);
  * received and no TSRV_MSG_RESUME followed yet). EINA_FALSE for ts == NULL. */
 Eina_Bool termtymux_paused_get(const TermTymux *ts);
 
+/* Why ts's session stopped being mirrored, valid once its shadow pty's
+ * cb.exited fires. TERMTYMUX_END_DETACHED for ts == NULL. */
+TermTymuxEndReason termtymux_end_reason_get(const TermTymux *ts);
+
 /* Parses the argument of Terminology's private native hand-off escape,
  * "\033}at<variant>;NAME\0", where arg points right after "at":
  *   ";NAME"   -> TSRV_ATTACH_HIJACK

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.

Reply via email to