Fix mobile data static IP bearer, call recording, earpiece audio, and ringtone/vibration

This commit is contained in:
Luxferre committed 2026-10-25 20:23:17 +02:00
1 parent 21dfb187be
commit a8a28fa685
23 files changed
+624 -212

No files matched your search

+157 -57
View File
@@ -192,10 +192,12 @@ std::string sanitizeForFilename(const std::string& raw) {
}
}
if (out.empty()) {
return "unknown";
return "voice";
}
if (out.size() > 20) {
out = out.substr(0, 20);
if (out.size() > 10 && out[0] == '+') {
out = out.substr(out.size() - 10);
} else if (out.size() > 10) {
out = out.substr(0, 10);
}
return out;
}
@@ -217,7 +219,7 @@ void finalizeWavHeader(const std::string& path) {
return;
}
uint8_t hdr[64] = {0};
ssize_t n = :: pread(fd, hdr, sizeof(hdr), 0);
ssize_t n = ::pread(fd, hdr, sizeof(hdr), 0);
if (n < 44 || std::memcmp(hdr, "RIFF", 4) != 0 ||
std::memcmp(hdr + 8, "WAVE", 4) != 0) {
::close(fd);
@@ -237,6 +239,12 @@ void finalizeWavHeader(const std::string& path) {
};
(void)::pwrite(fd, b, 4, 4);
}
ssize_t dataOff = -1;
uint32_t dataBytes = 0;
uint16_t audioFmt = static_cast<uint16_t>(hdr[20]) |
(static_cast<uint16_t>(hdr[21]) << 8);
uint16_t bitsPerSample = static_cast<uint16_t>(hdr[34]) |
(static_cast<uint16_t>(hdr[35]) << 8);
for (ssize_t off = 12; off + 8 <= n;) {
uint32_t chunkLen = static_cast<uint32_t>(hdr[off + 4]) |
(static_cast<uint32_t>(hdr[off + 5]) << 8) |
@@ -244,6 +252,8 @@ void finalizeWavHeader(const std::string& path) {
(static_cast<uint32_t>(hdr[off + 7]) << 24);
if (std::memcmp(hdr + off, "data", 4) == 0) {
uint32_t expectedData = static_cast<uint32_t>(st.st_size - (off + 8));
dataOff = off + 8;
dataBytes = expectedData;
if (chunkLen == 0 || chunkLen != expectedData) {
uint8_t b[4] = {
static_cast<uint8_t>(expectedData & 0xff),
@@ -258,6 +268,33 @@ void finalizeWavHeader(const std::string& path) {
if (chunkLen == 0) break;
off += 8 + static_cast<ssize_t>((chunkLen + 1) & ~1u);
}
// Peak-normalize quiet 16-bit signed PCM recordings so playback is clearly audible
if (audioFmt == 1 && bitsPerSample == 16 && dataOff >= 44 &&
dataBytes >= 2 && dataBytes <= 32 * 1024 * 1024) {
size_t numSamples = dataBytes / 2;
std::vector<int16_t> pcm(numSamples);
ssize_t rd = ::pread(fd, pcm.data(), numSamples * sizeof(int16_t), dataOff);
if (rd == static_cast<ssize_t>(numSamples * sizeof(int16_t))) {
int maxAbs = 0;
size_t skip = std::min<size_t>(numSamples / 4, 400);
for (size_t i = skip; i < numSamples; ++i) {
int v = std::abs(static_cast<int>(pcm[i]));
if (v > maxAbs) {
maxAbs = v;
}
}
if (maxAbs >= 32 && maxAbs < 18000) {
double gain = std::min(24.0, 24000.0 / static_cast<double>(maxAbs));
if (gain > 1.15) {
for (size_t i = 0; i < numSamples; ++i) {
int scaled = static_cast<int>(std::lround(pcm[i] * gain));
pcm[i] = static_cast<int16_t>(std::clamp(scaled, -32767, 32767));
}
(void)::pwrite(fd, pcm.data(), numSamples * sizeof(int16_t), dataOff);
}
}
}
}
::fsync(fd);
::close(fd);
}
@@ -434,7 +471,7 @@ int callscreen_run_main(int argc, char** argv) {
bool ringMuted = false;
bool micMuted = false;
bool speakerOn = false;
int callVolumePct = 80;
int callVolumePct = 85;
int selectedRow = 0;
int64_t callStartMs = 0;
@@ -446,6 +483,8 @@ int callscreen_run_main(int argc, char** argv) {
int64_t recordStartMs = 0;
std::string recordFilePath;
std::string recordFileName;
int lastRecordedSec = 0;
int recordedClipCount = 0;
bool visible = false;
int64_t openStartMs = 0;
@@ -454,6 +493,22 @@ int callscreen_run_main(int argc, char** argv) {
int softkeyPending = 0;
int64_t softkeyDeadlineMs = 0;
auto pctToHwVol = [](int pct, bool spk) -> 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);
};
auto hwVolToPct = [](long hwVol, bool spk) -> int {
if (hwVol < 68) {
return 85;
}
int span = spk ? 26 : 24;
int pct = static_cast<int>(std::lround(((hwVol - 68) * 100.0) / span));
pct = std::clamp(pct, 5, 100);
return std::max(5, ((pct + 2) / 5) * 5);
};
auto refreshHardwareAudioState = [&]() {
unsigned int dec1 = alsa.getEnum("DEC1 MUX", 1);
if (dec1 > 0) {
@@ -464,27 +519,18 @@ int callscreen_run_main(int argc, char** argv) {
savedMicMux = static_cast<unsigned int>(std::max(1, readIntFile("/tmp/xdrawer_mic_mux", 1)));
micMuted = true;
}
bool spkSwitch = alsa.getBoolean("SPK DAC Switch", false);
bool earSwitch = (alsa.getEnum("EAR_S", 1) != 0);
speakerOn = (spkSwitch && !earSwitch);
long rawVol = speakerOn ? alsa.getInteger("RX3 Digital Volume", 68)
: alsa.getInteger("RX1 Digital Volume", 68);
if (rawVol <= 0) {
rawVol = alsa.getInteger("RX1 Digital Volume", 68);
}
callVolumePct = std::clamp(static_cast<int>(std::lround((rawVol * 100.0) / 84.0)), 0, 100);
callVolumePct = (callVolumePct / 5) * 5;
if (callVolumePct == 0) {
callVolumePct = 75;
int savedVol = readIntFile("/tmp/sxmo_call_vol", -1);
if (savedVol >= 5 && savedVol <= 100) {
callVolumePct = savedVol;
}
};
auto setMicMuted = [&](bool mute) {
micMuted = mute;
alsa.setEnum("DEC1 MUX", mute ? 0 : (savedMicMux > 0 ? savedMicMux : 1));
alsa.setInteger("ADC1 Volume", mute ? 0 : 6);
alsa.setInteger("ADC3 Volume", mute ? 0 : 6);
alsa.setInteger("ADC1 Volume", mute ? 0 : 8);
alsa.setInteger("ADC3 Volume", mute ? 0 : 8);
alsa.setInteger("TX1 Digital Volume", mute ? 0 : 88);
alsa.setBoolean("VoiceMMode1 Capture Mixer TERT_MI2S_TX", !mute);
runAsyncCmd(mute ? "wpctl set-mute @DEFAULT_AUDIO_SOURCE@ 1 >/dev/null 2>&1"
: "wpctl set-mute @DEFAULT_AUDIO_SOURCE@ 0 >/dev/null 2>&1");
@@ -494,7 +540,15 @@ int callscreen_run_main(int argc, char** argv) {
auto setSpeakerRoute = [&](bool toSpeaker) {
speakerOn = toSpeaker;
long hwVol = std::lround((callVolumePct * 84.0) / 100.0);
long hwVol = pctToHwVol(callVolumePct, toSpeaker);
alsa.setBoolean("PRI_MI2S_RX Voice Mixer VoiceMMode1", true);
alsa.setBoolean("VoiceMMode1 Capture Mixer TERT_MI2S_TX", !micMuted);
alsa.setEnum("CIC1 MUX", 0);
if (!micMuted) {
alsa.setEnum("DEC1 MUX", savedMicMux > 0 ? savedMicMux : 1);
alsa.setInteger("ADC1 Volume", 8);
alsa.setInteger("TX1 Digital Volume", 88);
}
if (toSpeaker) {
alsa.setEnum("EAR_S", 0);
alsa.setEnum("RX1 MIX1 INP1", 0);
@@ -505,9 +559,11 @@ int callscreen_run_main(int argc, char** argv) {
} else {
alsa.setBoolean("SPK DAC Switch", false);
alsa.setEnum("RX3 MIX1 INP1", 0);
alsa.setInteger("RX3 Digital Volume", 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);
statusNotice = "Audio routed to Earpiece";
}
@@ -516,10 +572,10 @@ int callscreen_run_main(int argc, char** argv) {
auto adjustVolume = [&](int deltaPct) {
callVolumePct = std::clamp(callVolumePct + deltaPct, 5, 100);
long hwVol = std::lround((callVolumePct * 84.0) / 100.0);
alsa.setInteger("RX1 Digital Volume", hwVol);
writeIntFile("/tmp/sxmo_call_vol", callVolumePct);
alsa.setInteger("RX1 Digital Volume", pctToHwVol(callVolumePct, false));
if (speakerOn) {
alsa.setInteger("RX3 Digital Volume", hwVol);
alsa.setInteger("RX3 Digital Volume", pctToHwVol(callVolumePct, true));
}
char cmd[128];
std::snprintf(cmd, sizeof(cmd),
@@ -528,6 +584,24 @@ int callscreen_run_main(int argc, char** argv) {
runAsyncCmd(cmd);
};
auto finishRecordingFile = [&]() {
alsa.setBoolean("MultiMedia2 Mixer TERT_MI2S_TX", false);
finalizeWavHeader(recordFilePath);
struct stat st {};
if (!recordFilePath.empty() && ::stat(recordFilePath.c_str(), &st) == 0 &&
st.st_size > 44) {
int durFromBytes = static_cast<int>((st.st_size - 44 + 8000) / 16000);
int durFromWall = static_cast<int>((nowMs() - recordStartMs + 500) / 1000);
lastRecordedSec = std::max(1, std::max(durFromBytes, durFromWall));
++recordedClipCount;
statusNotice = "SAVED (" + formatTimerSec(lastRecordedSec) + ") " + recordFileName;
statusNoticeUntilMs = nowMs() + 8000;
} else if (!recordFileName.empty()) {
statusNotice = "Recording failed";
statusNoticeUntilMs = nowMs() + 4000;
}
};
auto checkRecordingAlive = [&]() -> bool {
if (recordPid <= 0) {
return false;
@@ -536,7 +610,7 @@ int callscreen_run_main(int argc, char** argv) {
pid_t r = ::waitpid(recordPid, &status, WNOHANG);
if (r == recordPid || (r < 0 && ::kill(recordPid, 0) != 0)) {
recordPid = -1;
finalizeWavHeader(recordFilePath);
finishRecordingFile();
return false;
}
return true;
@@ -565,11 +639,7 @@ int callscreen_run_main(int argc, char** argv) {
}
recordPid = -1;
}
finalizeWavHeader(recordFilePath);
if (!recordFileName.empty()) {
statusNotice = "Saved: " + recordFileName;
statusNoticeUntilMs = nowMs() + 3500;
}
finishRecordingFile();
};
auto startRecording = [&]() -> bool {
@@ -581,18 +651,19 @@ int callscreen_run_main(int argc, char** argv) {
struct tm tmNow {};
localtime_r(&nowSec, &tmNow);
char tsBuf[32];
std::strftime(tsBuf, sizeof(tsBuf), "%Y%m%d_%H%M%S", &tmNow);
std::strftime(tsBuf, sizeof(tsBuf), "%H%M%S", &tmNow);
std::string tag = sanitizeForFilename(
!contactName.empty() ? contactName : (!callNumber.empty() ? callNumber : "voice"));
recordFileName = "call_" + tag + "_" + tsBuf + ".wav";
recordFileName = std::string("rec_") + tsBuf + "_" + tag + ".wav";
recordFilePath = dir + "/" + recordFileName;
// Ensure MultiMedia2 capture mixer and primary mic path are active for PipeWire recording
// Enable MultiMedia2 capture mixer at 8 kHz mono to match active VoiceMMode1 AFE port
alsa.setEnum("CIC1 MUX", 0);
if (!micMuted) {
alsa.setEnum("DEC1 MUX", savedMicMux > 0 ? savedMicMux : 1);
alsa.setInteger("ADC1 Volume", 6);
alsa.setInteger("ADC1 Volume", 8);
alsa.setInteger("TX1 Digital Volume", 88);
}
alsa.setBoolean("MultiMedia2 Mixer TERT_MI2S_TX", true);
@@ -602,12 +673,6 @@ int callscreen_run_main(int argc, char** argv) {
}
if (pid == 0) {
::setsid();
std::string runtimeDir = "/run/user/" + std::to_string(::getuid());
const char* xdg = std::getenv("XDG_RUNTIME_DIR");
struct stat stRuntime {};
if (!xdg || !*xdg || ::stat(xdg, &stRuntime) != 0) {
::setenv("XDG_RUNTIME_DIR", runtimeDir.c_str(), 1);
}
int nullfd = ::open("/dev/null", O_RDWR);
if (nullfd >= 0) {
::dup2(nullfd, STDIN_FILENO);
@@ -617,19 +682,17 @@ int callscreen_run_main(int argc, char** argv) {
::close(nullfd);
}
}
execlp("pw-record", "pw-record", "--format", "s16", "--rate", "16000",
"--channels", "1", recordFilePath.c_str(),
execlp("arecord", "arecord", "-q", "-D", "hw:0,1", "-f", "S16_LE",
"-r", "8000", "-c", "1", recordFilePath.c_str(),
static_cast<char*>(nullptr));
execlp("parecord", "parecord", "--rate=16000", "--channels=1",
"--file-format=wav", recordFilePath.c_str(), static_cast<char*>(nullptr));
execlp("arecord", "arecord", "-f", "S16_LE", "-r", "16000", "-c", "1",
execlp("arecord", "arecord", "-q", "-f", "S16_LE", "-r", "8000", "-c", "1",
recordFilePath.c_str(), static_cast<char*>(nullptr));
_exit(1);
}
recordPid = pid;
recordStartMs = nowMs();
statusNotice = "Recording to " + recordFileName;
statusNoticeUntilMs = nowMs() + 2500;
statusNotice = "REC -> " + recordFileName;
statusNoticeUntilMs = nowMs() + 3000;
return true;
};
@@ -804,9 +867,17 @@ int callscreen_run_main(int argc, char** argv) {
drawPango(layoutSmall, &colLight, 8, 88, 26,
"[Call] Answer [*] Mute [End] Reject", 224);
} else if (callState == CallState::Ended) {
drawPango(layoutSmall, &colMuted, 8, 88, 26,
recordFileName.empty() ? "Call finished" : ("Saved: " + recordFileName),
224);
if (recordedClipCount > 0 && !recordFileName.empty()) {
std::string savedStr = "SAVED (" + formatTimerSec(lastRecordedSec) + ") " +
recordFileName;
drawPango(layoutSmall, &colGreen, 8, 88, 26, savedStr, 224);
} else {
drawPango(layoutSmall, &colMuted, 8, 88, 26, "Call finished", 224);
}
} else if (recordedClipCount > 0 && !recordFileName.empty()) {
std::string savedStr = "SAVED (" + formatTimerSec(lastRecordedSec) + ") " +
recordFileName;
drawPango(layoutSmall, &colGreen, 8, 88, 26, savedStr, 224);
} else {
drawPango(layoutSmall, &colMuted, 8, 88, 26,
"[1] Mic [2] Rec [3] Route [0] DTMF", 224);
@@ -883,16 +954,21 @@ int callscreen_run_main(int argc, char** argv) {
"[1] Microphone", std::max(80, kWinWidth - vw - 48));
drawPango(layoutMain, icCol, kWinWidth - vw - 8, ry, rowH, valStr);
} else if (i == 1) {
XftColor* icCol = isRec ? &colRed : &colMuted;
std::string valStr = isRec
? ("REC " + formatTimerSec(static_cast<int>((now - recordStartMs) / 1000)))
: "OFF";
XftColor* icCol = isRec ? &colRed
: (recordedClipCount > 0 ? &colGreen : &colMuted);
std::string valStr;
if (isRec) {
valStr = "REC " + formatTimerSec(static_cast<int>((now - recordStartMs) / 1000));
} else if (recordedClipCount > 0) {
valStr = "SAVED " + formatTimerSec(lastRecordedSec);
} else {
valStr = "OFF";
}
int vw = pangoWidth(layoutMain, valStr);
drawPango(layoutIcon, icCol, 10, ry, rowH, kIconRecord);
drawPango(layoutMain, isSel ? &colWhite : &colLight, 34, ry, rowH,
"[2] Record", std::max(80, kWinWidth - vw - 44));
drawPango(layoutMain, isRec ? &colRed : &colMuted,
kWinWidth - vw - 8, ry, rowH, valStr);
"[2] Record", std::max(72, kWinWidth - vw - 44));
drawPango(layoutMain, icCol, kWinWidth - vw - 8, ry, rowH, valStr);
} else if (i == 2) {
const char* ic = speakerOn ? kIconSpeaker : kIconEarpiece;
XftColor* icCol = speakerOn ? &colYellow : &colBlue;
@@ -1242,9 +1318,13 @@ int callscreen_run_main(int argc, char** argv) {
}
}
if (foundState == "active" && callState != CallState::Active) {
if (callState != CallState::Outgoing) {
speakerOn = false;
}
callState = CallState::Active;
callStartMs = nowMs();
ringMuted = false;
setSpeakerRoute(speakerOn);
writeActiveStateFiles();
render();
} else if (foundState == "ringing-in" && callState == CallState::Idle) {
@@ -1359,10 +1439,15 @@ int callscreen_run_main(int argc, char** argv) {
callId.clear();
callNumber.clear();
contactName.clear();
recordFileName.clear();
recordFilePath.clear();
lastRecordedSec = 0;
recordedClipCount = 0;
updateCallerDetails(arg1, arg2);
callState = CallState::Incoming;
callStartMs = nowMs();
ringMuted = false;
speakerOn = false;
dtmfMode = false;
dtmfSent.clear();
selectedRow = 0;
@@ -1372,24 +1457,39 @@ int callscreen_run_main(int argc, char** argv) {
callId.clear();
callNumber.clear();
contactName.clear();
recordFileName.clear();
recordFilePath.clear();
lastRecordedSec = 0;
recordedClipCount = 0;
updateCallerDetails(arg1, arg2);
callState = CallState::Outgoing;
callStartMs = nowMs();
ringMuted = false;
speakerOn = false;
dtmfMode = false;
dtmfSent.clear();
selectedRow = 0;
setSpeakerRoute(false);
statusNotice.clear();
statusNoticeUntilMs = 0;
writeActiveStateFiles();
showWindow();
} else if (verb == "ACTIVE") {
bool wasActive = (callState == CallState::Active);
bool wasOutgoing = (callState == CallState::Outgoing);
updateCallerDetails(arg1, arg2);
callState = CallState::Active;
if (!wasActive) {
callStartMs = nowMs();
if (!wasOutgoing) {
speakerOn = false;
}
}
ringMuted = false;
selectedRow = 0;
setSpeakerRoute(speakerOn);
statusNotice.clear();
statusNoticeUntilMs = 0;
writeActiveStateFiles();
showWindow();
} else if (verb == "OPEN") {