Auto detecting monitors in Linux

When using an external monitor to my laptop I have set my X to enable TwinView and have the larger external display be primary display left of the laptop display. But when I am away from home and does not have external monitor connected the nv driver does not detect that and still set up TwinView as before leaving much of the desktop out of display. To remedy this I have made two xorg.conf, one for single display and one for dual.

I have copied these manually before when I’m switching. Now I have something slightly better. I start X, run xrandr, capture its output and copy one of the configs depending on what’s in the output. This is how I do it

rm -f /tmp/xrandr /tmp/nullconf
/usr/bin/xinit /usr/bin/xrandr -q -- /usr/bin/X -config /tmp/nullconf -quiet >/tmp/xrandr 2>/dev/null
if grep -q '^DVI.* connected' /tmp/xrandr ; then
cp -f /etc/X11/xorg.conf.dual /etc/X11/xorg.conf
else
cp -f /etc/X11/xorg.conf.single /etc/X11/xorg.conf
fi
rm -f /tmp/nullconf

My external monitor is detected as a DVI and the internal as LVDS

Leave a comment