Ubuntu – How to automatically switch monitors with the laptop dock

11.04dockmultiple-monitorsnvidia

I have a Dell Precision M4300 with a Dell Dock PRO1X running Ubuntu Natty.

When I have the laptop docked the lcd display is off and I use two external monitors however when I undock the laptop it does not switch to the laptop screen. The same happens when I dock the laptop, it does not switch to the external monitors.

If I reboot the laptop it will choose the correct setup but it will not automatically change monitors with Ubuntu running.

My Xorg.conf

Best Answer

Dock/UnDock Scripts

I have finally put together a complete script setup that will detect my dock status and switch the monitors automatically.

The first thing I did was to get rid of my Xorg.conf as it caused more issues that it solved. Since I am using an nvdia card I eventually found that using disper was the best way to go. I had discounted it before as its standard commands could not achieve my dualview setup but then I found I could create a custom metamode to use with disper.

To create the custom metamode I used the values stored in Xorg.conf along with previous testing with nv-control-dpy and xrandr as a guide. This metamode and other twinview values were added to a file that can be passed to disper through stdin.

Due to having different sized monitors with Twinview I need to use XCreateMouseVoid, as described in this question.

Below is a complete breakdown of the scripts and files with their contents.


Scripts

  1. Create XCreateMouseVoid script:

    Script Name: ~/.mousevoid

    #!/bin/bash
    echo "Mouse Void"
    /opt/XCreateMouseVoid/XCreateMouseVoid 0 1112 1280 88 &
    /opt/XCreateMouseVoid/XCreateMouseVoid 0 0 1280 88 &
    

    chmod +x ~/.mousevoid

  2. Create Disper settings file:

    File Name: ~/.disperDocked

    backend: nvidia
    associated displays: CRT-0, DFP-0, DFP-1
    metamode: CRT-0: 1280x1024_60 @1280x1024 +0+88, DFP-1: 1920x1200 @1920x1200 +1280+0
    scaling: default, default, stretched
    xinerama info order: DFP-1, CRT-0, DFP-0
    
  3. Create the export dbus session script:

    Script Name: ~/.export_x_info

    #!/bin/bash
    # Export the dbus session address on startup so it can be used by any other environment
    sleep 5
    touch $HOME/.Xdbus
    chmod 600 $HOME/.Xdbus
    env | grep DBUS_SESSION_BUS_ADDRESS > $HOME/.Xdbus
    echo 'export DBUS_SESSION_BUS_ADDRESS' >> $HOME/.Xdbus
    # Export XAUTHORITY value on startup so it can be used by cron
    env | grep XAUTHORITY >> $HOME/.Xdbus
    echo 'export XAUTHORITY' >> $HOME/.Xdbus
    

    Set to user executable only: chmod 700 ~/.export_x_info

    Then put it in Startup Applications:

    Name: X Environment Variables
    Command: /home/your_user/.export_x_info
    

    Unless you restart you need to run ~/.export_x_info to create the .Xdbus file

  4. Create the udev rule that will run with upon a dock event:

    Script Name: /etc/udev/rules.d/81-dell-dock.rules

    KERNEL=="dock.0", ACTION=="change", RUN+="/usr/local/sbin/dell-dock"

    chmod +x /etc/udev/rules.d/81-dell-dock.rules

  5. Create the script that is associated with the udev rule:

    Script Name: /usr/local/sbin/dell-dock

    #!/bin/sh
    # wait for the dock state to change
    sleep 0.5
    DOCKED=$(cat /sys/devices/platform/dock.0/docked)
    case "$DOCKED" in
       "0")
           echo "Run UnDocking Script..."
            /usr/local/sbin/undock
            ;;
        "1") 
            echo "Run Docking Script..."
            /usr/local/sbin/dock
            ;;
    esac
    exit 0
    

    chmod +x /usr/local/sbin/dell-dock

  6. Create the undock script (change your_user to match your user name):

    Script Name: /usr/local/sbin/undock

    #!/bin/bash
    USER=your_user
    export HOME=/home/$USER
    source $HOME/.Xdbus
    export DISPLAY=:0
    echo "UnDocking Script"
    echo "Switch primary monitor"
    sudo -u $USER disper -s
    echo "Killall XCreateMouseVoid"
    pkill XCreate
    echo "Restart conky"
    pkill conky
    sudo -u $USER conky &
    

    chmod +x /usr/local/sbin/undock

  7. Create the dock script (change your_user to match your user name):

    Script Name: /usr/local/sbin/dock

    #!/bin/bash
    USER=your_user
    export HOME=/home/$USER
    source $HOME/.Xdbus
    export DISPLAY=:0
    echo "Docking Script"
    echo "Switch to Dualview"
    sudo -u $USER disper -i < $HOME/.disperDocked
    echo "Start XCreateMouseVoid"
    pkill XCreate
    sudo -u $USER $HOME/.mousevoid
    echo "Restart conky"
    pkill conky
    sudo -u $USER conky &
    

    chmod +x /usr/local/sbin/dock


Debug

To debug the dock scripts you have to enable udev debug logging:

udevadm control --log-priority=debug

Then open syslog in Log File Viewer


Resources

nVidia Readme: Config Twinview, X Config Options
Autorandr
Disper PPA
nv-control-dpy
Ubuntu Forum - Un/Dock Scripts
Thinkwiki
udevadm