Every morning when I would come to the office, I would place my laptop on the desk and connect the power, network cable and the external display. And every time I'd have to go through the same external display configuration dialog. And that's just annoying. So I decided to automate. Isn't that a thrilling word? Automate.
A friend helped me write a shell script to enable or disable a twin display configuration using xrandr, and I set it up to be triggered by UDEV when an external display is hotplugged on the VGA port.
This is the script:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
# External display automatic switching | |
# | |
# Enables or disables the external display port, when a display is connected or disconnected. | |
# Run this script manually (via hotkey) or trigger it from UDEV. | |
# Desired position of the external display (uncomment what you need) | |
EXTERNAL_POSITION="above" | |
# EXTERNAL_POSITION="below" | |
# EXTERNAL_POSITION="left-of" | |
# EXTERNAL_POSITION="right-of" | |
# EXTERNAL_POSITION="same-as" | |
# Base name of the built-in display (find it out by running 'xrandr' in shell) | |
BUILTIN_BASE="LVDS" | |
# get all outputs | |
get_outputs() { | |
xrandr | awk '/connected/ {print $1}' | |
} | |
OUTPUT_BUILTIN="$(get_outputs | grep "$BUILTIN_BASE")" | |
OUTPUT_EXTERNAL="$(get_outputs | grep -v "$BUILTIN_BASE")" | |
# echo "Found outputs: $OUTPUT_BUILTIN (built-in), $OUTPUT_EXTERNAL (external)" | |
STATUS_DISPLAYPORT=$(xrandr -q | grep "^$OUTPUT_EXTERNAL" | cut -d" " -f2) | |
if [ "$STATUS_DISPLAYPORT" = "connected" ]; then | |
echo "Enabling external display connected to $OUTPUT_EXTERNAL..." | |
xrandr --output $OUTPUT_EXTERNAL --${EXTERNAL_POSITION} $OUTPUT_BUILTIN --auto | |
xrandr --output $OUTPUT_BUILTIN --primary | |
wait | |
echo "Done." | |
else | |
echo "External display disconnected. Switching to LCD only..." | |
xrandr --output $OUTPUT_EXTERNAL --off | |
wait | |
echo "Done." | |
fi | |
exit 0 |
The two things you will probably want to change are the constants
EXTERNAL_POSITION
and BUILTIN_BASE
. If you want the external display to show a mirror of your primary display, use setting EXTERNAL_POSITION="same-as"
.
I placed the script in
/usr/local/lib/
and set up an UDEV rule that triggers it when an external display is connected.This is my UDEV rule, in
/etc/udev/rules.d/
:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ACTION=="change", SUBSYSTEM=="drm", RUN+="/usr/local/lib/external-display-hotplug.sh" |
When you add the UDEV rule, you must reload rules for the changes to take effect. You do it by saying
$ sudo udevadm control --reload-rules
And that's it. Enjoy!
No comments:
Post a Comment