ftp://ftp.smr.ru/pub/0/FreeBSD/releases/distfiles/xscreensaver-3.16.tar.gz
[xscreensaver] / utils / xdbe.c
1 /* xscreensaver, Copyright (c) 1998, 1999 by Jamie Zawinski <jwz@jwz.org>
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 /* The XDBE (Double Buffering) extension is pretty tricky to use, since you
13    can get X errors at inconvenient times during initialization.  This file
14    contains a utility routine to make it easier to deal with.
15  */
16
17 #include "utils.h"
18 #include "xdbe.h"
19 #include "resources.h"          /* for get_string_resource() */
20
21 /* #define DEBUG */
22
23 #ifdef DEBUG
24 # include <X11/Xmu/Error.h>
25 #endif
26
27 extern char *progname;
28
29 #ifdef HAVE_DOUBLE_BUFFER_EXTENSION     /* whole file */
30
31 static Bool xdbe_got_x_error = False;
32 static int
33 xdbe_ehandler (Display *dpy, XErrorEvent *error)
34 {
35   xdbe_got_x_error = True;
36
37 #ifdef DEBUG
38   fprintf (stderr, "\n%s: ignoring X error from DOUBLE-BUFFER:\n", progname);
39   XmuPrintDefaultErrorMessage (dpy, error, stderr);
40   fprintf (stderr, "\n");
41 #endif
42
43   return 0;
44 }
45
46
47 XdbeBackBuffer 
48 xdbe_get_backbuffer (Display *dpy, Window window,
49                      XdbeSwapAction action)
50 {
51   XdbeBackBuffer b;
52   XErrorHandler old_handler;
53   int maj, min;
54
55   if (!get_boolean_resource("useDBE", "Boolean"))
56     return 0;
57
58   if (!XdbeQueryExtension (dpy, &maj, &min))
59     return 0;
60
61   XSync (dpy, False);
62   xdbe_got_x_error = False;
63   old_handler = XSetErrorHandler (xdbe_ehandler);
64   b = XdbeAllocateBackBufferName(dpy, window, XdbeUndefined);
65   XSync (dpy, False);
66   XSetErrorHandler (old_handler);
67
68   if (xdbe_got_x_error)
69     return 0;
70
71   return b;
72 }
73
74 #endif /* HAVE_DOUBLE_BUFFER_EXTENSION */