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

+190 -58
View File
@@ -48,6 +48,7 @@ constexpr int kRowCount = 10;
// Nerd Font PUA icons matching sxmo_hook_icons.sh & dwm ("Fira Code, DejaVuSansM Nerd Font")
constexpr const char* kIconSpeaker = "\xef\x80\xa8"; // U+F028 speaker
constexpr const char* kIconHeadphones = "\xef\x80\xa5"; // U+F025 headphones
constexpr const char* kIconEarpiece = "\xef\x8a\xa0"; // U+F2A0 earpiece
constexpr const char* kIconFlashlight = "\xf3\xb0\x89\x84"; // U+F0244 flashlight
constexpr const char* kIconSoundOn = "\xf3\xb0\x8e\x88"; // U+F0388 music/sound on
@@ -292,32 +293,56 @@ class PhoneController {
void refreshAll() {
items_.clear();
// 1. Speaker volume (0..100%, mapped to ALSA RX3 Digital Volume 60..92 + PipeWire 0..100%)
int spkPct = readIntFile(getSxmoCfgDir() + "/speaker_vol", -1);
if (spkPct < 0 || spkPct > 100) {
long rx3 = alsa_.getInteger("RX3 Digital Volume", 92);
if (rx3 <= 0) {
spkPct = 0;
} else if (rx3 >= 84) {
spkPct = 100;
std::string outDev = readActiveOutputDevice();
bool outMuted = (readIntFile("/tmp/sxmo_out_muted", 0) == 1);
// 1. Primary output volume (Speaker or Headphones if Headphones are active, linear 0..100%)
if (outDev == "Headphones") {
int hpPct = readIntFile(getSxmoCfgDir() + "/headphones_vol", -1);
if (hpPct < 0 || hpPct > 100) {
long rx1 = alsa_.getInteger("RX1 Digital Volume", 78);
if (rx1 <= 0) {
hpPct = 0;
} else if (rx1 >= 84) {
hpPct = 100;
} else {
hpPct = std::clamp(static_cast<int>(std::lround(((rx1 - 52) * 100.0) / 32.0)), 5, 100);
}
}
hpPct = ((hpPct + 2) / 5) * 5;
items_.push_back({ControlId::SpeakerVol, kIconHeadphones, kIconSoundOff,
"Headphones", true, 0, 100, 5, hpPct});
} else {
int spkPct = readIntFile(getSxmoCfgDir() + "/speaker_vol", -1);
if (spkPct < 0 || spkPct > 100) {
long rx3 = alsa_.getInteger("RX3 Digital Volume", 84);
if (rx3 <= 0) {
spkPct = 0;
} else if (rx3 >= 88) {
spkPct = 100;
} else {
spkPct = std::clamp(static_cast<int>(std::lround(((rx3 - 56) * 100.0) / 32.0)), 5, 100);
}
}
spkPct = ((spkPct + 2) / 5) * 5;
items_.push_back({ControlId::SpeakerVol, kIconSpeaker, kIconSoundOff,
"Speaker", true, 0, 100, 5, spkPct});
}
// 2. Earpiece volume (0..100%, linear mapped to ALSA RX1 Digital Volume 60..92)
int earPct = readIntFile(getSxmoCfgDir() + "/earpiece_vol", -1);
if (earPct < 0 || earPct > 100) {
long rx1 = alsa_.getInteger("RX1 Digital Volume", 88);
if (rx1 <= 0) {
earPct = 0;
} else if (rx1 >= 92) {
earPct = 100;
} else {
spkPct = std::clamp(static_cast<int>(std::lround(((rx3 - 60) * 100.0) / 32.0)), 5, 100);
earPct = std::clamp(static_cast<int>(std::lround(((rx1 - 60) * 100.0) / 32.0)), 5, 100);
}
}
spkPct = (spkPct / 5) * 5;
items_.push_back({ControlId::SpeakerVol, kIconSpeaker, kIconSoundOff,
"Speaker", true, 0, 100, 5, spkPct});
// 2. Earpiece volume (0..100%, mapped to ALSA RX1 Digital Volume 68..92)
long rx1 = alsa_.getInteger("RX1 Digital Volume", 88);
int earPct = 85;
if (rx1 <= 0) {
earPct = 0;
} else if (rx1 >= 68) {
earPct = std::clamp(static_cast<int>(std::lround(((rx1 - 68) * 100.0) / 24.0)), 5, 100);
}
earPct = ((earPct + 2) / 5) * 5;
items_.push_back({ControlId::EarpieceVol, kIconEarpiece, kIconEarpiece,
items_.push_back({ControlId::EarpieceVol, kIconEarpiece, kIconSoundOff,
"Earpiece", true, 0, 100, 5, earPct});
// 3. Ringtone volume (0..100%, stored as SXMO_RINGVOL in ~/.config/sxmo/ringsettings)
@@ -335,17 +360,29 @@ class PhoneController {
items_.push_back({ControlId::VibrateStrength, kIconBell, kIconBell,
"Vibration", false, 1, 3, 1, vibVal});
// 5. Flashlight on/off (toggle)
// 6. Flashlight on/off (toggle)
int torchCur = readIntFile(torchDir_ + "/brightness", 0);
items_.push_back({ControlId::Flashlight, kIconFlashlight, kIconFlashlight,
"Flashlight", false, 0, 1, 1, (torchCur > 0) ? 1 : 0});
// 6. Sound on/off (toggle)
bool spkOn = alsa_.getBoolean("SPK DAC Switch", true);
items_.push_back({ControlId::SoundMute, kIconSoundOn, kIconSoundOff,
"Sound", false, 0, 1, 1, spkOn ? 1 : 0});
// 7. Audio Output Route / Mute (0=Muted, 1=Speaker, 2=Earpiece, 3=Headphones)
int outMode = 1;
if (outMuted) {
outMode = 0;
} else if (outDev == "Earpiece") {
outMode = 2;
} else if (outDev == "Headphones") {
outMode = 3;
} else {
outMode = 1;
}
const char* outIcon = (outMode == 3) ? kIconHeadphones
: (outMode == 2) ? kIconEarpiece
: kIconSoundOn;
items_.push_back({ControlId::SoundMute, outIcon, kIconSoundOff,
"Output", false, 0, 3, 1, outMode});
// 7. Microphone on/off (toggle)
// 8. Microphone on/off (toggle)
unsigned int dec1 = alsa_.getEnum("DEC1 MUX", 1);
if (dec1 > 0) {
savedMicMux_ = dec1;
@@ -398,7 +435,7 @@ class PhoneController {
ControlItem& it = items_[idx];
if (it.isSlider) {
it.value = std::clamp(it.value + dir * it.stepVal, it.minVal, it.maxVal);
} else if (it.id == ControlId::RingerMode) {
} else if (it.id == ControlId::RingerMode || it.id == ControlId::SoundMute) {
it.value = (it.value + dir + 4) % 4;
} else if (it.id == ControlId::VibrateStrength) {
it.value = ((it.value - 1 + dir + 3) % 3) + 1;
@@ -413,7 +450,7 @@ class PhoneController {
return;
}
ControlItem& it = items_[idx];
if (it.id == ControlId::RingerMode) {
if (it.id == ControlId::RingerMode || it.id == ControlId::SoundMute) {
it.value = (it.value + 1) % 4;
apply(it);
} else if (it.id == ControlId::VibrateStrength) {
@@ -426,6 +463,32 @@ class PhoneController {
}
private:
static std::string readActiveOutputDevice() {
auto readLine = [](const std::string& path) -> std::string {
std::ifstream ifs(path);
std::string s;
if (ifs && std::getline(ifs, s)) {
while (!s.empty() && (s.back() == '\r' || s.back() == '\n' || s.back() == ' ')) {
s.pop_back();
}
return s;
}
return {};
};
std::string dev = readLine("/tmp/sxmo_audio_out_device");
if (dev.empty()) {
dev = readLine(getSxmoCfgDir() + "/audio_out_device");
}
if (dev.find("Earpiece") != std::string::npos || dev.find("earpiece") != std::string::npos) {
return "Earpiece";
}
if (dev.find("Headphone") != std::string::npos || dev.find("headphone") != std::string::npos ||
dev.find("Headset") != std::string::npos || dev.find("headset") != std::string::npos) {
return "Headphones";
}
return "Speaker";
}
static std::string getSxmoCfgDir() {
const char* xdg = std::getenv("XDG_CONFIG_HOME");
if (xdg && xdg[0]) {
@@ -518,26 +581,58 @@ class PhoneController {
case ControlId::SpeakerVol: {
std::string cfg = getSxmoCfgDir();
::mkdir(cfg.c_str(), 0755);
writeIntFile(cfg + "/speaker_vol", it.value);
long hw = (it.value <= 0) ? 0 : (60 + std::lround((it.value * 32.0) / 100.0));
if (it.value > 0) {
alsa_.setBoolean("SPK DAC Switch", true);
alsa_.setEnum("RX3 MIX1 INP1", 3);
std::string outDev = readActiveOutputDevice();
if (outDev == "Headphones") {
writeIntFile(cfg + "/headphones_vol", it.value);
writeIntFile("/tmp/sxmo_out_muted", (it.value <= 0) ? 1 : 0);
long hw = (it.value <= 0) ? 0 : (52 + std::lround((it.value * 32.0) / 100.0));
alsa_.setEnum("HPHL", (it.value > 0) ? 1 : 0);
alsa_.setEnum("HPHR", (it.value > 0) ? 1 : 0);
alsa_.setInteger("RX1 Digital Volume", hw);
alsa_.setInteger("RX2 Digital Volume", hw);
char cmd[160];
std::snprintf(cmd, sizeof(cmd),
"sxmo_audio.sh vol set %d Headphones >/dev/null 2>&1",
it.value);
runAsyncCmd(cmd);
} else {
writeIntFile(cfg + "/speaker_vol", it.value);
long hw = (it.value <= 0) ? 0 : (56 + std::lround((it.value * 32.0) / 100.0));
if (outDev == "Speaker") {
writeIntFile("/tmp/sxmo_out_muted", (it.value <= 0) ? 1 : 0);
alsa_.setBoolean("SPK DAC Switch", it.value > 0);
if (it.value > 0) {
alsa_.setEnum("RX3 MIX1 INP1", 3);
}
alsa_.setInteger("RX3 Digital Volume", hw);
}
char cmd[160];
std::snprintf(cmd, sizeof(cmd),
"sxmo_audio.sh vol set %d Speaker >/dev/null 2>&1",
it.value);
runAsyncCmd(cmd);
}
alsa_.setInteger("RX3 Digital Volume", hw);
char cmd[160];
std::snprintf(
cmd, sizeof(cmd),
"wpctl set-mute @DEFAULT_AUDIO_SINK@ %d >/dev/null 2>&1; "
"wpctl set-volume @DEFAULT_AUDIO_SINK@ %d%% >/dev/null 2>&1; "
"sxmo_hook_statusbar.sh volume >/dev/null 2>&1",
(it.value <= 0) ? 1 : 0, it.value);
runAsyncCmd(cmd);
break;
}
case ControlId::EarpieceVol: {
long hw = (it.value <= 0) ? 0 : (68 + std::lround((it.value * 24.0) / 100.0));
alsa_.setInteger("RX1 Digital Volume", hw);
std::string cfg = getSxmoCfgDir();
::mkdir(cfg.c_str(), 0755);
writeIntFile(cfg + "/earpiece_vol", it.value);
long hw = (it.value <= 0) ? 0 : (60 + std::lround((it.value * 32.0) / 100.0));
if (readActiveOutputDevice() == "Earpiece") {
writeIntFile("/tmp/sxmo_out_muted", (it.value <= 0) ? 1 : 0);
alsa_.setEnum("EAR_S", (it.value > 0) ? 1 : 0);
if (it.value > 0) {
alsa_.setEnum("RX1 MIX1 INP1", 3);
alsa_.setEnum("RDAC2 MUX", 0);
}
alsa_.setInteger("RX1 Digital Volume", hw);
}
char cmd[160];
std::snprintf(cmd, sizeof(cmd),
"sxmo_audio.sh vol set %d Earpiece >/dev/null 2>&1",
it.value);
runAsyncCmd(cmd);
break;
}
case ControlId::RingVol: {
@@ -562,24 +657,55 @@ class PhoneController {
break;
}
case ControlId::SoundMute: {
alsa_.setBoolean("SPK DAC Switch", it.value != 0);
if (it.value != 0) {
alsa_.setEnum("RX3 MIX1 INP1", 3);
if (it.value == 0) {
writeIntFile("/tmp/sxmo_out_muted", 1);
it.iconOn = kIconSoundOff;
runAsyncCmd("sxmo_audio.sh vol mute >/dev/null 2>&1");
} else {
writeIntFile("/tmp/sxmo_out_muted", 0);
const char* targetDev = (it.value == 2) ? "Earpiece"
: (it.value == 3) ? "Headphones"
: "Speaker";
it.iconOn = (it.value == 3) ? kIconHeadphones
: (it.value == 2) ? kIconEarpiece
: kIconSoundOn;
{
std::ofstream ofs("/tmp/sxmo_audio_out_device", std::ios::trunc);
if (ofs) ofs << targetDev << "\n";
}
if (!items_.empty() && items_[0].id == ControlId::SpeakerVol) {
if (it.value == 3) {
int hpPct = readIntFile(getSxmoCfgDir() + "/headphones_vol", 80);
items_[0].label = "Headphones";
items_[0].iconOn = kIconHeadphones;
items_[0].value = std::clamp(((hpPct + 2) / 5) * 5, 0, 100);
} else {
int spkPct = readIntFile(getSxmoCfgDir() + "/speaker_vol", 85);
items_[0].label = "Speaker";
items_[0].iconOn = kIconSpeaker;
items_[0].value = std::clamp(((spkPct + 2) / 5) * 5, 0, 100);
}
}
char cmd[128];
std::snprintf(cmd, sizeof(cmd),
"sxmo_audio.sh vol unmute >/dev/null 2>&1; "
"sxmo_audio.sh device set %s >/dev/null 2>&1",
targetDev);
runAsyncCmd(cmd);
}
runAsyncCmd(it.value
? "wpctl set-mute @DEFAULT_AUDIO_SINK@ 0 >/dev/null 2>&1; "
"sxmo_hook_statusbar.sh audio >/dev/null 2>&1"
: "wpctl set-mute @DEFAULT_AUDIO_SINK@ 1 >/dev/null 2>&1; "
"sxmo_hook_statusbar.sh audio >/dev/null 2>&1");
break;
}
case ControlId::MicMute: {
writeIntFile("/tmp/sxmo_mic_muted", it.value ? 0 : 1);
alsa_.setEnum("DEC1 MUX", it.value ? (savedMicMux_ > 0 ? savedMicMux_ : 1) : 0);
alsa_.setInteger("ADC1 Volume", it.value ? 6 : 0);
alsa_.setInteger("ADC3 Volume", it.value ? 6 : 0);
alsa_.setInteger("ADC1 Volume", it.value ? 7 : 0);
alsa_.setInteger("ADC2 Volume", it.value ? 7 : 0);
alsa_.setInteger("TX1 Digital Volume", it.value ? 84 : 0);
runAsyncCmd(it.value
? "wpctl set-mute @DEFAULT_AUDIO_SOURCE@ 0 >/dev/null 2>&1"
: "wpctl set-mute @DEFAULT_AUDIO_SOURCE@ 1 >/dev/null 2>&1");
? "wpctl set-mute @DEFAULT_AUDIO_SOURCE@ 0 >/dev/null 2>&1; "
"sxmo_hook_statusbar.sh volume >/dev/null 2>&1"
: "wpctl set-mute @DEFAULT_AUDIO_SOURCE@ 1 >/dev/null 2>&1; "
"sxmo_hook_statusbar.sh volume >/dev/null 2>&1");
break;
}
case ControlId::RingerMode: {
@@ -820,6 +946,12 @@ static int xdrawer_run_main(int argc, char** argv) {
XftColor* rCol = (it.value == 0) ? (isSel ? &colYellow : &colGreen)
: ((it.value == 1) ? &colYellow : &colMuted);
drawPango(layoutSmall, rCol, kWinWidth - stW - 6, ry, rowH, stStr);
} else if (it.id == ControlId::SoundMute) {
const char* oModes[4] = {"Muted", "Speaker", "Earpiece", "Headset"};
std::string stStr = std::string("< ") + oModes[std::clamp(it.value, 0, 3)] + " >";
int stW = pangoWidth(layoutSmall, stStr);
XftColor* oCol = (it.value > 0) ? (isSel ? &colYellow : &colGreen) : &colMuted;
drawPango(layoutSmall, oCol, kWinWidth - stW - 6, ry, rowH, stStr);
} else if (it.id == ControlId::VibrateStrength) {
const char* vModes[3] = {"Light", "Medium", "Strong"};
int vIdx = std::clamp(it.value, 1, 3) - 1;