Tweak sources for --with-x/--without-x option.
[midnight-commander.git] / lib / tty / x11conn.c
blob0ce1f22f6d1d9103de199ec20e349841d951b2b5
1 /*
2 X11 support for the Midnight Commander.
4 Copyright (C) 2005, 2007, 2011
5 The Free Software Foundation, Inc.
7 Written by:
8 Roland Illig <roland.illig@gmx.de>, 2005.
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software: you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation, either version 3 of the License,
15 or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 /** \file x11conn.c
27 * \brief Source: X11 support
28 * \warning This code uses setjmp() and longjmp(). Before you modify _anything_ here,
29 * please read the relevant sections of the C standard.
32 #include <config.h>
34 #include <setjmp.h>
35 #include <X11/Xlib.h>
36 #ifdef HAVE_GMODULE
37 #include <gmodule.h>
38 #endif
40 #include "lib/global.h"
41 #include "x11conn.h"
43 /*** global variables ****************************************************************************/
45 /*** file scope macro definitions ****************************************************************/
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
55 /*** file scope type declarations ****************************************************************/
57 typedef int (*mc_XErrorHandler_callback) (Display *, XErrorEvent *);
58 typedef int (*mc_XIOErrorHandler_callback) (Display *);
60 /*** file scope variables ************************************************************************/
62 #ifdef HAVE_GMODULE
63 static Display *(*func_XOpenDisplay) (_Xconst char *);
64 static int (*func_XCloseDisplay) (Display *);
65 static mc_XErrorHandler_callback (*func_XSetErrorHandler) (mc_XErrorHandler_callback);
66 static mc_XIOErrorHandler_callback (*func_XSetIOErrorHandler) (mc_XIOErrorHandler_callback);
67 static Bool (*func_XQueryPointer) (Display *, Window, Window *, Window *,
68 int *, int *, int *, int *, unsigned int *);
69 static GModule *x11_module;
70 #endif
72 static gboolean handlers_installed = FALSE;
74 /* This flag is set as soon as an X11 error is reported. Usually that
75 * means that the DISPLAY is not available anymore. We do not try to
76 * reconnect, as that would violate the X11 protocol. */
77 static gboolean lost_connection = FALSE;
79 static jmp_buf x11_exception; /* FIXME: get a better name */
80 static gboolean longjmp_allowed = FALSE;
82 /*** file scope functions ************************************************************************/
83 /* --------------------------------------------------------------------------------------------- */
85 static int
86 x_io_error_handler (Display * dpy)
88 (void) dpy;
90 lost_connection = TRUE;
91 if (longjmp_allowed)
93 longjmp_allowed = FALSE;
94 longjmp (x11_exception, 1);
96 return 0;
99 /* --------------------------------------------------------------------------------------------- */
101 static int
102 x_error_handler (Display * dpy, XErrorEvent * ee)
104 (void) ee;
105 (void) func_XCloseDisplay (dpy);
106 return x_io_error_handler (dpy);
109 /* --------------------------------------------------------------------------------------------- */
111 static void
112 install_error_handlers (void)
114 if (handlers_installed)
115 return;
117 (void) func_XSetErrorHandler (x_error_handler);
118 (void) func_XSetIOErrorHandler (x_io_error_handler);
119 handlers_installed = TRUE;
122 /* --------------------------------------------------------------------------------------------- */
124 static gboolean
125 x11_available (void)
127 #ifdef HAVE_GMODULE
128 gchar *x11_module_fname;
130 if (lost_connection)
131 return FALSE;
133 if (x11_module != NULL)
134 return TRUE;
136 x11_module_fname = g_module_build_path (NULL, "X11");
137 x11_module = g_module_open (x11_module_fname, G_MODULE_BIND_LAZY);
138 if (x11_module == NULL)
139 x11_module = g_module_open ("libX11.so.6", G_MODULE_BIND_LAZY);
141 g_free (x11_module_fname);
143 if (x11_module == NULL)
144 return FALSE;
146 if (!g_module_symbol (x11_module, "XOpenDisplay", (void *) &func_XOpenDisplay))
147 goto cleanup;
148 if (!g_module_symbol (x11_module, "XCloseDisplay", (void *) &func_XCloseDisplay))
149 goto cleanup;
150 if (!g_module_symbol (x11_module, "XQueryPointer", (void *) &func_XQueryPointer))
151 goto cleanup;
152 if (!g_module_symbol (x11_module, "XSetErrorHandler", (void *) &func_XSetErrorHandler))
153 goto cleanup;
154 if (!g_module_symbol (x11_module, "XSetIOErrorHandler", (void *) &func_XSetIOErrorHandler))
155 goto cleanup;
157 install_error_handlers ();
158 return TRUE;
160 cleanup:
161 func_XOpenDisplay = 0;
162 func_XCloseDisplay = 0;
163 func_XQueryPointer = 0;
164 func_XSetErrorHandler = 0;
165 func_XSetIOErrorHandler = 0;
166 g_module_close (x11_module);
167 x11_module = NULL;
168 return FALSE;
169 #else
170 install_error_handlers ();
171 return !(lost_connection);
172 #endif
175 /* --------------------------------------------------------------------------------------------- */
176 /*** public functions ****************************************************************************/
177 /* --------------------------------------------------------------------------------------------- */
179 Display *
180 mc_XOpenDisplay (const char *displayname)
182 Display *retval;
184 if (x11_available ())
186 if (setjmp (x11_exception) == 0)
188 longjmp_allowed = TRUE;
189 retval = func_XOpenDisplay (displayname);
190 longjmp_allowed = FALSE;
191 return retval;
194 return NULL;
197 /* --------------------------------------------------------------------------------------------- */
200 mc_XCloseDisplay (Display * display)
202 int retval;
204 if (x11_available ())
206 if (setjmp (x11_exception) == 0)
208 longjmp_allowed = TRUE;
209 retval = func_XCloseDisplay (display);
210 longjmp_allowed = FALSE;
211 return retval;
214 return 0;
217 /* --------------------------------------------------------------------------------------------- */
219 Bool
220 mc_XQueryPointer (Display * display, Window win, Window * root_return,
221 Window * child_return, int *root_x_return, int *root_y_return,
222 int *win_x_return, int *win_y_return, unsigned int *mask_return)
224 Bool retval;
226 if (x11_available ())
228 if (setjmp (x11_exception) == 0)
230 longjmp_allowed = TRUE;
231 retval = func_XQueryPointer (display, win, root_return,
232 child_return, root_x_return, root_y_return,
233 win_x_return, win_y_return, mask_return);
234 longjmp_allowed = FALSE;
235 return retval;
238 *root_return = None;
239 *child_return = None;
240 *root_x_return = 0;
241 *root_y_return = 0;
242 *win_x_return = 0;
243 *win_y_return = 0;
244 *mask_return = 0;
245 return False;
248 /* --------------------------------------------------------------------------------------------- */