Hotplug an External Screen to your Laptop on Linux

With udev you can trigger actions when an external screen/monitor is connected. This is, e.g., useful when you want to automatically perform some actions when an external Screen is connected to a laptop. Such actions could be to automatically enable the additional screen and set its orientation using xrandr or the like.

The following udev rule (e.g., in a file such as /etc/udev/rules.d/95-monitor-hotplug.rules) executes a script when the external VGA output is connected (Thanks to Andy for the hint in his post.):

#Rule for executing commands when an external screen is plugged in.
#Credits go to: http://unix.stackexchange.com/questions/4489/a-tool-for-automatically-applying-randr-configuration-when-external-display-is-p

ACTION=="change", SUBSYSTEM=="drm", HOTPLUG=="1", RUN+="/usr/local/bin/hotplug_monitor.sh"

Note: you can reload the udev rules, e.g., with “udevadm trigger”. The following script (/usr/local/bin/hotplug_monitor.sh) takes care of executing the appropriate scripts etc. when the external screen is (un-)plugged:

export DISPLAY=:0.0

function connect(){
xrandr --output VGA1 --auto --left-of LVDS1
}

function disconnect(){
xrandr --output VGA1 --off
}

xrandr | grep "VGA1 connected" &> /dev/null && connect || disconnect

This script simply activates or deactivates the external output but different other things could be done as well.

About these ads
This entry was posted in Snippets and tagged , . Bookmark the permalink.

One Response to Hotplug an External Screen to your Laptop on Linux

  1. Thank you for this! Seems to work well for me. Running Debian Squeeze on a little eeepc netbook.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s