So I seem to have finally resolved my long-standing issue with Xwayland always forgetting my primary display I give it with xrandr under Sway, making fullscreen games all sorts of broken with multiple displays.
As you may or may not know, neither the Wayland protocol nor Sway have any concept of a primary output, as opposed to the older X protocol, in which most fullscreen applications use that concept to choose which screen to fill. This is a problem, because Sway does not allow you to sanely set a primary monitor for X applications to use, and those then keep misbehaving (setting weird resolutions, showing up on the wrong monitors, etc.).
To add some more context, I have an admittedly gnarly ever-evolving Python script in my dotfiles that manages all my multi-monitor setups. At its end, this script runs xrandr --output {name} --primary to resolve the issue with fullscreen X applications. The problem is, for some time now, the setting wouldn’t stick. I would have to remember to re-run the script before starting a game so that it would fill the correct display.
Today, I finally found out why this was happening, and the solution is a simple one, really.
By default, Sway uses wlroots’ lazy mode for Xwayland, which only keeps the Xwayland server running when there are actual X clients running. There is some hold-off period, but after the last X client has stopped, the Xwayland server is stopped eventually as well in this mode. Since the primary display setting is only kept in-memory by the server, it’s lost every time it stops.
I was originally thinking about having some sort of a hook that would run every time Xwayland started to set the primary display, but honestly, there is a much simpler solution:
xwayland force
Putting this in your Sway config makes the Xwayland server run permanently, so setting the primary output once will keep it around for the rest of the Sway session. Yes, this means that Xwayland will keep about a hundred megs of “idle” memory around1, but honestly, that’s the most hassle-free way to solve this issue. Arguably, having to repeatedly start and stop the Xwayland server might be a bigger waste of resources than just keeping it around.