Automatic Display Switching on Resume

From ThinkWiki
Revision as of 18:25, 31 May 2010 by Gettalong15 (Talk | contribs) (Automatic Display Switching on Resume)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Automatic Display Switching on Resume

I have a T410s and a Mini Dock 3 Plus and wanted Ubuntu 10.04 to automatically switch to the external monitor on resume if it is connected. However, gnome-power-manager and gnome-display-manager can't do this correctly currently and therefore I have written a script to automate this.

The following script should be saved in the file /etc/pm/sleep.d/10_activate_monitor and does the following on resume:

  • If any external monitor is attached, activate it (the first monitor from the list EXTERNALS), and disable the internal monitor if the lid is closed.
  • If no external monitor is attached, activate the internal monitor.
#!/bin/bash

. /usr/share/acpi-support/power-funcs

INTERNAL="LVDS1"
EXTERNALS="VGA1 HDMI1 DP1 HDMI2 HDMI3 DP2 DP3"


case "$1" in
        thaw|resume)
		for x in /tmp/.X11-unix/*; do
		    displaynum=`echo $x | sed s#/tmp/.X11-unix/X##`
		    getXuser;

		    grep -q closed /proc/acpi/button/lid/*/state
		    LID_CLOSED=$?

		    if [ x"$XAUTHORITY" != x"" ]; then
		        export DISPLAY=":$displaynum"

		    	INTERNAL_STATE=$(xrandr | grep ^$INTERNAL | grep " con" | sed "s/.*connected//" | sed "s/ //" | sed "s/ .*//g")
		    	for I in $EXTERNALS; do
		    		EXTERNAL=$I
		    		EXTERNAL_STATE=$(xrandr | grep ^$EXTERNAL | grep " con" | sed "s/.*connected//" | sed "s/ //" | sed "s/ .*//g")
		    		if [ ! \( -z "$EXTERNAL_STATE" \) ]; then break; fi
		    	done

		    	logger -t "pm-utils-custom-display" "$INTERAL -- $INTERNAL_STATE :: $EXTERNAL -- $EXTERNAL_STATE"
			if [ ! \( -z "$EXTERNAL_STATE" \) ]; then
				logger -t "pm-utils-custom-display" "Activating external display"
				if [ $LID_CLOSED = 0 ]; then xrandr --output $INTERNAL --off; fi
				xrandr --output $EXTERNAL --auto
			else
				logger -t "pm-utils-custom-display" "Activating internal display"
				xrandr --output $INTERNAL --auto
				xrandr --output $EXTERNAL --off
			fi
		    fi
		done
                ;;
        *) exit $NA
                ;;
esac

exit 0