You are not logged in.

#1 2014-02-05 23:13:39

Winston-Wolfe
Member
Registered: 2013-06-01
Posts: 49

HDMI udev toggle

I have a udev rule that toggles HDMI on and off automatically when the cable is un/plugged in. It's working fine under my Ubuntu install, but I can't seem to make it work under my Arch install (same system).

Running

xrandr --output LVDS1 --auto --primary --output HDMI1 --auto --right-of LVDS1

in terminal works as intended, just can't seem to get udev to toggle it.

Here's what works in Ubuntu, is there anything Arch specific that needs to be changed?:

/etc/udev/rules.d/hdmi.rules:

SUBSYSTEM=="drm", ACTION=="change", RUN+="/usr/local/bin/MToggle-udev"

/usr/local/bin/MToggle-udev:

USER="$(who | grep :0\) | cut -f 1 -d ' ')"
export XAUTHORITY=/home/$USER/.Xauthority
export DISPLAY=:0

########### Settings ###########

# Use 'xrandr' to find these
DP="DP1"
VGA="VGA1"
HDMI="HDMI1"
INTERNAL_DISPLAY="LVDS1"

# Check /sys/class/drm for the exact location
DP_STATUS="$(cat /sys/class/drm/card0-DP-1/status)"
VGA_STATUS="$(cat /sys/class/drm/card0-VGA-1/status)"
HDMI_STATUS="$(cat /sys/class/drm/card0-HDMI-A-1/status)"

# Do no change!
EXTERNAL_DISPLAY=""

# Check to see if the external display is connected
if [ "${DP_STATUS}" = connected ]; then
EXTERNAL_DISPLAY=$DP
fi
if [ "${VGA_STATUS}" = connected ]; then
EXTERNAL_DISPLAY=$VGA
fi
if [ "${HDMI_STATUS}" = connected ]; then
EXTERNAL_DISPLAY=$HDMI
fi

# The external display is connected
if [ "$EXTERNAL_DISPLAY" != "" ]; then

# Set the display settings
xrandr --output LVDS1 --auto --primary --output HDMI1 --auto --right-of LVDS1

# If connected via HDMI, change the sound profile to output HDMI audio
if [ $EXTERNAL_DISPLAY=$HDMI ]; then
sudo -u $USER pactl set-card-profile 0 output:hdmi-surround
fi

# The external display is not connected
else

# Restore to single display
xrandr --output $EXTERNAL_DISPLAY --off

# Restore laptop sound profile
sudo -u $USER pactl set-card-profile 0 output:analog-stereo+input:analog-stereo
fi
exit 0

I made sure the script was executable and 755 permissions, and that I updated the udev rules:

sudo udevadm control --reload-rules

Last edited by Winston-Wolfe (2014-02-05 23:29:57)

Offline

#2 2014-02-06 01:16:29

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: HDMI udev toggle

See `man udev` about (ab)using RUN+.

You are better off using SYSTEMD_WANTS and a service file. See `man systemd.device` for the details. Or this...


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

Board footer

Powered by FluxBB