diff options
Diffstat (limited to 'runtime')
| -rw-r--r-- | runtime/client.zig | 142 |
1 files changed, 142 insertions, 0 deletions
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()); +} |
