From eac8dd3bf8ce8ef5458612ed290e64032276ac66 Mon Sep 17 00:00:00 2001 From: ANSI TV Admin Date: Wed, 11 Nov 2026 12:05:22 -0800 Subject: [PATCH 03/24] SyncTERM: Kitty keyboard protocol -- report key PRESS and RELEASE A terminal reports characters, and a character cannot say "still held" or "let go", so a game driven over the wire cannot see two keys held at once (walking forward while strafing). This adds the Kitty keyboard protocol: CSI > flags u enable / push CSI < n u pop n levels (default 1); at zero, off CSI key ; mods : event u report; event 1 press, 2 repeat, 3 release Events are sent IN ADDITION to the normal key bytes, so Alt-X and every existing application keep working, and a terminal without the patch simply ignores the enable. The mechanism mirrors the mouse: a CIO_KEY_KEYEVENT sentinel in the key queue, with the detail read separately through ciolib_getkeyevent(). A full event ring drops the OLDEST event, never the newest: a dropped release leaves the far end believing a key is still held. Kitty's "CSI ? u" query is deliberately NOT implemented -- CTerm already uses that sequence for XTRESTORE, and answering a query there would break every program using the older meaning. TWO THINGS FOR A REVIEWER: * CIO_KEY_KEYEVENT is 0x7bE0, NOT 0x7cE0. Upstream master took 0x7cE0 for CIO_KEY_KEY_EVENT (its own physical/evdev key notification) after this patch was first written. They are two different queues read by two different calls and must not share a sentinel. * Master already reports physical keys by evdev code (CSI = 1 h, ciokey_gotevent/ciokey_getevent). This patch is the Kitty spelling of a similar idea and does not replace it; both are wired in the SDL backend, and a program gets only what it asks for. Only the SDL backend is wired (sdl_con.c), which is what the Windows and X11 builds use. Files: conio/{ciolib.h,cterm.h,cterm_cterm.h,cterm.c,cterm_cterm.c,sdl_con.c}, syncterm/term.c --- src/conio/ciolib.h | 40 +++++++++++++ src/conio/cterm.c | 4 ++ src/conio/cterm.h | 5 ++ src/conio/cterm_cterm.c | 51 ++++++++++++++++ src/conio/cterm_cterm.h | 5 ++ src/conio/sdl_con.c | 129 ++++++++++++++++++++++++++++++++++++++++ src/syncterm/term.c | 34 +++++++++++ 7 files changed, 268 insertions(+) diff --git a/src/conio/ciolib.h b/src/conio/ciolib.h index afdb57ae..8dbf567d 100644 --- a/src/conio/ciolib.h +++ b/src/conio/ciolib.h @@ -872,6 +872,46 @@ CIOLIBEXPORT int ciokey_wait(void); #define CIO_KEY_ABORTED 0x01E0 // ESC key by scancode #define CIO_KEY_LITERAL_E0 0xE0E0 // Literal 0xe0 character #define CIO_KEY_WREN_CONSOLE 0x29E0 // Ctrl-` opens the Wren scripting console (high byte = `'s scancode 0x29) +#define CIO_KEY_KEYEVENT 0x7bE0 // A key was pressed or released; read it with ciolib_getkeyevent() + // NOT 0x7cE0: that is upstream's CIO_KEY_KEY_EVENT (physical/evdev + // key notification), a different queue read a different way. + +/* + * Key events -- presses AND releases, for programs that need to know a key + * is being HELD. + * + * A terminal normally reports characters, not keys: hold W and you get a + * repeat stream, and nothing at all says when it stopped. That is fine for + * a BBS and useless for a game, where walking forward and strafing at once + * means two keys down together. + * + * This is the same arrangement the mouse already uses: the backend puts + * CIO_KEY_KEYEVENT in the key queue as a marker, and the detail is read + * separately with ciolib_getkeyevent(). Nothing is reported unless the + * program asks for it with ciolib_set_key_events(), so ordinary use is + * exactly as it was. + */ +#define CIO_KEYEVENT_PRESS 1 +#define CIO_KEYEVENT_REPEAT 2 +#define CIO_KEYEVENT_RELEASE 3 + +/* Modifier bits, matching the Kitty keyboard protocol's numbering. */ +#define CIO_KEYEVENT_SHIFT 0x01 +#define CIO_KEYEVENT_ALT 0x02 +#define CIO_KEYEVENT_CTRL 0x04 +#define CIO_KEYEVENT_SUPER 0x08 + +struct cio_key_event { + uint32_t key; /* Unicode codepoint, or a CIO_KEY_* functional code */ + uint8_t event; /* CIO_KEYEVENT_PRESS / _REPEAT / _RELEASE */ + uint8_t mods; /* CIO_KEYEVENT_SHIFT | _ALT | _CTRL | _SUPER */ +}; + +/* Ask the backend to report key events (non-zero) or stop (zero). + * Answers non-zero if this backend can do it at all. */ +CIOLIBEXPORT int ciolib_set_key_events(int on); +/* Take the next key event. Answers 0 when there is none waiting. */ +CIOLIBEXPORT int ciolib_getkeyevent(struct cio_key_event *ev); /* Exclusive mouse: capture the pointer to the window and report motion * rather than position. Answers non-zero if the backend could do it. diff --git a/src/conio/cterm.c b/src/conio/cterm.c index ca009849..678ea0b9 100644 --- a/src/conio/cterm.c +++ b/src/conio/cterm.c @@ -1805,6 +1805,10 @@ const struct seq_dispatch cterm_ansi_dispatch[] = { { cterm_handle_decrara, (SEQ_CSI | SEQ_INTERM('$') | SEQ_FINAL('t')), 0 }, /* CSI $ t */ { cterm_handle_scorc, SEQ_SCORC, 0 }, /* CSI u */ + { cterm_handle_kbd_pop, + (SEQ_CSI | SEQ_PRIV('<') | SEQ_FINAL('u')), 0 }, /* CSI < u */ + { cterm_handle_kbd_push, + (SEQ_CSI | SEQ_PRIV('>') | SEQ_FINAL('u')), 0 }, /* CSI > u */ { cterm_handle_restore_dec_modes, (SEQ_CSI | SEQ_PRIV('?') | SEQ_FINAL('u')), 0 }, /* CSI ? u */ { cterm_handle_deccra, SEQ_DECCRA, 0 }, /* CSI $ v */ diff --git a/src/conio/cterm.h b/src/conio/cterm.h index 7fa8c563..880a1fc9 100644 --- a/src/conio/cterm.h +++ b/src/conio/cterm.h @@ -206,6 +206,11 @@ struct cterminal { int font_slot; int font_size; // Bytes int doorway_mode; + /* Kitty keyboard protocol: how deep the stack is (0 = off) and + * the flags in force. Only a program that asked gets key events; + * see CIO_KEY_KEYEVENT in ciolib.h. */ + int key_event_flags; + int key_event_depth; int doorway_char; // Indicates next char is a "doorway" mode char bool pk_mode; bool suppress_translated_keys; diff --git a/src/conio/cterm_cterm.c b/src/conio/cterm_cterm.c index 55481499..b1879050 100644 --- a/src/conio/cterm_cterm.c +++ b/src/conio/cterm_cterm.c @@ -1590,3 +1590,54 @@ cterm_cterm_apply_sgr(struct cterminal *cterm, int *pi) } cterm_dec_apply_sgr(cterm, pi); } + +/* + * The Kitty keyboard protocol, enough of it to hold two keys at once. + * + * A terminal reports characters, and a character cannot say "still held" or + * "let go". Walking forward while strafing is two keys down together, and + * down is the part that has never been expressible. These sequences ask the + * terminal to report key events instead of guessing: + * + * CSI > flags u push flags and start reporting + * CSI < n u pop n levels (default 1); at zero, reporting stops + * + * Kitty also specifies CSI ? u to query the flags. That one is NOT + * implemented, and cannot be: CTerm already uses CSI ? u for XTRESTORE + * (restore DEC private modes), and a terminal that answered a query there + * instead of restoring modes would break every program using the older + * meaning. A host that wants to know whether this works should ask the + * terminal what it is, the way it already asks about everything else. + * + * Bit 1 (0x02) is the one that matters here -- "report event types", which + * is what turns a keypress into a press and a release. The flags are kept + * so a query answers honestly, but a program that sets anything at all gets + * events; there is nothing to be gained by refusing. + * + * Reports are written by the terminal side (term.c), which is where the + * connection is. This only decides whether they happen. + */ +void +cterm_handle_kbd_push(struct cterminal *cterm, int *speed) /* CSI > u */ +{ + (void)speed; + cterm_seq_default(cterm, 0, 0); + if (cterm->key_event_depth < 16) + cterm->key_event_depth++; + cterm->key_event_flags = cterm->seq_param_int[0]; + ciolib_set_key_events(cterm->key_event_flags != 0); +} + +void +cterm_handle_kbd_pop(struct cterminal *cterm, int *speed) /* CSI < u */ +{ + (void)speed; + cterm_seq_default(cterm, 0, 1); + cterm->key_event_depth -= cterm->seq_param_int[0]; + if (cterm->key_event_depth < 0) + cterm->key_event_depth = 0; + if (cterm->key_event_depth == 0) { + cterm->key_event_flags = 0; + ciolib_set_key_events(0); + } +} diff --git a/src/conio/cterm_cterm.h b/src/conio/cterm_cterm.h index f3d72d66..f0bdc216 100644 --- a/src/conio/cterm_cterm.h +++ b/src/conio/cterm_cterm.h @@ -67,4 +67,9 @@ void cterm_parse_extended_colour(struct cterminal *cterm, int *i, bool fg); * cterm_dec_apply_sgr for anything else. */ void cterm_cterm_apply_sgr(struct cterminal *cterm, int *pi); +/* Kitty keyboard protocol -- key events, so a program can know a key is + * being HELD. CSI > flags u pushes, CSI < n u pops. */ +void cterm_handle_kbd_push(struct cterminal *cterm, int *speed); +void cterm_handle_kbd_pop(struct cterminal *cterm, int *speed); + #endif /* _CTERM_CTERM_H_ */ diff --git a/src/conio/sdl_con.c b/src/conio/sdl_con.c index 95ad6d20..9230970a 100644 --- a/src/conio/sdl_con.c +++ b/src/conio/sdl_con.c @@ -58,6 +58,17 @@ static int sdl_mouse_exclusive = 0; static int sdl_rel_x = 0, sdl_rel_y = 0; static int sdl_rel_centred = 0; +/* + * Key events -- see ciolib.h. A ring the size of the key buffer, holding + * presses and releases for a program that asked for them. Guarded by + * sdl_keylock, the same lock the key buffer uses, because the two are + * always filled together: the event goes in here and its marker goes in + * there, and a reader must never see one without the other. + */ +static struct cio_key_event sdl_keyevents[256]; +static unsigned char sdl_keyevent_first = 0; +static unsigned char sdl_keyevent_next = 0; +static int sdl_keyevents_wanted = 0; unsigned char sdl_key=0; /* Index into keybuf for next key in buffer */ unsigned char sdl_keynext=0; /* Index into keybuf for next free position */ @@ -850,6 +861,115 @@ ciolib_set_mouse_exclusive(int on) return 1; } +static void sdl_add_key(unsigned int keyval, struct video_stats *vs); + +/* + * An SDL key, as the Kitty keyboard protocol numbers it. + * + * Printable keys are their own codepoint; the rest come from Kitty's + * functional-key table, which puts them in the private use area. The + * modifier keys are in there too, and a game needs them: Ctrl fires and + * Shift runs, so "Ctrl went down" is a real event and not just a state + * change on somebody else's keypress. Answers 0 for a key with no + * agreed number, which is then not reported at all. + */ +static uint32_t +sdl_keyevent_code(SDL_Keycode sym) +{ + switch (sym) { + case SDLK_ESCAPE: return 27; + case SDLK_RETURN: case SDLK_KP_ENTER: return 13; + case SDLK_TAB: return 9; + case SDLK_BACKSPACE: return 127; + case SDLK_INSERT: return 57348; + case SDLK_DELETE: return 57349; + case SDLK_LEFT: return 57350; + case SDLK_RIGHT: return 57351; + case SDLK_UP: return 57352; + case SDLK_DOWN: return 57353; + case SDLK_PAGEUP: return 57354; + case SDLK_PAGEDOWN: return 57355; + case SDLK_HOME: return 57356; + case SDLK_END: return 57357; + case SDLK_LSHIFT: return 57441; + case SDLK_LCTRL: return 57442; + case SDLK_LALT: return 57443; + case SDLK_LGUI: return 57444; + case SDLK_RSHIFT: return 57447; + case SDLK_RCTRL: return 57448; + case SDLK_RALT: return 57449; + case SDLK_RGUI: return 57450; + default: + if (sym >= SDLK_F1 && sym <= SDLK_F12) + return 57364 + (sym - SDLK_F1); + /* Printable ASCII is its own codepoint, unshifted and lower + * case -- which is what the protocol asks for, and happens to + * be exactly what SDL hands over. */ + if (sym >= 32 && sym < 127) + return (uint32_t)sym; + return 0; + } +} + +/* Put one key event in the ring and its marker in the key queue. + * Called with sdl_keylock NOT held. */ +static void +sdl_add_key_event(SDL_Keycode sym, SDL_Keymod mod, int repeat, int pressed) +{ + struct cio_key_event ev; + uint32_t code; + + if (!sdl_keyevents_wanted) + return; + code = sdl_keyevent_code(sym); + if (code == 0) + return; + ev.key = code; + ev.event = pressed ? (repeat ? CIO_KEYEVENT_REPEAT : CIO_KEYEVENT_PRESS) + : CIO_KEYEVENT_RELEASE; + ev.mods = ((mod & KMOD_SHIFT) ? CIO_KEYEVENT_SHIFT : 0) + | ((mod & KMOD_ALT) ? CIO_KEYEVENT_ALT : 0) + | ((mod & KMOD_CTRL) ? CIO_KEYEVENT_CTRL : 0) + | ((mod & KMOD_GUI) ? CIO_KEYEVENT_SUPER : 0); + + assert_pthread_mutex_lock(&sdl_keylock); + if ((unsigned char)(sdl_keyevent_next + 1) == sdl_keyevent_first) { + /* Full. Drop the OLDEST, not the newest: a release that is + * thrown away leaves the far end believing a key is still + * held, and it walks into a wall until something else + * happens. The stalest event is the safest to lose. */ + sdl_keyevent_first++; + } + sdl_keyevents[sdl_keyevent_next++] = ev; + assert_pthread_mutex_unlock(&sdl_keylock); + sdl_add_key(CIO_KEY_KEYEVENT, NULL); +} + +CIOLIBEXPORT int +ciolib_set_key_events(int on) +{ + assert_pthread_mutex_lock(&sdl_keylock); + sdl_keyevents_wanted = on ? 1 : 0; + if (!on) /* nothing half-reported is left behind */ + sdl_keyevent_first = sdl_keyevent_next; + assert_pthread_mutex_unlock(&sdl_keylock); + return 1; +} + +CIOLIBEXPORT int +ciolib_getkeyevent(struct cio_key_event *ev) +{ + int got = 0; + + assert_pthread_mutex_lock(&sdl_keylock); + if (sdl_keyevent_first != sdl_keyevent_next) { + *ev = sdl_keyevents[sdl_keyevent_first++]; + got = 1; + } + assert_pthread_mutex_unlock(&sdl_keylock); + return got; +} + static void sdl_add_key(unsigned int keyval, struct video_stats *vs) { int w, h; @@ -1120,6 +1240,13 @@ void sdl_video_event_thread(void *data) evdev = sdl_evdev_key(&ev.key.keysym); if (evdev != 0) ciokey_gotevent(evdev, true); + /* Reported alongside the normal key, never + * instead of it: Alt-X and the rest are handled + * locally further up, and a game asking to see + * key events must not cost the user the ability + * to quit the program. */ + sdl_add_key_event(ev.key.keysym.sym, + ev.key.keysym.mod, ev.key.repeat, 1); if (bios_key_parsing) { if (ev.key.keysym.sym >= SDLK_KP_1 && ev.key.keysym.sym <= SDLK_KP_0) { if (bios_key == 0 && ev.key.keysym.sym == SDLK_KP_0) @@ -1221,6 +1348,8 @@ void sdl_video_event_thread(void *data) evdev = sdl_evdev_key(&ev.key.keysym); if (evdev != 0) ciokey_gotevent(evdev, false); + sdl_add_key_event(ev.key.keysym.sym, + ev.key.keysym.mod, 0, 0); if (bios_key_parsing) { // If Mod1 (ie: ALT) is released, *and* the only bytes were KP numbers, do the BIOS thing. if (ev.key.keysym.sym == SDLK_LALT || ev.key.keysym.sym == SDLK_RALT) { diff --git a/src/syncterm/term.c b/src/syncterm/term.c index 7118cddb..8e838545 100644 --- a/src/syncterm/term.c +++ b/src/syncterm/term.c @@ -4724,6 +4724,37 @@ drain_drag_events(void) } } +/* + * One key event, reported to the host in the Kitty keyboard protocol's form: + * + * CSI unicode ; modifiers : event u + * + * where event is 1 press, 2 repeat, 3 release, and modifiers is the usual + * one-plus-bitmask. The host asked for these with CSI > flags u; if it did + * not, the event is dropped here rather than at the point it was captured, + * so the flag can be turned off mid-stream without leaving a queue behind. + * + * This is what lets a game know a key is HELD. A character cannot say that, + * which is why holding two direction keys at once has never worked over a + * terminal. + */ +static void +handle_key_event(void) +{ + struct cio_key_event ev; + char buf[48]; + int len; + + while (ciolib_getkeyevent(&ev)) { + if (cterm == NULL || cterm->key_event_flags == 0) + continue; + len = sprintf(buf, "\x1b[%lu;%u:%uu", + (unsigned long)ev.key, (unsigned)ev.mods + 1, + (unsigned)ev.event); + conn_send(buf, len, 0); + } +} + static void handle_mouse_event(struct mouse_state *ms) { @@ -5463,6 +5494,9 @@ doterm(struct bbslist *bbs) case CIO_KEY_MOUSE: handle_mouse_event(&ms); continue; + case CIO_KEY_KEYEVENT: + handle_key_event(); + continue; /* Alt-Z (always) and Ctrl-S (text-mode only) are * handled by online_menu.wren — registers * Hook.onKey hooks that drive the OnlineMenu -- 2.47.3