Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Wrap mouse pointer at screen edges
/*
* Wrap mouse pointer at screen edges.
*
* Compile:
* $ cc -o mousewrap mouse_wrap.c -lX11 -lXi
*
* Usage:
* $ ./mwrap
*
* Copyright (C) 2012 Alessandro Ghedini <alessandro@ghedini.me>
* --------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* Alessandro Ghedini wrote this file. As long as you retain this
* notice you can do whatever you want with this stuff. If we
* meet some day, and you think this stuff is worth it, you can
* buy me a beer in return.
* --------------------------------------------------------------
*/
#include <X11/Xlib.h>
#include <X11/extensions/XInput2.h>
#define P_WARP(COND, X, Y) \
if (COND) { \
p_new_x = X; \
p_new_y = Y; \
goto warp; \
}
int main(int argc, char **argv) {
Display *dpy = XOpenDisplay(NULL);
Window root = DefaultRootWindow(dpy);
int screen = DefaultScreen(dpy);
int s_width = DisplayWidth(dpy, screen);
int s_height = DisplayHeight(dpy, screen);
int xi_op, first, error;
XQueryExtension(dpy, "XInputExtension", &xi_op, &first, &error);
unsigned char mask_bytes[XIMaskLen(XI_RawMotion)];
XISetMask(mask_bytes, XI_RawMotion);
XIEventMask mask;
mask.deviceid = XIAllMasterDevices;
mask.mask_len = sizeof(mask_bytes);
mask.mask = mask_bytes;
XISelectEvents(dpy, root, &mask, 1);
while (1) {
XEvent event;
XNextEvent(dpy, &event);
if ((event.xcookie.type == GenericEvent) &&
(event.xcookie.extension == xi_op)) {
XGetEventData(dpy, &event.xcookie);
if (event.xcookie.evtype == XI_RawMotion) {
unsigned int p_mask;
Window p_root, p_child;
int p_x, p_y, p_new_x, p_new_y,
p_win_x, p_win_y;
XQueryPointer(
dpy, root, &p_root, &p_child,
&p_x, &p_y, &p_win_x, &p_win_y, &p_mask
);
P_WARP((p_x == 0), s_width, p_y)
P_WARP((p_x == (s_width - 1)), 0, p_y)
P_WARP((p_y == 0), p_x, s_height);
P_WARP((p_y == (s_height - 1)), p_x, 0);
p_new_x = p_x;
p_new_y = p_y;
warp:
XWarpPointer(
dpy, None, root, 0, 0, 0, 0,
p_new_x, p_new_y
);
}
XFreeEventData(dpy, &event.xcookie);
}
}
XCloseDisplay(dpy);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.