aboutsummaryrefslogtreecommitdiff
path: root/pw-prof
blob: 82ab6d9c26c39d2825a019941343e0e809ab630b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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"