diff options
Diffstat (limited to 'src/Handover.zig')
| -rw-r--r-- | src/Handover.zig | 61 |
1 files changed, 49 insertions, 12 deletions
diff --git a/src/Handover.zig b/src/Handover.zig index d245a6af..2233d228 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 (`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 +//! out: dumped alone (`Carried`, each pane as dump.savePane makes it, in +//! the image of one dump, `handover-<seq>`: Db.zig), 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 @@ -35,7 +36,8 @@ const libc = std.c; /// to it, or a whole column. pub const Kind = enum { pane, column }; -/// A handover's dump: `carried_magic`, then this. +/// What a handover's image holds, as its root `carried` (a build before +/// the image's, 0.24, wrote `carried_magic`, then this as a byte dump). const Carried = struct { kind: Kind, /// A carried column's tag. @@ -114,7 +116,7 @@ pub fn begin(h: *Handover, p: *Pardes, carried: Kind, at: usize) void { else "only text panes go to another window, for now"); } - const bytes = dumpCarried(p, carried, at, ids[0..n]) catch |err| return p.reportError(said, "handover", err); + const bytes = dumpCarried(p, carried, at, ids[0..n], h.seq +% 1) catch |err| return p.reportError(said, "handover", err); h.deinit(p.gpa); h.bytes = bytes; h.seq +%= 1; @@ -180,8 +182,8 @@ pub fn finish(h: *Handover, p: *Pardes, seq: u32, to: []const u8, failure: ?[]co } /// 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 { +/// (a pane's, `at` is its slot): an image of one dump, handover `seq`. +fn dumpCarried(p: *Pardes, kind: Kind, at: usize, ids: []const usize, seq: u32) ![]u8 { const arena = p.scratch.allocator(); const bytes = try arena.alloc([]const u8, ids.len); const weights = try arena.alloc(f32, ids.len); @@ -200,8 +202,44 @@ fn dumpCarried(p: *Pardes, kind: Kind, at: usize, ids: []const usize) ![]u8 { .weights = weights, .owners = owners, }; - const body = try walk.save(Carried, arena, &value); - return std.mem.concat(p.gpa, u8, &.{ carried_magic, body }); + var buf: std.Io.Writer.Allocating = .init(p.gpa); + defer buf.deinit(); + var db = try walk.Mem.init(std.Io.failing, .{ .buffer = &buf, .file_id = 0 }); + var w: walk.Put = .{ .gpa = p.gpa }; + defer w.deinit(); + try db.runContext(struct { + w: *walk.Put, + value: *const Carried, + pub fn run(ctx: @This(), cursor: *walk.Mem.Cursor(.read_write)) !void { + try ctx.w.member(try walk.Mem.HashMap(.read_write).init(cursor.*), "carried", Carried, ctx.value); + } + }{ .w = &w, .value = &value }); + var name: [32]u8 = undefined; + return pardes.Db.image(p.gpa, std.fmt.bufPrint(&name, "handover-{d}", .{seq}) catch "handover", buf.written(), w.blobs.items, ids.len); +} + +/// Whether `bytes` are a handover's dump (`dumpCarried`'s, or an older +/// build's), not a session's. +pub fn carries(bytes: []const u8) bool { + if (std.mem.startsWith(u8, bytes, carried_magic)) return true; + if (!pardes.Db.isImage(bytes)) return false; + const r = pardes.Db.Read.open(std.heap.page_allocator, bytes, null) catch return false; + defer r.close(); + return (walk.Get.member(r.root(), "carried") catch null) != null; +} + +/// The carried panes in `bytes`, made in `a`. +fn readCarried(a: std.mem.Allocator, bytes: []const u8) !Carried { + if (pardes.Db.isImage(bytes)) { + const r = pardes.Db.Read.open(a, bytes, null) catch return error.NotCarried; + defer r.close(); + const at = (walk.Get.member(r.root(), "carried") catch return error.NotCarried) orelse return error.NotCarried; + var l: walk.Get = .init(a); + defer l.deinit(); + return l.get(Carried, at) catch error.NotCarried; + } + if (!std.mem.startsWith(u8, bytes, carried_magic)) return error.NotCarried; + return walk.load(Carried, a, bytes[carried_magic.len..], null); } // ---- the receiving window ---- @@ -212,10 +250,9 @@ fn dumpCarried(p: *Pardes, kind: Kind, at: usize, ids: []const usize) ![]u8 { /// 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 { - 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 st = try readCarried(arena.allocator(), bytes); 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. @@ -327,7 +364,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}.dump", .{ dir, libc.getpid(), seq }, 0); + return std.fmt.allocPrintSentinel(gpa, "{s}/pardes-handover-{d}-{d}.xitdb", .{ dir, libc.getpid(), seq }, 0); } /// Runs the handover's carrier on `io` (io.concurrent), kept in `h.task` @@ -494,7 +531,7 @@ test "a grip let go past the window's edge dumps its pane alone, and closes it o try std.testing.expectEqual(before, p.rects[other]); 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); + const carried = try readCarried(arena.allocator(), p.handover.bytes.?); 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); |
