RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process
Implement coupled manual half-tile resizing
Mel77102fd49375
diff --git a/src/Input.zig b/src/Input.zigindex 0fe1cc5..6fd73e6 100644--- a/src/Input.zig+++ b/src/Input.zig@@ -70,6 +70,17 @@ pub const PanSession = struct {inhibited: bool = false,};+/// The other half of a manually tiled pair being resized together.+pub const ResizePeerSession = struct {+ toplevel: *Toplevel,+ snapshot: resize.ResizeSnapshot,+ latest_desired_size: resize.ClientSize,+ dirty: bool = false,+ outstanding_configure: Toplevel.ConfigureWait = .none,+ last_sent_width: i32 = 0,+ last_sent_height: i32 = 0,+};+pub const ResizeSession = struct {toplevel: *Toplevel,generation: u64,@@ -78,12 +89,27 @@ pub const ResizeSession = struct {edges: resize.Edges,snapshot: resize.ResizeSnapshot,latest_desired_size: resize.ClientSize,+ peer: ?ResizePeerSession = null,dirty: bool = false,pacing_output: ?*Output = null,outstanding_configure: Toplevel.ConfigureWait = .none,last_sent_width: i32 = 0,last_sent_height: i32 = 0,settling: bool = false,++ /// Whether a toplevel participates in this resize gesture.+ pub fn includes(self: *const ResizeSession, toplevel: *const Toplevel) bool {+ if (self.toplevel == toplevel) return true;+ const peer = self.peer orelse return false;+ return peer.toplevel == toplevel;+ }++ /// Whether either participant is still waiting for its client configure.+ pub fn hasOutstandingConfigure(self: *const ResizeSession) bool {+ if (self.outstanding_configure != .none) return true;+ const peer = self.peer orelse return false;+ return peer.outstanding_configure != .none;+ }};server: *Server,@@ -491,7 +517,7 @@ pub fn detachToplevel(input: *Input, toplevel: *Toplevel) void {input.drag_dodge.detach(toplevel);if (input.grabbed_toplevel == toplevel) input.clearGrab();if (input.resize_session) |session| {- if (session.toplevel == toplevel) input.cancelResize();+ if (session.includes(toplevel)) input.cancelResize();}}diff --git a/src/input/grabs.zig b/src/input/grabs.zigindex 42741b5..0dbad49 100644--- a/src/input/grabs.zig+++ b/src/input/grabs.zig@@ -176,6 +176,78 @@ pub fn heldGlass(input: *const Input) ?*glass.Effect {return toplevel.glass_effect;}+/// Find the opposite manually tiled half sharing this window's inner edge.+/// Only an unambiguous, geometrically adjacent pair is eligible.+fn manualHalfPeer(input: *Input, toplevel: *Toplevel, edges: resize.Edges) ?*Toplevel {+ const tile = toplevel.tile orelse return null;+ const peer_tile: snap.Tile = switch (tile) {+ .left => blk: {+ if (!edges.right or edges.left or edges.top or edges.bottom) return null;+ break :blk .right;+ },+ .right => blk: {+ if (!edges.left or edges.right or edges.top or edges.bottom) return null;+ break :blk .left;+ },+ else => return null,+ };++ if (toplevel.auto_target != null) return null;+ const output = toplevel.currentOutput() orelse return null;+ const target = toplevel.tile_target orelse return null;+ const position = input.server.world.toLayout(+ @floatFromInt(target.x),+ @floatFromInt(target.y),+ );+ const frame = toplevel.chromeSizeForClient(target.width, target.height);+ const frame_width = @as(f64, @floatFromInt(frame.w)) * toplevel.zoom();+ const frame_height = @as(f64, @floatFromInt(frame.h)) * toplevel.zoom();+ const gap: f64 = @floatFromInt(input.server.config.compositor.windowGap());++ var found: ?*Toplevel = null;+ var it = input.server.world.toplevels.iterator(.forward);+ while (it.next()) |candidate| {+ if (candidate == toplevel or+ candidate.tile != peer_tile or+ candidate.tile_target == null or+ candidate.auto_target != null or+ candidate.workspace_id != toplevel.workspace_id or+ !candidate.in_world or+ candidate.minimized or+ candidate.tab_hidden or+ candidate.closing or+ !candidate.isMapped() or+ !@import("../workspaces.zig").visible(candidate) or+ candidate.currentOutput() != output)+ {+ continue;+ }++ const peer_target = candidate.tile_target.?;+ const peer_position = input.server.world.toLayout(+ @floatFromInt(peer_target.x),+ @floatFromInt(peer_target.y),+ );+ const peer_frame = candidate.chromeSizeForClient(peer_target.width, peer_target.height);+ const peer_width = @as(f64, @floatFromInt(peer_frame.w)) * candidate.zoom();+ const peer_height = @as(f64, @floatFromInt(peer_frame.h)) * candidate.zoom();++ const vertical_error = @abs(position.y - peer_position.y);+ const height_error = @abs(frame_height - peer_height);+ const separation = if (tile == .left)+ peer_position.x - (position.x + frame_width)+ else+ position.x - (peer_position.x + peer_width);++ if (vertical_error > 2 or height_error > 2 or @abs(separation - gap) > 2) continue;++ // Never couple an arbitrary window when more than one candidate fits.+ if (found != null) return null;+ found = candidate;+ }+ return found;+}+pub fn startResize(input: *Input,toplevel: *Toplevel,@@ -187,6 +259,7 @@ pub fn startResize(if (@import("../tiling_grab.zig").begin(input, toplevel, wlr_edges, initiating_button)) return;const edges = resize.Edges.fromWlr(wlr_edges);if (edges.isNone()) return;+ const peer = manualHalfPeer(input, toplevel, edges);if (toplevel.minimized ortoplevel.isMaximized() or@@ -200,14 +273,20 @@ pub fn startResize(input.cancelResize();}- // Resizing a tile makes it an ordinary window of the tile's geometry.+ // A paired manual half keeps both tile identities. An ordinary resize+ // still converts the active tile into a floating window.const layout_before = toplevel.layout();const layout_gesture = @import("../layout_undo.zig").beginGesture(input.server, toplevel.workspace_id);- toplevel.leaveLayout();+ if (peer == null) toplevel.leaveLayout();input.server.world.focus(toplevel);toplevel.finishZoomAnimation();toplevel.finishMoveAnimation();toplevel.finishSizeAnimation();+ if (peer) |peer_toplevel| {+ peer_toplevel.finishZoomAnimation();+ peer_toplevel.finishMoveAnimation();+ peer_toplevel.finishSizeAnimation();+ }input.setHover(null, null);input.cursor_mode = .resize;@@ -238,6 +317,36 @@ pub fn startResize(edges,);+ // The peer tracks the same pointer, but resizes its opposite edge:+ // growing one half therefore shrinks the other by the same amount.+ const peer_session: ?Input.ResizePeerSession = if (peer) |peer_toplevel| blk: {+ const peer_geom = peer_toplevel.clientGeometry();+ const peer_target = peer_toplevel.tile_target.?;+ const peer_client_w = if (peer_geom.width > 0) peer_geom.width else peer_target.width;+ const peer_client_h = if (peer_geom.height > 0) peer_geom.height else peer_target.height;+ const peer_edges: resize.Edges = .{+ .left = edges.right,+ .right = edges.left,+ };+ const peer_snapshot = resize.createSnapshot(+ input.server.world.toWorld(input.cursor.x, input.cursor.y).x / peer_toplevel.worldScale(),+ input.server.world.toWorld(input.cursor.x, input.cursor.y).y / peer_toplevel.worldScale(),+ peer_toplevel.x,+ peer_toplevel.y,+ peer_client_w,+ peer_client_h,+ peer_toplevel.titlebarHeight(),+ peer_toplevel.borderWidth(),+ peer_toplevel.footerHeight(),+ peer_edges,+ );+ break :blk .{+ .toplevel = peer_toplevel,+ .snapshot = peer_snapshot,+ .latest_desired_size = .{ .width = peer_client_w, .height = peer_client_h },+ };+ } else null;+const gen = input.next_session_generation;input.next_session_generation += 1;@@ -251,6 +360,7 @@ pub fn startResize(.edges = edges,.snapshot = snapshot,.latest_desired_size = .{ .width = client_w, .height = client_h },+ .peer = peer_session,.dirty = false,.pacing_output = pacing_output,};@@ -287,12 +397,22 @@ pub fn finishResize(input: *Input) void {// pending. Otherwise dirty is cleared here, so flushResizeConfigure ignores// a settling session (it used to return early on `settling`).session.dirty = !std.meta.eql(shown, final_size);- if (!session.dirty) {+ if (!session.dirty and session.outstanding_configure == .none) {session.outstanding_configure = toplevel.requestSize(final_size.width, final_size.height);session.last_sent_width = final_size.width;session.last_sent_height = final_size.height;}+ if (session.peer) |*peer| {+ if (peer.dirty and peer.outstanding_configure == .none) {+ const peer_size = peer.latest_desired_size;+ peer.outstanding_configure = peer.toplevel.requestSize(peer_size.width, peer_size.height);+ peer.last_sent_width = peer_size.width;+ peer.last_sent_height = peer_size.height;+ peer.dirty = false;+ }+ }+input.cursor_mode = .passthrough;input.grabbed_toplevel = null;session.settling = true;@@ -319,6 +439,7 @@ pub fn finishResize(input: *Input) void {pub fn cancelResize(input: *Input) void {const session = input.resize_session orelse return;const toplevel = session.toplevel;+ const peer = session.peer;@import("../effects/window_move.zig").cancelFor(input.server, toplevel.id);input.resize_session = null;@@ -329,9 +450,19 @@ pub fn cancelResize(input: *Input) void {if (toplevel.isMapped()) {toplevel.setResizing(false);- const geom = toplevel.clientGeometry();- if (geom.width > 0 and geom.height > 0) {- _ = toplevel.requestSize(geom.width, geom.height);+ _ = toplevel.requestSize(+ session.snapshot.initial_client_width,+ session.snapshot.initial_client_height,+ );+ toplevel.setPosition(session.snapshot.initial_frame_x, session.snapshot.initial_frame_y);+ }+ if (peer) |pair| {+ if (pair.toplevel.isMapped()) {+ _ = pair.toplevel.requestSize(+ pair.snapshot.initial_client_width,+ pair.snapshot.initial_client_height,+ );+ pair.toplevel.setPosition(pair.snapshot.initial_frame_x, pair.snapshot.initial_frame_y);}}input.processCursorMotion(0);@@ -340,32 +471,55 @@ pub fn cancelResize(input: *Input) void {pub fn flushResizeConfigure(input: *Input, output: *Output) void {const session = &(input.resize_session orelse return);if (session.pacing_output != null and session.pacing_output != output) return;- if (!session.dirty) return;- if (session.outstanding_configure != .none) return;-const toplevel = session.toplevel;- const desired = @import("../effects/window_move.zig").resizeSize(toplevel, Toplevel.nowMs(), session.latest_desired_size);- const following = !std.meta.eql(desired, session.latest_desired_size);+ if (session.dirty and session.outstanding_configure == .none) {+ const desired = @import("../effects/window_move.zig").resizeSize(+ toplevel,+ Toplevel.nowMs(),+ session.latest_desired_size,+ );+ const following = !std.meta.eql(desired, session.latest_desired_size);++ if (desired.width == session.last_sent_width and desired.height == session.last_sent_height) {+ session.dirty = following;+ } else {+ session.outstanding_configure = toplevel.requestSize(desired.width, desired.height);+ session.last_sent_width = desired.width;+ session.last_sent_height = desired.height;+ session.dirty = following;+ }+ }- if (desired.width == session.last_sent_width and desired.height == session.last_sent_height) {- session.dirty = following;- if (session.settling and !following) input.finishSettlement(session.generation);- return;+ if (session.peer) |*peer| {+ if (peer.dirty and peer.outstanding_configure == .none) {+ const desired = peer.latest_desired_size;+ if (desired.width == peer.last_sent_width and desired.height == peer.last_sent_height) {+ peer.dirty = false;+ } else {+ peer.outstanding_configure = peer.toplevel.requestSize(desired.width, desired.height);+ peer.last_sent_width = desired.width;+ peer.last_sent_height = desired.height;+ peer.dirty = false;+ }+ }}- session.outstanding_configure = toplevel.requestSize(desired.width, desired.height);- session.last_sent_width = desired.width;- session.last_sent_height = desired.height;- session.dirty = following;- if (session.settling and !following and session.outstanding_configure == .none) input.finishSettlement(session.generation);+ const peer_dirty = if (session.peer) |peer| peer.dirty else false;+ if (session.settling and !session.dirty and !peer_dirty and !session.hasOutstandingConfigure()) {+ input.finishSettlement(session.generation);+ }}pub fn finishSettlement(input: *Input, generation: u64) void {if (input.resize_session) |session| {if (session.generation == generation) {const toplevel = session.toplevel;+ const peer = if (session.peer) |pair| pair.toplevel else null;input.resize_session = null;toplevel.syncChrome(false, Toplevel.nowMs()) catch {};+ if (peer) |peer_toplevel| {+ peer_toplevel.syncChrome(false, Toplevel.nowMs()) catch {};+ }}}}diff --git a/src/input/motion.zig b/src/input/motion.zigindex 3816745..e3ed6c0 100644--- a/src/input/motion.zig+++ b/src/input/motion.zig@@ -583,22 +583,42 @@ pub fn processResize(input: *Input) void {if (session.settling) return;const toplevel = session.toplevel;- const constraints = toplevel.sizeConstraints();-- // Use the same unscaled cursor space as the resize snapshot.- const cursor_wx = input.server.world.toWorld(input.cursor.x, input.cursor.y).x / toplevel.worldScale();- const cursor_wy = input.server.world.toWorld(input.cursor.x, input.cursor.y).y / toplevel.worldScale();+ const cursor_world = input.server.world.toWorld(input.cursor.x, input.cursor.y);+ // Each participant measures the same pointer in its own unscaled+ // client space. Their snapshots select opposite edges, so the shared+ // boundary moves in opposite directions while both outer edges stay put.const desired = resize.computeDesiredClientSize(session.snapshot,- cursor_wx,- cursor_wy,- constraints,+ cursor_world.x / toplevel.worldScale(),+ cursor_world.y / toplevel.worldScale(),+ toplevel.sizeConstraints(),);+ var changed = false;if (desired.width != session.latest_desired_size.width or desired.height != session.latest_desired_size.height) {session.latest_desired_size = desired;session.dirty = true;+ changed = true;@import("../effects/window_move.zig").resizeMotion(toplevel);+ }++ if (session.peer) |*peer| {+ const peer_desired = resize.computeDesiredClientSize(+ peer.snapshot,+ cursor_world.x / peer.toplevel.worldScale(),+ cursor_world.y / peer.toplevel.worldScale(),+ peer.toplevel.sizeConstraints(),+ );+ if (peer_desired.width != peer.latest_desired_size.width or+ peer_desired.height != peer.latest_desired_size.height)+ {+ peer.latest_desired_size = peer_desired;+ peer.dirty = true;+ changed = true;+ }+ }++ if (changed) {if (session.pacing_output) |out| {out.wlr_output.scheduleFrame();} else {diff --git a/src/toplevel/info.zig b/src/toplevel/info.zigindex 43d3536..b9be612 100644--- a/src/toplevel/info.zig+++ b/src/toplevel/info.zig@@ -350,7 +350,7 @@ pub fn insideFrame(toplevel: *Toplevel, px: f32, py: f32) bool {pub fn isResizing(toplevel: *Toplevel) bool {const session = toplevel.server.input.resize_session orelse return false;- return session.toplevel == toplevel;+ return session.includes(toplevel);}pub fn isGeometrySettled(toplevel: *Toplevel, now_ms: i64) bool {diff --git a/src/toplevel/lifecycle.zig b/src/toplevel/lifecycle.zigindex e90806f..43f9a8c 100644--- a/src/toplevel/lifecycle.zig+++ b/src/toplevel/lifecycle.zig@@ -43,11 +43,34 @@ pub fn handleSurfaceCommit(toplevel: *Toplevel) void {}// A queued pre-release commit must not discard the fixed-edge- // snapshot before the client acknowledges the final resize size.- if (session.settling and !session.dirty and session.outstanding_configure == .none) {+ // snapshot before both clients acknowledge their final sizes.+ const peer_dirty = if (session.peer) |peer| peer.dirty else false;+ if (session.settling and !session.dirty and !peer_dirty and !session.hasOutstandingConfigure()) {const gen = session.generation;toplevel.server.input.finishSettlement(gen);}+ } else if (session.peer) |*peer| {+ if (peer.toplevel == toplevel) {+ toplevel.positionDuringResize();+ if (peer.outstanding_configure == .x11_commit) {+ peer.outstanding_configure = .none;+ } else if (peer.outstanding_configure != .none) {+ if (toplevel.configureCompleted(peer.outstanding_configure)) {+ peer.outstanding_configure = .none;+ }+ }++ if (peer.dirty and peer.outstanding_configure == .none) {+ if (session.pacing_output) |output| output.wlr_output.scheduleFrame() else toplevel.server.scheduleFrames();+ }++ // The peer has its own protocol configure lifecycle. Its+ // commit must not clear or settle the active window's wait.+ if (session.settling and !session.dirty and !peer.dirty and !session.hasOutstandingConfigure()) {+ const gen = session.generation;+ toplevel.server.input.finishSettlement(gen);+ }+ }}}diff --git a/src/toplevel/zoom.zig b/src/toplevel/zoom.zigindex 856473b..dd281b5 100644--- a/src/toplevel/zoom.zig+++ b/src/toplevel/zoom.zig@@ -191,12 +191,18 @@ pub fn setZoomAnimated(toplevel: *Toplevel, index: usize, lx: f64, ly: f64) void// soon as their size changes too.pub fn positionDuringResize(toplevel: *Toplevel) void {const session = toplevel.server.input.resize_session orelse return;- if (session.toplevel != toplevel) return;+ const snapshot = if (session.toplevel == toplevel)+ session.snapshot+ else blk: {+ const peer = session.peer orelse return;+ if (peer.toplevel != toplevel) return;+ break :blk peer.snapshot;+ };const geom = toplevel.clientGeometry();if (geom.width > 0 and geom.height > 0) {- const pos = resize.computeFramePosition(session.snapshot, geom.width, geom.height);- const initial_x = session.snapshot.initial_frame_x;- const initial_y = session.snapshot.initial_frame_y;+ const pos = resize.computeFramePosition(snapshot, geom.width, geom.height);+ const initial_x = snapshot.initial_frame_x;+ const initial_y = snapshot.initial_frame_y;// The fixed opposite edge is in displayed world coordinates.const dx: f64 = @floatFromInt(pos.x - initial_x);const dy: f64 = @floatFromInt(pos.y - initial_y);