http://slackware.bholcomb.com/slackware/slackware-11.0/source/xap/xscreensaver/xscree...
[xscreensaver] / utils / xdbe.c
1 /* xscreensaver, Copyright (c) 1998, 1999, 2006
2  *  by Jamie Zawinski <jwz@jwz.org>
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation.  No representations are made about the suitability of this
9  * software for any purpose.  It is provided "as is" without express or 
10  * implied warranty.
11  */
12
13 /* The XDBE (Double Buffering) extension is pretty tricky to use, since you
14    can get X errors at inconvenient times during initialization.  This file
15    contains a utility routine to make it easier to deal with.
16  */
17
18 #include "utils.h"
19 #include "xdbe.h"
20 #include "resources.h"          /* for get_string_resource() */
21
22 /* #define DEBUG */
23
24 #ifdef DEBUG
25 # include <X11/Xmu/Error.h>
26 #endif
27
28 extern char *progname;
29
30 #ifdef HAVE_DOUBLE_BUFFER_EXTENSION     /* whole file */
31
32 static Bool xdbe_got_x_error = False;
33 static int
34 xdbe_ehandler (Display *dpy, XErrorEvent *error)
35 {
36   xdbe_got_x_error = True;
37
38 #ifdef DEBUG
39   fprintf (stderr, "\n%s: ignoring X error from DOUBLE-BUFFER:\n", progname);
40   XmuPrintDefaultErrorMessage (dpy, error, stderr);
41   fprintf (stderr, "\n");
42 #endif
43
44   return 0;
45 }
46
47
48 XdbeBackBuffer 
49 xdbe_get_backbuffer (Display *dpy, Window window,
50                      XdbeSwapAction action)
51 {
52   XdbeBackBuffer b;
53   XErrorHandler old_handler;
54   int maj, min;
55
56   if (!get_boolean_resource(dpy, "useDBE", "Boolean"))
57     return 0;
58
59   if (!XdbeQueryExtension (dpy, &maj, &min))
60     return 0;
61
62   XSync (dpy, False);
63   xdbe_got_x_error = False;
64   old_handler = XSetErrorHandler (xdbe_ehandler);
65   b = XdbeAllocateBackBufferName(dpy, window, XdbeUndefined);
66   XSync (dpy, False);
67   XSetErrorHandler (old_handler);
68
69   if (xdbe_got_x_error)
70     return 0;
71
72   return b;
73 }
74
75 #endif /* HAVE_DOUBLE_BUFFER_EXTENSION */