Friday, March 7, 2008

Switching display modes on a laptop using xrandr

I have written this script quite a while back and already forgot where I've got the original idea. I think it was from somewhere inside the ubuntu forums. Whichever way it is, I find it quite useful when I have to use a laptop to display to a projector but the function keys to switch display is not configured or is not working. You have to have zenity installed though. Just a simple "sudo apt-get install zenity" on Ubuntu (or "pacman -S zenity" on Arch linux ;) and you can use it.

#!/bin/sh

whichmode=`zenity --list --radiolist \
--title "Change Display Mode" \
--text "Choose where you want to display your desktop." \
--column="" --column="" --column="Options" \
"" "1" "Just the Laptop's LCD" \
"" "2" "Just the Output Monitor" \
"" "3" "VGA Dual Monitor" \
"" "4" "VGA Cloned (projector mode)"`

if [ $whichmode -eq 1 ]
then
xrandr --output VGA --off --output LVDS --auto
fi
if [ $whichmode -eq 2 ]
then
if xrandr -q | grep -q "VGA connected"; then
xrandr --output LVDS --off --output VGA --mode 1024x768
fi
fi
if [ $whichmode -eq 3 ]
then
if xrandr -q | grep -q "VGA connected"; then
xrandr --output VGA --mode 1024x768 --left-of LVDS --output LVDS --mode 1280x768
fi
fi
if [ $whichmode -eq 4 ]
then
if xrandr -q | grep -q "VGA connected"; then
xrandr --output VGA --mode 1024x768 --pos 0x0 --output LVDS --mode 1280x768 --pos 0x0
fi
fi

I have to warn you thought that I have my monitor on the left of my laptop. So if you have it on the right of your laptop, then you might want to change --left-of to --right-of. Pretty neat.

p/s: If the Dual Monitor mode does not work, try running it from a terminal and see what error it output. If it says something along the line of "screen cannot be larger than x y" then go edit your xorg.conf (Most of the time it is at /etc/X11/xorg.conf, got to be root to edit it) and add the line "Virtual x y" (replacing x and y from the error) in the "Display" subsection under the "Screen" section.

No comments:

Haiya ala Solah