diff options
-rwxr-xr-x | pw-prof | 75 |
1 files changed, 75 insertions, 0 deletions
@@ -0,0 +1,75 @@ +#!/bin/sh +# +# Toggle between the Headset (HSP/HFP) and High Fidelity profiles using PipeWire +# +# Usage: ./toggle_pw_headset {cycle|a2dp|headset} +################################################################################ + +DEVICES="$(pw-dump | jq 'map(select(.info.props."device.bus" == "bluetooth"))')" +NAME="$(echo "$DEVICES" | jq -r 'map(.info.props."device.alias")[]')" +NDEVS="$(echo "$NAME" | wc -l)" + +if [ "$NDEVS" -ne 1 ]; then + echo "ERROR: there should be 1 device, found $NDEVS:\n" + echo "${NAME}" + exit 1 +fi + +DEVID="$(pw-dump | jq -r ".[] | select(.info.props.\"device.alias\" == \"$NAME\").id")" +PARAMS="$(pw-dump | jq -r ".[] | select(.info.props.\"device.alias\" == \"$NAME\").info.params")" +CURPROFS="$(echo "$PARAMS" | jq -r '.Profile | map(.name) | .[]')" + +PROFILES="$(echo "$PARAMS" | jq -r ".EnumProfile | map(select(.available == \"yes\") | \"\(.priority)\t\(.index)\t\(.name)\") | .[]" | sort -rhk1)" + +BEST_A2DP="$(echo "$PROFILES" | grep -m1 a2dp)" +BEST_HEADSET="$(echo "$PROFILES" | grep -m1 headset)" + +cat >&2 <<EOF +NAME: $NAME +DEVID: $DEVID +CURPROF: $CURPROFS + +PROFILES: +prio idx name +$PROFILES + +BEST PROFILES: +prio idx name +$BEST_A2DP +$BEST_HEADSET + +EOF + +usage() { + echo "Usage: $0 {cycle|a2dp|headset}" + exit 0 +} + +case "$1" in + cycle) + if echo "$CURPROFS" | grep -iq a2dp; then + newprof=headset + else + newprof=a2dp + fi + ;; + a2dp) + newprof=a2dp;; + headset) + newprof=headset;; + *) + usage + ;; +esac + +if [ "$newprof" = "headset" ]; then + newprofidx="$(echo "$BEST_HEADSET" | cut -f2)" + newprofname="$(echo "$BEST_HEADSET" | cut -f3)" +elif [ "$newprof" = "a2dp" ]; then + newprofidx="$(echo "$BEST_A2DP" | cut -f2)" + newprofname="$(echo "$BEST_A2DP" | cut -f3)" +fi + +echo "[$NAME] Setting profile to '$newprofname' (idx $newprofidx)..." + +wpctl set-profile "$DEVID" "$newprofidx" |