I have a laptop connected to a external monitor. I want to configure the display settings automatically when the external monitor is connected or disconnected. So, i understand that this can be accomplished using udev events and a bash script using xrandr.
#!/bin/sh
set -e
VGA_STATUS=$(</sys/class/drm/card0/card0-VGA-1/status)
if [ "connected" == $VGA_STATUS ]; then
/usr/bin/xrandr --output LVDS1 --noprimary --auto
/usr/bin/xrandr --output VGA1 --primary --mode 1920x1080 --left-of LVDS1
echo "VGA plugged in" >> /home/my_username/monitor.log
else
/usr/bin/xrandr --output VGA1 --off
/usr/bin/xrandr --output LVDS1 --primary --auto
echo "External monitor disconnected" >> /home/my_username/monitor.log
exit
fi
KERNEL=="card0", SUBSYSTEM=="drm", ACTION=="change", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/run/user/1000/gdm/Xauthority", RUN+="/bin/bash /path/to/my/script.sh"
I tested the bash script and it works in both cases. The problem i have is that the udev rule only works when the monitor gets disconnected but not in the other case. The log file shows the "External monitor disconnected" message but it doesn't show the "VGA plugged in" message.
This is the output of xrandr -q
when i disconnect the external monitor:
Screen 0: minimum 8 x 8, current 1366 x 768, maximum 32767 x 32767
LVDS1 connected primary 1366x768+0+0 (normal left inverted right x axis y axis) 310mm x 170mm
1366x768 59.98*+
1024x768 60.00
1024x576 60.00
960x540 60.00
800x600 60.32 56.25
864x486 60.00
640x480 59.94
720x405 60.00
680x384 60.00
640x360 60.00
DP1 disconnected (normal left inverted right x axis y axis)
DP2 disconnected (normal left inverted right x axis y axis)
DP3 disconnected (normal left inverted right x axis y axis)
HDMI1 disconnected (normal left inverted right x axis y axis)
HDMI2 disconnected (normal left inverted right x axis y axis)
HDMI3 disconnected (normal left inverted right x axis y axis)
VGA1 disconnected (normal left inverted right x axis y axis)
VIRTUAL1 disconnected (normal left inverted right x axis y axis)
This is the output of xrandr -q
after the external monitor is connected:
Screen 0: minimum 8 x 8, current 1366 x 768, maximum 32767 x 32767
LVDS1 connected 1366x768+0+0 (normal left inverted right x axis y axis) 310mm x 170mm
1366x768 59.98*+
1024x768 60.00
1024x576 60.00
960x540 60.00
800x600 60.32 56.25
864x486 60.00
640x480 59.94
720x405 60.00
680x384 60.00
640x360 60.00
DP1 disconnected (normal left inverted right x axis y axis)
DP2 disconnected (normal left inverted right x axis y axis)
DP3 disconnected (normal left inverted right x axis y axis)
HDMI1 disconnected (normal left inverted right x axis y axis)
HDMI2 disconnected (normal left inverted right x axis y axis)
HDMI3 disconnected (normal left inverted right x axis y axis)
VGA1 connected (normal left inverted right x axis y axis)
1920x1080 60.00 +
1600x900 60.00
1280x1024 75.02 60.02
1152x864 75.00
1024x768 75.03 60.00
800x600 75.00 60.32
640x480 75.00 59.94
720x400 70.08
VIRTUAL1 disconnected (normal left inverted right x axis y axis)
This is the output of xrandr -q
after the external monitor is connected and i run the bash script manually:
Screen 0: minimum 8 x 8, current 3286 x 1080, maximum 32767 x 32767
LVDS1 connected 1366x768+1920+0 (normal left inverted right x axis y axis) 310mm x 170mm
1366x768 59.98*+
1024x768 60.00
1024x576 60.00
960x540 60.00
800x600 60.32 56.25
864x486 60.00
640x480 59.94
720x405 60.00
680x384 60.00
640x360 60.00
DP1 disconnected (normal left inverted right x axis y axis)
DP2 disconnected (normal left inverted right x axis y axis)
DP3 disconnected (normal left inverted right x axis y axis)
HDMI1 disconnected (normal left inverted right x axis y axis)
HDMI2 disconnected (normal left inverted right x axis y axis)
HDMI3 disconnected (normal left inverted right x axis y axis)
VGA1 connected primary 1920x1080+0+0 (normal left inverted right x axis y axis) 530mm x 300mm
1920x1080 60.00*+
1600x900 60.00
1280x1024 75.02 60.02
1152x864 75.00
1024x768 75.03 60.00
800x600 75.00 60.32
640x480 75.00 59.94
720x400 70.08
VIRTUAL1 disconnected (normal left inverted right x axis y axis)
What i'm doing wrong?
i'm in Fedora 25 using i3.
#!/bin/sh
if you run it with/bin/bash
? – Dmitry Grigoryev Apr 24 '17 at 14:30ENV{XAUTHORITY}="/run/user/1000/gdm/Xauthority"
is not enough? – Dmitry Grigoryev Apr 24 '17 at 14:34DISPLAY
as well, and obviously this'll only work if you use gdm, it's already started and user 1000 is logged in. So in particular it won't work if the monitor is plugged in at boot time. – Gilles 'SO- stop being evil' Apr 24 '17 at 19:40