When you purchase through links on our site, we may earn an affiliate commission. Here’s how it works
One of the most interesting things about Wayland is how it handles window focus, unlike X11, where focus stealing can be frustrating and even a security risk. Its main advantage is a mechanism that prevents focus stealing. The protocol that plays a role in this is known as "XDG Activation."
Here's how it works: Say you double-click a PDF file in your file manager. The file manager first asks the Wayland compositor for a special, single-use "activation token". This request is tied directly to your click, proving a human wanted this to happen.
The file manager then launches your PDF viewer and hands it the token. The PDF viewer, upon starting, shows this token to the compositor and asks to be activated. The compositor checks if the token is legit and, if so, gives the PDF viewer focus.
If the token is missing, old, or otherwise invalid, the compositor says no. The viewer window will not get focus, and instead, its icon in the taskbar will start flashing to grab your attention.
Flashing icon in the taskbar | Image: Kai-Uwe Broulik
Kai-Uwe Broulik, a KDE developer, recently wrote about the plan to "switch on KWin’s focus stealing on Wayland at a low level". This means KWin, the window manager for KDE Plasma, will begin enforcing this properly.
Under X11, new or dialog windows can only grab focus if their application was most recently active, a check often based on a timestamp called _NET_WM_USER_TIME . It was a flimsy system at best. For example, Kai cited how the prevention logic on X11 would sometimes stop the Adobe Flash Player fullscreen window from showing on top of a YouTube video. On X11, an application could just call XSetInputFocus on another app's window, and while KWin would try to undo it, focus did flicker for a moment.
Over on Wayland, things are much better with XDG Activation, but some apps still violate the protocol through improper usage. In situations like that, KWin would, by default, just focus any new window that opened. This is changing.
A new "Extreme" setting for "Focus Stealing Prevention" in the Window Management settings will force KWin to activate a window if and only if it requests activation with a valid token.
Using this stricter mode, developers Xaver Hugl and Kai-Uwe Broulik have already fixed a ton of issues, introducing several key changes:
Dolphin no longer discards its activation token when launching a new instance.
KRunner, Kickoff, and other Plasmoid popups now correctly request activation.
LayerShell-Qt now requests activation on show and properly reads the XDG_ACTIVATION_TOKEN from the environment.
from the environment. Allowing privileged clients like Plasma to request tokens correctly.
Ignoring modifier key presses for focus prevention logic, since they are often part of a global shortcut.
The work extends to the backend as well, with the DBusRunner specification now gaining a SetActivationToken method that is called just before an action runs. Baloo, the desktop search runner, now uses this to ensure that opening files in an existing application window works correctly.
You can learn more on Kai's blog post.