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
@@ -18,6 +18,7 @@
|
||||
#endif
|
||||
|
||||
#include <algorithm>
|
||||
#include <csignal>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
@@ -205,6 +206,72 @@ class RemedyEmulator {
|
||||
int pendingSoftkey = 0; // 1=left, 2=right
|
||||
int64_t pendingSoftkeyDeadlineMs = 0;
|
||||
int64_t rightSoftPressMs = 0;
|
||||
bool grabYielded = false;
|
||||
int64_t overlayGraceUntilMs = 0;
|
||||
|
||||
static bool isPidAliveFile(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);
|
||||
}
|
||||
|
||||
static bool isExternalOverlayActive() {
|
||||
if (::access("/tmp/bananarchy_screen_locked", F_OK) == 0 ||
|
||||
::access("/tmp/dwm_plugin_dmenu.active", F_OK) == 0 ||
|
||||
::access("/tmp/sxmo_call_ringing", F_OK) == 0 ||
|
||||
::access("/tmp/sxmo_active_call", F_OK) == 0) {
|
||||
return true;
|
||||
}
|
||||
static const char* kOverlayPids[] = {
|
||||
"xlocker.pid",
|
||||
"xdrawer.pid",
|
||||
"xcallscreen.pid",
|
||||
"xdialer.pid",
|
||||
"xcalllog.pid",
|
||||
"xcontacts.pid",
|
||||
"xsms.pid",
|
||||
"xfiles.pid",
|
||||
"xcamera.pid",
|
||||
"xthemes.pid",
|
||||
"xmodem.pid",
|
||||
"xnetworks.pid",
|
||||
"xringtones.pid",
|
||||
"xwallpapers.pid",
|
||||
"xmusic.pid",
|
||||
"xsip.pid",
|
||||
"xemail.pid",
|
||||
"xirc.pid",
|
||||
"xtox.pid",
|
||||
"xsmallweb.pid",
|
||||
"xwebarchy.pid",
|
||||
"xgps.pid",
|
||||
"xpasscode.pid",
|
||||
"xengmenu.pid",
|
||||
};
|
||||
char uidDir[64];
|
||||
std::snprintf(uidDir, sizeof(uidDir), "/run/user/%u",
|
||||
static_cast<unsigned>(::getuid()));
|
||||
const char* envRdir = std::getenv("XDG_RUNTIME_DIR");
|
||||
for (const char* pf : kOverlayPids) {
|
||||
if (envRdir && envRdir[0] &&
|
||||
isPidAliveFile(std::string(envRdir) + "/" + pf)) {
|
||||
return true;
|
||||
}
|
||||
if (isPidAliveFile(std::string(uidDir) + "/" + pf)) {
|
||||
return true;
|
||||
}
|
||||
if (isPidAliveFile(std::string("/tmp/") + pf)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Minimum 80ms / 2-tick hold for Canvas key presses so polling game loops
|
||||
// (e.g. 58ms sleep in Castlevania / Tomb Raider) never miss brisk key taps
|
||||
@@ -282,13 +349,21 @@ class RemedyEmulator {
|
||||
|
||||
void grabKeyboard() {
|
||||
if (!neoWin.dpy || neoWin.win == None) return;
|
||||
if (isExternalOverlayActive()) {
|
||||
ungrabKeyboard();
|
||||
return;
|
||||
}
|
||||
bool grabbed = false;
|
||||
for (int i = 0; i < 15; ++i) {
|
||||
if (XGrabKeyboard(neoWin.dpy, neoWin.win, True, GrabModeAsync,
|
||||
GrabModeAsync, CurrentTime) == GrabSuccess) {
|
||||
grabbed = true;
|
||||
break;
|
||||
}
|
||||
usleep(10000);
|
||||
}
|
||||
grabYielded = !grabbed;
|
||||
XFlush(neoWin.dpy);
|
||||
}
|
||||
|
||||
void ungrabKeyboard() {
|
||||
@@ -296,27 +371,21 @@ class RemedyEmulator {
|
||||
XUngrabKeyboard(neoWin.dpy, CurrentTime);
|
||||
XFlush(neoWin.dpy);
|
||||
}
|
||||
grabYielded = true;
|
||||
}
|
||||
|
||||
void openXdrawerAndRestoreGrab() {
|
||||
cldcRuntime.sound.setPaused(true);
|
||||
ungrabKeyboard();
|
||||
std::system("dwm-plugin xdrawer >/dev/null 2>&1");
|
||||
usleep(120000);
|
||||
for (int wait = 0; wait < 600; ++wait) {
|
||||
usleep(50000);
|
||||
Window foc = None;
|
||||
int rev = 0;
|
||||
XGetInputFocus(neoWin.dpy, &foc, &rev);
|
||||
if (foc == neoWin.win || foc == PointerRoot || foc == None) break;
|
||||
overlayGraceUntilMs = Jvm::NowMs() + 300;
|
||||
int ffd = ::open("/tmp/dwm_plugin_xdrawer.fifo",
|
||||
O_WRONLY | O_NONBLOCK | O_CLOEXEC);
|
||||
if (ffd >= 0) {
|
||||
(void)::write(ffd, "TOGGLE\n", 7);
|
||||
::close(ffd);
|
||||
} else {
|
||||
std::system("dwm-plugin xdrawer >/dev/null 2>&1 &");
|
||||
}
|
||||
XRaiseWindow(neoWin.dpy, neoWin.win);
|
||||
XSetInputFocus(neoWin.dpy, neoWin.win, RevertToParent, CurrentTime);
|
||||
grabKeyboard();
|
||||
cldcRuntime.sound.refreshSystemVolume(true);
|
||||
cldcRuntime.sound.setPaused(mode != EmuUiMode::RUNNING);
|
||||
neoWin.checkReloadTheme();
|
||||
appCtx.repaintPending = true;
|
||||
}
|
||||
|
||||
void scanBrowser() {
|
||||
@@ -1224,23 +1293,15 @@ class RemedyEmulator {
|
||||
} else if (keycode == 124 || ks == XF86XK_PowerOff) {
|
||||
cldcRuntime.sound.setPaused(true);
|
||||
ungrabKeyboard();
|
||||
overlayGraceUntilMs = Jvm::NowMs() + 300;
|
||||
int ffd = ::open("/tmp/dwm_plugin_dmenu.fifo",
|
||||
O_WRONLY | O_NONBLOCK | O_CLOEXEC);
|
||||
if (ffd >= 0) {
|
||||
(void)::write(ffd, "OPEN power\n", 11);
|
||||
::close(ffd);
|
||||
} else {
|
||||
std::system("dmenu power >/dev/null 2>&1");
|
||||
std::system("dmenu power >/dev/null 2>&1 &");
|
||||
}
|
||||
usleep(120000);
|
||||
for (int wait = 0; wait < 600; ++wait) {
|
||||
if (::access("/tmp/dwm_plugin_dmenu.active", F_OK) != 0) break;
|
||||
usleep(50000);
|
||||
}
|
||||
XRaiseWindow(neoWin.dpy, neoWin.win);
|
||||
XSetInputFocus(neoWin.dpy, neoWin.win, RevertToParent, CurrentTime);
|
||||
grabKeyboard();
|
||||
appCtx.repaintPending = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1598,7 +1659,32 @@ class RemedyEmulator {
|
||||
bool running = true;
|
||||
while (running) {
|
||||
updateTimer(cfg.fpsLimit);
|
||||
cldcRuntime.sound.setPaused(mode != EmuUiMode::RUNNING);
|
||||
bool overlayNow =
|
||||
isExternalOverlayActive() || (Jvm::NowMs() < overlayGraceUntilMs);
|
||||
if (overlayNow) {
|
||||
if (!grabYielded) {
|
||||
ungrabKeyboard();
|
||||
}
|
||||
leftSoftDown = false;
|
||||
rightSoftDown = false;
|
||||
pendingSoftkey = 0;
|
||||
cldcRuntime.sound.setPaused(true);
|
||||
} else if (grabYielded) {
|
||||
XRaiseWindow(neoWin.dpy, neoWin.win);
|
||||
XSetInputFocus(neoWin.dpy, neoWin.win, RevertToParent, CurrentTime);
|
||||
grabKeyboard();
|
||||
cldcRuntime.sound.refreshSystemVolume(true);
|
||||
cldcRuntime.sound.setPaused(mode != EmuUiMode::RUNNING);
|
||||
neoWin.checkReloadTheme();
|
||||
appCtx.repaintPending = true;
|
||||
if (mode == EmuUiMode::BROWSER) renderBrowserMenu();
|
||||
else if (mode == EmuUiMode::PAUSED) renderPauseMenu();
|
||||
else if (mode == EmuUiMode::CONTROLS || mode == EmuUiMode::WAITING_KEY) {
|
||||
renderControlsMenu();
|
||||
}
|
||||
} else {
|
||||
cldcRuntime.sound.setPaused(mode != EmuUiMode::RUNNING);
|
||||
}
|
||||
while (XPending(neoWin.dpy) > 0) {
|
||||
XEvent ev;
|
||||
XNextEvent(neoWin.dpy, &ev);
|
||||
@@ -1609,13 +1695,36 @@ class RemedyEmulator {
|
||||
else if (mode == EmuUiMode::CONTROLS || mode == EmuUiMode::WAITING_KEY) {
|
||||
renderControlsMenu();
|
||||
}
|
||||
} else if (ev.type == VisibilityNotify) {
|
||||
if (isExternalOverlayActive() || (Jvm::NowMs() < overlayGraceUntilMs)) {
|
||||
ungrabKeyboard();
|
||||
cldcRuntime.sound.setPaused(true);
|
||||
} else if (ev.xvisibility.state != VisibilityUnobscured) {
|
||||
XRaiseWindow(neoWin.dpy, neoWin.win);
|
||||
XFlush(neoWin.dpy);
|
||||
}
|
||||
} else if (ev.type == FocusOut) {
|
||||
if (isExternalOverlayActive() || (Jvm::NowMs() < overlayGraceUntilMs)) {
|
||||
ungrabKeyboard();
|
||||
cldcRuntime.sound.setPaused(true);
|
||||
}
|
||||
} else if (ev.type == KeyPress) {
|
||||
if (isExternalOverlayActive() || (Jvm::NowMs() < overlayGraceUntilMs)) {
|
||||
ungrabKeyboard();
|
||||
cldcRuntime.sound.setPaused(true);
|
||||
continue;
|
||||
}
|
||||
KeySym ks = XLookupKeysym(&ev.xkey, 0);
|
||||
if (!handleKeyPress(ks, ev.xkey.keycode)) {
|
||||
running = false;
|
||||
break;
|
||||
}
|
||||
} else if (ev.type == KeyRelease) {
|
||||
if (isExternalOverlayActive() || (Jvm::NowMs() < overlayGraceUntilMs)) {
|
||||
ungrabKeyboard();
|
||||
cldcRuntime.sound.setPaused(true);
|
||||
continue;
|
||||
}
|
||||
if (XEventsQueued(neoWin.dpy, QueuedAfterReading)) {
|
||||
XEvent nev;
|
||||
XPeekEvent(neoWin.dpy, &nev);
|
||||
@@ -1645,8 +1754,8 @@ class RemedyEmulator {
|
||||
}
|
||||
if (!running) break;
|
||||
|
||||
if (mode == EmuUiMode::RUNNING && rightSoftDown && !leftSoftDown &&
|
||||
!chordTriggered && !rightSoftLongTriggered &&
|
||||
if (!overlayNow && mode == EmuUiMode::RUNNING && rightSoftDown &&
|
||||
!leftSoftDown && !chordTriggered && !rightSoftLongTriggered &&
|
||||
Jvm::NowMs() - rightSoftPressMs >= 500) {
|
||||
rightSoftLongTriggered = true;
|
||||
pendingSoftkey = 0;
|
||||
@@ -1657,7 +1766,8 @@ class RemedyEmulator {
|
||||
renderPauseMenu();
|
||||
}
|
||||
|
||||
if (pendingSoftkey != 0 && Jvm::NowMs() >= pendingSoftkeyDeadlineMs) {
|
||||
if (!overlayNow && pendingSoftkey != 0 &&
|
||||
Jvm::NowMs() >= pendingSoftkeyDeadlineMs) {
|
||||
int s = pendingSoftkey;
|
||||
pendingSoftkey = 0;
|
||||
if (!chordTriggered && !dispatchSoftkeyAction(s)) {
|
||||
@@ -1676,7 +1786,7 @@ class RemedyEmulator {
|
||||
uint64_t exp = 0;
|
||||
if (read(tfd, &exp, sizeof(exp)) < 0) {}
|
||||
neoWin.checkReloadTheme();
|
||||
if (mode == EmuUiMode::RUNNING) {
|
||||
if (mode == EmuUiMode::RUNNING && !overlayNow) {
|
||||
tickRunningMidlet();
|
||||
frameCounter++;
|
||||
int64_t now = Jvm::NowMs();
|
||||
|
||||
Reference in new issue
Block a user