bluetooth: fix BLE/HID keyboard & mouse pairing, auto-bypass T9 on external keyboard, and show mouse cursor
This commit is contained in:
1 parent
b9e7c6847b
commit
02ce3ab1f4
67 files changed
+1227
-293
No files matched your search
@@ -458,7 +458,11 @@ int runContactsApp(int argc, char** argv) {
|
||||
|
||||
const char* modeNames[4] = {"[Abc]", "[ABC]", "[abc]", "[123]"};
|
||||
std::string modeBadge =
|
||||
(editField == 0) ? modeNames[static_cast<int>(caseMode)] : "[+0-9]";
|
||||
(editField == 0)
|
||||
? (isExternalKeyboardActive()
|
||||
? "[KBD]"
|
||||
: modeNames[static_cast<int>(caseMode)])
|
||||
: "[+0-9]";
|
||||
int mw = pangoWidth(layoutSmall, modeBadge);
|
||||
drawPango(layoutSmall, &colYellow, kWinWidth - mw - 6, 0, kHeaderH,
|
||||
modeBadge);
|
||||
@@ -1208,7 +1212,7 @@ int runContactsApp(int argc, char** argv) {
|
||||
}
|
||||
}
|
||||
} else if (ev.type == KeyPress) {
|
||||
KeySym sym = XLookupKeysym(&ev.xkey, 0);
|
||||
KeySym sym = XLookupKeysym(&ev.xkey, (ev.xkey.state & ShiftMask) ? 1 : 0);
|
||||
bool isLeftSoft = (sym == XK_Menu || sym == XF86XK_KbdLcdMenu1 ||
|
||||
ev.xkey.keycode == 135);
|
||||
bool isRightSoft = (sym == XF86XK_Back || sym == XF86XK_KbdLcdMenu2 ||
|
||||
@@ -1389,36 +1393,29 @@ int runContactsApp(int argc, char** argv) {
|
||||
} else if (mode == ViewMode::Edit) {
|
||||
char dialCh = mapDialKey(sym, ev.xkey.keycode, ev.xkey.state);
|
||||
if (editField == 0) {
|
||||
if (dialCh == '#') {
|
||||
caseMode = static_cast<TextCaseMode>(
|
||||
(static_cast<int>(caseMode) + 1) % 4);
|
||||
lastTapDigit = -1;
|
||||
render();
|
||||
continue;
|
||||
}
|
||||
if (dialCh == '*') {
|
||||
editName.push_back(' ');
|
||||
lastTapDigit = -1;
|
||||
render();
|
||||
continue;
|
||||
}
|
||||
if (dialCh >= '0' && dialCh <= '9') {
|
||||
handleMultiTapNameChar(dialCh - '0');
|
||||
render();
|
||||
continue;
|
||||
}
|
||||
if (sym >= XK_a && sym <= XK_z) {
|
||||
char c = static_cast<char>('a' + (sym - XK_a));
|
||||
if (ev.xkey.state & ShiftMask) {
|
||||
c = static_cast<char>(std::toupper(static_cast<unsigned char>(c)));
|
||||
if (!isExternalKeyboardActive()) {
|
||||
if (dialCh == '#') {
|
||||
caseMode = static_cast<TextCaseMode>(
|
||||
(static_cast<int>(caseMode) + 1) % 4);
|
||||
lastTapDigit = -1;
|
||||
render();
|
||||
continue;
|
||||
}
|
||||
if (dialCh == '*') {
|
||||
editName.push_back(' ');
|
||||
lastTapDigit = -1;
|
||||
render();
|
||||
continue;
|
||||
}
|
||||
if (dialCh >= '0' && dialCh <= '9') {
|
||||
handleMultiTapNameChar(dialCh - '0');
|
||||
render();
|
||||
continue;
|
||||
}
|
||||
editName.push_back(c);
|
||||
lastTapDigit = -1;
|
||||
render();
|
||||
continue;
|
||||
}
|
||||
if (sym == XK_space) {
|
||||
editName.push_back(' ');
|
||||
if (sym >= 0x20 && sym <= 0x7e &&
|
||||
!(ev.xkey.state & (ControlMask | Mod1Mask))) {
|
||||
editName.push_back(static_cast<char>(sym));
|
||||
lastTapDigit = -1;
|
||||
render();
|
||||
continue;
|
||||
|
||||
@@ -50,7 +50,7 @@ std::vector<BtSendTarget> queryBtSendTargets(bool scanFirst = false) {
|
||||
runCaptureCmd("SXMO_OS=alpine doas -n sxmo_bluetoothtoggle.sh on >/dev/null 2>&1");
|
||||
}
|
||||
if (scanFirst) {
|
||||
runCaptureCmd("timeout 5 bluetoothctl --timeout 4 scan on >/dev/null 2>&1");
|
||||
runCaptureCmd("timeout 16 bluetoothctl --timeout 15 scan on >/dev/null 2>&1");
|
||||
}
|
||||
std::unordered_set<std::string> pairedMacs;
|
||||
std::unordered_set<std::string> connectedMacs;
|
||||
@@ -1450,7 +1450,8 @@ int runFilesApp(int argc, char** argv) {
|
||||
drawPango(layoutMain, &colWhite, 28, 0, 36, inputTitle, 145);
|
||||
|
||||
const char* mBadge = "[abc]";
|
||||
if (caseMode == TextCaseMode::Title) mBadge = "[Abc]";
|
||||
if (isExternalKeyboardActive()) mBadge = "[KBD]";
|
||||
else if (caseMode == TextCaseMode::Title) mBadge = "[Abc]";
|
||||
else if (caseMode == TextCaseMode::Upper) mBadge = "[ABC]";
|
||||
else if (caseMode == TextCaseMode::Num) mBadge = "[123]";
|
||||
drawPango(layoutSmall, &colYellow, kWinWidth - 46, 0, 36, mBadge);
|
||||
@@ -1516,7 +1517,8 @@ int runFilesApp(int argc, char** argv) {
|
||||
drawPango(layoutMain, &colWhite, 25, 0, 32, titleDisp, 114);
|
||||
|
||||
const char* mBadge = "[abc]";
|
||||
if (editCaseMode == TextCaseMode::Title) mBadge = "[Abc]";
|
||||
if (isExternalKeyboardActive()) mBadge = "[KBD]";
|
||||
else if (editCaseMode == TextCaseMode::Title) mBadge = "[Abc]";
|
||||
else if (editCaseMode == TextCaseMode::Upper) mBadge = "[ABC]";
|
||||
else if (editCaseMode == TextCaseMode::Num) mBadge = "[123]";
|
||||
else if (editCaseMode == TextCaseMode::Sym) mBadge = "[Sym]";
|
||||
@@ -2405,8 +2407,8 @@ int runFilesApp(int argc, char** argv) {
|
||||
} else if (mode == ViewMode::BtSendModal) {
|
||||
if (isLeft) {
|
||||
if (btTargets.empty()) {
|
||||
statusBanner = "Scanning Bluetooth...";
|
||||
statusBannerUntilMs = nowMs() + 2500;
|
||||
statusBanner = "Scanning Bluetooth (15s)...";
|
||||
statusBannerUntilMs = nowMs() + 16000;
|
||||
drawUI();
|
||||
btTargets = queryBtSendTargets(true);
|
||||
btTargetSel = 0;
|
||||
@@ -2475,7 +2477,7 @@ int runFilesApp(int argc, char** argv) {
|
||||
} else if (ev.type == KeyPress) {
|
||||
int64_t tNow = nowMs();
|
||||
unsigned int kc = ev.xkey.keycode;
|
||||
KeySym sym = XLookupKeysym(&ev.xkey, 0);
|
||||
KeySym sym = XLookupKeysym(&ev.xkey, (ev.xkey.state & ShiftMask) ? 1 : 0);
|
||||
|
||||
bool isLeftSk =
|
||||
(kc == 135 || sym == XK_Menu || sym == XF86XK_KbdLcdMenu1 ||
|
||||
@@ -2686,17 +2688,28 @@ int runFilesApp(int argc, char** argv) {
|
||||
insertEditorNewline();
|
||||
drawUI();
|
||||
} else {
|
||||
char d = mapDialKey(sym, kc, ev.xkey.state);
|
||||
if (d == '#') {
|
||||
if (!isExternalKeyboardActive()) {
|
||||
char d = mapDialKey(sym, kc, ev.xkey.state);
|
||||
if (d == '#') {
|
||||
editLastTapDigit = -1;
|
||||
editCaseMode = static_cast<TextCaseMode>(
|
||||
(static_cast<int>(editCaseMode) + 1) % 5);
|
||||
drawUI();
|
||||
continue;
|
||||
} else if (d == '*') {
|
||||
commitEditorTap(10);
|
||||
drawUI();
|
||||
continue;
|
||||
} else if (d >= '0' && d <= '9') {
|
||||
commitEditorTap(d - '0');
|
||||
drawUI();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (sym >= 0x20 && sym <= 0x7e &&
|
||||
!(ev.xkey.state & (ControlMask | Mod1Mask))) {
|
||||
editLastTapDigit = -1;
|
||||
editCaseMode = static_cast<TextCaseMode>(
|
||||
(static_cast<int>(editCaseMode) + 1) % 5);
|
||||
drawUI();
|
||||
} else if (d == '*') {
|
||||
commitEditorTap(10);
|
||||
drawUI();
|
||||
} else if (d >= '0' && d <= '9') {
|
||||
commitEditorTap(d - '0');
|
||||
insertEditorStr(std::string(1, static_cast<char>(sym)));
|
||||
drawUI();
|
||||
}
|
||||
}
|
||||
@@ -2757,17 +2770,28 @@ int runFilesApp(int argc, char** argv) {
|
||||
lastTapDigit = -1;
|
||||
drawUI();
|
||||
} else {
|
||||
char d = mapDialKey(sym, kc, ev.xkey.state);
|
||||
if (d == '#') {
|
||||
if (!isExternalKeyboardActive()) {
|
||||
char d = mapDialKey(sym, kc, ev.xkey.state);
|
||||
if (d == '#') {
|
||||
lastTapDigit = -1;
|
||||
caseMode = static_cast<TextCaseMode>(
|
||||
(static_cast<int>(caseMode) + 1) % 4);
|
||||
drawUI();
|
||||
continue;
|
||||
} else if (d == '*') {
|
||||
commitModalTap(1);
|
||||
drawUI();
|
||||
continue;
|
||||
} else if (d >= '0' && d <= '9') {
|
||||
commitModalTap(d - '0');
|
||||
drawUI();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (sym >= 0x20 && sym <= 0x7e &&
|
||||
!(ev.xkey.state & (ControlMask | Mod1Mask))) {
|
||||
lastTapDigit = -1;
|
||||
caseMode = static_cast<TextCaseMode>(
|
||||
(static_cast<int>(caseMode) + 1) % 4);
|
||||
drawUI();
|
||||
} else if (d == '*') {
|
||||
commitModalTap(1);
|
||||
drawUI();
|
||||
} else if (d >= '0' && d <= '9') {
|
||||
commitModalTap(d - '0');
|
||||
inputValue.push_back(static_cast<char>(sym));
|
||||
drawUI();
|
||||
}
|
||||
}
|
||||
@@ -2795,8 +2819,8 @@ int runFilesApp(int argc, char** argv) {
|
||||
drawUI();
|
||||
}
|
||||
} else if (d == '*') {
|
||||
statusBanner = "Scanning Bluetooth...";
|
||||
statusBannerUntilMs = nowMs() + 2500;
|
||||
statusBanner = "Scanning Bluetooth (15s)...";
|
||||
statusBannerUntilMs = nowMs() + 16000;
|
||||
drawUI();
|
||||
btTargets = queryBtSendTargets(true);
|
||||
btTargetSel = 0;
|
||||
|
||||
@@ -459,7 +459,26 @@ 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) {
|
||||
}
|
||||
|
||||
auto isRawMacName = [](const BtDeviceEntry& e) {
|
||||
if (e.name == e.mac) return true;
|
||||
if (e.name.size() == 17 && e.name[2] == '-' && e.name[5] == '-') return true;
|
||||
return false;
|
||||
};
|
||||
std::sort(out.begin(), out.end(),
|
||||
[&](const BtDeviceEntry& a, const BtDeviceEntry& b) {
|
||||
if (a.connected != b.connected) return a.connected > b.connected;
|
||||
if (a.paired != b.paired) return a.paired > b.paired;
|
||||
bool aRaw = isRawMacName(a);
|
||||
bool bRaw = isRawMacName(b);
|
||||
if (aRaw != bRaw) return aRaw < bRaw;
|
||||
return a.name < b.name;
|
||||
});
|
||||
|
||||
for (const auto& d : out) {
|
||||
if (infoCount >= 12) break;
|
||||
if (d.connected || d.paired || !isRawMacName(d)) {
|
||||
infoCmd += "printf '===BTDEV %s===\\n' " + shellQuote(d.mac) +
|
||||
"; timeout 1 bluetoothctl info " + shellQuote(d.mac) +
|
||||
" 2>/dev/null; ";
|
||||
@@ -571,9 +590,12 @@ std::vector<BtDeviceEntry> queryBtDevices() {
|
||||
}
|
||||
|
||||
std::sort(out.begin(), out.end(),
|
||||
[](const BtDeviceEntry& a, const BtDeviceEntry& b) {
|
||||
[&](const BtDeviceEntry& a, const BtDeviceEntry& b) {
|
||||
if (a.connected != b.connected) return a.connected > b.connected;
|
||||
if (a.paired != b.paired) return a.paired > b.paired;
|
||||
bool aRaw = isRawMacName(a);
|
||||
bool bRaw = isRawMacName(b);
|
||||
if (aRaw != bRaw) return aRaw < bRaw;
|
||||
return a.name < b.name;
|
||||
});
|
||||
return out;
|
||||
@@ -757,12 +779,13 @@ int runNetworksApp(int argc, char** argv) {
|
||||
} else if (activeTab == NetTab::Bluetooth) {
|
||||
btOn = isBtEnabledFast();
|
||||
if (btOn) {
|
||||
if (::access("/tmp/bananarchy_bt_obex.pid", F_OK) != 0) {
|
||||
int obexPid = readIntFile("/tmp/bananarchy_bt_obex.pid");
|
||||
if (obexPid <= 0 || ::kill(obexPid, 0) != 0) {
|
||||
runAsyncCmd("/usr/bin/bananarchy-bt-obex start >/dev/null 2>&1");
|
||||
}
|
||||
btRx = readBtRxStatus();
|
||||
if (forceScan) {
|
||||
runCaptureCmd("timeout 6 bluetoothctl --timeout 5 scan on >/dev/null 2>&1");
|
||||
runCaptureCmd("timeout 16 bluetoothctl --timeout 15 scan on >/dev/null 2>&1");
|
||||
}
|
||||
btList = queryBtDevices();
|
||||
} else {
|
||||
@@ -788,6 +811,7 @@ int runNetworksApp(int argc, char** argv) {
|
||||
};
|
||||
|
||||
auto textModeLabel = [&]() -> const char* {
|
||||
if (isExternalKeyboardActive()) return "KBD";
|
||||
switch (textMode) {
|
||||
case TextMode::Lower: return "abc";
|
||||
case TextMode::Title: return "Abc";
|
||||
@@ -800,43 +824,45 @@ int runNetworksApp(int argc, char** argv) {
|
||||
|
||||
auto handleMultiTapKey = [&](std::string& targetStr, KeySym sym,
|
||||
unsigned int kc, unsigned int state) -> bool {
|
||||
char dch = mapDialKey(sym, kc, state);
|
||||
if (dch == '#') {
|
||||
textMode = static_cast<TextMode>((static_cast<int>(textMode) + 1) % 5);
|
||||
lastTapDigit = -1;
|
||||
return true;
|
||||
}
|
||||
if (dch >= '0' && dch <= '9') {
|
||||
int digit = dch - '0';
|
||||
int64_t now = nowMs();
|
||||
if (textMode == TextMode::Num) {
|
||||
targetStr.push_back(dch);
|
||||
if (!isExternalKeyboardActive()) {
|
||||
char dch = mapDialKey(sym, kc, state);
|
||||
if (dch == '#') {
|
||||
textMode = static_cast<TextMode>((static_cast<int>(textMode) + 1) % 5);
|
||||
lastTapDigit = -1;
|
||||
return true;
|
||||
}
|
||||
const char* seq = (textMode == TextMode::Sym)
|
||||
? kSymTapMap[digit]
|
||||
: kMultiTapMap[digit];
|
||||
int seqLen = static_cast<int>(std::strlen(seq));
|
||||
if (seqLen <= 0) return false;
|
||||
if (dch >= '0' && dch <= '9') {
|
||||
int digit = dch - '0';
|
||||
int64_t now = nowMs();
|
||||
if (textMode == TextMode::Num) {
|
||||
targetStr.push_back(dch);
|
||||
lastTapDigit = -1;
|
||||
return true;
|
||||
}
|
||||
const char* seq = (textMode == TextMode::Sym)
|
||||
? kSymTapMap[digit]
|
||||
: kMultiTapMap[digit];
|
||||
int seqLen = static_cast<int>(std::strlen(seq));
|
||||
if (seqLen <= 0) return false;
|
||||
|
||||
if (digit == lastTapDigit && (now - lastTapMs) <= 850 && !targetStr.empty()) {
|
||||
popUtf8Char(targetStr);
|
||||
lastTapCycle = (lastTapCycle + 1) % seqLen;
|
||||
} else {
|
||||
lastTapDigit = digit;
|
||||
lastTapCycle = 0;
|
||||
}
|
||||
lastTapMs = now;
|
||||
if (digit == lastTapDigit && (now - lastTapMs) <= 850 && !targetStr.empty()) {
|
||||
popUtf8Char(targetStr);
|
||||
lastTapCycle = (lastTapCycle + 1) % seqLen;
|
||||
} else {
|
||||
lastTapDigit = digit;
|
||||
lastTapCycle = 0;
|
||||
}
|
||||
lastTapMs = now;
|
||||
|
||||
char outCh = seq[lastTapCycle];
|
||||
if (textMode == TextMode::Upper) {
|
||||
outCh = static_cast<char>(std::toupper(static_cast<unsigned char>(outCh)));
|
||||
} else if (textMode == TextMode::Title && targetStr.empty()) {
|
||||
outCh = static_cast<char>(std::toupper(static_cast<unsigned char>(outCh)));
|
||||
char outCh = seq[lastTapCycle];
|
||||
if (textMode == TextMode::Upper) {
|
||||
outCh = static_cast<char>(std::toupper(static_cast<unsigned char>(outCh)));
|
||||
} else if (textMode == TextMode::Title && targetStr.empty()) {
|
||||
outCh = static_cast<char>(std::toupper(static_cast<unsigned char>(outCh)));
|
||||
}
|
||||
targetStr.push_back(outCh);
|
||||
return true;
|
||||
}
|
||||
targetStr.push_back(outCh);
|
||||
return true;
|
||||
}
|
||||
if (sym == XK_BackSpace || sym == XK_Delete) {
|
||||
if (!targetStr.empty()) {
|
||||
@@ -1499,13 +1525,21 @@ int runNetworksApp(int argc, char** argv) {
|
||||
if (act == 0) {
|
||||
setBanner("Connecting " + targetBt.name + "...");
|
||||
render();
|
||||
runCaptureCmd("timeout 8 bluetoothctl connect " + shellQuote(targetBt.mac) +
|
||||
" >/dev/null 2>&1", &rc);
|
||||
runCaptureCmd("/usr/bin/bananarchy-bt-obex connect " +
|
||||
shellQuote(targetBt.mac) + " " +
|
||||
shellQuote(targetBt.name) + " >/dev/null 2>&1", &rc);
|
||||
refreshCurrentTab(false);
|
||||
if (rc == 0 && targetBt.kind == BtDevKind::Audio) {
|
||||
bool isConn = (rc == 0);
|
||||
for (const auto& d : btList) {
|
||||
if ((d.mac == targetBt.mac || d.name == targetBt.name) && d.connected) {
|
||||
isConn = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (isConn && targetBt.kind == BtDevKind::Audio) {
|
||||
runAsyncCmd("sxmo_audio.sh device set Bluetooth >/dev/null 2>&1");
|
||||
}
|
||||
setBanner(rc == 0 ? ("Connected: " + targetBt.name) : "Connect failed");
|
||||
setBanner(isConn ? ("Connected: " + targetBt.name) : "Connect failed");
|
||||
} else if (act == 1) {
|
||||
setBanner("Disconnecting " + targetBt.name + "...");
|
||||
render();
|
||||
@@ -1516,14 +1550,23 @@ int runNetworksApp(int argc, char** argv) {
|
||||
} else if (act == 2) {
|
||||
setBanner("Pairing " + targetBt.name + "...");
|
||||
render();
|
||||
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; "
|
||||
"timeout 6 bluetoothctl connect " + shellQuote(targetBt.mac) +
|
||||
" >/dev/null 2>&1", &rc);
|
||||
runCaptureCmd("/usr/bin/bananarchy-bt-obex pair " +
|
||||
shellQuote(targetBt.mac) + " " +
|
||||
shellQuote(targetBt.name) + " >/dev/null 2>&1", &rc);
|
||||
refreshCurrentTab(false);
|
||||
setBanner("Paired & trusted " + targetBt.name);
|
||||
bool isPairedOrConn = (rc == 0);
|
||||
for (const auto& d : btList) {
|
||||
if ((d.mac == targetBt.mac || d.name == targetBt.name) &&
|
||||
(d.connected || d.paired)) {
|
||||
isPairedOrConn = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (isPairedOrConn && targetBt.kind == BtDevKind::Audio) {
|
||||
runAsyncCmd("sxmo_audio.sh device set Bluetooth >/dev/null 2>&1");
|
||||
}
|
||||
setBanner(isPairedOrConn ? ("Paired & connected: " + targetBt.name)
|
||||
: ("Pairing failed: " + targetBt.name));
|
||||
} else if (act == 3) {
|
||||
std::string macU = targetBt.mac;
|
||||
for (char& c : macU) {
|
||||
@@ -1678,7 +1721,7 @@ int runNetworksApp(int argc, char** argv) {
|
||||
lastTapDigit = -1;
|
||||
modal = NetModal::BtNameEditor;
|
||||
} else if (btRowSel == 3 && btOn) {
|
||||
setBanner("Scanning Bluetooth devices (5s)...");
|
||||
setBanner("Scanning Bluetooth devices (15s)...", 16000);
|
||||
render();
|
||||
refreshCurrentTab(true);
|
||||
setBanner("Found " + std::to_string(btList.size()) + " device(s)");
|
||||
@@ -1728,7 +1771,7 @@ int runNetworksApp(int argc, char** argv) {
|
||||
runCaptureCmd("SXMO_OS=alpine doas -n sxmo_bluetoothtoggle.sh on >/dev/null 2>&1");
|
||||
refreshCurrentTab(false);
|
||||
} else {
|
||||
setBanner("Scanning Bluetooth (5s)...");
|
||||
setBanner("Scanning Bluetooth (15s)...", 16000);
|
||||
render();
|
||||
refreshCurrentTab(true);
|
||||
setBanner("Found " + std::to_string(btList.size()) + " device(s)");
|
||||
@@ -1772,7 +1815,7 @@ int runNetworksApp(int argc, char** argv) {
|
||||
int64_t now = nowMs();
|
||||
if (now < startupIgnoreUntilMs) continue;
|
||||
|
||||
KeySym sym = XLookupKeysym(&ev.xkey, 0);
|
||||
KeySym sym = XLookupKeysym(&ev.xkey, (ev.xkey.state & ShiftMask) ? 1 : 0);
|
||||
unsigned int kc = ev.xkey.keycode;
|
||||
|
||||
bool isLSK = (kc == 135 || sym == XK_F1 || sym == XK_Menu ||
|
||||
@@ -1999,6 +2042,9 @@ int runNetworksApp(int argc, char** argv) {
|
||||
if (visible) render();
|
||||
} else if (cmd == "REFRESH_BT") {
|
||||
btRx = readBtRxStatus();
|
||||
if (visible && activeTab == NetTab::Bluetooth) {
|
||||
refreshCurrentTab(false);
|
||||
}
|
||||
if (visible) render();
|
||||
} else if (cmd == "TOGGLE") {
|
||||
if (visible) hideWindow();
|
||||
|
||||
@@ -469,8 +469,61 @@ inline void runAsyncArgv(const char* prog,
|
||||
}
|
||||
}
|
||||
|
||||
inline bool isExternalKeyboardActive() {
|
||||
static time_t lastDirSec = -1;
|
||||
static long lastDirNsec = -1;
|
||||
static bool cachedActive = false;
|
||||
|
||||
struct stat st {};
|
||||
if (::stat("/dev/input", &st) == 0) {
|
||||
if (st.st_mtim.tv_sec == lastDirSec && st.st_mtim.tv_nsec == lastDirNsec) {
|
||||
return cachedActive;
|
||||
}
|
||||
lastDirSec = st.st_mtim.tv_sec;
|
||||
lastDirNsec = st.st_mtim.tv_nsec;
|
||||
}
|
||||
|
||||
FILE* fp = std::fopen("/proc/bus/input/devices", "r");
|
||||
if (!fp) {
|
||||
cachedActive = false;
|
||||
return false;
|
||||
}
|
||||
bool extBus = false;
|
||||
bool found = false;
|
||||
char line[512];
|
||||
while (std::fgets(line, sizeof(line), fp)) {
|
||||
if (line[0] == '\n' || line[0] == '\r' || line[0] == '\0') {
|
||||
extBus = false;
|
||||
continue;
|
||||
}
|
||||
if (std::strncmp(line, "I: Bus=0005", 11) == 0 ||
|
||||
std::strncmp(line, "I: Bus=0003", 11) == 0) {
|
||||
extBus = true;
|
||||
} else if (extBus && std::strncmp(line, "B: KEY=", 7) == 0) {
|
||||
const char* p = line + 7;
|
||||
const char* lastToken = nullptr;
|
||||
while (*p) {
|
||||
while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r') ++p;
|
||||
if (!*p) break;
|
||||
lastToken = p;
|
||||
while (*p && *p != ' ' && *p != '\t' && *p != '\n' && *p != '\r') ++p;
|
||||
}
|
||||
if (lastToken) {
|
||||
unsigned long long mask = std::strtoull(lastToken, nullptr, 16);
|
||||
if ((mask & 0x03ff0000ULL) == 0x03ff0000ULL) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
std::fclose(fp);
|
||||
cachedActive = found;
|
||||
return cachedActive;
|
||||
}
|
||||
|
||||
inline char mapDialKey(KeySym sym, unsigned int keycode, unsigned int state) {
|
||||
if (state & (ControlMask | Mod1Mask | Mod4Mask)) {
|
||||
if (state & (ShiftMask | ControlMask | Mod1Mask | Mod4Mask)) {
|
||||
return '\0';
|
||||
}
|
||||
// Keypad star (*)
|
||||
|
||||
@@ -470,6 +470,7 @@ int privacy_run_main(int argc, char** argv) {
|
||||
};
|
||||
|
||||
auto textModeBadge = [&]() -> const char* {
|
||||
if (isExternalKeyboardActive()) return "[KBD]";
|
||||
switch (textMode) {
|
||||
case TextMode::Lower: return "[abc]";
|
||||
case TextMode::Title: return "[Abc]";
|
||||
@@ -508,50 +509,52 @@ int privacy_run_main(int argc, char** argv) {
|
||||
|
||||
auto handleMultiTapKey = [&](std::string& targetStr, KeySym sym,
|
||||
unsigned int kc, unsigned int state) -> bool {
|
||||
char dch = mapDialKey(sym, kc, state);
|
||||
if (dch == '#') {
|
||||
textMode = static_cast<TextMode>((static_cast<int>(textMode) + 1) % 5);
|
||||
lastTapDigit = -1;
|
||||
return true;
|
||||
}
|
||||
if (dch == '*') {
|
||||
if (!targetStr.empty()) {
|
||||
popUtf8Char(targetStr);
|
||||
lastTapDigit = -1;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
if (dch >= '0' && dch <= '9') {
|
||||
int digit = dch - '0';
|
||||
int64_t now = nowMs();
|
||||
if (textMode == TextMode::Num) {
|
||||
targetStr.push_back(dch);
|
||||
if (!isExternalKeyboardActive()) {
|
||||
char dch = mapDialKey(sym, kc, state);
|
||||
if (dch == '#') {
|
||||
textMode = static_cast<TextMode>((static_cast<int>(textMode) + 1) % 5);
|
||||
lastTapDigit = -1;
|
||||
return true;
|
||||
}
|
||||
const char* seq = (textMode == TextMode::Sym)
|
||||
? kSymTapMap[digit]
|
||||
: kMultiTapMap[digit];
|
||||
int seqLen = static_cast<int>(std::strlen(seq));
|
||||
if (seqLen <= 0) return false;
|
||||
|
||||
if (digit == lastTapDigit && (now - lastTapMs) <= 850 && !targetStr.empty()) {
|
||||
popUtf8Char(targetStr);
|
||||
lastTapCycle = (lastTapCycle + 1) % seqLen;
|
||||
} else {
|
||||
lastTapDigit = digit;
|
||||
lastTapCycle = 0;
|
||||
if (dch == '*') {
|
||||
if (!targetStr.empty()) {
|
||||
popUtf8Char(targetStr);
|
||||
lastTapDigit = -1;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
lastTapMs = now;
|
||||
if (dch >= '0' && dch <= '9') {
|
||||
int digit = dch - '0';
|
||||
int64_t now = nowMs();
|
||||
if (textMode == TextMode::Num) {
|
||||
targetStr.push_back(dch);
|
||||
lastTapDigit = -1;
|
||||
return true;
|
||||
}
|
||||
const char* seq = (textMode == TextMode::Sym)
|
||||
? kSymTapMap[digit]
|
||||
: kMultiTapMap[digit];
|
||||
int seqLen = static_cast<int>(std::strlen(seq));
|
||||
if (seqLen <= 0) return false;
|
||||
|
||||
char outCh = seq[lastTapCycle];
|
||||
if (textMode == TextMode::Upper) {
|
||||
outCh = static_cast<char>(std::toupper(static_cast<unsigned char>(outCh)));
|
||||
} else if (textMode == TextMode::Title && targetStr.empty()) {
|
||||
outCh = static_cast<char>(std::toupper(static_cast<unsigned char>(outCh)));
|
||||
if (digit == lastTapDigit && (now - lastTapMs) <= 850 && !targetStr.empty()) {
|
||||
popUtf8Char(targetStr);
|
||||
lastTapCycle = (lastTapCycle + 1) % seqLen;
|
||||
} else {
|
||||
lastTapDigit = digit;
|
||||
lastTapCycle = 0;
|
||||
}
|
||||
lastTapMs = now;
|
||||
|
||||
char outCh = seq[lastTapCycle];
|
||||
if (textMode == TextMode::Upper) {
|
||||
outCh = static_cast<char>(std::toupper(static_cast<unsigned char>(outCh)));
|
||||
} else if (textMode == TextMode::Title && targetStr.empty()) {
|
||||
outCh = static_cast<char>(std::toupper(static_cast<unsigned char>(outCh)));
|
||||
}
|
||||
targetStr.push_back(outCh);
|
||||
return true;
|
||||
}
|
||||
targetStr.push_back(outCh);
|
||||
return true;
|
||||
}
|
||||
if (sym == XK_BackSpace || sym == XK_Delete) {
|
||||
if (!targetStr.empty()) {
|
||||
@@ -1147,7 +1150,7 @@ int privacy_run_main(int argc, char** argv) {
|
||||
if (!visible) continue;
|
||||
unsigned int kc = ev.xkey.keycode;
|
||||
unsigned int st = ev.xkey.state;
|
||||
KeySym sym = XLookupKeysym(&ev.xkey, 0);
|
||||
KeySym sym = XLookupKeysym(&ev.xkey, (st & ShiftMask) ? 1 : 0);
|
||||
|
||||
if (querySoftkeysDown(dpy) == 3) {
|
||||
writeSoftkeySuppress(nowMs() + 400);
|
||||
|
||||
@@ -706,6 +706,8 @@ int runSmsApp(int argc, char** argv) {
|
||||
const char* modeBadge = "[Abc]";
|
||||
if (composeField == 0) {
|
||||
modeBadge = "[123]";
|
||||
} else if (isExternalKeyboardActive()) {
|
||||
modeBadge = "[KBD]";
|
||||
} else if (caseMode == TextCaseMode::Upper) {
|
||||
modeBadge = "[ABC]";
|
||||
} else if (caseMode == TextCaseMode::Lower) {
|
||||
@@ -1393,7 +1395,7 @@ int runSmsApp(int argc, char** argv) {
|
||||
} else if (ev.type == KeyPress) {
|
||||
int64_t tNow = nowMs();
|
||||
unsigned int kc = ev.xkey.keycode;
|
||||
KeySym sym = XLookupKeysym(&ev.xkey, 0);
|
||||
KeySym sym = XLookupKeysym(&ev.xkey, (ev.xkey.state & ShiftMask) ? 1 : 0);
|
||||
|
||||
bool isLeftSk =
|
||||
(kc == 135 || sym == XK_Menu || sym == XF86XK_KbdLcdMenu1 ||
|
||||
@@ -1576,19 +1578,26 @@ int runSmsApp(int argc, char** argv) {
|
||||
drawUI();
|
||||
}
|
||||
} else if (composeField == 1) {
|
||||
if (d == '#') {
|
||||
lastTapDigit = -1;
|
||||
caseMode = static_cast<TextCaseMode>(
|
||||
(static_cast<int>(caseMode) + 1) % 4);
|
||||
drawUI();
|
||||
} else if (d == '*') {
|
||||
commitMultiTapChar(1);
|
||||
drawUI();
|
||||
} else if (d >= '0' && d <= '9') {
|
||||
commitMultiTapChar(d - '0');
|
||||
drawUI();
|
||||
} else if (sym == XK_space) {
|
||||
composeText.push_back(' ');
|
||||
if (!isExternalKeyboardActive()) {
|
||||
if (d == '#') {
|
||||
lastTapDigit = -1;
|
||||
caseMode = static_cast<TextCaseMode>(
|
||||
(static_cast<int>(caseMode) + 1) % 4);
|
||||
drawUI();
|
||||
continue;
|
||||
} else if (d == '*') {
|
||||
commitMultiTapChar(1);
|
||||
drawUI();
|
||||
continue;
|
||||
} else if (d >= '0' && d <= '9') {
|
||||
commitMultiTapChar(d - '0');
|
||||
drawUI();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (sym >= 0x20 && sym <= 0x7e &&
|
||||
!(ev.xkey.state & (ControlMask | Mod1Mask))) {
|
||||
composeText.push_back(static_cast<char>(sym));
|
||||
lastTapDigit = -1;
|
||||
drawUI();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user