Ask Your Question
2

How to execute script when HDMI cable is plugged in

asked 2015-06-19 05:02:21 -0500

stemid gravatar image

I know this question has been asked before because I've based my udev rule on those answers found on google.

Unfortunately I can't get it to work on Fedora 22.

Here is a paste showing my udev rule and my script.

ACTION=="change", SUBSYSTEM=="drm", HOTPLUG=="1", RUN+="/home/stemid/bin/dp_hotplug.sh"

I've tried making the script log to a file to ensure that it is running but it never outputs anything.

I've executed the script manually and it does what it's supposed to, so it seems to work when executed by root.

I'm using Fedora 22. I've also verified that the HDMI-A-1 status file is updated when my displayport cable is plugged in.

As for activating the rule I've tried udevadm control --reload-rules and rebooting but nothing helps.

I get the impression that people are using this method, so what am I missing?

Edit: Also I must add that I can see the event of the cable being plugged in and out in udevadm monitor, I can also see my own udevadm trigger -s drm in udevadm monitor but the script never runs. The events are happening on card0 with subsystem drm as expected.

If I run the script while the cable is plugged in then it changes the audio profile of the card and sound is output on HDMI as intended.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2015-06-25 13:14:13 -0500

stemid gravatar image

I had named the udev rule .rule instead of .rules. So case closed. ;)

Also I noticed that it didn't work well with HOTPLUG=="1". So in the end, this is what worked for me.

ACTION=="change", SUBSYSTEM=="drm", RUN+="/home/stemid/bin/dp_hotplug.sh"

In a file called /etc/udev/rules.d/70-dp_hotplug.rules

And just for posterity, here is the script.

#!/bin/bash

Username=stemid
UserID=$(id -u $Username)
HDMIStatus=$(cat /sys/class/drm/card0/*HDMI-A-1/status)

export PULSE_SERVER="unix:/run/user/$UserID/pulse/native"

if [ $HDMIStatus = 'connected' ]; then
  sudo -u $Username pactl --server $PULSE_SERVER set-card-profile 0 output:hdmi-stereo+input:analog-stereo
  sudo -u $Username notify-send HDMI "Connected"
else
  sudo -u $Username pactl --server $PULSE_SERVER set-card-profile 0 output:analog-stereo
  sudo -u $Username notify-send HDMI "Disconnected"
fi

Change Username to your own, make it executable and stick it where the udev rule can find it.

Find out which audio profiles you have with pacmd list-cards.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-06-19 04:48:57 -0500

Seen: 2,210 times

Last updated: Jun 25 '15