feat(wayland): keep xdg_toplevel's activated and suspended states on the window

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.
This commit is contained in:
Jorijn van der Graaf 2026-11-17 00:49:53 +01:00
commit c006519fa0
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
7 changed files with 310 additions and 4 deletions

View file

@ -318,7 +318,12 @@ void Device::handle_global(void *data, wl_registry *registry, std::uint32_t name
} else if (compositor == nullptr && strcmp(interface, wl_compositor_interface.name) == 0) {
compositor = reinterpret_cast<wl_compositor*>(wl_registry_bind(registry, name, &wl_compositor_interface, 3));
} else if (strcmp(interface, xdg_wm_base_interface.name) == 0) {
xdgWmBase = reinterpret_cast<xdg_wm_base*>(wl_registry_bind(registry, name, &xdg_wm_base_interface, 1));
// Up to version 6, for xdg_toplevel's `suspended` state
// (Window::suspended); `activated` is there from version 1. The
// requests the windows make are all version 1's. On the way come
// xdg_toplevel's configure_bounds (4) and wm_capabilities (5), which
// Window's listener takes and drops.
xdgWmBase = reinterpret_cast<xdg_wm_base*>(wl_registry_bind(registry, name, &xdg_wm_base_interface, std::min<std::uint32_t>(version, 6)));
xdg_wm_base_add_listener(xdgWmBase, &xdgWmBaseListener, nullptr);
} else if (strcmp(interface, zxdg_decoration_manager_v1_interface.name) == 0) {
manager = reinterpret_cast<zxdg_decoration_manager_v1*>(wl_registry_bind(registry, name, &zxdg_decoration_manager_v1_interface, 1));

View file

@ -1871,13 +1871,33 @@ void Window::wl_surface_frame_done(void* data, struct wl_callback *cb, uint32_t
}
void Window::xdg_toplevel_configure(void* data, xdg_toplevel*, std::int32_t width, std::int32_t height, wl_array*){
void Window::xdg_toplevel_configure(void* data, xdg_toplevel*, std::int32_t width, std::int32_t height, wl_array* states){
// xdg-shell batches state — width/height are pending until the matching
// xdg_surface.configure arrives. Width/height are in surface-local
// (logical DP) units; (0, 0) means "compositor has no preference".
Window* window = reinterpret_cast<Window*>(data);
window->pendingLogicalWidth = width;
window->pendingLogicalHeight = height;
// The states are batched with them. The array lists the ones that are
// set: one it leaves out is not.
window->pendingActivated = false;
window->pendingSuspended = false;
const std::uint32_t* state = static_cast<const std::uint32_t*>(states->data);
for (std::size_t i = 0; i < states->size / sizeof(std::uint32_t); ++i) {
if (state[i] == XDG_TOPLEVEL_STATE_ACTIVATED) window->pendingActivated = true;
else if (state[i] == XDG_TOPLEVEL_STATE_SUSPENDED) window->pendingSuspended = true;
}
}
void Window::xdg_toplevel_configure_bounds(void*, xdg_toplevel*, std::int32_t, std::int32_t) {}
void Window::xdg_toplevel_wm_capabilities(void*, xdg_toplevel*, wl_array*) {}
void Window::ApplyToplevelState() {
if (activated == pendingActivated && suspended == pendingSuspended) return;
activated = pendingActivated;
suspended = pendingSuspended;
onToplevelState.Invoke();
}
void Window::xdg_toplevel_handle_close(void* data, xdg_toplevel*) {
@ -1910,6 +1930,8 @@ void Window::xdg_surface_handle_configure(void* data, xdg_surface* xdg_surface,
}
window->configured = true;
// Last: the handler does nothing with the window after its listeners.
window->ApplyToplevelState();
}
void Window::xdg_surface_handle_preferred_scale(void* data, wp_fractional_scale_v1*, std::uint32_t scale) {