Should headphones be detected automatically

Installing pipewire breaks my script above. It seems that pacmd is not ready for pipewire and that pactl (which also comes from pulseaudio-utils) should be used instead. Here is a bug from 2020 about the issue: What is missing for pacmd to work with pipewire? (#357) · Issues · PipeWire / pipewire · GitLab

Indeed pulseaudio devs seem to advise to migrate from pacmd to pactl (due to missing pipewire support) even giving a migration guide:

So I adapted my script. Another advantage is, that pactl can output in json which makes the script much more robust compared to using grep on the output.

#!/bin/sh

set -eu

active_port=$(pactl --format=json list sinks | jq -r '.[] | select(.name == "alsa_output.platform-sound.stereo-fallback") | .active_port')

if [ "${1:-}" = "status" ]; then
	case $active_port in
	analog-output-speaker) echo '{"text": "🔈", "tooltip": "speakers"}';;
	analog-output-headphones) echo '{"text": "🎧", "tooltip": "headphones"}';;
	esac
else
	case $active_port in
	analog-output-speaker) pactl set-sink-port alsa_output.platform-sound.stereo-fallback analog-output-headphones;;
	analog-output-headphones) pactl set-sink-port alsa_output.platform-sound.stereo-fallback analog-output-speaker;;
	esac
	pkill -SIGRTMIN+8 waybar
fi
3 Likes