diff options
Diffstat (limited to 'src/replica.zig')
| -rw-r--r-- | src/replica.zig | 111 |
1 files changed, 54 insertions, 57 deletions
diff --git a/src/replica.zig b/src/replica.zig index dd6f34bc..b746ddb6 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -30,9 +30,8 @@ const walk = @import("walk.zig"); const wire = @import("wire.zig"); const daemon = @import("daemon.zig"); const writeAll = daemon.writeAll; -const screen_state = @import("screen_state.zig"); const c_heap = @import("c_heap"); -const terminal = pardes.panes.terminal; +const Term = pardes.Term; const Allocator = std.mem.Allocator; const Pardes = pardes.Pardes; @@ -281,7 +280,7 @@ pub const Authority = struct { } }; -// ---- terminals as screen state (src/screen_state.zig) ---- +// ---- terminals as screen state (src/term/screen_state.zig) ---- // // A message of either side is u8 pane, u32 the pane's serial (a slot's // next pane is another), u8 kind, and the kind's bytes: @@ -330,7 +329,7 @@ fn writeScreenHead(out: *std.ArrayListUnmanaged(u8), gpa: Allocator, pane: u8, s /// pane, and what each peer was sent of it. pub const Screens = struct { gpa: Allocator, - enc: screen_state.Encoder, + enc: Term.Encoder, tracks: [pardes.MAX_PANES]?Tracked = @splat(null), peers: std.ArrayListUnmanaged(*[pardes.MAX_PANES]PeerOf) = .empty, free_peers: std.ArrayListUnmanaged(u16) = .empty, @@ -338,8 +337,8 @@ pub const Screens = struct { /// Nanoseconds: what each flush spent encoding, for every peer. encode_ns: u64 = 0, - const Tracked = struct { serial: u32, state: *terminal.State, track: screen_state.Track }; - const PeerOf = struct { serial: u32 = 0, peer: screen_state.Peer = .{}, shadow: screen_state.ShadowSent = .{} }; + const Tracked = struct { serial: u32, state: *Term.State, track: Term.Track }; + const PeerOf = struct { serial: u32 = 0, peer: Term.Peer = .{}, shadow: pardes.Kind.ShadowSent = .{} }; pub fn init(gpa: Allocator) Screens { return .{ .gpa = gpa, .enc = .init(gpa) }; @@ -347,7 +346,7 @@ pub const Screens = struct { /// `core` is the session's, its terminals still there. pub fn deinit(s: *Screens, core: *Pardes) void { - for (&s.tracks, 0..) |*slot, id| if (slot.*) |*tr| tr.track.deinit(s.gpa, if (s.live(core, id, tr)) &tr.state.vt else null); + for (&s.tracks, 0..) |*slot, id| if (slot.*) |*tr| Term.untrack(&tr.track, s.gpa, if (s.live(core, id, tr)) tr.state else null); for (s.peers.items) |p| { for (p) |*pp| { pp.peer.deinit(s.gpa); @@ -402,12 +401,11 @@ pub const Screens = struct { pp.peer.reset(); pp.serial = pane.serial; } - var marks: [terminal.marks_len]u8 = undefined; s.out.clearRetainingCapacity(); try writeScreenHead(&s.out, s.gpa, @intCast(id), pane.serial, kind_delta); var w: std.Io.Writer.Allocating = .fromArrayList(s.gpa, &s.out); defer s.out = w.toArrayList(); - if (!try screen_state.encode(&s.enc, &tr.track, &pp.peer, &state.vt, state.changes, terminal.marksOf(state, &marks), &w.writer)) continue; + if (!try Term.encode(state, &s.enc, &tr.track, &pp.peer, &w.writer)) continue; try out.append(s.gpa, try s.gpa.dupe(u8, w.written())); } } @@ -440,14 +438,14 @@ pub const Screens = struct { // lacks, each a message before the frame naming them. for (frame.places[0..@min(frame.n, 64)]) |*pl| { // A row past a strip's bound cannot cross: not shown there. - if (@as(usize, pl.w) * 4 > screen_state.strip_max) continue; + if (@as(usize, pl.w) * 4 > pardes.Kind.strip_max) continue; var owed = try pp.shadow.owed(x.s.gpa, pl); while (owed != 0) : (owed &= owed - 1) { x.s.out.clearRetainingCapacity(); try writeScreenHead(&x.s.out, x.s.gpa, @intCast(id), pane.serial, kind_raster); var w: std.Io.Writer.Allocating = .fromArrayList(x.s.gpa, &x.s.out); defer x.s.out = w.toArrayList(); - try screen_state.encodeStrip(pl, @ctz(owed), &w.writer); + try pardes.Kind.encodeStrip(pl, @ctz(owed), &w.writer); try x.out.append(x.s.gpa, try x.s.gpa.dupe(u8, w.written())); } } @@ -455,7 +453,7 @@ pub const Screens = struct { try writeScreenHead(&x.s.out, x.s.gpa, @intCast(id), pane.serial, kind_shadow); var w: std.Io.Writer.Allocating = .fromArrayList(x.s.gpa, &x.s.out); defer x.s.out = w.toArrayList(); - if (!try screen_state.encodeShadow(x.s.gpa, &pp.shadow, &frame, k.pointer, &w.writer)) continue; + if (!try pardes.Kind.encodeShadow(x.s.gpa, &pp.shadow, &frame, k.pointer, &w.writer)) continue; try x.out.append(x.s.gpa, try x.s.gpa.dupe(u8, w.written())); }; } @@ -473,7 +471,7 @@ pub const Screens = struct { for (core.panes, 0..) |slot, id| { const pane = slot orelse continue; const state = pane.terminal orelse continue; - if (!state.read) continue; + if (!Term.wasRead(state)) continue; const pp = &s.peers.items[peer][id]; if (pp.serial != pane.serial or pp.peer.holes.items.len == 0) continue; const tr = try s.track(core, id); @@ -485,7 +483,7 @@ pub const Screens = struct { try writeScreenHead(&s.out, s.gpa, @intCast(id), pane.serial, kind_rows); var w: std.Io.Writer.Allocating = .fromArrayList(s.gpa, &s.out); defer s.out = w.toArrayList(); - try screen_state.encodeRows(&s.enc, &tr.track, &state.vt, lo, hi, &w.writer); + try Term.encodeRows(state, &s.enc, &tr.track, lo, hi, &w.writer); try out.append(s.gpa, try s.gpa.dupe(u8, w.written())); lo = hi; } @@ -497,7 +495,7 @@ pub const Screens = struct { /// Every peer is filled: the steps' reads are forgotten. pub fn filled(_: *Screens, core: *Pardes) void { for (core.panes) |slot| if (slot) |pane| if (pane.terminal) |state| { - state.read = false; + Term.clearRead(state); }; } @@ -506,7 +504,7 @@ pub const Screens = struct { if (s.tracks[id]) |*tr| { if (s.live(core, id, tr)) return tr; // Its pane closed (its emulator, the pin with it, gone). - tr.track.deinit(s.gpa, null); + Term.untrack(&tr.track, s.gpa, null); } s.tracks[id] = .{ .serial = pane.serial, .state = pane.terminal.?, .track = .init(s.gpa) }; return &s.tracks[id].?; @@ -533,7 +531,7 @@ pub const Screens = struct { try writeScreenHead(&s.out, s.gpa, m.pane, m.serial, kind_rows); var w: std.Io.Writer.Allocating = .fromArrayList(s.gpa, &s.out); defer s.out = w.toArrayList(); - try screen_state.encodeRows(&s.enc, &tr.track, &state.vt, std.mem.readInt(u64, m.payload[0..8], .little), std.mem.readInt(u64, m.payload[8..16], .little), &w.writer); + try Term.encodeRows(state, &s.enc, &tr.track, std.mem.readInt(u64, m.payload[0..8], .little), std.mem.readInt(u64, m.payload[8..16], .little), &w.writer); return try s.gpa.dupe(u8, w.written()); }, else => {}, @@ -673,20 +671,20 @@ pub const Replica = struct { /// Its terminals' screen state, as the authority's deltas made it. screens: [pardes.MAX_PANES]?ScreenOf = @splat(null), - /// Each terminal's view (`terminal.viewOf`) as the copy was taken, or + /// Each terminal's view (`Term.grid`'s view) as the copy was taken, or /// as the last delta since set it: the copy holds no emulator, and a /// speculation may move a view, so a rollback puts these back. views: [pardes.MAX_PANES]View = @splat(.{}), screen_out: std.ArrayListUnmanaged(u8) = .empty, /// Its Shadows' rasters, from the session's strips (17.3). - blobs: screen_state.Blobs = .{}, - /// Its predicted echo (src/echo.zig), drawn over its core's terminals: + blobs: pardes.Kind.Blobs = .{}, + /// Its predicted echo (src/term/echo.zig), drawn over its core's terminals: /// fed by its keys, its units and its deltas here, and by `echoFrame`. - echo: pardes.echo.Echo = .{}, + echo: Term.Echo = .{}, const ScreenOf = struct { serial: u32, - rep: screen_state.Replica = .{}, + rep: Term.Replica = .{}, /// A delta was refused, and a reset asked for: those in flight /// before it are refused unasked. refused: bool = false, @@ -739,7 +737,7 @@ pub const Replica = struct { o.state = .off; // Its emulators off the epoch, as `terminal.create` makes a screen // replica's: deltas write them while a copy is held. - pardes.dump.terminal_gpa = terminal.emulator_gpa; + pardes.dump.terminal_gpa = pardes.panes.emulator_gpa; defer pardes.dump.terminal_gpa = null; const c = try pardes.dump.load(epoch, o, bytes, null); // The chrome's colours are local, never dumped: the restored theme's. @@ -754,13 +752,13 @@ pub const Replica = struct { c.in_len = 0; c.in_head = 0; tape.adoptCore(&r.t, c); - // Its keys into a terminal drawn at once (src/echo.zig). + // Its keys into a terminal drawn at once (src/term/echo.zig). c.echo = &r.echo; return r; } pub fn deinit(r: *Replica) void { - for (&r.screens) |*slot| if (slot.*) |*sc| sc.rep.deinit(terminal.emulator_gpa); + for (&r.screens) |*slot| if (slot.*) |*sc| sc.rep.deinit(pardes.panes.emulator_gpa); if (r.t.core) |c| { const was = Stepping.begin(&r.t, &r.epoch); if (r.copy.held) _ = r.copy.restore(); @@ -1090,12 +1088,12 @@ pub const Replica = struct { fn keepViews(r: *Replica) void { if (!r.t.screen_state) return; - for (r.core().panes, &r.views) |slot, *v| v.* = .{ .at = if (slot) |pane| terminal.viewOf(pane) else null }; + for (r.core().panes, &r.views) |slot, *v| v.* = .{ .at = if (slot) |pane| if (pane.terminal) |state| pardes.Term.grid(state).view else null else null }; } fn putViews(r: *Replica, after: usize) void { if (!r.t.screen_state) return; - for (r.core().panes, r.views) |slot, v| if (v.after == after) if (slot) |pane| if (pane.terminal != null) terminal.viewTo(pane, v.at); + for (r.core().panes, r.views) |slot, v| if (v.after == after) if (slot) |pane| if (pane.terminal) |state| pardes.Term.viewTo(state, v.at); } /// Whether its core loaded from the dump is the session as it was @@ -1144,7 +1142,7 @@ pub const Replica = struct { // A pane of a kind: the authority's frame of it, drawn as it is. if (m.kind == kind_shadow) if (pane.addon) |k| { const sh = k.shadow orelse sh: { - const made = k.gpa.create(screen_state.Shadow) catch return null; + const made = k.gpa.create(pardes.Kind.Shadow) catch return null; made.* = .{}; k.shadow = made; break :sh made; @@ -1158,7 +1156,7 @@ pub const Replica = struct { const state = pane.terminal orelse return null; const slot = &r.screens[m.pane]; if (slot.* == null or slot.*.?.serial != m.serial) { - if (slot.*) |*old| old.rep.deinit(terminal.emulator_gpa); + if (slot.*) |*old| old.rep.deinit(pardes.panes.emulator_gpa); slot.* = .{ .serial = m.serial }; } const sc = &slot.*.?; @@ -1168,7 +1166,7 @@ pub const Replica = struct { switch (m.kind) { kind_delta => { const t0 = nowNs(); - const applied = sc.rep.apply(state.gpa, terminal.terminalIo(), &state.vt, m.payload) catch |err| { + const applied = Term.apply(state, &sc.rep, m.payload) catch |err| { if (err != error.BaseMismatch) r.fail("a terminal's delta did not apply"); if (sc.refused) return null; sc.refused = true; @@ -1176,13 +1174,12 @@ pub const Replica = struct { }; if (r.stats.samples) r.stats.apply_ns.append(r.gpa, nowNs() - t0) catch {}; sc.refused = false; - terminal.takeMarks(state, applied.extra); r.echo.delta(c, m.pane, sc.rep.top); // Its rows as the core keeps them read are another grid's. c.shell_rows.markStale(pane); // Its view is the authority's now: a rollback puts it back // here, and what was applied over a terminal goes on top. - if (r.copy.held) r.views[m.pane] = .{ .at = terminal.viewOf(pane), .after = r.since.items.len }; + if (r.copy.held) r.views[m.pane] = .{ .at = if (pane.terminal) |held| pardes.Term.grid(held).view else null, .after = r.since.items.len }; r.overTerminals(); var seq: [4]u8 = undefined; std.mem.writeInt(u32, &seq, applied.seq, .little); @@ -1190,7 +1187,7 @@ pub const Replica = struct { }, kind_rows => { sc.asked = null; - sc.rep.applyRows(state.gpa, &state.vt, m.payload) catch r.fail("a terminal's rows did not apply"); + Term.applyRows(state, &sc.rep, m.payload) catch r.fail("a terminal's rows did not apply"); c.shell_rows.markStale(pane); r.overTerminals(); return null; @@ -1225,13 +1222,13 @@ pub const Replica = struct { const pane = c.panes[id] orelse continue; if (pane.serial != sc.serial) continue; const state = pane.terminal orelse continue; - const shown = sc.rep.shown(&state.vt); + const shown = Term.shown(state, &sc.rep); var holes: std.ArrayList([2]u64) = .empty; defer holes.deinit(r.gpa); sc.rep.missing(shown[0], shown[1], &holes, r.gpa) catch return null; if (holes.items.len == 0) continue; const want: [2]u64 = .{ holes.items[0][0], holes.items[holes.items.len - 1][1] }; - sc.rep.blank(&state.vt, want[0], want[1]); + Term.blank(state, &sc.rep, want[0], want[1]); sc.asked = want; var range: [16]u8 = undefined; std.mem.writeInt(u64, range[0..8], want[0], .little); @@ -1852,7 +1849,7 @@ pub const Peers = struct { boundary_ns: std.ArrayListUnmanaged(u64) = .empty, cut: u32 = 0, head: [unit_head]u8 = undefined, - /// Its clients' terminals as screen state (src/screen_state.zig), while + /// Its clients' terminals as screen state (src/term/screen_state.zig), while /// it records: what each was sent. screens: Screens = undefined, msgs: std.ArrayListUnmanaged([]u8) = .empty, @@ -1903,7 +1900,7 @@ pub const Peers = struct { for (ps.list) |slot| if (slot) |peer| peer.send(&.{ &head, &ps.head, bytes }); ps.t.buf.clearRetainingCapacity(); // The sizes its steps gave terminals, now: the next flush says so. - if (ps.t.core) |core| terminal.resizeDeferred(core); + if (ps.t.core) |core| pardes.panes.resizeDeferred(core); } /// After each of the session's pumps: its unit sent, the clients that @@ -2219,7 +2216,7 @@ pub const Peers = struct { fn stop(ps: *Peers, core: *Pardes) void { if (!ps.recording) return; - terminal.resizeDeferred(core); + pardes.panes.resizeDeferred(core); ps.screens.deinit(core); if (core.host.vtable == &ps.t.vtable) core.host = ps.t.real; tape.on = null; @@ -3093,7 +3090,7 @@ fn key(r: *Replica, cp: u21, sent: *std.ArrayListUnmanaged([]u8)) !void { /// What the session sends a peer: its terminals' changes (`.flush`), or, /// around a step's unit (Peers.input), the rows the step read that the /// peer lacks (`.fill`), after which the caller gives terminals the sizes -/// the step gave them (`terminal.resizeDeferred`), then the unit. +/// the step gave them (`panes.resizeDeferred`), then the unit. fn deliver(a: *Authority, screens: *Screens, peer: u16, r: *Replica, comptime what: enum { flush, fill }) !void { const gpa = std.testing.allocator; var msgs: std.ArrayListUnmanaged([]u8) = .empty; @@ -3422,7 +3419,7 @@ test "a delta applied under a speculation outlives its rollback: the terminal st try std.testing.expect(r.copy.held); try deliver(a, &screens, peer, r, .flush); // The rows the core keeps, read once under the copy. - _ = try terminal.shellRows(r.core(), r.core().panes[shell].?); + _ = try pardes.edit.shellRows(r.core(), r.core().panes[shell].?); } for (sent.items) |m| { r.confirm(try a.take(m)); @@ -3436,7 +3433,7 @@ test "a delta applied under a speculation outlives its rollback: the terminal st // another's unit puts the copy back: the rows it kept are of an older // grid and must not be read. r.confirm(try a.local(.{ .focus = false })); - _ = try terminal.shellRows(r.core(), r.core().panes[shell].?); + _ = try pardes.edit.shellRows(r.core(), r.core().panes[shell].?); try key(r, 'x', &sent); for (0..40) |i| a.output(.{ .output = .{ .pane = shell, .bytes = try std.fmt.bufPrint(&line, "last line {d}\r\n", .{i}) } }); r.confirm((try a.pending()).?); @@ -3445,8 +3442,8 @@ test "a delta applied under a speculation outlives its rollback: the terminal st r.confirm(try a.local(.{ .focus = true })); r.settle(); const n = a.core.panes[shell].?.rows; - const back = try terminal.shellRows(r.core(), r.core().panes[shell].?); - const now = try terminal.shellRows(a.core, a.core.panes[shell].?); + const back = try pardes.edit.shellRows(r.core(), r.core().panes[shell].?); + const now = try pardes.edit.shellRows(a.core, a.core.panes[shell].?); for (now[now.len - n ..], back[back.len - n ..]) |x, y| try std.testing.expectEqualStrings(x, y); for (sent.items) |m| { r.confirm(try a.take(m)); @@ -3460,11 +3457,11 @@ test "a delta applied under a speculation outlives its rollback: the terminal st defer dump_a.deinit(); var dump_r: std.Io.Writer.Allocating = .init(gpa); defer dump_r.deinit(); - try screen_state.dumpScreen(&a.core.panes[shell].?.terminal.?.vt, &dump_a.writer); - try screen_state.dumpScreen(&r.core().panes[shell].?.terminal.?.vt, &dump_r.writer); + try Term.dumpScreen(a.core.panes[shell].?.terminal.?, &dump_a.writer); + try Term.dumpScreen(r.core().panes[shell].?.terminal.?, &dump_r.writer); try std.testing.expectEqualStrings(dump_a.written(), dump_r.written()); - const rows_a = try terminal.shellRows(a.core, a.core.panes[shell].?); - const rows_r = try terminal.shellRows(r.core(), r.core().panes[shell].?); + const rows_a = try pardes.edit.shellRows(a.core, a.core.panes[shell].?); + const rows_r = try pardes.edit.shellRows(r.core(), r.core().panes[shell].?); // As many rows (the history is the authority's to the row), the screen's // the same; above it, rows scrolled past more than a screen at once are // holes until fetched. @@ -3583,7 +3580,7 @@ test "a terminal sized again by a client's key: the step reads the grid it had, const u = try std.testing.allocator.dupe(u8, try au.take(msg)); defer std.testing.allocator.free(u); try deliver(au, sc, pe, rr, .fill); - terminal.resizeDeferred(au.core); + pardes.panes.resizeDeferred(au.core); rr.confirm(u); rr.settle(); } @@ -3609,7 +3606,7 @@ test "a terminal sized again by a client's key: the step reads the grid it had, const back = try gpa.dupe(u8, try a.unit(0, a.seq)); defer gpa.free(back); try deliver(a, &screens, peer, r, .fill); - terminal.resizeDeferred(a.core); + pardes.panes.resizeDeferred(a.core); r.confirm(back); r.settle(); try deliver(a, &screens, peer, r, .flush); @@ -3656,7 +3653,7 @@ test "pardes's own keys over a terminal's history apply at once: its view moves try std.testing.expectEqual(@as(u32, 0), r.stats.stopped); try std.testing.expectEqual(sent.items.len, r.applied); const here = r.core().panes[shell].?; - try std.testing.expect(terminal.viewOf(here) != null); + try std.testing.expect(pardes.Term.grid(here.terminal).view != null); // The shell prints while they wait: its unit, then its delta, which // sets the view the authority's (at the bottom still): what was applied // goes on top of it again. @@ -3666,7 +3663,7 @@ test "pardes's own keys over a terminal's history apply at once: its view moves try deliver(a, &screens, peer, r, .flush); r.settle(); try std.testing.expectEqual(sent.items.len, r.applied); - try std.testing.expect(terminal.viewOf(r.core().panes[shell].?) != null); + try std.testing.expect(pardes.Term.grid(r.core().panes[shell].?.terminal).view != null); // The authority takes them in order, the terminal sent before each and // the rows its step read after, as the session's loop does. for (sent.items) |m| { @@ -3684,19 +3681,19 @@ test "pardes's own keys over a terminal's history apply at once: its view moves try std.testing.expect(r.diverged == null and r.quiet()); try std.testing.expectEqual(@as(u32, 0), r.stats.mispredicted); try std.testing.expectEqual(digestOf(a), r.digest()); - try std.testing.expect(terminal.viewOf(a.core.panes[shell].?) != null); - try std.testing.expectEqual(terminal.viewOf(a.core.panes[shell].?), terminal.viewOf(r.core().panes[shell].?)); + try std.testing.expect(pardes.Term.grid(a.core.panes[shell].?.terminal).view != null); + try std.testing.expectEqual(pardes.Term.grid(a.core.panes[shell].?.terminal).view, pardes.Term.grid(r.core().panes[shell].?.terminal).view); // A rollback with nothing confirmed on top puts the view back: one more // key up, and another's unit first. try sent.append(gpa, try gpa.dupe(u8, try r.input(.{ .key = .{ .cp = 'u', .ctrl = true } }))); - const before = terminal.viewOf(a.core.panes[shell].?); - try std.testing.expect(terminal.viewOf(r.core().panes[shell].?).? < before.?); + const before = pardes.Term.grid(a.core.panes[shell].?.terminal).view; + try std.testing.expect(pardes.Term.grid(r.core().panes[shell].?.terminal).view.? < before.?); r.confirm(try a.local(.{ .focus = false })); r.settle(); const last = try gpa.dupe(u8, try a.take(sent.items[sent.items.len - 1])); defer gpa.free(last); r.toConfirmed(); - try std.testing.expectEqual(before, terminal.viewOf(r.core().panes[shell].?)); + try std.testing.expectEqual(before, pardes.Term.grid(r.core().panes[shell].?.terminal).view); r.dirty = true; r.settle(); try deliver(a, &screens, peer, r, .fill); @@ -3707,7 +3704,7 @@ test "pardes's own keys over a terminal's history apply at once: its view moves if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); try std.testing.expect(r.diverged == null and r.quiet()); try std.testing.expectEqual(digestOf(a), r.digest()); - try std.testing.expectEqual(terminal.viewOf(a.core.panes[shell].?), terminal.viewOf(r.core().panes[shell].?)); + try std.testing.expectEqual(pardes.Term.grid(a.core.panes[shell].?.terminal).view, pardes.Term.grid(r.core().panes[shell].?.terminal).view); } test "a replica joined from the session's dump replays the units after it" { |
