/* gcc x11test-grabpointer.c -o x11test-grabpointer -lX11
 */

#include <X11/X.h>
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
 
int main(void) {
	Display *d;
	Window w, r;
	XEvent e;
	int s, status;

	d = XOpenDisplay(NULL);
	if (d == NULL) {
		fprintf(stderr, "Cannot open display\n");
		exit(1);
	}

	s = DefaultScreen(d);
	r = RootWindow(d, s);
	w = XCreateSimpleWindow(d, r, 10, 10, 100, 100, 1, BlackPixel(d, s), WhitePixel(d, s));
	XSelectInput(d, w, ExposureMask | KeyPressMask);
	XMapWindow(d, w);

	XNextEvent(d, &e);

	sleep(5);
	
	status =
		XGrabPointer(d, w, 1, 
			ButtonPressMask|ButtonReleaseMask|PointerMotionMask,
			GrabModeAsync, GrabModeAsync, r, None, CurrentTime);
	if (status != 0) {
		printf("Grab failed with error: %d\n", status);
		return 0;
	}
	printf("Pointer grabbed\n");

	sleep(5);

	XCloseDisplay(d);
	return 0;
}
