diff options
| -rw-r--r-- | dev/gui/gui.zig | 7 | ||||
| -rw-r--r-- | dev/tui/tui.zig | 39 | ||||
| -rw-r--r-- | test/process/window.zig | 20 |
3 files changed, 62 insertions, 4 deletions
diff --git a/dev/gui/gui.zig b/dev/gui/gui.zig index 6cd97b69..9f6e4876 100644 --- a/dev/gui/gui.zig +++ b/dev/gui/gui.zig @@ -1555,10 +1555,13 @@ const StdinFeed = struct { // finger;<kind>;<id>;<x>;<y>;<pressure> // close: the window's close button // One the read cut short, or what may yet begin one, waits for - // the next read. + // the next read; but a read that ends in a lone ESC ends in the + // Escape key, as the tty's reader has a read of one ESC (a driver + // writes each key and event whole): held, it waited for the next + // write, which then came as Alt on its first key. const rest = f.buf[seq_start..len]; const osc = "\x1b]777;"; - if (std.mem.startsWith(u8, rest, osc) or std.mem.startsWith(u8, osc, rest)) { + if (std.mem.startsWith(u8, rest, osc) or (rest.len > 1 and std.mem.startsWith(u8, osc, rest))) { const bel = std.mem.indexOfScalar(u8, rest, 0x07) orelse { std.mem.copyForwards(u8, f.buf[0..rest.len], rest); f.fill = rest.len; diff --git a/dev/tui/tui.zig b/dev/tui/tui.zig index 542ef59a..aa1f8849 100644 --- a/dev/tui/tui.zig +++ b/dev/tui/tui.zig @@ -179,6 +179,9 @@ fn readInput(loop: *Loop, tty: anytype, vx: *vaxis.Vaxis, feed: *Feed) !void { var carried: usize = 0; // The sequence carried is past max_sequence, and said so. var dropping = false; + // The ESC the last read ended in, carried because more came while it + // waited for a sequence's rest. + var esc_held = false; while (true) { // stopInput cancels the task; the read then fails with Canceled if (carried == buf.len and buf.len < max_sequence) { if (gpa.realloc(buf, @min(2 * buf.len, max_sequence))) |grown| buf = grown else |_| {} @@ -227,6 +230,12 @@ fn readInput(loop: *Loop, tty: anytype, vx: *vaxis.Vaxis, feed: *Feed) !void { if (scalar_len > end - (lead - 1)) parse_end = lead - 1; } var consumed: usize = 0; + // That ESC is the Escape key unless this read goes on with a + // sequence's rest (an ssh packet cut it after its ESC): its + // introducer, CSI's `[`, SS3's `O`, a string's, or another ESC. A + // key typed just after Escape is its own, not Alt on it. + const lone_esc = esc_held and end > 1 and buf[0] == 0x1b and std.mem.indexOfScalar(u8, "[O]P_^\\\x1b", buf[1]) == null; + esc_held = false; // A read that is one ESC and nothing else is the Escape key: a // terminal writes each key's sequence whole and ssh carries each of // its writes whole, so no sequence arrives cut right after its ESC. @@ -247,9 +256,12 @@ fn readInput(loop: *Loop, tty: anytype, vx: *vaxis.Vaxis, feed: *Feed) !void { std.mem.startsWith(u8, FrameAck.answer, rest)) break; if (!waited and parse_end == end and rest[0] == 0x1b and (rest.len == 1 or (rest.len == 2 and rest[1] == 0x1b))) { waited = true; - if (try inputFollows(tty, loop.io, if (vx.caps.kitty_keyboard) 1000 else 50)) break; + if (try inputFollows(tty, loop.io, if (vx.caps.kitty_keyboard) 1000 else 50)) { + esc_held = rest.len == 1; + break; + } } - const result = try parser.parse(buf[consumed..parse_end], vx.opts.system_clipboard_allocator); + const result = try parser.parse(buf[consumed..if (lone_esc and consumed == 0) 1 else parse_end], vx.opts.system_clipboard_allocator); if (result.n == 0) break; consumed += result.n; // A modifier pressed on its own, which the kitty keyboard @@ -2668,6 +2680,29 @@ test "an ESC read by itself is the Escape key at once" { t.vx.caps.kitty_keyboard = false; } +test "an ESC that ends a read, a key read after it within the wait, is the Escape key and then that key" { + var t: TestInput = undefined; + try t.init(); + defer t.deinit(); + // Typed faster than read (a burst over a link): `x` and Escape in one + // read, `A` in the next while the ESC waits for a sequence's rest. Joined + // to it, it was Alt-A and the Escape was lost (insert mode kept, `A` + // typed nothing). A sequence cut after its ESC still joins (the test + // above: `[`, `O` and the strings' introducers, another ESC). + var reader: TestReader = .{ .parts = &.{ "x\x1b", "A" } }; + try readInput(&t.loop, &reader, &t.vx, &t.feed); + try std.testing.expect(reader.asked); + try std.testing.expectEqual(.winsize, std.meta.activeTag((try t.loop.tryEvent()).?)); + try std.testing.expectEqual(@as(u21, 'x'), (try t.loop.tryEvent()).?.key_press.codepoint); + const escape = (try t.loop.tryEvent()).?.key_press; + try std.testing.expectEqual(vaxis.Key.escape, escape.codepoint); + try std.testing.expect(!escape.mods.alt); + const a = (try t.loop.tryEvent()).?.key_press; + try std.testing.expectEqual(@as(u21, 'A'), a.codepoint); + try std.testing.expect(!a.mods.alt); + try std.testing.expect(try t.loop.tryEvent() == null); +} + test "an Escape read with the click after it is the Escape key, then the click" { var t: TestInput = undefined; try t.init(); diff --git a/test/process/window.zig b/test/process/window.zig index 2a1331ad..292e0e00 100644 --- a/test/process/window.zig +++ b/test/process/window.zig @@ -8,6 +8,7 @@ const process = @import("../process.zig"); pub const cases = .{ .{ "window: a monkey over the effects: random clicks, drags, wheels, keys, columns, looks and themes, then it settles, alive, with no crash record", monkey }, .{ "window: its frame corpus, every kept frame and its sha256 into PARDES_GUI_CORPUS, to compare two builds on one machine", corpus }, + .{ "window: a lone Esc written to its test stdin is the Escape key at once, the key written after it its own", loneEscape }, }; fn envOr(name: [*:0]const u8, default: []const u8) []const u8 { @@ -193,6 +194,25 @@ fn bodyHas(f: *process.Files, serial: u32, word: []const u8) !void { if (process.nowMs() > deadline) return error.NoOutput; } +/// A lone Esc a driver writes to the window's stdin, as its keys come: +/// the Escape key at once. Held for the next write (DESIGN D15, POLISH 16), +/// that write's `A` came as Alt-A and its `y` was typed where insert mode +/// still was. +fn loneEscape(gpa: std.mem.Allocator, exe: [:0]const u8) !void { + try process.only(.gui); + var d = try process.Detached.start(gpa, exe, "loneesc", .{}); + defer d.deinit(); + const f = &d.files; + const pane = try paneWith(f, "loneesc.txt"); + try d.typeKeys("ix"); + try d.settle(gpa); + try d.typeKeys("\x1b"); + try d.settle(gpa); + try d.typeKeys("Ay"); + try d.settle(gpa); + try std.testing.expectEqualStrings("xinitialy\n", try f.read(f.at("/pane/{d}/body", .{pane}))); +} + extern "c" fn system(cmd: [*:0]const u8) c_int; extern "c" fn ioctl(fd: c_int, request: c_ulong, ...) c_int; |
