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)
This commit is contained in:
Luxferre committed 2026-10-29 20:42:26 +02:00
1 parent e42eb573ed
commit 370f8b085c
33 files changed
+1351 -488

No files matched your search

+37 -7
View File
@@ -468,10 +468,10 @@ int callscreen_run_main(int argc, char** argv) {
int softkeyPending = 0;
int64_t softkeyDeadlineMs = 0;
auto pctToHwVol = [](int pct, bool spk) -> long {
auto pctToHwVol = [](int pct, bool spk, bool hp = false) -> long {
int c = std::clamp(pct, 5, 100);
// RX1/RX3 Digital Volume: 84 = 0 dB, 88 = +4 dB, 92 = +8 dB, 94 = +10 dB
return spk ? (68 + (c * 26) / 100) : (68 + (c * 24) / 100);
if (hp) return 52 + (c * 32 + 50) / 100;
return spk ? (56 + (c * 32 + 50) / 100) : (60 + (c * 32 + 50) / 100);
};
auto refreshHardwareAudioState = [&]() {
@@ -505,7 +505,8 @@ int callscreen_run_main(int argc, char** argv) {
auto setSpeakerRoute = [&](bool toSpeaker) {
speakerOn = toSpeaker;
long hwVol = pctToHwVol(callVolumePct, toSpeaker);
bool jackPlugged = (readIntFile("/tmp/sxmo_jack_plugged", 0) == 1);
long hwVol = pctToHwVol(callVolumePct, toSpeaker, !toSpeaker && jackPlugged);
alsa.setBoolean("PRI_MI2S_RX Voice Mixer VoiceMMode1", true);
alsa.setBoolean("VoiceMMode1 Capture Mixer TERT_MI2S_TX", !micMuted);
alsa.setEnum("CIC1 MUX", 0);
@@ -516,20 +517,43 @@ int callscreen_run_main(int argc, char** argv) {
}
if (toSpeaker) {
alsa.setEnum("EAR_S", 0);
alsa.setEnum("HPHL", 0);
alsa.setEnum("HPHR", 0);
alsa.setEnum("RX1 MIX1 INP1", 0);
alsa.setEnum("RX2 MIX1 INP1", 0);
alsa.setBoolean("SPK DAC Switch", true);
alsa.setEnum("RX3 MIX1 INP1", 3);
alsa.setInteger("RX3 Digital Volume", hwVol);
runAsyncCmd("sxmo_audio.sh device set Speaker >/dev/null 2>&1");
statusNotice = "Audio routed to Speaker";
} else if (jackPlugged) {
alsa.setBoolean("SPK DAC Switch", false);
alsa.setEnum("RX3 MIX1 INP1", 0);
alsa.setInteger("RX3 Digital Volume", 0);
alsa.setEnum("EAR_S", 0);
alsa.setEnum("RX1 MIX1 INP1", 3);
alsa.setEnum("RX2 MIX1 INP1", 4);
alsa.setEnum("RDAC2 MUX", 1);
alsa.setEnum("HPHL", 1);
alsa.setEnum("HPHR", 1);
alsa.setBoolean("RX1 Mute Switch", false);
alsa.setInteger("RX1 Digital Volume", hwVol);
alsa.setInteger("RX2 Digital Volume", hwVol);
runAsyncCmd("sxmo_audio.sh device set Headphones >/dev/null 2>&1");
statusNotice = "Audio routed to Headphones";
} else {
alsa.setBoolean("SPK DAC Switch", false);
alsa.setEnum("RX3 MIX1 INP1", 0);
alsa.setInteger("RX3 Digital Volume", 0);
alsa.setEnum("HPHL", 0);
alsa.setEnum("HPHR", 0);
alsa.setEnum("RX2 MIX1 INP1", 0);
alsa.setEnum("RX1 MIX1 INP1", 3);
alsa.setEnum("RDAC2 MUX", 0);
alsa.setEnum("EAR_S", 1);
alsa.setBoolean("RX1 Mute Switch", false);
alsa.setInteger("RX1 Digital Volume", hwVol);
runAsyncCmd("sxmo_audio.sh device set Earpiece >/dev/null 2>&1");
statusNotice = "Audio routed to Earpiece";
}
statusNoticeUntilMs = nowMs() + 2000;
@@ -538,13 +562,19 @@ int callscreen_run_main(int argc, char** argv) {
auto adjustVolume = [&](int deltaPct) {
callVolumePct = std::clamp(callVolumePct + deltaPct, 5, 100);
writeIntFile("/tmp/sxmo_call_vol", callVolumePct);
alsa.setInteger("RX1 Digital Volume", pctToHwVol(callVolumePct, false));
bool jackPlugged = (readIntFile("/tmp/sxmo_jack_plugged", 0) == 1);
if (speakerOn) {
alsa.setInteger("RX3 Digital Volume", pctToHwVol(callVolumePct, true));
alsa.setInteger("RX3 Digital Volume", pctToHwVol(callVolumePct, true, false));
} else if (jackPlugged) {
long hw = pctToHwVol(callVolumePct, false, true);
alsa.setInteger("RX1 Digital Volume", hw);
alsa.setInteger("RX2 Digital Volume", hw);
} else {
alsa.setInteger("RX1 Digital Volume", pctToHwVol(callVolumePct, false, false));
}
char cmd[128];
std::snprintf(cmd, sizeof(cmd),
"wpctl set-volume @DEFAULT_AUDIO_SINK@ %d%% >/dev/null 2>&1",
"sxmo_audio.sh vol set %d >/dev/null 2>&1",
callVolumePct);
runAsyncCmd(cmd);
};
+17 -20
View File
@@ -256,33 +256,30 @@ std::string getConfigDirPath() {
}
int readSavedSpeakerVol() {
std::ifstream ifs(getConfigDirPath() + "/speaker_vol");
int v = 85;
std::string outDev = "Speaker";
{
std::ifstream dfs("/tmp/sxmo_audio_out_device");
if (dfs) std::getline(dfs, outDev);
}
std::string vfile = "/speaker_vol";
int defv = 85;
if (outDev.find("Headphone") != std::string::npos || outDev.find("Headset") != std::string::npos) {
vfile = "/headphones_vol";
defv = 80;
} else if (outDev.find("Earpiece") != std::string::npos) {
vfile = "/earpiece_vol";
}
std::ifstream ifs(getConfigDirPath() + vfile);
int v = defv;
if (ifs && (ifs >> v)) {
return std::clamp((v / 5) * 5, 0, 100);
}
return 85;
return defv;
}
void applySystemSpeakerVol(int pct) {
pct = std::clamp((pct / 5) * 5, 0, 100);
std::string cfgDir = getConfigDirPath();
::mkdir(cfgDir.c_str(), 0755);
{
std::ofstream ofs(cfgDir + "/speaker_vol", std::ios::trunc);
if (ofs) ofs << pct << "\n";
}
int rx3 = (pct <= 0) ? 0 : (54 + (pct * 38) / 100);
runAsyncCmd("amixer -c 0 cset \"name='SPK DAC Switch'\" " +
std::string(pct > 0 ? "1" : "0") + " >/dev/null 2>&1; "
"amixer -c 0 cset \"name='RX3 MIX1 INP1'\" 3 >/dev/null 2>&1; "
"amixer -c 0 cset \"name='RX3 Digital Volume'\" " +
std::to_string(rx3) + " >/dev/null 2>&1; "
"wpctl set-mute @DEFAULT_AUDIO_SINK@ " +
std::string(pct == 0 ? "1" : "0") + " >/dev/null 2>&1; "
"wpctl set-volume @DEFAULT_AUDIO_SINK@ " +
std::to_string(pct) + "% >/dev/null 2>&1; "
"sxmo_hook_statusbar.sh audio >/dev/null 2>&1");
runAsyncCmd("sxmo_audio.sh vol set " + std::to_string(pct) + " >/dev/null 2>&1");
}
int runMusicApp(int argc, char** argv) {
+7 -10
View File
@@ -327,6 +327,7 @@ int runRingtonesApp(int argc, char** argv) {
::kill(previewPid, SIGTERM);
::waitpid(previewPid, nullptr, WNOHANG);
previewPid = -1;
runAsyncCmd("sxmo_audio.sh device apply >/dev/null 2>&1");
}
previewPath.clear();
};
@@ -354,9 +355,8 @@ int runRingtonesApp(int argc, char** argv) {
stopPreview();
settings = loadRingSettings();
int rvol = std::clamp(settings.ringVol, 0, 100);
int rx3 = (rvol <= 0) ? 0 : (60 + rvol * 24 / 100);
int mpvVol = rvol;
std::string volArg = "--volume=" + std::to_string(mpvVol);
int rx3 = (rvol <= 0) ? 0 : (56 + (rvol * 32 + 50) / 100);
std::string volArg = "--volume=100";
runAsyncCmd("amixer -c 0 cset \"name='EAR_S'\" 0 >/dev/null 2>&1; "
"amixer -c 0 cset \"name='RX1 MIX1 INP1'\" 0 >/dev/null 2>&1; "
"amixer -c 0 cset \"name='PRI_MI2S_RX Audio Mixer MultiMedia1'\" 1 >/dev/null 2>&1; "
@@ -365,12 +365,10 @@ int runRingtonesApp(int argc, char** argv) {
"amixer -c 0 cset \"name='RX3 Digital Volume'\" " +
std::to_string(rx3) + " >/dev/null 2>&1; "
"wpctl set-mute @DEFAULT_AUDIO_SINK@ 0 >/dev/null 2>&1; "
"wpctl set-volume @DEFAULT_AUDIO_SINK@ " +
std::to_string(rvol) + "% >/dev/null 2>&1; "
"wpctl set-volume @DEFAULT_AUDIO_SINK@ 100% >/dev/null 2>&1; "
"(sleep 0.25; amixer -c 0 cset \"name='RX3 Digital Volume'\" " +
std::to_string(rx3) + " >/dev/null 2>&1; "
"wpctl set-volume @DEFAULT_AUDIO_SINK@ " +
std::to_string(rvol) + "% >/dev/null 2>&1) &");
"wpctl set-volume @DEFAULT_AUDIO_SINK@ 100% >/dev/null 2>&1) &");
pid_t pid = ::fork();
if (pid == 0) {
std::string runtimeDir = "/run/user/" + std::to_string(::getuid());
@@ -623,11 +621,10 @@ int runRingtonesApp(int argc, char** argv) {
settings.ringVol = std::clamp(settings.ringVol + delta * 5, 0, 100);
saveRingSettings(settings);
if (previewPid > 0) {
int rx3 = (settings.ringVol <= 0) ? 0 : (60 + settings.ringVol * 34 / 100);
int rx3 = (settings.ringVol <= 0) ? 0 : (56 + (settings.ringVol * 32 + 50) / 100);
runAsyncCmd("amixer -c 0 cset \"name='RX3 Digital Volume'\" " +
std::to_string(rx3) + " >/dev/null 2>&1; "
"wpctl set-volume @DEFAULT_AUDIO_SINK@ " +
std::to_string(settings.ringVol) + "% >/dev/null 2>&1");
"wpctl set-volume @DEFAULT_AUDIO_SINK@ 100% >/dev/null 2>&1");
}
setBanner("Ring volume: " + std::to_string(settings.ringVol) + "%");
} else if (settingsRow == 2) {