2012-04-29

Display modes for xrandr

I got a new laptop and decided to use ScrotWM as my desktop. The details of the setup will be covered in a later post. For now I would like to share with the world a quick and dirty script I made to cycle trough the different display modes one would expect to achieve when pressing the XF86Display button.
I had some trouble getting ScrotWM to recognise some other XF86* key-bindings (mainly the volume buttons), so I installed xbindkeys and configured the shortcuts with that.
So I set-up a script in /usr/local/bin/ and called it "xrandr-cycle".
There are four display modes I could imagine would be of use to me:
  1. Show all external displays as being right-of the laptop's own display.
  2. Show the same picture on all displays.
  3. Show output on the external displays only (useful for docking).
  4. Turn all external displays off and only use the internal display.
Havig considered these modes, here is what I came up with:
#!/bin/bash
#--help
if [ "$1" == "--help" ];then
        echo "`basename $0` - this script is intended to cycle trough all the different display modes"
        exit 0
fi
#basenline variables
_internaldisplay="LVDS1"
_internaloffset="`xrandr -q | grep -A 1 $_internaldisplay | tail -1 | tr -s ' ' | cut -d' ' -f2 | cut -d'x' -f1`"
#working part
xrandr -q | cut -d' ' -f1-3 | grep -v "Screen" | while read _monitor _status _mode;do
        if [ -z "$_monitor" ] || [ "$_status" == "disconnected" ];then
                continue
        fi
        if [ "$_monitor" == "$_internaldisplay" ] && [ "$_mode" == "(normal" ]; then
                _internal=false
        elif [ "$_monitor" == "$_internaldisplay" ] && [ "$_mode" != "(normal" ]; then
                _internal=true
        else
                if $_internal && [ "$_mode" == "(normal" ];then
                        echo "Internal online and $_monitor offline - showing right-of"
                        xrandr --output $_monitor --right-of $_internaldisplay --auto
                        nitrogen --restore
                elif $_internal && [ "`echo $_mode | cut -d'+' -f2`" == "$_internaloffset" ];then
                        echo "Internal online and $_monitor right-of - showing same"
                        xrandr --output $_internaldisplay --off
                        xrandr --auto
                        nitrogen --restore
                elif $_internal && [ "`echo $_mode | cut -d'+' -f2`" == "0" ];then
                        echo "Internal online and $_monitor online - turning internal off"
                        xrandr --output $_internaldisplay --off
                        nitrogen --restore
                elif ! $_internal;then
                        echo "Internal offline - turning internal on and evrything else off"
                        xrandr --output $_internaldisplay --auto
                        xrandr --output $_monitor --off
                        nitrogen --restore
                fi

        fi
done
If you would like to use it for your own systems, be sure to change the _internaldisplay variable to the correct value.

P.S. Yes I know one could get the _internaldisplayoffset using some sed or awk magic, but I have yet to master those and this also works.
P.P.S. After having seen a video about licensing I should add one to this script as well. Since this is stupid/simple Bash, BSD should be more than enough.