"; */ ?>

Switch Between Dual/Single Monitor on (Ubuntu) Linux

xorg logo ubuntuRecently I wrote a howto on dual monitor configuration, which works great for my setup. However one thing that is not that great is switching between two modes: dual and single monitor. At work I have an external monitor that I use (which means I use two monitors – my laptop’s and external one), but whenever I am not at work I only need to use my laptop’s. Since all the xorg configuration resides in xorg.conf file, and this file is a regular static text file that is used by X (window system – gdm, kdm, etc.) when it starts, it is nontrivial to change this configuration while running X without some X tools. Unfortunately, Ubuntu is not that fancy (yet) and does not provide these tools by default, so here is a way to do it (sort of) manually.

What we can do is to create two xorg.conf files – "xorg.conf.single" and "xorg.conf.dual". In "xorg.conf.single" just comment out the following line from ServerLayout section:

# /etc/X11/xorg.conf (xorg X Window System server configuration file)
#
.....
Section "ServerLayout"
        Identifier      "Default Layout"
        Screen          0 "0 Screen"
        #Screen         1 "1 Screen" Above "0 Screen"   <-- comment out this line
        Option          "Xinerama" "on"
        Option          "Clone" "off"
.....
EndSection

Here is the listing of "xorg" files that I have:

user@host:/etc/X11$ ll xorg.conf*
-rw-r--r-- 1 root root 4457 2007-06-02 15:05 xorg.conf
-rw-r--r-- 1 root root 4456 2007-05-22 22:03 xorg.conf.dual
-rw-r--r-- 1 root root 4457 2007-05-22 22:04 xorg.conf.single

Now the idea is simple - before X (I use Gnome Desktop, but it can be any desktop environment) starts, we need to copy xorg file that we need (dual or single) to "xorg.conf", which will be picked and loaded by X.

In /home/user/ directory we have a .bashrc file that is loaded whenever the user logs in (if we use bash shell, which is a most popular shell anyway). Therefore we can leverage this file to define aliases that we would like to use once we login. Since alias can be anything we'd like, why not make a dual/single commands as aliases? Here is an example:

user@host:/etc/X11$ tail -5 /home/user/.bashrc
# restart gdm with dual monitor support
alias xdual='sudo cp /etc/X11/xorg.conf.dual /etc/X11/xorg.conf; sudo /etc/init.d/gdm restart'

# restart gdm with single monitor support
alias xsingle='sudo cp /etc/X11/xorg.conf.single /etc/X11/xorg.conf; sudo /etc/init.d/gdm restart'

Now when we need to switch between dual/single monitor, we can fire up shell (by pressing Ctrl+Alt+1, or gnome-terminal, or kterm, etc.) and type xsingle or xdual whichever we need.