1

newbie here.

I want to be able to switch to HDMI audio output by plugging in the HDMI cable during playback. I have followed the advice given here: How to change pulseaudio sink with "pacmd set-default-sink" during playback?

I have integrated it into a udev rule but still it doesn't move the input stream automatically. If I run the same rule/script from terminal it works. You can find the script below.

Note: I do run "sudo udevadm control --reload-rules" everytime I change the script.

I also couldn't manage to retrieve hdmi video output names and audio sink numbers from pactl or xrandr, so I have written them manually, I should do some reading on bash scripts. I would be happy if someone helps with that also. Sorry if I used wrong terminology. And thanks in advance.

#!/bin/bash
# Sound Toggle
# By Charles Cruz

########### Settings ###########
# Backlight Settings
BACKLIGHT_BATTERY=20
BACKLIGHT_AC=100

USERID="$(cat /var/run/ConsoleKit/database | grep -B 6 is_active=true | grep uid= | cut -f 2 -d '=')"
USER="$(grep $USERID /etc/passwd | cut -f 1 -d ':')"
HDMI_STATUS="$(cat /sys/class/drm/card0-HDMI-A-1/status)"
GNOME_SCREENSAVER_PROC=`ps xa | grep gnome-screensaver | head -n 1 | awk '{print $1}'`

INT="eDP1"
EXT="HDMI1"

export `grep -z DBUS_SESSION_BUS_ADDRESS /proc/$GNOME_SCREENSAVER_PROC/environ`
export XAUTHORITY="/home/$USER/.Xauthority"
export DISPLAY="$(cat /var/run/ConsoleKit/database | grep x11_display= | cut -f 2 -d '=')"

# HDMI is connected
if [ "$HDMI_STATUS" = connected ]; then

# Set output to HDMI
sudo -u $USER xrandr --output $EXT --auto
sudo -u $USER xrandr --output $INT --off

# Set the sound card profile
 sudo -u $USER pactl set-default-sink 0

# Move inputs to HDMI

inputs=$(pacmd list-sink-inputs | awk '$1 == "index:" {print $2}')
for INPUT in $inputs; do # Move all current inputs to the new default sound card
 pacmd move-sink-input $INPUT 0
done

# Disable the lockscreen if it's enabled so you can close the lockscreen
if [[ $(sudo -u $USER gsettings get org.gnome.desktop.screensaver lock-enabled) = true ]]; then
sudo -u $USER gsettings set org.gnome.desktop.screensaver lock-enabled false
fi

# HDMI is not connected
else

# Set output to INTERNAL
sudo -u $USER xrandr --output $INT --auto
sudo -u $USER xrandr --output $EXT --off 

# Set the sound card profile
 sudo -u $USER pactl set-default-sink-1

# Move inputs to internal

inputs=$(pacmd list-sink-inputs | awk '$1 == "index:" {print $2}')
for INPUT in $inputs; do # Move all current inputs to the new default sound card
  pacmd move-sink-input $INPUT 1
done

# Restore the lock setting, if necessary
if [[ $(sudo -u $USER gsettings get org.gnome.desktop.screensaver lock-enabled) = false ]]; then
sudo -u $USER gsettings set org.gnome.desktop.screensaver lock-enabled true
fi

# Restore default battery/power brightness
cat /proc/acpi/ac_adapter/AC/state | grep "on-line"
if [ $? -eq 0 ]; then
xbacklight -set $BACKLIGHT_AC # Power cable is connected
else
xbacklight -set $BACKLIGHT_BATTERY # Power cable is not connected
fi
fi

exit 0

And here is the udev rule. It's path is /etc/udev/rules.d/hdmi.rules The path for the previous script was /usr/bin/toggle-sound

SUBSYSTEM=="drm", ACTION=="change", RUN+="/usr/bin/toggle-sound"
5
  • Welcome to AU ! First, please add yr udev rule to yr post and notify me with @Cbhihe afterwards in a comment below. Second, make sure the variable $USER is known when you run yr script from within the udev rule. The pbm may well lie therein.
    – Cbhihe
    Apr 3, 2016 at 6:36
  • @Cbhibe thanks for the welcome. I've added the rule. Only the part where inputs should be moved doesn't work, and that part didn't have a sudo (I ran pacmd commands without sudo and they worked), so I don't know if the variable $USER matters, but to be honest I don't know how to make sure that it is known either.
    – ssulun
    Apr 6, 2016 at 13:44
  • Go ahead and test things anew with comments and references provided below , then we'll get to yr script, but better move to "chat" because it would be too long in this comment area.
    – Cbhihe
    Apr 6, 2016 at 19:24
  • 1) See man udev as well as reactivated.net/writing_udev_rules.html to write udev rules. 2) Temporarily rename yr rule /etc/udev/rules.d/99hdmi.rules. 3) Yr rule seems too broad in its application scope. I am not sure SUBYSTEM=="drm" is enough to correctly identify the device(s) of interest. 4) Use ACTION=="add|remove". 5) I tried yr script. No go. Are you sure it works in terminal ? 6) What OS do you ran ? We can continue this in a chat room (see chat.stackexchange.com/rooms/38034/pacmd-udev-rule)...
    – Cbhihe
    Apr 6, 2016 at 19:36
  • Have you resolved this? What works for me is to create a systemd unit a run it with systemctl [email protected] --user USER --now start my_unit.service where USER is mu actualy user. That way udev run as root can connect to my user's "session" and get pactl to work.
    – sup
    Jan 9, 2022 at 18:38

0

You must log in to answer this question.

Browse other questions tagged .