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 c58a2c34d3bd3903be3538295dafd35a1654a542
Author: [email protected] <[email protected]>
AuthorDate: Fri Sep 25 18:09:28 2026 -0600

    termsrv: share the tymux detach key chord
    
    Terminology is going to recognise Ctrl+b d itself in a terminal showing
    a session natively, so the chord moves from tymux.c to termsrv, where
    both can use it.
    
    A Ctrl+b followed by any other byte is forwarded along with it, so
    Ctrl+b still reaches the application. A Ctrl+b ending a read is held
    until the next one, so a chord split across two reads still detaches.
    Since that can make the output one byte longer than the input, the
    filter appends to an Eina_Binbuf the caller keeps rather than to a
    buffer the caller has to size. termsrv_chord_flush() releases a held
    Ctrl+b for a caller that knows the next bytes cannot complete the
    chord.
---
 src/bin/termsrv.c    | 181 +++++++++++++++++++++++++++++++++++++++++++++++++++
 src/bin/termsrv.h    |  38 +++++++++++
 src/bin/tymux.c      |  53 +++++++--------
 src/bin/tytest.c     |   2 +
 src/bin/unit_tests.h |   2 +
 tests/tymux.py       |  11 ++++
 6 files changed, 258 insertions(+), 29 deletions(-)

diff --git a/src/bin/termsrv.c b/src/bin/termsrv.c
index 7690fc84..550155d7 100644
--- a/src/bin/termsrv.c
+++ b/src/bin/termsrv.c
@@ -643,6 +643,46 @@ err:
    return -1;
 }
 
+Eina_Bool
+termsrv_chord_filter(TermSrvChord *chord, const char *in, int in_len,
+                     Eina_Binbuf *out)
+{
+   int i;
+
+   EINA_SAFETY_ON_NULL_RETURN_VAL(out, EINA_FALSE);
+   for (i = 0; i < in_len; i++)
+     {
+        unsigned char c = (unsigned char)in[i];
+
+        if (chord->prefix_pending)
+          {
+             unsigned char pair[2] = { TERMSRV_CHORD_PREFIX, c };
+
+             chord->prefix_pending = 0;
+             if (c == TERMSRV_CHORD_KEY)
+               return EINA_TRUE;
+             eina_binbuf_append_length(out, pair, sizeof(pair));
+          }
+        else if (c == TERMSRV_CHORD_PREFIX)
+          chord->prefix_pending = 1;
+        else
+          eina_binbuf_append_length(out, &c, 1);
+     }
+   return EINA_FALSE;
+}
+
+Eina_Bool
+termsrv_chord_flush(TermSrvChord *chord, Eina_Binbuf *out)
+{
+   unsigned char c = TERMSRV_CHORD_PREFIX;
+
+   EINA_SAFETY_ON_NULL_RETURN_VAL(out, EINA_FALSE);
+   if (!chord->prefix_pending) return EINA_FALSE;
+   chord->prefix_pending = 0;
+   eina_binbuf_append_length(out, &c, 1);
+   return EINA_TRUE;
+}
+
 #if defined(BINARY_TYTEST)
 #include <assert.h>
 #include <fcntl.h>
@@ -650,6 +690,147 @@ err:
 #include <pthread.h>
 #include <Elementary.h>
 #include "termpty.h"
+
+/* Test: termsrv_chord_filter() forwards plain input untouched, drops the
+ * detach chord, forwards Ctrl+b followed by anything else, and recognizes
+ * a chord split across two calls. out is one Eina_Binbuf reused (and reset)
+ * across cases, as a real caller is expected to reuse one per client. */
+int
+tytest_termsrv_chord_filter(void)
+{
+   TermSrvChord chord;
+   Eina_Binbuf *out = eina_binbuf_new();
+
+   assert(out);
+
+   memset(&chord, 0, sizeof(chord));
+   eina_binbuf_reset(out);
+   assert(termsrv_chord_filter(&chord, "hi", 2, out) == EINA_FALSE);
+   assert(eina_binbuf_length_get(out) == 2 &&
+          !memcmp(eina_binbuf_string_get(out), "hi", 2));
+
+   /* Ctrl+b followed by anything but 'd' is forwarded whole */
+   memset(&chord, 0, sizeof(chord));
+   eina_binbuf_reset(out);
+   assert(termsrv_chord_filter(&chord, "p\x02q", 3, out) == EINA_FALSE);
+   assert(eina_binbuf_length_get(out) == 3 &&
+          !memcmp(eina_binbuf_string_get(out), "p\x02q", 3));
+
+   /* Ctrl+b Ctrl+b forwards both bytes of the pair; only "Ctrl+b d"
+    * detaches, so the second Ctrl+b is treated as ruling out the chord,
+    * the same as any other non-'d' byte */
+   memset(&chord, 0, sizeof(chord));
+   eina_binbuf_reset(out);
+   assert(termsrv_chord_filter(&chord, "\x02\x02", 2, out) == EINA_FALSE);
+   assert(eina_binbuf_length_get(out) == 2 &&
+          !memcmp(eina_binbuf_string_get(out), "\x02\x02", 2));
+   assert(!chord.prefix_pending);
+
+   /* the chord, in one call */
+   memset(&chord, 0, sizeof(chord));
+   eina_binbuf_reset(out);
+   assert(termsrv_chord_filter(&chord, "x\x02" "d", 3, out) == EINA_TRUE);
+   assert(eina_binbuf_length_get(out) == 1 &&
+          ((const char *)eina_binbuf_string_get(out))[0] == 'x');
+
+   /* the chord, split across two calls: Ctrl+b at the end of one, 'd' at
+    * the start of the next */
+   memset(&chord, 0, sizeof(chord));
+   eina_binbuf_reset(out);
+   assert(termsrv_chord_filter(&chord, "x\x02", 2, out) == EINA_FALSE);
+   assert(eina_binbuf_length_get(out) == 1 &&
+          ((const char *)eina_binbuf_string_get(out))[0] == 'x');
+   assert(chord.prefix_pending);
+   eina_binbuf_reset(out);
+   assert(termsrv_chord_filter(&chord, "d", 1, out) == EINA_TRUE);
+   assert(eina_binbuf_length_get(out) == 0);
+
+   /* a trailing Ctrl+b held back, then ruled out by the next call's byte */
+   memset(&chord, 0, sizeof(chord));
+   eina_binbuf_reset(out);
+   assert(termsrv_chord_filter(&chord, "x\x02", 2, out) == EINA_FALSE);
+   assert(eina_binbuf_length_get(out) == 1 &&
+          ((const char *)eina_binbuf_string_get(out))[0] == 'x');
+   eina_binbuf_reset(out);
+   assert(termsrv_chord_filter(&chord, "q", 1, out) == EINA_FALSE);
+   assert(eina_binbuf_length_get(out) == 2 &&
+          !memcmp(eina_binbuf_string_get(out), "\x02q", 2));
+   assert(!chord.prefix_pending);
+
+   /* a long write with a Ctrl+b held from a previous call: nothing is
+    * dropped or truncated, unlike the fixed-size buffers this used to
+    * require (see git history: out[sizeof(in)] in tymux.c and
+    * out[_CHORD_BUF_MAX] in termtymux.c once overflowed by exactly this
+    * case). big_in is bigger than termtymux.c's old _CHORD_STACK_MAX (64)
+    * to also exercise the case in tytest_termtymux_input_filter_long(). */
+   {
+      char big_in[100];
+      int i;
+
+      for (i = 0; i < (int)sizeof(big_in); i++) big_in[i] = 'a' + (i % 26);
+
+      memset(&chord, 0, sizeof(chord));
+      eina_binbuf_reset(out);
+      assert(termsrv_chord_filter(&chord, "\x02", 1, out) == EINA_FALSE);
+      assert(eina_binbuf_length_get(out) == 0 && chord.prefix_pending);
+      assert(termsrv_chord_filter(&chord, big_in, (int)sizeof(big_in),
+                                  out) == EINA_FALSE);
+      assert(eina_binbuf_length_get(out) == (size_t)sizeof(big_in) + 1);
+      assert(((const unsigned char *)eina_binbuf_string_get(out))[0] ==
+             TERMSRV_CHORD_PREFIX);
+      assert(!memcmp((const char *)eina_binbuf_string_get(out) + 1,
+                     big_in, sizeof(big_in)));
+      assert(!chord.prefix_pending);
+
+      /* the same held Ctrl+b, but this long call's first byte completes
+       * the chord instead of ruling it out */
+      memset(&chord, 0, sizeof(chord));
+      eina_binbuf_reset(out);
+      assert(termsrv_chord_filter(&chord, "\x02", 1, out) == EINA_FALSE);
+      assert(eina_binbuf_length_get(out) == 0 && chord.prefix_pending);
+      big_in[0] = 'd';
+      eina_binbuf_reset(out);
+      assert(termsrv_chord_filter(&chord, big_in, (int)sizeof(big_in),
+                                  out) == EINA_TRUE);
+      assert(eina_binbuf_length_get(out) == 0);
+      assert(!chord.prefix_pending);
+   }
+
+   eina_binbuf_free(out);
+   return 0;
+}
+
+/* Test: termsrv_chord_flush() releases a held Ctrl+b on its own and clears
+ * prefix_pending, or does nothing when none is held. */
+int
+tytest_termsrv_chord_flush(void)
+{
+   TermSrvChord chord;
+   Eina_Binbuf *out = eina_binbuf_new();
+
+   assert(out);
+
+   memset(&chord, 0, sizeof(chord));
+   assert(termsrv_chord_flush(&chord, out) == EINA_FALSE);
+   assert(eina_binbuf_length_get(out) == 0);
+
+   memset(&chord, 0, sizeof(chord));
+   assert(termsrv_chord_filter(&chord, "\x02", 1, out) == EINA_FALSE);
+   assert(eina_binbuf_length_get(out) == 0 && chord.prefix_pending);
+   assert(termsrv_chord_flush(&chord, out) == EINA_TRUE);
+   assert(eina_binbuf_length_get(out) == 1 &&
+          ((const unsigned char *)eina_binbuf_string_get(out))[0] ==
+          TERMSRV_CHORD_PREFIX);
+   assert(!chord.prefix_pending);
+
+   /* flushing again with nothing held is a no-op */
+   eina_binbuf_reset(out);
+   assert(termsrv_chord_flush(&chord, out) == EINA_FALSE);
+   assert(eina_binbuf_length_get(out) == 0);
+
+   eina_binbuf_free(out);
+   return 0;
+}
 #include "unit_tests.h"
 
 int
diff --git a/src/bin/termsrv.h b/src/bin/termsrv.h
index abf1c55b..491434c4 100644
--- a/src/bin/termsrv.h
+++ b/src/bin/termsrv.h
@@ -198,6 +198,44 @@ int termsrv_shm_read(const TermSrvShmHeader *shm, TermSrvFrame *frame,
 /* Returns 0 if name can be used as a tymux name, -1 otherwise. */
 int termsrv_name_check(const char *name);
 
+/* Ctrl+b d, as in tmux: the detach chord shared by every client that reads
+ * raw keyboard bytes on behalf of a user (the command line client in
+ * tymux.c, and the native client's key input in termtymux.c). */
+#define TERMSRV_CHORD_PREFIX 0x02
+#define TERMSRV_CHORD_KEY    'd'
+
+/* Holds a Ctrl+b seen at the end of one termsrv_chord_filter() call so a
+ * chord split across two calls (Ctrl+b at the end of one read or key,
+ * 'd' at the start of the next) is still recognized. Zero it before use. */
+typedef struct
+{
+   int prefix_pending;
+} TermSrvChord;
+
+/* Appends in[0..in_len) to out, an Eina_Binbuf the caller owns (and should
+ * reset before each call it wants to start fresh, to reuse one binbuf
+ * across many calls instead of allocating one per keystroke), leaving out
+ * the "Ctrl+b d" detach chord. A Ctrl+b followed by any other byte, Ctrl+b
+ * included, is forwarded with it, so Ctrl+b still reaches the application.
+ * A Ctrl+b ending in is held in *chord and resolved by the next call.
+ *
+ * Returns EINA_TRUE when the chord completed: out holds only the bytes
+ * before it, and the rest of in must be ignored, as it was typed after the
+ * detach. Returns EINA_FALSE otherwise, with out holding what to
+ * forward. */
+Eina_Bool termsrv_chord_filter(TermSrvChord *chord, const char *in,
+                               int in_len, Eina_Binbuf *out);
+
+/* Flushes a Ctrl+b held in *chord (chord->prefix_pending) by appending it
+ * to out, an Eina_Binbuf the caller owns, and clears prefix_pending; does
+ * nothing if none was held. For input that must never be scanned for the
+ * rest of the chord, such as pasted text: a held Ctrl+b was still actually
+ * typed and has to reach the application, but nothing in that other input
+ * can complete "Ctrl+b d", so it is forwarded on its own instead of being
+ * handed to termsrv_chord_filter() ahead of it. Returns EINA_TRUE if a
+ * byte was held, EINA_FALSE otherwise. */
+Eina_Bool termsrv_chord_flush(TermSrvChord *chord, Eina_Binbuf *out);
+
 /* Fill buf with $XDG_RUNTIME_DIR/terminology/tymux, or
  * /tmp/terminology/tymux when XDG_RUNTIME_DIR is unset.
  * Returns 0 on success, -1 if buf is too small. */
diff --git a/src/bin/tymux.c b/src/bin/tymux.c
index a8c4f834..3a9556fc 100644
--- a/src/bin/tymux.c
+++ b/src/bin/tymux.c
@@ -19,10 +19,6 @@
 #include <Ecore.h>
 #include "termsrv.h"
 
-/* Ctrl+b d, as in tmux */
-#define DETACH_PREFIX 0x02
-#define DETACH_KEY    'd'
-
 typedef struct
 {
    int                     sock;
@@ -33,7 +29,8 @@ typedef struct
    size_t                  cells_cap;
    bool                    prev_valid;
    uint32_t                prev_cols, prev_rows;
-   bool                    prefix_pending;
+   TermSrvChord            chord;
+   Eina_Binbuf            *chord_out; /* reused across _input_handle() calls */
    bool                    paused;
    bool                    running;
    TermSrvReader           reader;
@@ -381,15 +378,14 @@ _detach(Proxy *proxy)
    proxy->running = false;
 }
 
-/* Forward what was typed, minus the detach chord: a prefix not followed by
- * the detach key is sent along with that key. While paused, only the chord
- * is honored. */
+/* Forward what was typed, minus the detach chord (termsrv_chord_filter(),
+ * shared with the native client's key input in termtymux.c). While paused,
+ * only the chord is honored. */
 static void
 _input_handle(Proxy *proxy)
 {
-   char in[4096], out[sizeof(in) + 1];
-   size_t out_len = 0;
-   ssize_t n, i;
+   char in[4096];
+   ssize_t n;
 
    n = read(STDIN_FILENO, in, sizeof(in));
    if (n < 0 && errno == EINTR)
@@ -399,26 +395,16 @@ _input_handle(Proxy *proxy)
         _detach(proxy);
         return;
      }
-   for (i = 0; i < n; i++)
+   eina_binbuf_reset(proxy->chord_out);
+   if (termsrv_chord_filter(&proxy->chord, in, (int)n, proxy->chord_out))
      {
-        if (proxy->prefix_pending)
-          {
-             proxy->prefix_pending = false;
-             if (in[i] == DETACH_KEY)
-               {
-                  _detach(proxy);
-                  return;
-               }
-             out[out_len++] = DETACH_PREFIX;
-             out[out_len++] = in[i];
-          }
-        else if (in[i] == DETACH_PREFIX)
-          proxy->prefix_pending = true;
-        else
-          out[out_len++] = in[i];
+        _detach(proxy);
+        return;
      }
-   if (out_len > 0 && !proxy->paused &&
-       termsrv_msg_send(proxy->sock, TSRV_MSG_INPUT, out, out_len) < 0)
+   if (eina_binbuf_length_get(proxy->chord_out) > 0 && !proxy->paused &&
+       termsrv_msg_send(proxy->sock, TSRV_MSG_INPUT,
+                        eina_binbuf_string_get(proxy->chord_out),
+                        (uint32_t)eina_binbuf_length_get(proxy->chord_out)) < 0)
      proxy->running = false;
 }
 
@@ -597,12 +583,20 @@ _proxy_run(int sock)
         munmap(shm, TERMSRV_SHM_TOTAL_SIZE);
         return 1;
      }
+   proxy.chord_out = eina_binbuf_new();
+   if (!proxy.chord_out)
+     {
+        perror("tymux: malloc");
+        munmap(shm, TERMSRV_SHM_TOTAL_SIZE);
+        return 1;
+     }
 
    if (pipe2(proxy.winch_pipe, O_CLOEXEC | O_NONBLOCK) < 0)
      {
         perror("tymux: pipe");
         free(proxy.cur);
         free(proxy.prev);
+        eina_binbuf_free(proxy.chord_out);
         munmap(shm, TERMSRV_SHM_TOTAL_SIZE);
         return 1;
      }
@@ -646,6 +640,7 @@ _proxy_run(int sock)
    _write_str("\033[0m\033[?25h\r\n");
    free(proxy.cur);
    free(proxy.prev);
+   eina_binbuf_free(proxy.chord_out);
    termsrv_reader_clear(&proxy.reader);
    munmap(shm, TERMSRV_SHM_TOTAL_SIZE);
    return proxy.exit_code;
diff --git a/src/bin/tytest.c b/src/bin/tytest.c
index ba3840d6..bc104a0f 100644
--- a/src/bin/tytest.c
+++ b/src/bin/tytest.c
@@ -157,6 +157,8 @@ static struct {
        { "fuzz_resize_max", tytest_fuzz_resize_max},
 #if defined(HAVE_TYMUX)
        { "termsrv_name", tytest_termsrv_name},
+       { "termsrv_chord_filter", tytest_termsrv_chord_filter},
+       { "termsrv_chord_flush", tytest_termsrv_chord_flush},
        { "termsrv_socket_path", tytest_termsrv_socket_path},
        { "termsrv_tymux_dir_ensure", tytest_termsrv_tymux_dir_ensure},
        { "termsrv_daemon_kill", tytest_termsrv_daemon_kill},
diff --git a/src/bin/unit_tests.h b/src/bin/unit_tests.h
index 348546c4..b05f5214 100644
--- a/src/bin/unit_tests.h
+++ b/src/bin/unit_tests.h
@@ -115,6 +115,8 @@ int tytest_fuzz_header(void);
 int tytest_fuzz_resize_max(void);
 #if defined(HAVE_TYMUX)
 int tytest_termsrv_name(void);
+int tytest_termsrv_chord_filter(void);
+int tytest_termsrv_chord_flush(void);
 int tytest_termsrv_socket_path(void);
 int tytest_termsrv_tymux_dir_ensure(void);
 int tytest_termsrv_daemon_kill(void);
diff --git a/tests/tymux.py b/tests/tymux.py
index af2cf9e9..86fe9850 100644
--- a/tests/tymux.py
+++ b/tests/tymux.py
@@ -191,6 +191,17 @@ def main():
     a = Client(["attach", NAME])
     check(a.expect("hello-42"), "attach: the screen is drawn again")
 
+    # the detach chord split across two reads (Ctrl+b at the end of one,
+    # 'd' at the start of the next): termsrv_chord_filter() keeps the
+    # pending prefix in Proxy.chord between calls to _input_handle().
+    a.send("\x02")
+    a.idle(0.2)
+    a.send("d")
+    check(a.wait() == 0, "Ctrl+b d detaches even split across two reads")
+
+    a = Client(["attach", NAME])
+    check(a.expect("hello-42"), "attach: the screen is drawn again")
+
     readonly = Client(["attach", "--readonly", NAME])
     check(readonly.expect("hello-42"), "readonly: the screen is drawn")
     readonly.send("echo readonly-$((2+2))\r")

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

Reply via email to