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"
$USER
is known when you run yr script from within the udev rule. The pbm may well lie therein./etc/udev/rules.d/99hdmi.rules
. 3) Yr rule seems too broad in its application scope. I am not sureSUBYSTEM=="drm"
is enough to correctly identify the device(s) of interest. 4) UseACTION=="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)...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.