404 lines
11 KiB
C++
404 lines
11 KiB
C++
// SPDX-License-Identifier: GPL-2.0-only
|
|
// Bananarchy Engineering Menu DWM Plugin (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>
|
|
#include <pango/pango.h>
|
|
#include <pango/pangoxft.h>
|
|
|
|
#include <algorithm>
|
|
#include <csignal>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <fcntl.h>
|
|
#include <poll.h>
|
|
#include <string>
|
|
#include <sys/wait.h>
|
|
#include <unistd.h>
|
|
#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;
|
|
}
|
|
|
|
struct EngMenuItem {
|
|
std::string title;
|
|
std::string subtitle;
|
|
std::string command;
|
|
bool runInTerminal = true;
|
|
};
|
|
|
|
void launchEngItemAsRoot(const EngMenuItem& item) {
|
|
if (::access("/tmp/bananarchy_duress_mode", F_OK) == 0 ||
|
|
isPhonePasscodeLocked() || item.command.empty()) {
|
|
return;
|
|
}
|
|
pid_t pid = ::fork();
|
|
if (pid == 0) {
|
|
if (::fork() == 0) {
|
|
::setsid();
|
|
int nullfd = ::open("/dev/null", O_RDWR);
|
|
if (nullfd >= 0) {
|
|
::dup2(nullfd, STDIN_FILENO);
|
|
::dup2(nullfd, STDOUT_FILENO);
|
|
::dup2(nullfd, STDERR_FILENO);
|
|
if (nullfd > 2) {
|
|
::close(nullfd);
|
|
}
|
|
}
|
|
if (item.runInTerminal) {
|
|
::execl("/usr/bin/bananarchy-engmenu-exec", "bananarchy-engmenu-exec",
|
|
"--term", item.title.c_str(), item.command.c_str(),
|
|
static_cast<char*>(nullptr));
|
|
::execl("/usr/bin/doas", "doas", "-n",
|
|
"/usr/bin/bananarchy-engmenu-exec", "--term",
|
|
item.title.c_str(), item.command.c_str(),
|
|
static_cast<char*>(nullptr));
|
|
} else {
|
|
::execl("/usr/bin/bananarchy-engmenu-exec", "bananarchy-engmenu-exec",
|
|
"--exec", item.command.c_str(), static_cast<char*>(nullptr));
|
|
::execl("/usr/bin/doas", "doas", "-n",
|
|
"/usr/bin/bananarchy-engmenu-exec", "--exec",
|
|
item.command.c_str(), static_cast<char*>(nullptr));
|
|
}
|
|
::_exit(0);
|
|
}
|
|
::_exit(0);
|
|
} else if (pid > 0) {
|
|
int st = 0;
|
|
(void)::waitpid(pid, &st, 0);
|
|
}
|
|
}
|
|
|
|
int engmenu_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 (!isPreload && ::access("/tmp/bananarchy_duress_mode", F_OK) == 0) {
|
|
return 0;
|
|
}
|
|
|
|
if (sendPluginCommand("engmenu", isPreload ? "NOP" : "OPEN")) {
|
|
return 0;
|
|
}
|
|
|
|
int daemonLockFd = acquirePluginDaemonLock("engmenu");
|
|
if (daemonLockFd < 0) {
|
|
for (int i = 0; i < 10; ++i) {
|
|
::usleep(30000);
|
|
if (sendPluginCommand("engmenu", isPreload ? "NOP" : "OPEN")) {
|
|
return 0;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
std::string daemonPidPath = getPluginDaemonPidPath("engmenu");
|
|
std::string activePidPath = getRuntimeFile("xengmenu.pid");
|
|
writeIntFile(daemonPidPath, static_cast<int>(::getpid()));
|
|
int fifoFd = setupPluginDaemonFifo("engmenu");
|
|
|
|
struct sigaction sa {};
|
|
sa.sa_handler = handleSignal;
|
|
sigemptyset(&sa.sa_mask);
|
|
sigaction(SIGTERM, &sa, nullptr);
|
|
sigaction(SIGINT, &sa, nullptr);
|
|
sigaction(SIGHUP, &sa, nullptr);
|
|
|
|
NeoWindow neo;
|
|
if (!neo.create("xengmenu", "XEngMenu", "Engineering Menu",
|
|
kWinX, kWinY, kWinWidth, kWinHeight, true,
|
|
ExposureMask | KeyPressMask | KeyReleaseMask |
|
|
VisibilityChangeMask | StructureNotifyMask)) {
|
|
if (fifoFd >= 0) {
|
|
::close(fifoFd);
|
|
}
|
|
::unlink(daemonPidPath.c_str());
|
|
return 1;
|
|
}
|
|
|
|
Display* dpy = neo.dpy;
|
|
Window win = neo.win;
|
|
Pixmap pix = neo.pix;
|
|
GC gc = neo.gc;
|
|
PangoLayout* layoutMain = neo.layoutMain;
|
|
PangoLayout* layoutSmall = neo.layoutSmall;
|
|
PluginTheme& theme = neo.theme;
|
|
|
|
// Intentionally empty for now; each item added here runs as root via
|
|
// /usr/bin/bananarchy-engmenu-exec (--term or --exec).
|
|
std::vector<EngMenuItem> items;
|
|
|
|
bool visible = false;
|
|
int selIdx = 0;
|
|
int scrollIdx = 0;
|
|
|
|
auto openWindow = [&]() {
|
|
if (::access("/tmp/bananarchy_duress_mode", F_OK) == 0 ||
|
|
isPhonePasscodeLocked()) {
|
|
return;
|
|
}
|
|
closeOtherPlugins("engmenu");
|
|
writeIntFile(activePidPath, static_cast<int>(::getpid()));
|
|
selIdx = 0;
|
|
scrollIdx = 0;
|
|
if (!visible) {
|
|
XMapRaised(dpy, win);
|
|
XMoveResizeWindow(dpy, win, kWinX, kWinY, kWinWidth, kWinHeight);
|
|
visible = true;
|
|
}
|
|
grabPluginKeyboard(dpy, win);
|
|
};
|
|
|
|
auto closeWindow = [&]() {
|
|
if (visible) {
|
|
XUngrabKeyboard(dpy, CurrentTime);
|
|
XUnmapWindow(dpy, win);
|
|
XFlush(dpy);
|
|
visible = false;
|
|
}
|
|
::unlink(activePidPath.c_str());
|
|
};
|
|
|
|
auto render = [&]() {
|
|
if (!visible) {
|
|
return;
|
|
}
|
|
neo.clearBg();
|
|
XSetForeground(dpy, gc, theme.spec.bgHeader);
|
|
XFillRectangle(dpy, pix, gc, 0, 0, kWinWidth, 26);
|
|
neo.drawPango(neo.layoutIcon, &theme.colYellow, 6, 0, 26, kIconSliders);
|
|
neo.drawPango(layoutMain, &theme.colWhite, 28, 0, 26,
|
|
"Engineering Menu", 160);
|
|
int rbw = neo.pangoWidth(layoutSmall, "ROOT");
|
|
neo.drawPango(layoutSmall, &theme.colRed, kWinWidth - rbw - 8, 0, 26,
|
|
"ROOT");
|
|
XSetForeground(dpy, gc, theme.spec.border);
|
|
XDrawLine(dpy, pix, gc, 0, 25, kWinWidth, 25);
|
|
|
|
if (items.empty()) {
|
|
int cardX = 14;
|
|
int cardY = 86;
|
|
int cardW = kWinWidth - 28;
|
|
int cardH = 92;
|
|
XSetForeground(dpy, gc, theme.spec.bgAltOdd);
|
|
XFillRectangle(dpy, pix, gc, cardX, cardY, cardW, cardH);
|
|
XSetForeground(dpy, gc, theme.spec.border);
|
|
XDrawRectangle(dpy, pix, gc, cardX, cardY, cardW - 1, cardH - 1);
|
|
|
|
neo.drawPango(layoutMain, &theme.colYellow, cardX + 8, cardY + 14, 22,
|
|
"Engineering Mode", cardW - 16, 1);
|
|
neo.drawPango(layoutSmall, &theme.colMuted, cardX + 8, cardY + 40, 18,
|
|
"No applets configured", cardW - 16, 1);
|
|
neo.drawPango(layoutSmall, &theme.colMuted, cardX + 8, cardY + 60, 18,
|
|
"Root runner ready (suid)", cardW - 16, 1);
|
|
} else {
|
|
constexpr int kRowY = 28;
|
|
constexpr int kRowH = 38;
|
|
constexpr int kMaxRows = 6;
|
|
if (selIdx < scrollIdx) {
|
|
scrollIdx = selIdx;
|
|
} else if (selIdx >= scrollIdx + kMaxRows) {
|
|
scrollIdx = selIdx - kMaxRows + 1;
|
|
}
|
|
for (int r = 0; r < kMaxRows; ++r) {
|
|
int idx = scrollIdx + r;
|
|
if (idx >= static_cast<int>(items.size())) {
|
|
break;
|
|
}
|
|
int ry = kRowY + r * kRowH;
|
|
bool selected = (idx == selIdx);
|
|
uint32_t rowBg = selected
|
|
? theme.spec.selBg
|
|
: ((idx % 2 == 0) ? theme.spec.bgAltEven
|
|
: theme.spec.bgAltOdd);
|
|
XSetForeground(dpy, gc, rowBg);
|
|
XFillRectangle(dpy, pix, gc, 0, ry, kWinWidth, kRowH);
|
|
XSetForeground(dpy, gc, theme.spec.border);
|
|
XDrawLine(dpy, pix, gc, 0, ry + kRowH - 1, kWinWidth, ry + kRowH - 1);
|
|
|
|
const auto& it = items[idx];
|
|
neo.drawPango(layoutMain,
|
|
selected ? &theme.colWhite : &theme.colLight,
|
|
8, ry + 2, 20, it.title, kWinWidth - 16);
|
|
neo.drawPango(layoutSmall,
|
|
selected ? &theme.colYellow : &theme.colMuted,
|
|
8, ry + 20, 16, it.subtitle, kWinWidth - 16);
|
|
}
|
|
neo.drawScrollbar(kWinWidth - 4, kRowY, kMaxRows * kRowH,
|
|
static_cast<int>(items.size()), kMaxRows, scrollIdx);
|
|
}
|
|
|
|
neo.drawSoftbar(items.empty() ? "" : "Run",
|
|
items.empty() ? "" : "Exec",
|
|
"Close", &theme.colYellow);
|
|
neo.present();
|
|
};
|
|
|
|
if (!isPreload) {
|
|
openWindow();
|
|
render();
|
|
}
|
|
|
|
int xfd = ConnectionNumber(dpy);
|
|
|
|
while (gRunning) {
|
|
if (fifoFd >= 0) {
|
|
for (const std::string& cmd : readFifoLines(fifoFd)) {
|
|
if (cmd == "NOP") {
|
|
continue;
|
|
}
|
|
if (cmd == "RELOAD_THEME") {
|
|
neo.reloadTheme();
|
|
if (visible) {
|
|
render();
|
|
}
|
|
continue;
|
|
}
|
|
if (cmd == "OPEN" || cmd.rfind("OPEN ", 0) == 0) {
|
|
openWindow();
|
|
render();
|
|
continue;
|
|
}
|
|
if (cmd == "CLOSE") {
|
|
closeWindow();
|
|
continue;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (visible && neo.checkReloadTheme()) {
|
|
render();
|
|
}
|
|
|
|
while (XPending(dpy) > 0) {
|
|
XEvent ev;
|
|
XNextEvent(dpy, &ev);
|
|
if (ev.type == Expose) {
|
|
if (visible && ev.xexpose.count == 0) {
|
|
render();
|
|
}
|
|
} else if (ev.type == VisibilityNotify && visible &&
|
|
ev.xvisibility.state != VisibilityUnobscured) {
|
|
if (isAnotherPluginActive("engmenu")) {
|
|
closeWindow();
|
|
} else {
|
|
XRaiseWindow(dpy, win);
|
|
grabPluginKeyboard(dpy, win);
|
|
render();
|
|
}
|
|
} else if (ev.type == KeyPress) {
|
|
if (!visible) {
|
|
continue;
|
|
}
|
|
unsigned int kc = ev.xkey.keycode;
|
|
KeySym sym = XLookupKeysym(&ev.xkey, 0);
|
|
|
|
if (querySoftkeysDown(dpy) == 3) {
|
|
writeSoftkeySuppress(nowMs() + 400);
|
|
closeWindow();
|
|
sendPluginCommand("xdrawer", "OPEN");
|
|
continue;
|
|
}
|
|
|
|
if (isPowerKey(sym, kc)) {
|
|
closeWindow();
|
|
openPowerMenuFromPlugin(dpy);
|
|
continue;
|
|
}
|
|
|
|
if (isRightSoftkey(sym, kc) || sym == XK_Escape ||
|
|
sym == XK_BackSpace || sym == XF86XK_HangupPhone ||
|
|
sym == XK_Cancel) {
|
|
writeSoftkeySuppress(nowMs() + 300);
|
|
closeWindow();
|
|
continue;
|
|
}
|
|
|
|
if (!items.empty()) {
|
|
if (sym == XK_Up || sym == XK_KP_Up) {
|
|
selIdx = (selIdx > 0) ? (selIdx - 1)
|
|
: (static_cast<int>(items.size()) - 1);
|
|
render();
|
|
continue;
|
|
}
|
|
if (sym == XK_Down || sym == XK_KP_Down) {
|
|
selIdx = (selIdx + 1 < static_cast<int>(items.size()))
|
|
? (selIdx + 1)
|
|
: 0;
|
|
render();
|
|
continue;
|
|
}
|
|
if (isLeftSoftkey(sym, kc) || sym == XK_Return ||
|
|
sym == XK_KP_Enter) {
|
|
if (selIdx >= 0 && selIdx < static_cast<int>(items.size())) {
|
|
EngMenuItem chosen = items[selIdx];
|
|
closeWindow();
|
|
launchEngItemAsRoot(chosen);
|
|
}
|
|
continue;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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, -1);
|
|
}
|
|
|
|
closeWindow();
|
|
if (fifoFd >= 0) {
|
|
::close(fifoFd);
|
|
}
|
|
if (daemonLockFd >= 0) {
|
|
::close(daemonLockFd);
|
|
}
|
|
::unlink(getPluginFifoPath("engmenu").c_str());
|
|
::unlink(daemonPidPath.c_str());
|
|
return 0;
|
|
}
|
|
|
|
int engmenu_plugin_init(const DwmHostApi* host) {
|
|
if (host && host->spawn_plugin) {
|
|
host->spawn_plugin("engmenu", "--preload");
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
extern "C" __attribute__((visibility("default"))) const DwmPlugin dwm_plugin_entry = {
|
|
DWM_PLUGIN_API_VERSION,
|
|
"engmenu",
|
|
"Engineering menu plugin",
|
|
engmenu_plugin_init,
|
|
nullptr,
|
|
nullptr,
|
|
nullptr,
|
|
nullptr,
|
|
engmenu_run_main
|
|
};
|