This source file includes following definitions.
- x_io_error_handler
- x_error_handler
- install_error_handlers
- x11_available
- mc_XOpenDisplay
- mc_XCloseDisplay
- mc_XQueryPointer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 #include <config.h>
33
34 #include <setjmp.h>
35 #include <X11/Xlib.h>
36 #ifdef HAVE_GMODULE
37 #include <gmodule.h>
38 #endif
39
40 #include "lib/global.h"
41 #include "x11conn.h"
42
43
44
45
46
47 #ifndef HAVE_GMODULE
48 #define func_XOpenDisplay XOpenDisplay
49 #define func_XCloseDisplay XCloseDisplay
50 #define func_XSetErrorHandler XSetErrorHandler
51 #define func_XSetIOErrorHandler XSetIOErrorHandler
52 #define func_XQueryPointer XQueryPointer
53 #endif
54
55
56
57 typedef int (*mc_XErrorHandler_callback) (Display *, XErrorEvent *);
58 typedef int (*mc_XIOErrorHandler_callback) (Display *);
59
60
61
62
63
64 #ifdef HAVE_GMODULE
65 static Display *(*func_XOpenDisplay) (_Xconst char *);
66 static int (*func_XCloseDisplay) (Display *);
67 static mc_XErrorHandler_callback (*func_XSetErrorHandler) (mc_XErrorHandler_callback);
68 static mc_XIOErrorHandler_callback (*func_XSetIOErrorHandler) (mc_XIOErrorHandler_callback);
69 static Bool (*func_XQueryPointer) (Display *, Window, Window *, Window *,
70 int *, int *, int *, int *, unsigned int *);
71
72 static GModule *x11_module;
73 #endif
74
75 static gboolean handlers_installed = FALSE;
76
77
78
79
80 static gboolean lost_connection = FALSE;
81
82 static jmp_buf x11_exception;
83 static gboolean longjmp_allowed = FALSE;
84
85
86
87
88
89 static int
90 x_io_error_handler (Display * dpy)
91 {
92 (void) dpy;
93
94 lost_connection = TRUE;
95 if (longjmp_allowed)
96 {
97 longjmp_allowed = FALSE;
98 longjmp (x11_exception, 1);
99 }
100 return 0;
101 }
102
103
104
105 static int
106 x_error_handler (Display * dpy, XErrorEvent * ee)
107 {
108 (void) ee;
109 (void) func_XCloseDisplay (dpy);
110 return x_io_error_handler (dpy);
111 }
112
113
114
115 static void
116 install_error_handlers (void)
117 {
118 if (handlers_installed)
119 return;
120
121 (void) func_XSetErrorHandler (x_error_handler);
122 (void) func_XSetIOErrorHandler (x_io_error_handler);
123 handlers_installed = TRUE;
124 }
125
126
127
128 static gboolean
129 x11_available (void)
130 {
131 #ifdef HAVE_GMODULE
132 gchar *x11_module_fname;
133
134 if (lost_connection)
135 return FALSE;
136
137 if (x11_module != NULL)
138 return TRUE;
139
140 x11_module_fname = g_module_build_path (NULL, "X11");
141 x11_module = g_module_open (x11_module_fname, G_MODULE_BIND_LAZY);
142 if (x11_module == NULL)
143 x11_module = g_module_open ("libX11.so.6", G_MODULE_BIND_LAZY);
144
145 g_free (x11_module_fname);
146
147 if (x11_module == NULL)
148 return FALSE;
149
150 if (!g_module_symbol (x11_module, "XOpenDisplay", (void *) &func_XOpenDisplay))
151 goto cleanup;
152 if (!g_module_symbol (x11_module, "XCloseDisplay", (void *) &func_XCloseDisplay))
153 goto cleanup;
154 if (!g_module_symbol (x11_module, "XQueryPointer", (void *) &func_XQueryPointer))
155 goto cleanup;
156 if (!g_module_symbol (x11_module, "XSetErrorHandler", (void *) &func_XSetErrorHandler))
157 goto cleanup;
158 if (!g_module_symbol (x11_module, "XSetIOErrorHandler", (void *) &func_XSetIOErrorHandler))
159 goto cleanup;
160
161 install_error_handlers ();
162 return TRUE;
163
164 cleanup:
165 func_XOpenDisplay = 0;
166 func_XCloseDisplay = 0;
167 func_XQueryPointer = 0;
168 func_XSetErrorHandler = 0;
169 func_XSetIOErrorHandler = 0;
170 g_module_close (x11_module);
171 x11_module = NULL;
172 return FALSE;
173 #else
174 install_error_handlers ();
175 return !(lost_connection);
176 #endif
177 }
178
179
180
181
182
183 Display *
184 mc_XOpenDisplay (const char *displayname)
185 {
186 if (x11_available ())
187 {
188 if (setjmp (x11_exception) == 0)
189 {
190 Display *retval;
191
192
193 longjmp_allowed = TRUE;
194
195 retval = func_XOpenDisplay (displayname);
196
197
198 longjmp_allowed = FALSE;
199 return retval;
200 }
201 }
202 return NULL;
203 }
204
205
206
207 int
208 mc_XCloseDisplay (Display * display)
209 {
210 if (x11_available ())
211 {
212 if (setjmp (x11_exception) == 0)
213 {
214 int retval;
215
216
217 longjmp_allowed = TRUE;
218
219 retval = func_XCloseDisplay (display);
220
221
222 longjmp_allowed = FALSE;
223
224 return retval;
225 }
226 }
227 return 0;
228 }
229
230
231
232 Bool
233 mc_XQueryPointer (Display * display, Window win, Window * root_return,
234 Window * child_return, int *root_x_return, int *root_y_return,
235 int *win_x_return, int *win_y_return, unsigned int *mask_return)
236 {
237 Bool retval;
238
239 if (x11_available ())
240 {
241 if (setjmp (x11_exception) == 0)
242 {
243
244 longjmp_allowed = TRUE;
245
246 retval = func_XQueryPointer (display, win, root_return,
247 child_return, root_x_return, root_y_return,
248 win_x_return, win_y_return, mask_return);
249
250
251 longjmp_allowed = FALSE;
252
253 return retval;
254 }
255 }
256 *root_return = None;
257 *child_return = None;
258 *root_x_return = 0;
259 *root_y_return = 0;
260 *win_x_return = 0;
261 *win_y_return = 0;
262 *mask_return = 0;
263 return False;
264 }
265
266