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?

guy
  • 141
  • 5

2 Answers2

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

Frederick Nord
  • 549
  • 5
  • 9
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 ...

user.dz
  • 48,105
  • Tried, doesn't work... updated the question – guy Oct 26 '14 at 07: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 '14 at 13:27
  • @guy, try sudo -u yourusername pactl ... – user.dz Oct 26 '14 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 '14 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 '14 at 14:27
  • ok, still working manually. didn't work because of writing the log there was needed root permission (tried without writing the log and worked). – guy Oct 26 '14 at 14:31
  • Does it work if you switch to root, su - then run it? – user.dz Oct 26 '14 at 14:35
  • yes, when I run from terminal after changing to root user – guy Oct 26 '14 at 21:02