Change system settings depending on whether a monitor is plugged in

Solution 1:

At first – in order to use this method – you need to see if your graphics card sends an udev-event, when a VGA monitor is plugged in. To do this, open a terminal, type udevadm monitor --property and plug a monitor in. You should see something like this:

KERNEL[7671.540341] change   /devices/pci0000:00/0000:00:02.0/drm/card0 (drm)
ACTION=change
DEVNAME=dri/card0
DEVPATH=/devices/pci0000:00/0000:00:02.0/drm/card0
DEVTYPE=drm_minor
HOTPLUG=1
MAJOR=226
MINOR=0
SEQNUM=2296
SUBSYSTEM=drm
UDEV_LOG=3

UDEV  [7672.099723] change   /devices/pci0000:00/0000:00:02.0/drm/card0 (drm)
ACTION=change
DEVNAME=/dev/dri/card0
DEVPATH=/devices/pci0000:00/0000:00:02.0/drm/card0
DEVTYPE=drm_minor
HOTPLUG=1
MAJOR=226
MINOR=0
SEQNUM=2296
SUBSYSTEM=drm
TAGS=:udev-acl:
UDEV_LOG=3
USEC_INITIALIZED=20836591

If you don't get an event, then it is not possible to use this method.

Next, a rule for udev is created. This can be done by creating a file /etc/udev/rules.d/90-monitor-hotplug.rules. To do so, open a terminal and type gksudo gedit /etc/udev/rules.d/90-monitor-hotplug.rules. The gksudo is needed, because only root may create files in this directoy. Then paste the following line in the file and save it:

SUBSYSTEM=="drm", ACTION=="change", RUN+="/bin/su user -c '/home/user/bin/monitorhotplug.sh'"

Of course, you have to substitute the word user with your own username. This will cause udev to run the command specified behind RUN. The command executes the file /home/user/bin/monitorhotplug.sh as your user. This file has now to be created.

To to so, open a terminal and type gedit /home/user/bin/monitorhotplug.sh. Remeber to substitute user with your own username. Now paste the following script into the file and save it:

#!/bin/bash

# we need some environment variables
export DISPLAY=:0
export XAUTHORITY=~/.Xauthority
eval $(sed -n '/^#/! { s/^/export /; s/$/;/; p }' ~/.dbus/session-bus/*-${DISPLAY//:/})

# if a VGA monitor is plugged in:
if [ $(cat /sys/class/drm/card0-VGA-1/status) = "connected" ]; then
    # set my power settings
   gsettings set org.gnome.settings-daemon.plugins.power lid-close-ac-action 'nothing'
   gsettings set org.gnome.settings-daemon.plugins.power lid-close-battery-action 'nothing'
    xrandr --output VGA1 --auto
    # turn off laptop screen
    xrandr --output LVDS1 --off
# in case of anything else reset to standard options
else
   # reset my power settings
   gsettings set org.gnome.settings-daemon.plugins.power lid-close-ac-action 'suspend'
   gsettings set org.gnome.settings-daemon.plugins.power lid-close-battery-action 'suspend'
   # xrandr does the rest alone
    #xrandr --output VGA1 --off
    #xrandr --output LVDS1 --auto
fi

Now the file has to be made executable. To do so, open a terminal and type chmod +x /home/user/bin/monitorhotplug.sh.

Now it should work: 1. When you plug in a VGA monitor, your internal Laptop Screen (LVDS) will be turned off and your laptop will not suspend, if you close the lid. 2. When you plug the monitor out, or any other event with monitors occures, it will reset the power settings to suspend when the lid is closed.

There is still one little issue: When you have your monitor plugged in and close the lid, ubuntu will lock the screen, so you have to move the mouse and enter your password again.

I hope this helps somebody. Me, I am personnaly enjoying my little DIY docking station. :)