How to Set Primary X Display on Sway
In my PC setup, I have three displays. My somewhat recent post about my setup is outdated now; from left to right I have a 1440x900 60Hz monitor oriented vertically, a 1920x1080 120Hz monitor, and a 1920x1080 60Hz monitor.
I noticed some games would try to run at the resolution and refresh rate of my leftmost vertical monitor, which is not what I want. There is no way to set a “primary display” in Sway, because that construct doesn’t exist in Wayland. Applications simply open on the display you have focused.
However, many applications (particularly games) will try to query for the primary display.
XWayland will (in my experience) default to setting the primary display to the one which is the closest to the “origin” (top left pixel).
This can be remedied by running
xrandr --listmonitors to find what X thinks the names of your displays are, and then using
xrandr --output XWAYLANDN --primary where N is the number of the correct display, to set the primary display.
I wrote a basic script to handle this:
#!/bin/sh display=$(xrandr --listactivemonitors | grep "$1" | head -n1 | cut -d' ' -f6) if [ -n "$display" ]; then xrandr --output $display --primary fi
I have this saved in ~/.local/bin/set-correct-monitor so I can use it anywhere.
What this does is
grep the output of
xrandr --listactivemonitors for the script argument, and set the display to the first display that matches.
For example, I run
set-correct-monitor 300+900 to set my display, since that is the position of the monitor I want to be considered my primary.
Doing this will work until you do one of several things:
- otherwise kill Sway
- disconnect a monitor
The first two were pretty okay for me to deal with for some time, since usually I only logout once a day, and many games don’t run into an issue with this, particularly if they are run in “windowed mode” (which I recommend). The last was pretty frustrating for me though, because I use a KVM switch to change between my work laptop and my PC. Every time I switch between devices, my primary display usually defaults back to my vertical monitor.
To make this setting persist, I made a small C program which will execute a command each time a new display is attached. The program can be found here: https://git.robbyzambito.me/robby/wayland-run-on-new-display.git
By adding this line to my ~/.config/sway/config, my primary display is always properly set when I run Sway:
exec wayland-run-on-new-display set-correct-monitor 300+900