summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--addons/fake/fake.c2
-rw-r--r--addons/image/image.c4
-rw-r--r--addons/snake/snake.c1
-rw-r--r--src/Addon.zig18
-rw-r--r--src/Kind.zig253
-rw-r--r--src/addon_test.zig9
-rw-r--r--src/kinds/pardes_addon.h65
-rw-r--r--test/behaviour/addons.zig8
8 files changed, 308 insertions, 52 deletions
diff --git a/addons/fake/fake.c b/addons/fake/fake.c
index 818c8ea3..81a78741 100644
--- a/addons/fake/fake.c
+++ b/addons/fake/fake.c
@@ -76,7 +76,7 @@ static void paint(void *st, const pa_window *w, pa_paint *out) {
static int key(void *st, const pa_key *k) {
fake *f = st;
- if (k->cp == 'q') return 0; /* not taken: the core's */
+ if (k->cp == 'q' || k->cp == 0x1b) return 0; /* not taken: the core's (Esc leaves) */
f->count++;
f->last[0] = 'k';
f->last[1] = k->cp < 128 ? (char)k->cp : '?';
diff --git a/addons/image/image.c b/addons/image/image.c
index f7a228c8..1aa34428 100644
--- a/addons/image/image.c
+++ b/addons/image/image.c
@@ -131,10 +131,12 @@ static uint32_t dump(void *st, uint8_t *buf, uint32_t n) {
return im->len;
}
+/* On a state with no picture (a Restore's), the record's; one open made
+ * from the file as it is now (it changed on disk) keeps that one. */
static int load(void *st, const uint8_t *buf, uint32_t n, uint32_t version) {
(void)version;
if (!st) return 1;
- keep(st, buf, n);
+ if (!((image *)st)->raw) keep(st, buf, n);
return PA_OK;
}
diff --git a/addons/snake/snake.c b/addons/snake/snake.c
index e6ad96e4..ce9279dc 100644
--- a/addons/snake/snake.c
+++ b/addons/snake/snake.c
@@ -109,6 +109,7 @@ static void paint(void *st, const pa_window *w, pa_paint *out) {
static int key(void *st, const pa_key *k) {
snake *s = st;
+ if (k->cp == 0x1b) return 0; /* Esc leaves */
if (s->dead) { restart(s); return 1; }
int dx = 0, dy = 0;
switch (k->cp) {
diff --git a/src/Addon.zig b/src/Addon.zig
index 6955741d..9f94f1de 100644
--- a/src/Addon.zig
+++ b/src/Addon.zig
@@ -65,13 +65,19 @@ pub fn perform(p: *Pardes, op: Op, pane: u8, arg: []const u8) void {
};
return;
}
- var data_buf: [4096]u8 = undefined;
var words: [64]LoadedWord = undefined;
var why: Why = .{};
- const l = read(dataDir(&data_buf), arg, &words, &why);
+ const l = readLoaded(arg, &words, &why);
p.update(.{ .addon = .{ .op = op, .pane = pane, .kind = &l } });
}
+/// What a load reads off the library at `path` (held): the `addon`
+/// event's, for a caller that steps its core itself (a replica's authority).
+pub fn readLoaded(path: []const u8, words: *[64]LoadedWord, why: *Why) Loaded {
+ var data_buf: [4096]u8 = undefined;
+ return read(dataDir(&data_buf), path, words, why);
+}
+
/// The library at `path`, held (a second hold of the same bytes is the
/// first's library) and checked: what it brings, or why not.
fn read(data: []const u8, path: []const u8, words: *[64]LoadedWord, why: *Why) Loaded {
@@ -327,7 +333,13 @@ pub fn loadedOf(ops: *const Kind.Ops, path: []const u8, hash: [32]u8, words: *[6
.exts = if (h.exts == null) "" else std.mem.span(h.exts),
.words = words[0..head_words.len],
.ops = @intFromPtr(ops),
- .caps = .{ .tick = ops.tick != null, .key = ops.key != null, .mouse = ops.mouse != null, .ctl = ops.ctl != null, .jobs = ops.job_run != null, .load = ops.load != null },
+ .caps = .{
+ .tick = ops.tick != null, .key = ops.key != null, .mouse = ops.mouse != null, .ctl = ops.ctl != null, .jobs = ops.job_run != null, .load = ops.load != null,
+ // 2.1's: a library of 2.0 has no `search` to read, and Esc leaves it.
+ .search = h.abi & 0xffff >= 1 and ops.search != null,
+ .esc = h.abi & 0xffff >= 1 and ops.key != null,
+ .address = ops.address != null,
+ },
};
}
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
diff --git a/src/addon_test.zig b/src/addon_test.zig
index a4788109..427f0289 100644
--- a/src/addon_test.zig
+++ b/src/addon_test.zig
@@ -47,7 +47,9 @@ fn run(io: std.Io, gpa: std.mem.Allocator, lib: []const u8, script_path: ?[]cons
defer pardes.memory.deinit();
pardes.syntax.start(allocs.tree_sitter);
defer pardes.syntax.stop();
- const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24, .state = .memory, .image_allocator = allocs.image, .pdf_allocator = allocs.pdf, .tree_sitter_allocator = allocs.tree_sitter, .frame_allocator = allocs.frame });
+ // A terminal's view on every build: what a script prints is the same
+ // from a window's binary (a picture's raster is a view's, Kind.viewOf).
+ const p = try Pardes.init(gpa, .{ .tty_only = true, .windowed = false, .cols = 80, .rows = 24, .state = .memory, .image_allocator = allocs.image, .tree_sitter_allocator = allocs.tree_sitter, .frame_allocator = allocs.frame });
defer p.deinit();
// Loaded as `Addon load` loads it, the host's half here.
pardes.Addon.perform(p, .load, 0, lib);
@@ -218,6 +220,9 @@ fn show(p: *Pardes, gpa: std.mem.Allocator, id: usize, out: *std.Io.Writer, size
try out.print("|{s}\n", .{std.mem.trimEnd(u8, row.items, " ")});
}
for (s.placements.items) |pl| if (pl.pane == id) {
- try out.print("placement {d}x{d} src {d},{d} {d}x{d} over {d},{d} {d}x{d} key {x}\n", .{ pl.raster.w, pl.raster.h, pl.src.x, pl.src.y, pl.src.w, pl.src.h, pl.dst.x - r.x, pl.dst.y - r.y, pl.dst.w, pl.dst.h, pl.key });
+ try out.print("placement {d}x{d} src {d},{d} {d}x{d} over {d},{d} {d}x{d} key {x}", .{ pl.raster.w, pl.raster.h, pl.src.x, pl.src.y, pl.src.w, pl.src.h, pl.dst.x - r.x, pl.dst.y - r.y, pl.dst.w, pl.dst.h, pl.key });
+ if (pl.at) |at| try out.print(" at {d},{d} {d}x{d}", .{ at.x, at.y, at.w, at.h });
+ if (pl.dirty) |d| try out.print(" rows {d}+{d} of {x}", .{ d.y, d.h, d.from });
+ try out.writeAll("\n");
};
}
diff --git a/src/kinds/pardes_addon.h b/src/kinds/pardes_addon.h
index 6241c6c0..e7334063 100644
--- a/src/kinds/pardes_addon.h
+++ b/src/kinds/pardes_addon.h
@@ -16,7 +16,9 @@
* Conventions: init, ctl and load return PA_OK (0), or nonzero to refuse;
* key, mouse and address return nonzero when the event or text was theirs.
* The pa_api pointer handed to init is kept by the addon; it is about the
- * pane of the current call.
+ * pane of the current call. ABI 2.1 adds fields at the ends of pa_api,
+ * pa_paint and pa_ops: a core reads an addon's new ones by its head's
+ * minor, an addon pardes's by api->size.
*
* addons/fake/fake.c implements every function; it is the template, and
* addons/snake/snake.c a game in it.
@@ -30,7 +32,7 @@
/* Major in the high half: an addon of another major is refused. */
#define PA_ABI_MAJOR 2u
-#define PA_ABI_MINOR 0u
+#define PA_ABI_MINOR 1u
#define PA_ABI ((PA_ABI_MAJOR << 16) | PA_ABI_MINOR)
#define PA_OK 0
@@ -101,12 +103,16 @@ typedef struct pa_placement {
* core follows with one space, its own words for a picture shown
* (`petscii:on palette:commodore ascii:off`, when this frame placed one),
* and the pane's path; empty, the kind's name. paint is called once a
- * frame, body and prefix together, never over no cells. */
+ * frame, body and prefix together, never over no cells. 2.1: `at` and
+ * `span`, where the window is in the pane's whole (the share above it and
+ * the share it shows, 0 to 1), are the rail's thumb; 0 and 0, none. A kind
+ * writes them only to a core whose api->size says 2.1. */
typedef struct pa_paint {
pa_placement *placements;
uint32_t cap, n;
char prefix[96];
uint32_t prefix_len;
+ float at, span;
} pa_paint;
/* ---- input ---- */
@@ -134,11 +140,14 @@ typedef struct pa_key {
#define PA_RIGHT 2
#define PA_WHEEL_UP 3
#define PA_WHEEL_DOWN 4
+#define PA_WHEEL_LEFT 5
+#define PA_WHEEL_RIGHT 6
#define PA_PRESS 0
#define PA_RELEASE 1
#define PA_MOTION 2
#define PA_DRAG 3
-/* A pointer event in the window's cells. */
+/* A pointer event in the window's cells; a motion at col and row 0xffff
+ * when the pointer leaves the window. */
typedef struct pa_mouse {
uint8_t button, action, ctrl, shift, alt, pad;
uint16_t col, row;
@@ -146,6 +155,17 @@ typedef struct pa_mouse {
/* ---- what pardes does for an addon ---- */
+/* The pane as its next paint will see it (2.1, api->view): its window, a
+ * cell's pixels; the theme's text, paper, selection and link colours
+ * (PA_RGB); and the raster a picture is wanted at: `dpi`, at most `max_px`
+ * a side, and never smaller than the pixels it covers when `fill` is set. */
+typedef struct pa_view {
+ uint16_t cols, rows, cell_w, cell_h;
+ uint32_t fg, bg, sel, link;
+ uint16_t dpi, max_px;
+ uint8_t fill, pad[3];
+} pa_view;
+
/* Valid during any call pardes makes for a pane, and about that pane. */
typedef struct pa_api {
uint32_t size;
@@ -165,6 +185,17 @@ typedef struct pa_api {
/* The pane's seed: the same pane, the same number, on every machine (a
* game's board), and what `pardes --addon-test`'s `seed N` sets. */
uint64_t (*seed)(void);
+ /* 2.1 (api->size >= 96): memory of pardes's that outlives a call, as
+ * realloc's (n 0 frees it), on any thread at any time: what a pane's
+ * arena cannot hold, a cache, a library's own heap. */
+ void *(*heap)(void *ptr, size_t n);
+ /* The pane's window and the theme now (pa_view). */
+ void (*view)(pa_view *out);
+ /* Run `text` as a middle click would. */
+ void (*exec)(const char *text, uint32_t n);
+ /* The text the pane has selected now, which a yank, a chord's Snarf and
+ * a keyboard Look take; n 0, none. */
+ void (*select)(const char *text, uint32_t n);
} pa_api;
/* What a job allocates from, on its worker (pa_api is not there): as often
@@ -222,22 +253,29 @@ typedef struct pa_ops {
/* Time has come to now_ns (monotonic, any origin; --addon-test's starts
* at 1 s): step, and return the absolute time of the next step, 0 for
* none. Called when that time comes and once after open, a taken key or
- * mouse, ctl, job_done, load, and a change of PA_FOCUSED in `flags`: a
- * kind that returned 0 is woken by the next thing that happens to it.
+ * mouse, ctl, job_done, load, and a change of PA_FOCUSED in `flags` (2.1:
+ * or of its pa_view): a kind that returned 0 is woken by the next thing
+ * that happens to it.
* Absent, the pane repaints when the core repaints. */
uint64_t (*tick)(void *st, uint64_t now_ns, uint32_t flags);
/* Required. Pure: the same state paints the same, and changes nothing. */
void (*paint)(void *st, const pa_window *win, pa_paint *out);
/* Every key but Esc, which leaves; nonzero if taken. A key not taken goes
- * to the editor's own navigation. Absent: no keys. */
+ * to the editor's own navigation. Absent: no keys. 2.1: Esc too, first;
+ * not taken, it leaves. */
int (*key)(void *st, const pa_key *k);
/* Nonzero if taken. Absent: a click focuses the pane. */
int (*mouse)(void *st, const pa_mouse *m);
/* The pane's body as text (9P `body`). Absent: empty. */
uint64_t (*body_len)(void *st);
uint32_t (*body)(void *st, uint64_t off, char *buf, uint32_t n);
- /* Go to `text` (`:12`, `12:0.33`); nonzero if it is a place here. And
- * where the pane is now, as such a text (jumps, Recent, a Restore). */
+ /* Go to `text` (`:12`, `12:0.33`); nonzero if it is a place here (a miss
+ * may `say` why, an error). A place looked at in a list the kind made
+ * (`text`) comes as `<name> <place>`. And where the pane is now, as such
+ * a text (jumps, Recent, a Restore): `L` or `L:F`, its first number a
+ * line's and F how far into it, then, after a space, the window's height
+ * in lines if it has one: two places within it are one visit of the
+ * jumps. */
int (*address)(void *st, const char *text, uint32_t n);
uint32_t (*where)(void *st, char *buf, uint32_t n);
/* One of the addon's words, as written (`Fit width`): PA_OK, else
@@ -258,6 +296,9 @@ typedef struct pa_ops {
* head.version: PA_OK, or nonzero to refuse (the pane waits as a
* placeholder, its record kept). */
int (*load)(void *st, const uint8_t *buf, uint32_t n, uint32_t version);
+ /* 2.1: the pane's own search for the core's `/`: rows `place text`, a
+ * line each, into `buf`; their length. Absent: `/` finds nothing here. */
+ uint32_t (*search)(void *st, const char *pat, uint32_t n, char *buf, uint32_t cap);
} pa_ops;
/* The records' sizes on a 64-bit target: an addon built against another
@@ -267,11 +308,11 @@ _Static_assert(sizeof(pa_cell) == 20, "pa_cell");
_Static_assert(sizeof(pa_window) == 24, "pa_window");
_Static_assert(sizeof(pa_rect) == 16 && sizeof(pa_cells) == 8, "pa_rect");
_Static_assert(sizeof(pa_placement) == 80, "pa_placement");
-_Static_assert(sizeof(pa_paint) == 120, "pa_paint");
+_Static_assert(sizeof(pa_paint) == 128 && sizeof(pa_view) == 32, "pa_paint");
_Static_assert(sizeof(pa_key) == 8 && sizeof(pa_mouse) == 10, "pa_key");
-_Static_assert(sizeof(pa_api) == 64 && sizeof(pa_out) == 16, "pa_api");
+_Static_assert(sizeof(pa_api) == 96 && sizeof(pa_out) == 16, "pa_api");
_Static_assert(sizeof(pa_word) == 32 && sizeof(pa_head) == 56, "pa_head");
-_Static_assert(sizeof(pa_ops) == 192, "pa_ops");
+_Static_assert(sizeof(pa_ops) == 200, "pa_ops");
#endif
/* ---- helpers ---- */
diff --git a/test/behaviour/addons.zig b/test/behaviour/addons.zig
index c7a59951..8d780c98 100644
--- a/test/behaviour/addons.zig
+++ b/test/behaviour/addons.zig
@@ -37,7 +37,7 @@ test "addons: Addon load takes a library in, /addons lists it with the kinds com
defer unload(&s, "fake");
// `<kind> <version> <abi> <panes open> <path> <blake3> <state>`.
const listed = try s.read("/addons");
- try std.testing.expect(std.mem.startsWith(u8, listed, "fake 1 2.0 0 /"));
+ try std.testing.expect(std.mem.startsWith(u8, listed, "fake 1 2.1 0 /"));
var fields = std.mem.tokenizeScalar(u8, listed[0..std.mem.indexOfScalar(u8, listed, '\n').?], ' ');
for (0..5) |_| _ = fields.next();
try std.testing.expectEqual(@as(usize, 64), fields.next().?.len);
@@ -51,7 +51,7 @@ test "addons: Addon load takes a library in, /addons lists it with the kinds com
// A pane of it open, it stays; closed, it goes.
try s.write("/ctl", "Fake\n");
const pane = try paneOf(&s, "fake");
- try std.testing.expect(std.mem.startsWith(u8, try s.read("/addons"), "fake 1 2.0 1 /"));
+ try std.testing.expect(std.mem.startsWith(u8, try s.read("/addons"), "fake 1 2.1 1 /"));
try std.testing.expect(try s.refused("/ctl", "Addon unload fake\n") != 0);
try std.testing.expect(has(s.said, "1 fake pane is open"));
try s.write(s.at("/pane/{d}/ctl", .{pane}), "Del\n");
@@ -68,7 +68,7 @@ test "addons: a library of another ABI, one with no pardes_addon, and one whose
try std.testing.expect(std.mem.startsWith(u8, listed, "fakeabi 1 3.0 0 /"));
try std.testing.expect(has(listed, " refused: wants abi 3, this pardes speaks 2\n"));
try std.testing.expect(has(listed, " refused: it has no pardes_addon\n"));
- try std.testing.expect(has(listed, "\nfakeword 1 2.0 0 /"));
+ try std.testing.expect(has(listed, "\nfakeword 1 2.1 0 /"));
try std.testing.expect(has(listed, " refused: word Save is the core's\n"));
// A file that is no library at all: listed with what the loader said.
var tmp = std.testing.tmpDir(.{});
@@ -120,7 +120,7 @@ test "addons: the image addon opens the pictures a Look names, fitted, with the
defer s.deinit();
try load(&s, Session.image_addon);
const listed = try s.read("/addons");
- try std.testing.expect(std.mem.startsWith(u8, listed, "image 1 2.0 0 /") and has(listed, " loaded\n"));
+ try std.testing.expect(std.mem.startsWith(u8, listed, "image 1 2.1 0 /") and has(listed, " loaded\n"));
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
var buf: [4096]u8 = undefined;