Files
bananarchy/overlay/rootfs/usr/bin/sxmo_soundmonitor.sh
T
Luxferre 370f8b085c Implement headphone autodetection, earpiece routing, and linear volume controls
- Fix msm8916-wcd ALSA UCM2 configs (Earpiece.conf, Speaker.conf,
  Headphones.conf, PrimaryMic.conf, HeadsetMic.conf) so Speaker,
  Earpiece, and Headphones outputs and Mic1/Headset inputs are always
  available and mutually exclusive without invalid mixer controls
- Add event-driven wired headphone/headset jack detection and automatic
  audio routing in sxmo_soundmonitor.sh via evtest on the msm8916
  Headset Jack input device
- Make Earpiece, Speaker, and Headphones first-class routable output
  devices across sxmo_audio.sh, sxmo_modemaudio.sh, xdrawer, dmenu, and
  callscreen
- Make volume controls strictly linear across all channels by keeping
  PipeWire software gain at unity (100%) when unmuted and linearly
  mapping 0-100% to hardware digital volume registers (RX3 for Speaker,
  RX1 for Earpiece, RX1+RX2 for Headphones, TX1+ADC for Microphone)
2026-10-29 20:42:26 +02:00

140 lines
3.8 KiB
Bash
Executable File

#!/bin/sh
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2023 Sxmo Contributors
# shellcheck source=scripts/core/sxmo_common.sh
. "$(dirname "$0")/sxmo_common.sh"
find_jack_event_dev() {
for sysname in /sys/class/input/event*/device/name; do
[ -r "$sysname" ] || continue
read -r dname < "$sysname" 2>/dev/null || continue
case "$dname" in
*[Jj]ack*|*[Hh]eadset*)
ev="${sysname%/device/name}"
ev="/dev/input/${ev##*/}"
if [ -c "$ev" ]; then
printf "%s\n" "$ev"
return 0
fi
;;
esac
done
return 1
}
is_hp_plugged() {
jdev="$1"
if [ -n "$jdev" ] && command -v evtest >/dev/null 2>&1; then
evtest --query "$jdev" EV_SW SW_HEADPHONE_INSERT >/dev/null 2>&1
rc=$?
[ "$rc" -eq 10 ] && return 0
fi
if command -v amixer >/dev/null 2>&1; then
amixer -c 0 cget "name='Headphone Jack'" 2>/dev/null | grep -q ": values=on" && return 0
fi
return 1
}
is_mic_plugged() {
jdev="$1"
if [ -n "$jdev" ] && command -v evtest >/dev/null 2>&1; then
evtest --query "$jdev" EV_SW SW_MICROPHONE_INSERT >/dev/null 2>&1
rc=$?
[ "$rc" -eq 10 ] && return 0
fi
if command -v amixer >/dev/null 2>&1; then
amixer -c 0 cget "name='Mic Jack'" 2>/dev/null | grep -q ": values=on" && return 0
fi
return 1
}
handle_jack_insert() {
jdev="$1"
cur_out="$(sxmo_audio.sh device get 2>/dev/null || printf "Speaker")"
if [ "$cur_out" != "Headphones" ]; then
printf "%s\n" "$cur_out" > /tmp/sxmo_prev_out_device 2>/dev/null || true
fi
printf "1\n" > /tmp/sxmo_jack_plugged 2>/dev/null || true
if is_mic_plugged "$jdev"; then
printf "Headset\n" > /tmp/sxmo_audio_in_device 2>/dev/null || true
sxmo_audio.sh device setinput Headset >/dev/null 2>&1 || true
else
printf "Mic1\n" > /tmp/sxmo_audio_in_device 2>/dev/null || true
fi
sxmo_audio.sh device set Headphones >/dev/null 2>&1 || true
sxmo_log "Wired headphones plugged in -> routed audio to Headphones"
}
handle_jack_remove() {
printf "0\n" > /tmp/sxmo_jack_plugged 2>/dev/null || true
printf "Mic1\n" > /tmp/sxmo_audio_in_device 2>/dev/null || true
sxmo_audio.sh device setinput Mic1 >/dev/null 2>&1 || true
target_out="Speaker"
if [ -r /tmp/sxmo_in_call ]; then
read -r incall < /tmp/sxmo_in_call 2>/dev/null || incall=0
if [ "$incall" = "1" ]; then
target_out="Earpiece"
fi
fi
sxmo_audio.sh device set "$target_out" >/dev/null 2>&1 || true
sxmo_log "Wired headphones unplugged -> routed audio to $target_out"
}
monitor_jack_events() {
while : ; do
jdev="$(find_jack_event_dev || true)"
if [ -z "$jdev" ] || ! command -v evtest >/dev/null 2>&1; then
sleep 5
continue
fi
if is_hp_plugged "$jdev"; then
handle_jack_insert "$jdev"
else
printf "0\n" > /tmp/sxmo_jack_plugged 2>/dev/null || true
sxmo_audio.sh device apply >/dev/null 2>&1 || true
fi
evtest "$jdev" 2>/dev/null | while read -r line; do
case "$line" in
*SW_HEADPHONE_INSERT*value\ 1*)
sleep 0.1
handle_jack_insert "$jdev"
;;
*SW_HEADPHONE_INSERT*value\ 0*)
handle_jack_remove
;;
*SW_MICROPHONE_INSERT*value\ 1*)
sxmo_audio.sh device setinput Headset >/dev/null 2>&1 || true
;;
*SW_MICROPHONE_INSERT*value\ 0*)
sxmo_audio.sh device setinput Mic1 >/dev/null 2>&1 || true
;;
esac
done
sleep 2
done
}
handle_change() {
pactl subscribe 2>/dev/null |
grep -E --line-buffered "Event 'change' on (sink|source)" |
while read -r _; do
sxmo_hook_statusbar.sh volume >/dev/null 2>&1 || true
done
}
cleanup() {
kill 0 2>/dev/null || true
}
trap cleanup EXIT INT TERM
monitor_jack_events &
while : ; do
handle_change || true
sleep 2
done