diff --git a/compositor/src/state.rs b/compositor/src/state.rs index f0307b4..8e37e03 100644 --- a/compositor/src/state.rs +++ b/compositor/src/state.rs @@ -1376,7 +1376,16 @@ impl Ferese { }; rect } - Some(WindowPlacement::Floating { rect }) => rect, + Some(WindowPlacement::Floating { rect }) => { + let constrained = constrained_floating_rect( + rect, + constraints.get(id).copied().unwrap_or_default(), + ); + if constrained != rect { + let _ = self.workspaces.set_floating_rect(*id, constrained); + } + constrained + } None => continue, } }; @@ -1992,6 +2001,55 @@ impl Ferese { self.relayout(); } } + // A normal floating client may choose a different size (minimum sizes, + // terminal cell grids, or a dialog changing its contents). Its committed + // window geometry is authoritative once the latest configure is committed. + // Never let an old buffer undo a newer resize or a fullscreen transition. + let settled_configure = window.toplevel().is_some_and(|toplevel| { + with_states(toplevel.wl_surface(), |states| { + states + .data_map + .get::() + .is_some_and(|data| { + let Ok(data) = data.lock() else { return false }; + floating_commit_is_current( + data.pending_configures().is_empty(), + data.current_serial, + data.configure_serial, + data.current.states.contains(xdg_toplevel::State::Resizing), + ) + }) + }) + }); + let fullscreen = self + .workspaces + .workspace_for_window(id) + .and_then(|workspace| self.workspaces.workspace(workspace)) + .is_some_and(|workspace| workspace.fullscreen == Some(id)); + if settled_configure + && !fullscreen + && !self.maximized_windows.contains(&id) + && self + .window_geometry + .get(&id) + .is_some_and(|geometry| !geometry.is_zooming()) + && let Some(WindowPlacement::Floating { rect }) = self.workspaces.placement(id) + && ClientSize::from_rect(rect) != size + { + let rect = Rect::new( + rect.x, + rect.y, + f64::from(size.width), + f64::from(size.height), + ); + if self.workspaces.set_floating_rect(id, rect).is_ok() { + self.window_geometry + .insert(id, WindowGeometry::new(rect, Some(size))); + self.resize_transactions.remove(&id); + self.resize_snapshots.remove(&id); + self.relayout(); + } + } let Some(geometry) = self.window_geometry.get_mut(&id) else { return; }; @@ -2707,6 +2765,38 @@ fn centered_floating_rect(bounds: Rect) -> Rect { ) } +fn floating_commit_is_current( + no_pending_configures: bool, + committed: Option, + acknowledged: Option, + resizing: bool, +) -> bool { + no_pending_configures && committed.is_some() && committed == acknowledged && !resizing +} + +fn constrained_floating_rect(rect: Rect, constraints: SizeConstraints) -> Rect { + let min_width = constraints.min_width.max(1.0); + let min_height = constraints.min_height.max(1.0); + Rect::new( + rect.x, + rect.y, + rect.width.clamp( + min_width, + constraints + .max_width + .unwrap_or(f64::INFINITY) + .max(min_width), + ), + rect.height.clamp( + min_height, + constraints + .max_height + .unwrap_or(f64::INFINITY) + .max(min_height), + ), + ) +} + fn natural_floating_rect(bounds: Rect, size: ClientSize) -> Rect { let width = f64::from(size.width).clamp(1.0, bounds.width.max(1.0)); let height = f64::from(size.height).clamp(1.0, bounds.height.max(1.0)); @@ -2786,6 +2876,44 @@ impl ClientData for ClientState { #[cfg(test)] mod tests { + #[test] + fn floating_sizes_respect_client_limits_without_moving_the_window() { + let rect = Rect::new(40., 60., 300., 200.); + let constraints = SizeConstraints { + min_width: 640., + min_height: 480., + max_width: Some(900.), + max_height: Some(700.), + }; + assert_eq!( + constrained_floating_rect(rect, constraints), + Rect::new(40., 60., 640., 480.) + ); + assert_eq!( + constrained_floating_rect(Rect::new(40., 60., 1000., 800.), constraints), + Rect::new(40., 60., 900., 700.) + ); + assert_eq!( + constrained_floating_rect(rect, SizeConstraints::default()), + rect + ); + } + + #[test] + fn floating_commits_cannot_undo_pending_or_interactive_resizes() { + let serial = Some(12.into()); + assert!(floating_commit_is_current(true, serial, serial, false)); + assert!(!floating_commit_is_current(false, serial, serial, false)); + assert!(!floating_commit_is_current( + true, + Some(11.into()), + serial, + false + )); + assert!(!floating_commit_is_current(true, serial, serial, true)); + assert!(!floating_commit_is_current(true, None, None, false)); + } + #[test] fn display_reposition_moves_floats_with_their_output_and_clamps_after_shrinking() { let old = ferese_layout::Rect::new(0., 0., 1920., 1080.); diff --git a/scripts/tests/fixtures/floating-size.c b/scripts/tests/fixtures/floating-size.c new file mode 100644 index 0000000..241c86e --- /dev/null +++ b/scripts/tests/fixtures/floating-size.c @@ -0,0 +1,81 @@ +/* A floating client which chooses a larger window than the configure suggests. */ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include "xdg-shell-client-protocol.h" + +static struct wl_compositor *compositor; +static struct wl_shm *shm; +static struct xdg_wm_base *wm; +static struct wl_surface *surface; +static struct wl_buffer *buffer; +static void ping(void *data, struct xdg_wm_base *base, uint32_t serial) { + (void)data; + xdg_wm_base_pong(base, serial); +} +static const struct xdg_wm_base_listener wm_listener = {.ping = ping}; +static void global(void *data, struct wl_registry *registry, uint32_t name, + const char *interface, uint32_t version) { + (void)data; (void)version; + if (!strcmp(interface, "wl_compositor")) + compositor = wl_registry_bind(registry, name, &wl_compositor_interface, 4); + else if (!strcmp(interface, "wl_shm")) + shm = wl_registry_bind(registry, name, &wl_shm_interface, 1); + else if (!strcmp(interface, "xdg_wm_base")) { + wm = wl_registry_bind(registry, name, &xdg_wm_base_interface, 1); + xdg_wm_base_add_listener(wm, &wm_listener, NULL); + } +} +static void removed(void *data, struct wl_registry *registry, uint32_t name) { + (void)data; (void)registry; (void)name; +} +static const struct wl_registry_listener registry_listener = {global, removed}; +static void configure(void *data, struct xdg_surface *xdg, uint32_t serial) { + (void)data; + xdg_surface_ack_configure(xdg, serial); + xdg_surface_set_window_geometry(xdg, 0, 0, 640, 480); + wl_surface_attach(surface, buffer, 0, 0); + wl_surface_damage(surface, 0, 0, 640, 480); + wl_surface_commit(surface); +} +static const struct xdg_surface_listener surface_listener = {.configure = configure}; +static void size(void *data, struct xdg_toplevel *top, int32_t w, int32_t h, + struct wl_array *states) { + (void)data; (void)top; (void)w; (void)h; (void)states; +} +static void close_window(void *data, struct xdg_toplevel *top) { + (void)data; (void)top; exit(0); +} +static const struct xdg_toplevel_listener top_listener = {.configure = size, .close = close_window}; +int main(void) { + struct wl_display *display = wl_display_connect(NULL); + if (!display) return 1; + struct wl_registry *registry = wl_display_get_registry(display); + wl_registry_add_listener(registry, ®istry_listener, NULL); + if (wl_display_roundtrip(display) < 0 || !compositor || !shm || !wm) return 2; + int fd = memfd_create("floating-size-test", MFD_CLOEXEC); + size_t bytes = 640 * 480 * 4; + if (fd < 0 || ftruncate(fd, bytes)) return 3; + uint32_t *pixels = mmap(NULL, bytes, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (pixels == MAP_FAILED) return 4; + for (size_t i = 0; i < 640 * 480; i++) pixels[i] = 0xff12ed34; + struct wl_shm_pool *pool = wl_shm_create_pool(shm, fd, bytes); + buffer = wl_shm_pool_create_buffer(pool, 0, 640, 480, 640 * 4, WL_SHM_FORMAT_XRGB8888); + wl_shm_pool_destroy(pool); + close(fd); + surface = wl_compositor_create_surface(compositor); + struct xdg_surface *xdg = xdg_wm_base_get_xdg_surface(wm, surface); + xdg_surface_add_listener(xdg, &surface_listener, NULL); + struct xdg_toplevel *top = xdg_surface_get_toplevel(xdg); + xdg_toplevel_add_listener(top, &top_listener, NULL); + xdg_toplevel_set_app_id(top, "ferese.test.floating-size"); + xdg_toplevel_set_title(top, "Floating size regression"); + wl_surface_commit(surface); + while (wl_display_dispatch(display) != -1) {} + return 0; +} diff --git a/scripts/tests/test_floating_isolated.py b/scripts/tests/test_floating_isolated.py new file mode 100644 index 0000000..bead2cc --- /dev/null +++ b/scripts/tests/test_floating_isolated.py @@ -0,0 +1,85 @@ +"""Opt-in rendered regression test; opens only a temporary nested compositor. +FERESE_TEST_FLOATING=1 FERESE_TEST_BINARY=target/debug/ferese python3 scripts/tests/test_floating_isolated.py +Requires cc, pkg-config, wayland-scanner, wayland-protocols, grim, and Pillow. +""" +import os +from pathlib import Path +import subprocess +import tempfile +import time +import unittest + + +@unittest.skipUnless(os.environ.get("FERESE_TEST_FLOATING") == "1", "nested render test is opt-in") +class FloatingSizeTest(unittest.TestCase): + def test_committed_content_is_not_cropped_to_requested_size(self): + from PIL import Image, ImageChops + repo = Path(__file__).resolve().parents[2] + with tempfile.TemporaryDirectory(prefix="ferese-floating-test-") as tmp: + root = Path(tmp) + protocols = subprocess.check_output( + ["pkg-config", "--variable=pkgdatadir", "wayland-protocols"], text=True + ).strip() + xml = str(Path(protocols) / "stable/xdg-shell/xdg-shell.xml") + for kind, output in [("client-header", "xdg-shell-client-protocol.h"), + ("private-code", "xdg-shell-protocol.c")]: + subprocess.run(["wayland-scanner", kind, xml, str(root / output)], check=True) + flags = subprocess.check_output(["pkg-config", "--cflags", "--libs", "wayland-client"], text=True).split() + subprocess.run(["cc", "-Wall", "-Wextra", "-I", str(root), + str(repo / "scripts/tests/fixtures/floating-size.c"), + str(root / "xdg-shell-protocol.c"), "-o", str(root / "client"), *flags], check=True) + runtime = root / "runtime" + runtime.mkdir(mode=0o700) + config = root / "config/ferese" + config.mkdir(parents=True) + (config / "config.kdl").write_text( + 'window-rule app-id="ferese.test.floating-size" floating=#true width=320 height=240\n' + 'theme {\n geometry {\n border-width 0\n focus-ring-width 0\n window-radius 0\n }\n}\n' + ) + display = os.environ["WAYLAND_DISPLAY"] + if not display.startswith("/"): + display = str(Path(os.environ["XDG_RUNTIME_DIR"]) / display) + env = dict(os.environ, XDG_RUNTIME_DIR=str(runtime), XDG_CONFIG_HOME=str(root / "config"), + WAYLAND_DISPLAY=display, FERESE_ENABLE_SCREENCOPY="1") + binary = repo / os.environ.get("FERESE_TEST_BINARY", "target/debug/ferese") + with (root / "compositor.log").open("w") as log: + process = subprocess.Popen([str(binary), "--backend", "nested", "--", str(root / "client")], + env=env, stdout=log, stderr=subprocess.STDOUT) + try: + for _ in range(100): + if process.poll() is not None: + self.fail((root / "compositor.log").read_text()) + sockets = [p for p in runtime.glob("wayland-*") if not p.name.endswith(".lock")] + if sockets: + break + time.sleep(.1) + self.assertTrue(sockets, "nested Wayland socket did not appear") + childenv = dict(env, WAYLAND_DISPLAY=str(sockets[0])) + time.sleep(2) + screenshot = root / "frame.png" + subprocess.run(["grim", "-s", "1", str(screenshot)], env=childenv, check=True, timeout=15) + with Image.open(screenshot) as frame: + rgb = frame.convert("RGB") + red, green, blue = rgb.split() + mask = ImageChops.multiply( + ImageChops.multiply(red.point(lambda v: 255 if v < 60 else 0), + green.point(lambda v: 255 if v > 180 else 0)), + blue.point(lambda v: 255 if v < 100 else 0), + ) + bounds = mask.getbbox() + self.assertIsNotNone(bounds, "client content was not rendered") + # The shader antialiases the outermost pixel even at radius zero. + for actual, expected in zip((bounds[2] - bounds[0], bounds[3] - bounds[1]), (640, 480)): + self.assertTrue(expected - 2 <= actual <= expected, + f"floating content cropped: {bounds}, expected 640x480") + finally: + process.terminate() + try: + process.wait(timeout=5) + except subprocess.TimeoutExpired: + process.kill() + process.wait() + + +if __name__ == "__main__": + unittest.main()