SyncTERM 1.9: 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, detail read through ciolib_getkeyevent(). A full 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 -- that sequence is XTRESTORE here, and answering a query there would break every program using the older meaning. DIFFERENCES FROM THE MASTER VERSION OF THIS PATCH: * 1.9 has one monolithic cterm.c with a hand-written switch parser, not master's cterm_cterm.c dispatch table, so the two handlers are written into the private-extension "case 'u'" -- which '<' and '>' already reach. * The key-queue case follows 1.9's idiom (key = 0; break;) rather than master's continue. * CIO_KEY_KEYEVENT is 0x7bE0. It is free in 1.9; the value is kept the same as the master patch, where 0x7cE0 was later taken by upstream's own CIO_KEY_KEY_EVENT (physical/evdev notification, a different queue). Only the SDL backend is wired, which is what the Windows and X11 builds use. --- a/src/conio/ciolib.h +++ b/src/conio/ciolib.h @@ -835,4 +835,45 @@ * Set per BBS entry -- see "Exclusive Mouse" in the entry editor. */ CIOLIBEXPORT int ciolib_set_mouse_exclusive(int on); +#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); + #endif /* Do not add anything after this line */ --- a/src/conio/cterm.h +++ b/src/conio/cterm.h @@ -175,6 +175,11 @@ 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 int cursor; // Current cursor mode (Normal or None) char *fg_tc_str; --- a/src/conio/cterm.c +++ b/src/conio/cterm.c @@ -3904,7 +3904,41 @@ } break; case 'u': - if (seq->param_str[0] == '?' && parse_parameters(seq)) { + /* + * Kitty keyboard protocol: report key + * PRESSES and RELEASES, so a program can + * 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. + * + * CSI > flags u push flags, start + * CSI < n u pop n (default 1) + * + * Kitty's CSI ? u query is deliberately not + * implemented: that sequence is XTRESTORE + * here, and answering a query there would + * break every program using the older + * meaning. + */ + if (seq->param_str[0] == '>' && parse_parameters(seq)) { + seq_default(seq, 0, 0); + if (cterm->key_event_depth < 16) + cterm->key_event_depth++; + cterm->key_event_flags = (int)seq->param_int[0]; + ciolib_set_key_events(cterm->key_event_flags != 0); + } + else if (seq->param_str[0] == '<' && parse_parameters(seq)) { + seq_default(seq, 0, 1); + cterm->key_event_depth -= (int)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); + } + } + else if (seq->param_str[0] == '?' && parse_parameters(seq)) { gettextinfo(&ti); flags = getvideoflags(); attr2palette(cterm->attr, &oldfg, &oldbg); --- a/src/conio/sdl_con.c +++ b/src/conio/sdl_con.c @@ -57,6 +57,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 */ @@ -768,6 +779,115 @@ 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; @@ -1025,6 +1145,13 @@ continue; switch (ev.type) { case SDL_KEYDOWN: /* Keypress */ + /* 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) @@ -1123,6 +1250,8 @@ } break; case SDL_KEYUP: + 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) { --- a/src/syncterm/term.c +++ b/src/syncterm/term.c @@ -5516,6 +5516,37 @@ } } +/* + * 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) { @@ -6068,6 +6099,10 @@ handle_mouse_event(&ms); key = 0; break; + case CIO_KEY_KEYEVENT: + handle_key_event(); + key = 0; + break; case CIO_KEY_SHIFT_IC: /* Shift-Insert - Paste */ do_paste(); key = 0;