#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>

int main(int argc, char **argv)
{
	Display *d;
	char *p;
	int target_screen;
	Window target_root, current_root, current_w;
	int root_x, root_y, w_x, w_y;
	unsigned mask;
	XWindowAttributes target_attr, current_attr;

	if (argc != 2 || (target_screen = strtol(argv[1], &p, 0)) < 0 ||
		*p != 0)
		exit(1);

	d = XOpenDisplay("");
	if (d == NULL)
		exit(1);

	if (target_screen >= ScreenCount(d))
	{
		XBell(d, 0);
		XCloseDisplay(d);
		exit(1);
	}

	target_root = RootWindow(d, target_screen);

	if (XQueryPointer(d, target_root, &current_root, &current_w,
		&root_x, &root_y, &w_x, &w_y, &mask))
		return 0; /* already there */

	if (!XGetWindowAttributes(d, current_root, &current_attr))
		exit(1);
	if (!XGetWindowAttributes(d, target_root, &target_attr))
		exit(1);

	root_x = (long)root_x * target_attr.width / current_attr.width;
	root_y = (long)root_y * target_attr.height / current_attr.height;

	XWarpPointer(d, current_root, target_root, 0, 0, 0, 0, root_x, root_y);
	XCloseDisplay(d);
	return 0;
}
