A consumer could not tell a window somebody is looking at from one that merely exists. The compositor's word on it comes with every xdg_toplevel configure — the `activated` state, and since version 6 of xdg_wm_base `suspended`, "not ordinarily being repainted", for example for a content covered by another window or outputs switched off for a screen lock — and the window dropped the states array. A messenger built on it took a conversation left open behind a lock screen or another application for one that was being read: its texts were marked read and never notified. Window: `activated` and `suspended` hold the two states, and onToplevelState fires after a configure changed either, with both already set. They are batched as the size is: xdg_toplevel.configure leaves them pending and the xdg_surface.configure that ends the batch applies them (ApplyToplevelState) last, so the handler does nothing with the window after its listeners. The constructor takes the first configure before a listener can be added, so a consumer reads the two once after it. Both are reported and the library combines nothing: which of the two a lock screen or another window in front changes is the compositor's choice, so a consumer that asks whether the user is looking at the window wants `activated && !suspended`, and the consumer notes say so. The members are the Wayland backend's; the Win32 and DOM backends keep no such state. Device: xdg_wm_base is bound at the version the compositor offers, up to 6, where it was bound at 1: `suspended` does not exist below 6 (`activated` is version 1's). Every request the windows make is version 1's. Besides `suspended`, the higher versions bring the window state values it passes over (the tiled ones, 2) and two events, xdg_toplevel's configure_bounds (4) and wm_capabilities (5), which the toplevel's listener takes and drops, since libwayland aborts a client at an event whose handler is null. Tests: ToplevelState feeds the two configure callbacks of a window whose surfaces are proxies on a wl_display over a socketpair, as TitleWayland makes them, and reads the bind off the wire: the states pending until the surface's configure, the last toplevel configure of a batch applied, one event for a batch that changes both, none for a repeat or for other states, an empty array clearing both; an offer of 7, 6, 5 and 1 bound at 6, 6, 5 and 1; a handler in the xdg_wm_base, xdg_surface and xdg_toplevel listeners for every event of the bundled protocol.
234 lines
11 KiB
C++
234 lines
11 KiB
C++
//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 <wayland-client.h>
|
|
#include <sys/socket.h>
|
|
#include <unistd.h>
|
|
#include <cerrno>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#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<std::uint8_t> Sent(wl_display* display, int peer) {
|
|
wl_display_flush(display);
|
|
std::vector<std::uint8_t> 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<std::uint32_t> BoundVersions(const std::vector<std::uint8_t>& wire, std::uint32_t registry, std::string_view interface) {
|
|
std::vector<std::uint32_t> 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<std::size_t>(length) + 3) & ~static_cast<std::size_t>(3);
|
|
if (length > 0 && 16 + padded + 8 <= size
|
|
&& std::string_view(reinterpret_cast<const char*>(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<std::uint32_t> states) {
|
|
wl_array array;
|
|
wl_array_init(&array);
|
|
for (std::uint32_t state : states) {
|
|
*static_cast<std::uint32_t*>(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<std::uint32_t> 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 <class Listener>
|
|
bool HandlesEveryEvent(const Listener& listener, const wl_interface& interface) {
|
|
void (*handler)(void);
|
|
if (sizeof(Listener) != static_cast<std::size_t>(interface.event_count) * sizeof(handler)) return false;
|
|
const unsigned char* at = reinterpret_cast<const unsigned char*>(&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<wl_proxy*>(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_surface*>(wl_registry_bind(registry, 1, &wl_surface_interface, 1));
|
|
window.xdgSurface = static_cast<xdg_surface*>(wl_registry_bind(registry, 2, &xdg_surface_interface, 1));
|
|
window.xdgToplevel = static_cast<xdg_toplevel*>(wl_registry_bind(registry, 3, &xdg_toplevel_interface, 1));
|
|
|
|
std::vector<Seen> seen;
|
|
EventListener<void> 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<Seen>{ { 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<Seen>{ { 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<Seen>{ { true, true } }, "the two are independent: a window can be both");
|
|
seen.clear();
|
|
|
|
Batch(window, {});
|
|
Check(seen == std::vector<Seen>{ { 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<Seen>{ { 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<std::uint32_t> 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<std::uint32_t>{ 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<std::uint32_t>{ 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;
|
|
}
|