Connect Multiple Displays




Connect Multiple Displays

You plug an external monitor into your laptop or a second monitor into your desktop computer. At best, nothing happens. At worst, something horrible happens. Find out how to get it right, whether you want to clone your display or spread it across multiple monitors.

Laptops give us the ability to go mobile but usually with a number of compromises compared to a more full-featured desktop machine. The most obvious compromise is screen size. The typical laptop screen is tiny compared to what you probably have sitting on your desk, but setting up your laptop to use an external monitor can give you the best of both worlds.

But multiple screens aren't limited to laptops. If you spend hours in front of a desktop computer every day, a second or even third monitor can help you work more efficiently.

All it takes to add a second monitor is an extra video card, or a video card with multiple outputs and a bit of configuration of Xorg.

First, you need to determine your video chipset, which in turn will determine which drivers you can use. If you don't know the chipset, you can open SystemDevice Manager, and then scroll down the devices on the left until you find a device labeled "AGP" or "video." Alternatively, you can open a terminal and type lspci to see a list of all PCI devices in your system: look for a line that says "VGA compatible controller" or something similar. If your laptop has an nVidia or Radeon (ATI) video chipset, you have the option of using proprietary binary drivers made available by the manufacturer. These proprietary drivers offer more features and generally better performance than the currently available open source drivers, but many people specifically avoid them on idealistic grounds. Hopefully, we'll eventually see these proprietary drivers released under an open source licence, but in the meantime they do still offer some advantages if you don't mind using proprietary software.

Grok the Xorg Conf File

No matter which technique you use to configure your displays, you will need to make changes to the Xorg configuration file located at /etc/X11/xorg.conf, so it helps to be familiar with the way it's structured and the terms that are used.

xorg.conf contains a series of stanzas, each of which defines a specific part of the X configuration. Various elements such as video cards, screens, and input devices are defined separately and then progressively bound together toward the end of the file. There are a multitude of additional stanzas in the Xorg configuration file, but the following ones are the important sections when running multiple displays (or heads).

Section "Device"

A device is a video card or video adaptor, so you will need one Device section for each card in your system. A video card with multiple outputs is considered a single device, but requires two Device sections to distinguish the outputs. Each Device section includes, at a minimum, entries for Identifier and Driver, but if you have more than one video card installed, you will also need to enter a BusID value so Xorg knows which section is associated with each card; if you have a card with multiple outputs, enter a Screen value so Xorg knows which section is associated with which output. Use lspci -X to determine the BusID for each card in a format suitable for entry into xorg.conf:

Section "Device" 
  Identifier "NVIDIA Corporation NV34M [GeForce FX Go5200]" 
  Driver     "nvidia" 
  BusID      "PCI:1:0:0" 
EndSection

If you have a single device with multiple outputs, just number the Screen entries starting from 0. You should also give each Identifier a unique name (in this example, we've simply appended 0 or 1 to the end):

Section "Device" 
        Identifier      "NVIDIA Corporation NV40 [GeForce 6800] 0" 
        Driver          "nvidia" 
        BusID           "PCI:3:0:0" 
        Option          "RenderAccel"           "true" 
        Option          "AllowGLXWithComposite" "true" 
        Screen          0
EndSection

Section "Device" 
        Identifier      "NVIDIA Corporation NV40 [GeForce 6800] 1" 
        Driver          "nvidia" 
        BusID           "PCI:3:0:0" 
        Option          "RenderAccel"           "true" 
        Option          "AllowGLXWithComposite" "true" 
        Screen          1
EndSection

Section "Monitor"

You need one Monitor section per monitor you have attached to your computer. Give each one a sensible and unique Identifier value:

Section "Monitor" 
        Identifier      "Main Monitor" 
        Option          "DPMS" 
        HorizSync       28-80
        VertRefresh     43-60
EndSection

Section "Monitor" 
        Identifier      "Second Monitor" 
        Option          "DPMS" 
        HorizSync       28-80
        VertRefresh     43-60
EndSection

Section "Screen"

In X terminology, a screen is a combination of a device and a monitor. Screen sections have Device and Monitor values that refer to the appropriate sections by name:

Section "Screen" 
        Identifier      "Main Screen" 
        Device          "NVIDIA Corporation NV40 [GeForce 6800] 0" 
        Monitor         "Main Monitor" 
        DefaultDepth    24

        # Subsections for other display depths not shown...
        SubSection "Display" 
                Depth           24
                Modes           "1600x1200" 
        EndSubSection
EndSection

Section "Screen" 
        Identifier      "Second Screen" 
        Device          "NVIDIA Corporation NV40 [GeForce 6800] 1" 
        Monitor         "Second Monitor" 
        DefaultDepth    24
        SubSection "Display" 
                Depth           24
                Modes           "1280x1024" 
        EndSubSection
EndSection

Section "ServerLayout"

The ServerLayout section is in turn a combination of one or more Screen sections, along with InputDevice sections that define keyboards, mice, and trackpads. You'll see some examples of a ServerLayout section shortly.

Tracking Down X Errors

Once you start modifying the Xorg configuration file, there is a very high likelihood that you will end up with your computer in a state where X can't start at all. The first place to go looking for debugging information is the Xorg logfile, which is regenerated each time X starts and is stored in /var/log/Xorg.<0>.log or similar. The <0> is the identifier that Xorg uses to identify the X session; if you run multiple X sessions, you may also have an Xorg.1.log.

The Xorg log prepends a severity key to the start of each line, making it very easy to see exactly what caused the problem. (II) lines are informational only, (WW) lines are nonfatal warnings, and (EE) lines are fatal errors. Problems often cascade down through the logfile as one failure causes another, such as a failure to initialize a video card leading to a failure to initialize a screen.

Configure Xinerama

X can natively support multiple heads, but by default, each one is considered to be totally independent of the others. With independent heads, it's not possible to have windows span multiple screens, drag windows between screens, or move the mouse cursor from one to the other by dragging it off the edge of one screen and onto the other. Each head is effectively a totally separate environment.

Xinerama is a feature of X that overcomes this limitation by allowing multiple screens to be associated with each other and bound together into a single, unified "virtual screen." You can define the relationship of the screens to each other so your computer knows where each screen is physically located relative to the others, allowing you to drag windows around and have them overlap the screens in a way that makes sense given the position of your monitors on your desk.

First, make sure Xorg has Xinerama support activated by adding an appropriate ServerFlags section if one doesn't exist already:

Section "ServerFlags" 
   Option "Xinerama" "true" 
EndSection

Then add sections for your second device, monitor, and screen, as described earlier, giving them descriptive names. Finally, add your second screen into the ServerLayout stanza, describing its relationship to the primary screen. Here's an example with two screens:

Section "ServerLayout" 
        Identifier      "Twin Head" 
        Screen          "Main Screen" 
        Screen          "Second Screen" RightOf "Main Screen" 
        InputDevice     "Generic Keyboard" 
        InputDevice     "Configured Mouse" 
EndSection

Exit GNOME, press Ctrl-Alt-Backspace to kill off Xorg and force it to respawn, and watch as both monitors flash to life.

Configure TwinView on nVidia

nVidia cards with multiple outputs support a special video mode called TwinView using the proprietary (binary) drivers. TwinView is a bit like Xinerama because it binds multiple displays together into a single virtual display, but the difference is that it's done at a very low level within the video driver and the hardware itself. Both heads share a single framebuffer, which basically tricks Xorg into thinking you only have a single monitor, giving it much better performance than Xinerama, as well as allowing OpenGL to operate across both heads at full speed. As far as Xorg is concerned, it's only driving one screen: it just happens to be a screen with very odd dimensions.

By default, Ubuntu installs the open source nv driver, but to use TwinView, you will need to install the proprietary nvidia driver instead. The nvidia driver is part of the linux-restricted-modules-[arch] package, which comes in several different versions depending on what kernel you are running. Full instructions for installing this binary driver are in "Enable 3-D Video Acceleration" [Hack #52].

Once you have Xorg working successfully with a single monitor and the binary drivers, run the command sudo nvidia-xconfig -twinview to modify your xorg.conf to support TwinView. If you want to tweak anything (or see what was done), open /etc/X11/xorg.conf and go to the Device section for your nVidia card. Rather than defining the second monitor separately, the nVidia driver accepts a number of additional options to enable TwinView mode and configure the monitor directly:

Section "Device" 
    Identifier     "NVIDIA Corporation NV40 [GeForce 6800]" 
    Driver         "nvidia" 
EndSection

Section "Screen" 
    Identifier     "Default Screen" 
    Device         "NVIDIA Corporation NV40 [GeForce 6800]" 
    Monitor        "Generic Monitor" 
    DefaultDepth    24
    Option         "RenderAccel" "true" 
    Option         "AllowGLXWithComposite" "true" 
    Option         "TwinView" "True" 
    Option         "TwinViewOrientation" "RightOf" 
    Option         "UseEdidFreqs" "True" 
    Option         "MetaModes" "1280x1024,1280x1024; 1024x768,1024x768" 

    # Subsections for other display depths not shown...
   SubSection     "Display" 
        Depth     24
        Modes     "1600x1200" 
    EndSubSection
EndSection

The MetaModes option tells the driver what resolutions to use for the two monitors, with the resolution reported back to Xorg being the sum of the dimensions. Multiple resolution pairs can be specified; in this case, the monitor orientation is side-by-side (RightOf), so the effective resolution is either 2560x1024 or 2048x768.

You can also explicitly set offsets to accurately control the logical position of the screens:

Option "MetaModes" "1600x1200 +0+0, 1024x768 +1600+0;"

Other possible values for TwinViewOrientation are LeftOf, Above, Below, and Clone. Clone is a special mode that displays the same image on both monitors, ideal for running presentations on a laptop connected to an external projector.

Configuring MergedFB on ATI/Radeon

Recent ATI video cards support an Xorg mode called MergedFB, which is quite similar to nVidia's TwinView in that it manages connections to the second monitor directly within the driver and pretends to be a single large virtual monitor:

Section "Device" 
   Identifier "Radeon7000" 
   Driver     "radeon" 
   BusID      "PCI:0:9:0" 
   Option     "MergedFB" "true" 
   Option     "MonitorLayout" "AUTO, AUTO" 
   Option     "CRT2Hsync" "30-65" 
   Option     "CRT2VRefresh" "50-75" 
   Option     "CRT2Position" "LeftOf" 
   Option     "MetaModes" "1280x1024-1280x1024" 
EndSection

Like TwinView, there are options to configure the frequency, position, and resolution of the second monitor.

Combining Methods

Sometimes even two monitors aren't enough, but unfortunately video cards with more than two outputs are quite rare, so you may need to combine techniques if you want to run three or more heads. For example, you may have a twin-head nVidia card running two heads using TwinView, plus a third head connected to an older PCI video card. Because TwinView presents itself to X as being a single virtual screen, you can then combine it with the third head using Xinerama to achieve a triple-head system with relatively little effort.

With a bit of creativity, it's even possible to combine TwinView, MergedFB, and Xinerama all on the same machine (see Figure).

Quintuple-head desktop