1 /* xscreensaver, Copyright (c) 1992, 1993 Jamie Zawinski <jwz@lucid.com>
3 * Permission to use, copy, modify, distribute, and sell this software and its
4 * documentation for any purpose is hereby granted without fee, provided that
5 * the above copyright notice appear in all copies and that both that
6 * copyright notice and this permission notice appear in supporting
7 * documentation. No representations are made about the suitability of this
8 * software for any purpose. It is provided "as is" without express or
12 /* This file contains code for grabbing an image of the screen to hack its
13 bits. This is a little tricky, since doing this involves the need to tell
14 the difference between drawing on the actual root window, and on the fake
15 root window used by the screensaver, since at this level the illusion
24 #include <X11/Xatom.h>
27 MapNotify_event_p (dpy, event, window)
32 return (event->xany.type == MapNotify &&
33 event->xvisibility.window == (Window) window);
38 screensaver_window_p (dpy, window)
44 unsigned long nitems, bytesafter;
46 if (XGetWindowProperty (dpy, window,
47 XInternAtom (dpy, "_SCREENSAVER_VERSION", False),
48 0, 1, False, XA_STRING,
49 &type, &format, &nitems, &bytesafter,
50 (unsigned char **) &version)
58 grab_screen_image (dpy, window, root_p)
64 XWindowAttributes xgwa;
66 XGetWindowAttributes (dpy, window, &xgwa);
68 if (screensaver_window_p (dpy, window))
70 /* note: this assumes vroot.h didn't encapsulate the XRootWindowOfScreen
71 function, only the RootWindowOfScreen macro... */
72 Window real_root = XRootWindowOfScreen (DefaultScreenOfDisplay (dpy));
74 XSetWindowBackgroundPixmap (dpy, window, None);
76 /* prevent random viewer of the screen saver (locker) from messing
77 with windows. We don't check whether it succeeded, because what
78 are our options, really... */
79 XGrabPointer (dpy, real_root, True, ButtonPressMask|ButtonReleaseMask,
80 GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
81 XGrabKeyboard (dpy, real_root, True, GrabModeSync, GrabModeAsync,
84 XUnmapWindow (dpy, window);
86 sleep (5); /* wait for everyone to swap in and handle exposes... */
87 XMapRaised (dpy, window);
89 XUngrabPointer (dpy, CurrentTime);
90 XUngrabKeyboard (dpy, CurrentTime);
98 gcv.function = GXcopy;
99 gcv.subwindow_mode = IncludeInferiors;
100 gc = XCreateGC (dpy, window, GCFunction | GCSubwindowMode, &gcv);
101 pixmap = XCreatePixmap(dpy, window, xgwa.width, xgwa.height, xgwa.depth);
102 XCopyArea (dpy, RootWindowOfScreen (xgwa.screen), pixmap, gc,
103 xgwa.x, xgwa.y, xgwa.width, xgwa.height, 0, 0);
105 XSetWindowBackgroundPixmap (dpy, window, pixmap);
110 XSetWindowBackgroundPixmap (dpy, window, None);
111 XMapWindow (dpy, window);
113 XIfEvent (dpy, &event, MapNotify_event_p, (XPointer) window);