Skip to content

Instantly share code, notes, and snippets.

@jamesmcdonald
Last active March 26, 2019 07:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesmcdonald/66e03e6ea2d50b51c300def03a382c54 to your computer and use it in GitHub Desktop.
Save jamesmcdonald/66e03e6ea2d50b51c300def03a382c54 to your computer and use it in GitHub Desktop.
Monitor plug autodetection for i3
# This should go in /etc/udev/rules.d
KERNEL=="card0", SUBSYSTEM=="drm", ACTION=="change", RUN+="/usr/local/bin/monitorswitch.sh"
#!/bin/bash
# You will have to customise the device names and layout to whatever xrandr
# setup is appropriate for your machine. udev will run this script as root,
# which works fine for calling xrandr.
# /etc/udev/rules.d/98-monitorswitch.rules:
# KERNEL=="card0", SUBSYSTEM=="drm", ACTION=="change", RUN+="/usr/local/bin/monitorswitch.sh"
#exec >>/tmp/monitorswitch.log 2>&1
read user dn <<EOF
$(who | perl -lane 'if (/\(:([0-9])+\)/) {print "$F[0] $1"; exit}')
EOF
userhome=$(getent passwd $user | cut -f6 -d:)
export XAUTHORITY=$userhome/.Xauthority
export DISPLAY=:$dn
displays=$(xrandr | perl -lane '/ connected/ && print $F[0]')
echo "connected displays:" $displays
if echo $displays | grep -q DP-2 && echo $displays | grep -q DP-3; then
xrandr --output DP-2 --auto --right-of LVDS-1 --verbose
xrandr --output DP-3 --auto --right-of DP-2 --verbose
else
xrandr --output DP-2 --off
xrandr --output DP-3 --off
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment