Skip to content

Commit

Permalink
Fix for systems with PipeWire
Browse files Browse the repository at this point in the history
  • Loading branch information
vpkopylov authored and Smoren committed Mar 3, 2024
1 parent 81caa2b commit 7627de7
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions huawei-soundcard-headphones-monitor.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
pidof -o %PPID -x $0 >/dev/null && echo "Script $0 already running" && exit 1

function move_output() {
sudo hda-verb /dev/snd/hwC0D0 0x16 0x701 "$@" > /dev/null 2> /dev/null
hda-verb /dev/snd/hwC0D0 0x16 0x701 "$@" > /dev/null 2> /dev/null
}

function move_output_to_speaker() {
Expand All @@ -35,44 +35,57 @@ function switch_to_speaker() {
move_output_to_speaker

# enable speaker
sudo hda-verb /dev/snd/hwC0D0 0x17 0x70C 0x0002 > /dev/null 2> /dev/null
hda-verb /dev/snd/hwC0D0 0x17 0x70C 0x0002 > /dev/null 2> /dev/null

# disable headphones
sudo hda-verb /dev/snd/hwC0D0 0x1 0x715 0x2 > /dev/null 2> /dev/null
hda-verb /dev/snd/hwC0D0 0x1 0x715 0x2 > /dev/null 2> /dev/null
}

function switch_to_headphones() {
move_output_to_headphones

# disable speaker
sudo hda-verb /dev/snd/hwC0D0 0x17 0x70C 0x0000 > /dev/null 2> /dev/null
hda-verb /dev/snd/hwC0D0 0x17 0x70C 0x0000 > /dev/null 2> /dev/null

# pin output mode
sudo hda-verb /dev/snd/hwC0D0 0x1 0x717 0x2 > /dev/null 2> /dev/null
hda-verb /dev/snd/hwC0D0 0x1 0x717 0x2 > /dev/null 2> /dev/null

# pin enable
sudo hda-verb /dev/snd/hwC0D0 0x1 0x716 0x2 > /dev/null 2> /dev/null
hda-verb /dev/snd/hwC0D0 0x1 0x716 0x2 > /dev/null 2> /dev/null

# clear pin value
sudo hda-verb /dev/snd/hwC0D0 0x1 0x715 0x0 > /dev/null 2> /dev/null
hda-verb /dev/snd/hwC0D0 0x1 0x715 0x0 > /dev/null 2> /dev/null

# sets amixer sink port to headphones instead of the speaker
pacmd set-sink-port alsa_output.pci-0000_00_1f.3-platform-skl_hda_dsp_generic.HiFi__hw_sofhdadsp__sink "[Out] Headphones"
}

function get_sound_card_index() {
card_index=$(cat /proc/asound/cards | grep sof-hda-dsp | head -n1 | grep -Eo "^\s*[0-9]+")
# remove leading white spaces
card_index="${card_index#"${card_index%%[![:space:]]*}"}"
echo $card_index
}

sleep 2 # allows audio system to initialise first

card_index=$(get_sound_card_index)
if [ $card_index == "" ]; then
echo "sof-dha-dsp card is not found in /proc/asound/cards"
return 1
fi

old_status=0

while true; do
# if headphone jack isn't plugged:
if amixer get Headphone | grep -q "off"; then
if amixer "-c${card_index}" get Headphone | grep -q "off"; then
status=1
move_output_to_speaker
move_output_to_speaker
# if headphone jack is plugged:
else
status=2
move_output_to_headphones
move_output_to_headphones
fi

if [ ${status} -ne ${old_status} ]; then
Expand Down

0 comments on commit 7627de7

Please sign in to comment.