2015-10-06

Hotplug monitor setup using udev and xrandr

A small post about my configuration to configure my screens on monitor plug event

The process is quite simple :

  1. Add a udev rule to call a script when a monitor is plugged in.
  2. Design an appropriate script that setup connected monitor using xrandr.

Here is the udev rule, to be put in a file named (for example)
"95-monitor-hotplug.rules" and placed in the /etc/udev/rultes.d/ directory
 
KERNEL=="card0", SUBSYSTEM=="drm", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/home/\/.Xauthority", RUN+="/home/\/install/bin/updateMonitors.zsh"
(This should be on one line in the file)


The zsh script to configure monitors :


#!/bin/zsh
monitors=("DP2" "DP1-1" "DP1-2" "DP1-3" "HDMI1" "HDMI2" "VIRTUAL1")
# Associate various ports with their desired positions
typeset -A positions
# DP1-? are dvi display ports
positions[DP1-1]=--left-of
positions[DP1-2]=--left-of
positions[DP1-3]=--left-of

# DP2 is the VGA port
positions[DP2]=--right-of

positions[HDMI1]=--right-of
positions[HDMI2]=--right-of
positions[VIRTUAL1]=--right-of
integrated=eDP1
# Get monitors informations from xrandr output
xrandrOutput=`xrandr -q`

for m in $monitors
do
   # echo "Considering monitor : [$m]"
   # echo "associated position : [$positions[$m]]"

   # Manage deconnexions
   if [[ "$xrandrOutput" =~ "$m disconnected" ]]; then
      xrandr --output $m --off 
      # echo "xrandr --output $m --off"
   fi

   # Manage connexions
   if [[ "$xrandrOutput" =~ "$m connected" ]]; then
      xrandr --output $m --auto --noprimary $positions[$m] $integrated
      # echo "xrandr --output $m --auto --noprimary $positions[$m] $integrated"
   fi
done

No comments: