#!/bin/sh # SPDX-License-Identifier: AGPL-3.0-only # Copyright 2022 Sxmo Contributors export PATH="${XDG_CONFIG_HOME:-$HOME/.config}/sxmo/hooks:/usr/share/sxmo/default_hooks:$PATH" if [ -z "${XDG_RUNTIME_DIR:-}" ] || [ ! -d "${XDG_RUNTIME_DIR}" ]; then if [ -d "/run/user/$(id -u)" ]; then export XDG_RUNTIME_DIR="/run/user/$(id -u)" elif [ -d "/run/user/10000" ]; then export XDG_RUNTIME_DIR="/run/user/10000" fi fi sxmo_log() { logger -t sxmo_audio "$*" 2>/dev/null || true } CFG_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/sxmo" [ -d "$CFG_DIR" ] || mkdir -p "$CFG_DIR" 2>/dev/null || true ALSA_CARD="alsa_card.platform-7702000.sound" clamp_pct() { v="$1" d="${2:-85}" case "$v" in ''|*[!0-9-]*) v="$d" ;; esac [ "$v" -lt 0 ] && v=0 [ "$v" -gt 100 ] && v=100 printf "%d" "$v" } normalize_out_dev() { case "$1" in *[Bb]luetooth*|*[Bb]luez*) printf "Bluetooth" ;; *[Ee]arpiece*) printf "Earpiece" ;; *[Hh]eadphone*|*[Hh]eadset*) printf "Headphones" ;; *) printf "Speaker" ;; esac } normalize_in_dev() { case "$1" in *[Bb]luetooth*|*[Bb]luez*) printf "Bluetooth" ;; *[Hh]eadset*) printf "Headset" ;; *) printf "Mic1" ;; esac } find_bt_card() { pactl list short cards 2>/dev/null | awk '$2 ~ /^bluez_card\./ { print $2; exit }' } find_bt_profile() { bt_card="$(find_bt_card)" [ -z "$bt_card" ] && return 0 pactl list cards 2>/dev/null | awk -v c="$bt_card" ' $1 == "Name:" { in_card = ($2 == c) } in_card && $1 == "Active" && $2 == "Profile:" { print $3; exit } ' } is_bt_hfp() { prof="$(find_bt_profile)" case "$prof" in headset-head-unit*|*hfp*|*hsp*) return 0 ;; *) return 1 ;; esac } launch_bt_sco_pacat() { bt_sink="$1" [ -n "$bt_sink" ] || return 1 if [ -r /tmp/sxmo_bt_sco_keepalive.pid ]; then read -r kpid < /tmp/sxmo_bt_sco_keepalive.pid 2>/dev/null || kpid="" if [ -n "$kpid" ] && kill -0 "$kpid" 2>/dev/null && \ grep -qa 'pacat' "/proc/$kpid/cmdline" 2>/dev/null; then return 0 fi fi command -v pacat >/dev/null 2>&1 || return 1 pacat --playback --device="$bt_sink" --rate=8000 --channels=1 --format=s16le /dev/zero >/dev/null 2>&1 & printf "%d\n" "$!" > /tmp/sxmo_bt_sco_keepalive.pid 2>/dev/null || true return 0 } apply_bt_sink_volume() { pct="$1" bt_sink="$(find_bt_sink)" if [ "$pct" -gt 0 ]; then if [ -n "$bt_sink" ]; then pactl set-sink-mute "$bt_sink" 0 >/dev/null 2>&1 || true pactl set-sink-volume "$bt_sink" "${pct}%" >/dev/null 2>&1 || true fi pactl set-sink-mute @DEFAULT_SINK@ 0 >/dev/null 2>&1 || true pactl set-sink-volume @DEFAULT_SINK@ "${pct}%" >/dev/null 2>&1 || true else if [ -n "$bt_sink" ]; then if is_bt_hfp && [ -r /tmp/sxmo_bt_sco_keepalive.want ]; then pactl set-sink-volume "$bt_sink" 0% >/dev/null 2>&1 || true else pactl set-sink-mute "$bt_sink" 1 >/dev/null 2>&1 || true fi fi pactl set-sink-mute @DEFAULT_SINK@ 1 >/dev/null 2>&1 || true fi } start_bt_sco_keepalive() { printf "1\n" > /tmp/sxmo_bt_sco_keepalive.want 2>/dev/null || true bt_sink="$(find_bt_sink)" if [ -n "$bt_sink" ]; then launch_bt_sco_pacat "$bt_sink" || true return 0 fi if [ -r /tmp/sxmo_bt_sco_wait.pid ]; then read -r wpid < /tmp/sxmo_bt_sco_wait.pid 2>/dev/null || wpid="" if [ -n "$wpid" ] && kill -0 "$wpid" 2>/dev/null; then return 0 fi fi ( _n=0 while [ "$_n" -lt 25 ]; do sleep 0.1 [ -r /tmp/sxmo_bt_sco_keepalive.want ] || break _sink="$(find_bt_sink)" if [ -n "$_sink" ]; then launch_bt_sco_pacat "$_sink" || true _v="$(read_dev_vol Bluetooth)" apply_bt_sink_volume "$(clamp_pct "${_v:-80}" 80)" break fi _n=$(( _n + 1 )) done rm -f /tmp/sxmo_bt_sco_wait.pid 2>/dev/null || true ) >/dev/null 2>&1 & printf "%d\n" "$!" > /tmp/sxmo_bt_sco_wait.pid 2>/dev/null || true } stop_bt_sco_keepalive() { rm -f /tmp/sxmo_bt_sco_keepalive.want 2>/dev/null || true if [ -r /tmp/sxmo_bt_sco_wait.pid ]; then read -r wpid < /tmp/sxmo_bt_sco_wait.pid 2>/dev/null || wpid="" rm -f /tmp/sxmo_bt_sco_wait.pid 2>/dev/null || true [ -n "$wpid" ] && kill -TERM "$wpid" 2>/dev/null || true fi if [ -r /tmp/sxmo_bt_sco_keepalive.pid ]; then read -r kpid < /tmp/sxmo_bt_sco_keepalive.pid 2>/dev/null || kpid="" rm -f /tmp/sxmo_bt_sco_keepalive.pid 2>/dev/null || true [ -n "$kpid" ] && kill -TERM "$kpid" 2>/dev/null || true fi for _bpid in $(ps 2>/dev/null | awk '/pacat --playback --device=bluez_output/ && !/awk/ {print $1}'); do kill -TERM "$_bpid" 2>/dev/null || true done } find_bt_sink() { pactl list short sinks 2>/dev/null | awk '$2 ~ /^bluez_output\./ { print $2; exit }' } find_bt_source() { pactl list short sources 2>/dev/null | awk '$2 ~ /^bluez_input\./ { print $2; exit }' } move_streams_to_sink() { target_sink="$1" [ -z "$target_sink" ] && return 0 for sid in $(pactl list short sink-inputs 2>/dev/null | awk '{print $1}'); do pactl move-sink-input "$sid" "$target_sink" >/dev/null 2>&1 || true done } ensure_bt_a2dp_profile() { bt_card="$(find_bt_card)" [ -z "$bt_card" ] && return 1 prof="$(find_bt_profile)" if [ "$prof" = "off" ] || [ -z "$prof" ]; then if ! pactl set-card-profile "$bt_card" a2dp-sink >/dev/null 2>&1; then avail_prof="$(pactl list cards 2>/dev/null | awk -v c="$bt_card" ' $1 == "Name:" { in_c = ($2 == c); in_p = 0 } in_c && $1 == "Profiles:" { in_p = 1; next } in_c && in_p && /^[[:space:]]*Active Profile:/ { in_p = 0 } in_c && in_p && $1 ~ /^a2dp-sink/ && $0 !~ /available: no/ { sub(/:$/, "", $1); print $1; exit } ')" if [ -n "$avail_prof" ]; then pactl set-card-profile "$bt_card" "$avail_prof" >/dev/null 2>&1 || true else pactl set-card-profile "$bt_card" headset-head-unit-cvsd >/dev/null 2>&1 || pactl set-card-profile "$bt_card" headset-head-unit >/dev/null 2>&1 || true fi fi fi return 0 } vol_file_for_dev() { dev="$(normalize_out_dev "$1")" case "$dev" in Bluetooth) printf "%s/bluetooth_vol" "$CFG_DIR" ;; Earpiece) printf "%s/earpiece_vol" "$CFG_DIR" ;; Headphones) printf "%s/headphones_vol" "$CFG_DIR" ;; *) printf "%s/speaker_vol" "$CFG_DIR" ;; esac } default_vol_for_dev() { dev="$(normalize_out_dev "$1")" case "$dev" in Bluetooth|Headphones) printf "80" ;; *) printf "85" ;; esac } # Linear percentage (0..100%) to hardware digital volume register: # - Speaker (RX3 Digital Volume): 0 -> 0, 1..100 -> 56..88 (-28 dB .. +4 dB) # - Earpiece (RX1 Digital Volume): 0 -> 0, 1..100 -> 60..92 (-24 dB .. +8 dB) # - Headphones (RX1 & RX2 Digital): 0 -> 0, 1..100 -> 52..84 (-32 dB .. 0 dB) # - Microphone (TX1 Digital Volume): 0 -> 0, 1..100 -> 56..88 (-28 dB .. +4 dB) # PipeWire software sink/source is kept at 100% (0.0 dB unity gain) when unmuted # so software cubic scaling never compounds with the linear hardware curve. pct_to_hw_out() { dev="$(normalize_out_dev "$1")" pct="$(clamp_pct "$2" 85)" if [ "$pct" -le 0 ]; then printf "0" return fi case "$dev" in Earpiece) printf "%d" "$(( 60 + (pct * 32 + 50) / 100 ))" ;; Headphones) printf "%d" "$(( 52 + (pct * 32 + 50) / 100 ))" ;; *) printf "%d" "$(( 56 + (pct * 32 + 50) / 100 ))" ;; esac } pct_to_hw_mic() { pct="$(clamp_pct "$1" 85)" if [ "$pct" -le 0 ]; then printf "0" return fi printf "%d" "$(( 56 + (pct * 32 + 50) / 100 ))" } pct_to_hw_adc() { pct="$(clamp_pct "$1" 85)" if [ "$pct" -le 0 ]; then printf "0" return fi adc="$(( (pct * 8 + 50) / 100 ))" [ "$adc" -lt 1 ] && adc=1 printf "%d" "$adc" } deviceget() { sink="$(pactl get-default-sink 2>/dev/null || true)" case "$sink" in bluez_output.*) printf "Bluetooth\n" > /tmp/sxmo_audio_out_device 2>/dev/null || true printf "Bluetooth\n" return 0 ;; esac if [ -r /tmp/sxmo_audio_out_device ]; then read -r dev < /tmp/sxmo_audio_out_device 2>/dev/null || dev="" if [ -n "$dev" ]; then dev="$(normalize_out_dev "$dev")" if [ "$dev" = "Bluetooth" ]; then bt_sink="$(find_bt_sink)" if [ -n "$bt_sink" ]; then sco_mm=0 [ -r /tmp/sxmo_bt_sco_mm_active ] && read -r sco_mm < /tmp/sxmo_bt_sco_mm_active 2>/dev/null || sco_mm=0 if [ "$sco_mm" != "1" ]; then pactl set-default-sink "$bt_sink" >/dev/null 2>&1 || true move_streams_to_sink "$bt_sink" fi elif [ -z "$(find_bt_card)" ] && [ ! -e /tmp/sxmo_bt_connecting ]; then if [ "$(cat /tmp/sxmo_jack_plugged 2>/dev/null || true)" = "1" ]; then dev="Headphones" else dev="Speaker" fi printf "%s\n" "$dev" > /tmp/sxmo_audio_out_device 2>/dev/null || true fi fi printf "%s\n" "$dev" return 0 fi fi if [ -r "$CFG_DIR/audio_out_device" ]; then read -r dev < "$CFG_DIR/audio_out_device" 2>/dev/null || dev="" if [ -n "$dev" ]; then dev="$(normalize_out_dev "$dev")" if [ "$dev" = "Bluetooth" ] && [ -z "$(find_bt_sink)" ] && [ -z "$(find_bt_card)" ]; then dev="Speaker" fi printf "%s\n" "$dev" > /tmp/sxmo_audio_out_device 2>/dev/null || true printf "%s\n" "$dev" return 0 fi fi case "$sink" in *Earpiece*) dev="Earpiece" ;; *Headphones*) dev="Headphones" ;; *) dev="Speaker" ;; esac printf "%s\n" "$dev" > /tmp/sxmo_audio_out_device 2>/dev/null || true printf "%s\n" "$dev" } devicegetinput() { src="$(pactl get-default-source 2>/dev/null || true)" case "$src" in bluez_input.*) printf "Bluetooth\n" > /tmp/sxmo_audio_in_device 2>/dev/null || true printf "Bluetooth\n" return 0 ;; esac if [ -r /tmp/sxmo_audio_in_device ]; then read -r in_dev < /tmp/sxmo_audio_in_device 2>/dev/null || in_dev="" if [ -n "$in_dev" ]; then in_dev="$(normalize_in_dev "$in_dev")" if [ "$in_dev" = "Bluetooth" ] && [ -z "$(find_bt_source)" ]; then in_dev="Mic1" printf "%s\n" "$in_dev" > /tmp/sxmo_audio_in_device 2>/dev/null || true fi printf "%s\n" "$in_dev" return 0 fi fi if [ -r "$CFG_DIR/audio_in_device" ]; then read -r in_dev < "$CFG_DIR/audio_in_device" 2>/dev/null || in_dev="" if [ -n "$in_dev" ]; then in_dev="$(normalize_in_dev "$in_dev")" if [ "$in_dev" = "Bluetooth" ] && [ -z "$(find_bt_source)" ]; then in_dev="Mic1" fi printf "%s\n" "$in_dev" > /tmp/sxmo_audio_in_device 2>/dev/null || true printf "%s\n" "$in_dev" return 0 fi fi case "$src" in *Headset*) in_dev="Headset" ;; *) in_dev="Mic1" ;; esac printf "%s\n" "$in_dev" > /tmp/sxmo_audio_in_device 2>/dev/null || true printf "%s\n" "$in_dev" } fast_out_dev() { if [ -r /tmp/sxmo_audio_out_device ]; then read -r _fdev < /tmp/sxmo_audio_out_device 2>/dev/null || _fdev="" if [ -n "$_fdev" ]; then normalize_out_dev "$_fdev" return 0 fi fi deviceget } fast_in_dev() { if [ -r /tmp/sxmo_audio_in_device ]; then read -r _fidev < /tmp/sxmo_audio_in_device 2>/dev/null || _fidev="" if [ -n "$_fidev" ]; then normalize_in_dev "$_fidev" return 0 fi fi devicegetinput } read_dev_vol() { dev="$(normalize_out_dev "${1:-$(fast_out_dev)}")" vf="$(vol_file_for_dev "$dev")" defv="$(default_vol_for_dev "$dev")" val="$defv" if [ -r "$vf" ]; then read -r val < "$vf" 2>/dev/null || val="$defv" fi clamp_pct "$val" "$defv" } read_mic_vol() { val=85 if [ -r "$CFG_DIR/mic_vol" ]; then read -r val < "$CFG_DIR/mic_vol" 2>/dev/null || val=85 fi clamp_pct "$val" 85 } volismuted() { if [ -r /tmp/sxmo_out_muted ]; then read -r m < /tmp/sxmo_out_muted 2>/dev/null || m=0 [ "$m" = "1" ] && return 0 fi cur="$(read_dev_vol "$(fast_out_dev)")" [ "$cur" -le 0 ] } micismuted() { if [ -r /tmp/sxmo_mic_muted ]; then read -r m < /tmp/sxmo_mic_muted 2>/dev/null || m=0 [ "$m" = "1" ] && return 0 fi cur="$(read_mic_vol)" [ "$cur" -le 0 ] } apply_alsa_out() { dev="$(normalize_out_dev "${1:-$(fast_out_dev)}")" pct="$(clamp_pct "${2:-$(read_dev_vol "$dev")}" 85)" muted=0 if [ -r /tmp/sxmo_out_muted ]; then read -r muted < /tmp/sxmo_out_muted 2>/dev/null || muted=0 fi if [ "$muted" = "1" ] || [ "$pct" -le 0 ]; then eff_pct=0 else eff_pct="$pct" fi hw="$(pct_to_hw_out "$dev" "$eff_pct")" if command -v amixer >/dev/null 2>&1; then case "$dev" in Earpiece) if [ "$eff_pct" -gt 0 ]; then ear_sw="Switch" ear_hw="$hw" else ear_sw="ZERO" ear_hw="0" fi amixer -c 0 -s >/dev/null 2>&1 </dev/null 2>&1 </dev/null 2>&1 </dev/null 2>&1 < /tmp/sxmo_pw_sink_muted 2>/dev/null || true else printf "1\n" > /tmp/sxmo_pw_sink_muted 2>/dev/null || true fi apply_bt_sink_volume "$eff_pct" return 0 fi if [ "$eff_pct" -gt 0 ]; then printf "0\n" > /tmp/sxmo_pw_sink_muted 2>/dev/null || true pactl set-sink-mute @DEFAULT_SINK@ 0 >/dev/null 2>&1 || true bananarchy_set_sink_volume "$eff_pct" else printf "1\n" > /tmp/sxmo_pw_sink_muted 2>/dev/null || true pactl set-sink-mute @DEFAULT_SINK@ 1 >/dev/null 2>&1 || true fi } # Bananarchy: the MSM8905 mainline kernel exposes no ALSA card yet, so the # amixer csets below are no-ops and PipeWire's null sink is the real output. # @DEFAULT_SINK@ does not resolve when wireplumber has no default configured # node, so resolve the first live sink id and set its volume directly. bananarchy_set_sink_volume() { pct="$1" if [ -z "${XDG_RUNTIME_DIR:-}" ]; then XDG_RUNTIME_DIR="/run/user/$(id -u)" export XDG_RUNTIME_DIR fi if command -v wpctl >/dev/null 2>&1; then sink="$(wpctl status 2>/dev/null | sed -n '/Sinks:/,/Sources:/p' \ | sed -n 's/^[^0-9]*\([0-9][0-9]*\)\..*/\1/p' | head -n 1)" if [ -n "$sink" ]; then wpctl set-volume "$sink" "${pct}%" >/dev/null 2>&1 || true if [ "$pct" -le 0 ]; then wpctl set-mute "$sink" 1 >/dev/null 2>&1 || true else wpctl set-mute "$sink" 0 >/dev/null 2>&1 || true fi return 0 fi fi pactl set-sink-volume @DEFAULT_SINK@ "${pct}%" >/dev/null 2>&1 || true } apply_vol_out() { dev="$(normalize_out_dev "${1:-$(fast_out_dev)}")" pct="$(clamp_pct "${2:-$(read_dev_vol "$dev")}" 85)" muted=0 if [ -r /tmp/sxmo_out_muted ]; then read -r muted < /tmp/sxmo_out_muted 2>/dev/null || muted=0 fi if [ "$muted" = "1" ] || [ "$pct" -le 0 ]; then eff_pct=0 else eff_pct="$pct" fi pw_muted="" [ -r /tmp/sxmo_pw_sink_muted ] && read -r pw_muted < /tmp/sxmo_pw_sink_muted 2>/dev/null || pw_muted="" if [ "$dev" = "Bluetooth" ]; then if [ "$eff_pct" -gt 0 ]; then if [ "$pw_muted" != "0" ]; then printf "0\n" > /tmp/sxmo_pw_sink_muted 2>/dev/null || true fi else if [ "$pw_muted" != "1" ]; then printf "1\n" > /tmp/sxmo_pw_sink_muted 2>/dev/null || true fi fi apply_bt_sink_volume "$eff_pct" return 0 fi hw="$(pct_to_hw_out "$dev" "$eff_pct")" case "$dev" in Earpiece) if [ "$eff_pct" -gt 0 ]; then amixer -c 0 -s >/dev/null 2>&1 </dev/null 2>&1 </dev/null 2>&1 </dev/null 2>&1 </dev/null 2>&1 </dev/null 2>&1 < /tmp/sxmo_pw_sink_muted 2>/dev/null || true pactl set-sink-mute @DEFAULT_SINK@ 0 >/dev/null 2>&1 || true fi bananarchy_set_sink_volume "$eff_pct" else if [ "$pw_muted" != "1" ]; then printf "1\n" > /tmp/sxmo_pw_sink_muted 2>/dev/null || true pactl set-sink-mute @DEFAULT_SINK@ 1 >/dev/null 2>&1 || true fi bananarchy_set_sink_volume 0 fi } apply_alsa_in() { in_dev="$(normalize_in_dev "${1:-$(fast_in_dev)}")" pct="$(clamp_pct "${2:-$(read_mic_vol)}" 85)" muted=0 if [ -r /tmp/sxmo_mic_muted ]; then read -r muted < /tmp/sxmo_mic_muted 2>/dev/null || muted=0 fi if [ "$muted" = "1" ] || [ "$pct" -le 0 ]; then eff_pct=0 else eff_pct="$pct" fi tx_hw="$(pct_to_hw_mic "$eff_pct")" adc_hw="$(pct_to_hw_adc "$eff_pct")" if command -v amixer >/dev/null 2>&1; then if [ "$eff_pct" -gt 0 ]; then if [ "$in_dev" = "Bluetooth" ] && is_bt_hfp && [ ! -r /tmp/sxmo_in_call ]; then amixer -c 0 -s >/dev/null 2>&1 < /tmp/xdrawer_mic_mux 2>/dev/null || true amixer -c 0 -s >/dev/null 2>&1 < /tmp/xdrawer_mic_mux 2>/dev/null || true amixer -c 0 -s >/dev/null 2>&1 </dev/null 2>&1 </dev/null 2>&1 || true if [ "$in_dev" = "Bluetooth" ]; then pactl set-source-volume @DEFAULT_SOURCE@ "${eff_pct}%" >/dev/null 2>&1 || true else pactl set-source-volume @DEFAULT_SOURCE@ 100% >/dev/null 2>&1 || true fi else pactl set-source-mute @DEFAULT_SOURCE@ 1 >/dev/null 2>&1 || true fi } profile_for_devices() { out_dev="$(normalize_out_dev "$1")" in_dev="$(normalize_in_dev "$2")" [ "$out_dev" = "Bluetooth" ] && out_dev="Speaker" [ "$in_dev" = "Bluetooth" ] && in_dev="Mic1" case "${out_dev}:${in_dev}" in Headphones:Headset) printf "HiFi (Headphones, Headset)" ;; Headphones:Mic1) printf "HiFi (Headphones, Mic1)" ;; Earpiece:Headset) printf "HiFi (Earpiece, Headset)" ;; Earpiece:Mic1) printf "HiFi (Earpiece, Mic1)" ;; Speaker:Headset) printf "HiFi (Headset, Speaker)" ;; *) printf "HiFi (Mic1, Speaker)" ;; esac } sync_pipewire_profile() { out_dev="$(normalize_out_dev "$1")" in_dev="$(normalize_in_dev "$2")" prof="$(profile_for_devices "$out_dev" "$in_dev")" pactl set-card-profile alsa_card.comprC0 off >/dev/null 2>&1 || true want_sco_mm=0 if [ "$out_dev" = "Bluetooth" ] && is_bt_hfp; then start_bt_sco_keepalive [ ! -r /tmp/sxmo_in_call ] && want_sco_mm=1 elif [ "$in_dev" = "Bluetooth" ] && is_bt_hfp; then start_bt_sco_keepalive [ ! -r /tmp/sxmo_in_call ] && want_sco_mm=1 else stop_bt_sco_keepalive fi prev_sco_mm=0 if [ -r /tmp/sxmo_bt_sco_mm_active ]; then read -r prev_sco_mm < /tmp/sxmo_bt_sco_mm_active 2>/dev/null || prev_sco_mm=0 fi if [ "$want_sco_mm" != "$prev_sco_mm" ]; then printf "%d\n" "$want_sco_mm" > /tmp/sxmo_bt_sco_mm_active 2>/dev/null || true if [ "$want_sco_mm" = "1" ]; then amixer -c 0 cset "name='PRI_MI2S_RX Audio Mixer MultiMedia1'" 0 >/dev/null 2>&1 || true amixer -c 0 cset "name='INT_BT_SCO_RX Audio Mixer MultiMedia1'" 1 >/dev/null 2>&1 || true amixer -c 0 cset "name='MultiMedia2 Mixer TERT_MI2S_TX'" 0 >/dev/null 2>&1 || true amixer -c 0 cset "name='MultiMedia2 Mixer INT_BT_SCO_TX'" 1 >/dev/null 2>&1 || true else amixer -c 0 cset "name='INT_BT_SCO_RX Audio Mixer MultiMedia1'" 0 >/dev/null 2>&1 || true amixer -c 0 cset "name='PRI_MI2S_RX Audio Mixer MultiMedia1'" 1 >/dev/null 2>&1 || true amixer -c 0 cset "name='MultiMedia2 Mixer INT_BT_SCO_TX'" 0 >/dev/null 2>&1 || true if [ ! -r /tmp/sxmo_in_call ]; then amixer -c 0 cset "name='MultiMedia2 Mixer TERT_MI2S_TX'" 1 >/dev/null 2>&1 || true fi fi pactl set-card-profile "$ALSA_CARD" off >/dev/null 2>&1 || true fi pactl set-card-profile "$ALSA_CARD" "$prof" >/dev/null 2>&1 || true if [ "$out_dev" = "Bluetooth" ]; then if [ "$want_sco_mm" = "1" ]; then rm -f /tmp/sxmo_bt_connecting 2>/dev/null || true pactl set-default-sink "alsa_output.platform-7702000.sound.HiFi__Speaker__sink" >/dev/null 2>&1 || true move_streams_to_sink "alsa_output.platform-7702000.sound.HiFi__Speaker__sink" else if [ ! -r /tmp/sxmo_in_call ]; then ensure_bt_a2dp_profile || true fi bt_sink="$(find_bt_sink)" if [ -n "$bt_sink" ]; then rm -f /tmp/sxmo_bt_connecting 2>/dev/null || true pactl set-default-sink "$bt_sink" >/dev/null 2>&1 || true move_streams_to_sink "$bt_sink" else touch /tmp/sxmo_bt_connecting 2>/dev/null || true ( for _i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16; do sleep 0.25 cur_want="$(cat /tmp/sxmo_audio_out_device 2>/dev/null || true)" [ "$cur_want" = "Bluetooth" ] || break if [ ! -r /tmp/sxmo_in_call ]; then ensure_bt_a2dp_profile || true fi bt_sink="$(find_bt_sink)" if [ -n "$bt_sink" ]; then rm -f /tmp/sxmo_bt_connecting 2>/dev/null || true pactl set-default-sink "$bt_sink" >/dev/null 2>&1 || true move_streams_to_sink "$bt_sink" out_vol="$(read_dev_vol Bluetooth)" apply_alsa_out Bluetooth "$out_vol" sxmo_hook_statusbar.sh volume >/dev/null 2>&1 || true if [ -p /tmp/dwm_plugin_networks.fifo ]; then printf "REFRESH\n" > /tmp/dwm_plugin_networks.fifo 2>/dev/null || true fi exit 0 fi done rm -f /tmp/sxmo_bt_connecting 2>/dev/null || true ) >/dev/null 2>&1 & fi fi else rm -f /tmp/sxmo_bt_connecting 2>/dev/null || true sink_name="alsa_output.platform-7702000.sound.HiFi__${out_dev}__sink" pactl set-default-sink "$sink_name" >/dev/null 2>&1 || true move_streams_to_sink "$sink_name" fi if [ "$in_dev" = "Bluetooth" ]; then if [ "$want_sco_mm" = "1" ]; then pactl set-default-source "alsa_input.platform-7702000.sound.HiFi__Mic1__source" >/dev/null 2>&1 || true else bt_src="$(find_bt_source)" if [ -n "$bt_src" ]; then pactl set-default-source "$bt_src" >/dev/null 2>&1 || true fi fi else src_name="alsa_input.platform-7702000.sound.HiFi__${in_dev}__source" pactl set-default-source "$src_name" >/dev/null 2>&1 || true fi } notifyvol() { vol="$1" if [ "-" = "$vol" ]; then vol="$(cat)" fi if [ "muted" = "$vol" ]; then vol=0 fi if [ -z "$SXMO_WOB_DISABLE" ] && [ -p "${XDG_RUNTIME_DIR:-/run/user/10000}/sxmo.obsock" ]; then printf "%s\n" "$vol" > "$XDG_RUNTIME_DIR"/sxmo.obsock fi } volget() { target_dev="${1:-$(fast_out_dev)}" if [ -z "$1" ] && volismuted; then printf "muted\n" return 0 fi read_dev_vol "$target_dev" printf "\n" } acquire_volset_lock() { if mkdir /tmp/sxmo_volset.lock 2>/dev/null; then printf "%d\n" "$$" > /tmp/sxmo_volset.lock/pid 2>/dev/null || true return 0 fi lpid="" [ -r /tmp/sxmo_volset.lock/pid ] && read -r lpid < /tmp/sxmo_volset.lock/pid 2>/dev/null || lpid="" if [ -n "$lpid" ] && kill -0 "$lpid" 2>/dev/null; then return 1 fi rm -rf /tmp/sxmo_volset.lock 2>/dev/null || true if mkdir /tmp/sxmo_volset.lock 2>/dev/null; then printf "%d\n" "$$" > /tmp/sxmo_volset.lock/pid 2>/dev/null || true return 0 fi return 1 } volset() { pct="$(clamp_pct "$1" 85)" active_dev="$(fast_out_dev)" target_dev="$(normalize_out_dev "${2:-$active_dev}")" vf="$(vol_file_for_dev "$target_dev")" printf "%d\n" "$pct" > "$vf" 2>/dev/null || true if [ "$target_dev" = "$active_dev" ]; then if [ "$pct" -gt 0 ]; then printf "0\n" > /tmp/sxmo_out_muted 2>/dev/null || true else printf "1\n" > /tmp/sxmo_out_muted 2>/dev/null || true fi if [ -r /tmp/sxmo_call_vol ]; then printf "%d\n" "$pct" > /tmp/sxmo_call_vol 2>/dev/null || true fi if ! acquire_volset_lock; then return 0 fi applied_pct="" while : ; do cur_pct="$(read_dev_vol "$target_dev")" apply_vol_out "$target_dev" "$cur_pct" applied_pct="$cur_pct" next_pct="$(read_dev_vol "$target_dev")" [ "$next_pct" = "$applied_pct" ] && break done rm -rf /tmp/sxmo_volset.lock 2>/dev/null || true final_pct="$(read_dev_vol "$target_dev")" if [ "$final_pct" != "$applied_pct" ]; then apply_vol_out "$target_dev" "$final_pct" fi sxmo_hook_statusbar.sh volume >/dev/null 2>&1 & fi } # A direct evdev volume daemon and the X11 binding can both react to one # physical key press. Collapse calls that arrive within 300 ms so the volume # only steps once per press. bananarchy_vol_debounce() { # Timestamp based, no background subshell: a stale lock directory could # otherwise block every future volume step forever. now="$(awk '{printf "%d", $1 * 100}' /proc/uptime 2>/dev/null || printf 0)" last="$(cat /tmp/bananarchy_vol_step 2>/dev/null || printf 0)" [ -n "$now" ] || now=0 [ -n "$last" ] || last=0 [ $((now - last)) -lt 30 ] && return 1 printf '%s\n' "$now" > /tmp/bananarchy_vol_step 2>/dev/null || true return 0 } volup() { bananarchy_vol_debounce || return 0 step="${1:-5}" target_dev="$(normalize_out_dev "${2:-$(fast_out_dev)}")" cur="$(read_dev_vol "$target_dev")" next="$(( cur + step ))" [ "$next" -gt 100 ] && next=100 volset "$next" "$target_dev" } voldown() { bananarchy_vol_debounce || return 0 step="${1:-5}" target_dev="$(normalize_out_dev "${2:-$(fast_out_dev)}")" cur="$(read_dev_vol "$target_dev")" next="$(( cur - step ))" [ "$next" -lt 0 ] && next=0 volset "$next" "$target_dev" } voltogglemute() { active_dev="$(fast_out_dev)" if volismuted; then printf "0\n" > /tmp/sxmo_out_muted 2>/dev/null || true cur="$(read_dev_vol "$active_dev")" [ "$cur" -le 0 ] && cur="$(default_vol_for_dev "$active_dev")" printf "%d\n" "$cur" > "$(vol_file_for_dev "$active_dev")" 2>/dev/null || true apply_vol_out "$active_dev" "$cur" else printf "1\n" > /tmp/sxmo_out_muted 2>/dev/null || true apply_vol_out "$active_dev" 0 fi sxmo_hook_statusbar.sh volume >/dev/null 2>&1 & } volmute() { active_dev="$(fast_out_dev)" printf "1\n" > /tmp/sxmo_out_muted 2>/dev/null || true apply_vol_out "$active_dev" 0 sxmo_hook_statusbar.sh volume >/dev/null 2>&1 & } volunmute() { active_dev="$(fast_out_dev)" printf "0\n" > /tmp/sxmo_out_muted 2>/dev/null || true cur="$(read_dev_vol "$active_dev")" [ "$cur" -le 0 ] && cur="$(default_vol_for_dev "$active_dev")" printf "%d\n" "$cur" > "$(vol_file_for_dev "$active_dev")" 2>/dev/null || true apply_vol_out "$active_dev" "$cur" sxmo_hook_statusbar.sh volume >/dev/null 2>&1 & } micvolget() { if micismuted; then printf "muted\n" return 0 fi read_mic_vol printf "\n" } micvolset() { pct="$(clamp_pct "$1" 85)" printf "%d\n" "$pct" > "$CFG_DIR/mic_vol" 2>/dev/null || true if [ "$pct" -gt 0 ]; then printf "0\n" > /tmp/sxmo_mic_muted 2>/dev/null || true else printf "1\n" > /tmp/sxmo_mic_muted 2>/dev/null || true fi apply_alsa_in "$(fast_in_dev)" "$pct" sxmo_hook_statusbar.sh volume >/dev/null 2>&1 & } micvolup() { step="${1:-5}" cur="$(read_mic_vol)" next="$(( cur + step ))" [ "$next" -gt 100 ] && next=100 micvolset "$next" } micvoldown() { step="${1:-5}" cur="$(read_mic_vol)" next="$(( cur - step ))" [ "$next" -lt 0 ] && next=0 micvolset "$next" } mictogglemute() { if micismuted; then printf "0\n" > /tmp/sxmo_mic_muted 2>/dev/null || true cur="$(read_mic_vol)" [ "$cur" -le 0 ] && cur=85 printf "%d\n" "$cur" > "$CFG_DIR/mic_vol" 2>/dev/null || true apply_alsa_in "$(fast_in_dev)" "$cur" else printf "1\n" > /tmp/sxmo_mic_muted 2>/dev/null || true apply_alsa_in "$(fast_in_dev)" 0 fi sxmo_hook_statusbar.sh volume >/dev/null 2>&1 & } deviceset() { out_dev="$(normalize_out_dev "$1")" in_dev="$(devicegetinput)" printf "%s\n" "$out_dev" > /tmp/sxmo_audio_out_device 2>/dev/null || true printf "%s\n" "$out_dev" > "$CFG_DIR/audio_out_device" 2>/dev/null || true sync_pipewire_profile "$out_dev" "$in_dev" out_vol="$(read_dev_vol "$out_dev")" apply_alsa_out "$out_dev" "$out_vol" apply_alsa_in "$in_dev" "$(read_mic_vol)" if [ -p /tmp/dwm_plugin_music.fifo ]; then ( printf 'AUDIO_ROUTE\n' > /tmp/dwm_plugin_music.fifo ) 2>/dev/null & fi sxmo_hook_statusbar.sh volume >/dev/null 2>&1 & } devicesetinput() { in_dev="$(normalize_in_dev "$1")" out_dev="$(deviceget)" printf "%s\n" "$in_dev" > /tmp/sxmo_audio_in_device 2>/dev/null || true printf "%s\n" "$in_dev" > "$CFG_DIR/audio_in_device" 2>/dev/null || true sync_pipewire_profile "$out_dev" "$in_dev" apply_alsa_out "$out_dev" "$(read_dev_vol "$out_dev")" apply_alsa_in "$in_dev" "$(read_mic_vol)" sxmo_hook_statusbar.sh volume >/dev/null 2>&1 & } devicelist() { if [ -n "$(find_bt_sink)" ] || [ -n "$(find_bt_card)" ]; then printf "Bluetooth\nSpeaker\nEarpiece\nHeadphones\n" else printf "Speaker\nEarpiece\nHeadphones\n" fi } devicelistinput() { if [ -n "$(find_bt_source)" ]; then printf "Bluetooth\nMic1\nHeadset\n" else printf "Mic1\nHeadset\n" fi } devicecycle() { dir="${1:-next}" cur="$(deviceget)" has_bt=0 if [ -n "$(find_bt_sink)" ] || [ -n "$(find_bt_card)" ]; then has_bt=1 fi if [ "$dir" = "prev" ]; then if [ "$has_bt" = "1" ]; then case "$cur" in Bluetooth) next="Headphones" ;; Speaker) next="Bluetooth" ;; Earpiece) next="Speaker" ;; *) next="Earpiece" ;; esac else case "$cur" in Speaker) next="Headphones" ;; Earpiece) next="Speaker" ;; *) next="Earpiece" ;; esac fi else if [ "$has_bt" = "1" ]; then case "$cur" in Bluetooth) next="Speaker" ;; Speaker) next="Earpiece" ;; Earpiece) next="Headphones" ;; *) next="Bluetooth" ;; esac else case "$cur" in Speaker) next="Earpiece" ;; Earpiece) next="Headphones" ;; *) next="Speaker" ;; esac fi fi deviceset "$next" printf "%s\n" "$next" } deviceapply() { out_dev="$(deviceget)" in_dev="$(devicegetinput)" sync_pipewire_profile "$out_dev" "$in_dev" apply_alsa_out "$out_dev" "$(read_dev_vol "$out_dev")" apply_alsa_in "$in_dev" "$(read_mic_vol)" if [ -p /tmp/dwm_plugin_music.fifo ]; then ( printf 'AUDIO_ROUTE\n' > /tmp/dwm_plugin_music.fifo ) 2>/dev/null & fi sxmo_hook_statusbar.sh volume >/dev/null 2>&1 & } devicegetdefaultsink() { pactl get-default-sink 2>/dev/null || true } devicegetdefaultsource() { pactl get-default-source 2>/dev/null || true } sourceset() { pactl set-default-source "$1" >/dev/null 2>&1 || true case "$1" in bluez_input.*|*Bluetooth*) devicesetinput Bluetooth ;; *Headset*) devicesetinput Headset ;; *) devicesetinput Mic1 ;; esac } sinkset() { pactl set-default-sink "$1" >/dev/null 2>&1 || true case "$1" in bluez_output.*|*Bluetooth*) deviceset Bluetooth ;; *Earpiece*) deviceset Earpiece ;; *Headphones*) deviceset Headphones ;; *) deviceset Speaker ;; esac } profileset() { pactl set-card-profile "$1" "$2" >/dev/null 2>&1 || true case "$2" in *Headphones*Headset*) printf "Headset\n" > /tmp/sxmo_audio_in_device deviceset Headphones ;; *Headphones*) printf "Mic1\n" > /tmp/sxmo_audio_in_device deviceset Headphones ;; *Earpiece*Headset*) printf "Headset\n" > /tmp/sxmo_audio_in_device deviceset Earpiece ;; *Earpiece*) printf "Mic1\n" > /tmp/sxmo_audio_in_device deviceset Earpiece ;; *Headset*Speaker*) printf "Headset\n" > /tmp/sxmo_audio_in_device deviceset Speaker ;; *) printf "Mic1\n" > /tmp/sxmo_audio_in_device deviceset Speaker ;; esac } _outportssubmenu() { active_out="$(deviceget)" for dev in $(devicelist); do if [ "$active_out" = "$dev" ]; then icon="$icon_ton" else icon="$icon_tof" fi v="$(read_dev_vol "$dev")" printf " %s %s (%s%%) ^ deviceset %s\n" "$icon" "$dev" "$v" "$dev" done } _inportssubmenu() { active_in="$(devicegetinput)" for dev in $(devicelistinput); do if [ "$active_in" = "$dev" ]; then icon="$icon_ton" else icon="$icon_tof" fi printf " %s %s ^ devicesetinput %s\n" "$icon" "$dev" "$dev" done } _ringmodesubmenu() { if [ -f "$CFG_DIR/.noring" ]; then printf " %s Ring ^ rm -f \"%s/.noring\"\n" "$icon_tof" "$CFG_DIR" else printf " %s Ring ^ touch \"%s/.noring\"\n" "$icon_ton" "$CFG_DIR" fi if [ -f "$CFG_DIR/.novibrate" ]; then printf " %s Vibrate ^ rm -f \"%s/.novibrate\"\n" "$icon_tof" "$CFG_DIR" else printf " %s Vibrate ^ touch \"%s/.novibrate\"\n" "$icon_ton" "$CFG_DIR" fi } menuchoices() { cur_out="$(deviceget)" cur_vol="$(volget)" cur_mic_vol="$(micvolget)" grep . <