diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-18 17:44:54 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-18 17:44:54 -0300 |
| commit | 41f665c986886790e474bef78928f4b0d3b05c21 (patch) | |
| tree | fe14dc9afdbcc36e42fbdcb17e7257083dda9d98 | |
| parent | e3e9a2d35ded6b668c2bb668845bb39d86e0412b (diff) | |
| parent | f3a38a45c00eb02f0aaaca7185e5601ab7224913 (diff) | |
| download | pardes-41f665c986886790e474bef78928f4b0d3b05c21.tar.gz pardes-41f665c986886790e474bef78928f4b0d3b05c21.zip | |
integration: addons in a client part 4 (the scroll bench's clock, a view laid out as the active one, the Shadow path deleted, address P:H on a late joiner) with the client path's round 4 and Zig 0.17.1: no conflict; on Zig 0.17.1: replicas with five clients: diverged 0, unequal 0, deltas checked/wrong 1782/0, 1769/0, 1871/0, quiet checks 582/0, 773/0, 791/0; tui core-test 819/960 (141 skipped), shell-test 995/998, behaviour-test 274/274 and its replay 20 seeds with 0 failures, pane-test 183/184; gui frame corpus 77/77 Debug and ReleaseFast against mbase171, unit-test 1592/1596, the tui build has no SDL symbol; the perf window, each of its three runs now the gated tree's: corpus2x -0.096% (spread 0.19%), corpus-snake -0.164%: the +0.77% that held this merge was the gate measuring another workspace (the helper that ignored GATE_W)
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
| -rw-r--r-- | addons/pdf/pdf.zig | 36 | ||||
| -rw-r--r-- | docs/next-run.md | 15 | ||||
| -rw-r--r-- | docs/typ/running.typ | 4 | ||||
| -rw-r--r-- | src/Kind.zig | 410 | ||||
| -rw-r--r-- | src/draw.zig | 2 | ||||
| -rw-r--r-- | src/dump.zig | 13 | ||||
| -rw-r--r-- | src/replica.zig | 128 | ||||
| -rw-r--r-- | test/daemon.py | 6 | ||||
| -rw-r--r-- | test/panes.zig | 148 | ||||
| -rw-r--r-- | test/pdf_scroll_bench.zig | 6 | ||||
| -rw-r--r-- | test/replicas.zig | 2 |
11 files changed, 225 insertions, 545 deletions
diff --git a/addons/pdf/pdf.zig b/addons/pdf/pdf.zig index c9a49b7c..8f314bbe 100644 --- a/addons/pdf/pdf.zig +++ b/addons/pdf/pdf.zig @@ -614,7 +614,7 @@ pub const Pdf = extern struct { var said: [160]u8 = undefined; if (page == 0 or page > st.pages) return miss(std.fmt.bufPrint(&said, "has no page {d}", .{page})); const f = &st.find; - if (second > 0 and (f.len == 0 or !f.counted or countsOf(st, ownOf(st))[page - 1] < second)) return miss(std.fmt.bufPrint(&said, "has no search hit {d} on page {d}", .{ second, page })); + if (second > 0 and (f.len == 0 or !f.counted or countOn(st, ownOf(st), page - 1) < second)) return miss(std.fmt.bufPrint(&said, "has no search hit {d} on page {d}", .{ second, page })); dropSel(st); goPage(st, page - 1); if (second > 0) f.hit, f.reveal = .{ second - 1, true }; @@ -913,7 +913,7 @@ const Strip = struct { /// What a pane's process holds beside its block, found by the block's /// address: functions of the file and the block, never read by a key, the /// mouse or a tick but through these (`stripOf`, `rasterOf`, `pageText`, -/// `hitsOf`, `countsOf`, `selOf`), which make what they lack again (a +/// `hitsOf`, `countOn`, `selOf`), which make what they lack again (a /// client joined late has none of what the state jobs answered before). const Own = struct { of: ?*const Pdf = null, @@ -1034,24 +1034,19 @@ fn hitsOf(st: *const Pdf, o: *Own) []const m.pardes_pdf_search_quad { return o.hits; } -/// Each page's count of the search's hits: the job's, else counted here. -fn countsOf(st: *const Pdf, o: *Own) []const u32 { +/// Page `p`'s count of the search's hits: the job's, else that page's +/// alone found here (a client joined after the search has no counts, and +/// every page's would hold its loop for seconds on a long file). +fn countOn(st: *const Pdf, o: *const Own, p: u32) u32 { const f = &st.find; - if (o.counts.len == st.pages and std.mem.eql(u8, &o.counts_of, &f.query)) return o.counts; - heap.free(o.counts); - o.counts = heap.alloc(u32, st.pages) catch &.{}; - @memset(o.counts, 0); - o.counts_of = f.query; - const d = o.d orelse return o.counts; + if (o.counts.len == st.pages and std.mem.eql(u8, &o.counts_of, &f.query)) return o.counts[p]; + const d = o.d orelse return 0; var q: [257]u8 = @splat(0); @memcpy(q[0..f.len], f.query[0..f.len]); - for (o.counts, 0..) |*n, p| { - var r: m.pardes_pdf_search_result = undefined; - if (m.pardes_pdf_search_page(d.base, @intCast(p), &q, &r) != ok) continue; - n.* = @intCast(r.hit_count); - m.pardes_pdf_drop_search_result(d.base, r.handle); - } - return o.counts; + var r: m.pardes_pdf_search_result = undefined; + if (m.pardes_pdf_search_page(d.base, @intCast(p), &q, &r) != ok) return 0; + defer m.pardes_pdf_drop_search_result(d.base, r.handle); + return @intCast(r.hit_count); } /// The selection made: its quads and text, MuPDF's from the block's. @@ -1209,7 +1204,12 @@ fn layout(st: *Pdf, l: *Layout, k: usize) void { } if (l.rev != st.rev) { // a pixel's: P:F rounds const sp = stripOf(st, ownOf(st), k); - l.top = @round(f64Of(sp.y[st.page]) + std.math.clamp(st.frac, 0, 1) * f64Of(sp.ph[st.page])); + // Laid as the active view is (a window of its size in another + // process): the active's top as it is, a fraction and all, so the + // two show the same pixels (from P:F, rounded, one showed its page + // a pixel off the other's). + const same = if (pixels(st, st.active)) |a| a != l and a.rev == st.rev and a.vw == vw and a.vh == vh and a.fit == l.fit else false; + l.top = if (same) st.views[st.active - 1].top else @round(f64Of(sp.y[st.page]) + std.math.clamp(st.frac, 0, 1) * f64Of(sp.ph[st.page])); l.rev = st.rev; } l.top = std.math.clamp(l.top, 0, f64Of(l.height -| l.vh)); diff --git a/docs/next-run.md b/docs/next-run.md index de03bfb1..084f7c26 100644 --- a/docs/next-run.md +++ b/docs/next-run.md @@ -89,12 +89,15 @@ what is true, and whether it was measured. window's with addon panes). Today only a second attach pays it; after, every attach to a detached session does, which is the tmux habit (`pardes --detach=work`, then `pardes attach work`). -- **An addon's code does not run in a replica.** True, read: a client's - core is loaded with no kind's operations (`replica.zig:756-757`, - `c.kinds.ops = @splat(null)`), so its addon panes are Shadows, drawn from - what the session's code drew and sends (`replica.zig:309`, `:418-470`; - `Replica.screen` at `:1140-1160`). Not measured: what a Shadow costs a - local client against the session's own frontend drawing the pane. +- **An addon's code does not run in a replica.** No longer so for an + addon whose state is plain (`PA_PLAIN`: the games, the picture and the + PDF): a client loads its library at that kind's first pane + (`Addon.plainOps`), runs it as the session does and draws its pictures + itself; one it lacks shows a line saying so. Any other addon's panes + show a line in a client saying it runs in the session alone + (`Kind.draw`): the session sends no pane's pictures to a client (the + path that painted a pane for each client and sent its frames was + deleted). - **A window client scrolls whole rows and keeps its own fonts.** Not so for the wheel, measured (`.scratch/sketch/reports/pardes-measure.md` sections C and E): a notch in a client glides as it does in the diff --git a/docs/typ/running.typ b/docs/typ/running.typ index 07274157..01fb7f32 100644 --- a/docs/typ/running.typ +++ b/docs/typ/running.typ @@ -146,8 +146,8 @@ client only; the last client of a detached session leaves its layout as the session's, for the next one to find. In a client, an addon whose state is plain (`PA_PLAIN`: the games) runs in the client that shows it, from that client's own addons, its keys applied at once; one the client lacks -shows a line saying so; any other addon's panes show what the session's -code drew. A +shows a line saying so, and any other addon's panes show a line saying +it runs in the session alone. A window client's wheel glides as the session's own window does: whole rows go to the session, and the picture moves between them on the client's own clock. It takes the session's font only when the same face is installed on diff --git a/src/Kind.zig b/src/Kind.zig index 233365c7..aae6145a 100644 --- a/src/Kind.zig +++ b/src/Kind.zig @@ -7,10 +7,9 @@ //! //! The core is deterministic and a kind is the authority's: a replica's //! core has the same registry (its words, its extensions) but no code for a -//! kind a library brought, and draws its panes from the authority's frames -//! (a Shadow, screen_state.zig); but a PA_PLAIN kind, whose state is one -//! plain block the core keeps (`Slot.plain`), runs in a client that has it, -//! as the session runs it. So every call into a kind whose answer the core +//! kind a library brought, and shows its panes as a line saying so; but a +//! PA_PLAIN kind, whose state is one plain block the core keeps +//! (`Slot.plain`), runs in a client that has it, as the session runs it. So every call into a kind whose answer the core //! acts on is made at a place both cores reach, and its `Outcome` (the //! answer, what it said, a Look, a plain kind's state's hash) is taped: a //! replica takes it off the tape. Paint and the reads (body, ctl, a dump) @@ -138,11 +137,11 @@ pub const Slot = struct { // jobs' inputs, its file's hash, a placeholder's record), and what // a frame makes (the thumb, the pointer, whether one was drawn: a // view's, never a step's). - .local = .{ "ops", "st", "gpa", "arena", "plain", "file", "opened", "reloads", "jobs", "record", "thumb_at", "thumb_span", "pointer", "painted", "over", "shadow", "used" }, + .local = .{ "ops", "st", "gpa", "arena", "plain", "file", "opened", "reloads", "jobs", "record", "thumb_at", "thumb_span", "pointer", "painted", "over", "used" }, }; /// Its kind's code here; null in a placeholder (`live` false: its kind /// not in the registry, its record kept for the day it comes) or a - /// Shadow (a replica's pane of a library's kind). + /// replica's pane of a kind with no code there. ops: ?*const Ops = null, live: bool = false, st: ?*anyopaque = null, @@ -191,7 +190,7 @@ pub const Slot = struct { /// no `dump` is given again when its pane comes back. values: Buf(256) = .{}, /// Its prefix as its last tick set it (the tick's outcome, on the - /// tape: a Shadow's is the authority's); where its window is in its + /// tape: a replica's is the authority's); where its window is in its /// whole (the rail's thumb), 0 and 0 for none, as its last frame says. prefix: Buf(96) = .{}, thumb_at: f32 = 0, @@ -213,8 +212,6 @@ pub const Slot = struct { /// Its tag's default words: Term, its kind's words for its panes, /// Collapse Del. tail: Buf(160) = .{}, - /// A replica's frame of it, as the authority painted it. - shadow: ?*Shadow = null, /// A client's join: its plain kind's file, until the pane is opened /// from it (dump.plainHere). file: ?[]u8 = null, @@ -240,7 +237,6 @@ pub const Slot = struct { for (&s.jobs) |*j| if (j.*) |job| s.gpa.free(job.in); s.gpa.free(s.selection); if (checked) s.used.deinit(std.heap.page_allocator); - if (s.shadow) |sh| sh.deinit(s.gpa); if (s.plain) |b| if (b.len > 0) s.gpa.free(b); s.arena.deinit(); s.gpa.free(s.path); @@ -355,7 +351,7 @@ fn poke(p: *const Pardes, s: *Slot) void { /// Its file changed on disk: the pane opened again from the file as it is /// now, its record kept for its `load` (a PDF's place, fit and tint: the /// state `open` made from the new bytes takes them) or else its place and -/// its words. The authority's: a replica's Shadow follows its frames. +/// its words. The authority's: a replica with no code for it, its tape. pub fn fileChanged(p: *Pardes, id: usize, _: []const u8) bool { const pane = p.panes[id] orelse return false; const s = pane.addon orelse return false; @@ -415,8 +411,8 @@ pub fn close(p: *Pardes, pane: *Pane) void { } /// The pane's body painted over the `cols` x `rows` cells at (x, y): -/// its cells into `s`, its placements after the frame's others. A Shadow -/// draws the authority's frame of it; a placeholder says what it waits for. +/// its cells into `s`, its placements after the frame's others. A pane +/// whose kind has no code here, or a placeholder, says why in a line. pub fn draw(p: *Pardes, s: *surface.Surface, arena: std.mem.Allocator, pane: *Pane, id: usize, x: u16, y: u16, cols: u16, rows: u16) void { const slot = pane.addon orelse return; if (cols == 0 or rows == 0) return; @@ -424,13 +420,12 @@ pub fn draw(p: *Pardes, s: *surface.Surface, arena: std.mem.Allocator, pane: *Pa const fg: surface.Color = if (th.fg) |f| .{ .rgb = f } else .default; const bg: surface.Color = if (th.bg) |b| .{ .rgb = b } else .default; var frame: Frame = .{ .cols = cols, .rows = rows }; - if (slot.ops == null and slot.live and !caps(p, slot).plain) { - const sh = slot.shadow orelse return; - frame = .{ .cells = sh.cells, .cols = sh.cols, .rows = sh.rows, .places = sh.places.items, .n = @intCast(sh.places.items.len), .gpu = sh.gpu }; - } else if (slot.ops == null and slot.live) { - // A plain kind runs where it is shown, and this client lacks it. - var line: [160]u8 = undefined; - _ = s.print(x, y, cols, std.fmt.bufPrint(&line, "addon {s} {d} does not run in this client: it is not in its addons, or it broke its promise here", .{ slot.nameOf(), slot.version }) catch &line, .{ .fg = fg, .bg = bg }); + if (slot.ops == null and slot.live) { + // A client: a plain kind runs where it is shown, and this one lacks + // it; any other runs in its session alone. + const why = if (caps(p, slot).plain) "it is not in its addons, or it broke its promise here" else "its state is not plain (PA_PLAIN), so it runs in the session alone"; + var line: [200]u8 = undefined; + _ = s.print(x, y, cols, std.fmt.bufPrint(&line, "addon {s} {d} does not run in this client: {s}", .{ slot.nameOf(), slot.version, why }) catch &line, .{ .fg = fg, .bg = bg }); return; } else if (slot.ops == null) { var line: [160]u8 = undefined; @@ -620,7 +615,7 @@ pub fn search(p: *Pardes, id: usize, pat: []const u8) ?[]const u8 { /// the miss (empty when it said nothing). pub fn address(p: *Pardes, id: usize, text: []const u8, said: *Buf(160)) bool { const slot = (p.panes[id] orelse return false).addon orelse return false; - // By its caps, not its code: a Shadow calls as the authority does. + // By its caps, not its code: a replica without it calls as the authority does. if (!caps(p, slot).address) return false; if (tape.authorityOnly("a kind's address")) return true; var cl: Call = .{ .p = p, .id = id, .slot = slot, .missing = said }; @@ -989,7 +984,7 @@ const Call = struct { } cl.slot.state = o.state; // Its last job's token the session's, on a core whose kind asked - // none here (a Shadow's). + // none here (one with no code for it). for (o.jobs) |token| if (token != 0) { cl.slot.tokens = token; }; @@ -1343,283 +1338,6 @@ pub fn Buf(comptime n: usize) type { }; } -// ---- a pane of a kind, as a Shadow (architecture ยง16.5) ---- -// -// A replica's core has no code for a library's kind: such a pane is drawn -// from the authority's frame of it, painted at the size the peer's view -// gives it. A frame crosses as its size and the pointer it asks for, each -// row by its hash (with its cells when the peer's row there is another), -// then each placement by its key; its raster's pixels cross before it, as -// strips of rows (`raster` messages, `encodeStrip`), those over its `src` -// the peer was not sent with the frame before. The stream is ordered and -// loses nothing, so what the authority last sent a peer is what the peer -// holds, but for a raster its `Blobs` let go, which it asks for again. - -const ShadowCell = c.pa_cell; -const Place = c.pa_placement; -/// A row's or a frame's content address, as a terminal's rows have one. -const Hash = u64; - -/// A replica's frame of a pane of a kind: what Kind.draw draws for it, its -/// placements' pixels its peer's `Blobs`. -pub const Shadow = struct { - gpu: []const u8 = &.{}, - cols: u16 = 0, - rows: u16 = 0, - cells: []ShadowCell = &.{}, - hashes: []Hash = &.{}, - places: std.ArrayList(Place) = .empty, - pointer: u8 = 0, - /// The raster it last asked for again (`apply`'s): asked once. - wanted: u64 = 0, - - pub fn deinit(sh: *Shadow, gpa: std.mem.Allocator) void { - gpa.free(sh.gpu); - gpa.free(sh.cells); - gpa.free(sh.hashes); - sh.places.deinit(gpa); - gpa.destroy(sh); - } - - /// A frame message applied (`encodeShadow`'s), of the pane `serial`, - /// each placement's pixels from `blobs`: one it lacks is not shown, and - /// its name is what to ask for (once), else 0. - pub fn apply(sh: *Shadow, gpa: std.mem.Allocator, bytes: []const u8, serial: u32, blobs: *Blobs) !u64 { - var r: std.Io.Reader = .fixed(bytes); - const cols = try r.takeInt(u16, .little); - const rows = try r.takeInt(u16, .little); - if (cols != sh.cols or rows != sh.rows) { - gpa.free(sh.cells); - gpa.free(sh.hashes); - sh.cells = try gpa.alloc(ShadowCell, @as(usize, cols) * rows); - sh.hashes = try gpa.alloc(Hash, rows); - @memset(std.mem.sliceAsBytes(sh.cells), 0); - @memset(sh.hashes, 0); - sh.cols = cols; - sh.rows = rows; - } - sh.pointer = try r.takeByte(); - for (0..rows) |y| { - sh.hashes[y] = try r.takeInt(u64, .little); - if (try r.takeByte() == 0) continue; - for (sh.cells[y * cols ..][0..cols]) |*cell| { - @memcpy(&cell.text, try r.take(8)); - cell.fg = try r.takeInt(u32, .little); - cell.bg = try r.takeInt(u32, .little); - cell.attrs = try r.takeInt(u16, .little); - cell.len = try r.takeByte(); - cell.flags = try r.takeByte(); - } - } - sh.places.clearRetainingCapacity(); - var lacking: u64 = 0; - for (0..try r.takeByte()) |_| { - var pl = std.mem.zeroes(Place); - pl.w = try r.takeInt(u32, .little); - pl.h = try r.takeInt(u32, .little); - inline for (.{ &pl.src.x, &pl.src.y, &pl.src.w, &pl.src.h, &pl.at.x, &pl.at.y, &pl.at.w, &pl.at.h }) |f| f.* = try r.takeInt(u32, .little); - inline for (.{ &pl.dst.x, &pl.dst.y, &pl.dst.w, &pl.dst.h }) |f| f.* = try r.takeInt(u16, .little); - pl.key = try r.takeInt(u64, .little); - const name = std.hash.Wyhash.hash(serial, std.mem.asBytes(&pl.key)); - pl.rgba = (blobs.shows(name, &pl) orelse { - lacking = name; - continue; - }).ptr; - try sh.places.append(gpa, pl); - } - // Older frames have no optional GPU tail. - const n = if (r.seek < r.end) try r.takeInt(u32, .little) else 0; - if (n > 4 << 20) return error.BadGpuFrame; - const gpu = try gpa.dupe(u8, try r.take(n)); - gpa.free(sh.gpu); - sh.gpu = gpu; - if (lacking == sh.wanted) return 0; - sh.wanted = lacking; - return lacking; - } -}; - -/// A raster's strip: at most this many bytes of its rows (17.3). -pub const strip_max = 4 << 20; - -/// How many of a `w`-pixel raster's rows a strip holds. -pub fn stripRows(w: u32) u32 { - return @max(1, strip_max / (@as(u32, @max(w, 1)) * 4)); -} - -/// The strips of `pl`'s raster over its `src` rows, a bit each. -fn stripsOver(pl: *const Place) u64 { - const per = stripRows(pl.w); - const lo = @min(pl.src.y, pl.h) / per; - const hi = (@min(pl.src.y +| pl.src.h, pl.h) + per - 1) / per; - var bits: u64 = 0; - for (lo..@min(hi, 64)) |i| bits |= @as(u64, 1) << @intCast(i); - return bits; -} - -/// Strip `i` of `pl`'s raster, a `raster` message's payload: its key, its -/// size, its first row, and its rows' bytes. -pub fn encodeStrip(pl: *const Place, i: u32, w: *std.Io.Writer) !void { - const y0 = i * stripRows(pl.w); - const row = @as(usize, pl.w) * 4; - try w.writeInt(u64, pl.key, .little); - try w.writeInt(u32, pl.w, .little); - try w.writeInt(u32, pl.h, .little); - try w.writeInt(u32, y0, .little); - try w.writeAll(pl.rgba[y0 * row .. @min(pl.h, y0 + stripRows(pl.w)) * row]); -} - -/// A peer's rasters of the panes of a kind, made from their strips by -/// name (the key with its pane's serial), kept to `max` bytes by least -/// recent use but for those a Shadow shows (17.3). -pub const Blobs = struct { - list: std.ArrayList(Blob) = .empty, - clock: u64 = 0, - - pub const max = 64 << 20; - const Blob = struct { name: u64, w: u32, h: u32, bytes: []u8, have: u64 = 0, used: u64 = 0 }; - - pub fn deinit(b: *Blobs, gpa: std.mem.Allocator) void { - for (b.list.items) |x| gpa.free(x.bytes); - b.list.deinit(gpa); - } - - /// A `raster` message's payload (`encodeStrip`'s) into raster `name`, - /// made at its first strip; `shown`, the pixels Shadows show now. - pub fn strip(b: *Blobs, gpa: std.mem.Allocator, name: u64, payload: []const u8, shown: []const [*]const u8) !void { - var r: std.Io.Reader = .fixed(payload); - _ = try r.takeInt(u64, .little); - const w = try r.takeInt(u32, .little); - const h = try r.takeInt(u32, .little); - const y0 = try r.takeInt(u32, .little); - const rows = r.buffered(); - const row = @as(usize, w) * 4; - if (row == 0 or rows.len % row != 0 or y0 % stripRows(w) != 0 or y0 + rows.len / row > h) return error.BadShadow; - const x = for (b.list.items) |*x| { - if (x.name == name) break x; - } else made: { - var held: usize = row * h; - for (b.list.items) |x| held += x.bytes.len; - while (held > max) { - var old: ?usize = null; - for (b.list.items, 0..) |x, i| if (std.mem.indexOfScalar([*]const u8, shown, x.bytes.ptr) == null and (old == null or x.used < b.list.items[old.?].used)) { - old = i; - }; - const i = old orelse break; - held -= b.list.items[i].bytes.len; - gpa.free(b.list.swapRemove(i).bytes); - } - const bytes = try gpa.alloc(u8, row * h); - errdefer gpa.free(bytes); - try b.list.append(gpa, .{ .name = name, .w = w, .h = h, .bytes = bytes }); - break :made &b.list.items[b.list.items.len - 1]; - }; - if (x.w != w or x.h != h) return error.BadShadow; - @memcpy(x.bytes[y0 * row ..][0..rows.len], rows); - x.have |= @as(u64, 1) << @intCast(@min(63, y0 / stripRows(w))); - b.clock += 1; - x.used = b.clock; - } - - /// Raster `name`'s pixels when it has every strip `pl` shows. - fn shows(b: *Blobs, name: u64, pl: *const Place) ?[]u8 { - for (b.list.items) |*x| if (x.name == name and x.w == pl.w and x.h == pl.h and stripsOver(pl) & ~x.have == 0) { - b.clock += 1; - x.used = b.clock; - return x.bytes; - }; - return null; - } -}; - -/// What the authority last sent one peer of a pane of a kind: its rows' -/// hashes, its placements, and which strips of each raster it holds. -pub const ShadowSent = struct { - serial: u32 = 0, - cols: u16 = 0, - hashes: std.ArrayList(Hash) = .empty, - places: Hash = 0, - keys: std.ArrayList(struct { key: u64, have: u64 }) = .empty, - - pub fn deinit(s: *ShadowSent, gpa: std.mem.Allocator) void { - s.hashes.deinit(gpa); - s.keys.deinit(gpa); - s.* = .{}; - } - - /// The strips of `pl`'s raster over its `src` the peer lacks (a bit - /// each, `encodeStrip`'s `i`), counted sent now. - pub fn owed(s: *ShadowSent, gpa: std.mem.Allocator, pl: *const Place) !u64 { - const need = stripsOver(pl); - for (s.keys.items) |*k| if (k.key == pl.key) { - defer k.have |= need; - return need & ~k.have; - }; - try s.keys.append(gpa, .{ .key = pl.key, .have = need }); - return need; - } -}; - -/// Frame `f` of a pane (the pointer its paint asked for) for a peer that -/// was last sent `sent`: false, and nothing written, when it holds this -/// frame already. Its placements cross by key, their pixels as strips -/// before it (`ShadowSent.owed`), so a frame is small whatever it shows. -pub fn encodeShadow(gpa: std.mem.Allocator, sent: *ShadowSent, f: *const Frame, pointer: u8, w: *std.Io.Writer) !bool { - var hashes: std.ArrayList(Hash) = .empty; - errdefer hashes.deinit(gpa); - for (0..f.rows) |y| try hashes.append(gpa, std.hash.Wyhash.hash(0, std.mem.sliceAsBytes(f.cells[y * f.cols ..][0..f.cols]))); - const places = f.places[0..@min(f.n, 64)]; - var ph = std.hash.Wyhash.init(pointer); - for (places) |*pl| inline for (.{ pl.w, pl.h, pl.src, pl.at, pl.dst, pl.key }) |v| ph.update(std.mem.asBytes(&v)); - if (f.gpu.len != 0) ph.update(f.gpu); - const places_hash = ph.final(); - if (sent.cols == f.cols and std.mem.eql(Hash, sent.hashes.items, hashes.items) and sent.places == places_hash) { - hashes.deinit(gpa); - return false; - } - try w.writeInt(u16, f.cols, .little); - try w.writeInt(u16, f.rows, .little); - try w.writeByte(pointer); - for (hashes.items, 0..) |h, y| { - try w.writeInt(u64, h, .little); - // Held only at the same size: a peer's Shadow of another starts afresh. - const held = sent.cols == f.cols and sent.hashes.items.len == f.rows and sent.hashes.items[y] == h; - try w.writeByte(@intFromBool(!held)); - if (!held) for (f.cells[y * f.cols ..][0..f.cols]) |*cell| { - try w.writeAll(&cell.text); - try w.writeInt(u32, cell.fg, .little); - try w.writeInt(u32, cell.bg, .little); - try w.writeInt(u16, cell.attrs, .little); - try w.writeByte(cell.len); - try w.writeByte(cell.flags); - }; - } - try w.writeByte(@intCast(places.len)); - for (places) |pl| { - try w.writeInt(u32, pl.w, .little); - try w.writeInt(u32, pl.h, .little); - inline for (.{ pl.src.x, pl.src.y, pl.src.w, pl.src.h, pl.at.x, pl.at.y, pl.at.w, pl.at.h }) |v| try w.writeInt(u32, v, .little); - inline for (.{ pl.dst.x, pl.dst.y, pl.dst.w, pl.dst.h }) |v| try w.writeInt(u16, v, .little); - try w.writeInt(u64, pl.key, .little); - } - if (f.gpu.len > 4 << 20) return error.BadGpuFrame; - try w.writeInt(u32, @intCast(f.gpu.len), .little); - try w.writeAll(f.gpu); - sent.hashes.deinit(gpa); - sent.hashes = hashes; - sent.cols = f.cols; - sent.places = places_hash; - // The strips of the rasters this frame shows: a key gone is sent whole again. - var i: usize = 0; - while (i < sent.keys.items.len) { - const kept = for (places) |pl| { - if (pl.key == sent.keys.items[i].key) break true; - } else false; - if (kept) i += 1 else _ = sent.keys.swapRemove(i); - } - return true; -} - test "a kind's head is checked before a core takes it, and a core's kinds are its own" { const Fake = struct { fn paint(_: ?*anyopaque, _: [*c]const c.pa_window, _: [*c]c.pa_paint) callconv(.c) void {} @@ -1647,97 +1365,3 @@ const GpuPaint = struct { return 0; } }; - -test "a pane of a kind's frame a row taller or shorter is drawn whole at its peer, never a row it let go" { - const gpa = std.testing.allocator; - var cells: [2 * 3]ShadowCell = @splat(std.mem.zeroes(ShadowCell)); - cells[0] = .{ .text = "a".* ++ @as([7]u8, @splat(0)), .fg = 1, .bg = 2, .attrs = 0, .len = 1, .flags = c.PA_PAINTED }; - var f: Frame = .{ .cells = &cells, .cols = 2, .rows = 2 }; - var sent: ShadowSent = .{}; - defer sent.deinit(gpa); - var blobs: Blobs = .{}; - const sh = try gpa.create(Shadow); - sh.* = .{}; - defer sh.deinit(gpa); - var out: std.Io.Writer.Allocating = .init(gpa); - defer out.deinit(); - for ([_]u16{ 2, 3, 2 }) |rows| { - f.rows = rows; - out.clearRetainingCapacity(); - try std.testing.expect(try encodeShadow(gpa, &sent, &f, 0, &out.writer)); - _ = try sh.apply(gpa, out.written(), 1, &blobs); - try std.testing.expectEqualStrings("a", sh.cells[0].text[0..1]); - } -} - -test "a pane of a kind crosses as its frame, a row the peer holds as its hash, a raster by key after its strips over what it shows" { - const gpa = std.testing.allocator; - var cells: [3 * 2]ShadowCell = @splat(std.mem.zeroes(ShadowCell)); - cells[0] = .{ .text = "a".* ++ @as([7]u8, @splat(0)), .fg = 1, .bg = 2, .attrs = 0, .len = 1, .flags = c.PA_PAINTED }; - // A raster of three strips (4 MiB a strip at 1,024 px), of which it shows the second. - const w: u32 = 1024; - const per = stripRows(w); - const pixels = try gpa.alloc(u8, @as(usize, w) * per * 3 * 4); - defer gpa.free(pixels); - @memset(pixels, 5); - var f: Frame = .{ .cells = &cells, .cols = 3, .rows = 2, .n = 1 }; - f.buf[0] = std.mem.zeroInit(Place, .{ .rgba = pixels.ptr, .w = w, .h = per * 3, .src = .{ .x = 0, .y = per + 1, .w = w, .h = 10 }, .key = 9 }); - f.places = &f.buf; - var sent: ShadowSent = .{}; - defer sent.deinit(gpa); - var blobs: Blobs = .{}; - defer blobs.deinit(gpa); - const sh = try gpa.create(Shadow); - sh.* = .{}; - defer sh.deinit(gpa); - var out: std.Io.Writer.Allocating = .init(gpa); - defer out.deinit(); - const name = std.hash.Wyhash.hash(1, std.mem.asBytes(&f.buf[0].key)); - try std.testing.expectEqual(@as(u64, 0b010), try sent.owed(gpa, &f.buf[0])); - try encodeStrip(&f.buf[0], 1, &out.writer); - try std.testing.expect(out.written().len <= strip_max + 20); - try blobs.strip(gpa, name, out.written(), &.{}); - out.clearRetainingCapacity(); - try std.testing.expect(try encodeShadow(gpa, &sent, &f, 0, &out.writer)); - try std.testing.expectEqual(@as(u64, 0), try sh.apply(gpa, out.written(), 1, &blobs)); - try std.testing.expectEqualStrings("a", sh.cells[0].text[0..1]); - try std.testing.expectEqual(@as(u8, 5), sh.places.items[0].rgba[per * w * 4]); - // The same frame again: nothing to send, no strip owed. - out.clearRetainingCapacity(); - try std.testing.expect(!try encodeShadow(gpa, &sent, &f, 0, &out.writer)); - try std.testing.expectEqual(@as(u64, 0), try sent.owed(gpa, &f.buf[0])); - // Its src moves into the third strip: that one is owed, and a peer - // without it does not show the raster and asks for it, once. - f.buf[0].src.y = 2 * per + 1; - try std.testing.expectEqual(@as(u64, 0b100), try sent.owed(gpa, &f.buf[0])); - out.clearRetainingCapacity(); - try std.testing.expect(try encodeShadow(gpa, &sent, &f, 0, &out.writer)); - try std.testing.expect(out.written().len < 4 + 1 + 2 * 9 + 60 + 8); - try std.testing.expectEqual(name, try sh.apply(gpa, out.written(), 1, &blobs)); - try std.testing.expectEqual(@as(usize, 0), sh.places.items.len); - try std.testing.expectEqual(@as(u64, 0), try sh.apply(gpa, out.written(), 1, &blobs)); -} - -test "optional GPU frames cross shadows, change independently, and disappear" { - const gpa = std.testing.allocator; - var sent: ShadowSent = .{}; - defer sent.deinit(gpa); - const shadow = try gpa.create(Shadow); - shadow.* = .{}; - defer shadow.deinit(gpa); - var blobs: Blobs = .{}; - defer blobs.deinit(gpa); - var cell = std.mem.zeroes(ShadowCell); - var frame: Frame = .{ .cols = 1, .rows = 1, .cells = @as([*]ShadowCell, @ptrCast(&cell))[0..1], .gpu = "first scene" }; - var out: std.Io.Writer.Allocating = .init(gpa); - defer out.deinit(); - for ([_][]const u8{ "first scene", "second scene", "" }) |bytes| { - frame.gpu = bytes; - out.clearRetainingCapacity(); - try std.testing.expect(try encodeShadow(gpa, &sent, &frame, 0, &out.writer)); - _ = try shadow.apply(gpa, out.written(), 1, &blobs); - try std.testing.expectEqualStrings(bytes, shadow.gpu); - out.clearRetainingCapacity(); - try std.testing.expect(!try encodeShadow(gpa, &sent, &frame, 0, &out.writer)); - } -} diff --git a/src/draw.zig b/src/draw.zig index 10556ac9..187f2f4e 100644 --- a/src/draw.zig +++ b/src/draw.zig @@ -263,7 +263,7 @@ pub fn render(p: *Pardes, arena: std.mem.Allocator) !*Surface { // Over a kind's pane, the pointer its last frame asked for (a PDF's // hand over a link; 2.2). if (v.kind_hover) |id| if (p.panes[id]) |pane| if (pane.addon) |k| { - if ((if (k.shadow) |sh| sh.pointer else k.pointer) == pardes.Kind.c.PA_POINTER_LINK) s.pointer_shape = .link; + if (k.pointer == pardes.Kind.c.PA_POINTER_LINK) s.pointer_shape = .link; }; for (&s.body_layers) |*layer| layer.rows = 0; for (&s.tag_layers) |*layer| layer.rows = 0; diff --git a/src/dump.zig b/src/dump.zig index 102b10d0..7f45ecc8 100644 --- a/src/dump.zig +++ b/src/dump.zig @@ -768,7 +768,8 @@ const SlotRecord = struct { values: []const u8 = "", live: bool = false, /// What decides its next calls (Kind.step): a replica joining keeps - /// them, its Shadow calling where the authority's kind does. + /// them, its pane of a kind with no code there calling where the + /// authority's kind does. next_tick: u64 = 0, views: [views.max]u64 = @splat(0), focused: bool = false, @@ -832,7 +833,8 @@ pub fn slotRecord(s: *Kind.Slot) !?[]u8 { } /// The pane back from its record, a placeholder holding it until its -/// kind opens it (`revivePane`; a replica's core keeps it a Shadow). +/// kind opens it (`revivePane`; a replica's core with no code for it keeps +/// it so). pub fn slotRead(out: *Kind.Slot, gpa: std.mem.Allocator, bytes: []const u8) !void { var arena: std.heap.ArenaAllocator = .init(gpa); defer arena.deinit(); @@ -880,9 +882,10 @@ pub fn reviveSlot(p: *Pardes, id: usize, s: *Kind.Slot) void { } else { s.version = p.kinds.kinds[k].version; if (s.ops == null) { - // A replica's Shadow is the authority's pane as it is: it ticks - // when that one does, not because it was loaded (a kind opened - // here ticks to lay itself out). + // A replica's pane of a kind with no code there is the + // authority's as it is: it ticks when that one does, not + // because it was loaded (a kind opened here ticks to lay + // itself out). s.next_tick, s.views, s.focused = kept; } } diff --git a/src/replica.zig b/src/replica.zig index 3af1caac..16c6b15d 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -342,15 +342,10 @@ pub const Authority = struct { // 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: // to a replica: kind 1, a delta; kind 2, rows (encodeRows): asked for, -// or a step read them; kind 4, a pane of a kind's frame -// (a Shadow, screen_state.encodeShadow), at the size the -// peer's view gives the pane; kind 5, a strip of one of -// its rasters (encodeStrip), before the frame naming it; +// or a step read them; // to the authority: kind 1, an acknowledgement (u32 seq); kind 2, a // request for rows (u64 lo, u64 hi, absolute); kind 3, a -// delta refused: start this terminal afresh; kind 4, a -// kind's frame naming a raster the replica lacks: all -// of that pane again. +// delta refused: start this terminal afresh. // In the authority's stream to a peer, before it steps any input but a // pty's output, and once a frame: its own steps since the last unit, as one // (`Authority.pending`), then what the peer is owed of the terminals @@ -361,11 +356,8 @@ pub const Authority = struct { const kind_delta = 1; const kind_rows = 2; -const kind_shadow = 4; -const kind_raster = 5; const kind_ack = 1; const kind_reset = 3; -const kind_want = 4; const ScreenMsg = struct { pane: u8, serial: u32, kind: u8, payload: []const u8 }; @@ -396,7 +388,7 @@ pub const Screens = struct { encode_ns: u64 = 0, const Tracked = struct { serial: u32, state: *Term.State, track: *Term.Track }; - const PeerOf = struct { serial: u32 = 0, peer: Term.Peer = .{}, shadow: pardes.Kind.ShadowSent = .{} }; + const PeerOf = struct { serial: u32 = 0, peer: Term.Peer = .{} }; pub fn init(gpa: Allocator) Screens { return .{ .gpa = gpa }; @@ -406,10 +398,7 @@ pub const Screens = struct { pub fn deinit(s: *Screens, core: *Pardes) void { 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); - pp.shadow.deinit(s.gpa); - } + for (p) |*pp| pp.peer.deinit(s.gpa); s.gpa.destroy(p); } s.peers.deinit(s.gpa); @@ -436,20 +425,16 @@ pub const Screens = struct { /// Peer `peer` gone: what it was sent forgotten, its number free for /// the next `addPeer` (a socket's clients come and go). pub fn dropPeer(s: *Screens, peer: u16) void { - for (s.peers.items[peer]) |*pp| { - pp.peer.deinit(s.gpa); - pp.shadow.deinit(s.gpa); - } + for (s.peers.items[peer]) |*pp| pp.peer.deinit(s.gpa); s.peers.items[peer].* = @splat(.{}); s.free_peers.append(s.gpa, peer) catch {}; } /// What `peer` is owed of every terminal, each a message onto `out` /// (owned): nothing for one it has as it is, or with `bound` in flight. - pub fn flush(s: *Screens, core: *Pardes, peer: u16, view: u8, out: *std.ArrayListUnmanaged([]u8)) !void { + pub fn flush(s: *Screens, core: *Pardes, peer: u16, _: u8, out: *std.ArrayListUnmanaged([]u8)) !void { const t0 = nowNs(); defer s.encode_ns += nowNs() - t0; - try s.shadows(core, peer, view, out); for (core.panes, 0..) |slot, id| { const pane = slot orelse continue; const state = pane.terminal orelse continue; @@ -468,57 +453,6 @@ pub const Screens = struct { } } - /// What `peer` is owed of each pane of a kind this core runs: its - /// frame as the peer's view shows it, when it is not what it was sent. - fn shadows(s: *Screens, core: *Pardes, peer: u16, view: u8, out: *std.ArrayListUnmanaged([]u8)) !void { - const any = for (core.panes) |slot| { - if (slot) |pane| if (pane.addon) |k| if (k.ops != null) break true; - } else false; - if (!any) return; - const Ctx = struct { s: *Screens, peer: u16, out: *std.ArrayListUnmanaged([]u8) }; - try pardes.views.within(core, view, Ctx{ .s = s, .peer = peer, .out = out }, struct { - fn each(x: Ctx, p: *Pardes) anyerror!void { - var arena: std.heap.ArenaAllocator = .init(x.s.gpa); - defer arena.deinit(); - // A plain kind's is drawn where it is shown (Kind.runsHere). - for (p.panes, 0..) |slot, id| if (slot) |pane| if (pane.addon) |k| if (k.ops != null and k.plain == null and !pane.collapsed) { - const pp = &x.s.peers.items[x.peer][id]; - if (pp.shadow.serial != pane.serial) { - pp.shadow.deinit(x.s.gpa); - pp.shadow.serial = pane.serial; - } - const r = p.view().rects[id]; - const cols = r.w -| pardes.config.GUTTER; - const rows = (r.h -| pane.tag_rows) -| pane.notices.span(); - if (cols == 0 or rows == 0) continue; - var frame: pardes.Kind.Frame = .{ .cols = cols, .rows = rows }; - frame.cells = pardes.Kind.paint(p, k, id, arena.allocator(), cols, rows, &frame) orelse continue; - // Its rasters' strips over what it shows that the peer - // 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 > 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 pardes.Kind.encodeStrip(pl, @ctz(owed), &w.writer); - try x.out.append(x.s.gpa, try x.s.gpa.dupe(u8, w.written())); - } - } - x.s.out.clearRetainingCapacity(); - 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 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())); - }; - } - }.each); - } - /// Rows a peer holds as holes of every terminal a step read (its rows, /// history and all: text mode over a shell), each a rows message onto /// `out` (owned): sent after the step and before its unit, so the @@ -583,8 +517,6 @@ pub const Screens = struct { const m = parseScreen(msg) orelse return null; const pane = core.panes[m.pane] orelse return null; if (pane.serial != m.serial) return null; - // A kind's frame whose raster the peer lacks: its pane sent afresh. - if (m.kind == kind_want and pane.addon != null) s.peers.items[peer][m.pane].shadow.deinit(s.gpa); const state = pane.terminal orelse return null; const pp = &s.peers.items[peer][m.pane]; if (pp.serial != m.serial) return null; @@ -746,8 +678,6 @@ pub const Replica = struct { serials: [pardes.MAX_PANES]u32 = @splat(0), retiring: bool = false, screen_out: std.ArrayListUnmanaged(u8) = .empty, - /// Its Shadows' rasters, from the session's strips (17.3). - blobs: pardes.Kind.Blobs = .{}, /// A record's bytes by hash coming in pieces (`blobPiece`), and how /// many of them came. incoming: ?tape.Big = null, @@ -822,7 +752,7 @@ pub const Replica = struct { pardes.colors.finishThemeInitialization(c); // It runs a plain kind's code of its own (Addon.plainOps), loaded // at its first pane, each opened here from its record; another's - // panes are Shadows. + // panes show a line. c.kinds.ops = @splat(null); for (c.panes, 0..) |pslot, at| if (pslot) |pane| if (pane.addon) |s| if (c.kinds.byName(s.nameOf())) |k| { if (c.kinds.ops[k] == null) c.kinds.ops[k] = pardes.Addon.plainOps(&c.kinds.kinds[k]); @@ -856,7 +786,6 @@ pub const Replica = struct { c.deinit(); was.end(); } - r.blobs.deinit(r.gpa); if (r.incoming) |b| r.gpa.free(b.bytes); r.copy.deinit(); for (r.pending.items) |p| r.freePending(p); @@ -1287,28 +1216,6 @@ pub const Replica = struct { const c = r.t.core orelse return null; const pane = c.panes[m.pane] orelse return null; if (pane.serial != m.serial) return null; // a pane gone: its slot's next is another - // A strip of a raster of a pane of a kind, kept until a frame shows it. - if (m.kind == kind_raster and m.payload.len >= 8) { - var shown: std.ArrayList([*]const u8) = .empty; - defer shown.deinit(r.gpa); - for (c.panes) |slot| if (slot) |p| if (p.addon) |k| if (k.shadow) |sh| for (sh.places.items) |pl| shown.append(r.gpa, pl.rgba) catch {}; - r.blobs.strip(r.gpa, std.hash.Wyhash.hash(m.serial, m.payload[0..8]), m.payload, shown.items) catch r.fail("a kind's raster did not apply"); - return null; - } - // 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(pardes.Kind.Shadow) catch return null; - made.* = .{}; - k.shadow = made; - break :sh made; - }; - const lacking = sh.apply(k.gpa, m.payload, m.serial, &r.blobs) catch { - r.fail("a kind's frame did not apply"); - return null; - }; - return if (lacking != 0) r.screenMessage(m.pane, m.serial, kind_want, &.{}) else null; - }; const state = pane.terminal orelse return null; const slot = &r.screens[m.pane]; if (slot.* == null or slot.*.?.serial != m.serial) { @@ -2178,11 +2085,7 @@ pub const Peers = struct { }, .screen => |in| { defer ps.gpa.free(in.msg); - const reply = (ps.screens.take(core, in.peer.sid, in.msg) catch null) orelse { - // A raster asked for again: its pane's frame now. - if (in.msg.len > 5 and in.msg[5] == kind_want) ps.screensTo(core, in.peer, .flush); - continue; - }; + const reply = (ps.screens.take(core, in.peer.sid, in.msg) catch null) orelse continue; defer ps.gpa.free(reply); const head = wire.header(@backingInt(wire.ServerTag.screen), reply.len); in.peer.send(&.{ &head, reply }); @@ -3310,8 +3213,8 @@ const PtyHost = struct { } }; -/// A kind as a library brings one, for the Shadow's test: a counter its -/// keys move, painted as `count N`. +/// A kind as a library brings one, its state not plain (no PA_PLAIN): a +/// counter its keys move, painted as `count N`. const Counter = struct { const pa = pardes.Kind.c; fn open(_: [*c]const u8, _: [*c]const u8, _: u64) callconv(.c) ?*anyopaque { @@ -4694,13 +4597,10 @@ test "a step that reads a terminal's rows as the core keeps them sends a new cli for ([_]*Replica{ r1, r2 }) |r| try agrees(a, r); } -test "a replica with no code for a kind draws its pane from the authority's frames, and its keys go to the authority" { +test "a replica with no code for a kind whose state is not plain shows its pane as a line saying so, and its keys go to the authority" { const gpa = std.testing.allocator; const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); defer a.deinit(); - var screens: Screens = .init(gpa); - defer screens.deinit(a.core); - const peer = try screens.addPeer(); const r = try Replica.init(gpa, 1, try a.unit(0, 0), false); defer r.deinit(); // What the host read off the library, as the `addon` event: the @@ -4710,8 +4610,7 @@ test "a replica with no code for a kind draws its pane from the authority's fram r.confirm(try a.local(.{ .addon = .{ .op = .load, .pane = 0, .kind = &counter } })); r.confirm(try a.local(.{ .command = "Counter" })); try std.testing.expect(r.core().kinds.ops[r.core().kinds.n - 1] == null); - try deliver(a, &screens, peer, r, .flush); - try std.testing.expect(try shows(r.core(), "count 0")); + try std.testing.expect(try shows(r.core(), "addon counter 1 does not run in this client")); // A key into it, typed at the replica: the authority's kind takes it. var sent: std.ArrayListUnmanaged([]u8) = .empty; defer { @@ -4723,9 +4622,8 @@ test "a replica with no code for a kind draws its pane from the authority's fram r.confirm(try a.take(m)); r.settle(); } - try deliver(a, &screens, peer, r, .flush); try std.testing.expect(try shows(a.core, "count 1")); - try std.testing.expect(try shows(r.core(), "count 1")); + try std.testing.expect(!try shows(r.core(), "count 1")); if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); try std.testing.expect(r.diverged == null); try std.testing.expectEqual(digestOf(a), r.digest()); diff --git a/test/daemon.py b/test/daemon.py index 051297a8..5bdf1fc4 100644 --- a/test/daemon.py +++ b/test/daemon.py @@ -649,9 +649,9 @@ def pdf_window(binary, gui_binary): """A PDF in a terminal's detached session (the pdf addon its prefix installs, `<prefix>/lib/pardes/addons/pdf.so`), a window client attached and its wheel turned over the PDF: the session's page moves, the - window's digest is the session's, and the window, which runs no addon - code, draws the session's frames of it. Skipped when the build has no - pdf.so.""" + window's digest is the session's, and the window, which runs the + addon's code itself (its state is plain: PA_PLAIN), draws its own + pages from the file. Skipped when the build has no pdf.so.""" lib = Path(binary).resolve().parent.parent / 'lib' / 'pardes' / 'addons' / 'pdf.so' if not lib.exists(): print(f'daemon: no {lib}: the PDF window case skipped') diff --git a/test/panes.zig b/test/panes.zig index fb8adc36..de9d3aea 100644 --- a/test/panes.zig +++ b/test/panes.zig @@ -3937,7 +3937,7 @@ const PdfAddon = struct { else try std.testing.expectEqualDeep(@field(s, name), @field(t, name)); } - // Its place: what its kind says, and the Shadow's copy of it. + // Its place: what its kind says, and the loaded copy of it. var here: [32]u8 = undefined; var there: [32]u8 = undefined; try std.testing.expectEqualStrings(pardes.Kind.where(s, &here), pardes.Kind.where(t, &there)); @@ -4178,6 +4178,152 @@ const PdfAddon = struct { try std.testing.expect(notch > 0 and first > at[0] and first < at[1]); try std.testing.expectApproxEqRel(notch / 4, at[2] - at[1], 0.05); } + + test "a window client the size of its session's window shows a PDF's pages where the session's window shows them, a fraction of a pixel scrolled and all" { + if (!addons.has_pdf) return error.SkipZigTest; + const R = pardes.replica; + const gpa = std.testing.allocator; + const a = try R.Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, .{ .vtable = &host_vtable }, false); + defer a.deinit(); + const win = try R.Replica.init(gpa, 2, try a.unit(0, 0), false); + defer win.deinit(); + const land = struct { + a: *R.Authority, + r: *R.Replica, + fn step(l: @This(), ev: pardes.Event) !void { + l.r.confirm(try l.a.local(ev)); + } + }{ .a = a, .r = win }; + // The session's window, and the client's of its size on a view of its own. + const window: pardes.Event = .{ .frontend = .{ .windowed = true, .native_images = true } }; + try land.step(window); + // The client has the addon (a test's replica finds no library of + // its own: it is handed the session's, loaded in this process). + var words: [64]pardes.Kind.LoadedWord = undefined; + var why: pardes.Kind.Buf(160) = .{}; + const lib = pardes.Addon.readLoaded(pdf_lib, &words, &why); + try land.step(.{ .addon = .{ .op = .load, .pane = 0, .kind = &lib } }); + const kind = a.core.kinds.n - 1; + win.core().kinds.ops[kind] = a.core.kinds.ops[kind]; + const id = try opened(a, land); + const k = try a.openView(2); + win.confirm(k.unit); + win.view = k.view; + win.confirm(try a.take(try win.input(window))); + try land.step(.tick); + // A touchpad's 0.6 of a notch in the client: its view's top a + // fraction of a pixel past a row (as a glide's half steps leave it). + const r = win.core().view().rects[id]; + win.confirm(try a.take(try win.input(.{ .mouse = .{ .button = .wheel_down, .kind = .press, .col = r.x + 4, .row = r.y + r.h - 2, .amount = 0.6 } }))); + for (0..2) |_| { + try land.step(.tick); + try jobs(a.core, land); + } + win.settle(); + try std.testing.expect(win.diverged == null); + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + var at: [2][8][4]u32 = @splat(@splat(@splat(0))); + for ([_]*Pardes{ a.core, win.core() }, [_]u8{ 0, k.view }, &at) |core, view, *rows| { + core.toView(view); + var n: usize = 0; + for ((try core.render(arena.allocator())).placements.items) |pl| if (pl.pane == id and pl.raster.w > 1 and n < rows.len) { + rows[n] = if (pl.at) |x| .{ x.y, x.h, pl.src.y, pl.src.h } else .{ 0, 0, pl.src.y, pl.src.h }; + n += 1; + }; + try std.testing.expect(n > 0); + } + try std.testing.expectEqualSlices([4]u32, &at[0], &at[1]); + } + + /// A session with design.pdf open and a client on a view of its own + /// (`frontend`'s), which runs the kind as the session does: handed the + /// session's library (a test's replica finds none of its own). + fn clientOf(a: *pardes.replica.Authority, win: *pardes.replica.Replica, frontend: pardes.Event) !struct { id: usize, view: u8 } { + const land = Confirmed{ .a = a, .r = win }; + try land.step(.{ .frontend = .{ .windowed = true, .native_images = true } }); + var words: [64]pardes.Kind.LoadedWord = undefined; + var why: pardes.Kind.Buf(160) = .{}; + const lib = pardes.Addon.readLoaded(pdf_lib, &words, &why); + try land.step(.{ .addon = .{ .op = .load, .pane = 0, .kind = &lib } }); + const kind = a.core.kinds.n - 1; + win.core().kinds.ops[kind] = a.core.kinds.ops[kind]; + const id = try opened(a, land); + const k = try a.openView(2); + win.confirm(k.unit); + win.view = k.view; + win.confirm(try a.take(try win.input(frontend))); + try land.step(.tick); + try jobs(a.core, land); + return .{ .id = id, .view = k.view }; + } + + const Confirmed = struct { + a: *pardes.replica.Authority, + r: *pardes.replica.Replica, + fn step(l: @This(), ev: pardes.Event) !void { + l.r.confirm(try l.a.local(ev)); + } + }; + + test "a terminal's client shows a PDF's page as its text, the session's page's lines from its first" { + if (!addons.has_pdf) return error.SkipZigTest; + const R = pardes.replica; + const gpa = std.testing.allocator; + const a = try R.Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, .{ .vtable = &host_vtable }, false); + defer a.deinit(); + const win = try R.Replica.init(gpa, 2, try a.unit(0, 0), false); + defer win.deinit(); + const c = try clientOf(a, win, .{ .frontend = .{ .windowed = false, .native_images = false } }); + try (Confirmed{ .a = a, .r = win }).step(.tick); + try jobs(a.core, Confirmed{ .a = a, .r = win }); + // The page's text (its body) as the session reads it: its first line + // is the client pane's first row. + const text = try pardes.Kind.body(a.core.panes[c.id].?, gpa); + defer gpa.free(text); + const first = std.mem.trim(u8, text[0 .. std.mem.indexOfScalar(u8, text, '\n') orelse text.len], " "); + try std.testing.expect(first.len > 0); + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + win.core().toView(c.view); + const s = try win.core().render(arena.allocator()); + const r = win.core().view().rects[c.id]; + var row: [512]u8 = undefined; + var n: usize = 0; + for (r.x..r.x + r.w) |x| { + const g = s.at(@intCast(x), r.y + win.core().panes[c.id].?.tag_rows).grapheme(); + if (n + g.len > row.len) break; + @memcpy(row[n..][0..g.len], g); + n += g.len; + } + try std.testing.expect(std.mem.indexOf(u8, row[0..n], first[0..@min(first.len, 20)]) != null); + var placed: usize = 0; + for (s.placements.items) |pl| placed += @intFromBool(pl.pane == c.id); + try std.testing.expectEqual(@as(usize, 0), placed); + } + + test "a PDF pane a client closes waits for the session's order (its kind's close is never guessed), then is gone in both" { + if (!addons.has_pdf) return error.SkipZigTest; + const R = pardes.replica; + const gpa = std.testing.allocator; + const a = try R.Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, .{ .vtable = &host_vtable }, false); + defer a.deinit(); + const win = try R.Replica.init(gpa, 2, try a.unit(0, 0), false); + defer win.deinit(); + const c = try clientOf(a, win, .{ .frontend = .{ .windowed = true, .native_images = true } }); + // The client's click in the PDF: its pane the client's own view's. + const r = win.core().view().rects[c.id]; + for ([_]pardes.Mouse.Kind{ .press, .release }) |kind| win.confirm(try a.take(try win.input(.{ .mouse = .{ .button = .left, .kind = kind, .col = r.x + 4, .row = r.y + r.h - 2 } }))); + win.settle(); + const stopped = win.stats.stopped; + const sent = try win.input(.{ .command = "Del" }); + try std.testing.expect(win.core().panes[c.id] != null); + try std.testing.expectEqual(stopped + 1, win.stats.stopped); + win.confirm(try a.take(sent)); + win.settle(); + try std.testing.expect(a.core.panes[c.id] == null and win.core().panes[c.id] == null); + try std.testing.expect(win.diverged == null); + } }; test { diff --git a/test/pdf_scroll_bench.zig b/test/pdf_scroll_bench.zig index bb4640f7..595037ae 100644 --- a/test/pdf_scroll_bench.zig +++ b/test/pdf_scroll_bench.zig @@ -277,9 +277,15 @@ fn measure(config: Config, plan: Plan) !Result { var visual: u64 = 0x7061_7264_6573_5343; var shown: u64 = 0; var total: u64 = 0; + // The core's clock a frame on at each, as a shell's loop moves it + // (its glide and a fling are on that clock). + var now: u64 = std.time.ns_per_s; + core.advance(now); for (0..plan.frames) |frame| { const button: pardes.Mouse.Button = if (plan.scenario == .fling_reverse and frame >= plan.down_frames) .wheel_up else .wheel_down; const started = nowNs(); + now += pardes.frame_ns; + core.advance(now); for (0..ticks) |_| core.update(.{ .mouse = .{ .button = button, .kind = .press, .col = rect.x + rect.w / 2, .row = rect.y + rect.h / 2, .amount = config.turn } }); _ = arena.reset(.retain_capacity); const surface = try core.render(arena.allocator()); diff --git a/test/replicas.zig b/test/replicas.zig index 8d4b2e38..aea6ef7f 100644 --- a/test/replicas.zig +++ b/test/replicas.zig @@ -1239,7 +1239,7 @@ fn push(q: *std.PriorityQueue(Msg, void, Msg.order), nn: *u64, m: Msg) !void { fn flushAll(sc: *replica.Screens, core: *pardes.Pardes, ps: []const u16, list: *std.ArrayListUnmanaged([]u8), q: *std.PriorityQueue(Msg, void, Msg.order), nn: *u64, now: u64, dl: []Link, rnd: std.Random, ow: u64, jt: u64, vs: *std.AutoHashMapUnmanaged(u64, u64), tot: *Totals) !void { for (ps, 0..) |peer, c| { list.clearRetainingCapacity(); - // No pane of a kind here: the view a Shadow is painted for is moot. + // Terminals alone cross: the view is not read. try sc.flush(core, peer, 0, list); for (list.items) |bytes| { tot.deltas += 1; |
