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 0c9d55ee3eeb715b9c08f3fc416c52e03300b76f
Author: Boris Faure <[email protected]>
AuthorDate: Sat Nov 14 22:31:13 2026 +0000

    termptyesc: track the kitty keyboard flags per screen
    
    Parse the push, pop and set escapes of the kitty keyboard protocol:
    https://sw.kovidgoyal.net/kitty/keyboard-protocol/#progressive-enhancement
    
    The spec requires a separate stack for each screen, so they are indexed
    by altbuf like the saved cursor, and capped at 8 entries against abuse.
    RIS and DECSTR clear both, like every other mode.
    
    The query (CSI ? u) stays unanswered until the encoder exists, so that
    a client never sees a reply advertising enhancements that do nothing.
---
 src/bin/termpty.c    | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/bin/termpty.h    | 15 +++++++++
 src/bin/termptyesc.c | 42 +++++++++++++++++++++++-
 src/bin/termptyops.c | 69 ++++++++++++++++++++++++++++++++++++++
 src/bin/termptyops.h |  9 +++++
 src/bin/tytest.c     |  1 +
 src/bin/unit_tests.h |  1 +
 7 files changed, 229 insertions(+), 1 deletion(-)

diff --git a/src/bin/termpty.c b/src/bin/termpty.c
index 003aef87..589ca18a 100644
--- a/src/bin/termpty.c
+++ b/src/bin/termpty.c
@@ -3443,6 +3443,99 @@ tytest_kitty_keyboard_ignored(void)
    return 0;
 }
 
+/* The kitty keyboard flag stack. Values are not masked here: the escape
+ * handler masks, the stack functions store anything. */
+int
+tytest_kitty_kbd_stack(void)
+{
+   Termpty ty;
+   int i;
+
+   _ty_test_init(&ty, 80, 24);
+
+   /* fresh state */
+   assert(termpty_kitty_kbd_flags_get(&ty) == 0);
+   assert(ty.kitty_kbd[0].depth == 0);
+   assert(ty.kitty_kbd[1].depth == 0);
+
+   /* a push creates the base entry, a pop returns to it */
+   termpty_kitty_kbd_push(&ty, 5);
+   assert(termpty_kitty_kbd_flags_get(&ty) == 5);
+   assert(ty.kitty_kbd[0].depth == 2);
+   termpty_kitty_kbd_pop(&ty, 1);
+   assert(termpty_kitty_kbd_flags_get(&ty) == 0);
+   assert(ty.kitty_kbd[0].depth == 1);
+
+   /* 9 pushes into 8 slots evict the base and the first value */
+   termpty_kitty_kbd_pop(&ty, 10);
+   for (i = 1; i <= 9; i++)
+     termpty_kitty_kbd_push(&ty, i);
+   assert(ty.kitty_kbd[0].depth == TERMPTY_KITTY_KBD_STACK_SIZE);
+   assert(termpty_kitty_kbd_flags_get(&ty) == 9);
+   for (i = 8; i >= 2; i--)
+     {
+        termpty_kitty_kbd_pop(&ty, 1);
+        assert(termpty_kitty_kbd_flags_get(&ty) == i);
+     }
+   termpty_kitty_kbd_pop(&ty, 1);
+   assert(ty.kitty_kbd[0].depth == 0);
+   assert(termpty_kitty_kbd_flags_get(&ty) == 0);
+
+   /* a set on an empty stack creates the base entry */
+   termpty_kitty_kbd_set(&ty, 3, 1);
+   termpty_kitty_kbd_set(&ty, 4, 2);
+   assert(termpty_kitty_kbd_flags_get(&ty) == 7);
+   termpty_kitty_kbd_set(&ty, 1, 3);
+   assert(termpty_kitty_kbd_flags_get(&ty) == 6);
+   /* modes outside 1..3 are ignored */
+   termpty_kitty_kbd_set(&ty, 1, 0);
+   termpty_kitty_kbd_set(&ty, 1, 4);
+   assert(termpty_kitty_kbd_flags_get(&ty) == 6);
+
+   /* over-pop clamps to an empty stack */
+   termpty_kitty_kbd_push(&ty, 1);
+   termpty_kitty_kbd_push(&ty, 1);
+   assert(ty.kitty_kbd[0].depth == 3);
+   termpty_kitty_kbd_pop(&ty, 200);
+   assert(ty.kitty_kbd[0].depth == 0);
+
+   /* one stack per screen */
+   ty.altbuf = 1;
+   assert(termpty_kitty_kbd_flags_get(&ty) == 0);
+   termpty_kitty_kbd_push(&ty, 2);
+   ty.altbuf = 0;
+   assert(termpty_kitty_kbd_flags_get(&ty) == 0);
+   ty.altbuf = 1;
+   assert(termpty_kitty_kbd_flags_get(&ty) == 2);
+
+   /* RIS and DECSTR clear both screens */
+   termpty_soft_reset_state(&ty);
+   assert(ty.kitty_kbd[0].depth == 0);
+   assert(ty.kitty_kbd[1].depth == 0);
+
+   /* the index follows altbuf through a screen switch; the soft reset
+    * above did not leave the alt screen, so switch back first */
+   _ty_feed(&ty, "\033[?1049l");
+   _ty_feed(&ty, "\033[>1u");
+   _ty_feed(&ty, "\033[?1049h");
+   assert(ty.altbuf == 1);
+   assert(termpty_kitty_kbd_flags_get(&ty) == 0);
+   _ty_feed(&ty, "\033[?1049l");
+   assert(termpty_kitty_kbd_flags_get(&ty) == 1);
+
+   /* a resize does not touch the stacks, on either screen */
+   _ty_feed(&ty, "\033[?1049h");
+   _ty_feed(&ty, "\033[>1u");
+   assert(termpty_kitty_kbd_flags_get(&ty) == 1);
+   termpty_resize(&ty, 100, 30);
+   assert(termpty_kitty_kbd_flags_get(&ty) == 1);
+   _ty_feed(&ty, "\033[?1049l");
+   assert(termpty_kitty_kbd_flags_get(&ty) == 1);
+
+   _ty_test_shutdown(&ty);
+   return 0;
+}
+
 /* VS16-aware emoji width (retro-widen)
  *
  * emoji_dbl_width makes _termpty_is_wide() report double-width for a
diff --git a/src/bin/termpty.h b/src/bin/termpty.h
index fc917134..bc8b67f5 100644
--- a/src/bin/termpty.h
+++ b/src/bin/termpty.h
@@ -179,6 +179,20 @@ typedef struct tag_Term_Cursor {
     unsigned char wrapnext : 1;
 } Term_Cursor;
 
+/* kitty keyboard protocol, https://sw.kovidgoyal.net/kitty/keyboard-protocol/ */
+#define TERMPTY_KITTY_KBD_DISAMBIGUATE       (1 << 0)
+#define TERMPTY_KITTY_KBD_REPORT_EVENTS      (1 << 1)
+#define TERMPTY_KITTY_KBD_REPORT_ALTERNATES  (1 << 2)
+#define TERMPTY_KITTY_KBD_REPORT_ALL         (1 << 3)
+#define TERMPTY_KITTY_KBD_REPORT_TEXT        (1 << 4)
+#define TERMPTY_KITTY_KBD_STACK_SIZE 8
+
+typedef struct tag_Termpty_Kitty_Kbd
+{
+   uint8_t flags[TERMPTY_KITTY_KBD_STACK_SIZE];
+   uint8_t depth;
+} Termpty_Kitty_Kbd;
+
 struct tag_Termpty
 {
    Evas_Object *obj;
@@ -279,6 +293,7 @@ struct tag_Termpty
    Term_State termstate;
    Term_Cursor cursor_state;
    Term_Cursor cursor_save[2];
+   Termpty_Kitty_Kbd kitty_kbd[2]; /* indexed by altbuf */
    /* Column/row of the cell written by the last text codepoint appended by
     * termpty_text_append(), or -1 when there is none. Used to validate
     * VS16 (U+FE0F) retro-widen: a cursor move that lands anywhere else
diff --git a/src/bin/termptyesc.c b/src/bin/termptyesc.c
index 8f57c6c7..0e174b32 100644
--- a/src/bin/termptyesc.c
+++ b/src/bin/termptyesc.c
@@ -3747,6 +3747,46 @@ _seq_incomplete(Termpty *ty, const Eina_Unicode *c, const Eina_Unicode *cc)
    return 0;
 }
 
+/* https://sw.kovidgoyal.net/kitty/keyboard-protocol/#progressive-enhancement */
+static void
+_handle_esc_csi_kitty_kbd(Termpty *ty, Eina_Unicode **ptr)
+{
+   Eina_Unicode *b = *ptr;
+   Eina_Unicode cmd = *b;
+   int flags, arg;
+
+   switch (cmd)
+     {
+      case '?':
+         DBG("ignoring kitty keyboard query");
+         break;
+      case '>':
+         flags = _csi_arg_get(ty, &b);
+         if (flags == -ESC_ARG_ERROR)
+           return;
+         termpty_kitty_kbd_push(ty, (flags < 0 ? 0 : flags)
+                                & TERMPTY_KITTY_KBD_SUPPORTED);
+         break;
+      case '<':
+         arg = _csi_arg_get(ty, &b);
+         if (arg == -ESC_ARG_ERROR)
+           return;
+         termpty_kitty_kbd_pop(ty, arg < 0 ? 1 : arg);
+         break;
+      case '=':
+         flags = _csi_arg_get(ty, &b);
+         if (flags == -ESC_ARG_ERROR)
+           return;
+         arg = _csi_arg_get(ty, &b);
+         if (arg == -ESC_ARG_ERROR)
+           return;
+         termpty_kitty_kbd_set(ty, (flags < 0 ? 0 : flags)
+                               & TERMPTY_KITTY_KBD_SUPPORTED,
+                               arg < 0 ? 1 : arg);
+         break;
+     }
+}
+
 /* Returns the codepoints consumed after the CSI introducer, 0 when the CSI
  * is incomplete, or -1 when an ESC right after the introducer aborts it. */
 static int
@@ -4001,7 +4041,7 @@ _handle_esc_csi(Termpty *ty, const Eina_Unicode *c, const Eina_Unicode *ce)
           }
         else if ((*b == '?') || (*b == '>') || (*b == '<') || (*b == '='))
           {
-             DBG("ignoring kitty keyboard sequence");
+             _handle_esc_csi_kitty_kbd(ty, &b);
           }
         else
           {
diff --git a/src/bin/termptyops.c b/src/bin/termptyops.c
index 2a9fb5e1..587a9806 100644
--- a/src/bin/termptyops.c
+++ b/src/bin/termptyops.c
@@ -709,6 +709,74 @@ termpty_reset_att(Termatt *att)
    att->tab_last = 0;
 }
 
+unsigned int
+termpty_kitty_kbd_flags_get(const Termpty *ty)
+{
+   const Termpty_Kitty_Kbd *kk = &ty->kitty_kbd[ty->altbuf];
+
+   if (!kk->depth)
+     return 0;
+   return kk->flags[kk->depth - 1];
+}
+
+static Termpty_Kitty_Kbd *
+_kitty_kbd_top_ensure(Termpty *ty)
+{
+   Termpty_Kitty_Kbd *kk = &ty->kitty_kbd[ty->altbuf];
+
+   if (!kk->depth)
+     {
+        kk->flags[0] = 0;
+        kk->depth = 1;
+     }
+   return kk;
+}
+
+void
+termpty_kitty_kbd_set(Termpty *ty, unsigned int flags, int mode)
+{
+   Termpty_Kitty_Kbd *kk;
+   uint8_t *top;
+
+   if ((mode < 1) || (mode > 3))
+     return;
+   kk = _kitty_kbd_top_ensure(ty);
+   top = &kk->flags[kk->depth - 1];
+   if (mode == 1)
+     *top = flags;
+   else if (mode == 2)
+     *top |= flags;
+   else
+     *top &= ~flags;
+}
+
+void
+termpty_kitty_kbd_push(Termpty *ty, unsigned int flags)
+{
+   Termpty_Kitty_Kbd *kk = _kitty_kbd_top_ensure(ty);
+
+   if (kk->depth == TERMPTY_KITTY_KBD_STACK_SIZE)
+     {
+        memmove(kk->flags, kk->flags + 1, TERMPTY_KITTY_KBD_STACK_SIZE - 1);
+        kk->depth--;
+     }
+   kk->flags[kk->depth++] = flags;
+}
+
+void
+termpty_kitty_kbd_pop(Termpty *ty, unsigned int count)
+{
+   Termpty_Kitty_Kbd *kk = &ty->kitty_kbd[ty->altbuf];
+
+   kk->depth = (count >= kk->depth) ? 0 : kk->depth - count;
+}
+
+void
+termpty_kitty_kbd_reset(Termpty *ty)
+{
+   memset(ty->kitty_kbd, 0, sizeof(ty->kitty_kbd));
+}
+
 void
 termpty_soft_reset_state(Termpty *ty)
 {
@@ -758,6 +826,7 @@ termpty_soft_reset_state(Termpty *ty)
    ty->mouse_mode = MOUSE_OFF;
    ty->mouse_ext = MOUSE_EXT_NONE;
    ty->bracketed_paste = 0;
+   termpty_kitty_kbd_reset(ty);
    termpty_sync_output_reset(ty);
 
    termio_reset_main_colors(ty->obj);
diff --git a/src/bin/termptyops.h b/src/bin/termptyops.h
index 70555250..5f0df737 100644
--- a/src/bin/termptyops.h
+++ b/src/bin/termptyops.h
@@ -35,6 +35,15 @@ void termpty_clear_all(Termpty *ty);
 void termpty_reset_att(Termatt *att);
 void termpty_reset_state(Termpty *ty);
 void termpty_soft_reset_state(Termpty *ty);
+
+/* kitty keyboard protocol: the flags implemented so far */
+#define TERMPTY_KITTY_KBD_SUPPORTED (TERMPTY_KITTY_KBD_DISAMBIGUATE)
+
+unsigned int termpty_kitty_kbd_flags_get(const Termpty *ty);
+void termpty_kitty_kbd_set(Termpty *ty, unsigned int flags, int mode);
+void termpty_kitty_kbd_push(Termpty *ty, unsigned int flags);
+void termpty_kitty_kbd_pop(Termpty *ty, unsigned int count);
+void termpty_kitty_kbd_reset(Termpty *ty);
 void termpty_cursor_copy(Termpty *ty, Eina_Bool save);
 void termpty_clear_tabs_on_screen(Termpty *ty);
 void termpty_clear_backlog(Termpty *ty);
diff --git a/src/bin/tytest.c b/src/bin/tytest.c
index 704bddef..05d384e4 100644
--- a/src/bin/tytest.c
+++ b/src/bin/tytest.c
@@ -100,6 +100,7 @@ static struct {
        { "kam", tytest_kam},
        { "color_scheme_notify", tytest_color_scheme_notify},
        { "kitty_keyboard_ignored", tytest_kitty_keyboard_ignored},
+       { "kitty_kbd_stack", tytest_kitty_kbd_stack},
        { "keyin_legacy", tytest_keyin_legacy},
        { "keyin_modify_other_keys", tytest_keyin_modify_other_keys},
        { "keyin_modify_other_keys_level1", tytest_keyin_modify_other_keys_level1},
diff --git a/src/bin/unit_tests.h b/src/bin/unit_tests.h
index 3e4046d7..aa66ca96 100644
--- a/src/bin/unit_tests.h
+++ b/src/bin/unit_tests.h
@@ -73,6 +73,7 @@ int tytest_decrqm(void);
 int tytest_kam(void);
 int tytest_color_scheme_notify(void);
 int tytest_kitty_keyboard_ignored(void);
+int tytest_kitty_kbd_stack(void);
 int tytest_vs16_regression_no_scroll(void);
 int tytest_vs16_bare_narrow(void);
 int tytest_vs16_widens_one_call(void);

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

Reply via email to