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) {
+5 -11
View File
@@ -824,7 +824,7 @@ class PcmAudioOut {
if (sysMuted_) {
volScale_ = 0;
} else {
volScale_ = std::max(1, (pct * pct * 20 + 5000) / 10000);
volScale_ = std::max(1, (pct * 20 + 50) / 100);
}
}
@@ -840,16 +840,10 @@ class PcmAudioOut {
std::ofstream ofs(cfgDir + "/speaker_vol", std::ios::trunc);
if (ofs) ofs << nextPct << "\n";
}
snd_ctl_t* ctl = nullptr;
if (snd_ctl_open(&ctl, "hw:0", 0) == 0 && ctl) {
long targetRx3 =
(nextPct <= 0) ? 0 : (56 + ((nextPct * 28 + 50) / 100));
if (nextPct > 0) {
writeCtlBool(ctl, "SPK DAC Switch", true);
}
writeCtlInt(ctl, "RX3 Digital Volume", targetRx3);
snd_ctl_close(ctl);
}
char cmd[128];
std::snprintf(cmd, sizeof(cmd),
"sxmo_audio.sh vol set %d >/dev/null 2>&1 &", nextPct);
(void)std::system(cmd);
refreshVolume(false);
}
+15 -29
View File
@@ -57,6 +57,15 @@ class AlsaSoundEngine {
return sysMuted_.load(std::memory_order_relaxed);
}
static int readSimpleInt(const char* path, int fallback) {
FILE* f = std::fopen(path, "r");
if (!f) return fallback;
int v = fallback;
(void)std::fscanf(f, "%d", &v);
std::fclose(f);
return v;
}
static std::string sxmoConfigDir() {
const char* xdg = std::getenv("XDG_CONFIG_HOME");
if (xdg && *xdg) return std::string(xdg) + "/sxmo";
@@ -121,20 +130,13 @@ class AlsaSoundEngine {
}
}
volumePct.store(pct, std::memory_order_relaxed);
bool muted = !spkOn || pct <= 0;
bool muted = (readSimpleInt("/tmp/sxmo_out_muted", 0) == 1) || pct <= 0;
sysMuted_.store(muted, std::memory_order_relaxed);
// PipeWire clients get the sink volume applied as a cubic curve on top of
// the RX3 hardware attenuation; ReMEdy writes to hw:0,0 directly, so it
// applies the same cubic software gain to sound exactly as loud as every
// other application at the same system volume.
float lin = pct / 100.0f;
sysGain_.store(muted ? 0.0f : lin * lin * lin, std::memory_order_relaxed);
sysGain_.store(muted ? 0.0f : lin, std::memory_order_relaxed);
}
// Change the system-wide speaker volume exactly like the xdrawer "Speaker"
// slider does: persist speaker_vol, program the WCD9302 RX3 digital gain
// (60..92), unmute the speaker DAC, and mirror the level into the PipeWire
// default sink so every application follows the same setting.
// Change the system-wide output volume linearly via sxmo_audio.sh.
void setSystemVolume(int pct) {
pct = std::clamp(pct, 0, 100);
std::string dir = sxmoConfigDir();
@@ -146,22 +148,9 @@ class AlsaSoundEngine {
std::fclose(f);
}
}
snd_ctl_t* ctl = nullptr;
if (snd_ctl_open(&ctl, "hw:0", 0) == 0 && ctl) {
long hw = (pct <= 0) ? 0 : (60 + std::lround((pct * 32.0) / 100.0));
if (pct > 0) {
writeCtlBool(ctl, "SPK DAC Switch", true);
writeCtlEnum(ctl, "RX3 MIX1 INP1", 3);
}
writeCtlInt(ctl, "RX3 Digital Volume", hw);
snd_ctl_close(ctl);
}
char cmd[200];
char cmd[128];
std::snprintf(cmd, sizeof(cmd),
"(wpctl set-mute @DEFAULT_AUDIO_SINK@ %d; "
"wpctl set-volume @DEFAULT_AUDIO_SINK@ %d%%; "
"sxmo_hook_statusbar.sh volume) >/dev/null 2>&1 &",
(pct <= 0) ? 1 : 0, pct);
"sxmo_audio.sh vol set %d >/dev/null 2>&1 &", pct);
std::thread([c = std::string(cmd)]() { (void)std::system(c.c_str()); })
.detach();
refreshSystemVolume(true);
@@ -173,10 +162,7 @@ class AlsaSoundEngine {
static float perceptualGain(int levelPct) {
if (levelPct <= 0) return 0.0f;
if (levelPct >= 100) return 1.0f;
// MIDP VolumeControl levels are meant to be linear in perceived loudness.
// Loudness grows roughly with amplitude^0.6, so map level -> amplitude
// with an exponent of ~1.5 (level 50 -> ~35%, 25 -> ~13%, 80 -> ~72%).
return std::pow(std::clamp(levelPct, 0, 100) / 100.0f, 1.5f);
return std::clamp(levelPct, 0, 100) / 100.0f;
}
// 4096-entry sine table shared by the synth voices (avoids libm sin()
+17 -4
View File
@@ -2549,11 +2549,24 @@ open_builtin_menu(const char *menu_name, int keep_idx)
snprintf(label, sizeof(label), "%s Do Not Disturb %s", ICON_ZZZ, (access(pbuf, F_OK) == 0) ? ICON_TON : ICON_TOF);
add_menu_item(label, "@toggle:dnd", 1);
snprintf(label, sizeof(label), "%s Route to Speaker", ICON_MUS);
add_menu_item(label, "amixer -c 0 cset name='SPK DAC Switch' on >/dev/null 2>&1; sxmo_hook_statusbar.sh audio", 1);
char out_dev[64] = "Speaker";
if (!read_first_line("/tmp/sxmo_audio_out_device", out_dev, sizeof(out_dev))) {
snprintf(pbuf, sizeof(pbuf), "%s/.config/sxmo/audio_out_device", home);
read_first_line(pbuf, out_dev, sizeof(out_dev));
}
int is_ear = (strstr(out_dev, "Earpiece") != NULL || strstr(out_dev, "earpiece") != NULL);
int is_hp = (strstr(out_dev, "Headphone") != NULL || strstr(out_dev, "headphone") != NULL ||
strstr(out_dev, "Headset") != NULL || strstr(out_dev, "headset") != NULL);
int is_spk = (!is_ear && !is_hp);
snprintf(label, sizeof(label), "%s Route to Earpiece", ICON_PHN);
add_menu_item(label, "amixer -c 0 cset name='SPK DAC Switch' off >/dev/null 2>&1; sxmo_hook_statusbar.sh audio", 1);
snprintf(label, sizeof(label), "%s Route to Speaker %s", ICON_MUS, is_spk ? ICON_TON : ICON_TOF);
add_menu_item(label, "printf 'Speaker\\n' > /tmp/sxmo_audio_out_device; sxmo_audio.sh device set Speaker >/dev/null 2>&1", 1);
snprintf(label, sizeof(label), "%s Route to Earpiece %s", ICON_PHN, is_ear ? ICON_TON : ICON_TOF);
add_menu_item(label, "printf 'Earpiece\\n' > /tmp/sxmo_audio_out_device; sxmo_audio.sh device set Earpiece >/dev/null 2>&1", 1);
snprintf(label, sizeof(label), "%s Route to Headphones %s", ICON_MUS, is_hp ? ICON_TON : ICON_TOF);
add_menu_item(label, "printf 'Headphones\\n' > /tmp/sxmo_audio_out_device; sxmo_audio.sh device set Headphones >/dev/null 2>&1", 1);
snprintf(label, sizeof(label), "%s System Menu", ICON_MNU);
add_menu_item(label, "@menu:sys", 1);
+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;