diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-31 06:28:48 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-31 06:28:48 -0300 |
| commit | a479792a1ab2b593642d7f648194bfa23ce01dba (patch) | |
| tree | 96ddee7d879a9084daa07db43b0b7910b072a785 /src/Handover.zig | |
| parent | e179cc44fe5060c76a5b00c41e53f11209f5acd9 (diff) | |
| parent | 4c761893e5b3fdae422622cfa02104ab36633045 (diff) | |
| download | pardes-a479792a1ab2b593642d7f648194bfa23ce01dba.tar.gz pardes-a479792a1ab2b593642d7f648194bfa23ce01dba.zip | |
Merge the upstream daemon and replicated client/server refactor
Diffstat (limited to 'src/Handover.zig')
| -rw-r--r-- | src/Handover.zig | 112 |
1 files changed, 78 insertions, 34 deletions
diff --git a/src/Handover.zig b/src/Handover.zig index 6124e49f..549e4d58 100644 --- a/src/Handover.zig +++ b/src/Handover.zig @@ -1,7 +1,8 @@ //! Panes between windows, as a browser's tabs go between its windows. A //! pane's grip, or a column's, released past the window's edge carries it -//! out: dumped alone (dump.dumpBytes, `carried`), it opens in a new window -//! of its own, `pardes-gui -l` of that dump. The pane closes here only once +//! out: dumped alone (`Carried`, each pane as dump.savePane makes it), it +//! opens in a new window of its own, `pardes-gui -l` of that dump. The pane +//! closes here only once //! the new window answers on its 9P socket, so its text is never in no //! window at all; a failed handover leaves it where it was, and a pane //! edited while it was on its way stays here too (both then have it). @@ -25,15 +26,33 @@ const dump = @import("dump.zig"); const ninep_io = @import("9p_io.zig"); const panes = @import("panes.zig"); const tagline = @import("tagline.zig"); +const walk = @import("walk.zig"); const Pardes = pardes.Pardes; const MAX_PANES = pardes.MAX_PANES; const libc = std.c; +/// What a grip carries out of its window: one pane with the panes attached +/// to it, or a whole column. +pub const Kind = enum { pane, column }; + +/// A handover's dump: `carried_magic`, then this. +const Carried = struct { + kind: Kind, + /// A carried column's tag. + tag: ?[]const u8 = null, + /// Each pane as dump.savePane makes it, top to bottom. + panes: []const []const u8, + weights: []const f32, + /// Which of them each one is attached to (layout.zig, "Attached panes"). + owners: []const ?u32, +}; +pub const carried_magic = "pardes-carried\n"; + /// The handover in flight, one at a time: its number, what it carries, /// and each carried pane's serial and revision as it was dumped. seq: u32 = 0, flying: bool = false, -carried: dump.Carried = .pane, +carried: Kind = .pane, column_serial: u32 = 0, count: usize = 0, serials: [MAX_PANES]u32 = @splat(0), @@ -44,6 +63,10 @@ bytes: ?[]u8 = null, /// its answer comes in (`finish`), cancelled at the session's end. task: ?struct { future: std.Io.Future(void), io: std.Io } = null, +/// The dump in flight and its carrier are the host's errand, not the +/// session's (src/walk.zig). +pub const replica = .{ .authority = .{ "bytes", "task" } }; + pub fn deinit(h: *Handover, gpa: std.mem.Allocator) void { // The session ends with the carrier still out: it stops at its next // wait (a 9P call it is in finishes first, within its own budget). @@ -62,8 +85,7 @@ pub fn deinit(h: *Handover, gpa: std.mem.Allocator) void { /// an attachment), or column `at`, released past the window's edge: dumped, /// and the host asked to carry it. Only text panes travel: a terminal's /// shell is this process's child and would die with the pane here. -pub fn begin(h: *Handover, p: *Pardes, carried: dump.Carried, at: usize) void { - var keep: [MAX_PANES]bool = @splat(false); +pub fn begin(h: *Handover, p: *Pardes, carried: Kind, at: usize) void { var ids: [MAX_PANES]usize = undefined; var n: usize = 0; switch (carried) { @@ -92,8 +114,7 @@ pub fn begin(h: *Handover, p: *Pardes, carried: dump.Carried, at: usize) void { else "only text panes go to another window, for now"); } - for (ids[0..n]) |id| keep[id] = true; - const bytes = dump.dumpBytes(p, &keep, carried) catch |err| return p.reportError(said, "handover", err); + const bytes = dumpCarried(p, carried, at, ids[0..n]) catch |err| return p.reportError(said, "handover", err); h.deinit(p.gpa); h.bytes = bytes; h.seq +%= 1; @@ -158,6 +179,31 @@ pub fn finish(h: *Handover, p: *Pardes, seq: u32, to: []const u8, failure: ?[]co std.fmt.bufPrint(&buf, "copied to {s}; edited on the way, kept here too", .{to}) catch "copied"); } +/// The dump of panes `ids` (gpa's), carried as `kind` from column `at`'s +/// (a pane's, `at` is its slot). +fn dumpCarried(p: *Pardes, kind: Kind, at: usize, ids: []const usize) ![]u8 { + const arena = p.scratch.allocator(); + const bytes = try arena.alloc([]const u8, ids.len); + const weights = try arena.alloc(f32, ids.len); + const owners = try arena.alloc(?u32, ids.len); + for (ids, 0..) |id, i| { + const pane = p.panes[id].?; + bytes[i] = try dump.savePane(arena, pane, false); + weights[i] = pane.vweight; + const owner = layout.ownerOf(p, id); + owners[i] = if (owner) |o| if (std.mem.indexOfScalar(usize, ids, o)) |k| @intCast(k) else null else null; + } + const value: Carried = .{ + .kind = kind, + .tag = if (kind == .column) p.col_tags[at].own else null, + .panes = bytes, + .weights = weights, + .owners = owners, + }; + const body = try walk.save(Carried, arena, &value); + return std.mem.concat(p.gpa, u8, &.{ carried_magic, body }); +} + // ---- the receiving window ---- /// A carried dump (`begin`'s) taken into this window where the pointer is: @@ -166,13 +212,12 @@ pub fn finish(h: *Handover, p: *Pardes, seq: u32, to: []const u8, failure: ?[]co /// alone. Every other pane stays where it was. Answers the slot of the /// first pane taken; on an error nothing of it is left here. pub fn adopt(p: *Pardes, bytes: []const u8) !usize { - var parsed = try dump.readZon(p.gpa, bytes, "adopt"); - defer parsed.deinit(); - const st = parsed.value; - const carried = st.carried orelse return error.NotCarried; - if (st.columns.len != 1 or st.columns[0].panes.len == 0) return error.NotCarried; - const order = st.columns[0].panes; - for (order) |i| if (i >= st.panes.len or st.panes[i].kind != .file) return error.NotText; + if (!std.mem.startsWith(u8, bytes, carried_magic)) return error.NotCarried; + var arena: std.heap.ArenaAllocator = .init(p.gpa); + defer arena.deinit(); + const st = try walk.load(Carried, arena.allocator(), bytes[carried_magic.len..], null); + const carried = st.kind; + if (st.panes.len == 0 or st.weights.len != st.panes.len or st.owners.len != st.panes.len) return error.NotCarried; // Where the pointer is, else the middle of the pane with the keyboard. layout.compute(p); const here = p.rects[p.active]; @@ -181,19 +226,17 @@ pub fn adopt(p: *Pardes, bytes: []const u8) !usize { var slots: [MAX_PANES]usize = undefined; var n: usize = 0; errdefer for (slots[0..n]) |slot| p.removePane(slot, null) catch {}; - for (order) |i| { + for (st.panes) |one| { const slot = p.freeSlot() orelse return error.TooManyPanes; - const src = st.panes[i]; - const pane = try panes.File.restore(p, slot, src); + const pane = try dump.loadPane(p, slot, one); slots[n] = slot; n += 1; - tagline.restoreDumpTail(p, pane, src); - pane.collapsed = false; + if (pane.file == null) return error.NotText; } // An attachment names its owner by its place in the dump. - for (order, 0..) |i, k| { - const owner = st.panes[i].attached_to orelse continue; - const at = std.mem.indexOfScalar(usize, order, owner) orelse continue; + for (st.owners, 0..) |owner, k| { + const at = owner orelse continue; + if (at >= n) continue; p.panes[slots[k]].?.attached_to = p.panes[slots[at]].?.serial; } const tc = layout.targetColumn(p, x); @@ -201,12 +244,12 @@ pub fn adopt(p: *Pardes, bytes: []const u8) !usize { if (carried == .column or p.ncol == 0) { const c = if (p.ncol == 0) layout.insertColumn(p, 0, false) else layout.insertColumn(p, tc, x < p.col_x[tc] + p.col_w[tc] / 2); const col = c orelse return error.NoRoom; - if (carried == .column) if (st.columns[0].tag) |tag| { + if (carried == .column) if (st.tag) |tag| { p.col_tags[col].own = try p.gpa.dupe(u8, tag); }; for (slots[0..n], 0..) |slot, k| { layout.insert(p, col, k, slot); - p.panes[slot].?.vweight = st.panes[order[k]].vweight; + p.panes[slot].?.vweight = st.weights[k]; } p.active = slots[0]; p.setActiveColumn(col); @@ -240,10 +283,10 @@ pub fn adopt(p: *Pardes, bytes: []const u8) !usize { heights[owner] = keeps; // The carried share what was given as they shared their old rows. var total: f32 = 0; - for (order) |i| total += st.panes[i].vweight; + for (st.weights) |w| total += w; var left = give; for (slots[0..n], 0..) |slot, j| { - const share: u16 = if (j + 1 == n) left else @intFromFloat(@max(1, @floor(@as(f32, @floatFromInt(give)) * st.panes[order[j]].vweight / @max(total, 1)))); + const share: u16 = if (j + 1 == n) left else @intFromFloat(@max(1, @floor(@as(f32, @floatFromInt(give)) * st.weights[j] / @max(total, 1)))); heights[slot] = @min(share, left); left -|= share; } @@ -284,7 +327,7 @@ pub const Job = struct { pub fn dumpPath(gpa: std.mem.Allocator, seq: u32) ![:0]u8 { var dir_buf: [1024]u8 = undefined; const dir: []const u8 = if (libc.getenv("XDG_RUNTIME_DIR")) |x| std.mem.span(x) else dump.defaultDirectory(&dir_buf) orelse return error.NoDirectory; - return std.fmt.allocPrintSentinel(gpa, "{s}/pardes-handover-{d}-{d}.zon", .{ dir, libc.getpid(), seq }, 0); + return std.fmt.allocPrintSentinel(gpa, "{s}/pardes-handover-{d}-{d}.dump", .{ dir, libc.getpid(), seq }, 0); } /// Runs the handover's carrier on `io` (io.concurrent), kept in `h.task` @@ -451,12 +494,13 @@ test "a grip let go past the window's edge dumps its pane alone, and closes it o try std.testing.expect(p.drag == .none); try std.testing.expect(p.panes[0] != null); try std.testing.expectEqual(before, p.rects[other]); - var parsed = try dump.readZon(std.testing.allocator, p.handover.bytes.?, "test"); - defer parsed.deinit(); - try std.testing.expectEqual(@as(?dump.Carried, .pane), parsed.value.carried); - try std.testing.expectEqual(@as(usize, 1), parsed.value.panes.len); - try std.testing.expectEqual(@as(usize, 1), parsed.value.columns.len); - try std.testing.expectEqualStrings("/test.txt", parsed.value.panes[0].file.?.path); + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + const carried = try walk.load(Carried, arena.allocator(), p.handover.bytes.?[carried_magic.len..], null); + try std.testing.expectEqual(Kind.pane, carried.kind); + try std.testing.expectEqual(@as(usize, 1), carried.panes.len); + const pane = try walk.load(pardes.Pane, arena.allocator(), carried.panes[0], null); + try std.testing.expectEqualStrings("/test.txt", pane.file.?.path); // A second one waits for the first. releaseOutside(p, other); try std.testing.expect(p.panes[other] != null); @@ -543,7 +587,7 @@ test "a column let go over another window goes beside the column the pointer is from.update(.{ .mouse = .{ .button = .left, .kind = .drag, .col = from.screen_w - 1, .row = r.y } }); from.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = from.screen_w - 1, .row = r.y, .outside = true } }); try std.testing.expect(from.handover.flying); - try std.testing.expectEqual(dump.Carried.column, from.handover.carried); + try std.testing.expectEqual(Kind.column, from.handover.carried); const to = try Pardes.init(gpa, .{ .tty_only = true, .cols = 120, .rows = 30 }); defer to.deinit(); _ = try to.setTestFile("x\n"); |
