summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Handover.zig235
-rw-r--r--src/gui/gui.zig6
-rw-r--r--src/ninep/ctl.zig25
-rw-r--r--src/pardes.zig4
4 files changed, 269 insertions, 1 deletions
diff --git a/src/Handover.zig b/src/Handover.zig
index fdbe509b..3acfc4e6 100644
--- a/src/Handover.zig
+++ b/src/Handover.zig
@@ -5,6 +5,11 @@
//! 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
//! edited while it was on its way stays here too (both then have it).
+//! Let go over another pardes window, the pane goes there instead: the
+//! worker finds the window under the pointer among the registry's sessions
+//! (each says in /status whether the pointer is over it) and writes it
+//! `adopt <dump>` (ninep/ctl.zig), which takes the pane in where the pointer
+//! is (`adopt`) before the write is answered.
//!
//! The core only dumps, and closes or keeps when told (`finish`); the host
//! writes the dump, and its worker (`carry`, on a thread of its own) spawns
@@ -17,6 +22,8 @@ const pardes = @import("pardes.zig");
const layout = @import("layout.zig");
const dump = @import("dump.zig");
const ninep_io = @import("9p_io.zig");
+const panes = @import("panes.zig");
+const tagline = @import("tagline.zig");
const Pardes = pardes.Pardes;
const MAX_PANES = pardes.MAX_PANES;
const libc = std.c;
@@ -127,6 +134,106 @@ pub fn finish(h: *Handover, p: *Pardes, seq: u32, to: []const u8, failure: ?[]co
std.fmt.bufPrint(&buf, "copied to {s}; edited on the way, kept here too", .{to}) catch "copied");
}
+// ---- the receiving window ----
+
+/// A carried dump (`begin`'s) taken into this window where the pointer is:
+/// a pane under the pane the pointer is over, out of that pane's rows
+/// alone; a column beside the column the pointer is over, out of its width
+/// 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 {
+ var parsed = try dump.readZon(p.gpa, bytes, "adopt");
+ defer parsed.deinit();
+ const st = parsed.value;
+ const carried = st.carried orelse return error.NotCarried;
+ if (st.columns.len != 1 or st.columns[0].panes.len == 0) return error.NotCarried;
+ const order = st.columns[0].panes;
+ for (order) |i| if (i >= st.panes.len or st.panes[i].kind != .file) return error.NotText;
+ // Where the pointer is, else the middle of the pane with the keyboard.
+ layout.compute(p);
+ const here = p.rects[p.active];
+ const x = if (p.pointer_inside) p.hover_col else here.x + here.w / 2;
+ const y = if (p.pointer_inside) p.hover_row else here.y + here.h / 2;
+ var slots: [MAX_PANES]usize = undefined;
+ var n: usize = 0;
+ errdefer for (slots[0..n]) |slot| p.removePane(slot, null) catch {};
+ for (order) |i| {
+ const slot = p.freeSlot() orelse return error.TooManyPanes;
+ const src = st.panes[i];
+ const pane = try panes.File.restore(p, slot, src);
+ slots[n] = slot;
+ n += 1;
+ tagline.restoreDumpTail(p, pane, src);
+ pane.collapsed = false;
+ }
+ // An attachment names its owner by its place in the dump.
+ for (order, 0..) |i, k| {
+ const owner = st.panes[i].attached_to orelse continue;
+ const at = std.mem.indexOfScalar(usize, order, owner) orelse continue;
+ p.panes[slots[k]].?.attached_to = p.panes[slots[at]].?.serial;
+ }
+ const tc = layout.targetColumn(p, x);
+ var heights: [MAX_PANES]u16 = @splat(0);
+ if (carried == .column or p.ncol == 0) {
+ const c = if (p.ncol == 0) layout.insertColumn(p, 0, false) else layout.insertColumn(p, tc, x < p.col_x[tc] + p.col_w[tc] / 2);
+ const col = c orelse return error.NoRoom;
+ if (carried == .column) if (st.columns[0].tag) |tag| {
+ p.col_tags[col].own = try p.gpa.dupe(u8, tag);
+ };
+ for (slots[0..n], 0..) |slot, k| {
+ layout.insert(p, col, k, slot);
+ p.panes[slot].?.vweight = st.panes[order[k]].vweight;
+ }
+ p.active = slots[0];
+ p.setActiveColumn(col);
+ p.sync();
+ return slots[0];
+ }
+ for (0..p.ncol) |c| for (p.col_panes[c][0..p.col_n[c]]) |id| {
+ heights[id] = p.rects[id].h;
+ };
+ // The group under the pointer (a pane with what is attached to it): the
+ // new panes go under all of it, out of its owner's rows.
+ var after: usize = p.col_n[tc];
+ var root: ?usize = null;
+ var k: usize = 0;
+ while (k < p.col_n[tc]) {
+ const end = layout.groupEnd(p, tc, k);
+ const first = p.col_panes[tc][k];
+ const last = p.col_panes[tc][end - 1];
+ root = first;
+ after = end;
+ if (y < p.rects[last].y + p.rects[last].h) break;
+ k = end;
+ }
+ if (root) |owner| {
+ const r = p.rects[owner];
+ // Down to the pointer's row; let go over one of its attachments,
+ // half of it.
+ const keeps = @max(layout.minRows(p, owner), if (y >= r.y and y < r.y + r.h) (y - r.y) + 1 else r.h / 2);
+ const give = heights[owner] -| keeps;
+ if (give < 1 + layout.min_body_rows) return error.NoRoom;
+ heights[owner] = keeps;
+ // The carried share what was given as they shared their old rows.
+ var total: f32 = 0;
+ for (order) |i| total += st.panes[i].vweight;
+ var left = give;
+ for (slots[0..n], 0..) |slot, j| {
+ const share: u16 = if (j + 1 == n) left else @intFromFloat(@max(1, @floor(@as(f32, @floatFromInt(give)) * st.panes[order[j]].vweight / @max(total, 1))));
+ heights[slot] = @min(share, left);
+ left -|= share;
+ }
+ } else for (slots[0..n]) |slot| {
+ heights[slot] = 1;
+ }
+ for (slots[0..n], 0..) |slot, j| layout.insert(p, tc, after + j, slot);
+ layout.setColumnWeights(p, tc, &heights);
+ p.active = slots[0];
+ p.setActiveColumn(tc);
+ p.sync();
+ return slots[0];
+}
+
// ---- the host's half: a worker that carries the dump ----
/// A handover for a worker: the dump's file, and the window's geometry for
@@ -142,6 +249,10 @@ pub const Job = struct {
/// the words are borrowed for the call.
post: *const fn (ctx: ?*anyopaque, seq: u32, ok: bool, words: []const u8) void,
post_ctx: ?*anyopaque,
+ /// This window's 9P socket, which its registry entry links to: the one
+ /// window never asked whether the pointer is over it.
+ own: [512]u8 = undefined,
+ own_len: usize = 0,
};
/// The file a handover's dump goes in: the runtime directory, the user's
@@ -166,6 +277,20 @@ fn carry(job: *Job) void {
job.gpa.destroy(job);
}
var words: [256]u8 = undefined;
+ // Let go over another window: it takes the dump in, where the pointer
+ // is, before its `adopt` answers. A refusal (no room there, too many
+ // panes) leaves the pane here: no new window instead, the user aimed.
+ var dial_buf: [512]u8 = undefined;
+ if (windowUnderPointer(job, &dial_buf)) |dial| {
+ var line_buf: [600]u8 = undefined;
+ const line = std.fmt.bufPrint(&line_buf, "adopt {s}\n", .{job.path}) catch return job.post(job.post_ctx, job.seq, false, "its dump's name is too long");
+ const name = std.fs.path.basename(dial);
+ if (ninep_io.Client.write(job.gpa, dial, "ctl", line)) {
+ return job.post(job.post_ctx, job.seq, true, std.fmt.bufPrint(&words, "pardes/{s}", .{name}) catch "another window");
+ } else |err| return job.post(job.post_ctx, job.seq, false, std.fmt.bufPrint(&words, "pardes/{s} would not take it: {s}", .{
+ name, if (err == error.Remote) ninep_io.Client.lastSaid() else @errorName(err),
+ }) catch "the window would not take it");
+ }
const pid = spawnWindow(job) catch |err| return job.post(job.post_ctx, job.seq, false, std.fmt.bufPrint(&words, "no new window: {s}", .{@errorName(err)}) catch "no new window");
// Up when its registry entry answers: it read the dump before it
// listened (gui.localSession), so the file is no longer needed.
@@ -186,6 +311,43 @@ fn carry(job: *Job) void {
job.post(job.post_ctx, job.seq, false, "the new window did not come up in 15 s");
}
+extern "c" fn readdir(dir: *libc.DIR) ?*libc.dirent;
+
+/// The registry entry of the window the pointer was let go over, asked of
+/// every window by its `status` (`pointer in`): Wayland tells no client
+/// where any window is, only whether the pointer is over its own, and the
+/// window let go over learns it a moment after the release (the grab ends,
+/// then it is entered), so it is asked a few times. None answering with a
+/// pointer line at all (no other window, or an older pardes) ends it at once.
+fn windowUnderPointer(job: *Job, out: *[512]u8) ?[]const u8 {
+ const run = std.mem.span(libc.getenv("XDG_RUNTIME_DIR") orelse return null);
+ var reg_buf: [512:0]u8 = undefined;
+ const reg = std.fmt.bufPrintSentinel(&reg_buf, "{s}/9p/pardes", .{run}, 0) catch return null;
+ const own = job.own[0..job.own_len];
+ var round: u32 = 0;
+ while (round < 10) : (round += 1) {
+ if (round > 0) sleepMs(40);
+ const dir = libc.opendir(reg) orelse return null;
+ defer _ = libc.closedir(dir);
+ var windows: usize = 0;
+ while (readdir(dir)) |ent| {
+ const name = std.mem.sliceTo(&ent.name, 0);
+ if (name.len == 0 or name[0] == '.') continue;
+ const entry = std.fmt.bufPrintSentinel(out, "{s}/{s}", .{ reg, name }, 0) catch continue;
+ var link: [512]u8 = undefined;
+ const n = libc.readlink(entry, &link, link.len);
+ if (n > 0 and std.mem.eql(u8, link[0..@intCast(n)], own)) continue;
+ const status = ninep_io.Client.readLimit(job.gpa, entry, "status", "status", 256) catch continue;
+ defer job.gpa.free(status);
+ if (std.mem.indexOf(u8, status, "\npointer ") == null) continue;
+ windows += 1;
+ if (std.mem.indexOf(u8, status, "\npointer in\n") != null) return entry;
+ }
+ if (windows == 0) return null;
+ }
+ return null;
+}
+
fn sleepMs(ms: u32) void {
var ts: libc.timespec = .{ .sec = 0, .nsec = @as(isize, ms) * std.time.ns_per_ms };
_ = libc.nanosleep(&ts, null);
@@ -305,3 +467,76 @@ test "a terminal's grip let go past the edge stays: its shell is this window's"
try std.testing.expect(!p.handover.flying);
try std.testing.expect(p.panes[id] != null);
}
+
+test "a pane let go over another window goes under the pane the pointer is over, out of its rows alone, unsaved text and all" {
+ const gpa = std.testing.allocator;
+ const from = try twoPanes(gpa);
+ defer from.deinit();
+ _ = pardes.ctlfs.pane.spliceBody(from, from.panes[0].?, 0, 0, "unsaved ").?;
+ releaseOutside(from, 0);
+ const to = try twoPanes(gpa);
+ defer to.deinit();
+ to.sync();
+ const before = to.rects;
+ const c = layout.findPane(to, 0).?.col;
+ const upper = to.col_panes[c][0];
+ const lower = to.col_panes[c][1];
+ // The pointer over the upper pane, six rows into it.
+ to.pointer_inside = true;
+ to.hover_col = to.rects[upper].x + 5;
+ to.hover_row = to.rects[upper].y + 6;
+ const id = try adopt(to, from.handover.bytes.?);
+ to.sync();
+ const pane = to.panes[id].?;
+ try std.testing.expectEqualStrings("unsaved alpha\nbeta\n", pane.file.?.content);
+ try std.testing.expect(tagline.dirty(pane));
+ try std.testing.expectEqual(upper, to.col_panes[c][0]);
+ try std.testing.expectEqual(id, to.col_panes[c][1]);
+ try std.testing.expectEqual(lower, to.col_panes[c][2]);
+ // The pane under the pointer keeps its top and gives its rows below
+ // the pointer; the new one is just there; every other pane is where it was.
+ try std.testing.expectEqual(before[upper].y, to.rects[upper].y);
+ try std.testing.expectEqual(@as(u16, 7), to.rects[upper].h);
+ try std.testing.expectEqual(before[upper].y + 7, to.rects[id].y);
+ try std.testing.expectEqual(before[upper].h - 7, to.rects[id].h);
+ for (to.panes, 0..) |slot, k| if (slot != null and k != upper and k != id)
+ try std.testing.expectEqual(before[k], to.rects[k]);
+ try std.testing.expectEqual(id, to.active);
+}
+
+test "a column let go over another window goes beside the column the pointer is over, out of its width alone" {
+ const gpa = std.testing.allocator;
+ const from = try twoPanes(gpa);
+ defer from.deinit();
+ const r = from.rects[0];
+ const c = layout.findPane(from, 0).?.col;
+ from.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = from.col_x[c], .row = from.topBarHeight() } });
+ from.update(.{ .mouse = .{ .button = .left, .kind = .drag, .col = from.screen_w - 1, .row = r.y } });
+ from.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = from.screen_w - 1, .row = r.y, .outside = true } });
+ try std.testing.expect(from.handover.flying);
+ try std.testing.expectEqual(dump.Carried.column, from.handover.carried);
+ const to = try Pardes.init(gpa, .{ .tty_only = true, .cols = 120, .rows = 30 });
+ defer to.deinit();
+ _ = try to.setTestFile("x\n");
+ try std.testing.expect(to.executeBuiltinLine(to.active, "Newcol"));
+ to.sync();
+ const ncol = to.ncol;
+ const widths = to.col_w;
+ const xs = to.col_x;
+ // Over the right half of the last column: the new one goes after it.
+ to.pointer_inside = true;
+ to.hover_col = to.col_x[ncol - 1] + to.col_w[ncol - 1] - 2;
+ to.hover_row = 10;
+ const id = try adopt(to, from.handover.bytes.?);
+ to.sync();
+ try std.testing.expectEqual(ncol + 1, to.ncol);
+ try std.testing.expectEqual(ncol, layout.findPane(to, id).?.col);
+ try std.testing.expectEqual(@as(usize, 2), to.col_n[ncol]);
+ for (0..ncol - 1) |k| {
+ try std.testing.expectEqual(widths[k], to.col_w[k]);
+ try std.testing.expectEqual(xs[k], to.col_x[k]);
+ }
+ // The finish here: both panes and their column gone from the window.
+ from.update(.{ .handed = .{ .seq = from.handover.seq, .to = "pardes/2" } });
+ try std.testing.expect(from.ncol == 1 or from.col_n[c] == 0 or layout.findPane(from, 0) == null);
+}
diff --git a/src/gui/gui.zig b/src/gui/gui.zig
index 39eedf89..5b80b342 100644
--- a/src/gui/gui.zig
+++ b/src/gui/gui.zig
@@ -2251,6 +2251,7 @@ fn localSession(
defer core.deinit();
g.hit_core = core;
defer g.hit_core = null;
+ core.windowed = true;
core.native_images = true;
core.native_image_patches = true;
observeGuiFont(g, core);
@@ -2386,6 +2387,7 @@ fn localSession(
nc.native_image_patches = true;
nc.host = host;
if (fs) |f| f.reset(nc);
+ nc.windowed = true;
core.deinit();
core = nc;
g.hit_core = nc;
@@ -4604,6 +4606,10 @@ fn handOver(ctx: ?*anyopaque, seq: u32, bytes: []const u8) void {
return s.core.update(.{ .handed = .{ .seq = seq, .failure = "out of memory" } });
};
job.* = .{ .gpa = s.gpa, .seq = seq, .path = path, .post = Queue.pushHanded, .post_ctx = s.queue };
+ if (s.fs) |f| {
+ job.own_len = @min(f.path().len, job.own.len);
+ @memcpy(job.own[0..job.own_len], f.path()[0..job.own_len]);
+ }
if (s.gui) |g| {
var w: c_int = 0;
var h: c_int = 0;
diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig
index 258ea1ee..d9bd4372 100644
--- a/src/ninep/ctl.zig
+++ b/src/ninep/ctl.zig
@@ -380,10 +380,14 @@ fn statusText(p: *Pardes, buf: []u8) []const u8 {
for (p.panes) |slot| count += @intFromBool(slot != null);
const pid: u32 = if (comptime pardes.hosted) @intCast(std.c.getpid()) else 0;
w.print("pid {d}\nversion {s}\npanes {d}\n", .{ pid, pardes.version, count }) catch {};
+ // Whether the pointer is over this window: what a grip let go past
+ // another window's edge asks of every window, to find the one it was
+ // let go over (Handover.zig), where no window may know another's place.
+ if (p.windowed) w.print("pointer {s}\n", .{if (p.pointer_inside) "in" else "out"}) catch {};
return w.buffered();
}
-pub const status_text_cap = 64 + pardes.version.len;
+pub const status_text_cap = 80 + pardes.version.len;
pub fn statusLen(p: *Pardes) u64 {
var buf: [status_text_cap]u8 = undefined;
@@ -804,6 +808,19 @@ pub fn writeRoot(p: *Pardes, req: Req) Reply {
}
continue;
}
+ // `adopt <dump>`: a pane or column another window's grip carried
+ // here (Handover.zig), taken in where the pointer is. A window's
+ // word to a window, as `size` is a frontend's: no builtin.
+ if (std.mem.startsWith(u8, line, "adopt ")) {
+ const path = std.mem.trim(u8, line["adopt".len..], " \t");
+ if (!apply) continue;
+ // ponytail: read on the 9P task, a small local file; a big
+ // one would want pardes.turn's yield.
+ const bytes = fs.readFile(p.gpa, path) catch |err| return refuseQuoting(p, req, @errorName(err), path);
+ defer p.gpa.free(bytes);
+ _ = pardes.Handover.adopt(p, bytes) catch |err| return refuseQuoting(p, req, @errorName(err), path);
+ continue;
+ }
if (!apply) {
if (checkBuiltin(p, req, line, .session)) |refusal| return refusal;
continue;
@@ -1731,6 +1748,12 @@ test "status reads the editor, and look and exec validate whole batches" {
for (p.panes) |slot| live += @intFromBool(slot != null);
try testing.expectEqualStrings(try std.fmt.bufPrint(&expected, "panes {d}", .{live}), lines.next().?);
try testing.expect(lines.next() == null);
+ // A window says whether the pointer is over it.
+ p.windowed = true;
+ var again = std.mem.tokenizeScalar(u8, rd(p, root_status, 0, 4096).bytes, '\n');
+ for (0..3) |_| _ = again.next();
+ try testing.expectEqualStrings("pointer out", again.next().?);
+ p.windowed = false;
const stat = call(p, .{ .tag = 1, .op = .getattr, .node = root_status });
try testing.expectEqual(@as(u64, status.bytes.len), stat.reply.attr.size);
try testing.expectEqual(@as(u16, 0o444), stat.reply.attr.mode);
diff --git a/src/pardes.zig b/src/pardes.zig
index 99231a57..1b1355ef 100644
--- a/src/pardes.zig
+++ b/src/pardes.zig
@@ -4531,6 +4531,10 @@ pub const Pardes = struct {
pointer_tag_hit: ?TagHit = null,
raw_hover_intent: bool = false,
pointer_inside: bool = false,
+ /// A pixel shell's window, which hears the pointer leave it: only then
+ /// does /status say whether the pointer is over it (a terminal's mouse
+ /// reports never say it left, so a tty would claim it for ever).
+ windowed: bool = false,
/// The last plain B1 click, for acme's double-click (text.c:881,
/// textselect: a second click at the same point within 500 ms).
last_click: ?struct { id: usize, on_tag: bool, row: i32, col: i32, ms: i64 } = null,