summaryrefslogtreecommitdiff
path: root/dev/tui
diff options
context:
space:
mode:
Diffstat (limited to 'dev/tui')
-rw-r--r--dev/tui/tui.zig277
1 files changed, 118 insertions, 159 deletions
diff --git a/dev/tui/tui.zig b/dev/tui/tui.zig
index 5903e448..c2f3dbf1 100644
--- a/dev/tui/tui.zig
+++ b/dev/tui/tui.zig
@@ -16,6 +16,7 @@ const file_watch = @import("../file_watch.zig");
const selection_pipe = @import("core").selection_pipe;
const ninep_io = @import("runtime").ninep_io;
const daemon = @import("runtime").daemon;
+const client_loop = @import("runtime").client;
const host_io = @import("../host_io.zig");
const upgrade = @import("runtime").upgrade;
const replica = @import("core").replica;
@@ -619,44 +620,106 @@ pub fn run(init: std.process.Init, opts: pardes.Options, term: Term) !void {
try localSession(init, opts, &screen, &loop, &paste_buf);
}
-/// A replica client's link to its session (`runClient`), drawn here.
-const Client = struct {
- link: replica.Link,
+/// A replica client's terminal (`runClient`): the frontend of the client
+/// loop (runtime/client.zig), its view drawn here as the session draws its
+/// terminals'.
+const Client = client_loop.Loop(Tty);
- /// The client's own view drawn on its terminal, outside any step: the
- /// replica's digest stays the session's (views.drawOne); the frame
- /// statistics are this process's (`Shell.frames`).
- fn draw(c: *Client, s: *Shell) void {
- const frames = s.frames;
- frames.mark_start = pardes.FrameStats.nowNs();
- frames.mark_update = frames.mark_start;
- c.link.owed = !c.link.r.draw(.{ .ctx = s, .f = Shell.present });
- frames.mark_render = frames.mark_update;
- const drew = frames.bytes > 0;
- frames.record(pardes.FrameStats.nowNs(), false, c.link.owed);
- c.link.drawn(drew);
+const Tty = struct {
+ sh: *Shell,
+ screen: *Screen,
+ loop: *Loop,
+ joins: *client_loop.Start,
+
+ /// A join's first inputs: what it is, then its size.
+ pub fn start(t: *Tty, l: *Client) void {
+ t.sh.core = l.link.r.core();
+ t.sh.own = t.joins.joined.view;
+ l.input(.{ .frontend = .{ .windowed = false } });
+ t.screen.size = null;
+ t.sh.winched();
+ t.screen.vx.queueRefresh();
}
-};
-/// The session's stream, onto the loop: its units, and its end.
-fn linkReader(gpa: std.mem.Allocator, fd: posix.fd_t, rest: *replica.Framer, loop: *Loop, done: *std.atomic.Value(bool)) void {
- defer done.store(true, .release);
- const Post = struct {
- fn post(l: *Loop, said: replica.Said) bool {
- l.postEvent(.{ .said = said }) catch return false;
- return true;
+ /// A terminal's own work is all in its wait.
+ pub fn own(_: *Tty, _: *Client) bool {
+ return true;
+ }
+
+ /// The tty's own motions (its glide, a jump's trail): the last frame
+ /// again, moved on, as the session's terminal draws them (`ownFrame`),
+ /// not rendered again. One the terminal's answer holds is skipped (the
+ /// next tick's moves on).
+ pub fn again(t: *Tty, l: *Client) bool {
+ const core = l.link.r.core();
+ if (!t.sh.moving() or core.surface.cells.len == 0) return false;
+ _ = Shell.present(t.sh, &core.surface);
+ return true;
+ }
+
+ /// An event already queued is the wait's at once; otherwise the
+ /// session's own wait: an input drawn at once, the session's units and
+ /// deltas a frame a tick at most, the tty's own motions on its grid of
+ /// ticks (`waitInput`).
+ pub fn deadline(t: *Tty, _: *Client) u32 {
+ if (t.loop.tryEvent() catch null) |ev| {
+ t.sh.held = ev;
+ return 0;
}
- };
- replica.readLink(gpa, fd, rest, loop, Post.post);
- loop.postEvent(.link_gone) catch {};
-}
+ return client_loop.forever;
+ }
+
+ /// Its inputs go to the loop through `Shell.feed`, the session's
+ /// stream through `.said`; the terminal's capabilities, once it has
+ /// answered, enabled and drawn.
+ pub fn wait(t: *Tty, l: *Client, ms: u32) void {
+ Shell.waitInput(t.sh, if (ms == client_loop.forever) 0 else ms);
+ const screen = t.screen;
+ if (screen.caps_pending and screen.vx.queries_done.load(.unordered)) {
+ screen.caps_pending = false;
+ screen.vx.enableDetectedFeatures(screen.tty.writer()) catch {};
+ screen.vx.queueRefresh();
+ l.link.dirty = true;
+ }
+ }
+
+ pub fn present(ctx: ?*anyopaque, surface: *const pardes.Surface) bool {
+ const t: *Tty = @ptrCast(@alignCast(ctx.?));
+ return Shell.present(t.sh, surface);
+ }
+
+ /// Whether it wrote anything to the terminal (Shell.present counts
+ /// the bytes); its motions draw frames the timings do not count.
+ pub fn drawn(t: *Tty) client_loop.Drawn {
+ return .{ .drew = t.sh.frames.bytes > 0, .moving = false };
+ }
+
+ /// The session's stream, onto the loop.
+ pub fn said(t: *Tty, msg: replica.Said) bool {
+ t.loop.postEvent(.{ .said = msg }) catch return false;
+ return true;
+ }
+
+ pub fn ended(t: *Tty) void {
+ t.loop.postEvent(.link_gone) catch {};
+ }
+
+ /// What the reader and the terminal posted, let go.
+ pub fn drop(t: *Tty) void {
+ while (t.loop.tryEvent() catch null) |ev| switch (ev) {
+ .said => |msg| switch (msg) {
+ .unit, .screen, .refused, .blob => |b| std.heap.c_allocator.free(b),
+ .checked => {},
+ },
+ .paste => |b| t.sh.gpa.free(@constCast(b)),
+ else => {},
+ };
+ }
+};
/// `pardes --attach=<socket>:<session>` (and `<host>:<session>`, through
/// ssh, main.zig): this terminal a replica client of a session elsewhere
-/// (docs/next-daemon.md, "Phase 5 part B notes"). Its own core, loaded
-/// from the session's dump, applies its keys at once (core/replica.zig);
-/// the session's units are the truth it rolls back to. Its own view of the
-/// session is drawn here, as the session draws its terminals'.
+/// (docs/next-daemon.md, "Phase 5 part B notes"), the client loop over it.
pub fn runClient(init: std.process.Init, opts: pardes.Options, path: []const u8, session: []const u8, far_link: bool) !void {
const io = init.io;
const gpa = init.gpa;
@@ -667,33 +730,9 @@ pub fn runClient(init: std.process.Init, opts: pardes.Options, path: []const u8,
std.process.exit(1);
}
};
- var why: [600]u8 = @splat(0);
- // Far (through ssh, or PARDES_REPLICA_FAR=1): its join's blobs by hash,
- // those it holds kept from the link, and its stream in zstd. A client
- // on this machine's own socket is sent the dump whole, as before: a
- // hash and a compression there cost more than the bytes.
- const far = if (init.environ_map.get("PARDES_REPLICA_FAR")) |v| std.mem.eql(u8, v, "1") else far_link;
- var have_store: replica.Have = if (far) .init(io) else .{ .io = io };
- defer have_store.deinit();
- const have: ?*replica.Have = if (far) &have_store else null;
- const allocs = pardes.memory.init(gpa);
- defer pardes.memory.deinit();
- pardes.Term.start(allocs.image);
- pardes.Kind.heap = allocs.addon;
- pardes.syntax.start(allocs.tree_sitter);
- defer {
- pardes.Term.stop();
- pardes.syntax.stop();
- }
- var o = opts;
- o.image_allocator = allocs.image;
- var distrusted: pardes.Addon.Distrusted = .{};
- o.distrusted = &distrusted;
- // Its frame timings, this process's (Pardes.frames).
- var frames: pardes.FrameStats = .{};
- o.frames = &frames;
- var joined: replica.Joined = undefined;
- var r = replica.join(gpa, &joined, path, session, o, have, &why, null) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)});
+ var start: client_loop.Start = undefined;
+ if (!start.open(init, opts, .{ .path = path, .session = session, .far = far_link })) Said.no(io, "{s}", .{start.said()});
+ defer start.close();
var winch_set = posix.sigemptyset();
posix.sigaddset(&winch_set, posix.SIG.WINCH);
@@ -714,27 +753,27 @@ pub fn runClient(init: std.process.Init, opts: pardes.Options, path: []const u8,
var paste_buf: std.Io.Writer.Allocating = .init(gpa);
defer paste_buf.deinit();
var loop: Loop = .init(io, &screen.tty, &screen.vx);
- var frame_arena: std.heap.ArenaAllocator = .init(allocs.frame);
+ var frame_arena: std.heap.ArenaAllocator = .init(start.allocs.frame);
defer frame_arena.deinit();
- var client_link: Client = .{ .link = .{ .r = r, .fd = joined.fd } };
- const link = &client_link.link;
- try link.fromEnv(gpa, init.environ_map);
- defer link.deinit(gpa);
+ var tty: Tty = undefined;
+ var c = try Client.init(&tty, &start);
+ defer c.link.deinit(gpa);
var sh: Shell = .{
.io = io,
.gpa = gpa,
- .lsp_gpa = allocs.lsp,
- .core = r.core(),
- .frames = &frames,
+ .lsp_gpa = start.allocs.lsp,
+ .core = start.r.core(),
+ .frames = &start.frames,
.prompt_rcs = undefined,
.loop = &loop,
.frame = &frame_arena,
.paste_buf = &paste_buf,
.inotify_fd = -1,
- .own = joined.view,
+ .own = start.joined.view,
.screen = &screen,
- .client = &client_link,
+ .client = &c,
};
+ tty = .{ .sh = &sh, .screen = &screen, .loop = &loop, .joins = &start };
pardes.Kind.drawer = .{ .ctx = &sh, .go = Shell.draw };
defer pardes.Kind.drawer = null;
@@ -743,96 +782,16 @@ pub fn runClient(init: std.process.Init, opts: pardes.Options, path: []const u8,
(try std.Thread.spawn(.{}, winchWatch, .{&loop})).detach();
(try std.Thread.spawn(.{}, wakeWatch, .{ io, &loop })).detach();
try screen.vx.queryTerminalSend(screen.tty.writer());
- // A join a pass: the first, and one after the replica diverged or the
- // session let it go to upgrade. The terminal stays as it is.
- while (true) {
- sh.core = r.core();
- 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 });
- // What it is, then its size, are its view's first inputs.
- link.input(.{ .frontend = .{ .windowed = false } });
- screen.size = null;
- sh.winched();
- screen.vx.queueRefresh();
- while (!link.quit) {
- // Its predicted echo judged and caught up, once a frame.
- if (r.echoFrame()) link.dirty = true;
- if (link.dirty) {
- client_link.draw(&sh);
- } else if (sh.moving() and r.core().surface.cells.len != 0) {
- // The tty's own motions (its glide, a jump's trail): the last
- // frame again, moved on, as the session's terminal draws them
- // (`ownFrame`), not rendered again. One the terminal's answer
- // holds is skipped (the next tick's moves on).
- _ = Shell.present(&sh, &r.core().surface);
- link.again += 1;
- }
- // Idle, its frame drawn: the next key needs no copy taken.
- if (loop.tryEvent() catch null) |ev| sh.held = ev else r.ready();
- // The session's own wait: an input drawn at once, the session's
- // units and deltas a frame a tick at most, the tty's own motions
- // on its grid of ticks; predictions judged a frame at a time.
- var wait: u32 = if (r.echo.n > 0 or r.echo.cursor != null or r.echo.nd > 0) @intCast(pardes.frame_ns / std.time.ns_per_ms) else 0;
- if (link.quietCheck()) |ms| wait = if (wait == 0) ms else @min(wait, ms);
- Shell.waitInput(&sh, wait);
- r.settle();
- // A rollback since it drew (`toConfirmed`): its frame is drawn
- // again, never moved on from cells the copy put back.
- if (r.moved) {
- r.moved = false;
- link.dirty = true;
- }
- if (screen.caps_pending and screen.vx.queries_done.load(.unordered)) {
- screen.caps_pending = false;
- screen.vx.enableDetectedFeatures(screen.tty.writer()) catch {};
- screen.vx.queueRefresh();
- link.dirty = true;
- }
- if (link.owed or r.echo.changed) link.dirty = true;
- if (r.diverged != null) link.quit = true;
- }
- if (init.environ_map.get("PARDES_REPLICA_STATS")) |file| link.writeStats(file);
- if (!link.gone) replica.bye(joined.fd, r.diverged != null);
- // The link's reader ends with the link, posting as it goes: what it
- // and the terminal posted is let go until it has. Its end is read
- // before the drain, not after: its last post (`link_gone`) comes
- // just before it, and left queued it ended the next link at once.
- _ = libc.shutdown(joined.fd, 2);
- while (true) {
- const ended = reader_done.load(.acquire);
- while (loop.tryEvent() catch null) |ev| switch (ev) {
- .said => |said| switch (said) {
- .unit, .screen, .refused, .blob => |b| std.heap.c_allocator.free(b),
- .checked => {},
- },
- .paste => |b| gpa.free(@constCast(b)),
- else => {},
- };
- if (ended) break;
- std.Thread.yield() catch {};
- }
- reader.join();
- _ = libc.close(joined.fd);
- joined.rest.deinit(gpa);
- // Why it joins again, in a line over its frame until a key.
- if (!link.rejoins()) {
- // What it made since it joined, kept for its next start.
- if (have) |h| h.keep(r.core());
- r.deinit();
- if (link.gone) {
- screen.leave();
- Said.no(io, "'{s}' let this client go (a Detach in it, or the session's end)", .{session});
- }
- return;
- }
- // The session again, the core it leaves held meanwhile: its blobs
- // are not sent again.
- r = replica.join(gpa, &joined, path, session, o, have, &why, r) orelse {
+ switch (try c.run(&start)) {
+ .done => {},
+ .gone => {
screen.leave();
- Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)});
- };
+ Said.no(io, "'{s}' let this client go (a Detach in it, or the session's end)", .{session});
+ },
+ .failed => {
+ screen.leave();
+ Said.no(io, "{s}", .{start.said()});
+ },
}
}
@@ -1267,7 +1226,7 @@ const Shell = struct {
/// (applied at once there, and sent to the session).
fn feed(s: *Shell, ev: pardes.Event) void {
const c = s.client orelse return s.core.update(ev);
- c.link.input(ev);
+ c.input(ev);
}
/// Whether a terminal of this process's shows the session, or
@@ -1761,7 +1720,7 @@ const Shell = struct {
else => false,
};
if (echo) s.focused_ns = @intCast(std.Io.Clock.awake.now(s.io).nanoseconds);
- c.link.take(said);
+ c.take(said);
if (said == .refused) return true;
} else switch (said) {
.unit, .screen, .refused, .blob => |b| std.heap.c_allocator.free(b),