Files
bananarchy/src/dwm-plugins/calllog_plugin.cpp
T

1153 lines
38 KiB
C++

// SPDX-License-Identifier: GPL-2.0-only
// CallLog DWM Plugin - Preloaded resident X11 call log viewer, anti-spam blocklist & dialer launcher (240x320)
#include <X11/Xatom.h>
#include <X11/Xft/Xft.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
#include <X11/XF86keysym.h>
#ifndef _EVDEVK
#define _EVDEVK(_v) (0x10081000 + (_v))
#endif
#include <pango/pango.h>
#include <pango/pangoxft.h>
#include <algorithm>
#include <csignal>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fcntl.h>
#include <poll.h>
#include <string>
#include <vector>
#include "../sxmo-dwm/dwm_plugin.h"
#include "phone_data.h"
namespace {
using namespace bananarchy;
volatile sig_atomic_t gRunning = 1;
void handleSignal(int) {
gRunning = 0;
}
enum class ViewMode {
Log = 0,
Options = 1,
Blocklist = 2,
AddBlock = 3,
};
struct OptionEntry {
int id;
std::string icon;
std::string label;
bool danger = false;
};
int calllog_run_main(int argc, char** argv) {
gRunning = 1;
bool isPreload = false;
for (int i = 1; i < argc; ++i) {
if (argv[i] && std::strcmp(argv[i], "--preload") == 0) {
isPreload = true;
}
}
if (sendPluginCommand("calllog", isPreload ? "NOP" : "OPEN")) {
return 0;
}
std::string daemonPidPath = getPluginDaemonPidPath("calllog");
std::string activePidPath = getRuntimeFile("xcalllog.pid");
writeIntFile(daemonPidPath, static_cast<int>(::getpid()));
int fifoFd = setupPluginDaemonFifo("calllog");
struct sigaction sa {};
sa.sa_handler = handleSignal;
sigemptyset(&sa.sa_mask);
sigaction(SIGTERM, &sa, nullptr);
sigaction(SIGINT, &sa, nullptr);
NeoWindow neo;
if (!neo.create("xcalllog", "XCallLog", "Call Log")) {
if (fifoFd >= 0) ::close(fifoFd);
::unlink(daemonPidPath.c_str());
return 1;
}
Display* dpy = neo.dpy;
int screen = neo.screen;
Visual* visual = neo.visual;
Colormap cmap = neo.cmap;
Window win = neo.win;
Pixmap pix = neo.pix;
GC gc = neo.gc;
PangoLayout* layoutMain = neo.layoutMain;
PangoLayout* layoutSmall = neo.layoutSmall;
PangoLayout* layoutIcon = neo.layoutIcon;
PluginTheme& theme = neo.theme;
XftColor& colWhite = theme.colWhite;
XftColor& colLight = theme.colLight;
XftColor& colMuted = theme.colMuted;
XftColor& colYellow = theme.colYellow;
XftColor& colGreen = theme.colGreen;
XftColor& colBlue = theme.colBlue;
XftColor& colRed = theme.colRed;
ContactBook book;
BlockList blockList;
std::vector<CallLogItem> allLogs;
std::vector<CallLogItem> blockedLogs;
std::vector<CallLogItem> filtered;
FileStamp contactsStamp;
FileStamp logStamp;
FileStamp blockFileStamp;
FileStamp blockLogStamp;
constexpr int kNumTabs = 5;
const char* kTabLabels[kNumTabs] = {"All", "Miss", "In", "Out", "Spam"};
ViewMode mode = ViewMode::Log;
int filterTab = 0; // 0 = All, 1 = Missed, 2 = In, 3 = Out, 4 = Spam/Blocked
int selected = 0;
int optionSelected = 0;
int blockSelected = 0;
std::vector<OptionEntry> options;
std::string addBlockInput;
std::string toastMsg;
int64_t toastUntilMs = 0;
bool visible = false;
int64_t openStartMs = 0;
int64_t startupIgnoreUntilMs = 0;
bool initialSoftkeysReleased = true;
int softkeyPending = 0;
int64_t softkeyDeadlineMs = 0;
auto showToast = [&](const std::string& msg) {
toastMsg = msg;
toastUntilMs = nowMs() + 1800;
};
auto applyFilter = [&]() {
filtered.clear();
if (filterTab == 4) {
for (auto item : blockedLogs) {
item.isBlockedNumber = blockList.isBlocked(item.number);
filtered.push_back(item);
}
for (auto item : allLogs) {
if (blockList.isBlocked(item.number)) {
item.isBlockedNumber = true;
filtered.push_back(item);
}
}
} else {
for (auto item : allLogs) {
if (filterTab == 1 && item.direction != CallDirection::Missed) continue;
if (filterTab == 2 && item.direction != CallDirection::Incoming) continue;
if (filterTab == 3 && item.direction != CallDirection::Outgoing) continue;
item.isBlockedNumber = blockList.isBlocked(item.number);
filtered.push_back(item);
}
}
if (selected >= static_cast<int>(filtered.size())) {
selected = std::max(0, static_cast<int>(filtered.size()) - 1);
}
};
bool logsLoadedOnce = false;
auto reloadLogsIfNeeded = [&](bool force = false) -> bool {
bool cChanged = contactsStamp.checkAndUpdate(getContactsFilePath());
bool lChanged = logStamp.checkAndUpdate(getModemLogFilePath());
bool bChanged = blockFileStamp.checkAndUpdate(getBlockFilePath());
bool blChanged = blockLogStamp.checkAndUpdate(getBlockedModemLogFilePath());
if (force || cChanged || lChanged || bChanged || blChanged || !logsLoadedOnce) {
logsLoadedOnce = true;
book.load();
blockList.load();
allLogs = loadCallLog(book);
blockedLogs = loadBlockedCallLog(book);
applyFilter();
if (blockSelected >= static_cast<int>(blockList.entries().size())) {
blockSelected = std::max(0, static_cast<int>(blockList.entries().size()) - 1);
}
return true;
}
return false;
};
reloadLogsIfNeeded(true);
auto buildOptions = [&]() {
options.clear();
bool hasSel = (!filtered.empty() && selected >= 0 &&
selected < static_cast<int>(filtered.size()));
if (hasSel) {
const CallLogItem& item = filtered[selected];
bool isBlk = blockList.isBlocked(item.number);
options.push_back({1, kIconPhone, "Call Number", false});
options.push_back({2, kIconPhoneLog, "Open in Dialer", false});
options.push_back({3, kIconSms, "Send SMS", false});
if (isBlk) {
options.push_back({4, kIconCheck, "Unblock Number", false});
} else {
options.push_back({4, kIconBan, "Block Number (Spam)", true});
}
options.push_back({5, kIconUser, "Contacts", false});
} else {
options.push_back({2, kIconPhoneLog, "Open Dialer", false});
}
std::string blkCount =
"Blocklist (" + std::to_string(blockList.entries().size()) + ")";
options.push_back({6, kIconShield, blkCount, false});
options.push_back({7, kIconPlus, "Block a Number...", false});
if (filterTab == 4) {
if (!blockedLogs.empty()) {
options.push_back({8, kIconTrash, "Clear Spam Log", true});
}
} else if (!allLogs.empty()) {
options.push_back({9, kIconTrash, "Clear Call Log", true});
}
optionSelected = 0;
};
auto drawPango = [&](PangoLayout* layout, XftColor* color, int x, int y, int h,
const std::string& text, int maxW = -1) {
neo.drawPango(layout, color, x, y, h, text, maxW);
};
auto pangoWidth = [&](PangoLayout* layout, const std::string& text) -> int {
return neo.pangoWidth(layout, text);
};
constexpr int kHeaderH = 34;
constexpr int kSoftbarH = 26;
constexpr int kListY = kHeaderH;
constexpr int kListH = kWinHeight - kHeaderH - kSoftbarH;
constexpr int kVisibleRows = 5;
auto render = [&]() {
XSetForeground(dpy, gc, theme.spec.bgMain);
XFillRectangle(dpy, pix, gc, 0, 0, kWinWidth, kWinHeight);
XSetForeground(dpy, gc, theme.spec.bgHeader);
XFillRectangle(dpy, pix, gc, 0, 0, kWinWidth, kHeaderH);
std::string softLeft = "Options";
std::string softCenter = "CALL";
std::string softRight = "Close";
if (mode == ViewMode::Log) {
for (int t = 0; t < kNumTabs; ++t) {
int tx = (t * kWinWidth) / kNumTabs;
int tw = ((t + 1) * kWinWidth) / kNumTabs - tx;
bool active = (t == filterTab);
if (active) {
XSetForeground(dpy, gc, theme.spec.selBg);
XFillRectangle(dpy, pix, gc, tx, 0, tw, kHeaderH - 2);
XSetForeground(dpy, gc, (t == 4) ? theme.spec.danger : theme.spec.accent);
XFillRectangle(dpy, pix, gc, tx, kHeaderH - 3, tw, 3);
}
std::string lbl(kTabLabels[t]);
int lw = pangoWidth(layoutSmall, lbl);
XftColor* tabCol = active ? ((t == 4) ? &colRed : &colWhite) : &colMuted;
drawPango(layoutSmall, tabCol, tx + (tw - lw) / 2, 0, kHeaderH - 2, lbl);
}
XSetForeground(dpy, gc, theme.spec.border);
XFillRectangle(dpy, pix, gc, 0, kHeaderH - 1, kWinWidth, 1);
if (filtered.empty()) {
drawPango(layoutIcon, &colMuted, 112, kListY + 54, 24,
(filterTab == 4) ? kIconShield : kIconPhoneLog);
std::string emptyTitle =
(filterTab == 4) ? "No blocked calls" : "No call history";
int etw = pangoWidth(layoutMain, emptyTitle);
drawPango(layoutMain, &colMuted, std::max(8, (kWinWidth - etw) / 2),
kListY + 84, 26, emptyTitle, 224);
std::string emptySub =
(filterTab == 4)
? ("Blocklist: " + std::to_string(blockList.entries().size()) +
" number(s)")
: "Press 0-9 to dial or Options";
int esw = pangoWidth(layoutSmall, emptySub);
drawPango(layoutSmall, &colMuted, std::max(8, (kWinWidth - esw) / 2),
kListY + 114, 20, emptySub, 224);
} else {
int total = static_cast<int>(filtered.size());
int scrollOffset = 0;
if (selected >= kVisibleRows) {
scrollOffset = selected - kVisibleRows + 1;
}
if (scrollOffset > std::max(0, total - kVisibleRows)) {
scrollOffset = std::max(0, total - kVisibleRows);
}
for (int i = 0; i < kVisibleRows; ++i) {
int idx = scrollOffset + i;
if (idx >= total) {
break;
}
int ry = kListY + (i * kListH) / kVisibleRows;
int nextRy = kListY + ((i + 1) * kListH) / kVisibleRows;
int rowH = nextRy - ry;
bool isSel = (idx == selected);
XSetForeground(
dpy, gc,
isSel ? theme.spec.selBg
: ((i % 2 == 0) ? theme.spec.bgAltEven : theme.spec.bgAltOdd));
XFillRectangle(dpy, pix, gc, 0, ry, kWinWidth, rowH);
if (isSel) {
XSetForeground(dpy, gc, theme.spec.accent);
XFillRectangle(dpy, pix, gc, 0, ry, 4, rowH);
}
const CallLogItem& item = filtered[idx];
const char* dirIcon = kIconInCall;
XftColor* dirCol = &colGreen;
const char* dirName = "In";
if (item.direction == CallDirection::Blocked) {
dirIcon = kIconBan;
dirCol = &colRed;
dirName = "Blocked";
} else if (item.direction == CallDirection::Outgoing) {
dirIcon = kIconOutCall;
dirCol = &colBlue;
dirName = "Out";
} else if (item.direction == CallDirection::Missed) {
dirIcon = kIconMissedCall;
dirCol = &colRed;
dirName = "Missed";
}
drawPango(layoutIcon, dirCol, 8, ry, rowH, dirIcon);
std::string rightBadge = formatDuration(item.durationSec);
XftColor* badgeCol = isSel ? &colYellow : &colGreen;
if (item.isBlockedNumber) {
rightBadge = "BLK";
badgeCol = &colRed;
}
int durW = 0;
if (!rightBadge.empty()) {
durW = pangoWidth(layoutSmall, rightBadge);
drawPango(layoutSmall, badgeCol, kWinWidth - durW - 6, ry + 3, 22,
rightBadge);
}
std::string primary =
item.contactName.empty() ? item.number : item.contactName;
XftColor* priCol =
((item.direction == CallDirection::Missed ||
item.direction == CallDirection::Blocked ||
item.isBlockedNumber) &&
!isSel)
? &colRed
: (isSel ? &colWhite : &colLight);
int priMaxW = (durW > 0) ? (kWinWidth - 36 - durW - 6) : 200;
drawPango(layoutMain, priCol, 30, ry + 3, 22, primary, priMaxW);
int timeW = pangoWidth(layoutSmall, item.displayTime);
drawPango(layoutSmall, isSel ? &colWhite : &colMuted,
kWinWidth - timeW - 6, ry + 24, 19, item.displayTime);
std::string secondary =
!item.contactName.empty() ? item.number : std::string(dirName);
int secMaxW = std::max(60, kWinWidth - 36 - timeW - 8);
drawPango(layoutSmall, isSel ? &colYellow : &colMuted, 30, ry + 24,
19, secondary, secMaxW);
}
if (total > kVisibleRows) {
int sbH = std::max(16, (kListH * kVisibleRows) / total);
int sbY = kListY +
((kListH - sbH) * scrollOffset) / (total - kVisibleRows);
XSetForeground(dpy, gc, theme.spec.divider);
XFillRectangle(dpy, pix, gc, kWinWidth - 3, sbY, 3, sbH);
}
}
} else if (mode == ViewMode::Options) {
drawPango(layoutIcon, &colYellow, 8, 0, kHeaderH, kIconList);
drawPango(layoutMain, &colWhite, 28, 0, kHeaderH, "Call Log Options", 200);
XSetForeground(dpy, gc, theme.spec.border);
XFillRectangle(dpy, pix, gc, 0, kHeaderH - 1, kWinWidth, 1);
constexpr int kOptVis = 6;
int total = static_cast<int>(options.size());
int scrollOffset = 0;
if (optionSelected >= kOptVis) {
scrollOffset = optionSelected - kOptVis + 1;
}
int rowH = kListH / kOptVis;
for (int i = 0; i < kOptVis; ++i) {
int idx = scrollOffset + i;
if (idx >= total) break;
int ry = kListY + i * rowH;
bool isSel = (idx == optionSelected);
XSetForeground(
dpy, gc,
isSel ? theme.spec.selBg
: ((i % 2 == 0) ? theme.spec.bgAltEven : theme.spec.bgAltOdd));
XFillRectangle(dpy, pix, gc, 0, ry, kWinWidth, rowH);
if (isSel) {
XSetForeground(dpy, gc,
options[idx].danger ? theme.spec.danger : theme.spec.accent);
XFillRectangle(dpy, pix, gc, 0, ry, 4, rowH);
}
XftColor* icCol = options[idx].danger
? &colRed
: (isSel ? &colYellow : &colBlue);
XftColor* txCol = options[idx].danger
? &colRed
: (isSel ? &colWhite : &colLight);
drawPango(layoutIcon, icCol, 10, ry, rowH, options[idx].icon);
drawPango(layoutMain, txCol, 34, ry, rowH, options[idx].label,
kWinWidth - 42);
}
softLeft = "Select";
softCenter = "OK";
softRight = "Back";
} else if (mode == ViewMode::Blocklist) {
drawPango(layoutIcon, &colRed, 8, 0, kHeaderH, kIconBan);
std::string hdr =
"Blocklist (" + std::to_string(blockList.entries().size()) + ")";
drawPango(layoutMain, &colWhite, 28, 0, kHeaderH, hdr, 204);
XSetForeground(dpy, gc, theme.spec.border);
XFillRectangle(dpy, pix, gc, 0, kHeaderH - 1, kWinWidth, 1);
const auto& entries = blockList.entries();
if (entries.empty()) {
drawPango(layoutIcon, &colMuted, 112, kListY + 54, 24, kIconShield);
drawPango(layoutMain, &colMuted, 44, kListY + 84, 26,
"Blocklist is empty", 180);
drawPango(layoutSmall, &colMuted, 22, kListY + 114, 20,
"Left Softkey: Block a Number", 204);
} else {
int total = static_cast<int>(entries.size());
int scrollOffset = 0;
if (blockSelected >= kVisibleRows) {
scrollOffset = blockSelected - kVisibleRows + 1;
}
for (int i = 0; i < kVisibleRows; ++i) {
int idx = scrollOffset + i;
if (idx >= total) break;
int ry = kListY + (i * kListH) / kVisibleRows;
int nextRy = kListY + ((i + 1) * kListH) / kVisibleRows;
int rowH = nextRy - ry;
bool isSel = (idx == blockSelected);
XSetForeground(
dpy, gc,
isSel ? theme.spec.selBg
: ((i % 2 == 0) ? theme.spec.bgAltEven : theme.spec.bgAltOdd));
XFillRectangle(dpy, pix, gc, 0, ry, kWinWidth, rowH);
if (isSel) {
XSetForeground(dpy, gc, theme.spec.danger);
XFillRectangle(dpy, pix, gc, 0, ry, 4, rowH);
}
const auto& e = entries[idx];
std::string cname = book.resolveName(e.number);
std::string topLine = cname.empty() ? e.number : cname;
std::string subLine =
cname.empty() ? e.label : (e.number + " (" + e.label + ")");
drawPango(layoutIcon, &colRed, 8, ry, rowH, kIconBan);
drawPango(layoutMain, isSel ? &colWhite : &colLight, 30, ry + 3, 22,
topLine, kWinWidth - 38);
drawPango(layoutSmall, isSel ? &colYellow : &colMuted, 30, ry + 24,
19, subLine, kWinWidth - 38);
}
}
softLeft = "Add New";
softCenter = entries.empty() ? "" : "Unblock";
softRight = "Back";
} else if (mode == ViewMode::AddBlock) {
drawPango(layoutIcon, &colRed, 8, 0, kHeaderH, kIconBan);
drawPango(layoutMain, &colWhite, 28, 0, kHeaderH, "Block Phone Number", 204);
XSetForeground(dpy, gc, theme.spec.border);
XFillRectangle(dpy, pix, gc, 0, kHeaderH - 1, kWinWidth, 1);
drawPango(layoutSmall, &colMuted, 12, kListY + 18, 20,
"Enter number or prefix (e.g. +1800*):", kWinWidth - 24);
XSetForeground(dpy, gc, theme.spec.bgAltOdd);
XFillRectangle(dpy, pix, gc, 12, kListY + 44, kWinWidth - 24, 34);
XSetForeground(dpy, gc, theme.spec.accent);
XDrawRectangle(dpy, pix, gc, 12, kListY + 44, kWinWidth - 25, 33);
std::string disp = addBlockInput + "_";
drawPango(layoutMain, &colWhite, 18, kListY + 47, 28, disp, kWinWidth - 36);
drawPango(layoutSmall, &colMuted, 12, kListY + 90, 20,
"0-9 / * / #: Digits #: + prefix", kWinWidth - 24);
drawPango(layoutSmall, &colMuted, 12, kListY + 112, 20,
"Left/Down: Backspace", kWinWidth - 24);
softLeft = "Block";
softCenter = "SAVE";
softRight = "Cancel";
}
if (!toastMsg.empty() && nowMs() < toastUntilMs) {
int tw = std::min(kWinWidth - 16, pangoWidth(layoutSmall, toastMsg) + 20);
int tx = (kWinWidth - tw) / 2;
int ty = kWinHeight - kSoftbarH - 28;
XSetForeground(dpy, gc, theme.spec.selBg);
XFillRectangle(dpy, pix, gc, tx, ty, tw, 24);
XSetForeground(dpy, gc, theme.spec.accent);
XDrawRectangle(dpy, pix, gc, tx, ty, tw - 1, 23);
int textW = pangoWidth(layoutSmall, toastMsg);
drawPango(layoutSmall, &colYellow, tx + (tw - textW) / 2, ty + 2, 20,
toastMsg, tw - 8);
}
int sbY = kWinHeight - kSoftbarH;
XSetForeground(dpy, gc, theme.spec.bgFooter);
XFillRectangle(dpy, pix, gc, 0, sbY, kWinWidth, kSoftbarH);
XSetForeground(dpy, gc, theme.spec.border);
XFillRectangle(dpy, pix, gc, 0, sbY, kWinWidth, 1);
drawPango(layoutMain, &colLight, 6, sbY + 1, kSoftbarH - 1, softLeft);
if (!softCenter.empty()) {
int cw = pangoWidth(layoutMain, softCenter);
XftColor* cCol = &colGreen;
if (mode == ViewMode::Log && filtered.empty()) {
cCol = &colMuted;
} else if (mode == ViewMode::Blocklist) {
cCol = &colRed;
}
drawPango(layoutMain, cCol, (kWinWidth - cw) / 2, sbY + 1, kSoftbarH - 1,
softCenter);
}
int rw = pangoWidth(layoutMain, softRight);
drawPango(layoutMain, &colLight, kWinWidth - rw - 6, sbY + 1,
kSoftbarH - 1, softRight);
if (visible) {
XCopyArea(dpy, pix, win, gc, 0, 0, kWinWidth, kWinHeight, 0, 0);
XFlush(dpy);
}
};
auto executeOption = [&](int optId) {
bool hasSel = (!filtered.empty() && selected >= 0 &&
selected < static_cast<int>(filtered.size()));
std::string selNum = hasSel ? filtered[selected].number : "";
std::string selName = hasSel ? filtered[selected].contactName : "";
switch (optId) {
case 1: { // Call Number
if (!selNum.empty()) {
mode = ViewMode::Log;
writeSoftkeySuppress(nowMs() + 350);
XUngrabKeyboard(dpy, CurrentTime);
XUnmapWindow(dpy, win);
XFlush(dpy);
::unlink(activePidPath.c_str());
visible = false;
placePhoneCall(selNum);
}
break;
}
case 2: { // Open in Dialer
mode = ViewMode::Log;
XUngrabKeyboard(dpy, CurrentTime);
XUnmapWindow(dpy, win);
XFlush(dpy);
::unlink(activePidPath.c_str());
visible = false;
if (!sendPluginCommand("dialer",
selNum.empty() ? "OPEN" : ("OPEN " + selNum))) {
if (selNum.empty()) {
runAsyncArgv("xdialer");
} else {
runAsyncArgv("xdialer", selNum.c_str());
}
}
break;
}
case 3: { // Send SMS
if (!selNum.empty()) {
mode = ViewMode::Log;
XUngrabKeyboard(dpy, CurrentTime);
XUnmapWindow(dpy, win);
XFlush(dpy);
::unlink(activePidPath.c_str());
visible = false;
if (!sendPluginCommand("sms", "OPEN " + selNum)) {
runAsyncArgv("xsms", selNum.c_str());
}
}
break;
}
case 4: { // Block / Unblock Number
if (!selNum.empty()) {
if (blockList.isBlocked(selNum)) {
blockList.unblockNumber(selNum);
showToast("Unblocked " + selNum);
} else {
blockList.blockNumber(selNum, selName.empty() ? "Call Log" : selName);
showToast("Blocked " + selNum);
}
reloadLogsIfNeeded(true);
}
mode = ViewMode::Log;
render();
break;
}
case 5: { // Contacts
mode = ViewMode::Log;
XUngrabKeyboard(dpy, CurrentTime);
XUnmapWindow(dpy, win);
XFlush(dpy);
::unlink(activePidPath.c_str());
visible = false;
if (!sendPluginCommand("contacts", "OPEN")) {
runAsyncArgv("xcontacts");
}
break;
}
case 6: { // Manage Blocklist
blockList.load();
blockSelected = 0;
mode = ViewMode::Blocklist;
render();
break;
}
case 7: { // Block a Number...
addBlockInput = selNum;
mode = ViewMode::AddBlock;
render();
break;
}
case 8: { // Clear Spam Log
clearCallLogFile(getBlockedModemLogFilePath());
reloadLogsIfNeeded(true);
showToast("Cleared blocked call log");
mode = ViewMode::Log;
render();
break;
}
case 9: { // Clear Call Log
clearCallLogFile(getModemLogFilePath());
reloadLogsIfNeeded(true);
showToast("Cleared call history");
mode = ViewMode::Log;
render();
break;
}
default:
mode = ViewMode::Log;
render();
break;
}
};
// Pre-warm Pango/FreeType/Xrender glyph caches into offscreen Pixmap at startup
drawPango(layoutIcon, &colGreen, 0, 0, 20,
std::string(kIconInCall) + kIconOutCall + kIconMissedCall +
kIconPhoneLog + kIconBan + kIconShield);
render();
XSync(dpy, False);
auto showWindow = [&]() {
writeIntFile(activePidPath, static_cast<int>(::getpid()));
closeOtherPlugins("calllog");
bool themeChanged = theme.checkAndReload(
dpy, screen, visual, cmap, layoutMain, layoutSmall, layoutIcon,
nullptr, win);
if (!visible) {
openStartMs = nowMs();
startupIgnoreUntilMs = openStartMs + 90;
initialSoftkeysReleased = (querySoftkeysDown(dpy) == 0);
softkeyPending = 0;
softkeyDeadlineMs = 0;
mode = ViewMode::Log;
visible = true;
XMapRaised(dpy, win);
if (!themeChanged) {
XCopyArea(dpy, pix, win, gc, 0, 0, kWinWidth, kWinHeight, 0, 0);
}
grabPluginKeyboard(dpy, win);
if (reloadLogsIfNeeded() || themeChanged || filterTab != 0 || selected != 0) {
filterTab = 0;
selected = 0;
applyFilter();
render();
}
XFlush(dpy);
} else {
XRaiseWindow(dpy, win);
grabPluginKeyboard(dpy, win);
if (reloadLogsIfNeeded() || themeChanged) {
render();
}
}
};
auto hideWindow = [&](bool drainSoftkeys) {
if (!visible) {
return;
}
int xfd = ConnectionNumber(dpy);
if (drainSoftkeys) {
int64_t drainDeadline = nowMs() + 180;
while (nowMs() < drainDeadline && querySoftkeysDown(dpy) != 0) {
while (XPending(dpy)) {
XEvent ev;
XNextEvent(dpy, &ev);
}
struct pollfd pfd {};
pfd.fd = xfd;
pfd.events = POLLIN;
::poll(&pfd, 1, 10);
}
writeSoftkeySuppress(nowMs() + 300);
}
XUngrabKeyboard(dpy, CurrentTime);
XUnmapWindow(dpy, win);
XFlush(dpy);
::unlink(activePidPath.c_str());
visible = false;
softkeyPending = 0;
softkeyDeadlineMs = 0;
if (mode != ViewMode::Log || filterTab != 0 || selected != 0) {
mode = ViewMode::Log;
filterTab = 0;
selected = 0;
applyFilter();
render();
XFlush(dpy);
}
};
if (!isPreload) {
showWindow();
}
int xfd = ConnectionNumber(dpy);
while (gRunning) {
for (const std::string& cmd : readFifoLines(fifoFd)) {
if (cmd == "NOP") {
continue;
} else if (cmd == "CLOSE") {
hideWindow(false);
} else if (cmd.rfind("OPEN", 0) == 0) {
showWindow();
} else if (cmd == "RELOAD_THEME") {
theme.load(dpy, screen, visual, cmap, layoutMain, layoutSmall,
layoutIcon, nullptr, win);
reloadLogsIfNeeded(true);
render();
}
}
if (visible && !toastMsg.empty() && nowMs() >= toastUntilMs) {
toastMsg.clear();
render();
}
if (visible && !initialSoftkeysReleased && querySoftkeysDown(dpy) == 0) {
initialSoftkeysReleased = true;
}
while (XPending(dpy)) {
XEvent ev;
XNextEvent(dpy, &ev);
if (!visible) {
continue;
}
if (!initialSoftkeysReleased && querySoftkeysDown(dpy) == 0) {
initialSoftkeysReleased = true;
}
if (ev.type == Expose) {
if (ev.xexpose.count == 0) {
render();
}
} else if (ev.type == VisibilityNotify &&
ev.xvisibility.state != VisibilityUnobscured) {
if (isAnotherPluginActive("calllog")) {
hideWindow(false);
} else {
XRaiseWindow(dpy, win);
grabPluginKeyboard(dpy, win);
render();
}
} else if (ev.type == KeyRelease) {
KeySym sym = XLookupKeysym(&ev.xkey, 0);
if (sym == XK_Menu || sym == XF86XK_KbdLcdMenu1 ||
sym == XF86XK_Back || sym == XF86XK_KbdLcdMenu2 ||
ev.xkey.keycode == 135 || ev.xkey.keycode == 166) {
if (querySoftkeysDown(dpy) == 0) {
initialSoftkeysReleased = true;
}
}
} else if (ev.type == KeyPress) {
KeySym sym = XLookupKeysym(&ev.xkey, 0);
bool isLeftSoft = (sym == XK_Menu || sym == XF86XK_KbdLcdMenu1 ||
ev.xkey.keycode == 135);
bool isRightSoft = (sym == XF86XK_Back || sym == XF86XK_KbdLcdMenu2 ||
ev.xkey.keycode == 166);
if (isLeftSoft || isRightSoft) {
if (!initialSoftkeysReleased || nowMs() < startupIgnoreUntilMs) {
continue;
}
int which = (isLeftSoft ? 1 : 0) | (isRightSoft ? 2 : 0);
softkeyPending |= which | querySoftkeysDown(dpy);
if (softkeyPending == 3) {
writeSoftkeySuppress(nowMs() + 500);
hideWindow(false);
if (!sendPluginCommand("xdrawer", "TOGGLE")) {
runAsyncArgv("xdrawer");
}
break;
}
if (softkeyDeadlineMs == 0) {
softkeyDeadlineMs = nowMs() + 40;
}
continue;
}
if (isPowerKey(sym, ev.xkey.keycode)) {
hideWindow(false);
openPowerMenuFromPlugin(dpy);
continue;
}
if (mode == ViewMode::AddBlock) {
char dialCh = mapDialKey(sym, ev.xkey.keycode, ev.xkey.state);
if (dialCh == '#') {
if (addBlockInput.empty()) {
addBlockInput.push_back('+');
} else {
addBlockInput.push_back('#');
}
render();
continue;
} else if (dialCh != '\0') {
addBlockInput.push_back(dialCh);
render();
continue;
}
if (sym == XK_BackSpace || sym == XK_Left || sym == XK_Down) {
if (!addBlockInput.empty()) {
addBlockInput.pop_back();
render();
}
continue;
} else if (sym == XK_Escape) {
mode = ViewMode::Blocklist;
render();
continue;
} else if (sym == XK_Return || sym == XK_KP_Enter) {
std::string num = trimStr(addBlockInput);
if (!normalizePhone(num).empty()) {
blockList.blockNumber(num, "Manual");
reloadLogsIfNeeded(true);
showToast("Blocked " + num);
}
mode = ViewMode::Blocklist;
render();
continue;
}
continue;
}
if (mode == ViewMode::Options) {
switch (sym) {
case XK_Escape:
case XK_BackSpace:
case XK_Left:
mode = ViewMode::Log;
render();
break;
case XK_Up:
case XK_KP_Up:
case XK_k:
if (!options.empty()) {
optionSelected =
(optionSelected + static_cast<int>(options.size()) - 1) %
static_cast<int>(options.size());
render();
}
break;
case XK_Down:
case XK_KP_Down:
case XK_j:
if (!options.empty()) {
optionSelected =
(optionSelected + 1) % static_cast<int>(options.size());
render();
}
break;
case XK_Return:
case XK_KP_Enter:
if (!options.empty() && optionSelected >= 0 &&
optionSelected < static_cast<int>(options.size())) {
executeOption(options[optionSelected].id);
}
break;
default:
break;
}
continue;
}
if (mode == ViewMode::Blocklist) {
switch (sym) {
case XK_Escape:
case XK_BackSpace:
case XK_Left:
mode = ViewMode::Log;
render();
break;
case XK_Up:
case XK_KP_Up:
case XK_k:
if (!blockList.entries().empty()) {
int n = static_cast<int>(blockList.entries().size());
blockSelected = (blockSelected + n - 1) % n;
render();
}
break;
case XK_Down:
case XK_KP_Down:
case XK_j:
if (!blockList.entries().empty()) {
int n = static_cast<int>(blockList.entries().size());
blockSelected = (blockSelected + 1) % n;
render();
}
break;
case XK_Return:
case XK_KP_Enter:
if (!blockList.entries().empty() && blockSelected >= 0 &&
blockSelected < static_cast<int>(blockList.entries().size())) {
std::string num = blockList.entries()[blockSelected].number;
blockList.unblockNumber(num);
reloadLogsIfNeeded(true);
showToast("Unblocked " + num);
render();
}
break;
default:
break;
}
continue;
}
char dialCh = mapDialKey(sym, ev.xkey.keycode, ev.xkey.state);
if (dialCh != '\0') {
std::string arg(1, dialCh);
hideWindow(false);
if (!sendPluginCommand("dialer", "OPEN " + arg)) {
runAsyncArgv("xdialer", arg.c_str());
}
break;
}
switch (sym) {
case XK_Escape:
case XK_BackSpace:
case XK_q:
writeSoftkeySuppress(nowMs() + 300);
hideWindow(true);
break;
case XK_Left:
case XK_KP_Left:
case XK_h:
filterTab = (filterTab + kNumTabs - 1) % kNumTabs;
selected = 0;
applyFilter();
render();
break;
case XK_Right:
case XK_KP_Right:
case XK_l:
filterTab = (filterTab + 1) % kNumTabs;
selected = 0;
applyFilter();
render();
break;
case XK_Up:
case XK_KP_Up:
case XK_k:
if (!filtered.empty()) {
selected = (selected + static_cast<int>(filtered.size()) - 1) %
static_cast<int>(filtered.size());
render();
}
break;
case XK_Down:
case XK_KP_Down:
case XK_j:
if (!filtered.empty()) {
selected = (selected + 1) % static_cast<int>(filtered.size());
render();
}
break;
case XK_Return:
case XK_KP_Enter:
case XF86XK_Phone:
case XF86XK_PickupPhone:
if (nowMs() < startupIgnoreUntilMs) {
break;
}
if (!filtered.empty() && selected >= 0 &&
selected < static_cast<int>(filtered.size())) {
std::string num = filtered[selected].number;
writeSoftkeySuppress(nowMs() + 350);
hideWindow(true);
placePhoneCall(num);
} else if (filtered.empty()) {
if (filterTab == 4) {
blockList.load();
blockSelected = 0;
mode = ViewMode::Blocklist;
render();
} else {
hideWindow(false);
if (!sendPluginCommand("dialer", "OPEN")) {
runAsyncArgv("xdialer");
}
}
}
break;
default:
break;
}
}
}
if (visible && softkeyPending != 0 && nowMs() >= softkeyDeadlineMs) {
int comb = softkeyPending | querySoftkeysDown(dpy);
softkeyPending = 0;
softkeyDeadlineMs = 0;
if (comb == 3) {
writeSoftkeySuppress(nowMs() + 500);
hideWindow(false);
if (!sendPluginCommand("xdrawer", "TOGGLE")) {
runAsyncArgv("xdrawer");
}
} else if (comb == 1) {
if (mode == ViewMode::Log) {
buildOptions();
mode = ViewMode::Options;
render();
} else if (mode == ViewMode::Options) {
if (!options.empty() && optionSelected >= 0 &&
optionSelected < static_cast<int>(options.size())) {
executeOption(options[optionSelected].id);
}
} else if (mode == ViewMode::Blocklist) {
addBlockInput.clear();
mode = ViewMode::AddBlock;
render();
} else if (mode == ViewMode::AddBlock) {
std::string num = trimStr(addBlockInput);
if (!normalizePhone(num).empty()) {
blockList.blockNumber(num, "Manual");
reloadLogsIfNeeded(true);
showToast("Blocked " + num);
}
mode = ViewMode::Blocklist;
render();
}
} else if (comb == 2) {
if (mode == ViewMode::AddBlock) {
mode = ViewMode::Blocklist;
render();
} else if (mode == ViewMode::Options || mode == ViewMode::Blocklist) {
mode = ViewMode::Log;
render();
} else {
writeSoftkeySuppress(nowMs() + 350);
hideWindow(true);
}
}
}
int timeoutMs = -1;
if (visible) {
timeoutMs = 200;
if (softkeyDeadlineMs > 0) {
timeoutMs = static_cast<int>(std::max<int64_t>(5, softkeyDeadlineMs - nowMs()));
}
}
struct pollfd pfds[2] {};
pfds[0].fd = xfd;
pfds[0].events = POLLIN;
pfds[1].fd = fifoFd;
pfds[1].events = POLLIN;
::poll(pfds, (fifoFd >= 0) ? 2 : 1, timeoutMs);
}
hideWindow(false);
if (fifoFd >= 0) {
::close(fifoFd);
}
::unlink(getPluginFifoPath("calllog").c_str());
::unlink(daemonPidPath.c_str());
return 0;
}
int calllog_plugin_init(const DwmHostApi* host) {
if (host && host->spawn_plugin) {
host->spawn_plugin("calllog", "--preload");
}
return 0;
}
int calllog_on_global_key(const DwmHostApi* host, KeySym keysym,
unsigned int keycode, unsigned int state,
int is_press) {
(void)host;
if (!is_press || state != 0) {
return 0;
}
if (keysym == XF86XK_Phone || keysym == XF86XK_PickupPhone || keycode == 177) {
if (isCallRingingFast()) {
if (!sendPluginCommand("callscreen", "ACCEPT")) {
runAsyncArgv("xcallscreen", "accept");
}
return 1;
}
if (isCallActiveOrRingingFast()) {
if (!sendPluginCommand("callscreen", "OPEN")) {
runAsyncArgv("xcallscreen");
}
return 1;
}
if (!sendPluginCommand("calllog", "OPEN")) {
runAsyncArgv("xcalllog");
}
return 1;
}
return 0;
}
} // namespace
extern "C" __attribute__((visibility("default"))) const DwmPlugin dwm_plugin_entry = {
DWM_PLUGIN_API_VERSION,
"calllog",
"Preloaded resident X11 call log viewer, anti-spam blocklist & dialer launcher",
calllog_plugin_init,
nullptr,
nullptr,
nullptr,
calllog_on_global_key,
calllog_run_main
};