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
  • Here is [Set HDMI sound output automatically on connect/disconnect](http://askubuntu.com/questions/263248/set-hdmi-sound-output-automatically-on-connect-disconnect) and [Switching to HDMI Audio when HDMI is plugged into a laptop (14.04)](http://askubuntu.com/questions/458194/switching-to-hdmi-audio-when-hdmi-is-plugged-into-a-laptop-14-04) – Lety Oct 05 '14 at 22:15
  • I used those answers to get to my current state. Again, because the script works when I run it from the terminal, I'm pretty sure that the problem is in the udev rule. – guy Oct 06 '14 at 06:29
  • 1
    It can be related to user who runs script. Take a look at [Set HDMI sound output automatically on connect/disconnect](http://askubuntu.com/questions/263248/set-hdmi-sound-output-automatically-on-connect-disconnect) pactl is runned with sudo -u $user. When you run this script manually, script run with your privilege, while when it auto-run (I guess) user should be root. – Lety Oct 06 '14 at 10:59
  • changed the script, still doesn't auto-run... – guy Oct 15 '14 at 12:06

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
  • 47,137
  • 13
  • 140
  • 258
  • 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