Easy Display Layout Switching in Ubuntu

I use Ubuntu on my laptop and am generally very happy with it. The only limitation is that the X server does not expand the desktop to new monitors and projectors as they are plugged in. However, I've been able to configure GDM to provide a simple workaround by adding options to the menu on the GDM login screen to switch between display layouts before logging in. I've not found the approach I've used documented anywhere on the web, so here's how I did it.

First I defined server layouts in the X server configuration file (/etc/X11/xorg.conf) for each display configuration I need: the built-in laptop screen by default, the laptop display and a 16:9 display for when I'm at home, the laptop display and a 4:3 display for when I'm at work, and so on. Here are the server layout sections from my X configuration:

...

Section "ServerLayout"
        Identifier      "Default"
        Screen          0 "Default Screen"
        InputDevice     "Generic Keyboard"
        InputDevice     "Configured Mouse"
        InputDevice     "stylus" "SendCoreEvents"
        InputDevice     "Synaptics Touchpad"
EndSection

Section "ServerLayout"
        Identifier      "Home"
        Screen          0 "Default Screen"
        Screen          1 "External 16:9 Screen" Above "Default Screen"
        InputDevice     "Generic Keyboard"
        InputDevice     "Configured Mouse"
        InputDevice     "stylus" "SendCoreEvents"
        InputDevice     "Synaptics Touchpad"
EndSection

Section "ServerLayout"
        Identifier      "Work"
        Screen          0 "Default Screen"
        Screen          1 "External 4:3 Screen" RightOf "Default Screen"
        InputDevice     "Generic Keyboard"
        InputDevice     "Configured Mouse"
        InputDevice     "stylus" "SendCoreEvents"
        InputDevice     "Synaptics Touchpad"
EndSection

...

Then I configured gdm to launch the X server with the layout read from a configuration file named xserver-layout stored in the /etc/gdm directory by adding the following section to the /etc/gdm/gdm.conf-custom config file:

[server-Standard]
name=Standard server
command=/etc/gdm/xserver
flexible=true

The command /etc/gdm/xserver is the following script. It needs to be executable by root.

#!/bin/sh

LAYOUT=`cat /etc/gdm/xserver-layout`
if [ -z "$LAYOUT" ]; then
        LAYOUT=Default
fi

exec /usr/X11R6/bin/X -br -audit 0 -layout "$LAYOUT" $*

I then wrote another script, /etc/gdm/set-xserver-layout, to change the contents of the /etc/gdm/xserver-layout file and restart GDM. Again, this has to be executable by root.

#!/bin/sh

echo -n "${1:?no layout name given}" > /etc/gdm/xserver-layout
/etc/init.d/gdm restart

I then added menu items to the GDM menu to run the xserver-layout command with different parameters to switch GDM between different display layouts. You can do this from the System > Administration > Login Window control panel or add a section like the following to the /etc/gdm.conf-custom file:

[customcommand]
CustomCommandLabel0=Mobile Display Configuration
CustomCommandLRLabel0=Switch to Mobile Display Configuration
CustomCommandIsPersistent0=true
CustomCommandTooltip0=Switch to Mobile Display Configuration
CustomCommand0=/etc/gdm/set-xserver-layout Default

CustomCommandLabel1=Work Display Configuration
CustomCommandLRLabel1=Switch to Work Display Configuration
CustomCommandIsPersistent1=true
CustomCommandTooltip1=Switch to Work Display Configuration
CustomCommand1=/etc/gdm/set-xserver-layout Work

I can now easily select the display configuration I need before I first log in. Having also configured the Network Manager to not ask for my keychain password every time it wants to log into a WiFi network, being mobile with Ubuntu is a much smoother experience.

If you know of a better way, ideally a tool that lets me configure the X server on the fly, please drop me a line in the comments.

Update #1: thanks to Brendan, I've updated the xserver script above to pass the arguments from gdm through to the X server process.

Update #2: fixed a typo in the xserver script.

Copyright © 2007 Nat Pryce. Posted 2007-07-27. Share it.

Comments powered by Disqus