Preempt X11 keyboard grabs and window stacking when lockscreen is active
This commit is contained in:
1 parent
83dd79fe76
commit
f112b6ae72
78 files changed
+403
-53
No files matched your search
+156
-6
@@ -179,6 +179,59 @@ bool isCallActiveOrRingingInNeoui() {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool isPidFileAliveNeoui(const std::string& path) {
|
||||
int fd = ::open(path.c_str(), O_RDONLY | O_CLOEXEC);
|
||||
if (fd < 0) return false;
|
||||
char buf[32];
|
||||
ssize_t n = ::read(fd, buf, sizeof(buf) - 1);
|
||||
::close(fd);
|
||||
if (n <= 0) return false;
|
||||
buf[n] = '\0';
|
||||
int pid = std::atoi(buf);
|
||||
return (pid > 0 && ::kill(pid, 0) == 0);
|
||||
}
|
||||
|
||||
bool isRuntimePidAliveNeoui(const char* pidFile) {
|
||||
const char* envRdir = std::getenv("XDG_RUNTIME_DIR");
|
||||
if (envRdir && envRdir[0] &&
|
||||
isPidFileAliveNeoui(std::string(envRdir) + "/" + pidFile)) {
|
||||
return true;
|
||||
}
|
||||
char uidDir[64];
|
||||
std::snprintf(uidDir, sizeof(uidDir), "/run/user/%u/%s",
|
||||
static_cast<unsigned>(::getuid()), pidFile);
|
||||
if (isPidFileAliveNeoui(uidDir)) {
|
||||
return true;
|
||||
}
|
||||
return isPidFileAliveNeoui(std::string("/tmp/") + pidFile);
|
||||
}
|
||||
|
||||
bool isExternalOverlayOrLockActive(const std::string& appName) {
|
||||
if (appName != "callscreen" && appName != "xcallscreen") {
|
||||
if (isCallActiveOrRingingInNeoui() ||
|
||||
isRuntimePidAliveNeoui("xcallscreen.pid")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (appName != "locker" && appName != "xlocker") {
|
||||
if (::access("/tmp/bananarchy_screen_locked", F_OK) == 0 ||
|
||||
isRuntimePidAliveNeoui("xlocker.pid")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (appName != "xdrawer") {
|
||||
if (isRuntimePidAliveNeoui("xdrawer.pid")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (appName != "dmenu" && appName != "xdmenu") {
|
||||
if (::access("/tmp/dwm_plugin_dmenu.active", F_OK) == 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
int mapHardwareKey(KeySym sym, unsigned int keycode, unsigned int state) {
|
||||
if (keycode == 135 || sym == XK_Super_L || sym == XK_Menu ||
|
||||
static_cast<uint32_t>(sym) == _EVDEVK(0x8b)) {
|
||||
@@ -1406,7 +1459,7 @@ bool NeoWindow::create(const char* resName, const char* resClass,
|
||||
swa.override_redirect = overrideRedirect ? True : False;
|
||||
swa.background_pixel = 0x222222;
|
||||
swa.border_pixel = 0x005577;
|
||||
swa.event_mask = eventMask;
|
||||
swa.event_mask = eventMask | VisibilityChangeMask | FocusChangeMask;
|
||||
|
||||
win = XCreateWindow(
|
||||
dpy, root, x, y, width, height, 0, DefaultDepth(dpy, screen),
|
||||
@@ -1502,6 +1555,8 @@ struct NeoContext {
|
||||
bool initialSoftkeysReleased = true;
|
||||
int softkeyPending = 0;
|
||||
int64_t softkeyDeadlineMs = 0;
|
||||
bool grabYielded = false;
|
||||
int64_t overlayGraceUntilMs = 0;
|
||||
|
||||
// Last polled event
|
||||
int lastEventType = NEO_EV_NONE;
|
||||
@@ -1587,6 +1642,16 @@ namespace bananarchy {
|
||||
void NeoWindow::bindAsActiveContext() {
|
||||
NeoContext* ctx = ensureCtx();
|
||||
ctx->w = this;
|
||||
if (ctx->appName.empty() && dpy && win != None) {
|
||||
XClassHint ch{};
|
||||
if (XGetClassHint(dpy, win, &ch)) {
|
||||
if (ch.res_name && ch.res_name[0]) {
|
||||
ctx->appName = ch.res_name;
|
||||
}
|
||||
if (ch.res_name) XFree(ch.res_name);
|
||||
if (ch.res_class) XFree(ch.res_class);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace bananarchy
|
||||
@@ -1698,16 +1763,26 @@ int neoui_is_visible(void) {
|
||||
int neoui_raise_and_grab(void) {
|
||||
NeoContext* ctx = ensureCtx();
|
||||
if (!ctx->w || !ctx->w->dpy || ctx->w->win == None) return 0;
|
||||
if (bananarchy::isExternalOverlayOrLockActive(ctx->appName)) {
|
||||
XUngrabKeyboard(ctx->w->dpy, CurrentTime);
|
||||
XFlush(ctx->w->dpy);
|
||||
ctx->grabYielded = true;
|
||||
return 0;
|
||||
}
|
||||
XRaiseWindow(ctx->w->dpy, ctx->w->win);
|
||||
XSetInputFocus(ctx->w->dpy, ctx->w->win, RevertToPointerRoot, CurrentTime);
|
||||
bool grabbed = false;
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
if (XGrabKeyboard(ctx->w->dpy, ctx->w->win, True, GrabModeAsync,
|
||||
GrabModeAsync, CurrentTime) == GrabSuccess) {
|
||||
grabbed = true;
|
||||
break;
|
||||
}
|
||||
::usleep(12000);
|
||||
}
|
||||
return 1;
|
||||
ctx->grabYielded = !grabbed;
|
||||
XFlush(ctx->w->dpy);
|
||||
return grabbed ? 1 : 0;
|
||||
}
|
||||
|
||||
int neoui_ungrab(void) {
|
||||
@@ -1715,6 +1790,8 @@ int neoui_ungrab(void) {
|
||||
if (!ctx->w || !ctx->w->dpy) return 0;
|
||||
XUngrabKeyboard(ctx->w->dpy, CurrentTime);
|
||||
XSetInputFocus(ctx->w->dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
|
||||
ctx->grabYielded = true;
|
||||
XFlush(ctx->w->dpy);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -2645,6 +2722,38 @@ int neoui_poll_event(int timeout_ms) {
|
||||
|
||||
int64_t now = bananarchy::monotonicMs();
|
||||
|
||||
auto syncOverlayGrabState = [&]() -> int {
|
||||
int64_t tNow = bananarchy::monotonicMs();
|
||||
bool overlayActive =
|
||||
ctx->visible &&
|
||||
(bananarchy::isExternalOverlayOrLockActive(ctx->appName) ||
|
||||
tNow < ctx->overlayGraceUntilMs);
|
||||
if (overlayActive) {
|
||||
if (!ctx->grabYielded) {
|
||||
XUngrabKeyboard(dpy, CurrentTime);
|
||||
XFlush(dpy);
|
||||
ctx->grabYielded = true;
|
||||
}
|
||||
ctx->softkeyPending = 0;
|
||||
return NEO_EV_NONE;
|
||||
}
|
||||
if (ctx->visible && ctx->grabYielded) {
|
||||
neoui_raise_and_grab();
|
||||
if (!ctx->grabYielded) {
|
||||
ctx->startupIgnoreUntilMs = tNow + 200;
|
||||
ctx->initialSoftkeysReleased =
|
||||
(bananarchy::querySoftkeysMask(dpy) == 0);
|
||||
ctx->softkeyPending = 0;
|
||||
ctx->lastEventType = NEO_EV_EXPOSE;
|
||||
return NEO_EV_EXPOSE;
|
||||
}
|
||||
}
|
||||
return NEO_EV_NONE;
|
||||
};
|
||||
|
||||
int ovEv = syncOverlayGrabState();
|
||||
if (ovEv != NEO_EV_NONE) return ovEv;
|
||||
|
||||
// Check if multi-tap timer expired
|
||||
if (ctx->tapActive && (now - ctx->lastTapMs >= 850)) {
|
||||
ctx->tapActive = false;
|
||||
@@ -2693,6 +2802,11 @@ int neoui_poll_event(int timeout_ms) {
|
||||
XNextEvent(dpy, &ev);
|
||||
if (ev.type == Expose) {
|
||||
while (XCheckTypedWindowEvent(dpy, ctx->w->win, Expose, &ev)) {}
|
||||
if (ctx->visible && ctx->grabYielded &&
|
||||
!bananarchy::isExternalOverlayOrLockActive(ctx->appName) &&
|
||||
bananarchy::monotonicMs() >= ctx->overlayGraceUntilMs) {
|
||||
neoui_raise_and_grab();
|
||||
}
|
||||
ctx->lastEventType = NEO_EV_EXPOSE;
|
||||
return NEO_EV_EXPOSE;
|
||||
}
|
||||
@@ -2705,13 +2819,42 @@ int neoui_poll_event(int timeout_ms) {
|
||||
continue;
|
||||
}
|
||||
if (ev.type == VisibilityNotify && ctx->visible) {
|
||||
if (ev.xvisibility.state != VisibilityUnobscured) {
|
||||
if (bananarchy::isExternalOverlayOrLockActive(ctx->appName) ||
|
||||
bananarchy::monotonicMs() < ctx->overlayGraceUntilMs) {
|
||||
XUngrabKeyboard(dpy, CurrentTime);
|
||||
XFlush(dpy);
|
||||
ctx->grabYielded = true;
|
||||
} else if (ev.xvisibility.state != VisibilityUnobscured) {
|
||||
XRaiseWindow(dpy, ctx->w->win);
|
||||
XFlush(dpy);
|
||||
} else if (ctx->grabYielded) {
|
||||
neoui_raise_and_grab();
|
||||
ctx->startupIgnoreUntilMs = bananarchy::monotonicMs() + 200;
|
||||
ctx->initialSoftkeysReleased =
|
||||
(bananarchy::querySoftkeysMask(dpy) == 0);
|
||||
ctx->softkeyPending = 0;
|
||||
ctx->lastEventType = NEO_EV_EXPOSE;
|
||||
return NEO_EV_EXPOSE;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (ev.type == FocusOut && ctx->visible) {
|
||||
if (bananarchy::isExternalOverlayOrLockActive(ctx->appName) ||
|
||||
bananarchy::monotonicMs() < ctx->overlayGraceUntilMs) {
|
||||
XUngrabKeyboard(dpy, CurrentTime);
|
||||
XFlush(dpy);
|
||||
ctx->grabYielded = true;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (ev.type == KeyPress || ev.type == KeyRelease) {
|
||||
if (bananarchy::isExternalOverlayOrLockActive(ctx->appName) ||
|
||||
bananarchy::monotonicMs() < ctx->overlayGraceUntilMs) {
|
||||
XUngrabKeyboard(dpy, CurrentTime);
|
||||
XFlush(dpy);
|
||||
ctx->grabYielded = true;
|
||||
continue;
|
||||
}
|
||||
KeySym sym = XLookupKeysym(&ev.xkey, 0);
|
||||
unsigned int kc = ev.xkey.keycode;
|
||||
int nkey = bananarchy::mapHardwareKey(sym, kc, ev.xkey.state);
|
||||
@@ -2740,10 +2883,10 @@ int neoui_poll_event(int timeout_ms) {
|
||||
}
|
||||
if (::access("/tmp/bananarchy_screen_locked", F_OK) != 0) {
|
||||
neoui_ungrab();
|
||||
ctx->overlayGraceUntilMs = tNow + 250;
|
||||
XFlush(dpy);
|
||||
bananarchy::sendPluginFifoMsg("dmenu", "OPEN power");
|
||||
ctx->lastEventType = NEO_EV_CLOSE_REQ;
|
||||
return NEO_EV_CLOSE_REQ;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (ctx->chordXdrawer &&
|
||||
@@ -2754,6 +2897,7 @@ int neoui_poll_event(int timeout_ms) {
|
||||
if (ctx->softkeyPending == otherBit || (kmMask & otherBit)) {
|
||||
ctx->softkeyPending = 0;
|
||||
neoui_ungrab();
|
||||
ctx->overlayGraceUntilMs = tNow + 250;
|
||||
bananarchy::sendPluginFifoMsg("xdrawer", "TOGGLE");
|
||||
ctx->lastEventType = NEO_EV_SOFTKEY_CHORD;
|
||||
return NEO_EV_SOFTKEY_CHORD;
|
||||
@@ -2789,6 +2933,9 @@ int neoui_poll_event(int timeout_ms) {
|
||||
|
||||
// Calculate effective poll timeout
|
||||
int waitMs = timeout_ms;
|
||||
if (ctx->visible && ctx->grabYielded && waitMs != 0) {
|
||||
if (waitMs < 0 || waitMs > 100) waitMs = 100;
|
||||
}
|
||||
if (ctx->softkeyPending != 0) {
|
||||
int rem = static_cast<int>(
|
||||
std::max<int64_t>(1, ctx->softkeyDeadlineMs - bananarchy::monotonicMs()));
|
||||
@@ -2857,7 +3004,10 @@ int neoui_poll_event(int timeout_ms) {
|
||||
return NEO_EV_EXPOSE;
|
||||
}
|
||||
|
||||
return processNextXEvent();
|
||||
ovEv = syncOverlayGrabState();
|
||||
int postEv = processNextXEvent();
|
||||
if (postEv != NEO_EV_NONE) return postEv;
|
||||
return ovEv;
|
||||
}
|
||||
|
||||
int neoui_event_key(void) {
|
||||
|
||||
+1
-1
@@ -513,7 +513,7 @@ class NeoWindow {
|
||||
bool overrideRedirect = true,
|
||||
long eventMask = ExposureMask | KeyPressMask | KeyReleaseMask |
|
||||
ButtonPressMask | VisibilityChangeMask |
|
||||
StructureNotifyMask);
|
||||
FocusChangeMask | StructureNotifyMask);
|
||||
void destroy();
|
||||
|
||||
void bindAsActiveContext();
|
||||
|
||||
Reference in new issue
Block a user