diff options
Diffstat (limited to 'src/tui')
| -rw-r--r-- | src/tui/tui.zig | 382 |
1 files changed, 94 insertions, 288 deletions
diff --git a/src/tui/tui.zig b/src/tui/tui.zig index 860e96c8..0c74cbaa 100644 --- a/src/tui/tui.zig +++ b/src/tui/tui.zig @@ -23,11 +23,9 @@ extern "c" fn setenv(name: [*:0]const u8, value: [*:0]const u8, overwrite: c_int const TIOCSWINSZ: c_int = @bitCast(@as(u32, if (@hasDecl(posix.T, "IOCSWINSZ")) posix.T.IOCSWINSZ else 0x80087467)); -/// What a terminal's input reader keeps, one a terminal (views.zig): its -/// graphemes, the frame it waits on the answer to, the shm probe, and the -/// view its input is. +/// What the terminal's input reader keeps: its graphemes, the frame it +/// waits on the answer to, and the shm probe. const Feed = struct { - view: u8 = 0, cache: vaxis.GraphemeCache = .{}, ack: FrameAck = .{}, /// The terminal answered the shared-memory probe (`kittyShmProbe`): it @@ -43,25 +41,6 @@ const Feed = struct { shm_probing: std.atomic.Value(bool) = .init(false), }; -/// A terminal's input onto the loop's queue, as its view's: a `from` goes -/// ahead of it whenever another terminal posted last. With one terminal -/// none ever does. -const Poster = struct { - loop: *Loop, - view: u8, - var lock: std.Io.Mutex = .init; - var last: u8 = 0; - - pub fn postEvent(p: Poster, ev: Msg) !void { - lock.lockUncancelable(p.loop.io); - defer lock.unlock(p.loop.io); - if (last != p.view) { - try p.loop.postEvent(.{ .from = p.view }); - last = p.view; - } - try p.loop.postEvent(ev); - } -}; const kitty_shm_probe_id = 0x7fff_fff1; var kitty_shm_serial: u32 = 0; /// Transmissions an unread shm segment outlives before it is removed. @@ -129,8 +108,6 @@ const Msg = union(enum) { adopted: struct { pid: posix.pid_t, fd: posix.fd_t }, /// A timed wait ran out: the core's next wake has come. wake, - /// The input after this is the terminal of this view's (Poster). - from: u8, /// A SIGWINCH: a terminal changed size (Shell.winched). winched, /// A SIGHUP: the session's own terminal hung up. @@ -187,12 +164,11 @@ fn inputReader(loop: *Loop, tty: anytype, vx: *vaxis.Vaxis, feed: *Feed) void { if (err == error.Canceled) return; std.log.err("terminal input: {s}", .{@errorName(err)}); }; - (Poster{ .loop = loop, .view = feed.view }).postEvent(.quit) catch {}; + loop.postEvent(.quit) catch {}; } fn readInput(loop: *Loop, tty: anytype, vx: *vaxis.Vaxis, feed: *Feed) !void { - const post: Poster = .{ .loop = loop, .view = feed.view }; - try post.postEvent(.{ .winsize = try tty.getWinsize() }); + try loop.postEvent(.{ .winsize = try tty.getWinsize() }); var parser: vaxis.Parser = .{}; var buf: [1024]u8 = undefined; var carried: usize = 0; @@ -253,7 +229,7 @@ fn readInput(loop: *Loop, tty: anytype, vx: *vaxis.Vaxis, feed: *Feed) !void { // chord, and a shell pane was typed its private-use code. if (result.event) |event| if (event == .key_press and event.key_press.isModifier()) continue; if (result.event) |event| - vaxis.loop.handleEventGeneric(post, vx, &feed.cache, Msg, event, vx.opts.system_clipboard_allocator) catch |err| { + vaxis.loop.handleEventGeneric(loop, vx, &feed.cache, Msg, event, vx.opts.system_clipboard_allocator) catch |err| { if (event == .paste) if (vx.opts.system_clipboard_allocator) |gpa| gpa.free(@constCast(event.paste)); return err; }; @@ -1163,7 +1139,7 @@ pub fn run(init: std.process.Init, opts: pardes.Options, term: Term) !void { posix.sigprocmask(posix.SIG.BLOCK, &winch_set, null); var tty_buf: [0x10000]u8 = undefined; - var screen: Screen = .{ .io = io, .gpa = gpa, .env = init.environ_map, .buf = &tty_buf, .kitty = .init(gpa), .primary = true }; + var screen: Screen = .{ .io = io, .gpa = gpa, .env = init.environ_map, .buf = &tty_buf, .kitty = .init(gpa) }; defer screen.kitty.deinit(); switch (term) { .open => try screen.enter(try vaxis.Tty.init(io, &tty_buf)), @@ -1269,7 +1245,7 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se posix.sigaction(.WINCH, &.{ .handler = .{ .handler = keepSignal }, .mask = posix.sigemptyset(), .flags = 0 }, null); posix.sigprocmask(posix.SIG.BLOCK, &winch_set, null); var tty_buf: [0x10000]u8 = undefined; - var screen: Screen = .{ .io = io, .gpa = gpa, .env = init.environ_map, .buf = &tty_buf, .kitty = .init(gpa), .primary = true }; + var screen: Screen = .{ .io = io, .gpa = gpa, .env = init.environ_map, .buf = &tty_buf, .kitty = .init(gpa) }; defer screen.kitty.deinit(); try screen.enter(try vaxis.Tty.init(io, &tty_buf)); defer screen.leave(); @@ -1294,7 +1270,8 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se .frame = &frame_arena, .paste_buf = &paste_buf, .inotify_fd = -1, - .from = joined.view, + .own = joined.view, + .screen = &screen, .remote = &remote_link, }; @@ -1307,10 +1284,7 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se // session let it go to upgrade. The terminal stays as it is. while (true) { sh.core = r.core(); - sh.from = joined.view; - sh.terms = @splat(null); - sh.terms[joined.view] = &screen; - screen.feed.view = joined.view; + sh.own = joined.view; link.reset(r, &joined); var reader_done: std.atomic.Value(bool) = .init(false); const reader = try std.Thread.spawn(.{}, linkReader, .{ gpa, joined.fd, &joined.rest, &loop, &reader_done }); @@ -1396,9 +1370,8 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se /// A terminal a session draws on, while it has one: entered (raw, the /// alternate screen, mouse and paste reports) and left (all of it put back, /// the terminal closed) at the session's start and end, and at a detach and -/// a reattach in between (daemon.zig). One a view (views.zig): the session's -/// own (`primary`, its stub `daemon.stub`), and one more for each terminal -/// `pardes --attach`ed while it is attached, which keeps its own. +/// a reattach in between (daemon.zig), its stub `daemon.stub`. One a +/// process: a second terminal on a session is a replica client of it. const Screen = struct { io: std.Io, gpa: std.mem.Allocator, @@ -1407,11 +1380,6 @@ const Screen = struct { tty: vaxis.Tty = undefined, vx: vaxis.Vaxis = undefined, on: bool = false, - primary: bool = false, - /// The pipe to its stub, an attached one's (the primary's is daemon.stub). - stub: posix.fd_t = -1, - /// Its slot in Killed. - killed: ?usize = null, feed: Feed = .{}, thread: ?std.Io.Future(void) = null, /// The kitty images it holds, by placement key. @@ -1446,12 +1414,11 @@ const Screen = struct { try sc.vx.setMouseMode(sc.tty.writer(), true); try sc.vx.setBracketedPaste(sc.tty.writer(), true); if (comptime !builtin.is_test) { - sc.killed = Killed.arm(sc.tty.fd.handle, sc.tty.termios); + Killed.arm(sc.tty.fd.handle, sc.tty.termios); // What a panic puts back (vaxis.recover). - if (sc.primary) vaxis.tty.global_tty = sc.tty; + vaxis.tty.global_tty = sc.tty; } - const view = sc.feed.view; - sc.feed = .{ .view = view }; + sc.feed = .{}; sc.caps_pending = true; sc.size = null; sc.on = true; @@ -1460,28 +1427,17 @@ const Screen = struct { fn leave(sc: *Screen) void { if (!sc.on) return; sc.on = false; - if (sc.killed) |slot| Killed.disarm(slot); - sc.killed = null; + Killed.disarm(); clearNativeImages(&sc.kitty, &sc.vx, &sc.tty); sc.vx.exitAltScreen(sc.tty.writer()) catch {}; sc.vx.deinit(sc.gpa, sc.tty.writer()); - if (comptime !builtin.is_test) if (sc.primary) { - vaxis.tty.global_tty = null; - }; + if (comptime !builtin.is_test) vaxis.tty.global_tty = null; sc.tty.deinit(); // restore cooked termios LAST, after vx flushed its resets } - /// Its stub told the terminal is detached (it ends, and its shell has - /// the terminal back). - fn sayDetached(sc: *Screen) void { - if (sc.primary) return daemon.tellStubDetached(); - daemon.tellStub(sc.stub, "d{s}", .{daemon.name()}); - sc.stub = -1; - } - /// Its terminal hung up, or the stub that waits on it is gone. fn hungUp(sc: *const Screen) bool { - const stub = if (sc.primary) daemon.stub else sc.stub; + const stub = daemon.stub; const handle = if (@TypeOf(sc.tty.fd) == std.Io.File) sc.tty.fd.handle else sc.tty.fd; var fds = [_]posix.pollfd{ .{ .fd = handle, .events = 0, .revents = 0 }, @@ -1491,79 +1447,38 @@ const Screen = struct { const gone = posix.POLL.HUP | posix.POLL.ERR; return fds[0].revents & gone != 0 or (stub >= 0 and fds[1].revents & gone != 0); } - - fn setStub(sc: *Screen, fd: posix.fd_t) void { - if (sc.primary) daemon.stub = fd else sc.stub = fd; - } - - /// Another terminal's Screen, on the heap with a buffer of its own - /// (`destroy` lets both go); null with no room for either. - fn create(io: std.Io, gpa: std.mem.Allocator, env: *std.process.Environ.Map) ?*Screen { - const sc = gpa.create(Screen) catch return null; - const buf = gpa.alloc(u8, 0x10000) catch { - gpa.destroy(sc); - return null; - }; - sc.* = .{ .io = io, .gpa = gpa, .env = env, .buf = buf, .kitty = .init(gpa) }; - return sc; - } - - /// The terminal put back and its stub told; an attached one's Screen - /// freed (the session's own stays, a view of none). - fn drop(sc: *Screen, how: Shell.Leaving) void { - sc.leave(); - if (sc.primary) return sc.sayDetached(); - switch (how) { - .detach => sc.sayDetached(), - .exit => { - daemon.tellStub(sc.stub, "x{d}", .{0}); - sc.stub = -1; - }, - } - sc.destroy(); - } - - fn destroy(sc: *Screen) void { - sc.glide.deinit(sc.gpa); - sc.art.deinit(sc.gpa); - sc.kitty.deinit(); - sc.gpa.free(sc.buf); - sc.gpa.destroy(sc); - } }; -/// SIGTERM or SIGHUP: each terminal put back as a clean exit leaves it -- +/// SIGTERM or SIGHUP: the terminal put back as a clean exit leaves it -- /// the main screen, no mouse, paste or keyboard modes, the cursor shown, /// cooked termios -- and then the signal's own death. A nested editor /// killed in a pane must not leave that pane's terminal wedged. /// Async-signal-safe: write, tcsetattr, sigaction, raise. const Killed = struct { - var fds: [pardes.views.max]posix.fd_t = @splat(-1); - var cooked: [pardes.views.max]posix.termios = undefined; + 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"; - fn arm(tty_fd: posix.fd_t, termios: posix.termios) ?usize { - const slot = std.mem.findScalar(posix.fd_t, &fds, -1) orelse return null; - cooked[slot] = termios; - fds[slot] = tty_fd; + fn arm(tty_fd: posix.fd_t, termios: posix.termios) void { + cooked = termios; + fd = tty_fd; const on: posix.Sigaction = .{ .handler = .{ .handler = handle }, .mask = posix.sigemptyset(), .flags = posix.SA.RESETHAND }; posix.sigaction(.TERM, &on, null); // A hangup a daemon keeps the session through is winchWatch's. if (daemon.link < 0) posix.sigaction(.HUP, &on, null); - return slot; } /// 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. - fn disarm(slot: usize) void { - fds[slot] = -1; + fn disarm() void { + fd = -1; } fn handle(sig: posix.SIG) callconv(.c) void { - for (fds, &cooked) |fd, *termios| if (fd >= 0) { + if (fd >= 0) { _ = std.c.write(fd, reset, reset.len); - _ = std.c.tcsetattr(fd, .FLUSH, termios); - }; + _ = std.c.tcsetattr(fd, .FLUSH, &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(); @@ -1627,7 +1542,7 @@ fn localSession( .fs = fs, .test_clock = host_io.testClock(), }; - sh.terms[0] = screen; + sh.screen = screen; sh.peers = .{ .gpa = gpa, .io = io, .name = daemon.name(), .wake_ctx = loop, .wake = wakeForReplica }; defer sh.peers.deinit(); // Before the core goes: each client sees its link end. @@ -1636,13 +1551,6 @@ fn localSession( host_io.setExitWake(loop, wakeForExit); defer screen.glide.deinit(gpa); defer screen.art.deinit(gpa); - // The terminals of the other views end with the session: each put back, - // its stub told. - defer for (&sh.terms) |*slot| if (slot.*) |sc| if (!sc.primary) { - stopInput(io, &sc.thread); - sc.drop(.exit); - slot.* = null; - }; defer { host_io.setExitWake(null, null); pardes.lsp.setStatusSink(null, null); @@ -1695,7 +1603,7 @@ fn localSession( core.host = host; // An upgraded session's terminals, its own again. - if (comptime !builtin.is_test) if (upgrade.resumed) |*rec| sh.resumeTerminals(rec, screen); + if (comptime !builtin.is_test) if (upgrade.resumed) |*rec| sh.resumeTerminals(rec); // Nobody to see it, and nobody to size it: `size` sets it (ctl.zig). sh.setAttended(sh.attached()); while (core.nextEffect()) |effect| core.perform(effect); @@ -1714,18 +1622,6 @@ fn localSession( if (screen.on) try screen.vx.queryTerminalSend(screen.tty.writer()); // Detached from the start, nothing else may wake the first wait. if (!screen.on) daemon.watch(loop, deliverNote); - // PARDES_TEST_VIEWS: the terminals at these paths are views of the - // session too, from its start, as `pardes --attach`ed ones are (the - // snapshot harness's `start ... views=N`). - if (upgrade.resumed == null) if (init.environ_map.get("PARDES_TEST_VIEWS")) |paths| { - var it = std.mem.tokenizeScalar(u8, paths, ' '); - while (it.next()) |path| { - var path_buf: [256]u8 = undefined; - const z = std.fmt.bufPrintSentinel(&path_buf, "{s}", .{path}, 0) catch continue; - const fd = libc.open(z, .{ .ACCMODE = .RDWR, .NOCTTY = true, .CLOEXEC = true }, @as(libc.mode_t, 0)); - if (fd >= 0) sh.attachTerminal(fd, -1, null); - } - }; sh.threads_ok = true; for (&sh.ptys, 0..) |*slot, id| if (slot.*) |*pt| { @@ -1755,18 +1651,10 @@ fn localSession( sh.pipe_tasks.cancelAll(io); for (&sh.gens) |*generation| generation.* +%= 1; for (0..pardes.MAX_PANES) |pane| Shell.closePty(&sh, @intCast(pane)); - // One view, the live one's terminal (host_io.restoreCore). - const live = sh.term().?; - for (&sh.terms) |*slot| if (slot.*) |sc| if (sc != live) { - stopInput(io, &sc.thread); - sc.drop(.detach); - slot.* = null; - }; - sh.terms = @splat(null); - sh.terms[0] = live; + // One view, the terminal's (host_io.restoreCore). + const live = screen; stopInput(io, &live.thread); - live.feed.view = 0; - sh.from = 0; + sh.own = 0; if (live.on) startInput(loop, live) catch {}; if (live.on) clearNativeImages(&live.kitty, &live.vx, &live.tty); nc.view().frontend.native_images = live.on and live.vx.caps.kitty_graphics; @@ -1836,11 +1724,10 @@ const Shell = struct { core: *pardes.Pardes, prompt_rcs: *const host_io.Shell.PromptFiles, loop: *Loop, - /// Each view's terminal, by view (views.zig): the session's own first; - /// none in tests. - terms: [pardes.views.max]?*Screen = @splat(null), - /// The view the input taken now is from (`Msg.from`). - from: u8 = 0, + /// The terminal (none in tests), and the view it shows: a second + /// terminal on the session is a replica client (`pardes --attach`). + screen: ?*Screen = null, + own: u8 = 0, frame: *std.heap.ArenaAllocator, paste_buf: *std.Io.Writer.Allocating, inotify_fd: c_int, @@ -1857,8 +1744,6 @@ const Shell = struct { job_tasks: PipeTasks = .{}, threads_ok: bool = false, check_files: bool = false, - // ponytail: one bracketed paste at a time for every terminal: keys - // another terminal sends while one pastes join its paste. in_paste: bool = false, /// PARDES_TEST_CLOCK's virtual time (host_io.testClock), and where the /// running-out wait moves it: the core's next wake. @@ -1894,8 +1779,6 @@ const Shell = struct { /// its inputs go to the replica, never straight to the core. remote: ?*Remote = null, - const Leaving = enum { detach, exit }; - /// A terminal's input, to the core, or to a replica client's engine /// (applied at once there, and sent to the session). fn feed(s: *Shell, ev: pardes.Event) void { @@ -1906,8 +1789,7 @@ const Shell = struct { /// Whether a terminal of this process's shows the session, or /// nothing (a test) can. fn local(s: *const Shell) bool { - for (s.terms) |slot| if (slot != null) return true; - return false; + return s.screen != null; } /// Whether anyone looks at the session: a terminal here, or a replica @@ -1975,25 +1857,20 @@ const Shell = struct { return s.test_clock orelse @intCast(std.Io.Clock.awake.now(s.io).nanoseconds); } - /// The live view's terminal (null in tests). + /// The terminal, while its view is live (null in tests, and while a + /// replica client's is: its input the last). fn term(s: *const Shell) ?*Screen { - return s.terms[s.core.live]; + return if (s.core.live == s.own) s.screen else null; } fn attached(s: *const Shell) bool { - const screen = s.term() orelse return true; + const screen = s.screen orelse return true; return screen.on; } - /// Drawn only while a terminal shows it: a replica client's view live - /// (its input the last), whichever terminal here shows another. + /// Drawn only while a terminal shows it. fn watched(ctx: ?*anyopaque) bool { - const s = of(ctx); - if (s.term() == null and s.local()) { - for (s.terms) |slot| if (slot) |sc| if (sc.on) return true; - return false; - } - return s.attached(); + return of(ctx).attached(); } /// Detach: the terminal put back and let go, the stub told (it ends, @@ -2003,24 +1880,12 @@ const Shell = struct { // A replica client's: its link ends. if (s.peers.detach(s.core.live)) return; if (!s.attached()) return s.core.reportFailure(s.core.view().active, "Detach: no terminal is attached"); - // One of several: its view goes, and the session stays on the rest. - if (pardes.views.count(s.core) > 1) return s.dropView(s.core.live, .detach); if (!daemon.kept()) return s.core.reportFailure(s.core.view().active, "Detach: no pardes daemon keeps this session (it is its terminal's job)"); s.leaveTerminal(); } - /// View `k`'s terminal let go (detached, or gone) while others stay: the - /// view goes with it. - fn dropView(s: *Shell, k: u8, how: Leaving) void { - const sc = s.terms[k] orelse return; - stopInput(s.io, &sc.thread); - s.terms[k] = null; - s.core.closeView(k); - sc.drop(how); - } - fn leaveTerminal(s: *Shell) void { - const screen = s.term() orelse return; + const screen = s.screen orelse return; stopInput(s.io, &screen.thread); screen.leave(); s.core.view().frontend.native_images = false; @@ -2031,46 +1896,33 @@ const Shell = struct { for (0..3) |i| _ = libc.dup2(null_fd, @intCast(i)); if (null_fd > 2) _ = libc.close(null_fd); } - screen.sayDetached(); + daemon.tellStubDetached(); daemon.setState(true); } - /// `pardes --attach`'s terminal: for a detached session its own again, - /// entered as the first one was; for one attached already, a view of - /// its own (views.zig). Sized, asked what it can do, and drawn whole. + /// `pardes --attach`'s terminal, for a detached session: its own again, + /// entered as the first one was, sized, asked what it can do, and drawn + /// whole. One attached already takes no other (the daemon makes a second + /// a replica client). fn attachTerminal(s: *Shell, fd: posix.fd_t, stub: posix.fd_t, cooked: ?posix.termios) void { - // A replica client's view live (its input the last) has no terminal - // here: the session's own is the one a terminal takes, or joins. - const live = s.term() orelse for (s.terms) |slot| { - if (slot) |sc| if (sc.primary) break sc; - } else { + const screen = s.screen orelse { _ = libc.close(fd); return refuse(stub, "that session has no terminal of its own to attach to"); }; - const screen = if (!live.on) live else Screen.create(s.io, s.gpa, live.env) orelse { + if (screen.on) { _ = libc.close(fd); - return refuse(stub, "that session has no room for another terminal"); - }; - const extra = screen != live; + return refuse(stub, "that session has a terminal: join it as a client, `pardes --attach=<socket>:<name>`"); + } const tty = Screen.ttyOn(s.io, screen.buf, fd, cooked) catch { _ = libc.close(fd); - if (extra) screen.destroy() else daemon.setState(true); + daemon.setState(true); return refuse(stub, "that terminal would not go raw"); }; screen.enter(tty) catch { - if (extra) screen.destroy() else daemon.setState(true); + daemon.setState(true); return refuse(stub, "that terminal would not take the screen"); }; - if (extra) { - const k = s.core.openView() orelse { - screen.leave(); - screen.destroy(); - return refuse(stub, "that session has as many terminals as it takes"); - }; - screen.feed.view = k; - s.terms[k] = screen; - } - screen.setStub(stub); + daemon.stub = stub; s.setAttended(true); startInput(s.loop, screen) catch {}; screen.vx.queryTerminalSend(screen.tty.writer()) catch {}; @@ -2124,7 +1976,7 @@ const Shell = struct { s.upgrading = true; defer s.upgrading = false; for (&s.ptys) |*slot| if (slot.*) |*pt| pt.reader.cancel(s.io) catch {}; - for (s.terms) |slot| if (slot) |sc| stopInput(s.io, &sc.thread); + if (s.screen) |sc| stopInput(s.io, &sc.thread); while (s.loop.tryEvent() catch null) |ev| _ = s.apply(ev); core.settle(); // Its replica clients told, their views gone before the dump: each @@ -2132,11 +1984,11 @@ const Shell = struct { s.peers.refuseAll(core, "the session is upgrading to another build of pardes"); var rec: upgrade.Record = .{ .link = daemon.link, .stub = daemon.stub, .exe = path }; for (s.ptys, 0..) |slot, id| if (slot) |pt| rec.addPty(id, pt.pid, pt.file.handle); - for (s.terms, 0..) |slot, k| if (slot) |sc| if (sc.on) rec.addTty(.{ - .view = @intCast(k), + if (s.screen) |sc| if (sc.on) rec.addTty(.{ + .view = s.own, .fd = sc.tty.fd.handle, - .stub = if (sc.primary) daemon.stub else sc.stub, - .primary = sc.primary, + .stub = daemon.stub, + .primary = true, .termios = sc.tty.termios, }); var popped = false; @@ -2144,7 +1996,7 @@ const Shell = struct { const failed: []const u8 = upgrade.dumpInto(core, &rec, &at_buf, &why) orelse failed: { // What this process put in its terminals that the next puts in // again: its images, and its keyboard mode, a stack. - for (s.terms) |slot| if (slot) |sc| if (sc.on) { + if (s.screen) |sc| if (sc.on) { clearNativeImages(&sc.kitty, &sc.vx, &sc.tty); if (sc.vx.state.kitty_keyboard) sc.tty.writer().writeAll("\x1b[<u") catch {}; sc.vx.state.kitty_keyboard = false; @@ -2158,7 +2010,7 @@ const Shell = struct { for (&s.ptys, 0..) |*slot, id| if (slot.*) |*pt| { pt.reader = s.io.concurrent(readPty, .{ s.io, s.gpa, pt.file, id, s.gens[id], s.loop }) catch pt.reader; }; - for (s.terms) |slot| if (slot) |sc| if (sc.on) { + if (s.screen) |sc| if (sc.on) { if (popped) sc.vx.enableDetectedFeatures(sc.tty.writer()) catch {}; startInput(s.loop, sc) catch {}; }; @@ -2179,57 +2031,25 @@ const Shell = struct { } } - /// An upgraded session's terminals, each its view's again (`primary`, - /// run's, entered already); in a fresh session each but the primary a - /// view of its own, as `--attach` makes one. A view left with none - /// closes, but a replica client's, kept for its rejoin (views.away). - fn resumeTerminals(s: *Shell, rec: *const upgrade.Record, primary: *Screen) void { + /// An upgraded session's terminal, its view's again (`primary`, run's, + /// entered already); every other view but a replica client's, kept for + /// its rejoin (views.away), closes. A terminal another build kept for a + /// view of its own is let go, its stub told it is detached: one again + /// is a replica client. + fn resumeTerminals(s: *Shell, rec: *const upgrade.Record) void { if (comptime builtin.is_test) unreachable; const core = s.core; - if (rec.fresh) { - for (rec.ttys[0..rec.nttys]) |t| if (!t.primary) s.attachTerminal(t.fd, t.stub, t.termios); - return; - } - s.terms = @splat(null); - if (rec.nttys == 0) { - // The live view alone (never one kept for a rejoin) is the - // session's own. - for (0..pardes.views.max) |k| if (k != core.live and pardes.views.isOpen(core, @intCast(k)) and !pardes.views.isAway(core, @intCast(k))) core.closeView(@intCast(k)); - s.terms[core.live] = primary; - primary.feed.view = core.live; - return; - } + s.own = core.live; for (rec.ttys[0..rec.nttys]) |t| { - if (!pardes.views.isOpen(core, t.view) or s.terms[t.view] != null) { - if (t.primary) continue; - _ = libc.close(t.fd); - if (t.stub >= 0) daemon.tellStub(t.stub, "d{s}", .{daemon.name()}); + if (t.primary) { + if (!rec.fresh and pardes.views.isOpen(core, t.view)) s.own = t.view; continue; } - const sc = if (t.primary) primary else extra: { - const sc = Screen.create(s.io, s.gpa, primary.env) orelse break :extra null; - const tty = Screen.ttyOn(s.io, sc.buf, t.fd, t.termios) catch { - sc.destroy(); - break :extra null; - }; - sc.enter(tty) catch { - sc.destroy(); - break :extra null; - }; - sc.setStub(t.stub); - startInput(s.loop, sc) catch {}; - sc.vx.queryTerminalSend(sc.tty.writer()) catch {}; - break :extra sc; - } orelse { - _ = libc.close(t.fd); - if (t.stub >= 0) daemon.tellStub(t.stub, "d{s}", .{daemon.name()}); - continue; - }; - sc.feed.view = t.view; - s.terms[t.view] = sc; + _ = libc.close(t.fd); + if (t.stub >= 0) daemon.tellStub(t.stub, "d{s}", .{daemon.name()}); } - for (0..pardes.views.max) |k| if (pardes.views.isOpen(core, @intCast(k)) and s.terms[k] == null and !pardes.views.isAway(core, @intCast(k))) core.closeView(@intCast(k)); - if (s.terms[core.live] == null) s.terms[core.live] = primary; + if (rec.fresh) return; + for (0..pardes.views.max) |k| if (k != s.own and pardes.views.isOpen(core, @intCast(k)) and !pardes.views.isAway(core, @intCast(k))) core.closeView(@intCast(k)); } fn waitInput(ctx: ?*anyopaque, timeout_ms: u32) void { @@ -2394,27 +2214,20 @@ const Shell = struct { } if (s.input_seen) s.input_ns = @intCast(std.Io.Clock.awake.now(s.io).nanoseconds); if (event != .replica_mail) s.mail_only = false; - // A terminal's input goes into its view; a closed one's last is - // dropped. + // The terminal's input goes into its view. switch (event) { .key_press, .mouse, .paste, .paste_start, .paste_end, .winsize, .focus_in, .focus_out, .quit => { - if (s.terms[s.from] == null and s.local()) { - if (event == .paste) s.gpa.free(@constCast(event.paste)); - return false; - } - if (s.local() and s.remote == null) core.toView(s.from); + if (s.local() and s.remote == null) core.toView(s.own); }, else => {}, } switch (event) { .nop => {}, - .from => |k| s.from = k, .winched => s.winched(), - // A terminal hung up (its stub passes the hangup on) or its stub - // died (a forked session's death signal): that one's input has - // ended. A HUP says not whose: each is asked. - .hangup => for (s.terms, 0..) |slot, k| if (slot) |sc| if (sc.on and sc.hungUp()) { - s.from = @intCast(k); + // The terminal hung up (its stub passes the hangup on) or its + // stub died (a forked session's death signal): its input has + // ended. + .hangup => if (s.screen) |sc| if (sc.on and sc.hungUp()) { if (s.apply(.quit)) return true; }, .wake => if (s.test_clock) |*virtual| { @@ -2425,11 +2238,6 @@ const Shell = struct { c.link.quit = true; return true; } - // One of several: its view goes with it. - if (pardes.views.count(core) > 1) { - s.dropView(core.live, .detach); - return true; - } // The terminal went: a session the daemon keeps carries on // without it, to be attached again. Gone before the first // frame, the reader that takes the daemon's `ok` (kept @@ -2466,7 +2274,7 @@ const Shell = struct { s.feed(.pointer_leave); s.feed(.{ .focus = false }); }, - .winsize => |ws| if (s.attached()) s.resized(s.terms[s.from], ws), + .winsize => |ws| if (s.attached()) s.resized(s.screen, ws), .pty_read => |pr| { tookRead(s.io, pr.id); s.output_seen = true; @@ -2567,7 +2375,7 @@ const Shell = struct { return false; } - /// Terminal `screen` (the live view's) is `ws` big. + /// Terminal `screen` (its view live) is `ws` big. fn resized(s: *Shell, screen: ?*Screen, ws: vaxis.Winsize) void { if (screen) |sc| { sc.size = ws; @@ -2576,17 +2384,15 @@ const Shell = struct { s.feed(.{ .resize = if (comptime pardes.pdf_enabled) terminalSize(ws.cols, ws.rows, ws.x_pixel, ws.y_pixel) else .{ .cols = ws.cols, .rows = ws.rows } }); } - /// A SIGWINCH: each terminal whose size it changed is resized, in its - /// view (a terminal reporting in band says so as input). + /// A SIGWINCH: the terminal, if it changed size, resized in its view + /// (one reporting in band says so as input). fn winched(s: *Shell) void { - for (s.terms, 0..) |slot, k| { - const sc = slot orelse continue; - if (!sc.on or sc.vx.state.in_band_resize) continue; - const ws = sc.tty.getWinsize() catch continue; - if (sc.size) |was| if (std.meta.eql(was, ws)) continue; - if (s.remote == null) s.core.toView(@intCast(k)); - s.resized(sc, ws); - } + const sc = s.screen orelse return; + if (!sc.on or sc.vx.state.in_band_resize) return; + const ws = sc.tty.getWinsize() catch return; + if (sc.size) |was| if (std.meta.eql(was, ws)) return; + if (s.remote == null) s.core.toView(s.own); + s.resized(sc, ws); } fn reloadWatched(s: *Shell) void { |
