What's a clean way to check if my monitors are being mirrored or not with xrandr?
Background
I'm using polybar and as of now I have a script that launches an extra polybar if my second monitor is connected. Problem is that when I'm mirroring I don't want to launch that second bar. Here's my code if somebody's curious:
#!/bin/bash
# Terminate already running bar instances
killall -q polybar
# Wait until the processes have been shut down
while pgrep -u $UID -x polybar >/dev/null; do sleep 1; done
# Launch bar
polybar main_bar -r &
my_laptop_external_monitor=$(xrandr --query | grep 'HDMI-1')
if [[ $my_laptop_external_monitor = *connected* ]]; then
polybar external_bar &
fi
I ran xrandr --listmonitors
on my system, with and without cloned monitors:
Not cloned:
Monitors: 2
0: +*DisplayPort-0 1920/598x1080/336+1920+0 DisplayPort-0
1: +HDMI-0 1920/598x1080/336+0+0 HDMI-0
Cloned
Monitors: 2
0: +*DisplayPort-0 1920/598x1080/336+0+0 DisplayPort-0
1: +HDMI-0 1920/598x1080/336+0+0 HDMI-0
The only difference is the output's position within the screen, 1920+0
vs 0+0
. When the monitor is mirrored, the position of both monitors are the same (both 0+0
).
If you mirror your monitors this way 1, a good way to check if a monitor is mirrored is to compare their positions in the ouput of xrandr --listmonitors
.
I don't think there is a cleaner way than this. As far as I can tell, you don't actually mirror or clone monitors you only set their positions inside to screen in such a way that they overlap and when rendering, both monitors show the same picture because they're at the same location. That's why there isn't really a straightforward way to test for mirrored screens.
1Side Note: The monitor was cloned with the following command
xrandr --output HDMI-0 --same-as DisplayPort-0