diff options
| -rw-r--r-- | core/replica.zig | 11 | ||||
| -rw-r--r-- | dev/gui/gui.zig | 17 | ||||
| -rw-r--r-- | dev/tui/tui.zig | 16 | ||||
| -rw-r--r-- | runtime/client.zig | 142 | ||||
| -rw-r--r-- | test/replicas.zig | 36 |
5 files changed, 217 insertions, 5 deletions
diff --git a/core/replica.zig b/core/replica.zig index 92e49df5..c9a69bdf 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -3031,6 +3031,12 @@ pub const Link = struct { /// load took (PARDES_REPLICA_STATS). join: struct { blobs: u32 = 0, from_memory: u32 = 0, from_disk: u32 = 0, sent_bytes: u64 = 0 } = .{}, join_ms: u64 = 0, + /// Its view's yanks, pastes and links (Pardes.View.yanks) its + /// frontend has done: from the joined dump's, so a join does none + /// again (client.zig `asks`). + yanks: u32 = 0, + pastes: u32 = 0, + links: u32 = 0, /// Frames it drew whole (`drawn`), and the last one presented again /// moved on (its own motion), for PARDES_REPLICA_STATS. draws: u32 = 0, @@ -3066,6 +3072,11 @@ pub const Link = struct { c.asked = false; c.mispredicted = 0; c.put_off = false; + if (r.core().views[r.view]) |v| { + c.yanks = v.yanks; + c.pastes = v.pastes; + c.links = v.links; + } } pub fn notify(c: *Link, comptime fmt: []const u8, args: anytype) void { diff --git a/dev/gui/gui.zig b/dev/gui/gui.zig index 66b7328d..909c83c6 100644 --- a/dev/gui/gui.zig +++ b/dev/gui/gui.zig @@ -1422,6 +1422,23 @@ const Client = struct { return .{ .drew = cl.shell.presented, .moving = frontendMoving(cl.g) }; } + /// What its view asked of this machine (client.zig `asks`): register + /// `+` to the desktop's clipboard, the clipboard's text sent as a paste, + /// a link opened. + pub fn copy(cl: *Client, text: []const u8) void { + putClipboard(cl.gpa, text); + } + + pub fn paste(_: *Client, l: *ClientLoop) void { + const text = takeClipboard() orelse return; + defer c.SDL_free(text.ptr); + l.input(.{ .paste = text }); + } + + pub fn open(_: *Client, url: []const u8) void { + look.openLink(url); + } + /// The reader's: a message of the stream, for the loop, which it wakes. pub fn said(cl: *Client, msg: replica.Said) bool { cl.lock.lockUncancelable(cl.io); diff --git a/dev/tui/tui.zig b/dev/tui/tui.zig index c2f3dbf1..5b5c1e67 100644 --- a/dev/tui/tui.zig +++ b/dev/tui/tui.zig @@ -694,6 +694,22 @@ const Tty = struct { return .{ .drew = t.sh.frames.bytes > 0, .moving = false }; } + /// What its view asked of this machine (client.zig `asks`): register + /// `+` to the terminal's clipboard (OSC 52), its clipboard asked for + /// (the answer comes back as a paste, `Shell.feed`), a link opened. + pub fn copy(t: *Tty, text: []const u8) void { + const sc = t.screen; + if (text.len > 0) sc.vx.copyToSystemClipboard(sc.tty.writer(), text, t.sh.gpa) catch {}; + } + + pub fn paste(t: *Tty, _: *Client) void { + t.screen.vx.requestSystemClipboard(t.screen.tty.writer()) catch {}; + } + + pub fn open(_: *Tty, url: []const u8) void { + look.openLink(url); + } + /// The session's stream, onto the loop. pub fn said(t: *Tty, msg: replica.Said) bool { t.loop.postEvent(.{ .said = msg }) catch return false; diff --git a/runtime/client.zig b/runtime/client.zig index 7ba9e790..544be588 100644 --- a/runtime/client.zig +++ b/runtime/client.zig @@ -22,6 +22,11 @@ //! - `present(fe, surface) bool` (views.Present's): a frame shown, or held //! for the frontend's answer (owed); and `drawn() Drawn`, read after it: //! whether it wrote anything, and whether its own motion goes on. +//! - `copy(text)`, `paste(l)`, `open(url)`: what its view asked of the +//! machine it is shown on (the user's ruling: a client's yank fills +//! the CLIENT's clipboard): register `+` to its clipboard, its +//! clipboard asked for and sent back as a paste (through `l.input`, +//! now or as its answer comes), a link opened. //! per join: //! - `start(l)`: a join's first inputs (what it is, its size). //! - `said(msg) bool`, `ended()`: the session's stream, from the reader's @@ -229,6 +234,34 @@ pub fn Loop(comptime Frontend: type) type { } if (link.owed or r.echo.changed) link.dirty = true; if (r.diverged != null) link.quit = true; + l.asks(); + } + + /// What the view asked of its frontend since it last looked (the + /// session's host does not do it for a view a client draws): a + /// yank's register `+` copied out, the clipboard asked for, a link + /// opened. Each once: a count a rollback put back and made again + /// is not a second. + fn asks(l: *L) void { + const link = &l.link; + const c = link.r.core(); + const v = if (c.views[link.r.view]) |*v| v else return; + if (ahead(v.yanks, link.yanks)) { + link.yanks = v.yanks; + l.fe.copy(c.registers.text(c.gpa, '+') orelse ""); + } + if (ahead(v.pastes, link.pastes)) { + link.pastes = v.pastes; + l.fe.paste(l); + } + if (ahead(v.links, link.links)) { + link.links = v.links; + l.fe.open(v.link.slice()); + } + } + + fn ahead(now: u32, seen: u32) bool { + return @as(i32, @bitCast(now -% seen)) > 0; } /// Its own view drawn by the frontend, outside any step: the @@ -247,3 +280,112 @@ pub fn Loop(comptime Frontend: type) type { } }; } + +// ---- a client's clipboard and links are its frontend's ---- + +/// A frontend that draws nothing and counts what its view asked of it. +const Null = struct { + copied: u32 = 0, + pasted: u32 = 0, + opened: u32 = 0, + text: [64]u8 = undefined, + text_len: usize = 0, + url: [64]u8 = undefined, + url_len: usize = 0, + + const L = Loop(Null); + pub fn own(_: *Null, _: *L) bool { + return true; + } + pub fn again(_: *Null, _: *L) bool { + return false; + } + pub fn deadline(_: *Null, _: *L) u32 { + return forever; + } + pub fn wait(_: *Null, _: *L, _: u32) void {} + pub fn present(_: ?*anyopaque, _: *const pardes.Surface) bool { + return true; + } + pub fn drawn(_: *Null) Drawn { + return .{ .drew = true, .moving = false }; + } + pub fn copy(n: *Null, text: []const u8) void { + n.copied += 1; + n.text_len = @min(text.len, n.text.len); + @memcpy(n.text[0..n.text_len], text[0..n.text_len]); + } + pub fn paste(n: *Null, _: *L) void { + n.pasted += 1; + } + pub fn open(n: *Null, url: []const u8) void { + n.opened += 1; + n.url_len = @min(url.len, n.url.len); + @memcpy(n.url[0..n.url_len], url[0..n.url_len]); + } +}; + +/// The session's host, which counts what it is asked of the clipboard and +/// links. +const AskedHost = struct { + yanked: u32 = 0, + asked: u32 = 0, + linked: u32 = 0, + fn of(ctx: ?*anyopaque) *AskedHost { + return @ptrCast(@alignCast(ctx.?)); + } + fn setClipboard(ctx: ?*anyopaque, _: []const u8) void { + of(ctx).yanked += 1; + } + fn readClipboard(ctx: ?*anyopaque) void { + of(ctx).asked += 1; + } + fn openLink(ctx: ?*anyopaque, _: []const u8) void { + of(ctx).linked += 1; + } + const vtable = replica.everyEntry(.{ .set_clipboard = setClipboard, .read_clipboard = readClipboard, .open_link = openLink }); + fn host(h: *AskedHost) pardes.Host { + return .{ .ctx = h, .vtable = &vtable }; + } +}; + +test "a client's yank, paste and link reach its own frontend once each, through the loop, and the session's host is asked none of them" { + const gpa = std.testing.allocator; + var h: AskedHost = .{}; + const a = try replica.Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, h.host(), false); + defer a.deinit(); + _ = try a.unit(0, 0); + const j = (try a.join(1, null)) orelse return error.NoView; + defer gpa.free(j.dump); + const r = try replica.Replica.initDump(gpa, 1, j.view, j.dump, .{ .cols = 80, .rows = 24 }); + defer r.deinit(); + var fds: [2]c_int = undefined; + if (libc.socketpair(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0, &fds) != 0) return error.NoSocket; + defer for (fds) |fd| { + _ = libc.close(fd); + }; + var frames: pardes.FrameStats = .{}; + var n: Null = .{}; + var l: Null.L = .{ .link = .{ .r = r, .fd = fds[1] }, .fe = &n, .frames = &frames }; + const joined: replica.Joined = .{ .fd = fds[1], .pid = 0, .client = 1, .view = j.view, .gmtoff = 0, .dump = &.{} }; + l.link.reset(r, &joined); + _ = l.pass(); + var sink: [1 << 14]u8 = undefined; + for ([_][]const u8{ "ClipYank", "ClipPaste", "Look https://ziglang.org" }) |cmd| { + l.input(.{ .command = cmd }); + const msg = try gpa.dupe(u8, r.out.items); + defer gpa.free(msg); + // At once, by its own step; and not again when the session's unit + // confirms it. + l.woken(); + l.take(.{ .unit = try std.heap.c_allocator.dupe(u8, try a.take(msg)) }); + l.woken(); + _ = libc.read(fds[0], &sink, sink.len); + } + try std.testing.expectEqual(@as(u32, 1), n.copied); + try std.testing.expectEqual(@as(u32, 1), n.pasted); + try std.testing.expectEqual(@as(u32, 1), n.opened); + try std.testing.expectEqualStrings("https://ziglang.org", n.url[0..n.url_len]); + try std.testing.expectEqual(@as(u32, 0), h.yanked + h.asked + h.linked); + try std.testing.expect(r.diverged == null and r.quiet()); +} diff --git a/test/replicas.zig b/test/replicas.zig index 2d199c60..69207970 100644 --- a/test/replicas.zig +++ b/test/replicas.zig @@ -205,6 +205,8 @@ const Totals = struct { /// The wakes the clients' loops asked for, and the frames they drew. wakes: u64 = 0, draws: u64 = 0, + /// The clients' views' yanks, pastes and links their frontends did. + asked: [3]u64 = @splat(0), stops: [32]struct { name: []const u8 = "", n: u64 = 0 } = @splat(.{}), units: u64 = 0, fast: u64 = 0, @@ -285,7 +287,8 @@ const Totals = struct { /// page and at a search's hit (plain kinds a client runs as the session /// does, `looks` past the texts), the keys after going to its pane; a /// search (`/`, a pattern, Enter), a sweep of B1 over the PDF and its -/// wheel; now and then a client's Detach. +/// wheel; a yank to the system clipboard and a paste from it; now and +/// then a client's Detach. fn nextEvent(random: std.Random, buf: *[64]u8, aim: Aim, looks: []const []const u8, script: *Script) pardes.Event { const typed = "abcdefghijklmnopqrstuvwxyz0123456789 -_=+,:;()"; const normal = "hjklwbe0$xpuyoOiaAIdcv.nN%sC"; @@ -316,7 +319,7 @@ fn nextEvent(random: std.Random, buf: *[64]u8, aim: Aim, looks: []const []const for (buf[0..n]) |*c| c.* = typed[random.uintLessThan(usize, typed.len)]; return .{ .paste = buf[0..n] }; }, - 36 => return .{ .command = switch (random.uintLessThan(u8, 14)) { + 36 => return .{ .command = switch (random.uintLessThan(u8, 16)) { 0 => "Newcol", 1 => "Zerox", 2 => "Undo", @@ -332,6 +335,10 @@ fn nextEvent(random: std.Random, buf: *[64]u8, aim: Aim, looks: []const []const // Its link ended by the session (the session's own view's, // its terminal's: none here). 12 => if (random.uintLessThan(u8, 4) == 0) "Detach" else "Undo", + // The system clipboard, the client's own frontend's (client.zig + // `asks`): a yank of the selection to it, a paste from it. + 13 => "ClipYank", + 14 => "ClipPaste", else => return .{ .key = .{ .cp = if (random.boolean()) 'o' else 'i', .ctrl = true } }, } }, 37 => return .{ .resize = .{ .cols = @intCast(random.intRangeAtMost(u16, 60, 120)), .rows = @intCast(random.intRangeAtMost(u16, 16, 40)) } }, @@ -459,6 +466,8 @@ const Client = struct { /// real wait's timeout is; an earlier one replaces it), and how many. wake_at: u64 = 0, wakes: u64 = 0, + /// Its view's yanks, pastes and links its frontend did. + asked: [3]u64 = @splat(0), drew: bool = false, /// What it types in a row (a search, a sweep). script: Script = .{}, @@ -543,6 +552,19 @@ const Client = struct { defer cl.drew = false; return .{ .drew = cl.drew, .moving = false }; } + + /// What its view asked of its machine (client.zig `asks`), counted; + /// its clipboard's answer a paste, as a frontend's comes. + pub fn copy(cl: *Client, _: []const u8) void { + cl.asked[0] += 1; + } + pub fn paste(cl: *Client, l: *ClientLoop) void { + cl.asked[1] += 1; + l.input(.{ .paste = "from the client's clipboard" }); + } + pub fn open(cl: *Client, _: []const u8) void { + cl.asked[2] += 1; + } }; const ClientLoop = client_loop.Loop(Client); @@ -1253,6 +1275,7 @@ fn run(r: Run, dir: []const u8, totals: *Totals) !void { for (clients[0..r.clients], 1..) |*cl, id| { totals.resizes += cl.resizes; totals.wakes += cl.wakes; + for (&totals.asked, cl.asked) |*t, n| t.* += n; if (has_link[id - 1]) totals.draws += cl.loop.link.draws; // Its quiet checks: what the session's digest was against its own. if (readAll(cl.check)) |said| { @@ -2525,7 +2548,7 @@ pub fn main(init: std.process.Init) !void { t.screens_checked, t.screens_wrong, }); - try stop_lines.print(gpa, "{d:>4} {d:>8} {d:>6} {d:>6} ", .{ rtt, t.unlisted, t.wakes, t.draws }); + try stop_lines.print(gpa, "{d:>4} {d:>8} {d:>6} {d:>6} {d:>3}/{d}/{d} ", .{ rtt, t.unlisted, t.wakes, t.draws, t.asked[0], t.asked[1], t.asked[2] }); for (t.stops) |st| if (st.n > 0) try stop_lines.print(gpa, "{s}:{d} ", .{ st.name, st.n }); try stop_lines.append(gpa, '\n'); try join_lines.print(gpa, "{d:>4} {d:>5} {d:>7} {d:>8} {d:>8} {d:>6} {d:>5}/{d:<5} {d:>5}/{d:<6} {d:>7} {d:>5}/{d}/{d}\n", .{ rtt, t.joins, t.refused, t.rejoined, t.detached, t.detach_words, t.agree, t.differ, t.checks, t.units, t.resizes, t.kind_panes[0], t.kind_panes[1], t.kind_panes[2] }); @@ -2535,8 +2558,8 @@ pub fn main(init: std.process.Init) !void { std.debug.print(" rtt deltas/s delta B/s unit B/s encode p50/p99 apply p50/p99 fetched filled KB resets checked/wrong\n{s}", .{term_lines.items}); std.debug.print("clients: joins from a dump, refused (every view taken), joined again after a divergence put on them, left (of them by a Detach); quiet checks of the session's digest agreed/differed; the digests the clients compared (units and quiet), of the units they took; resizes the windows posted; the kinds' panes the session opened (image/life/pdf)\n", .{}); std.debug.print(" rtt joins refused rejoined detached detach agree/differ checks/units resizes kinds\n{s}", .{join_lines.items}); - std.debug.print("stops: what stopped the clients' inputs (a stop counted again as its input is applied again), by name; those at a name replica.may_wait does not list fail the run\n", .{}); - std.debug.print(" rtt unlisted wakes draws stops\n{s}", .{stop_lines.items}); + std.debug.print("stops: the wakes the clients' loops asked, the frames they drew, the yanks, pastes and links their frontends did (y/p/l), and what stopped their inputs (a stop counted again as its input is applied again), by name; those at a name replica.may_wait does not list fail the run\n", .{}); + std.debug.print(" rtt unlisted wakes draws y/p/l stops\n{s}", .{stop_lines.items}); if (failed) std.process.exit(1); } @@ -2637,6 +2660,9 @@ const Shows = struct { pub fn drawn(_: *Shows) client_loop.Drawn { return .{ .drew = true, .moving = false }; } + pub fn copy(_: *Shows, _: []const u8) void {} + pub fn paste(_: *Shows, _: *client_loop.Loop(Shows)) void {} + pub fn open(_: *Shows, _: []const u8) void {} }; /// The frame corpus's scenes (test/process/window.zig `corpus`, what a |
