ftp://ftp.zenez.com/pub/SCO/Skunk96/UnixWare/FreeBird/x11/utils/xscreensaver-1.18...
[xscreensaver] / utils / grabscreen.c
1 /* xscreensaver, Copyright (c) 1992, 1993 Jamie Zawinski <jwz@lucid.com>
2  *
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 
9  * implied warranty.
10  */
11
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 
16    breaks down...
17  */
18
19 #if __STDC__
20 #include <stdlib.h>
21 #endif
22
23 #include <X11/Xlib.h>
24 #include <X11/Xatom.h>
25
26 static Bool
27 MapNotify_event_p (dpy, event, window)
28      Display *dpy;
29      XEvent *event;
30      XPointer window;
31 {
32   return (event->xany.type == MapNotify &&
33           event->xvisibility.window == (Window) window);
34 }
35
36
37 static Bool
38 screensaver_window_p (dpy, window)
39      Display *dpy;
40      Window window;
41 {
42   Atom type;
43   int format;
44   unsigned long nitems, bytesafter;
45   char *version;
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)
51       == Success
52       && type != None)
53     return True;
54   return False;
55 }
56
57 Pixmap
58 grab_screen_image (dpy, window, root_p)
59      Display *dpy;
60      Window window;
61      int root_p;
62 {
63   Pixmap pixmap = 0;
64   XWindowAttributes xgwa;
65
66   XGetWindowAttributes (dpy, window, &xgwa);
67
68   if (screensaver_window_p (dpy, window))
69     {
70       /* note: this assumes vroot.h didn't encapsulate the XRootWindowOfScreen
71          function, only the RootWindowOfScreen macro... */
72       Window real_root = XRootWindowOfScreen (DefaultScreenOfDisplay (dpy));
73
74       XSetWindowBackgroundPixmap (dpy, window, None);
75
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,
82                      CurrentTime);
83
84       XUnmapWindow (dpy, window);
85       XSync (dpy, True);
86       sleep (5);     /* wait for everyone to swap in and handle exposes... */
87       XMapRaised (dpy, window);
88
89       XUngrabPointer (dpy, CurrentTime);
90       XUngrabKeyboard (dpy, CurrentTime);
91
92       XSync (dpy, True);
93     }
94   else if (root_p)
95     {
96       XGCValues gcv;
97       GC gc;
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);
104       XFreeGC (dpy, gc);
105       XSetWindowBackgroundPixmap (dpy, window, pixmap);
106     }
107   else
108     {
109       XEvent event;
110       XSetWindowBackgroundPixmap (dpy, window, None);
111       XMapWindow (dpy, window);
112       XFlush (dpy);
113       XIfEvent (dpy, &event, MapNotify_event_p, (XPointer) window);
114       XSync (dpy, True);
115     }
116   return pixmap;
117 }