I'm running KDE Plasma on a laptop with a built-in screen and an external monitor. I want to write a Bash script that toggles the layout of my external monitor (HDMI-A-1) between:
Above the laptop screen (eDP-1)
To the right of the laptop screen
I'm using xrandr, and here's the relevant output from xrandr --verbose | grep -E 'connected|[0-9]+x[0-9]+\+[0-9]+\+[0-9]+'
eDP-1 connected primary 1920x1080+0+1080 (0x25) normal (normal left inverted right x axis y axis) 293mm x 165mm
HDMI-A-1 connected 1920x1080+0+0 (0x25) normal (normal left inverted right x axis y axis) 531mm x 299mm
the other lay out (on the right):
eDP-1 connected primary 1920x1080+0+0 (0x25) normal (normal left inverted right x axis y axis) 293mm x 165mm
HDMI-A-1 connected 1920x1080+1920+0 (0x25) normal (normal left inverted right x axis y axis) 531mm x 299mm
Based on that, I'm checking the position of eDP-1
to determine the layout and using xrandr
to switch between them. Here's my current script:
#!/bin/bash
PRIMARY="eDP-1"
SECONDARY="HDMI-A-1"
PRIMARY_POS=$(xrandr --query | grep "^$PRIMARY connected" | grep -oP '\+\d+\+\d+')
if [[ "$PRIMARY_POS" == "+1080+0" ]]; then
xrandr --output "$SECONDARY" --off
xrandr --output "$SECONDARY" --auto --above "$PRIMARY"
elif [[ "$PRIMARY_POS" == "+0+0" ]]; then
xrandr --output "$SECONDARY" --off
xrandr --output "$SECONDARY" --auto --right-of "$PRIMARY"
else
xrandr --output "$SECONDARY" --off
xrandr --output "$SECONDARY" --auto --right-of "$PRIMARY"
fi
This just ends up failing with:
X Error of failed request: BadMatch (invalid parameter attributes)
This also happens when using arandr
to generate the scripts.
I suspect it has something to do with KDE’s own screen configuration tools getting in the way. Is there a better or more reliable way to do this with xrandr, or should I be using a KDE-specific tool instead?
Any tips for making this script more robust — or KDE-friendly — would be appreciated!
xrandr
does not work reliable on Wayland, it seems. Try wlr-randr, gnome-randr, kanshi, wdisplays or kscreen-doctor...