#include <signal.h>
#include <stdio.h>
#include <sys/types.h>
#include <X/Xlib.h>

caddr_t *bitmap;

Display *Dest,*Target;
Window Victim;
WindowInfo Info;

void SetDone();
extern int done;

grab(d,w)
Display *d;
Window w;
{
	Window current;
	int x,y;

	Victim=w;
	Target=d;
	done=0;

	if ((Dest = XOpenDisplay(""))==NULL)
		Error("Can't connect to display");
	XQueryMouse(RootWindow,&x,&y,&current);
	if (current)
		XFocusKeyboard(current); 
	signal(SIGINT,SetDone);
	XSetDisplay(Target);
	XQueryWindow(Victim, &Info);
	bitmap = (caddr_t *) calloc(XYPixmapSize(Info.width, 
		 Info.height, Target -> dplanes) ,1 );
	while (!done) {
		GetPicture();
		if (!done)
			PutPicture();
		if (!done)
			sleep(1);
	}
	signal(SIGINT,SIG_DFL);
	XSetDisplay(Dest);
	Xrefresh();
	XFocusKeyboard(RootWindow);
	XFlush();
	XCloseDisplay(Dest);
	XSetDisplay(Target);
	free(bitmap);
	printf("\n\n");
}

GetPicture()
{
	XSetDisplay(Target);
	XPixmapGetXY(Victim, 0, 0, Info.width, Info.height, bitmap);
}

PutPicture()
{
	Pixmap map;
  
	XSetDisplay(Dest);
	map = XStorePixmapXY(Info.width, Info.height, bitmap);
	XPixmapPut(RootWindow, 0, 0, 0, 0,
	             Info.width, Info.height, map, GXcopy, Dest -> dplanes);
	XFlush();
	XFreePixmap(map);
}

