diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-21 21:19:36 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-21 22:26:19 -0300 |
| commit | 752cd81c1c5e1e91e1429cefc1c4651647981f88 (patch) | |
| tree | 5c6ff207797f13deedbdc2d42b5ad1b2dd45a1f5 /dev | |
| parent | 27874074025f88dd9e27ece82c352c4a7c35ab51 (diff) | |
| download | pardes-752cd81c1c5e1e91e1429cefc1c4651647981f88.tar.gz pardes-752cd81c1c5e1e91e1429cefc1c4651647981f88.zip | |
pair: a pair's session puts the user's terminal back when its client dies with no word (P48): a tty pair (runtime/main.zig `pair`) keeps /dev/tty and its termios as they are before the client enters it and hands them to the session (dev/tui/tui.zig `Term.pair`, `Kept`); `Peers` marks the pair's first peer (`Peer.pair`, from `Joining.own`) and a bye (`Peer.bye`) and says at that peer's leave whether it left with no word at all (`Peers.pair_left`: no bye, not let go by the session, not cut); the session's frontend (`Own.sent`, now before a no-daemon pair's end in runtime/session.zig) writes on a loss what the own path writes when a terminal is lost under it (`Killed`'s bytes, which now also turn the in-band resize off: vaxis's terminal query turns it on) and the cooked termios, and lets the device go either way (a terminal held open keeps an ssh or its emulator waiting); not across an Upgrade (close-on-exec) nor after the pair's client joined again; daemon.py plain with the variable passes on Debug (it failed at 'the terminal is put back'), its row after a killed stub listing `(tty)` under the variable (a loss: still a pair's, P36), and --upgrade counts the device let go at the exec; core-test 896/1056, shell-test 1098/1101
Co-Authored-By: Claude Opus <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'dev')
| -rw-r--r-- | dev/tui/tui.zig | 41 |
1 files changed, 32 insertions, 9 deletions
diff --git a/dev/tui/tui.zig b/dev/tui/tui.zig index 92ac6a0c..25ee9075 100644 --- a/dev/tui/tui.zig +++ b/dev/tui/tui.zig @@ -559,8 +559,12 @@ fn surfaceHasKittyKey(surface: *const pardes.Surface, key: u64) bool { /// opens it; one open already (a session forked off its stub, /// daemon.forkSession); or none, detached from the start (`--detach`). /// The terminal a session starts on: /dev/tty, one its stub opened (an -/// upgraded session's is raw already, `termios` the cooked one), or none. -pub const Term = union(enum) { open, fd: struct { fd: posix.fd_t, termios: ?posix.termios = null }, detached }; +/// upgraded session's is raw already, `termios` the cooked one), or none: +/// a pair's (main.zig `pair`) draws on none, and keeps its client's, with +/// its modes before the client took it, to put back if that client dies +/// with no word (P48: `Own.sent`). +pub const Term = union(enum) { open, fd: struct { fd: posix.fd_t, termios: ?posix.termios = null }, detached, pair: Kept }; +pub const Kept = struct { fd: posix.fd_t, termios: posix.termios }; pub fn run(init: std.process.Init, opts: pardes.Options, term: Term) !void { const io = init.io; @@ -583,7 +587,7 @@ pub fn run(init: std.process.Init, opts: pardes.Options, term: Term) !void { switch (term) { .open => try screen.enter(try vaxis.Tty.init(io, &tty_buf)), .fd => |t| try screen.enter(try Screen.ttyOn(io, &tty_buf, t.fd, t.termios)), - .detached => {}, + .detached, .pair => {}, } defer screen.leave(); @@ -591,7 +595,7 @@ pub fn run(init: std.process.Init, opts: pardes.Options, term: Term) !void { defer paste_buf.deinit(); var loop: Loop = .init(io, &screen.tty, &screen.vx); - var own: Own = .{ .screen = &screen, .loop = &loop, .paste_buf = &paste_buf }; + var own: Own = .{ .screen = &screen, .loop = &loop, .paste_buf = &paste_buf, .kept = if (term == .pair) term.pair else null }; try session_loop.Loop(Own).run(init, opts, &own); } @@ -929,7 +933,9 @@ const Screen = struct { const Killed = struct { var fd: posix.fd_t = -1; var cooked: posix.termios = undefined; - const reset = "\x1b[<u\x1b[?1000l\x1b[?1002l\x1b[?1003l\x1b[?1006l\x1b[?1004l\x1b[?2004l\x1b[0m\x1b[?25h\x1b[?1049l"; + /// Every mode it may have turned on off (the in-band resize its + /// terminal query asks for, vaxis's `in_band_resize_set`, too). + const reset = "\x1b[<u\x1b[?1000l\x1b[?1002l\x1b[?1003l\x1b[?1006l\x1b[?1004l\x1b[?2004l\x1b[?2048l\x1b[0m\x1b[?25h\x1b[?1049l"; fn arm(tty_fd: posix.fd_t, termios: posix.termios) void { cooked = termios; fd = tty_fd; @@ -939,6 +945,11 @@ const Killed = struct { if (daemon.link < 0) posix.sigaction(.HUP, &on, null); } + fn putBack(tty: posix.fd_t, termios: *const posix.termios) void { + _ = std.c.write(tty, reset, reset.len); + _ = std.c.tcsetattr(tty, .FLUSH, termios); + } + /// The terminal gone (put back, or detached): a TERM still takes the /// socket, the prompt files and the shells with it, and leaves the /// terminal alone. @@ -947,10 +958,7 @@ const Killed = struct { } fn handle(sig: posix.SIG) callconv(.c) void { - if (fd >= 0) { - _ = std.c.write(fd, reset, reset.len); - _ = std.c.tcsetattr(fd, .FLUSH, &cooked); - } + if (fd >= 0) putBack(fd, &cooked); // The 9P socket and private prompt files go too: a killed editor // leaves none behind. Its shells too, the daemon's dups let go. ninep_io.unlinkOwnSocket(); @@ -971,6 +979,9 @@ const Own = struct { paste_buf: *std.Io.Writer.Allocating, frame_arena: std.heap.ArenaAllocator = undefined, sh: Shell = undefined, + /// A pair's: its client's terminal (Term.pair), until that client + /// leaves. + kept: ?Kept = null, const Session = session_loop.Loop(Own); @@ -1027,6 +1038,7 @@ const Own = struct { o.screen.art.deinit(gpa); o.screen.glide.deinit(gpa); o.frame_arena.deinit(); + if (o.kept) |k| _ = libc.close(k.fd); } pub fn start(o: *Own, l: *Session) !void { @@ -1051,6 +1063,17 @@ const Own = struct { pub fn sent(o: *Own) void { const sh = &o.sh; if (sh.peers.recording and sh.detached()) sh.drawn_ns = @intCast(std.Io.Clock.awake.now(sh.io).nanoseconds); + // A pair's client gone: dead with no word (a kill -9), its + // terminal put back as the own path's survivor puts it back (a + // forked session's when its stub is killed), from the alternate + // screen and raw; with a word, it did that itself. Let go either + // way: a terminal held open keeps an ssh, or its emulator, waiting. + const lost = sh.peers.pair_left orelse return; + sh.peers.pair_left = null; + const k = o.kept orelse return; + o.kept = null; + if (lost) Killed.putBack(k.fd, &k.termios); + _ = libc.close(k.fd); } pub fn upgradeAsked(o: *Own) void { |
