Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 36 additions & 5 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#include <X11/XKBlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/Xfixes.h>
#include <X11/extensions/XInput2.h>

#include <stdio.h>
Expand All @@ -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;
}
3 changes: 1 addition & 2 deletions xhidecursor.1
Original file line number Diff line number Diff line change
Expand Up @@ -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 <aleks.stier@icloud.com>.