//SPDX-License-Identifier: LGPL-3.0-only //SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® // Window::activated and Window::suspended on the Wayland backend: the two // xdg_toplevel states a consumer needs to tell whether the user is looking // at its window. The compositor names the states that are set in each // xdg_toplevel.configure and ends the batch with an xdg_surface.configure; // the window dropped the array, so a consumer took a window behind a lock // screen or another window for one that was seen (a messenger marks a text // read there and never notifies of it). // // The surfaces are proxies on a wl_display connected to one end of a // socketpair, as in TitleWayland, and the test calls the listener callbacks // itself, so no compositor is needed. Asserts: // // - the states of a configure are pending until the xdg_surface.configure // that ends its batch, and the last xdg_toplevel.configure of a batch is // the one applied // - onToplevelState finds both values set, and fires once for a batch // that changes both // - a batch that repeats the states, or changes other ones only // (maximized, resizing, tiled), fires nothing // - a state the array leaves out is not set // - xdg_wm_base is bound at the version the compositor offers, up to 6 // (where `suspended` is) and never above the offer: a bind above it is // a protocol error that ends the connection, so on a compositor with an // older xdg_wm_base no window would open at all // - the listeners of xdg_wm_base and of the xdg_surface and xdg_toplevel // made from it have a handler for every event of the bundled protocol: // the higher version brings xdg_toplevel's configure_bounds (4) and // wm_capabilities (5), and libwayland aborts a client at an event whose // handler is null #include #include #include #include #include #include #include "../../lib/xdg-shell-client-protocol.h" import Crafter.Graphics; import Crafter.Event; 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; } // What the display has written so far. std::vector Sent(wl_display* display, int peer) { 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); } return wire; } // The versions of the wl_registry.bind requests for `interface` in `wire`. A // request is its object id, then its size and opcode in one word, then its // arguments; bind's are the global's name, the interface as a string (its // length with the NUL, then its bytes padded to a word), the version and // the new object's id. std::vector BoundVersions(const std::vector& wire, std::uint32_t registry, std::string_view interface) { std::vector versions; 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; if (size < 8 || at + size > wire.size()) break; if (header[0] == registry && (header[1] & 0xFFFF) == WL_REGISTRY_BIND && size >= 16) { std::uint32_t length; std::memcpy(&length, wire.data() + at + 12, sizeof(length)); const std::size_t padded = (static_cast(length) + 3) & ~static_cast(3); if (length > 0 && 16 + padded + 8 <= size && std::string_view(reinterpret_cast(wire.data() + at + 16), length - 1) == interface) { std::uint32_t version; std::memcpy(&version, wire.data() + at + 16 + padded, sizeof(version)); versions.push_back(version); } } at += size; } return versions; } // The compositor's two events of one batch: the toplevel's configure with // the states that are set, then the surface's. void Toplevel(Window& window, std::initializer_list states) { wl_array array; wl_array_init(&array); for (std::uint32_t state : states) { *static_cast(wl_array_add(&array, sizeof(state))) = state; } Window::xdg_toplevel_configure(&window, window.xdgToplevel, 0, 0, &array); wl_array_release(&array); } std::uint32_t serial = 0; void Batch(Window& window, std::initializer_list states) { Toplevel(window, states); Window::xdg_surface_handle_configure(&window, window.xdgSurface, ++serial); } // Whether `listener` has a handler for every event of `interface`. A // listener is one function pointer per event, in the protocol's order. template bool HandlesEveryEvent(const Listener& listener, const wl_interface& interface) { void (*handler)(void); if (sizeof(Listener) != static_cast(interface.event_count) * sizeof(handler)) return false; const unsigned char* at = reinterpret_cast(&listener); for (int i = 0; i < interface.event_count; ++i, at += sizeof(handler)) { std::memcpy(&handler, at, sizeof(handler)); if (handler == nullptr) return false; } return true; } // The window's two states, as a listener or the test found them. struct Seen { bool activated; bool suspended; bool operator==(const Seen&) const = default; }; } // 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 std::uint32_t registryId = wl_proxy_get_id(reinterpret_cast(registry)); { // A default-constructed window, which ~Window can tear down without // a device, holding proxies as its surfaces: the surface's configure // acknowledges on the xdg_surface and, once the window is // configured, commits the wl_surface. Window window; 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)); std::vector seen; EventListener sub(&window.onToplevelState, [&] { seen.push_back({ window.activated, window.suspended }); }); const auto now = [&] { return Seen{ window.activated, window.suspended }; }; Check(now() == Seen{ false, false }, "a new window is neither activated nor suspended"); Toplevel(window, { XDG_TOPLEVEL_STATE_ACTIVATED }); Check(now() == Seen{ false, false } && seen.empty(), "a toplevel configure alone changes nothing: its states are pending"); Window::xdg_surface_handle_configure(&window, window.xdgSurface, ++serial); Check(seen == std::vector{ { true, false } }, "the surface configure applies them: activated, set before onToplevelState fires"); seen.clear(); Batch(window, { XDG_TOPLEVEL_STATE_ACTIVATED }); Batch(window, { XDG_TOPLEVEL_STATE_MAXIMIZED, XDG_TOPLEVEL_STATE_ACTIVATED, XDG_TOPLEVEL_STATE_RESIZING, XDG_TOPLEVEL_STATE_TILED_LEFT }); Check(seen.empty() && now() == Seen{ true, false }, "the same states again, and other states changing, fire nothing"); Batch(window, { XDG_TOPLEVEL_STATE_SUSPENDED }); Check(seen == std::vector{ { false, true } }, "suspended in, activated left out: both changed, one event"); seen.clear(); Batch(window, { XDG_TOPLEVEL_STATE_SUSPENDED, XDG_TOPLEVEL_STATE_ACTIVATED }); Check(seen == std::vector{ { true, true } }, "the two are independent: a window can be both"); seen.clear(); Batch(window, {}); Check(seen == std::vector{ { false, false } }, "an empty array clears both"); seen.clear(); Toplevel(window, { XDG_TOPLEVEL_STATE_SUSPENDED }); Toplevel(window, { XDG_TOPLEVEL_STATE_ACTIVATED }); Window::xdg_surface_handle_configure(&window, window.xdgSurface, ++serial); Check(seen == std::vector{ { true, false } }, "two toplevel configures in one batch: the last one is applied"); Sent(display, fds[1]); // ~Window destroys the three proxies. } // The bind. handle_global keeps the proxy in Device::xdgWmBase; each is // destroyed here before the next. std::vector bound; for (std::uint32_t offered : { 7u, 6u, 5u, 1u }) { Device::handle_global(nullptr, registry, 4, xdg_wm_base_interface.name, offered); for (std::uint32_t version : BoundVersions(Sent(display, fds[1]), registryId, xdg_wm_base_interface.name)) bound.push_back(version); if (Device::xdgWmBase != nullptr) xdg_wm_base_destroy(Device::xdgWmBase); Device::xdgWmBase = nullptr; } Check(bound == std::vector{ 6, 6, 5, 1 }, "xdg_wm_base offered at 7, 6, 5 and 1 is bound at 6, 6, 5 and 1"); if (bound != std::vector{ 6, 6, 5, 1 }) { for (std::uint32_t version : bound) std::println(" bound at {}", version); } // What the bind brings must find a handler. Check(HandlesEveryEvent(Device::xdgWmBaseListener, xdg_wm_base_interface) && HandlesEveryEvent(Window::xdg_surface_listener, xdg_surface_interface) && HandlesEveryEvent(Window::xdg_toplevel_listener, xdg_toplevel_interface), "the listeners of xdg_wm_base, xdg_surface and xdg_toplevel handle every event of the bundled protocol"); 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; }