Docking station support

I have a Toshiba laptop and use Ubuntu primarily.  Though the current Linux kernel has very nice support for my hardware, it still isn't too intelligent about when I'm using a docking station or when I'm not.

The only differences of my setup while docked is that when I have my HDMI monitor plugged in to my laptop, I want both sound and video to go through it.  I also want my resolution to be adjusted.  This script, assigned to a hotkey for convenience, takes care of this for me.

The general logic flow is:

Granted, Ubuntu usually does a pretty good job of setting the display without me telling it to.  I found the trick is, with HDMI connected, use the Displays control panel to set the display to HDMI and the appropriate resolution.  From there it will always pick up that setting when HDMI is connected, and degrade gracefully to my LCD screen (and proper resolution) when I disconnect HDMI.  The audio doesn't switch over though, so I still have use for this script from time to time.

#!/bin/bash

lsusb | grep -q CM106 && USB_SOUND=1

LVDS=`xrandr | grep -n "LVDS1 connected" | awk -F: '{printf "%s",$1}'`

VGA=`xrandr | grep -n "VGA1 connected" | awk -F: '{printf "%s",$1}'`

HDMI=`xrandr | grep -n "HDMI1 connected" | awk -F: '{printf "%s",$1}'`

SOUND="Internal speakers"

if [ $USB_SOUND ] && [ !$HDMI ]; then

SOUND="USB"

SOUND_PROFILE="input:analog-stereo"

pacmd "set-card-profile 1 output:analog-stereo"  >> /dev/null

fi

SOUND_PROFILE="output:analog-stereo+input:analog-stereo"

if [ $HDMI ] ; then

VIDEO_OUTPUT="HDMI"

SOUND="HDMI"

SOUND_PROFILE="output:hdmi-stereo+input:analog-stereo"

RES_LINE=$HDMI

xrandr --output HDMI1 --primary

xrandr --output LVDS1 --off

xrandr --output HDMI1 --auto

elif [ $VGA ]; then

VIDEO_OUTPUT="VGA"

RES_LINE=$VGA

xrandr --output VGA1 --primary

xrandr --output LVDS1 --off

xrandr --output VGA1 --auto

else

VIDEO_OUTPUT="LCD"

RES_LINE=$LVDS

xrandr --output LVDS1 --primary

fi

RESOLUTION=`xrandr | tail -n +$(($RES_LINE+1)) | head -n 1 | sed "s/^[ ]*//;s/[ ].*//"`

pacmd "set-card-profile 0 $SOUND_PROFILE" >> /dev/null

pgrep unity >> /dev/null && nohup unity --replace >> /dev/null &

echo "VIDEO_OUTPUT: $VIDEO_OUTPUT at $RESOLUTION.  Sound: $SOUND"