Update: I'm told that the latest betas of the proprietary nvidia drivers support hot plugging monitors, so it's possible that this will be unnecessary in the future.
I just set this up on my own Fedora 17 machine, and I figured I'd pass the information along so that other technically inclined people can get semi-decent monitor switching working on their laptops under Linux. Before I start, I would like to point out something very important: It's 20f***ing12, not 1995. This should not be necessary, and I'm annoyed that I had to do it. I'm doubly annoyed that it appears that there's absolutely no system event that fires off when a monitor is plugged in or unplugged.
Anyway, after mucking around over many days, I arrived at a halfway decent solution that allows my laptop, if not to automatically detect and react when a monitor is plugged in, to at least detect the display configuration with a single keypress.
Before we get started, you'll need:
- A basic knowledge of BASH scripting
- An nVidia card with the proprietary drivers (YMMV with other setups; sorry, I can't help you there)
- Disper -- Download, compile, and install it yourself.
Starting out:
First, plug in all your monitors and run this command:
You should see something like this:
Code:
display DFP-0: Chi Mei Optoelectronics corp.
resolutions: 320x175, 320x200, 360x200, 320x240, [...]
display CRT-0: Acer H233H
resolutions: 320x240, 400x300, 512x384, 640x480, [...]
display DFP-1: Acer S232HL
resolutions: 320x175, 320x200, 360x200, 320x240, [...]
(The [...]s are mine, just removed a bunch of extra resolutions that aren't relevant).
Note all the displays that are connected to your computer. If your laptop is like mine, you might have a DFP-0, DFP-1, and CRT-0. My laptop has a DisplayPort and a VGA port. CRT-0 is the VGA port, and DFP-0 and DFP-1 are the laptop monitor and the DisplayPort, respectively. You can verify which display is which by unplugging them and running 'disper -l' again.
Once you've done this, run:
You do not need to run this as root. For each typical monitor configuration that you want to handle:
- Go to 'X Server Display Configuration' inside nvidia-settings and set up the desired configuration, then click on 'Apply'
- From your command line, type 'disper -p > $HOME/disper.WHATEVER' (where WHATEVER is some short description of the display -- I use disper.laptop, disper.dual, and disper.single)
When you're done, you can test each of these sets of display settings from the command line by typing:
Code:
disper -i < disper.WHATEVER
Now it's time to write a BASH script to detect what monitors are plugged in and choose the configuration automatically based on that. Here's mine:
Code:
#!/bin/bash
if disper -l | grep -q 'CRT-0' ; then
# If the VGA port is connected, I'm assuming that the DisplayPort
# is connected to, and going with my dual monitor setup.
echo "Configuring dual external displays"
disper -i < /home/bart/disper.dual
elif disper -l | grep -q 'DFP-1' ; then
# If the DisplayPort is connected (but the VGA port is not), then I'm
# going with the single DisplayPort setup.
echo "Configuring single external display"
disper -i < /home/bart/disper.single
else
# If neither monitors are connected, go with the laptop-only setup.
echo "Configuring laptop display"
disper -i < /home/bart/disper.laptop
fi
One thing that my script is missing is a VGA-only setup, but that's reasonably simple to add. Put this script somewhere that you can easily run it from the command line (/usr/local/bin, or $HOME/bin, if that's in your $PATH). Now, when you change monitors, you can run the script from the command line and not have to restart X.
We're almost done. Now we want to auto-detect your monitor setup on boot. At the end of /etc/gdm/Init/Default (before the exit 0), add the full path to your script (and remember that if you put it in $HOME bin, to type out your home directory here, since it's not going to know what your $HOME is before you've logged in).
Finally, set up keyboard shortcuts in your desktop environment that run the script. I'm using KDE, so I configured mine in System Settings -> Shortcuts and Gestures and added preset actions. You may be running GNOME, and I assume there's a way to set up global hotkeys there too, although I don't know it offhand.
I apologize if this is convoluted. I'm providing this for two reasons: One, so technically inclined people can make it work, and two, to point out how incredibly stupid it is that I have to do this at all.