Ask Your Question
0

Set rule to automatically switch the sound output to HDMI when plugged in

asked 2014-11-17 14:52:04 -0500

elemer82 gravatar image

updated 2014-11-17 19:58:53 -0500

mether gravatar image

This is what I have done: I created a file /usr/lib/udev/rules.d/hdmi_sound.rules as root with the content:

SUBSYSTEM=="drm", RUN+="/usr/local/bin/hdmi_sound_toggle"

Create a file /usr/local/bin/hdmi_sound_toggle as root with the content:

#!/bin/sh
USER_NAME=`who | grep "(:0)" | cut -f 1 -d ' '| sort -u`
USER_ID=`id -u $USER_NAME`
HDMI_STATUS=`cat /sys/class/drm/card0/*HDMI*/status`

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

if [ $HDMI_STATUS = "connected" ]
then
    sudo -u $USER_NAME pactl --server $PULSE_SERVER set-card-profile 0 output:hdmi-stereo+input:analog-stereo
    notify-send HDMI "Connected"
else
    sudo -u $USER_NAME pactl --server $PULSE_SERVER set-card-profile 0 output:analog-stereo+input:analog-stereo
    notify-send HDMI "DISConnected"
fi

Then make it executable with chmod 0755 /usr/local/bin/hdmi_sound_toggle

Reloaded udev rules with: sudo udevadm control --reload-rules

And this udev rule is not firing, If i execute in terminal /usr/local/bin/hdmi_sound_toggle then the sound does go to the intended output, but for some reason udev is not starting my rule. I tried restarting my machine as well.

What am I missing?

How to fire/start a program when hdmi is plugged/unplugged ?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2014-11-17 20:25:07 -0500

mclmmc gravatar image

I'm not familiar udev, but I suspect that the problem is in the script.

The script is not launched by you.

To verify add the following line after HDMI_STATUS definition

echo USER_NAME USER_ID HDMI_STATUS > /tmp/hdmi.out
exit

I see another problem: what happen if you attach your device before log in, or isn't detached at shutdown?

edit flag offensive delete link more

Comments

@mclmmc I don't think the problem is in the script, because if I type in terminal /usr/local/bin/hdmi_sound_toggle than it gets executed and the notification pops up as well (notify-send HDMI "Connected"') for some reason the udev rule doesn't get executed, that is what I believe, however I don't have much experience with udev either.

elemer82 gravatar imageelemer82 ( 2014-11-18 11:54:54 -0500 )edit

@mclmmc I cannot start my Laptop if HDMI is connected, Dell Inspiron N5110 , there is some kind of bug with the BIOS, I was reading on the internet that it confuses the HDMI port with a USB port and wants to boot from the HDMI cable, but because cannot succeed; all it shows is a black screen, I have to restart with HDMI unplugged to start up normally, I can plug HDMI back once the OS started to load.

elemer82 gravatar imageelemer82 ( 2014-11-18 12:03:55 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2014-11-17 14:52:04 -0500

Seen: 1,118 times

Last updated: Nov 17 '14