Ubuntu – Automatically change resolution when not in dock

11.04displaylaptopmonitorxorg

I have Ubuntu 11.04 (yep, I know it's old news) on my Lenovo W520. At home, I have a dock with dual monitors. I have a pretty decent setup – things work almost perfectly (hence the reason I'm reluctant to upgrade… that and I'm not 100% sold on Unity).

Anyway, the only annoyance I have is that when I'm on travel, I use the laptop screen. When I un-dock the laptop, I need to manually go into nvidia x-server settings and change the resolution from 'Auto' to 1920×1200, or it will think I have two screens, and my mouse pointer will be able to go way off the left side of the screen. This isn't a big deal, but I need to do it every time I restart the x-server (so if I reboot, or have to kill it, etc…)

What would be really nice is if there was a way for it to automatically detect whether or not there is external monitors (which it seems to do already), and switch into the mode I select, depending on which monitors are connected. Is there any way to accomplish this? I've posted my xorg.conf file for reference.

# nvidia-settings: X configuration file generated by nvidia-settings
# nvidia-settings:  version 270.29  (buildd@allspice)  Fri Feb 25 14:42:07 UTC 2011

# nvidia-xconfig: X configuration file generated by nvidia-xconfig
# nvidia-xconfig:  version 275.19  (buildmeister@swio-display-x86-rhel47-07.nvidia.com)  Tue Jul 12 18:35:38 PDT 2011
#Section "Monitor"
#    Identifier     "Monitor1"
#    VendorName     "Lenovo"
#    ModelName      "ThinkpadLCD"
#    #HorizSync       28.0 - 33.0
#    #VertRefresh     43.0 - 72.0
#    #Option         "DPMS"
#EndSection

Section "ServerLayout"
    Identifier     "Layout0"
    Screen      0  "Screen0" 0 0
    InputDevice    "Keyboard0" "CoreKeyboard"
    InputDevice    "Mouse0" "CorePointer"
    Option         "Xinerama" "0"
EndSection

Section "InputDevice"
    Identifier     "Mouse0"
    Driver         "mouse"
    Option         "Protocol" "auto"
    Option         "Device" "/dev/psaux"
    Option         "Emulate3Buttons" "no"
    Option         "ZAxisMapping" "4 5"
EndSection

Section "InputDevice"

        # generated from default
    Identifier     "Keyboard0"
    Driver         "kbd"
EndSection

Section "Monitor"
    Identifier     "Monitor0"
                   VendorName     "Unknown"
    ModelName      "DELL U2410"
    HorizSync       30.0 - 81.0
    VertRefresh     56.0 - 76.0
    Option         "DPMS"
EndSection

Section "Device"
    Identifier     "Device0"
    Driver         "nvidia"
    VendorName     "NVIDIA Corporation"
    BoardName      "Quadro 1000M"
    Option         "RegistryDwords" "EnableBrightnessControl=1"
EndSection

Section "Screen"

# Removed Option "metamodes" "DFP-0: nvidia-auto-select +0+120, DFP-6: nvidia-auto-select +1920+0"
# Removed Option "metamodes" "DFP-0: nvidia-auto-select +0+120, DFP-5: nvidia-auto-select +1920+0"
# Removed Option "metamodes" "DFP-0: nvidia-auto-select +1920+419, DFP-5: nvidia-auto-select +3840+0, DFP-6: nvidia-auto-select +0+0"
# Removed Option "metamodes" "DFP-5: nvidia-auto-select +0+0, DFP-6: 1920x1200 +1920+0"
    Identifier     "Screen0"
    Device         "Device0"
    Monitor        "Monitor0"
    DefaultDepth    24
    Option         "NoLogo" "True"
    Option         "TwinViewXineramaInfoOrder" "DFP-0"
    Option         "TwinView" "1"
    Option         "metamodes" "DFP-5: nvidia-auto-select +1920+0, DFP-6: 1920x1200 +0+0"
    SubSection     "Display"
        Depth       24
    EndSubSection
EndSection

Best Answer

Install hwinfo if you don't already have it.

Now, run $hwinfo --monitor or just $hwinfo

DO NOT connect it to the dock now. Reboot, choose your favourite resolution and run

$ sudo cp /etc/X11/xorg.conf /etc/X11/xorg.undock

DOCK the system now, and reboot. Choose your new favourite resolution and run

$ sudo cp /etc/X11/xorg.conf /etc/X11/xorg.dock

Make a script file and add it to autorun:

    # Check if system is docked or undocked and use correct xorg.conf file.
    # <lm_name> is name of laptop monitor
    DOCKED="`hwinfo --monitor | grep "<lm_name>" | wc -l`" 
    # Next line is for debugging
    # echo $DOCKED > /root/docked-state.txt 
    if [[ $DOCKED = "0" ]] 
    then 
    cp /etc/X11/xorg.dock /etc/X11/xorg.conf 
    else 
    cp /etc/X11/xorg.undock /etc/X11/xorg.conf 
    fi 
    # End of dock-undock script