Skip to content

Instantly share code, notes, and snippets.

@jwshea
Forked from XenGi/99-displaymagic.rules
Created September 25, 2019 01:48
Show Gist options
  • Save jwshea/c0f30b804796b8f0a19d55468590f177 to your computer and use it in GitHub Desktop.
Save jwshea/c0f30b804796b8f0a19d55468590f177 to your computer and use it in GitHub Desktop.
display magic
#/etc/udev/rules.d/99-displaymagic.rules
# enable LVDS on HDMI disconnect
SUBSYSTEM=="drm", ACTION=="change", RUN+="/usr/local/bin/displaymagic.sh"
#!/bin/bash
#
# display magic v0.1.0
#
# The following script toggles between the internal monitor and an external
# monitor.
#
# by XenGi
##### edit config #####
EXTERNAL="HDMI1"
#TODO: EXTERNAL2="VGA1"
INTERNAL="LVDS1"
export DISPLAY=":0.0"
USER="bob"
#USER=$(who | grep $DISPLAY\) | head -1 | cut -f 1 -d ' ')
##### end config #####
# Check if we are root
if [[ $EUID -ne 0 ]] ; then
echo "This script must be run as root"
exit 1
fi
# check for udev rule
if [[ ! -f /etc/udev/rules.d/99-displaymagic.rules ]] ; then
echo "No udev rule found. Put '99-displaymagic.rules' in '/etc/udev/rules.d' and execute 'udevadm control --reload' as root."
exit
fi
# the script
export XAUTHORITY="/home/$USER/.Xauthority"
# just wait a second so that things run smooth
sleep 1
STATUS=$(su $USER -c "xrandr --current | grep $EXTERNAL | cut -d \ -f 2")
if [[ "$STATUS" == "disconnected" ]] ; then
# switch HDMI on and LVDS off
su $USER -c "xrandr --output $INTERNAL --off"
su $USER -c "xrandr --output $EXTERNAL --auto"
# set HDMI output as primary audio
su $USER -c "pactl set-card-profile 0 output:hdmi-surround"
fi
#TODO: add the same funcionality for the VGA port.
#STATUS="`su $USER -c "xrandr --current | grep $EXTERNAL2 | cut -d \ -f 2"`"
#if [[ "$STATUS" == "disconnected" ]] ; then
# # switch VGA on and LVDS off
# su $USER -c "xrandr --output $INTERNAL --off"
# su $USER -c "xrandr --output $EXTERNAL2 --auto"
# # set internal speakers and mic as primary audio
# su $USER -c "pactl set-card-profile 0 output:analog-stereo+input:analog-stereo"
#fi
if [[ "$STATUS" == "connected" ]] ; then
# switch HDMI off and LVDS on
su $USER -c "xrandr --output $EXTERNAL --off"
su $USER -c "xrandr --output $INTERNAL --auto"
# set internal speakers and mic as primary audio
su $USER -c "pactl set-card-profile 0 output:analog-stereo+input:analog-stereo"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment