redstrate.com/content/blog/wayland-ssd/index.md
2023-07-03 11:03:07 -04:00

2.7 KiB

title date draft tags
How to use xdg-decoration in a Wayland client 2023-07-03 false
Wayland

I wanted to enable server-side decorations in a Wayland client, but didn't know where to start. When searching for "How to enable server-side decorations wayland" in a search engine you get a lot of user support questions and not really anything for programmers. Here's a short tutorial on how to request server side decorations from the compositor if supported.

Which protocol to use?

When searching for the Wayland protocol to use, you'll come across two different ones:

What is the difference between the two? From what I can tell the first extension was by KDE and put into their protocol collection. It doesn't appear to be used anymore, and not even installed on my system. xdg-decoration is actually in wayland-protocols (albeit unstable) so we'll use that one.

If you're using ECM (Extra CMake Modules) then use ecm_add_wayland_client_protocol to generate the header:

ecm_add_wayland_client_protocol(
    WAYLAND_SOURCES
    BASENAME
    xdg-decoration-v1
    PROTOCOL
    ${WaylandProtocols_DATADIR}/unstable/xdg-decoration/xdg-decoration-unstable-v1.xml
)

How to use xdg-decoration

When enumerating the global registry, bind the zxdg_decoration_manager_v1 struct using the zxdg_decoration_manager_v1_interface interface:

auto manager = reinterpret_cast<zxdg_decoration_manager_v1 *>(wl_registry_bind(registry, id, &zxdg_decoration_manager_v1_interface, 1));

Using the decoration manager, you want to set the "mode" of the decoration to either client side or server side:

auto decoration = zxdg_decoration_manager_v1_get_toplevel_decoration(manager, toplevel); // toplevel is from xdg_surface_get_toplevel

zxdg_toplevel_decoration_v1_set_mode(decoration, ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE);

And that's it, you'll get server-side decorations enabled for your client:

Window decorations!

There's one more part of the extension I didn't cover, and that's the "configure" event. According to the protocol spec, it's possible that the compositor will change the decoration mode from underneath your feet. Is this something that actually happens in real world Wayland compositors? I can't imagine this happens often, although I'm curious if you've heard of this.