Autodetecting and configuring multiple monitors in Ubuntu


A common problem for laptop users is that they use various display configurations. When traveling they use only the internal LCD panel, but in the office they use internal LCD panel + external display. It is pain to configure external display manually each time you plug it in.

This how to will give you instructions to create a shell script which will configure displays and Gnome panels according to the plugged in displays.

Install magnificent disper tool

Edit your sources.list file

sudo gedit /etc/apt/sources.list

add the following lines

deb http://ppa.launchpad.net/wvengen/ppa/ubuntu intrepid main
deb-src http://ppa.launchpad.net/wvengen/ppa/ubuntu intrepid main

Save and exit the file

Update the source list using the following command

sudo apt-get update

Install disper tool using the following command

sudo apt-get install disper

Note:- Currently disper supports nVidia only, but the author claims ATI support is possible to add by contributions.

Run the following command to autodetect displays and set the extended desktop

disper --displays=auto -e

Dispering displays is not enough. You probably want to move Gnome panels to your primary (external) display when it is plugged in.

Gnome architects have been clever. Gnome panel stores its settings in gconf registry. gconf registry is not just dummy storage backend; changing these values immediately reflect changes in applications using the registry. One of these applications is gnome-panel. This means that we can move the panels by editing its registry setting related to the monitor configuration.

Using gconf-editor command the critical settings can be found under:

/apps/panel/toplevels/bottom_panel_screen0/monitor
/apps/panel/toplevels/top_panel_screen0/monitor

Value = 0 -> panels on internal LCD
Value = 1 -> panels on External display

Let's make a little command line script which will

a) Detect and configure monitors

b) Move gnome-panels according to the connected display count

Open monitor.sh file

sudo gedit monitor.sh

add the following lines save and exit


#!/bin/sh
#
# Detect displays and move panels to the primary display
#

# disper command will detect and configure monitors
disper --displays=auto -e

# parse output from disper tool how many displays we have attached
# disper prints 2 lines per displer
lines=`disper -l|wc -l`

display_count=$((lines / 2))

echo $display_count

echo "Detected display count:" $display_count

# Make sure that we move panels to the correct display based
# on the display count
if [ $display_count = 1 ] ; then
echo "Moving panels to the internal LCD display"
gconftool-2 \
--set "/apps/panel/toplevels/bottom_panel_screen0/monitor" \
--type integer "0"
gconftool-2 \
--set "/apps/panel/toplevels/top_panel_screen0/monitor" \
--type integer "0"
else
echo "Moving panels to the external display"
gconftool-2 \
--set "/apps/panel/toplevels/bottom_panel_screen0/monitor" \
--type integer "1"
gconftool-2 \
--set "/apps/panel/toplevels/top_panel_screen0/monitor" \
--type integer "1"
fi


Add monitor.sh to your Startup Programs in System > Services menu, so it will be run each time you login to Gnome.


Leave a Reply