Add Bluetooth audio/HID/discoverability/alias support, fix SD card hotplug & caller ID matching, and expand ReMEdy J2ME JSRs + 64MB GC

This commit is contained in:
Luxferre committed 2026-11-01 16:18:04 +02:00
1 parent 456d3fbc42
commit 471916424e
51 files changed
+2961 -305

No files matched your search

+7 -6
View File
@@ -44,12 +44,13 @@ struct BtSendTarget {
std::vector<BtSendTarget> queryBtSendTargets(bool scanFirst = false) {
std::string rfk = runCaptureCmd("/usr/sbin/rfkill list bluetooth 2>/dev/null");
if (rfk.find("Soft blocked: yes") != std::string::npos ||
if (::access("/run/bluetoothd.pid", F_OK) != 0 ||
rfk.find("Soft blocked: yes") != std::string::npos ||
rfk.find("Hard blocked: yes") != std::string::npos) {
runCaptureCmd("SXMO_OS=alpine doas -n sxmo_bluetoothtoggle.sh on >/dev/null 2>&1");
}
if (scanFirst) {
runCaptureCmd("bluetoothctl --timeout 4 scan on >/dev/null 2>&1");
runCaptureCmd("timeout 5 bluetoothctl --timeout 4 scan on >/dev/null 2>&1");
}
std::unordered_set<std::string> pairedMacs;
std::unordered_set<std::string> connectedMacs;
@@ -62,14 +63,14 @@ std::vector<BtSendTarget> queryBtSendTargets(bool scanFirst = false) {
}
}
};
parseSet(runCaptureCmd("bluetoothctl devices Paired 2>/dev/null || "
"bluetoothctl paired-devices 2>/dev/null"),
parseSet(runCaptureCmd("timeout 2 bluetoothctl devices Paired 2>/dev/null || "
"timeout 2 bluetoothctl paired-devices 2>/dev/null"),
pairedMacs);
parseSet(runCaptureCmd("bluetoothctl devices Connected 2>/dev/null"),
parseSet(runCaptureCmd("timeout 2 bluetoothctl devices Connected 2>/dev/null"),
connectedMacs);
std::vector<BtSendTarget> out;
std::string allRaw = runCaptureCmd("bluetoothctl devices 2>/dev/null");
std::string allRaw = runCaptureCmd("timeout 2 bluetoothctl devices 2>/dev/null");
std::istringstream ss(allRaw);
std::string line;
while (std::getline(ss, line)) {
+532 -85
View File
@@ -38,6 +38,20 @@ enum class NetModal {
WifiPassword = 2,
ApnEditor = 3,
BtActions = 4,
BtNameEditor = 5,
};
constexpr const char* kIconHeadphones = "\xef\x80\xa5"; // U+F025 headphones
constexpr const char* kIconKeyboard = "\xef\x84\x9c"; // U+F11C keyboard
constexpr const char* kIconMouse = "\xef\x89\x85"; // U+F245 mouse pointer
enum class BtDevKind {
Generic = 0,
Audio = 1,
Gamepad = 2,
Keyboard = 3,
Mouse = 4,
Phone = 5,
};
enum class TextMode {
@@ -109,8 +123,76 @@ struct BtDeviceEntry {
std::string name;
bool paired = false;
bool connected = false;
BtDevKind kind = BtDevKind::Generic;
std::string profileLabel;
int batteryPct = -1;
};
struct BtControllerState {
std::string name = "Bananarchy";
bool discoverable = true;
int activeCount = 0;
std::string activeSummary;
};
std::string getSystemHostname() {
char buf[128] = {};
if (::gethostname(buf, sizeof(buf) - 1) == 0 && buf[0] != '\0') {
std::string h = trimStr(buf);
if (!h.empty()) return h;
}
std::ifstream in("/etc/hostname");
std::string line;
if (in.is_open() && std::getline(in, line)) {
line = trimStr(line);
if (!line.empty()) return line;
}
return "Bananarchy";
}
std::string readSavedBtName() {
std::string path = getHomeDirPath() + "/.config/sxmo/bt_name";
std::ifstream in(path);
std::string line;
if (in.is_open() && std::getline(in, line)) {
line = trimStr(line);
if (!line.empty()) return line;
}
return getSystemHostname();
}
void writeSavedBtName(const std::string& name) {
std::string dir = getHomeDirPath() + "/.config/sxmo";
::mkdir(dir.c_str(), 0755);
std::ofstream out(dir + "/bt_name", std::ios::trunc);
if (out.is_open()) {
out << name << "\n";
}
}
bool readSavedBtDiscoverable() {
std::string path = getHomeDirPath() + "/.config/sxmo/bt_discoverable";
std::ifstream in(path);
std::string line;
if (in.is_open() && std::getline(in, line)) {
std::string v = toLowerStr(trimStr(line));
if (v == "0" || v == "off" || v == "no" || v == "false") {
return false;
}
if (!v.empty()) return true;
}
return true;
}
void writeSavedBtDiscoverable(bool disc) {
std::string dir = getHomeDirPath() + "/.config/sxmo";
::mkdir(dir.c_str(), 0755);
std::ofstream out(dir + "/bt_discoverable", std::ios::trunc);
if (out.is_open()) {
out << (disc ? "1\n" : "0\n");
}
}
struct BtRxStatus {
std::string state = "IDLE";
std::string fileName;
@@ -162,6 +244,9 @@ bool isWifiEnabledFast() {
}
bool isBtEnabledFast() {
if (::access("/run/bluetoothd.pid", F_OK) != 0) {
return false;
}
std::string out = runCaptureCmd("/usr/sbin/rfkill list bluetooth 2>/dev/null");
if (out.find("Soft blocked: yes") != std::string::npos ||
out.find("Hard blocked: yes") != std::string::npos) {
@@ -324,6 +409,17 @@ CellularNetInfo queryCellularInfo() {
return info;
}
const char* iconForBtDevice(const BtDeviceEntry& d) {
switch (d.kind) {
case BtDevKind::Audio: return kIconHeadphones;
case BtDevKind::Gamepad: return kIconGamepad;
case BtDevKind::Keyboard: return kIconKeyboard;
case BtDevKind::Mouse: return kIconMouse;
case BtDevKind::Phone: return kIconPhone;
default: return kIconBluetooth;
}
}
std::vector<BtDeviceEntry> queryBtDevices() {
std::vector<BtDeviceEntry> out;
std::unordered_set<std::string> pairedMacs;
@@ -334,22 +430,27 @@ std::vector<BtDeviceEntry> queryBtDevices() {
std::istringstream ss(raw);
std::string line;
while (std::getline(ss, line)) {
// Format: "Device XX:XX:XX:XX:XX:XX Name..."
if (line.rfind("Device ", 0) == 0 && line.size() >= 24) {
dst.insert(line.substr(7, 17));
}
}
};
parseMacSet(runCaptureCmd("bluetoothctl devices Paired 2>/dev/null || "
"bluetoothctl paired-devices 2>/dev/null"),
parseMacSet(runCaptureCmd("timeout 2 bluetoothctl devices Paired 2>/dev/null || "
"timeout 2 bluetoothctl paired-devices 2>/dev/null"),
pairedMacs);
parseMacSet(runCaptureCmd("bluetoothctl devices Connected 2>/dev/null"),
parseMacSet(runCaptureCmd("timeout 2 bluetoothctl devices Connected 2>/dev/null"),
connectedMacs);
std::string allRaw = runCaptureCmd("bluetoothctl devices 2>/dev/null");
std::string paRaw = runCaptureCmd(
"pactl list sinks short 2>/dev/null; pactl list sources short 2>/dev/null");
std::string allRaw = runCaptureCmd("timeout 2 bluetoothctl devices 2>/dev/null");
std::istringstream ss(allRaw);
std::string line;
std::string infoCmd;
int infoCount = 0;
while (std::getline(ss, line)) {
if (line.rfind("Device ", 0) != 0 || line.size() < 24) continue;
BtDeviceEntry d;
@@ -358,6 +459,115 @@ std::vector<BtDeviceEntry> queryBtDevices() {
d.paired = (pairedMacs.count(d.mac) > 0);
d.connected = (connectedMacs.count(d.mac) > 0);
out.push_back(d);
if ((d.connected || d.paired) && infoCount < 12) {
infoCmd += "printf '===BTDEV %s===\\n' " + shellQuote(d.mac) +
"; timeout 1 bluetoothctl info " + shellQuote(d.mac) +
" 2>/dev/null; ";
++infoCount;
}
}
std::unordered_map<std::string, std::string> infoByMac;
if (!infoCmd.empty()) {
std::string batchInfo = runCaptureCmd(infoCmd);
std::istringstream iss(batchInfo);
std::string iline;
std::string curMac;
while (std::getline(iss, iline)) {
if (iline.rfind("===BTDEV ", 0) == 0 && iline.size() >= 26) {
curMac = iline.substr(9, 17);
} else if (!curMac.empty()) {
infoByMac[curMac] += iline + "\n";
}
}
}
for (auto& d : out) {
std::string macU = d.mac;
for (char& c : macU) {
if (c == ':') c = '_';
}
bool hasPaSink = (paRaw.find("bluez_output." + macU) != std::string::npos);
bool hasPaSrc = (paRaw.find("bluez_input." + macU) != std::string::npos);
std::string info = infoByMac[d.mac];
std::string infoLow = toLowerStr(info);
std::string nameLow = toLowerStr(d.name);
bool hasA2dp = (infoLow.find("0000110b-") != std::string::npos ||
infoLow.find("0000110a-") != std::string::npos ||
infoLow.find("audio sink") != std::string::npos ||
infoLow.find("audio source") != std::string::npos);
bool hasHfp = (infoLow.find("00001108-") != std::string::npos ||
infoLow.find("00001112-") != std::string::npos ||
infoLow.find("0000111e-") != std::string::npos ||
infoLow.find("0000111f-") != std::string::npos ||
infoLow.find("handsfree") != std::string::npos ||
infoLow.find("headset") != std::string::npos);
bool hasHid = (infoLow.find("00001124-") != std::string::npos ||
infoLow.find("00001812-") != std::string::npos ||
infoLow.find("human interface device") != std::string::npos);
if (infoLow.find("icon: input-gaming") != std::string::npos ||
infoLow.find("icon: input-joystick") != std::string::npos ||
nameLow.find("gamepad") != std::string::npos ||
nameLow.find("controller") != std::string::npos ||
nameLow.find("joy-con") != std::string::npos ||
nameLow.find("dualshock") != std::string::npos ||
nameLow.find("dualsense") != std::string::npos ||
nameLow.find("8bitdo") != std::string::npos ||
nameLow.find("xbox") != std::string::npos) {
d.kind = BtDevKind::Gamepad;
d.profileLabel = "HID Gamepad";
} else if (infoLow.find("icon: input-keyboard") != std::string::npos ||
nameLow.find("keyboard") != std::string::npos ||
nameLow.find("keychron") != std::string::npos) {
d.kind = BtDevKind::Keyboard;
d.profileLabel = "HID Keyboard";
} else if (infoLow.find("icon: input-mouse") != std::string::npos ||
infoLow.find("icon: input-tablet") != std::string::npos ||
nameLow.find("mouse") != std::string::npos ||
nameLow.find("trackpad") != std::string::npos) {
d.kind = BtDevKind::Mouse;
d.profileLabel = "HID Mouse";
} else if (hasPaSink || hasPaSrc || hasA2dp || hasHfp ||
infoLow.find("icon: audio-") != std::string::npos) {
d.kind = BtDevKind::Audio;
if (d.connected) {
if (hasPaSrc) {
d.profileLabel = "HSP/HFP";
} else if (hasPaSink) {
d.profileLabel = "A2DP";
} else if (hasA2dp && hasHfp) {
d.profileLabel = "A2DP+HFP";
} else if (hasHfp) {
d.profileLabel = "HSP/HFP";
} else {
d.profileLabel = "A2DP";
}
} else {
if (hasA2dp && hasHfp) d.profileLabel = "Audio A2DP/HFP";
else if (hasHfp) d.profileLabel = "Audio HSP/HFP";
else d.profileLabel = "Audio A2DP";
}
} else if (hasHid) {
d.kind = BtDevKind::Keyboard;
d.profileLabel = "HID Input";
} else if (infoLow.find("icon: phone") != std::string::npos ||
infoLow.find("icon: computer") != std::string::npos) {
d.kind = BtDevKind::Phone;
d.profileLabel = "OBEX/PAN";
}
size_t batPos = info.find("Battery Percentage:");
if (batPos != std::string::npos) {
size_t lp = info.find('(', batPos);
size_t rp = info.find(')', batPos);
if (lp != std::string::npos && rp != std::string::npos && rp > lp + 1) {
d.batteryPct = std::clamp(
std::atoi(info.substr(lp + 1, rp - lp - 1).c_str()), 0, 100);
}
}
}
std::sort(out.begin(), out.end(),
@@ -369,6 +579,48 @@ std::vector<BtDeviceEntry> queryBtDevices() {
return out;
}
BtControllerState queryBtController(bool btOn,
const std::vector<BtDeviceEntry>& devs) {
BtControllerState st;
st.name = readSavedBtName();
st.discoverable = readSavedBtDiscoverable();
if (btOn) {
std::string showOut = runCaptureCmd("timeout 2 bluetoothctl show 2>/dev/null");
std::istringstream ss(showOut);
std::string line;
while (std::getline(ss, line)) {
std::string t = trimStr(line);
if (t.rfind("Discoverable:", 0) == 0) {
std::string v = toLowerStr(trimStr(t.substr(13)));
if (v == "yes") st.discoverable = true;
else if (v == "no") st.discoverable = false;
} else if (t.rfind("Alias:", 0) == 0) {
std::string al = trimStr(t.substr(6));
if (!al.empty() && al.rfind("BlueZ", 0) != 0 &&
::access((getHomeDirPath() + "/.config/sxmo/bt_name").c_str(), F_OK) != 0) {
st.name = al;
}
}
}
}
for (const auto& d : devs) {
if (!d.connected) continue;
++st.activeCount;
std::string item = d.name;
if (!d.profileLabel.empty()) {
item += " (" + d.profileLabel + ")";
}
if (st.activeSummary.empty()) {
st.activeSummary = item;
} else {
st.activeSummary += ", " + item;
}
}
return st;
}
int runNetworksApp(int argc, char** argv) {
gRunning = 1;
std::setlocale(LC_ALL, "");
@@ -448,8 +700,10 @@ int runNetworksApp(int argc, char** argv) {
bool btOn = false;
std::vector<BtDeviceEntry> btList;
BtControllerState btCtrl;
BtRxStatus btRx;
int btRowSel = 0; // 0 = BT Power Toggle, 1 = Scan Devices, 2 = File Reception Widget, 3..N = BT devices
std::string btNameEdit;
int btRowSel = 0; // 0=Power, 1=Discoverable, 2=Device Name, 3=Scan, 4=OBEX Rx, 5..N=Devices
int actionSel = 0;
@@ -503,16 +757,19 @@ int runNetworksApp(int argc, char** argv) {
} else if (activeTab == NetTab::Bluetooth) {
btOn = isBtEnabledFast();
if (btOn) {
runAsyncCmd("/usr/bin/bananarchy-bt-obex start >/dev/null 2>&1");
if (::access("/tmp/bananarchy_bt_obex.pid", F_OK) != 0) {
runAsyncCmd("/usr/bin/bananarchy-bt-obex start >/dev/null 2>&1");
}
btRx = readBtRxStatus();
if (forceScan) {
runCaptureCmd("bluetoothctl --timeout 5 scan on >/dev/null 2>&1");
runCaptureCmd("timeout 6 bluetoothctl --timeout 5 scan on >/dev/null 2>&1");
}
btList = queryBtDevices();
} else {
btList.clear();
}
int maxRow = btOn ? (2 + static_cast<int>(btList.size())) : 1;
btCtrl = queryBtController(btOn, btList);
int maxRow = btOn ? (4 + static_cast<int>(btList.size())) : 2;
if (btRowSel > maxRow) btRowSel = maxRow;
}
};
@@ -729,96 +986,146 @@ int runNetworksApp(int argc, char** argv) {
}
drawSoftkeyBar("Refresh", "Select", "Close");
} else if (activeTab == NetTab::Bluetooth) {
// Row 0: BT Power Toggle (height 26)
// Row 0: BT Power Toggle + Active Connection Badge (height 22)
bool sel0 = (btRowSel == 0);
XSetForeground(dpy, gc, sel0 ? pt.spec.selBg : pt.spec.bgAltEven);
XFillRectangle(dpy, pix, gc, 4, contentY, kWinWidth - 8, 26);
drawPango(layoutIcon, btOn ? &colBlue : &colMuted, 10, contentY, 26, kIconBluetooth);
drawPango(layoutMain, sel0 ? &colWhite : &colLight,
30, contentY, 26, std::string("Bluetooth: ") + (btOn ? "ON" : "OFF"));
XFillRectangle(dpy, pix, gc, 4, contentY, kWinWidth - 8, 22);
drawPango(layoutIcon, btOn ? &colBlue : &colMuted, 10, contentY, 22, kIconBluetooth);
std::string btPowerLbl = std::string("Bluetooth: ") + (btOn ? "ON" : "OFF");
if (btOn) {
btPowerLbl += (btCtrl.activeCount > 0)
? (" (" + std::to_string(btCtrl.activeCount) + " Active)")
: " (Idle)";
}
drawPango(layoutSmall, sel0 ? &colWhite : (btCtrl.activeCount > 0 ? &colGreen : &colLight),
28, contentY, 22, btPowerLbl, kWinWidth - 58);
drawPango(layoutIcon, btOn ? &colGreen : &colMuted,
kWinWidth - 28, contentY, 26, btOn ? kIconToggleOn : kIconToggleOff);
kWinWidth - 26, contentY, 22, btOn ? kIconToggleOn : kIconToggleOff);
// Row 1: Scan for Devices (height 24)
// Row 1: Discoverability Toggle (height 22)
bool sel1 = (btRowSel == 1);
int r1Y = contentY + 23;
XSetForeground(dpy, gc, sel1 ? pt.spec.selBg : pt.spec.bgAltOdd);
XFillRectangle(dpy, pix, gc, 4, contentY + 28, kWinWidth - 8, 24);
drawPango(layoutIcon, &colYellow, 10, contentY + 28, 24, kIconSearch);
XFillRectangle(dpy, pix, gc, 4, r1Y, kWinWidth - 8, 22);
drawPango(layoutIcon, btCtrl.discoverable ? &colGreen : &colMuted,
10, r1Y, 22, kIconEye);
drawPango(layoutSmall, sel1 ? &colWhite : &colLight,
30, contentY + 28, 24, "Scan for Bluetooth Devices");
28, r1Y, 22,
std::string("Discoverable: ") +
(btCtrl.discoverable ? "ON (Visible)" : "OFF (Hidden)"),
kWinWidth - 58);
drawPango(layoutIcon, btCtrl.discoverable ? &colGreen : &colMuted,
kWinWidth - 26, r1Y, 22,
btCtrl.discoverable ? kIconToggleOn : kIconToggleOff);
// Row 2: Bluetooth Device Name (height 22)
bool sel2 = (btRowSel == 2);
int r2Y = contentY + 46;
XSetForeground(dpy, gc, sel2 ? pt.spec.selBg : pt.spec.bgAltEven);
XFillRectangle(dpy, pix, gc, 4, r2Y, kWinWidth - 8, 22);
drawPango(layoutIcon, &colYellow, 10, r2Y, 22, kIconEdit);
drawPango(layoutSmall, sel2 ? &colWhite : &colLight,
28, r2Y, 22, "Name: " + btCtrl.name, kWinWidth - 66);
drawPango(layoutSmall, sel2 ? &colYellow : &colMuted,
kWinWidth - 38, r2Y, 22, "[Edit]");
if (!btOn) {
drawPango(layoutMain, &colMuted, 18, contentY + 96, 24,
drawPango(layoutMain, &colMuted, 18, contentY + 102, 24,
"Bluetooth is turned off.");
drawPango(layoutSmall, &colMuted, 18, contentY + 128, 20,
"Visibility & Name apply on start.");
} else {
// Row 2: Bluetooth File Reception Widget (height 36)
bool sel2 = (btRowSel == 2);
int rxY = contentY + 54;
XSetForeground(dpy, gc, sel2 ? pt.spec.selBg : pt.spec.bgHeader);
XFillRectangle(dpy, pix, gc, 4, rxY, kWinWidth - 8, 36);
// Row 3: Scan for Devices + Active Summary (height 22)
bool sel3 = (btRowSel == 3);
int r3Y = contentY + 69;
XSetForeground(dpy, gc, sel3 ? pt.spec.selBg : pt.spec.bgAltOdd);
XFillRectangle(dpy, pix, gc, 4, r3Y, kWinWidth - 8, 22);
drawPango(layoutIcon, &colYellow, 10, r3Y, 22, kIconSearch);
std::string scanLbl = (btCtrl.activeCount > 0)
? ("Scan [Active: " + btCtrl.activeSummary + "]")
: "Scan for Bluetooth Devices";
drawPango(layoutSmall, sel3 ? &colWhite : (btCtrl.activeCount > 0 ? &colGreen : &colLight),
28, r3Y, 22, scanLbl, kWinWidth - 34);
// Row 4: Compact OBEX File Reception Bar (height 22)
bool sel4 = (btRowSel == 4);
int rxY = contentY + 92;
XSetForeground(dpy, gc, sel4 ? pt.spec.selBg : pt.spec.bgHeader);
XFillRectangle(dpy, pix, gc, 4, rxY, kWinWidth - 8, 22);
XSetForeground(dpy, gc, btRx.state == "RECEIVING" ? pt.spec.warning
: (btRx.state == "RECEIVED" ? pt.spec.success : pt.spec.accent));
XDrawRectangle(dpy, pix, gc, 4, rxY, kWinWidth - 9, 35);
: (btRx.state == "RECEIVED" ? pt.spec.success : pt.spec.border));
XDrawRectangle(dpy, pix, gc, 4, rxY, kWinWidth - 9, 21);
drawPango(layoutIcon,
btRx.state == "RECEIVED" ? &colGreen
: (btRx.state == "RECEIVING" ? &colYellow : &colBlue),
10, rxY + 1, 18,
10, rxY, 22,
btRx.state == "RECEIVED" ? kIconCheck : kIconFolderOpen);
if (btRx.state == "RECEIVING") {
drawPango(layoutSmall, &colYellow, 30, rxY + 1, 18,
"Receiving: " + btRx.fileName + " (" + btRx.detail + ")",
kWinWidth - 38);
int barW = kWinWidth - 42;
XSetForeground(dpy, gc, pt.spec.bgInput);
XFillRectangle(dpy, pix, gc, 30, rxY + 22, barW, 8);
int fillW = (barW * std::clamp(btRx.pct, 0, 100)) / 100;
XSetForeground(dpy, gc, pt.spec.success);
XFillRectangle(dpy, pix, gc, 30, rxY + 22, fillW, 8);
drawPango(layoutSmall, &colYellow, 28, rxY, 22,
"Rx: " + btRx.fileName + " (" + btRx.detail + ")",
kWinWidth - 34);
} else if (btRx.state == "RECEIVED") {
drawPango(layoutSmall, &colGreen, 30, rxY + 1, 18,
"Received: " + btRx.fileName, kWinWidth - 38);
drawPango(layoutSmall, sel2 ? &colWhite : &colLight, 30, rxY + 18, 16,
btRx.detail + " [OK: Open in Files]", kWinWidth - 38);
drawPango(layoutSmall, sel4 ? &colWhite : &colGreen, 28, rxY, 22,
"Received: " + btRx.fileName + " [Open]",
kWinWidth - 34);
} else {
drawPango(layoutSmall, sel2 ? &colWhite : &colYellow, 30, rxY + 1, 18,
"File Receive: Ready (OBEX Push)", kWinWidth - 38);
drawPango(layoutSmall, sel2 ? &colLight : &colMuted, 30, rxY + 18, 16,
"Saves to ~/Downloads [OK: Open]", kWinWidth - 38);
drawPango(layoutSmall, sel4 ? &colWhite : &colMuted, 28, rxY, 22,
"OBEX Receive: ~/Downloads [Open]",
kWinWidth - 34);
}
int listY = contentY + 92;
int listH = contentH - 92;
int listY = contentY + 116;
int listH = contentH - 116;
if (btList.empty()) {
drawPango(layoutMain, &colMuted, 18, listY + 34, 24,
"No devices. Select Scan.");
drawPango(layoutSmall, &colMuted, 18, listY + 30, 22,
"No devices found. Press Scan.");
} else {
const int rowH = 36;
const int maxVis = std::max(1, listH / rowH);
int devSel = std::max(0, btRowSel - 3);
const int rowH = 38;
const int maxVis = std::max(1, listH / rowH); // 3 rows
int devSel = std::max(0, btRowSel - 5);
int startIdx = std::clamp(devSel - maxVis / 2, 0,
std::max(0, static_cast<int>(btList.size()) - maxVis));
for (int i = 0; i < maxVis && (startIdx + i) < static_cast<int>(btList.size()); ++i) {
int idx = startIdx + i;
const auto& d = btList[idx];
int ry = listY + i * rowH;
bool isSel = (btRowSel == (idx + 3));
bool isSel = (btRowSel == (idx + 5));
XSetForeground(dpy, gc, isSel ? pt.spec.selBg
: ((i % 2 == 0) ? pt.spec.bgAltEven : pt.spec.bgAltOdd));
XFillRectangle(dpy, pix, gc, 4, ry, kWinWidth - 8, rowH - 2);
if (d.connected) {
XSetForeground(dpy, gc, pt.spec.success);
XDrawRectangle(dpy, pix, gc, 4, ry, kWinWidth - 9, rowH - 3);
}
drawPango(layoutIcon, d.connected ? &colGreen : (d.paired ? &colBlue : &colMuted),
10, ry + 1, 18, kIconBluetooth);
10, ry + 1, 18, iconForBtDevice(d));
int nameMaxW = (d.batteryPct >= 0) ? (kWinWidth - 76) : (kWinWidth - 38);
drawPango(layoutMain, isSel ? &colWhite : &colLight,
30, ry + 1, 18, d.name, kWinWidth - 42);
28, ry + 1, 18, d.name, nameMaxW);
if (d.batteryPct >= 0) {
std::string batStr = std::to_string(d.batteryPct) + "%";
drawPango(layoutSmall, &colGreen, kWinWidth - 40, ry + 2, 16, batStr);
}
std::string st = d.connected ? "[Connected] "
: (d.paired ? "[Paired] " : "[Available] ");
std::string st;
if (d.connected) {
st = "[ACTIVE: " +
(d.profileLabel.empty() ? std::string("Connected") : d.profileLabel) +
"] ";
} else if (d.paired) {
st = "[Paired" +
(d.profileLabel.empty() ? std::string("") : (": " + d.profileLabel)) +
"] ";
} else {
st = "[Available] ";
}
st += d.mac;
drawPango(layoutSmall, d.connected ? &colGreen : (isSel ? &colLight : &colMuted),
30, ry + 18, 16, st, kWinWidth - 42);
28, ry + 19, 16, st, kWinWidth - 36);
}
}
}
@@ -938,26 +1245,69 @@ int runNetworksApp(int argc, char** argv) {
drawSoftkeyBar("Save", "Select", "Cancel");
} else if (modal == NetModal::BtActions) {
XSetForeground(dpy, gc, pt.spec.bgOverlay);
XFillRectangle(dpy, pix, gc, 12, 48, kWinWidth - 24, 174);
XFillRectangle(dpy, pix, gc, 10, 40, kWinWidth - 20, 194);
XSetForeground(dpy, gc, pt.spec.accent);
XDrawRectangle(dpy, pix, gc, 12, 48, kWinWidth - 25, 173);
XDrawRectangle(dpy, pix, gc, 10, 40, kWinWidth - 21, 193);
drawPango(layoutMain, &colYellow, 20, 52, 24, targetBt.name, kWinWidth - 40);
const char* acts[4] = {
drawPango(layoutMain, &colYellow, 18, 44, 22, targetBt.name, kWinWidth - 36);
const char* acts[5] = {
"Connect Device",
"Disconnect Device",
"Pair & Trust Device",
"Switch Audio (A2DP / HSP-HFP)",
"Forget / Remove Device",
};
for (int i = 0; i < 4; ++i) {
int ry = 82 + i * 32;
for (int i = 0; i < 5; ++i) {
int ry = 70 + i * 31;
bool isSel = (actionSel == i);
XSetForeground(dpy, gc, isSel ? pt.spec.selBg : pt.spec.bgButton);
XFillRectangle(dpy, pix, gc, 18, ry, kWinWidth - 36, 28);
XFillRectangle(dpy, pix, gc, 16, ry, kWinWidth - 32, 27);
drawPango(layoutSmall, isSel ? &colWhite : &colLight,
24, ry, 28, acts[i], kWinWidth - 48);
22, ry, 27, acts[i], kWinWidth - 44);
}
drawSoftkeyBar("Select", "OK", "Back");
} else if (modal == NetModal::BtNameEditor) {
XSetForeground(dpy, gc, pt.spec.bgOverlay);
XFillRectangle(dpy, pix, gc, 10, 42, kWinWidth - 20, 198);
XSetForeground(dpy, gc, pt.spec.accent);
XDrawRectangle(dpy, pix, gc, 10, 42, kWinWidth - 21, 197);
drawPango(layoutMain, &colYellow, 18, 48, 22,
"Bluetooth Name", kWinWidth - 80);
drawPango(layoutSmall, &colBlue, kWinWidth - 58, 48, 22,
std::string("[") + textModeLabel() + "]");
drawPango(layoutSmall, &colLight, 18, 74, 20,
"Visible name on other devices:");
XSetForeground(dpy, gc, pt.spec.bgInputFocus);
XFillRectangle(dpy, pix, gc, 18, 98, kWinWidth - 36, 34);
XSetForeground(dpy, gc, pt.spec.accent);
XDrawRectangle(dpy, pix, gc, 18, 98, kWinWidth - 37, 33);
std::string dispFit =
tailFitPangoText(layoutMain, btNameEdit + "_", kWinWidth - 48);
drawPango(layoutMain, &colWhite, 24, 98, 34, dispFit, kWinWidth - 48);
if (lastTapDigit >= 0 && lastTapDigit <= 9 &&
(nowMs() - lastTapMs) <= 850) {
const char* seq = (textMode == TextMode::Sym)
? kSymTapMap[lastTapDigit]
: kMultiTapMap[lastTapDigit];
std::string cand = formatMultiTapBar(
std::to_string(lastTapDigit) + ":", seq, lastTapCycle,
textMode == TextMode::Upper, 6, 19);
drawPango(layoutSmall, &colYellow, 18, 142, 18, cand, kWinWidth - 36);
} else {
drawPango(layoutSmall, &colMuted, 18, 142, 18,
"# Input mode", kWinWidth - 36);
}
drawPango(layoutSmall, &colMuted, 18, 162, 18,
"[BackSpace] Delete character");
drawPango(layoutSmall, &colMuted, 18, 182, 18,
"Independent from system hostname");
drawSoftkeyBar("Save", "Save", "Cancel");
}
if (!statusBanner.empty() && nowMs() < statusBannerUntilMs) {
@@ -1113,35 +1463,96 @@ int runNetworksApp(int argc, char** argv) {
}
};
auto saveBtDeviceName = [&]() {
std::string clean = trimStr(btNameEdit);
if (clean.empty()) {
clean = getSystemHostname();
}
modal = NetModal::None;
writeSavedBtName(clean);
btCtrl.name = clean;
if (btOn) {
runCaptureCmd("timeout 2 bluetoothctl system-alias " + shellQuote(clean) +
" >/dev/null 2>&1");
}
refreshCurrentTab(false);
setBanner("Bluetooth name: " + clean);
};
auto toggleBtDiscoverable = [&]() {
bool nextDisc = !btCtrl.discoverable;
writeSavedBtDiscoverable(nextDisc);
btCtrl.discoverable = nextDisc;
if (btOn) {
runCaptureCmd("timeout 2 bluetoothctl discoverable-timeout 0 >/dev/null 2>&1; "
"timeout 2 bluetoothctl discoverable " +
std::string(nextDisc ? "on" : "off") + " >/dev/null 2>&1");
}
refreshCurrentTab(false);
setBanner(nextDisc ? "Bluetooth: Visible to nearby devices"
: "Bluetooth: Hidden from discovery");
};
auto executeBtAction = [&](int act) {
modal = NetModal::None;
int rc = 0;
if (act == 0) {
setBanner("Connecting " + targetBt.name + "...");
render();
runCaptureCmd("bluetoothctl connect " + shellQuote(targetBt.mac) +
runCaptureCmd("timeout 8 bluetoothctl connect " + shellQuote(targetBt.mac) +
" >/dev/null 2>&1", &rc);
refreshCurrentTab(false);
if (rc == 0 && targetBt.kind == BtDevKind::Audio) {
runAsyncCmd("sxmo_audio.sh device set Bluetooth >/dev/null 2>&1");
}
setBanner(rc == 0 ? ("Connected: " + targetBt.name) : "Connect failed");
} else if (act == 1) {
setBanner("Disconnecting " + targetBt.name + "...");
render();
runCaptureCmd("bluetoothctl disconnect " + shellQuote(targetBt.mac) +
runCaptureCmd("timeout 4 bluetoothctl disconnect " + shellQuote(targetBt.mac) +
" >/dev/null 2>&1", &rc);
refreshCurrentTab(false);
setBanner("Disconnected " + targetBt.name);
} else if (act == 2) {
setBanner("Pairing " + targetBt.name + "...");
render();
runCaptureCmd("bluetoothctl --timeout 8 pair " + shellQuote(targetBt.mac) +
" >/dev/null 2>&1; bluetoothctl trust " +
runCaptureCmd("timeout 10 bluetoothctl --timeout 8 pair " +
shellQuote(targetBt.mac) +
" >/dev/null 2>&1; timeout 2 bluetoothctl trust " +
shellQuote(targetBt.mac) + " >/dev/null 2>&1; "
"bluetoothctl connect " + shellQuote(targetBt.mac) +
"timeout 6 bluetoothctl connect " + shellQuote(targetBt.mac) +
" >/dev/null 2>&1", &rc);
refreshCurrentTab(false);
setBanner("Paired & trusted " + targetBt.name);
} else if (act == 3) {
runCaptureCmd("bluetoothctl remove " + shellQuote(targetBt.mac) +
std::string macU = targetBt.mac;
for (char& c : macU) {
if (c == ':') c = '_';
}
std::string card = "bluez_card." + macU;
bool wantHfp = (targetBt.profileLabel.find("HSP") == std::string::npos &&
targetBt.profileLabel.find("HFP") == std::string::npos);
setBanner(std::string("Switching to ") + (wantHfp ? "HSP/HFP..." : "A2DP..."));
render();
if (wantHfp) {
runCaptureCmd("pactl set-card-profile " + shellQuote(card) +
" headset-head-unit >/dev/null 2>&1 || "
"pactl set-card-profile " + shellQuote(card) +
" headset-head-unit-msbc >/dev/null 2>&1 || "
"pactl set-card-profile " + shellQuote(card) +
" headset-head-unit-cvsd >/dev/null 2>&1", &rc);
runAsyncCmd("sxmo_audio.sh device set Bluetooth >/dev/null 2>&1; "
"sxmo_audio.sh device setinput Bluetooth >/dev/null 2>&1");
} else {
runCaptureCmd("pactl set-card-profile " + shellQuote(card) +
" a2dp-sink >/dev/null 2>&1", &rc);
runAsyncCmd("sxmo_audio.sh device set Bluetooth >/dev/null 2>&1");
}
refreshCurrentTab(false);
setBanner(rc == 0 ? (std::string("Audio profile: ") + (wantHfp ? "HSP/HFP" : "A2DP"))
: "Profile switch unsupported by device");
} else if (act == 4) {
runCaptureCmd("timeout 3 bluetoothctl remove " + shellQuote(targetBt.mac) +
" >/dev/null 2>&1", &rc);
refreshCurrentTab(false);
setBanner("Removed " + targetBt.name);
@@ -1169,6 +1580,10 @@ int runNetworksApp(int argc, char** argv) {
executeBtAction(actionSel);
return;
}
if (modal == NetModal::BtNameEditor) {
saveBtDeviceName();
return;
}
if (activeTab == NetTab::Wifi) {
if (wifiRowSel == 0) {
@@ -1255,12 +1670,19 @@ int runNetworksApp(int argc, char** argv) {
runCaptureCmd("SXMO_OS=alpine doas -n sxmo_bluetoothtoggle.sh " +
std::string(btOn ? "off" : "on") + " >/dev/null 2>&1");
refreshCurrentTab(false);
setBanner(btOn ? "Bluetooth enabled" : "Bluetooth disabled");
} else if (btRowSel == 1) {
toggleBtDiscoverable();
} else if (btRowSel == 2) {
btNameEdit = btCtrl.name;
lastTapDigit = -1;
modal = NetModal::BtNameEditor;
} else if (btRowSel == 3 && btOn) {
setBanner("Scanning Bluetooth devices (5s)...");
render();
refreshCurrentTab(true);
setBanner("Found " + std::to_string(btList.size()) + " device(s)");
} else if (btRowSel == 2 && btOn) {
} else if (btRowSel == 4 && btOn) {
std::string target = (!btRx.savedPath.empty())
? btRx.savedPath
: (getHomeDirPath() + "/Downloads");
@@ -1269,9 +1691,9 @@ int runNetworksApp(int argc, char** argv) {
if (!sendPluginCommand("files", "OPEN " + target)) {
runAsyncArgv("xfiles", target.c_str());
}
} else if (btRowSel - 3 >= 0 &&
btRowSel - 3 < static_cast<int>(btList.size())) {
targetBt = btList[btRowSel - 3];
} else if (btOn && btRowSel - 5 >= 0 &&
btRowSel - 5 < static_cast<int>(btList.size())) {
targetBt = btList[btRowSel - 5];
actionSel = 0;
modal = NetModal::BtActions;
}
@@ -1283,6 +1705,10 @@ int runNetworksApp(int argc, char** argv) {
saveAndConnectApn();
return;
}
if (modal == NetModal::BtNameEditor) {
saveBtDeviceName();
return;
}
if (modal != NetModal::None) {
handleEnterKey();
return;
@@ -1296,10 +1722,17 @@ int runNetworksApp(int argc, char** argv) {
refreshCurrentTab(false);
setBanner("Refreshed cellular state");
} else if (activeTab == NetTab::Bluetooth) {
setBanner("Scanning Bluetooth (5s)...");
render();
refreshCurrentTab(true);
setBanner("Found " + std::to_string(btList.size()) + " device(s)");
if (!btOn) {
setBanner("Turning Bluetooth ON...");
render();
runCaptureCmd("SXMO_OS=alpine doas -n sxmo_bluetoothtoggle.sh on >/dev/null 2>&1");
refreshCurrentTab(false);
} else {
setBanner("Scanning Bluetooth (5s)...");
render();
refreshCurrentTab(true);
setBanner("Found " + std::to_string(btList.size()) + " device(s)");
}
}
};
@@ -1422,12 +1855,25 @@ int runNetworksApp(int argc, char** argv) {
continue;
}
if (modal == NetModal::BtNameEditor) {
if (sym == XK_Return || sym == XK_KP_Enter) {
saveBtDeviceName();
render();
continue;
}
if (handleMultiTapKey(btNameEdit, sym, kc, ev.xkey.state)) {
render();
}
continue;
}
if (modal == NetModal::WifiActions || modal == NetModal::BtActions) {
int actCount = (modal == NetModal::BtActions) ? 5 : 4;
if (sym == XK_Up || sym == XK_KP_Up) {
actionSel = (actionSel + 3) % 4;
actionSel = (actionSel + actCount - 1) % actCount;
render();
} else if (sym == XK_Down || sym == XK_KP_Down) {
actionSel = (actionSel + 1) % 4;
actionSel = (actionSel + 1) % actCount;
render();
} else if (sym == XK_Return || sym == XK_KP_Enter) {
handleEnterKey();
@@ -1470,7 +1916,7 @@ int runNetworksApp(int argc, char** argv) {
} else if (activeTab == NetTab::Cellular) {
cellRowSel = (cellRowSel + 3) % 4;
} else if (activeTab == NetTab::Bluetooth) {
int total = btOn ? (3 + static_cast<int>(btList.size())) : 2;
int total = btOn ? (5 + static_cast<int>(btList.size())) : 3;
btRowSel = (btRowSel + total - 1) % total;
}
render();
@@ -1481,7 +1927,7 @@ int runNetworksApp(int argc, char** argv) {
} else if (activeTab == NetTab::Cellular) {
cellRowSel = (cellRowSel + 1) % 4;
} else if (activeTab == NetTab::Bluetooth) {
int total = btOn ? (3 + static_cast<int>(btList.size())) : 2;
int total = btOn ? (5 + static_cast<int>(btList.size())) : 3;
btRowSel = (btRowSel + 1) % total;
}
render();
@@ -1525,7 +1971,8 @@ int runNetworksApp(int argc, char** argv) {
}
}
if (lastTapDigit != -1 &&
(modal == NetModal::WifiPassword || modal == NetModal::ApnEditor)) {
(modal == NetModal::WifiPassword || modal == NetModal::ApnEditor ||
modal == NetModal::BtNameEditor)) {
int64_t tRem = (lastTapMs + 851) - now;
if (tRem <= 0) {
lastTapDigit = -1;