Add preloaded SMS, File Manager, and Camera (live viewfinder) DWM plugins

This commit is contained in:
Luxferre committed 2026-10-21 14:46:11 +03:00
1 parent c1e5c646d6
commit 55b20c5b85
45 files changed
+4767 -126

No files matched your search

+316 -2
View File
@@ -16,6 +16,7 @@
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <dirent.h>
#include <fcntl.h>
#include <fstream>
#include <sstream>
@@ -53,6 +54,21 @@ constexpr const char* kIconTrash = "\xef\x87\xb8"; // U+F1F8 delete/trash
constexpr const char* kIconSms = "\xef\x83\xa0"; // U+F0E0 envelope/SMS
constexpr const char* kIconList = "\xef\x80\xba"; // U+F03A list
constexpr const char* kIconCheck = "\xef\x80\x8c"; // U+F00C check
constexpr const char* kIconFolder = "\xef\x81\xbb"; // U+F07B folder
constexpr const char* kIconFolderOpen = "\xef\x81\xbc"; // U+F07C folder-open
constexpr const char* kIconFile = "\xef\x80\x96"; // U+F016 file
constexpr const char* kIconFileText = "\xef\x85\x9c"; // U+F15C file-text
constexpr const char* kIconImage = "\xef\x80\xbe"; // U+F03E image
constexpr const char* kIconMusic = "\xef\x80\x81"; // U+F001 music/audio
constexpr const char* kIconTerm = "\xef\x84\xa0"; // U+F120 terminal
constexpr const char* kIconCamera = "\xef\x80\xb0"; // U+F030 camera
constexpr const char* kIconFlash = "\xef\x83\xa7"; // U+F0E7 bolt/flash
constexpr const char* kIconSend = "\xef\x87\x98"; // U+F1D8 paper-plane
constexpr const char* kIconCopy = "\xef\x83\x85"; // U+F0C5 copy
constexpr const char* kIconInfo = "\xef\x81\x9a"; // U+F05A info-circle
constexpr const char* kIconRefresh = "\xef\x80\xa1"; // U+F021 refresh
constexpr const char* kIconSort = "\xef\x83\x9c"; // U+F0DC sort
constexpr const char* kIconEye = "\xef\x81\xae"; // U+F06E eye/view
inline int64_t nowMs() {
struct timespec ts {};
@@ -249,7 +265,9 @@ inline void runAsyncCmd(const std::string& cmd) {
}
}
inline void runAsyncArgv(const char* prog, const char* arg1 = nullptr) {
inline void runAsyncArgv(const char* prog,
const char* arg1 = nullptr,
const char* arg2 = nullptr) {
pid_t pid = ::fork();
if (pid == 0) {
::setsid();
@@ -262,7 +280,13 @@ inline void runAsyncArgv(const char* prog, const char* arg1 = nullptr) {
::close(nullfd);
}
}
if (arg1) {
if (arg1 && arg2) {
execlp(prog, prog, arg1, arg2, static_cast<char*>(nullptr));
std::string localBin = std::string("/home/user/.local/bin/") + prog;
execl(localBin.c_str(), prog, arg1, arg2, static_cast<char*>(nullptr));
std::string usrBin = std::string("/usr/bin/") + prog;
execl(usrBin.c_str(), prog, arg1, arg2, static_cast<char*>(nullptr));
} else if (arg1) {
execlp(prog, prog, arg1, static_cast<char*>(nullptr));
std::string localBin = std::string("/home/user/.local/bin/") + prog;
execl(localBin.c_str(), prog, arg1, static_cast<char*>(nullptr));
@@ -896,6 +920,296 @@ inline void placePhoneCall(const std::string& rawNumber) {
runAsyncArgv("sxmo_modemdial.sh", num.c_str());
}
inline void closeOtherPlugins(const char* selfPluginName) {
struct PluginRef {
const char* pidFile;
const char* name;
};
static const PluginRef kAllPlugins[] = {
{"xdrawer.pid", "xdrawer"},
{"xdialer.pid", "dialer"},
{"xcalllog.pid", "calllog"},
{"xcontacts.pid", "contacts"},
{"xsms.pid", "sms"},
{"xfiles.pid", "files"},
{"xcamera.pid", "camera"},
};
for (const auto& p : kAllPlugins) {
if (selfPluginName && std::strcmp(p.name, selfPluginName) == 0) {
continue;
}
if (isPidFileAlive(getRuntimeFile(p.pidFile))) {
sendPluginCommand(p.name, "CLOSE");
}
}
}
inline std::string getModemLogDirPath() {
const char* env = std::getenv("SXMO_LOGDIR");
if (env && env[0]) {
return env;
}
const char* xdg = std::getenv("XDG_DATA_HOME");
if (xdg && xdg[0]) {
return std::string(xdg) + "/sxmo/modem";
}
const char* home = std::getenv("HOME");
return std::string(home && home[0] ? home : "/home/user") +
"/.local/share/sxmo/modem";
}
struct SmsMessage {
bool incoming = true;
std::string sender;
std::string isoTime;
std::string displayTime;
time_t epoch = 0;
std::string text;
};
struct SmsThread {
std::string number;
std::string normNumber;
std::string contactName;
std::string t9Name;
std::string lowerName;
std::string lastPreview;
std::string lastDisplayTime;
time_t lastEpoch = 0;
bool lastIncoming = true;
bool hasDraft = false;
int messageCount = 0;
};
inline bool parseSmsHeaderLine(const std::string& line,
bool* outIncoming,
std::string* outSender,
std::string* outIsoTime) {
if (line.empty() || line.back() != ':') {
return false;
}
if (line.rfind("Received at ", 0) == 0 && line.size() > 13) {
*outIncoming = true;
outSender->clear();
*outIsoTime = line.substr(12, line.size() - 13);
return true;
}
if (line.rfind("Received from ", 0) == 0) {
size_t atPos = line.find(" at ");
if (atPos != std::string::npos && atPos + 4 < line.size() - 1) {
*outIncoming = true;
*outSender = line.substr(14, atPos - 14);
*outIsoTime = line.substr(atPos + 4, line.size() - (atPos + 4) - 1);
return true;
}
}
if (line.rfind("Sent at ", 0) == 0 && line.size() > 9) {
*outIncoming = false;
outSender->clear();
*outIsoTime = line.substr(8, line.size() - 9);
return true;
}
return false;
}
inline std::vector<SmsMessage> loadSmsThreadMessages(const std::string& number) {
std::vector<SmsMessage> msgs;
std::string path = getModemLogDirPath() + "/" + number + "/sms.txt";
std::ifstream in(path);
if (!in.is_open()) {
return msgs;
}
std::string line;
bool hasCur = false;
SmsMessage cur;
auto flushCur = [&]() {
if (!hasCur) {
return;
}
while (!cur.text.empty() &&
(cur.text.back() == '\n' || cur.text.back() == '\r')) {
cur.text.pop_back();
}
msgs.push_back(cur);
cur = SmsMessage{};
hasCur = false;
};
while (std::getline(in, line)) {
if (!line.empty() && line.back() == '\r') {
line.pop_back();
}
bool inc = true;
std::string sender;
std::string iso;
if (parseSmsHeaderLine(line, &inc, &sender, &iso)) {
flushCur();
hasCur = true;
cur.incoming = inc;
cur.sender = sender.empty() ? number : sender;
cur.isoTime = iso;
cur.displayTime = formatShortTime(iso);
cur.epoch = parseIsoTime(iso);
cur.text.clear();
} else if (hasCur) {
if (!cur.text.empty()) {
cur.text.push_back('\n');
}
cur.text += line;
} else if (!trimStr(line).empty()) {
hasCur = true;
cur.incoming = true;
cur.sender = number;
cur.text = line;
}
}
flushCur();
return msgs;
}
inline std::string loadSmsDraft(const std::string& number) {
if (number.empty()) {
return "";
}
std::string path = getModemLogDirPath() + "/" + number + "/draft.txt";
std::ifstream in(path);
if (!in.is_open()) {
return "";
}
std::ostringstream ss;
ss << in.rdbuf();
std::string s = ss.str();
while (!s.empty() && (s.back() == '\n' || s.back() == '\r')) {
s.pop_back();
}
if (s == "Enter message here") {
return "";
}
return s;
}
inline bool saveSmsDraft(const std::string& number, const std::string& text) {
if (number.empty()) {
return false;
}
std::string dir = getModemLogDirPath() + "/" + number;
std::string path = dir + "/draft.txt";
if (trimStr(text).empty()) {
::unlink(path.c_str());
return true;
}
ContactBook::ensureParentDir(path);
std::ofstream out(path, std::ios::trunc);
if (!out.is_open()) {
return false;
}
out << text << "\n";
return true;
}
inline void removeSmsDraft(const std::string& number) {
if (number.empty()) {
return;
}
std::string path = getModemLogDirPath() + "/" + number + "/draft.txt";
::unlink(path.c_str());
}
inline bool deleteSmsThread(const std::string& number) {
if (number.empty()) {
return false;
}
std::string dir = getModemLogDirPath() + "/" + number;
std::string smsPath = dir + "/sms.txt";
std::string draftPath = dir + "/draft.txt";
::unlink(smsPath.c_str());
::unlink(draftPath.c_str());
::rmdir(dir.c_str());
return true;
}
inline std::vector<SmsThread> loadSmsThreads(const ContactBook& book) {
std::vector<SmsThread> threads;
std::string baseDir = getModemLogDirPath();
DIR* d = ::opendir(baseDir.c_str());
if (!d) {
return threads;
}
struct dirent* ent;
while ((ent = ::readdir(d)) != nullptr) {
std::string name = ent->d_name;
if (name.empty() || name[0] == '.') {
continue;
}
std::string subDir = baseDir + "/" + name;
struct stat st {};
if (::stat(subDir.c_str(), &st) != 0 || !S_ISDIR(st.st_mode)) {
continue;
}
std::string smsPath = subDir + "/sms.txt";
std::string draftPath = subDir + "/draft.txt";
bool hasSms = (::access(smsPath.c_str(), R_OK) == 0);
std::string draft = loadSmsDraft(name);
bool hasDraft = !draft.empty();
if (!hasSms && !hasDraft) {
continue;
}
auto msgs = loadSmsThreadMessages(name);
if (msgs.empty() && !hasDraft) {
continue;
}
SmsThread th;
th.number = name;
th.normNumber = normalizePhone(name);
th.contactName = book.resolveName(name);
std::string label = th.contactName.empty() ? name : th.contactName;
th.t9Name = toT9Digits(label);
th.lowerName = toLowerStr(label);
th.hasDraft = hasDraft;
th.messageCount = static_cast<int>(msgs.size());
if (!msgs.empty()) {
const auto& last = msgs.back();
th.lastIncoming = last.incoming;
th.lastDisplayTime = last.displayTime;
th.lastEpoch = last.epoch;
std::string prev = last.text;
for (char& c : prev) {
if (c == '\n' || c == '\r' || c == '\t') {
c = ' ';
}
}
th.lastPreview = hasDraft ? ("[Draft] " + draft) : prev;
} else {
th.lastIncoming = false;
th.lastPreview = "[Draft] " + draft;
struct stat dst {};
if (::stat(draftPath.c_str(), &dst) == 0) {
th.lastEpoch = dst.st_mtim.tv_sec;
}
}
if (th.lastEpoch == 0 && ::stat(smsPath.c_str(), &st) == 0) {
th.lastEpoch = st.st_mtim.tv_sec;
}
threads.push_back(th);
}
::closedir(d);
std::sort(threads.begin(), threads.end(),
[](const SmsThread& a, const SmsThread& b) {
if (a.lastEpoch != b.lastEpoch) {
return a.lastEpoch > b.lastEpoch;
}
return a.number < b.number;
});
return threads;
}
} // namespace bananarchy
#endif // DWM_PLUGINS_PHONE_DATA_H