diff options
Diffstat (limited to 'src/Kind.zig')
| -rw-r--r-- | src/Kind.zig | 253 |
1 files changed, 224 insertions, 29 deletions
diff --git a/src/Kind.zig b/src/Kind.zig index 60d86778..c1640b57 100644 --- a/src/Kind.zig +++ b/src/Kind.zig @@ -38,9 +38,9 @@ pub const abi: u32 = c.PA_ABI; comptime { if (@sizeOf(usize) == 8) for (.{ .{ c.pa_cell, 20 }, .{ c.pa_window, 24 }, .{ c.pa_rect, 16 }, .{ c.pa_cells, 8 }, - .{ c.pa_placement, 80 }, .{ c.pa_paint, 120 }, .{ c.pa_key, 8 }, .{ c.pa_mouse, 10 }, - .{ c.pa_api, 64 }, .{ c.pa_out, 16 }, .{ c.pa_word, 32 }, .{ c.pa_head, 56 }, - .{ c.pa_ops, 192 }, + .{ c.pa_placement, 80 }, .{ c.pa_paint, 128 }, .{ c.pa_key, 8 }, .{ c.pa_mouse, 10 }, + .{ c.pa_api, 96 }, .{ c.pa_out, 16 }, .{ c.pa_word, 32 }, .{ c.pa_head, 56 }, + .{ c.pa_ops, 200 }, .{ c.pa_view, 32 }, }) |r| if (@sizeOf(r[0]) != r[1]) @compileError(std.fmt.comptimePrint("{s} is {d} bytes, the header says {d}", .{ @typeName(r[0]), @sizeOf(r[0]), r[1] })); } @@ -49,14 +49,18 @@ comptime { pub const max_kinds = 16; /// Which of its functions a kind has: what both cores decide by. -pub const Caps = packed struct(u8) { +pub const Caps = packed struct(u16) { tick: bool = false, key: bool = false, mouse: bool = false, ctl: bool = false, jobs: bool = false, load: bool = false, - pad: u2 = 0, + search: bool = false, + /// Esc comes to its `key` first (2.1). + esc: bool = false, + address: bool = false, + pad: u7 = 0, }; pub const Entry = struct { @@ -134,6 +138,9 @@ pub const Slot = struct { arena: std.heap.ArenaAllocator, name: [16]u8 = @splat(0), version: u32 = 0, + /// The hash of the file's bytes it was opened from, 0 for none (a + /// record's): whether the file a watch arms on is still those. + opened: u64 = 0, /// Its kind's generation when it was opened (Entry.gen). gen: u32 = 0, path: []u8 = &.{}, @@ -154,9 +161,25 @@ pub const Slot = struct { /// Its words' last values, a `Word arg` line each, which a kind with /// no `dump` is given again when its pane comes back. values: Buf(256) = .{}, - /// Its last frame's prefix, and whether it placed a picture. + /// Its last frame's prefix, and whether it placed a picture; where its + /// window is in its whole (the rail's thumb), 0 and 0 for none. prefix: Buf(96) = .{}, placed: bool = false, + /// Its tag's prefix as the last step read it (the authority's): what a + /// Shadow says in a step, and what a dump keeps of it, so a replica + /// joining lays its tag out as the session did. + told: Buf(400) = .{}, + thumb_at: f32 = 0, + thumb_span: f32 = 0, + /// What its view was (`api.view`) when `tick` was last told: a change + /// ticks it again (2.1). + view: u64 = 0, + /// What it has selected (`api.select`): a yank's, a chord's, a + /// keyboard Look's. + selection: []u8 = &.{}, + /// Its tag's default words: Term, its kind's words for its panes, + /// Collapse Del. + tail: Buf(160) = .{}, /// Its last frame changed its tag: a step is owed to lay the tag out /// again (the tag is laid out before the body paints). retag: bool = false, @@ -188,6 +211,7 @@ pub const Slot = struct { pub fn free(s: *Slot) void { for (&s.jobs) |*j| if (j.*) |job| s.gpa.free(job.in); + s.gpa.free(s.selection); if (checked) s.used.deinit(s.gpa); if (s.shadow) |sh| sh.deinit(s.gpa); s.arena.deinit(); @@ -240,6 +264,14 @@ pub fn openFile(p: *Pardes, id: usize, k: u8, path: []const u8) !*Pane { pub fn reopen(p: ?*Pardes, id: usize, s: *Slot, bytes: ?[]const u8) bool { var cl: Call = .{ .p = p, .id = id, .slot = s }; const prev = cl.begin("open"); + if (p) |core| if (core.kinds.byName(s.nameOf())) |k| { + var w: std.Io.Writer = .fixed(&s.tail.bytes); + w.writeAll(" Term") catch {}; + for (core.kinds.words[0..core.kinds.nwords]) |*kw| if (kw.kind == k and kw.opens == 0) w.print(" {s}", .{kw.name.slice()}) catch {}; + w.writeAll(" Collapse Del") catch {}; + s.tail.len = @intCast(w.buffered().len); + }; + s.opened = if (bytes) |b| std.hash.Wyhash.hash(0, b) else 0; if (s.ops) |ops| { const path_z = s.arena.allocator().dupeSentinel(u8, s.path, 0) catch ""; cl.opening = true; @@ -275,6 +307,36 @@ fn poke(p: *const Pardes, s: *Slot) void { if (caps(p, s).tick) s.next_tick = 1; } +/// 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. +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; + if (!s.live or s.path.len == 0) return false; + // Both cores read it and reopen it alike (the read and its open's + // outcome on the tape); the kind's record only the authority has. + const bytes: ?[]u8 = @import("fs.zig").read(p, s.path) catch null; + defer if (bytes) |b| p.gpa.free(b); + const record = @import("dump.zig").slotRecord(s) catch null; + var place: [32]u8 = undefined; + s.where_text.set(where(s, &place)); + if (s.ops) |o| if (o.close) |f| { + var cl: Call = .{ .p = null, .id = 0, .slot = s, .quiet = true }; + const prev = cl.begin("close"); + f(s.st); + _ = cl.end(prev); + }; + s.st = null; + s.arena.deinit(); + s.arena = .init(s.gpa); + if (checked) s.used.clearRetainingCapacity(); + if (s.record) |r| s.gpa.free(r); + s.record = record; + return reopen(p, id, s, bytes orelse ""); +} + /// Its kind's close, then everything it had. pub fn close(p: *Pardes, pane: *Pane) void { const s = pane.addon orelse return; @@ -352,7 +414,7 @@ pub fn paint(p: *const Pardes, slot: *Slot, id: usize, arena: std.mem.Allocator, const cells = arena.alloc(c.pa_cell, @as(usize, cols) * rows) catch return null; @memset(std.mem.sliceAsBytes(cells), 0); var out: c.pa_paint = std.mem.zeroInit(c.pa_paint, .{ .placements = &frame.buf, .cap = frame.buf.len }); - const px = if (comptime pardes.pdf_enabled) p.view().cell_pixels else struct { w: u16 = 8, h: u16 = 16 }{}; + const px = p.view().cell_pixels; const win = std.mem.zeroInit(c.pa_window, .{ .cells = cells.ptr, .stride = cols, .rows = rows, .cols = cols, .cell_w = px.w, .cell_h = px.h, .focused = @intFromBool(focusedAnywhere(p, id)) }); var cl: Call = .{ .p = null, .id = 0, .slot = slot, .quiet = true }; const prev = cl.begin("paint"); @@ -369,6 +431,8 @@ pub fn paint(p: *const Pardes, slot: *Slot, id: usize, arena: std.mem.Allocator, if (placed != slot.placed or !std.mem.eql(u8, own, slot.prefix.slice())) slot.retag = true; slot.prefix.set(own); slot.placed = placed; + slot.thumb_at = std.math.clamp(out.at, 0, 1); + slot.thumb_span = std.math.clamp(out.span, 0, 1); return cells; } @@ -406,11 +470,18 @@ pub fn prefix(p: *const Pardes, arena: std.mem.Allocator, pane: *const Pane) ![] const slot = pane.addon.?; var buf: [400]u8 = undefined; const made = tape.value(.addon, prefixOf, .{ p, slot, &buf }); + if (tape.stepping()) slot.told.set(made); return arena.dupe(u8, made); } fn prefixOf(p: *const Pardes, slot: *const Slot, buf: *[400]u8) []const u8 { if (!slot.live) return if (slot.path.len > 0) slot.path else slot.nameOf(); + // A Shadow's is the authority's last step's, never its frames' guess: + // a speculation, a load and a frame drawn between steps (which lays the + // tag out too) all lay it out as the session's step did. + // ponytail: so a window client's page in the tag is the session's last + // step's, a notch behind its frames until the next step. + if (slot.ops == null and slot.told.len > 0) return slot.told.slice(); const own = if (slot.shadow) |sh| sh.prefix.slice() else slot.prefix.slice(); const placed = if (slot.shadow) |sh| sh.placed else slot.placed; var w: std.Io.Writer = .fixed(buf); @@ -429,6 +500,7 @@ fn prefixOf(p: *const Pardes, slot: *const Slot, buf: *[400]u8) []const u8 { pub fn key(p: *Pardes, id: usize, k: pardes.Key) bool { const slot = (p.panes[id] orelse return false).addon orelse return false; if (!caps(p, slot).key) return false; + if (k.cp == pardes.Key.escape and !caps(p, slot).esc) return false; if (authorityOnly("a key into a kind's pane")) return true; const pk: c.pa_key = .{ .cp = k.cp, .ctrl = @intFromBool(k.ctrl), .alt = @intFromBool(k.alt), .shift = @intFromBool(k.shift), .pad = 0 }; var cl: Call = .{ .p = p, .id = id, .slot = slot }; @@ -447,6 +519,36 @@ pub fn takesKeys(p: *const Pardes, s: *const Slot) bool { return caps(p, s).key; } +/// The kind's own rows for the core's `/` (2.1's `search`), in `buf`; null +/// for a kind with none. +pub fn search(p: *Pardes, id: usize, pat: []const u8, buf: []u8) ?[]const u8 { + const slot = (p.panes[id] orelse return null).addon orelse return null; + if (!caps(p, slot).search) return null; + if (authorityOnly("a kind's search")) return null; + var cl: Call = .{ .p = p, .id = id, .slot = slot }; + const prev = cl.begin("search"); + if (slot.ops) |o| cl.rows = buf[0..@min(buf.len, o.search.?(slot.st, pat.ptr, @intCast(pat.len), buf.ptr, @intCast(buf.len)))]; + // The rows are the authority's, on the tape: a replica's are those. + const o = cl.end(prev); + poke(p, slot); + return o.rows; +} + +/// A place in it (`address`): taken, or null and what the kind said of +/// 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. + if (!caps(p, slot).address) return false; + if (authorityOnly("a kind's address")) return true; + var cl: Call = .{ .p = p, .id = id, .slot = slot, .missing = said }; + const prev = cl.begin("address"); + if (slot.ops) |o| cl.ret = o.address.?(slot.st, text.ptr, @intCast(text.len)); + const o = cl.end(prev); + poke(p, slot); + return o.ret != 0; +} + pub fn takesMouse(p: *const Pardes, s: *const Slot) bool { return caps(p, s).mouse; } @@ -463,6 +565,8 @@ pub fn mouse(p: *Pardes, id: usize, m: pardes.Mouse, col: u16, row: u16) bool { .right => c.PA_RIGHT, .wheel_up => c.PA_WHEEL_UP, .wheel_down => c.PA_WHEEL_DOWN, + .wheel_left => c.PA_WHEEL_LEFT, + .wheel_right => c.PA_WHEEL_RIGHT, else => 255, }, .action = switch (m.kind) { @@ -495,11 +599,15 @@ pub fn step(p: *Pardes) void { slot.retag = false; if (!caps(p, slot).tick) continue; const focused = focusedAnywhere(p, id); - if (focused != slot.focused) { + const seen = std.hash.Wyhash.hash(0, std.mem.asBytes(&viewOf(p, id))); + if (focused != slot.focused or seen != slot.view) { slot.focused = focused; + slot.view = seen; slot.next_tick = 1; } - if (slot.next_tick == 0 or now < slot.next_tick) continue; + // 1 is the next step, whatever the clock says (a session whose + // clock has not moved yet reads 0). + if (slot.next_tick == 0 or (slot.next_tick > 1 and now < slot.next_tick)) continue; var cl: Call = .{ .p = p, .id = id, .slot = slot }; const prev = cl.begin("tick"); if (slot.ops) |o| cl.ret = @bitCast(o.tick.?(slot.st, now, if (focused) c.PA_FOCUSED else 0)); @@ -508,6 +616,36 @@ pub fn step(p: *Pardes) void { }; } +/// Pane `id` as its next paint sees it: its body below its notices, as +/// draw.zig paints it, a cell's pixels, the theme's colours and the +/// raster this frontend wants a picture at. +pub fn viewOf(p: *const Pardes, id: usize) c.pa_view { + const v = p.view(); + const pane = p.panes[id] orelse return std.mem.zeroes(c.pa_view); + const r = v.rects[id]; + const th = p.theme(); + const rgb = struct { + fn f(x: [3]u8) u32 { + return @as(u32, x[0]) << 16 | @as(u32, x[1]) << 8 | x[2]; + } + }.f; + const policy = pardes.rasterPolicy(v.frontend.windowed); + return .{ + .cols = if (pane.collapsed) 0 else r.w -| pardes.config.GUTTER, + .rows = if (pane.collapsed) 0 else (r.h -| pane.tag_rows) -| pane.notices.span(), + .cell_w = v.cell_pixels.w, + .cell_h = v.cell_pixels.h, + .fg = rgb(th.fg orelse th.tag_fg), + .bg = rgb(th.bg orelse th.tag_bg), + .sel = rgb(th.sel_bg), + .link = rgb(th.box), + .dpi = policy.dpi, + .max_px = policy.max_dimension, + .fill = @intFromBool(policy.match_viewport), + .pad = @splat(0), + }; +} + /// Whether pane `id` has the keyboard in some view (its window focused). fn focusedAnywhere(p: *const Pardes, id: usize) bool { for (&p.views) |*v| if (v.*) |*vw| if (vw.active == id and vw.window_focused) return true; @@ -567,6 +705,15 @@ pub fn dumpCall(slot: *Slot, f: anytype, buf: []u8) u32 { return f(slot.st, buf.ptr, @intCast(buf.len)); } +/// `where`, as the authority's kind says it (on the tape): what a step +/// keeps of it (a jump, Recent) a replica keeps alike. +pub fn placeOf(slot: *Slot, buf: *[32]u8) []const u8 { + const w = tape.value(.addon, where, .{ slot, buf }); + // A Shadow's place is the authority's last (`where` says it after). + if (slot.ops == null) slot.where_text.set(w); + return w; +} + /// Where in it the pane is, as a text its `address` takes back. pub fn where(slot: *Slot, buf: *[32]u8) []const u8 { const ops = slot.ops orelse return slot.where_text.slice(); @@ -627,6 +774,12 @@ pub const Outcome = struct { look: []const u8 = "", pane_name: []const u8 = "", pane_text: []const u8 = "", + exec: []const u8 = "", + selected: ?[]const u8 = null, + rows: []const u8 = "", + /// The jobs it asked for (their tokens, 0 past the last): every core + /// emits them, so a replica's effects are the authority's. + jobs: [8]u32 = @splat(0), }; /// The call under way: what `api` acts on, and what it gathers. @@ -638,6 +791,8 @@ const Call = struct { /// nothing is taped. quiet: bool = false, opening: bool = false, + /// An address's: what it says of a miss is the Look's to say. + missing: ?*Buf(160) = null, name: []const u8 = "", ret: i64 = 0, refused: Buf(160) = .{}, @@ -646,6 +801,10 @@ const Call = struct { look: Buf(256) = .{}, pane_name: Buf(64) = .{}, pane_text: []const u8 = "", + exec: Buf(256) = .{}, + selected: ?[]const u8 = null, + rows: []const u8 = "", + jobs: [8]u32 = @splat(0), fn begin(cl: *Call, comptime name: []const u8) ?*Call { cl.name = name; @@ -658,11 +817,21 @@ const Call = struct { fn end(cl: *Call, prev: ?*Call) Outcome { current = prev; if (cl.quiet) return .{}; - const o = tape.value(.addon, same, .{Outcome{ .ret = cl.ret, .text = cl.refused.slice(), .said = cl.said.slice(), .err = cl.err, .look = cl.look.slice(), .pane_name = cl.pane_name.slice(), .pane_text = cl.pane_text }}); + const o = tape.value(.addon, same, .{Outcome{ .ret = cl.ret, .text = cl.refused.slice(), .said = cl.said.slice(), .err = cl.err, .look = cl.look.slice(), .pane_name = cl.pane_name.slice(), .pane_text = cl.pane_text, .exec = cl.exec.slice(), .selected = cl.selected, .rows = cl.rows, .jobs = cl.jobs }}); + if (cl.p) |p| if (p.panes[cl.id]) |pane| for (o.jobs) |token| if (token != 0) p.emit(.{ .job = .{ .pane = @intCast(cl.id), .serial = pane.serial, .token = token } }); + if (o.selected) |sel| { + cl.slot.gpa.free(cl.slot.selection); + cl.slot.selection = cl.slot.gpa.dupe(u8, sel) catch &.{}; + } + if (cl.missing) |m| if (o.err and o.ret == 0) { + m.set(o.said); + return o; + }; const p = cl.p orelse return o; if (o.said.len > 0) if (o.err) p.reportFailure(cl.id, o.said) else p.setMessage(cl.id, o.said); if (o.pane_name.len > 0) textPane(p, cl.id, cl.slot, o.pane_name, o.pane_text); if (o.look.len > 0) @import("look.zig").lookAt(p, cl.id, o.look); + if (o.exec.len > 0) @import("mouse.zig").dispatchPointerBuiltin(p, cl.id, pardes.config.exec_cmd, o.exec, null); return o; } }; @@ -705,18 +874,18 @@ pub const api: c.pa_api = .{ /// Its input kept for the host, which runs it (Effect.job). fn f(in: ?*const anyopaque, n: u32) callconv(.c) u32 { const cl = current orelse return 0; - const p = cl.p orelse return 0; - if (cl.quiet) return 0; + if (cl.p == null or cl.quiet) return 0; const s = cl.slot; const free = for (&s.jobs) |*j| { if (j.* == null) break j; } else return 0; + const asked = std.mem.indexOfScalar(u32, &cl.jobs, 0) orelse return 0; const copy = s.gpa.dupe(u8, if (in) |b| @as([*]const u8, @ptrCast(b))[0..n] else "") catch return 0; s.tokens +%= 1; if (s.tokens == 0) s.tokens = 1; free.* = .{ .token = s.tokens, .in = copy }; - const pane = p.panes[cl.id] orelse return 0; - p.emit(.{ .job = .{ .pane = @intCast(cl.id), .serial = pane.serial, .token = s.tokens } }); + // Emitted as the call ends (Call.end), on every core. + cl.jobs[asked] = s.tokens; return s.tokens; } }.f, @@ -755,28 +924,54 @@ pub const api: c.pa_api = .{ return std.hash.Wyhash.hash(seed_base, std.mem.asBytes(&serial)); } }.f, + .heap = struct { + /// `heap`'s, any thread: realloc's, n 0 a free. + fn f(ptr: ?*anyopaque, n: usize) callconv(.c) ?*anyopaque { + if (n == 0) { + kind_heap.free(ptr); + return null; + } + return kind_heap.realloc(ptr, n); + } + }.f, + .view = struct { + fn f(out: [*c]c.pa_view) callconv(.c) void { + const cl = current orelse return; + out.* = if (cl.p) |p| viewOf(p, cl.id) else std.mem.zeroes(c.pa_view); + } + }.f, + .exec = struct { + fn f(text: [*c]const u8, n: u32) callconv(.c) void { + const cl = current orelse return; + if (!cl.quiet and text != null) cl.exec.set(text[0..n]); + } + }.f, + .select = struct { + fn f(text: [*c]const u8, n: u32) callconv(.c) void { + const cl = current orelse return; + if (cl.quiet) return; + const p = cl.p orelse return; + cl.selected = p.scratch.allocator().dupe(u8, if (text == null) "" else text[0..n]) catch return; + } + }.f, }; +/// What `api.heap` hands out: memory that outlives a call, from any +/// thread (a document a kind keeps, its pixels, MuPDF's own). A host sets +/// it to its region for kinds (memory.zig); the C heap otherwise. +pub var heap: std.mem.Allocator = std.heap.c_allocator; +const kind_heap = @import("c_heap").Heap(&heap); + /// What every pane's seed is made from: 0, or `--addon-test`'s `seed N`. pub var seed_base: u64 = 0; +/// A kind's list (`api.text`): a pane named `name` in its file's +/// directory, refilled when one is there, whose rows n and N walk from the +/// kind's pane, a Look of one going to its kind (look.zig lookKind). fn textPane(p: *Pardes, id: usize, slot: *const Slot, name: []const u8, text: []const u8) void { const dir = std.fs.path.dirname(slot.path) orelse p.sessionDir(); - const free = p.freeSlot() orelse return; - const path = std.fmt.allocPrint(p.gpa, "{s}/{s}", .{ std.mem.trimEnd(u8, dir, "/"), name }) catch return; - const content = p.gpa.dupe(u8, text) catch return p.gpa.free(path); - const history = pardes.panes.File.History.create(p.gpa) catch { - p.gpa.free(path); - return p.gpa.free(content); - }; - const pane = p.newDocPane(free) catch { - p.gpa.destroy(history); - p.gpa.free(path); - return p.gpa.free(content); - }; - pane.file = .{ .path = path, .content = content, .history = history }; - pane.body.cur_pinned = true; - @import("exec.zig").placeNew(p, id, id, free, .doc); + const content = p.gpa.dupe(u8, text) catch return; + pardes.panes.Output.fillResults(p, id, dir, .kind, name, content, null) catch |err| p.reportError(id, name, err); } /// A kind for tests of the core's side of a kind's pane, no library: a |
