diff --git a/huawei-soundcard-headphones-monitor.sh b/huawei-soundcard-headphones-monitor.sh index 282a7dc..7eef0df 100644 --- a/huawei-soundcard-headphones-monitor.sh +++ b/huawei-soundcard-headphones-monitor.sh @@ -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() { @@ -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