From 1717b0ec2c63c4291532fc758b47dca0eccd480d Mon Sep 17 00:00:00 2001 From: r1w1s1 Date: Fri, 21 Aug 2026 00:17:08 -0300 Subject: [PATCH] xhidecursor: ignore modifier key presses --- main.c | 41 ++++++++++++++++++++++++++++++++++++----- xhidecursor.1 | 3 +-- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/main.c b/main.c index b5a0ed0..206bf61 100644 --- a/main.c +++ b/main.c @@ -1,3 +1,7 @@ +#include +#include +#include +#include #include #include @@ -18,29 +22,56 @@ void xi_select_events(const int event) { } int main(void) { + int fixes_event, fixes_error; + int xi_major = 2, xi_minor = 0; + if (!(d = XOpenDisplay(NULL))) { - printf("Couldn't open Display.\n"); + fprintf(stderr, "xhidecursor: couldn't open display %s\n", + XDisplayName(NULL)); + return 1; + } + if (!XFixesQueryExtension(d, &fixes_event, &fixes_error)) { + fprintf(stderr, "xhidecursor: XFixes extension is unavailable\n"); + XCloseDisplay(d); + return 1; + } + if (XIQueryVersion(d, &xi_major, &xi_minor) != Success) { + fprintf(stderr, "xhidecursor: XInput2 extension is unavailable\n"); + XCloseDisplay(d); return 1; } + r = XDefaultRootWindow(d); xi_select_events(XI_RawKeyPress); XEvent e; - XGenericEventCookie *c; - while (!XNextEvent(d, &e)) { - if (!XGetEventData(d, (c = &e.xcookie))) + while (XNextEvent(d, &e) == 0) { + if (e.type != GenericEvent || + !XGetEventData(d, &e.xcookie)) continue; + + XGenericEventCookie *c = &e.xcookie; switch (c->evtype) { - case XI_RawKeyPress: + case XI_RawKeyPress: { + XIRawEvent *raw = (XIRawEvent *)c->data; + KeySym keysym = XkbKeycodeToKeysym(d, (KeyCode)raw->detail, + 0, 0); + + if (IsModifierKey(keysym)) + break; + xi_select_events(XI_RawMotion); XFixesHideCursor(d, r); break; + } case XI_RawMotion: xi_select_events(XI_RawKeyPress); XFixesShowCursor(d, r); break; } XFreeEventData(d, c); + /* Synchronize selection changes and discard stale events. */ XSync(d, True); } XCloseDisplay(d); + return 0; } diff --git a/xhidecursor.1 b/xhidecursor.1 index 79799f2..adb0339 100644 --- a/xhidecursor.1 +++ b/xhidecursor.1 @@ -22,11 +22,10 @@ start it automatically: Place this line in ~/.xinitrc or your window manager startup file. .SH REQUIREMENTS -The XFixes extension must be available on the X server. +The XInput2 and XFixes extensions must be available on the X server. .SH SEE ALSO .BR XFixes (3) .SH AUTHORS xhidecursor was written by Aleksandrs Stier . -