3

I was annoyed that every time I connect my hdmi cable I need to manually change the sound setting.

I started looking into udev rules, what I came out with are these two files:

/etc/udev/rules.d/99-hdmi_sound.rules

SUBSYSTEM=="drm", RUN+="/lib/udev/hdmi_sound_toggle.sh"

/lib/udev/hdmi_sound_toggle.sh:

#!/bin/bash

HDMI_STATUS=`cat /sys/class/drm/card0/*HDMI*/status`
if [ $HDMI_STATUS = "connected" ]
then
    sudo -u root pactl set-card-profile 0 output:hdmi-stereo
else
    sudo -u root pactl set-card-profile 0 output:analog-stereo
fi

When I'm running hdmi_sound_toggle.sh in the terminal, it works. It does not auto-run, though.

What am I doing wrong?

4

2 Answers 2

0

Have you tried reloading udev? It should recognise new rules automatically, but maybe it didn't...

sudo udevadm control --reload-rules ; sudo udevadm trigger

1
  • I tried.. doesn't work
    – guy
    Oct 18, 2014 at 9:59
0
  • Try rename it to 99-hdmi_sound.rules (with 2 digits only)
  • & use RUN instead and omit ACTION

    SUBSYSTEM=="drm", RUN+="/lib/udev/hdmi_sound_toggle.sh"
    
  • It could be an environment problem (not same user, or undeclared env variables). Try adding some echo or touch commands to trace your script. Example:

    echo `date --rfc-3339='ns'` START >> /home/<your-username>/Desktop/udev_test_log.txt
    

    Put one in beginning, end, inside if, else ...

8
  • Tried, doesn't work... updated the question
    – guy
    Oct 26, 2014 at 7:42
  • ok, the rule actually working. the pactl cmd gives the error: Connection failure: Connection refused pa_context_connect() failed: Connection refused
    – guy
    Oct 26, 2014 at 13:27
  • @guy, try sudo -u yourusername pactl ...
    – user.dz
    Oct 26, 2014 at 14:00
  • tried... the error changed to Failure: No such entity the updated line: sudo -u guy pactl set-card-profile 0 output:hdmi-stereo 2>> /home/guy/Desktop/udev_then_log.txt. just checked - still work when I call the script from terminal
    – guy
    Oct 26, 2014 at 14:17
  • sorry, double checked and it doesn't work manually, I will go over the steps and tell you when it stopped to work.
    – guy
    Oct 26, 2014 at 14:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .