//SPDX-License-Identifier: LGPL-3.0-only //SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® // Issue #199: a size the compositor configures is answered by the frame drawn // at it. The configure listener used to make the swapchain again on the spot, // acknowledge the configure, send the viewport's destination for the new size // and commit the surface. The destination is the surface's size, and that // commit applied it to the buffer still attached: the old frame was shown // scaled to the new size until the consumer's next frame brought a buffer of // that size. The acknowledgement had named the old frame as the answer to // the configure, and the rebuild's Vulkan calls ran, and threw, inside // libwayland's dispatch (#200). // // Now the listener keeps the size and the serial, and Render() does the rest // at its boundaries: RebuildIfDeferred takes the size (TakeDeferredSize) and // makes the swapchain again, and SendFrameSurfaceState sends the destination // and the acknowledgement right before the present, whose commit applies // them with the frame's buffer. The surface's size is state of the window // for that (logicalWidth, logicalHeight): a size the compositor configured // is sent as the destination unchanged, and is not worked back from the // pixels and a scale that may have changed since. This test drives those // pieces without a swapchain: the window is default-constructed with a // made-up swapchain handle, its surfaces are proxies on a wl_display // connected to one end of a socketpair, as in ToplevelState, and the test // calls the listener callbacks itself and reads the requests off the other // end. `Begin`, `Present` and `End` below stand for Render()'s start, its // present and its end after the present; ResizeViewport runs the real one // against a compositor. Asserts: // // - a configure for a new size sends one bare commit: no destination, no // acknowledgement, and no Vulkan call (there is no device here, and the // old listener's rebuild would have crashed on it). The size and the // serial wait, the window's size stays the swapchain's, and onFrameOwed // fires // - the frame takes the size and sends the destination and the // acknowledgement before its present, once // - several configures before a frame cost one rebuild and one // acknowledgement, of the last serial, and a configure without a size // among them waits with the others // - a configure that asks for nothing new while nothing is owed is // acknowledged in the listener, before its commit, and fires nothing // - a configure back to the size the window has takes the wait away: it // is acknowledged at once, and the serial that waited never is, which // would be a protocol error after a later one // - a frame being recorded when a size comes does not acknowledge it // - a window's first frame brings the destination (the constructor sends // none), and so does the first frame at any new size; a configure that // comes before that frame waits for it // - a configure for the size a rebuild that threw is still pending for // waits for the frame as well, and so does one that comes after a // present that found the swapchain out of date, whose surface state is // not sent a second time // - without a viewport the acknowledgement still waits for the frame // - the size is turned into pixels with the scale the compositor has asked // for by the moment the frame takes it, and the destination is the // configured size whatever the scale does: when it changes before the // frame, between the frame's start and its present (a listener that // dispatches events), and when the new size at the new scale comes to // the pixels the swapchain has, in either order of the two events. That // last frame makes no swapchain and still brings the surface its size. A // scale that changes during a frame is taken at the boundary after its // present // - on a fractional scale the destination is the configured size and not a // pixel more, and a window configured at the size it has is left alone // (the numbers of #201's phone trace) // - a configured size's pixels are the size times the scale rounded half // away from zero, as fractional-scale-v1 has it for a toplevel, worked // out exactly from the scale's 120ths: the protocol's own 100x50 at 1.5, // halves that go up, less than halves that go down where rounding up // gave a pixel more, and whole numbers the sum in float came out above. // The pixels are never fewer than one, where the protocol's rounding // gives none: a length of one below a scale of a half; nor more than // the largest 32-bit value, which 2^30 at 4 comes past, where the cast // would wrap them to none. A surface's length worked out from pixels // stops at the largest int32 in the same way. A window the // application sized has its surface's size rounded up from its pixels, // exactly too. Worked back from its pixels, a configured size short // of the 32-bit limits the two sums stop at lands above itself where // its product with the scale has a fraction of a half or more, and, // from a scale of a half up, on itself below that // - a swapchain made for another reason after the scale changed: a window // the compositor sized keeps the surface's size and takes the pixels for // it at the new scale, which it draws at from that swapchain on, the // next frame boundary having nothing left to take; one the application // sized keeps its pixels and takes no scale there, as nothing of that // swapchain needs it, and until the next frame's start takes it a motion // is still at the pixels of the scale before, also where the swapchain // is made after a present that found it out of date // - of a Resize from a listener of the frame and a configured size, the // one asked for last is taken, also when it is the size the window has // - onFrameOwed fires after onToplevelState, with the states set, and not // at all for a window an onToplevelState listener destroyed // - every acknowledgement names a later serial than the one before // // Issue #165: a scale the compositor asks for by itself, with no configure // (a window that goes to an output of another scale). Its listener used to // set Window::scale and nothing else: the buffers and the surface kept the // old scale until something resized the window, while the pointer, the // touches and the caret were converted with the new one. Asserts: // // - the listener sends nothing, leaves `scale` and both sizes as they // are, and fires onFrameOwed; until a frame boundary takes the scale, a // motion or a touch at the surface's middle is at the middle of the // buffers drawn at the scale before, and a caret given in their pixels // goes to the text input converted with that scale too // - the next frame boundary takes it. A window the compositor sized keeps // its surface's size and takes the pixels for it at the new scale, so // the frame makes the swapchain; the surface is sent nothing. One the // application sized keeps its pixels and makes no swapchain, and its // surface takes the size they come to, which the frame sends. From then // the pointer, the touches and the caret are converted with the new // scale, and what the window held in pixels of the scale before is // taken to it: a pointer and a finger that rest are where their // surface-local places come to at the new scale, and the boundary sends // the caret again, converted, with no SetTextInput; an Input::Map // attached to the window and ticked across the boundary reads no delta // for a pointer that rests, nor across two boundaries between two Ticks, // and fires no event there // - input whose listener takes a scale that waits (as a SetPresentMode // outside a frame does for a window the compositor sized, at the new // pixels the scale gives it): the click a finger stands in // for, the finger's move and the move after an enter are in the pixels // of that scale // - a scale that comes while a frame is being recorded leaves that frame // at the scale it began with, and the boundary after its present takes // it only where it gives the window new pixels: a window the compositor // sized whose surface comes to other pixels at it. Where it gives none // (a window the application sized, one the compositor sized that it // leaves at its pixels, a configure that comes with it to the pixels the // window has), it waits, with the configure, for the start of the next // frame, the // one that sends the surface its new size: until then a motion is still // at the pixels of the scale before and the caret is not sent again, as // the compositor maps the surface as before until that frame's commit // - a scale that comes back to the one the window draws at before a frame // owes nothing, and fires nothing // - a configure without a size while a scale waits is acknowledged in the // listener, as the scale itself is answered by no acknowledgement; one // for the size the surface has waits for the frame that takes the // scale, also where its pixels at that scale are the window's, and the // window does not become one the compositor sized before then // - before the window has its first swapchain the listener only keeps the // scale, which the constructor's MatchSizesToScale takes; a scale of 0 // is not taken // // No compositor and no GPU. Needs the Wayland backend, hence Linux only. #include #include #include #include #include #include #include "../../lib/xdg-shell-client-protocol.h" #include "../../lib/viewporter.h" #include "../../lib/text-input-unstable-v3-client-protocol.h" import Crafter.Graphics; import Crafter.Event; import Crafter.Math; import std; using namespace Crafter; namespace { int failures = 0; void Check(bool ok, std::string_view what) { std::println("{} {}", ok ? "PASS" : "FAIL", what); if (!ok) ++failures; } using Requests = std::vector; // `got` against `expected`, printing what came when they differ. void Expect(const Requests& got, const Requests& expected, std::string_view what) { Check(got == expected, what); if (got != expected) { std::println(" sent: {}", got); std::println(" expected: {}", expected); } } Requests Sorted(Requests requests) { std::ranges::sort(requests); return requests; } // Every acknowledgement the test read off the wire, in order. std::vector acknowledged; // A compositor's serials: one counter, never reused. std::uint32_t lastSerial = 100; // The text input whose caret Sent() reports, 0 while there is none. std::uint32_t textInputId = 0; // A window as its constructor leaves it: the first configure taken, a // swapchain made, no frame presented yet. The swapchain is a handle nothing // dereferences and a generation; the surface, the xdg_surface, the toplevel // and the viewport are proxies nothing answers. struct Fixture { wl_display* display; int peer; std::optional holder; std::uint32_t surfaceId = 0; std::uint32_t xdgSurfaceId = 0; std::uint32_t viewportId = 0; int owed = 0; std::optional> owedSub; // `scale` in 120ths, as the compositor names it. Fixture(wl_display* display, wl_registry* registry, int peer, std::uint32_t scale, std::uint32_t width, std::uint32_t height, bool viewport = true) : display(display), peer(peer) { Window& window = holder.emplace(); window.surface = static_cast(wl_registry_bind(registry, 1, &wl_surface_interface, 1)); window.xdgSurface = static_cast(wl_registry_bind(registry, 2, &xdg_surface_interface, 1)); window.xdgToplevel = static_cast(wl_registry_bind(registry, 3, &xdg_toplevel_interface, 1)); if (viewport) { window.wpViewport = static_cast(wl_registry_bind(registry, 4, &wp_viewport_interface, 1)); viewportId = wl_proxy_get_id(reinterpret_cast(window.wpViewport)); } surfaceId = wl_proxy_get_id(reinterpret_cast(window.surface)); xdgSurfaceId = wl_proxy_get_id(reinterpret_cast(window.xdgSurface)); // The scale the compositor named while the constructor waited. window.preferredScale = scale; window.width = width; window.height = height; // The configure the constructor waits for, before there is a swapchain. Configure(0, 0); // The scale the window draws at, and the surface's size, from the // constructor's pixels. window.MatchSizesToScale(true); window.swapChain = reinterpret_cast(static_cast(0x1)); window.swapchainGeneration = 1; // Where the constructor lists it, and where the handler looks for a // window that outlived its listeners. Device::windows.push_back(&window); owedSub.emplace(&window.onFrameOwed, [this] { ++owed; }); Sent(); } ~Fixture() { // ~Window destroys the proxies; without a device it leaves the // swapchain handle alone. holder.reset(); Sent(); } Window& window() { return *holder; } // The compositor's two events of one batch. Returns the serial. std::uint32_t Configure(std::int32_t width, std::int32_t height, std::initializer_list states = {}) { Window& window = *holder; wl_array array; wl_array_init(&array); for (std::uint32_t state : states) { *static_cast(wl_array_add(&array, sizeof(state))) = state; } // The handles before the calls: the second may destroy the window. xdg_surface* const surface = window.xdgSurface; Window::xdg_toplevel_configure(&window, window.xdgToplevel, width, height, &array); wl_array_release(&array); const std::uint32_t serial = ++lastSerial; Window::xdg_surface_handle_configure(&window, surface, serial); return serial; } // What RebuildSwapchain does besides its Vulkan calls: the window's two // sizes at the scale it draws at, which takes a new one only where it // gives the window new pixels, and a swapchain, which CreateSwapchain // numbers, after which none waits to be made. void Rebuild() { Window& window = *holder; window.MatchSizesToScale(false); ++window.swapchainGeneration; window.swapchainRebuildPending = false; } // A frame boundary of Render(), which is RebuildIfDeferred: the size // that waits becomes the window's, and new pixels, or a rebuild that // waited, make a new swapchain. True when one was made. bool Boundary(Window::FrameBoundary boundary) { Window& window = *holder; if (!window.TakeDeferredSize(boundary) && !window.swapchainRebuildPending) return false; Rebuild(); return true; } // The start of Render(). bool Begin() { return Boundary(Window::FrameBoundary::Start); } // The surface state for the frame, then the present, after which the // surface shows this swapchain. void Present() { Window& window = *holder; window.SendFrameSurfaceState(); window.presentedGeneration = window.swapchainGeneration; } // The end of Render(), after the present: what the frame's listeners // asked for while it was being recorded. bool End() { return Boundary(Window::FrameBoundary::AfterPresent); } // A whole frame. void Frame() { Begin(); Present(); End(); } // What the window has sent to its surface, its xdg_surface and its // viewport since the last call, in order: "commit", "ack ", // "destination x", and the caret it gave the text input, "caret // , x". A request is its object id, then its size and // opcode in one word, then its arguments. Requests Sent() { wl_display_flush(display); std::vector wire; std::uint8_t chunk[4096]; for (ssize_t n; (n = recv(peer, chunk, sizeof(chunk), MSG_DONTWAIT)) > 0;) { wire.insert(wire.end(), chunk, chunk + n); } Requests requests; for (std::size_t at = 0; at + 8 <= wire.size();) { std::uint32_t header[2]; std::memcpy(header, wire.data() + at, sizeof(header)); const std::uint32_t size = header[1] >> 16; const std::uint32_t opcode = header[1] & 0xFFFF; if (size < 8 || at + size > wire.size()) break; const auto argument = [&](std::size_t index) { std::int32_t value = 0; if (8 + 4 * (index + 1) <= size) std::memcpy(&value, wire.data() + at + 8 + 4 * index, sizeof(value)); return value; }; if (header[0] == surfaceId && opcode == WL_SURFACE_COMMIT) { requests.push_back("commit"); } else if (header[0] == xdgSurfaceId && opcode == XDG_SURFACE_ACK_CONFIGURE) { requests.push_back(std::format("ack {}", static_cast(argument(0)))); acknowledged.push_back(static_cast(argument(0))); } else if (viewportId != 0 && header[0] == viewportId && opcode == WP_VIEWPORT_SET_DESTINATION) { requests.push_back(std::format("destination {}x{}", argument(0), argument(1))); } else if (textInputId != 0 && header[0] == textInputId && opcode == ZWP_TEXT_INPUT_V3_SET_CURSOR_RECTANGLE) { requests.push_back(std::format("caret {},{} {}x{}", argument(0), argument(1), argument(2), argument(3))); } at += size; } return requests; } }; bool NothingWaits(const Window& window) { return window.deferredLogicalWidth == 0 && window.deferredLogicalHeight == 0 && !window.unackedConfigure && window.presentedGeneration == window.swapchainGeneration && window.sentLogicalWidth == window.logicalWidth && window.sentLogicalHeight == window.logicalHeight && window.scale == window.preferredScale / 120.0f; } // The scale as the compositor names it, in 120ths. void Scale(Window& window, std::uint32_t scale120) { Window::xdg_surface_handle_preferred_scale(&window, nullptr, scale120); } // Where the pointer is in the window's pixels after a motion to (x, y) on // its surface, as the compositor reports it, in surface-local units. Vector Motion(Window& window, double x, double y) { Device::focusedWindow = &window; Device::PointerListenerHandleMotion(nullptr, nullptr, 0, wl_fixed_from_double(x), wl_fixed_from_double(y)); return window.currentMousePos; } // Whether `position` is (x, y), saying where it is when it is not. void CheckAt(Vector position, float x, float y, std::string_view what) { Check(position.x == x && position.y == y, what); if (position.x != x || position.y != y) std::println(" at {},{}, where {},{} was expected", position.x, position.y, x, y); } } // namespace int main() { int fds[2]; if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, fds) != 0) { std::println("FAIL socketpair: {}", std::strerror(errno)); return EXIT_FAILURE; } // The display owns fds[0] from here. wl_display* display = wl_display_connect_to_fd(fds[0]); if (display == nullptr) { std::println("FAIL wl_display_connect_to_fd: {}", std::strerror(errno)); return EXIT_FAILURE; } // libwayland makes whatever proxy it is asked to bind; nothing answers. wl_registry* registry = wl_display_get_registry(display); const auto fixture = [&](std::uint32_t scale, std::uint32_t width, std::uint32_t height, bool viewport = true) { return Fixture(display, registry, fds[1], scale, width, height, viewport); }; // The window's first frame: the constructor sends no destination, the // frame does. Every fixture after this one has it behind it. { Fixture f = fixture(240, 640, 480); f.Present(); Expect(f.Sent(), { "destination 320x240" }, "a window's first frame brings the viewport's destination"); Check(NothingWaits(f.window()), "after it nothing is owed"); f.Frame(); Expect(f.Sent(), {}, "the frames after it send nothing"); } // A new size, and the frame that answers it. { Fixture f = fixture(240, 640, 480); f.Frame(); f.Sent(); Window& window = f.window(); const std::uint32_t serial = f.Configure(400, 300); Expect(f.Sent(), { "commit" }, "a configure for a new size: one bare commit, with neither the surface's new size nor an acknowledgement"); Check(window.width == 640 && window.height == 480, std::format("the window's size is still the swapchain's ({}x{})", window.width, window.height)); Check(window.deferredLogicalWidth == 400 && window.deferredLogicalHeight == 300 && window.unackedConfigure == serial, "the size and the serial wait for a frame"); Check(f.owed == 1, std::format("onFrameOwed fired once ({})", f.owed)); Check(f.Begin(), "the next frame takes the size"); Check(window.width == 800 && window.height == 600 && window.deferredLogicalWidth == 0 && window.deferredLogicalHeight == 0, std::format("the window has it in pixels ({}x{} at scale 2), and no size waits", window.width, window.height)); Expect(f.Sent(), {}, "taking it sends nothing"); f.Present(); Expect(Sorted(f.Sent()), Sorted({ std::format("ack {}", serial), "destination 400x300" }), "before its present the frame sends the destination and the acknowledgement, and does not commit"); Check(NothingWaits(window), "after the frame nothing is owed"); f.Frame(); Expect(f.Sent(), {}, "the frames after it send neither again"); Check(f.owed == 1, "onFrameOwed fired for the configure only"); } // Several configures before a frame. { Fixture f = fixture(120, 320, 240); f.Frame(); f.Sent(); Window& window = f.window(); f.Configure(400, 300); f.Configure(500, 350); const std::uint32_t last = f.Configure(0, 0, { XDG_TOPLEVEL_STATE_ACTIVATED }); Expect(f.Sent(), { "commit", "commit", "commit" }, "three configures before a frame: three bare commits"); Check(window.deferredLogicalWidth == 500 && window.deferredLogicalHeight == 350, "the last size asked for waits, through a configure without a size"); Check(window.unackedConfigure == last, "the last serial waits, the one without a size"); Check(f.owed == 3, std::format("onFrameOwed fired for each ({})", f.owed)); Check(f.Begin() && window.width == 500 && window.height == 350, "one frame takes the last size"); f.Present(); Expect(Sorted(f.Sent()), Sorted({ std::format("ack {}", last), "destination 500x350" }), "and sends one destination and one acknowledgement, of the last serial"); Check(NothingWaits(window), "after the frame nothing is owed"); } // Nothing new while nothing is owed: answered in the listener, as before. { Fixture f = fixture(240, 640, 480); f.Frame(); f.Sent(); Window& window = f.window(); const std::uint32_t none = f.Configure(0, 0, { XDG_TOPLEVEL_STATE_ACTIVATED }); Expect(f.Sent(), { std::format("ack {}", none), "commit" }, "a configure without a size while nothing is owed: acknowledged in the listener, before its commit"); const std::uint32_t same = f.Configure(320, 240); Expect(f.Sent(), { std::format("ack {}", same), "commit" }, "a configure for the size the window has: the same"); Check(NothingWaits(window) && f.owed == 0, "neither leaves a frame owed or fires onFrameOwed"); Check(!f.Begin(), "and the next frame makes no swapchain"); f.Present(); Expect(f.Sent(), {}, "nor sends anything"); } // The compositor goes back to the size the window has before a frame. { Fixture f = fixture(120, 320, 240); f.Frame(); f.Sent(); Window& window = f.window(); const std::uint32_t first = f.Configure(400, 300); f.Sent(); const std::uint32_t back = f.Configure(320, 240); Expect(f.Sent(), { std::format("ack {}", back), "commit" }, "a configure back to the window's size: acknowledged at once, as the surface shows that size"); Check(NothingWaits(window), "the size and the serial that waited are gone"); Check(!f.Begin(), "the next frame makes no swapchain"); f.Present(); Expect(f.Sent(), {}, std::format("and never acknowledges {}, the serial before the one acknowledged", first)); } // A size that comes while a frame is being recorded. { Fixture f = fixture(120, 320, 240); f.Frame(); f.Sent(); Window& window = f.window(); f.Begin(); const std::uint32_t serial = f.Configure(400, 300); f.Present(); Expect(f.Sent(), { "commit" }, "a frame being recorded when a size comes: its present sends neither a destination nor an acknowledgement"); Check(window.unackedConfigure == serial && window.deferredLogicalWidth == 400, "the size and the serial wait on"); Check(f.Begin() && window.width == 400 && window.height == 300, "the frame after it takes the size"); f.Present(); Expect(Sorted(f.Sent()), Sorted({ std::format("ack {}", serial), "destination 400x300" }), "and answers the configure"); } // A configure before the window's first frame. { Fixture f = fixture(240, 640, 480); const std::uint32_t serial = f.Configure(0, 0, { XDG_TOPLEVEL_STATE_ACTIVATED }); Expect(f.Sent(), { "commit" }, "a configure before the window's first frame: not acknowledged in the listener"); Check(f.owed == 1, "onFrameOwed fired for it"); f.Present(); Expect(Sorted(f.Sent()), Sorted({ std::format("ack {}", serial), "destination 320x240" }), "the first frame brings the destination and the acknowledgement"); } // A swapchain made between frames, as Resize makes one outside a frame: // its first frame brings the destination, and a configure before that // frame waits for it. { Fixture f = fixture(240, 640, 480); f.Frame(); f.Sent(); Window& window = f.window(); window.width = 800; window.height = 500; window.logicalConfigured = false; f.Rebuild(); Expect(f.Sent(), {}, "a swapchain made at a new size sends the surface nothing"); const std::uint32_t serial = f.Configure(0, 0); Expect(f.Sent(), { "commit" }, "a configure while a new swapchain waits for its first frame: not acknowledged in the listener"); Check(!f.Begin(), "no size waits for the frame"); f.Present(); Expect(Sorted(f.Sent()), Sorted({ std::format("ack {}", serial), "destination 400x250" }), "the swapchain's first frame brings its destination and the acknowledgement"); // The same size again, as SetPresentMode makes a swapchain: the // surface has its size, and a configure still waits for a buffer of // the swapchain there is. f.Rebuild(); const std::uint32_t second = f.Configure(400, 250); Expect(f.Sent(), { "commit" }, "a configure for the window's size while a swapchain waits for its first frame: not acknowledged in the listener either"); Check(!f.Begin(), "nor is there a size for the frame to take"); f.Present(); Expect(f.Sent(), { std::format("ack {}", second) }, "that frame acknowledges it and sends no destination: the surface's size is the one it has"); } // A rebuild that threw leaves the window's sizes ahead of the swapchain // and stays pending for the next Render(). A configure for that size is // not answered by what the surface shows: it waits for the frame too. { Fixture f = fixture(240, 640, 480); f.Frame(); f.Sent(); Window& window = f.window(); // A size is taken and its rebuild throws: RebuildSwapchain has set // its flag and made nothing. f.Configure(400, 300); Check(window.TakeDeferredSize(Window::FrameBoundary::Start) && window.width == 800 && window.height == 600, "a size is taken, and the rebuild for it throws"); window.swapchainRebuildPending = true; f.Sent(); const std::uint32_t serial = f.Configure(400, 300); Expect(f.Sent(), { "commit" }, "a configure for the size a pending rebuild is for: not acknowledged in the listener, though the window's size says so already"); Check(window.deferredLogicalWidth == 0 && window.unackedConfigure == serial && f.owed == 2, "no size waits, the serial does, and onFrameOwed fired"); Check(f.Begin() && window.width == 800 && window.height == 600 && !window.swapchainRebuildPending, "the next frame makes the swapchain that waited"); f.Present(); Expect(Sorted(f.Sent()), Sorted({ std::format("ack {}", serial), "destination 400x300" }), "the frame of the swapchain made at last brings the destination and the acknowledgement"); } // A present that finds the swapchain out of date queues nothing, and // Render() makes the swapchain again after it. What went out before // that present is not sent a second time, and a configure that comes in // between waits for the first frame of the swapchain made after it. { Fixture f = fixture(240, 640, 480); f.Frame(); f.Sent(); Window& window = f.window(); const std::uint32_t serial = f.Configure(400, 300); f.Sent(); Check(f.Begin(), "a frame takes a size"); // The present that follows returns VK_ERROR_OUT_OF_DATE_KHR: // presentedGeneration stays, and a rebuild waits. window.SendFrameSurfaceState(); window.swapchainRebuildPending = true; Expect(Sorted(f.Sent()), Sorted({ std::format("ack {}", serial), "destination 400x300" }), "its destination and acknowledgement go out before its present, which finds the swapchain out of date"); const std::uint32_t next = f.Configure(400, 300); Expect(f.Sent(), { "commit" }, "a configure for that size before the swapchain is made again: not acknowledged in the listener"); Check(f.Begin(), "the next frame makes the swapchain"); f.Present(); Expect(f.Sent(), { std::format("ack {}", next) }, "and acknowledges, without sending the destination a second time"); Check(NothingWaits(window), "after it nothing is owed"); } // Without wp_viewporter there is no destination, and the scale is 1. { Fixture f = fixture(120, 320, 240, false); f.Frame(); Expect(f.Sent(), {}, "without a viewport the first frame sends nothing"); Window& window = f.window(); const std::uint32_t serial = f.Configure(400, 300); Expect(f.Sent(), { "commit" }, "without a viewport: a configure for a new size still waits for its frame"); Check(f.Begin() && window.width == 400 && window.height == 300, "the frame takes the size"); f.Present(); Expect(f.Sent(), { std::format("ack {}", serial) }, "and acknowledges, with no destination"); } // The scale that counts for the pixels is the one of the moment the frame // takes the size. The destination is the configured size whatever the // scale does. { Fixture f = fixture(240, 640, 480); f.Frame(); f.Sent(); Window& window = f.window(); const std::uint32_t serial = f.Configure(400, 300); Scale(window, 180); Check(f.Begin() && window.width == 600 && window.height == 450, std::format("a scale that changes before the frame: the size is taken at the new one ({}x{} at 1.5)", window.width, window.height)); f.Sent(); f.Present(); Expect(Sorted(f.Sent()), Sorted({ std::format("ack {}", serial), "destination 400x300" }), "and the destination is the size the compositor configured"); } // The scale changes between the frame's start and its present: a // listener of the frame dispatched the event (it made a window, or read // the clipboard). The swapchain was made for the old scale, and the // destination is still the configured size, not that swapchain's pixels // over the new scale (534x400). { Fixture f = fixture(240, 640, 480); f.Frame(); f.Sent(); Window& window = f.window(); const std::uint32_t serial = f.Configure(400, 300); Check(f.Begin() && window.width == 800 && window.height == 600, "the frame takes a size at scale 2"); Scale(window, 180); Check(window.scale == 2.0f && window.width == 800 && window.height == 600, "a scale that changes while the frame is recorded: the frame goes on at the one it began with"); f.Sent(); f.Present(); Expect(Sorted(f.Sent()), Sorted({ std::format("ack {}", serial), "destination 400x300" }), "its present still sends the configured size"); Check(!window.unackedConfigure && window.deferredLogicalWidth == 0 && window.scale == 2.0f, "and answers the configure, with the scale still waiting"); Check(f.End() && window.width == 600 && window.height == 450 && window.scale == 1.5f, std::format("the boundary after the present takes the scale: {}x{} for 400x300 at 1.5", window.width, window.height)); Check(!f.Begin(), "the next frame's start finds nothing left to take, and makes no swapchain"); f.Present(); Expect(f.Sent(), {}, "and the frame drawn at it sends the surface nothing, as its size is the one it has"); Check(NothingWaits(window), "after it nothing is owed"); } // A new size and a new scale that together come to the pixels the // swapchain has: an output whose scale changes under a window that fills // it. No swapchain is made, and the surface still has to be told its new // size, in the commit that acknowledges the configure. First the // configure, then the scale. { Fixture f = fixture(240, 640, 480); f.Frame(); Expect(f.Sent(), { "destination 320x240" }, "a window of 640x480 pixels at scale 2 is 320x240"); Window& window = f.window(); const std::uint32_t serial = f.Configure(640, 480); Expect(f.Sent(), { "commit" }, "a configure for 640x480 at scale 2: a new size, which waits"); Check(f.owed == 1, "onFrameOwed fired"); Scale(window, 120); Check(!f.Begin() && window.width == 640 && window.height == 480, "the scale becomes 1 before the frame: the size comes to the pixels the swapchain has, and none is made"); f.Present(); Expect(Sorted(f.Sent()), Sorted({ std::format("ack {}", serial), "destination 640x480" }), "the frame brings the surface its new size with the acknowledgement"); Check(NothingWaits(window), "after it nothing is owed"); const std::uint32_t again = f.Configure(640, 480); Expect(f.Sent(), { std::format("ack {}", again), "commit" }, "and the same configure again is answered by what the surface shows"); } // The other order: the scale first, then the configure, whose size is in // pixels the one the swapchain has. { Fixture f = fixture(180, 600, 450); f.Frame(); Expect(f.Sent(), { "destination 400x300" }, "a window of 600x450 pixels at scale 1.5 is 400x300"); Window& window = f.window(); Scale(window, 240); const std::uint32_t serial = f.Configure(300, 225); Expect(f.Sent(), { "commit" }, "the scale becomes 2, then a configure for 300x225, the pixels the swapchain has: not acknowledged in the listener, as the surface is 400x300"); Check(window.deferredLogicalWidth == 300 && window.deferredLogicalHeight == 225 && f.owed == 2, std::format("the size waits, and onFrameOwed fired for the scale and for the configure ({})", f.owed)); Check(!f.Begin() && window.width == 600 && window.height == 450, "the frame makes no swapchain"); f.Present(); Expect(Sorted(f.Sent()), Sorted({ std::format("ack {}", serial), "destination 300x225" }), "and brings the surface its new size with the acknowledgement"); } // The size the surface has, after the scale changed: the buffers are // made again for the new scale, and the surface is sent nothing. { Fixture f = fixture(240, 640, 480); f.Frame(); f.Sent(); Window& window = f.window(); Scale(window, 180); const std::uint32_t serial = f.Configure(320, 240); Expect(f.Sent(), { "commit" }, "a configure for the surface's size after the scale changed: it waits for buffers at the new scale"); Check(f.Begin() && window.width == 480 && window.height == 360, std::format("the frame makes the swapchain again for the scale ({}x{} at 1.5)", window.width, window.height)); f.Present(); Expect(f.Sent(), { std::format("ack {}", serial) }, "and acknowledges, with no destination: the surface's size is the one it has"); } // A fractional scale, with the numbers of #201's trace: a phone at 2.85 // whose maximized window is configured 392x796. The destination is that // size, where it used to be worked back from the 1118x2269 pixels to // 393x797, and the window is left alone when the compositor configures // it at the size it has, where it used to grow with each such configure // until it was 400x800. The pixels are 392x796 at 2.85 rounded half away // from zero, 1117.2 and 2268.6, where they were rounded up to 1118x2269. { Fixture f = fixture(342, 1140, 2280); f.Frame(); Expect(f.Sent(), { "destination 400x800" }, "a window of 1140x2280 pixels at scale 2.85 is 400x800"); Window& window = f.window(); const std::uint32_t serial = f.Configure(392, 796); f.Sent(); Check(f.Begin() && window.width == 1117 && window.height == 2269, std::format("392x796 at scale 2.85 is taken as {}x{} pixels", window.width, window.height)); f.Present(); Expect(Sorted(f.Sent()), Sorted({ std::format("ack {}", serial), "destination 392x796" }), "and the destination is 392x796, the configured size itself"); for (int i = 0; i < 3; ++i) { const std::uint32_t again = f.Configure(392, 796); Expect(f.Sent(), { std::format("ack {}", again), "commit" }, "the compositor configures the window at the size it has: acknowledged at once"); Check(!f.Begin() && window.width == 1117 && window.height == 2269, "and the window does not grow"); f.Present(); Expect(f.Sent(), {}, "nor is the surface sent anything"); } } // A window the application sized, whose pixels do not come to a whole // surface-local size: 320x240 at 1.75 is shown at 183x138. When the // compositor configures that size, the window takes the pixels for it // once, and is then left alone. 183x138 at 1.75 is 320.25x241.5, so // the width stays and the height goes to 242. { Fixture f = fixture(210, 320, 240); f.Frame(); Expect(f.Sent(), { "destination 183x138" }, "a window of 320x240 pixels at scale 1.75 is shown at 183x138"); Window& window = f.window(); const std::uint32_t serial = f.Configure(183, 138); f.Sent(); Check(f.Begin() && window.width == 320 && window.height == 242, std::format("configured at 183x138, it takes the pixels for that size ({}x{})", window.width, window.height)); f.Present(); Expect(f.Sent(), { std::format("ack {}", serial) }, "and the surface, which has that size, is sent no destination"); const std::uint32_t again = f.Configure(183, 138); Expect(f.Sent(), { std::format("ack {}", again), "commit" }, "the same configure again is acknowledged at once"); Check(!f.Begin() && window.width == 320 && window.height == 242, "and changes nothing"); } // The rounding of the pixels, as fractional-scale-v1 has it for a // toplevel: a configured size times the scale, rounded half away from // zero, worked out exactly. Its own example is 100x50 at 1.5, which is // 150x75. A half goes up: 853x101 at 1.5 is 1279.5x151.5. Less than a // half goes down, where rounding up gave a pixel more: 101x853 at 1.25 // is 126.25x1066.25. And a whole number stays one, where the sum in // float came out above it and was rounded up past it: 50x25 at 1.2 is // 60x30, and came to 61x31. { struct Case { std::uint32_t scale; std::int32_t width, height; std::uint32_t pixelsWidth, pixelsHeight; }; for (const Case& c : { Case{ 180, 100, 50, 150, 75 }, Case{ 180, 853, 101, 1280, 152 }, Case{ 150, 101, 853, 126, 1066 }, Case{ 144, 50, 25, 60, 30 } }) { Fixture f = fixture(c.scale, 640, 480); f.Frame(); f.Sent(); Window& window = f.window(); const std::uint32_t serial = f.Configure(c.width, c.height); f.Sent(); Check(f.Begin() && window.width == c.pixelsWidth && window.height == c.pixelsHeight, std::format("{}x{} at {} is taken as {}x{} pixels ({}x{})", c.width, c.height, c.scale / 120.0f, c.pixelsWidth, c.pixelsHeight, window.width, window.height)); f.Present(); Expect(Sorted(f.Sent()), Sorted({ std::format("ack {}", serial), std::format("destination {}x{}", c.width, c.height) }), "and the frame brings the surface that size"); const std::uint32_t again = f.Configure(c.width, c.height); Expect(f.Sent(), { std::format("ack {}", again), "commit" }, "the same configure again is acknowledged at once, as the pixels are those"); } } // The other way, for a window the application sized: the surface is its // pixels over the scale, rounded up, and worked out exactly too. 342x171 // at 2.85 is 120x60, which the sum in float came out above and was // rounded up past to 121x61. { Fixture f = fixture(342, 342, 171); f.Frame(); Expect(f.Sent(), { "destination 120x60" }, "a window of 342x171 pixels at scale 2.85 is 120x60"); } // Never below one pixel: at 0.4, 48 120ths, a length of one is 0.4 of a // pixel, which rounds to none, and a swapchain cannot be 0 wide. 1x300 // at 0.4 is taken as 1x120. { Fixture f = fixture(48, 640, 480); f.Frame(); f.Sent(); Window& window = f.window(); const std::uint32_t serial = f.Configure(1, 300); f.Sent(); Check(f.Begin() && window.width == 1 && window.height == 120, std::format("1x300 at 0.4 is taken as 1x120 pixels, one pixel and not none for the width ({}x{})", window.width, window.height)); f.Present(); Expect(Sorted(f.Sent()), Sorted({ std::format("ack {}", serial), "destination 1x300" }), "and the frame brings the surface 1x300"); const std::uint32_t again = f.Configure(1, 300); Expect(f.Sent(), { std::format("ack {}", again), "commit" }, "the same configure again is acknowledged at once, as the pixels are those"); } // Nor past the largest 32-bit value: a configured length of 2^30 at a // scale of 4 comes to 2^32 pixels, which the cast to 32 bits would wrap // to none. And the surface's length worked out from pixels of the // largest 32-bit value at a half stops at the largest int32, where the // cast would wrap it. { Fixture f = fixture(480, 640, 480); f.Frame(); f.Sent(); Window& window = f.window(); f.Configure(1073741824, 300); Check(f.Begin() && window.width == std::numeric_limits::max() && window.height == 1200, std::format("a configure of 2^30x300 at 4 takes {}x{} pixels, the largest 32-bit value and not none", window.width, window.height)); f.Present(); f.Sent(); } { // Value-initialized, as below: the scale it takes converts the // positions it holds. Window window{}; window.preferredScale = 60; window.width = std::numeric_limits::max(); window.height = 100; window.MatchSizesToScale(true); Check(window.logicalWidth == std::numeric_limits::max() && window.logicalHeight == 200, std::format("pixels of the largest 32-bit value at a half come to a surface {} wide, the largest int32", window.logicalWidth)); } // A configured size worked back from its pixels, as nothing in the // window does any more, the surface's size being state of its own: // short of the 32-bit limits the two sums stop at, it lands above itself // where its product with the scale has a fraction of a half or more, // and, from a scale of a half up, on itself below that. // 853x101 at 1.5 is 1279.5x151.5, so 1280x152 pixels, which come to // 854x102; 392x796 at 2.85 is 1117.2x2268.6, so 1117x2269 pixels, which // come to 392x797. The window's own sums: the pixels as for a configured // size, then the surface's size as for a window the application sized at // those pixels. { struct Case { std::uint32_t scale; std::int32_t width, height, backWidth, backHeight; }; for (const Case& c : { Case{ 180, 853, 101, 854, 102 }, Case{ 342, 392, 796, 392, 797 } }) { // Value-initialized: the scale it takes converts the positions // it holds, which a default-initialized window has none of. Window window{}; window.preferredScale = c.scale; window.logicalWidth = c.width; window.logicalHeight = c.height; window.logicalConfigured = true; window.MatchSizesToScale(true); const std::uint32_t pixelsWidth = window.width; const std::uint32_t pixelsHeight = window.height; window.logicalConfigured = false; window.MatchSizesToScale(true); Check(window.logicalWidth == c.backWidth && window.logicalHeight == c.backHeight, std::format("{}x{} at {} is {}x{} pixels, which come to {}x{} ({}x{})", c.width, c.height, c.scale / 120.0f, pixelsWidth, pixelsHeight, c.backWidth, c.backHeight, window.logicalWidth, window.logicalHeight)); } } // A swapchain made for another reason (a present mode, a swapchain out // of date) after the scale changed, with no configure in between. A // window the compositor sized keeps the surface's size and takes the // pixels for it at the new scale. One the application sized keeps its // pixels and takes no scale: nothing of that swapchain needs it, and // until the next frame, which sends the surface its new size, the // compositor maps the surface as before. { Fixture f = fixture(240, 640, 480); f.Frame(); f.Configure(400, 300); f.Frame(); f.Sent(); Window& window = f.window(); Check(window.width == 800 && window.height == 600 && window.logicalConfigured, "a window the compositor sized, 400x300 at scale 2"); Scale(window, 180); f.Rebuild(); Check(window.width == 600 && window.height == 450 && window.logicalWidth == 400 && window.logicalHeight == 300 && window.scale == 1.5f, std::format("a swapchain made after the scale became 1.5: the pixels for 400x300 at it ({}x{}), and the window draws at it", window.width, window.height)); Check(!f.Begin(), "the next frame boundary finds nothing left to take"); f.Present(); Expect(f.Sent(), {}, "and the surface, still 400x300, is sent nothing"); } { Fixture f = fixture(240, 640, 480); f.Frame(); f.Sent(); Window& window = f.window(); Check(!window.logicalConfigured, "a window the application sized, 640x480 pixels at scale 2"); Scale(window, 120); f.Rebuild(); Check(window.width == 640 && window.height == 480 && window.scale == 2.0f && window.logicalWidth == 320 && window.logicalHeight == 240, "a swapchain made after the scale became 1 keeps the pixels and takes no scale: the window still draws at 2, its surface still 320x240"); CheckAt(Motion(window, 160, 120), 320, 240, "until the next frame a motion to the surface's middle is at the middle of the frame on screen, drawn at 2"); Check(!f.Begin() && window.scale == 1.0f && window.logicalWidth == 640 && window.logicalHeight == 480, "the next frame's start takes the scale, with no swapchain"); CheckAt(window.currentMousePos, 160, 120, "the pointer, which has not moved, is where its surface-local (160,120) comes to at 1"); f.Present(); Expect(f.Sent(), { "destination 640x480" }, "and the surface takes the size the pixels come to at it with that frame"); } // The same with the swapchain made after a present that found it out of // date, while a scale that came during the frame waited, and with a // configure as well that comes with that scale to the pixels the window // has: the boundary after the present makes the swapchain again and // takes neither, and the next frame's start takes both. { Fixture f = fixture(240, 640, 480); f.Frame(); f.Sent(); Window& window = f.window(); f.Begin(); Scale(window, 120); f.Present(); window.swapchainRebuildPending = true; Check(f.End() && window.scale == 2.0f && window.logicalWidth == 320 && window.logicalHeight == 240, "a present out of date after a scale came during its frame: the boundary after it makes the swapchain again and takes no scale"); CheckAt(Motion(window, 160, 120), 320, 240, "until the next frame a motion to the surface's middle is at the middle of the frame on screen, drawn at 2"); Check(!f.Begin() && window.scale == 1.0f && window.logicalWidth == 640 && window.logicalHeight == 480, "the next frame's start takes it"); f.Present(); Expect(f.Sent(), { "destination 640x480" }, "and its frame brings the surface its size"); f.Begin(); Scale(window, 192); const std::uint32_t serial = f.Configure(400, 300); f.Present(); f.Sent(); window.swapchainRebuildPending = true; Check(f.End() && window.scale == 1.0f && window.width == 640 && window.height == 480 && window.deferredLogicalWidth == 400, "a scale of 1.6 and a configure of 400x300 that come to those 640x480 pixels, and a present out of date: the swapchain is made again at them, and neither is taken"); Check(!f.Begin() && window.scale == 1.6f && window.width == 640 && window.height == 480 && window.logicalWidth == 400 && window.logicalHeight == 300, "the next frame's start takes both, with no swapchain"); f.Present(); Expect(Sorted(f.Sent()), Sorted({ std::format("ack {}", serial), "destination 400x300" }), "and its frame brings the surface its size and the acknowledgement"); Check(NothingWaits(window), "after it nothing is owed"); } // Issue #165: a scale the compositor asks for by itself, with no // configure, as when a window goes to an output of another scale. Its // listener used to set the scale and nothing else, so the buffers and // the surface kept the old one until something resized the window, // while the pointer, the touches and the caret were converted with the // new one. Now the listener only keeps it, the window draws at the scale // it has and converts with that one until a frame boundary takes the // new one, as it takes a configured size, and onFrameOwed fires. A // window the compositor sized keeps its surface's size and takes new // pixels for it: the frame makes the swapchain. { Fixture f = fixture(240, 640, 480); f.Frame(); f.Configure(400, 300); f.Frame(); f.Sent(); f.owed = 0; Window& window = f.window(); Check(window.logicalConfigured && window.width == 800 && window.height == 600, "#165: a window the compositor sized, 400x300 at scale 2"); // The caret goes to a text input the compositor gave this window. zwp_text_input_v3* input = static_cast(wl_registry_bind(registry, 5, &zwp_text_input_v3_interface, 1)); textInputId = wl_proxy_get_id(reinterpret_cast(input)); Device::display = f.display; Device::textInput = input; Device::textInputWindow = &window; Scale(window, 180); Expect(f.Sent(), {}, "a scale from the compositor: its listener sends nothing"); Check(window.scale == 2.0f && window.width == 800 && window.height == 600 && window.logicalWidth == 400 && window.logicalHeight == 300 && window.preferredScale == 180, "the window still draws at 2, at 800x600 for its 400x300, and the scale waits"); Check(f.owed == 1, std::format("onFrameOwed fired once ({})", f.owed)); CheckAt(Motion(window, 200, 150), 400, 300, "a motion to the surface's middle is at the middle of the 800x600 buffers drawn at 2"); window.SetTextInput(true, 120, 240, 6, 48); Expect(f.Sent(), { "caret 60,120 3x24" }, "a caret given in those buffers' pixels goes to the text input at 2"); Check(f.Begin() && window.width == 600 && window.height == 450 && window.scale == 1.5f && window.logicalWidth == 400 && window.logicalHeight == 300, std::format("the next frame takes the scale, with the pixels for 400x300 at 1.5 ({}x{}), and makes the swapchain at them", window.width, window.height)); CheckAt(window.currentMousePos, 300, 225, "the pointer, which has not moved, is at the middle of the 600x450 buffers: the boundary took its position to the new scale"); Expect(f.Sent(), { "caret 80,160 4x32" }, "and sent the caret given in the pixels of 2 again, at 1.5, with no SetTextInput"); CheckAt(Motion(window, 200, 150), 300, 225, "from there the motion to the surface's middle is at the middle of the 600x450 buffers"); window.SetTextInput(true, 120, 240, 6, 48); Expect(f.Sent(), { "caret 80,160 4x32" }, "and a caret given in their pixels goes to the text input at 1.5"); f.Present(); Expect(f.Sent(), {}, "the frame sends the surface nothing, as its size is the one it had"); Check(NothingWaits(window) && f.owed == 1, "after it nothing is owed"); window.SetTextInput(false); f.Sent(); Device::textInputWindow = nullptr; Device::textInput = nullptr; Device::display = nullptr; zwp_text_input_v3_destroy(input); textInputId = 0; } // A window the application sized keeps its pixels: no swapchain is made, // and the surface takes the size they come to at the new scale, which // the frame sends with its buffer. A finger that goes down while the // scale waits and moves after the frame took it is at the buffers' // middle both times. { Fixture f = fixture(240, 640, 480); f.Frame(); f.Sent(); Window& window = f.window(); std::vector> touches; std::vector> lifts; EventListener downSub(&window.onTouchDown, [&](TouchPoint point) { if (point.id == 0) touches.push_back(point.position); }); EventListener moveSub(&window.onTouchMove, [&](TouchPoint point) { if (point.id == 0) touches.push_back(point.position); }); EventListener upSub(&window.onTouchUp, [&](TouchPoint point) { if (point.id == 1) lifts.push_back(point.position); }); zwp_text_input_v3* input = static_cast(wl_registry_bind(registry, 5, &zwp_text_input_v3_interface, 1)); textInputId = wl_proxy_get_id(reinterpret_cast(input)); Device::display = f.display; Device::textInput = input; Device::textInputWindow = &window; window.SetTextInput(true, 120, 240, 6, 48); Expect(f.Sent(), { "caret 60,120 3x24" }, "a caret given in the 640x480 pixels at 2 goes to the text input as such"); Scale(window, 120); Expect(f.Sent(), {}, "a scale for a window the application sized: its listener sends nothing"); Check(window.scale == 2.0f && window.width == 640 && window.height == 480 && window.logicalWidth == 320 && window.logicalHeight == 240 && f.owed == 1, "the window still draws at 2, at 640x480 for its 320x240, and onFrameOwed fired"); CheckAt(Motion(window, 160, 120), 320, 240, "a motion to the surface's middle is at the middle of the 640x480 buffers"); Device::touch_down(nullptr, nullptr, 1, 0, window.surface, 0, wl_fixed_from_double(160), wl_fixed_from_double(120)); Check(touches.size() == 1, "a finger goes down"); if (!touches.empty()) CheckAt(touches.back(), 320, 240, "at the surface's middle, which is the buffers' middle"); Device::touch_down(nullptr, nullptr, 2, 0, window.surface, 1, wl_fixed_from_double(80), wl_fixed_from_double(60)); Check(!f.Begin() && window.width == 640 && window.height == 480 && window.scale == 1.0f && window.logicalWidth == 640 && window.logicalHeight == 480, std::format("the next frame takes the scale with no swapchain: the pixels stay, and the surface is the {}x{} they come to at 1", window.logicalWidth, window.logicalHeight)); CheckAt(window.currentMousePos, 160, 120, "the pointer the first finger stands in for, which has not moved, is where its surface-local (160,120) comes to at 1"); Expect(f.Sent(), { "caret 120,240 6x48" }, "taking the scale sends the caret again, at 1, with no SetTextInput, and nothing else"); f.Present(); Expect(f.Sent(), { "destination 640x480" }, "the frame brings the surface that size with its buffer"); Device::touch_up(nullptr, nullptr, 3, 0, 1); Check(lifts.size() == 1, "a second finger, down before the boundary, lifts after it without moving"); if (!lifts.empty()) CheckAt(lifts.back(), 80, 60, "where its surface-local (80,60) comes to at 1"); CheckAt(Motion(window, 320, 240), 320, 240, "and a motion to its middle is at the buffers' middle"); Device::touch_motion(nullptr, nullptr, 0, 0, wl_fixed_from_double(320), wl_fixed_from_double(240)); Check(touches.size() == 2, "the finger moves"); if (touches.size() == 2) CheckAt(touches.back(), 320, 240, "to the surface's middle, which is the buffers' middle"); Device::touch_up(nullptr, nullptr, 4, 0, 0); Check(NothingWaits(window) && f.owed == 1, "after the frame nothing is owed"); window.SetTextInput(false); f.Sent(); Device::textInputWindow = nullptr; Device::textInput = nullptr; Device::display = nullptr; zwp_text_input_v3_destroy(input); textInputId = 0; } // An Input::Map attached to the window keeps a last position of its own // for MouseDeltaBind. Ticked across a boundary that takes a new scale, it // reads no delta for a pointer that rests, and then the motion in the // new pixels: 10 units at 1.5 are 15, read through a binding of a // sixteenth, as the Map clamps an axis to 1. { Fixture f = fixture(240, 640, 480); f.Frame(); f.Configure(400, 300); f.Frame(); f.Sent(); Window& window = f.window(); Input::Map map; Input::Action& look = map.AddAction("Look", Input::ActionType::Vector2); look.bindings = { Input::MouseDeltaBind{ 1.0f / 16.0f } }; map.Attach(window); Motion(window, 200, 150); map.Tick(); Scale(window, 180); Check(f.Begin() && window.scale == 1.5f, "an Input::Map's window takes a scale of 1.5 at a boundary"); map.Tick(); CheckAt(look.vector2, 0, 0, "the Map ticked across it reads no delta for the pointer, which rests"); Motion(window, 210, 150); map.Tick(); CheckAt(look.vector2, 15.0f / 16.0f, 0, "and a motion of 10 units after it as 15 pixels, a sixteenth of which is 0.9375"); map.Detach(); f.Present(); } // The same Map across two boundaries between two of its Ticks, on a // window the compositor sized, 1.75 taken by a swapchain made outside a // frame at the pixels it gives the window and 1 by the next frame's // start, with the pointer resting at surface 100.25: no delta and no // event, where a position taken along to each scale comes back with a // float residue. { Fixture f = fixture(120, 640, 480); f.Frame(); f.Configure(640, 480); f.Frame(); f.Sent(); Window& window = f.window(); Input::Map map; Input::Action& look = map.AddAction("Look", Input::ActionType::Vector2); look.bindings = { Input::MouseDeltaBind{} }; int changed = 0; EventListener> changedSub(&look.onVector2Changed, [&](Vector) { ++changed; }); map.Attach(window); Motion(window, 100.25, 80); map.Tick(); Scale(window, 210); f.Rebuild(); Scale(window, 120); f.Begin(); Check(window.scale == 1.0f, "a scale of 1.75 taken by a swapchain, and 1 again by a frame boundary, between two Ticks"); map.Tick(); CheckAt(look.vector2, 0, 0, "the Map reads no delta for the pointer, which rests"); Check(changed == 0, std::format("and fires no onVector2Changed ({})", changed)); map.Detach(); f.Present(); } // Input whose listener takes a scale that waits, as a SetPresentMode // outside a frame does for a window the compositor sized, through the // swapchain it makes at the new pixels: the position the handler stores // after the listener is in the pixels of that scale. A finger's down and // the click of the pointer it stands in for, its move, and the move // after an enter. { Fixture f = fixture(240, 640, 480); f.Frame(); f.Configure(320, 240); f.Frame(); f.Sent(); Window& window = f.window(); bool rebuild = false; const auto takes = [&] { if (rebuild) { rebuild = false; f.Rebuild(); } }; std::vector> at; EventListener downSub(&window.onTouchDown, [&](TouchPoint) { takes(); }); EventListener moveSub(&window.onTouchMove, [&](TouchPoint) { takes(); }); EventListener enterSub(&window.onMouseEnter, [&] { takes(); }); EventListener clickSub(&window.onMouseLeftClick, [&] { at.push_back(window.currentMousePos); }); EventListener pointerSub(&window.onMouseMove, [&] { at.push_back(window.currentMousePos); }); Scale(window, 120); rebuild = true; Device::touch_down(nullptr, nullptr, 1, 0, window.surface, 0, wl_fixed_from_double(160), wl_fixed_from_double(120)); Check(window.scale == 1.0f && at.size() == 1, "a listener of onTouchDown takes a scale of 1 that waited"); if (at.size() == 1) CheckAt(at.back(), 160, 120, "the click is where the finger's surface-local (160,120) comes to at 1"); Scale(window, 240); rebuild = true; Device::touch_motion(nullptr, nullptr, 0, 0, wl_fixed_from_double(100), wl_fixed_from_double(50)); Check(window.scale == 2.0f && at.size() == 2, "a listener of onTouchMove takes a scale of 2 that waited"); if (at.size() == 2) CheckAt(at.back(), 200, 100, "the move is where (100,50) comes to at 2"); Device::touch_up(nullptr, nullptr, 2, 0, 0); Scale(window, 180); rebuild = true; Device::PointerListenerHandleEnter(nullptr, nullptr, 3, window.surface, wl_fixed_from_double(100), wl_fixed_from_double(50)); Check(window.scale == 1.5f && at.size() == 3, "a listener of onMouseEnter takes a scale of 1.5 that waited"); if (at.size() == 3) CheckAt(at.back(), 150, 75, "the move after the enter is where (100,50) comes to at 1.5"); Device::PointerListenerHandleLeave(nullptr, nullptr, 4, window.surface); } // A scale that comes back to the one the window draws at before a frame // took the one between: nothing is owed any more. { Fixture f = fixture(240, 640, 480); f.Frame(); f.Sent(); Window& window = f.window(); Scale(window, 180); Scale(window, 240); Check(f.owed == 1 && window.preferredScale == 240, std::format("onFrameOwed fired for the new scale and not for the one back ({})", f.owed)); Check(!f.Begin() && window.scale == 2.0f && window.width == 640 && window.height == 480 && window.logicalWidth == 320, "the next frame takes nothing"); f.Present(); Expect(f.Sent(), {}, "and sends nothing"); Check(NothingWaits(window), "nothing is owed"); } // A scale that comes while a frame is being recorded, as when a listener // of the frame dispatched the event: the frame goes on at the scale it // began with. A window the compositor sized takes the new one at the // boundary after the present, with the pixels it needs, which the // swapchain is made at there. { Fixture f = fixture(240, 640, 480); f.Frame(); f.Configure(400, 300); f.Frame(); f.Sent(); Window& window = f.window(); f.Begin(); Scale(window, 180); Check(window.scale == 2.0f && window.width == 800 && window.height == 600, "a scale while a frame of a window the compositor sized is being recorded: the frame goes on at 2"); f.Present(); Expect(f.Sent(), {}, "its present sends nothing for the scale"); Check(f.End() && window.scale == 1.5f && window.width == 600 && window.height == 450 && window.logicalWidth == 400 && window.logicalHeight == 300, "the boundary after its present takes it, with the pixels for 400x300 at 1.5, and makes the swapchain at them"); CheckAt(Motion(window, 200, 150), 300, 225, "from there a motion to the surface's middle is at the middle of the 600x450 buffers"); Check(!f.Begin(), "the next frame's start finds nothing left to take, and makes no swapchain"); f.Present(); Expect(f.Sent(), {}, "and the next frame sends the surface nothing, as its size is the one it had"); Check(NothingWaits(window), "after it nothing is owed"); } // One the application sized takes it at the start of the next frame, // the one that sends its surface the size it comes to, and not after the // present, where it gives the window no new pixels: until that frame's // commit the compositor maps the surface by the destination it has, so a // motion in between is still at the pixels the scale before gives, // those of the frame on screen, and the caret is not sent again before // then. { Fixture f = fixture(240, 640, 480); f.Frame(); f.Sent(); Window& window = f.window(); zwp_text_input_v3* input = static_cast(wl_registry_bind(registry, 5, &zwp_text_input_v3_interface, 1)); textInputId = wl_proxy_get_id(reinterpret_cast(input)); Device::display = f.display; Device::textInput = input; Device::textInputWindow = &window; window.SetTextInput(true, 120, 240, 6, 48); Expect(f.Sent(), { "caret 60,120 3x24" }, "a caret given in the 640x480 pixels at 2 goes to the text input as such"); f.Begin(); Scale(window, 120); Check(window.scale == 2.0f && window.logicalWidth == 320 && window.logicalHeight == 240, "a scale while a frame of a window the application sized is being recorded: the frame goes on at 2, its surface 320x240"); f.Present(); Expect(f.Sent(), {}, "its present sends nothing for the scale"); Check(!f.End() && window.scale == 2.0f && window.logicalWidth == 320 && window.logicalHeight == 240, "the boundary after its present takes nothing: the window still draws at 2, and its surface is still 320x240"); Expect(f.Sent(), {}, "and sends nothing, the caret neither"); CheckAt(Motion(window, 160, 120), 320, 240, "until the next frame a motion to the surface's middle is at the middle of the frame on screen, drawn at 2"); Check(!f.Begin() && window.scale == 1.0f && window.logicalWidth == 640 && window.logicalHeight == 480, "the next frame's start takes it, with no swapchain: the surface is the 640x480 the pixels come to at 1"); CheckAt(window.currentMousePos, 160, 120, "the pointer, which has not moved, is where its surface-local (160,120) comes to at 1"); Expect(f.Sent(), { "caret 120,240 6x48" }, "and the caret goes to the text input again, at 1"); f.Present(); Expect(f.Sent(), { "destination 640x480" }, "the frame drawn at 1 brings the surface its size"); Check(NothingWaits(window), "after it nothing is owed"); window.SetTextInput(false); f.Sent(); Device::textInputWindow = nullptr; Device::textInput = nullptr; Device::display = nullptr; zwp_text_input_v3_destroy(input); textInputId = 0; } // A configure and a scale that come while a frame is being recorded and // together come to the pixels the window has (an output's scale changed // under a window that fills it): they give the window no new pixels, so // the boundary after the present takes neither, and the next frame's start // takes both, that frame bringing the surface its size and the // acknowledgement. Until then a motion is still at the pixels of the // frame on screen, and the caret is not sent again. { Fixture f = fixture(240, 640, 480); f.Frame(); f.Sent(); Window& window = f.window(); zwp_text_input_v3* input = static_cast(wl_registry_bind(registry, 5, &zwp_text_input_v3_interface, 1)); textInputId = wl_proxy_get_id(reinterpret_cast(input)); Device::display = f.display; Device::textInput = input; Device::textInputWindow = &window; window.SetTextInput(true, 120, 240, 6, 48); Expect(f.Sent(), { "caret 60,120 3x24" }, "a caret given in the 640x480 pixels at 2 goes to the text input as such"); f.Begin(); Scale(window, 192); const std::uint32_t serial = f.Configure(400, 300); f.Present(); Expect(f.Sent(), { "commit" }, "a scale of 1.6 and a configure of 400x300 during a frame: the configure's listener commits, and the present sends nothing"); Check(!f.End() && window.scale == 2.0f && window.width == 640 && window.height == 480 && window.logicalWidth == 320 && window.logicalHeight == 240 && window.deferredLogicalWidth == 400 && window.deferredLogicalHeight == 300 && window.unackedConfigure == serial, "they come to the 640x480 pixels the window has: the boundary after the present takes neither, and the configure waits"); Expect(f.Sent(), {}, "and sends nothing, the caret neither"); CheckAt(Motion(window, 160, 120), 320, 240, "until the next frame a motion to the surface's middle is at the middle of the frame on screen, drawn at 2"); Check(!f.Begin() && window.scale == 1.6f && window.width == 640 && window.height == 480 && window.logicalWidth == 400 && window.logicalHeight == 300 && window.logicalConfigured, "the next frame's start takes both, with no swapchain: 400x300 at 1.6 is the 640x480 the window has"); CheckAt(window.currentMousePos, 256, 192, "the pointer, which has not moved, is where its surface-local (160,120) comes to at 1.6"); Expect(f.Sent(), { "caret 75,150 3x30" }, "and the caret goes to the text input again, at 1.6"); f.Present(); Expect(Sorted(f.Sent()), Sorted({ std::format("ack {}", serial), "destination 400x300" }), "the frame drawn at 1.6 brings the surface its size and the acknowledgement"); Check(NothingWaits(window), "after it nothing is owed"); window.SetTextInput(false); f.Sent(); Device::textInputWindow = nullptr; Device::textInput = nullptr; Device::display = nullptr; zwp_text_input_v3_destroy(input); textInputId = 0; } // A window the compositor sized that a new scale leaves at its pixels, // as it can one this small: the scale gives it no new pixels either, so // it waits for the next frame's start too. { Fixture f = fixture(240, 640, 480); f.Frame(); f.Configure(50, 50); f.Frame(); f.Sent(); Window& window = f.window(); Check(window.logicalConfigured && window.width == 100 && window.height == 100, "a window the compositor sized, 50x50 at 2"); f.Begin(); Scale(window, 239); f.Present(); Check(!f.End() && window.scale == 2.0f && window.width == 100 && window.height == 100, "a scale during its frame that leaves its 100x100 pixels: the boundary after the present takes nothing"); Check(!f.Begin() && window.scale == 239 / 120.0f && window.width == 100 && window.height == 100 && window.logicalWidth == 50, "the next frame's start takes it, with no swapchain"); f.Present(); Expect(f.Sent(), {}, "and the frame sends the surface nothing, as its size is the one it had"); Check(NothingWaits(window), "after it nothing is owed"); } // A configure without a size while a scale waits asks for nothing new, // and is acknowledged in the listener as before: the scale is not the // compositor's to be answered. { Fixture f = fixture(240, 640, 480); f.Frame(); f.Sent(); Window& window = f.window(); Scale(window, 180); const std::uint32_t none = f.Configure(0, 0, { XDG_TOPLEVEL_STATE_ACTIVATED }); Expect(f.Sent(), { std::format("ack {}", none), "commit" }, "a configure without a size while a scale waits: acknowledged in the listener"); Check(!f.Begin() && window.scale == 1.5f && window.logicalWidth == 427 && window.logicalHeight == 320, std::format("the next frame takes the scale: 640x480 pixels at 1.5 are {}x{}", window.logicalWidth, window.logicalHeight)); f.Present(); Expect(f.Sent(), { "destination 427x320" }, "which it sends with its buffer"); } // A configure for the size the surface has while a new scale waits, // where its pixels at that scale are the ones the window has: 101x101 // pixels at 2 are a surface of 51x51, which at 237/120 comes to 101x101 // again, and at 2 to 102x102. It is no configure at the size the window // has, since the window draws at the scale before: it waits for the // frame that takes the scale and takes its size with it, and the window // stays one the application sized until then, its pixels those of the // surface it has at the scale it draws at. { Fixture f = fixture(240, 101, 101); f.Frame(); f.Sent(); Window& window = f.window(); Check(!window.logicalConfigured && window.logicalWidth == 51 && window.logicalHeight == 51, "a window the application sized, 101x101 pixels at 2, its surface 51x51"); Scale(window, 237); const std::uint32_t serial = f.Configure(51, 51); Expect(f.Sent(), { "commit" }, "a configure for 51x51 while a scale of 237/120 waits: not acknowledged in the listener"); Check(!window.logicalConfigured && window.deferredLogicalWidth == 51 && window.deferredLogicalHeight == 51 && window.unackedConfigure == serial && window.scale == 2.0f && window.width == 101 && window.height == 101, "it waits, and the window stays one the application sized, at 101x101 drawn at 2"); Check(!f.Begin() && window.logicalConfigured && window.scale == 237 / 120.0f && window.width == 101 && window.height == 101 && window.logicalWidth == 51 && window.logicalHeight == 51, "the next frame's start takes both, with no swapchain: 51x51 at 237/120 is the 101x101 the window has"); f.Present(); Expect(f.Sent(), { std::format("ack {}", serial) }, "and its frame acknowledges the configure, sending no destination: the surface's size is the one it has"); Check(NothingWaits(window), "after it nothing is owed"); } // Before the window has its first swapchain, as while the constructor // waits for its first configure (KWin names the scale then): the // listener keeps the scale and fires nothing, and the constructor takes // it before it makes the swapchain. A scale of 0 is not taken at all. { // Value-initialized: the scale it takes converts the positions it // holds, which a default-initialized window has none of. Window window{}; int owed = 0; EventListener owedSub(&window.onFrameOwed, [&] { ++owed; }); Scale(window, 0); Check(window.preferredScale == 120, "a scale of 0 is not taken"); Scale(window, 210); Check(window.preferredScale == 210 && window.scale == 1.0f && owed == 0, "a scale named before the window has a swapchain is kept, and fires nothing"); window.width = 640; window.height = 480; window.MatchSizesToScale(true); Check(window.scale == 1.75f && window.logicalWidth == 366 && window.logicalHeight == 275, std::format("the constructor takes it before its first swapchain: 640x480 pixels at 1.75 are {}x{}", window.logicalWidth, window.logicalHeight)); } // A Resize from a listener of the frame waits in pixels, for that // frame's present. { Fixture f = fixture(240, 640, 480); f.Frame(); f.Sent(); Window& window = f.window(); window.recording = true; window.Resize(1000, 700); window.recording = false; Check(window.width == 640 && window.deferredWidth == 1000, "a Resize during a frame waits"); Check(f.Begin() && window.width == 1000 && window.height == 700 && window.deferredWidth == 0 && window.deferredHeight == 0, "and is taken at the frame boundary"); f.Present(); Expect(f.Sent(), { "destination 500x350" }, "the first frame at it brings the surface the size the pixels come to"); } // Of a Resize and a configured size, the one asked for last is taken. // The Resize first: the compositor's later size takes its place. { Fixture f = fixture(240, 640, 480); f.Frame(); f.Sent(); Window& window = f.window(); window.recording = true; window.Resize(1200, 800); window.recording = false; f.Configure(400, 300); Check(window.deferredWidth == 0 && window.deferredHeight == 0 && window.deferredLogicalWidth == 400, "a configure after a Resize that waits: the compositor's size waits in its place"); Check(f.Begin() && window.width == 800 && window.height == 600, std::format("and is the one taken ({}x{})", window.width, window.height)); Check(!f.Begin(), "a second take finds nothing"); } // The same when the compositor's later size is the one the window has: // the Resize is dropped then too, and the window stays as it is. The // configure comes while the frame is being recorded, as the Resize did. { Fixture f = fixture(120, 800, 600); f.Frame(); f.Sent(); Window& window = f.window(); window.recording = true; window.Resize(1000, 700); const std::uint32_t serial = f.Configure(800, 600); Expect(f.Sent(), { std::format("ack {}", serial), "commit" }, "a configure for the size the window has, after a Resize that waits: acknowledged at once, as the surface shows that size"); Check(window.deferredWidth == 0 && window.deferredHeight == 0, "the Resize waits no more"); f.Present(); window.recording = false; Check(!f.Begin() && window.width == 800 && window.height == 600, std::format("after the frame the window is still the size the compositor configured ({}x{})", window.width, window.height)); Expect(f.Sent(), {}, "and nothing more is sent"); } // The configure first, then a Resize in the same frame: the // application's later size takes the compositor's place. The serial is // acknowledged by the frame being recorded, which no size waits behind // any more. { Fixture f = fixture(240, 640, 480); f.Frame(); f.Sent(); Window& window = f.window(); window.recording = true; const std::uint32_t serial = f.Configure(400, 300); window.Resize(1000, 700); Check(window.deferredLogicalWidth == 0 && window.deferredLogicalHeight == 0 && window.deferredWidth == 1000, "a Resize after a configure that waits: the application's size waits in its place"); f.Sent(); f.Present(); window.recording = false; Expect(f.Sent(), { std::format("ack {}", serial) }, "the frame acknowledges the configure"); Check(f.Begin() && window.width == 1000 && window.height == 700 && !window.logicalConfigured, std::format("and the size taken after it is the application's ({}x{})", window.width, window.height)); f.Present(); Expect(f.Sent(), { "destination 500x350" }, "which the surface takes with its first frame"); } // And a Resize outside a frame to the size the window has: the // compositor's size that waited is dropped, and nothing is rebuilt. { Fixture f = fixture(240, 640, 480); f.Frame(); f.Sent(); Window& window = f.window(); const std::uint32_t serial = f.Configure(400, 300); f.Sent(); window.Resize(640, 480); Check(window.deferredLogicalWidth == 0 && window.deferredLogicalHeight == 0 && window.unackedConfigure == serial, "a Resize to the size the window has, after a configure that waits: the size waits no more, the serial does"); Check(!f.Begin() && window.width == 640 && window.height == 480, "the next frame makes no swapchain"); f.Present(); Expect(f.Sent(), { std::format("ack {}", serial) }, "and acknowledges the configure"); } // The events: onToplevelState first, then onFrameOwed with the states set. { Fixture f = fixture(120, 320, 240); f.Frame(); f.Sent(); Window& window = f.window(); std::vector order; EventListener stateSub(&window.onToplevelState, [&] { order.push_back("state"); }); EventListener owedSub(&window.onFrameOwed, [&] { order.push_back(window.activated ? "owed, activated" : "owed"); }); f.Configure(400, 300, { XDG_TOPLEVEL_STATE_ACTIVATED }); Check(order == std::vector{ "state", "owed, activated" }, "onFrameOwed fires after onToplevelState, with the configure's states set"); if (order != std::vector{ "state", "owed, activated" }) std::println(" fired: {}", order); } // A listener of onToplevelState destroys the window: the handler fires // nothing more on it. Another window is made in its place, at the same // address, so an event fired through the old pointer would show. { Fixture f = fixture(120, 320, 240); f.Frame(); f.Sent(); int firedOnReplacement = 0; std::optional> replacementSub; EventListener stateSub(&f.window().onToplevelState, [&] { f.holder.reset(); Window& replacement = f.holder.emplace(); replacementSub.emplace(&replacement.onFrameOwed, [&] { ++firedOnReplacement; }); }); f.Configure(400, 300, { XDG_TOPLEVEL_STATE_ACTIVATED }); Check(firedOnReplacement == 0 && f.owed == 0, "a window destroyed from onToplevelState: onFrameOwed does not fire through the handler's pointer"); replacementSub.reset(); } Check(std::ranges::adjacent_find(acknowledged, std::greater_equal{}) == acknowledged.end() && !acknowledged.empty(), std::format("each of the {} acknowledgements names a later serial than the one before", acknowledged.size())); const int error = wl_display_get_error(display); Check(error == 0, "the display took every request"); if (error != 0) std::println(" the display failed: {}", std::strerror(error)); wl_registry_destroy(registry); wl_display_disconnect(display); close(fds[1]); if (failures != 0) { std::println("{} check(s) failed", failures); return EXIT_FAILURE; } std::println("all checks passed"); return EXIT_SUCCESS; }