1 /*
2  *	x11.c
3  *	X11-specific stuff
4  *	AYM 1999-08-03
5  */
6 
7 
8 /*
9 This file is part of Yadex.
10 
11 Yadex incorporates code from DEU 5.21 that was put in the public domain in
12 1994 by Rapha�l Quinet and Brendon Wyber.
13 
14 The rest of Yadex is Copyright � 1997-2003 Andr� Majorel and others.
15 
16 This program is free software; you can redistribute it and/or modify it under
17 the terms of the GNU General Public License as published by the Free Software
18 Foundation; either version 2 of the License, or (at your option) any later
19 version.
20 
21 This program is distributed in the hope that it will be useful, but WITHOUT
22 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
23 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
24 
25 You should have received a copy of the GNU General Public License along with
26 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
27 Place, Suite 330, Boston, MA 02111-1307, USA.
28 */
29 
30 
31 #include "yadex.h"
32 #ifdef Y_X11
33 #include <X11/Xlib.h>
34 #include <X11/Xutil.h>
35 #include "gfx.h"
36 #include "x11.h"
37 
38 
39 static bool _have_error = false;
40 static unsigned char _error_code;
41 
42 
43 /*
44  *	x_bell
45  *	Ring the bell
46  */
x_bell()47 void x_bell ()
48 {
49   if (dpy)
50     XBell (dpy, 0);
51   else
52     nf_bug ("x_bell: not connected");
53 }
54 
55 
56 /*
57  *	x_catch_on
58  *	Setup things so that from now on, any X protocol errors
59  *	will be handled by x_error_handler instead of the
60  *	default handler (that has the annoying property of
61  *	calling exit()).
62  */
x_catch_on()63 void x_catch_on ()
64 {
65   XSetErrorHandler (x_error_handler);
66   XSynchronize (dpy, True);
67   x_clear_error ();
68 }
69 
70 
71 /*
72  *	x_catch_off
73  *	Restore the default error handler.
74  */
x_catch_off()75 void x_catch_off ()
76 {
77   XSynchronize (dpy, False);
78   XSetErrorHandler (0);
79 }
80 
81 
82 /*
83  *	x_error_handler
84  *	An error handler that does not exit.
85  */
x_error_handler(Display * dpy,XErrorEvent * e)86 int x_error_handler (Display *dpy, XErrorEvent *e)
87 {
88   _have_error = true;
89   _error_code = e->error_code;  // We're only interested in the error code
90   return 0;
91 }
92 
93 
94 /*
95  *	x_clear_error
96  *	Call this before attempting an operation that might
97  *	cause an error that you want to catch.
98  */
x_clear_error()99 void x_clear_error ()
100 {
101   _have_error = false;
102 }
103 
104 
105 /*
106  *	x_error
107  *	Return a string corresponding to the last error caught
108  *	or a NULL pointer if no error has occured since last
109  *	call to x_clear_error().
110  */
x_error()111 const char *x_error ()
112 {
113   if (! _have_error)
114     return 0;
115   static char buf[100];
116   XGetErrorText (dpy, _error_code, buf, sizeof buf);
117   return buf;
118 }
119 
120 
121 #endif  /* DO NOT ADD ANYTHING AFTER THIS LINE */
122