Ticket #1369 (Update *.hlp files and other docs)
[midnight-commander.git] / src / x11conn.c
blobc5f98a1b154189035c0536769e35c0d9c4026509
1 /*
2 X11 support for the Midnight Commander.
4 Copyright (C) 2005, 2007 Free Software Foundation, Inc.
6 Written by:
7 Roland Illig <roland.illig@gmx.de>, 2005.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 /** \file x11conn.c
25 * \brief Source: X11 support
26 * \warning This code uses setjmp() and longjmp(). Before you modify _anything_ here,
27 * please read the relevant sections of the C standard.
30 #include <config.h>
32 #ifndef HAVE_TEXTMODE_X11_SUPPORT
33 typedef int dummy; /* C99 forbids empty compilation unit */
34 #else
36 #include <setjmp.h>
38 #include <X11/Xlib.h>
40 #include "../src/global.h"
42 #ifdef HAVE_GMODULE
43 # include <gmodule.h>
44 #endif
46 #include "x11conn.h"
48 /*** file scope type declarations **************************************/
50 typedef int (*mc_XErrorHandler_callback) (Display *, XErrorEvent *);
51 typedef int (*mc_XIOErrorHandler_callback) (Display *);
53 /*** file scope variables **********************************************/
55 #ifdef HAVE_GMODULE
57 static Display * (*func_XOpenDisplay) (_Xconst char *);
58 static int (*func_XCloseDisplay) (Display *);
59 static mc_XErrorHandler_callback (*func_XSetErrorHandler)
60 (mc_XErrorHandler_callback);
61 static mc_XIOErrorHandler_callback (*func_XSetIOErrorHandler)
62 (mc_XIOErrorHandler_callback);
63 static Bool (*func_XQueryPointer) (Display *, Window, Window *, Window *,
64 int *, int *, int *, int *, unsigned int *);
66 static GModule *x11_module;
68 #else
70 #define func_XOpenDisplay XOpenDisplay
71 #define func_XCloseDisplay XCloseDisplay
72 #define func_XSetErrorHandler XSetErrorHandler
73 #define func_XSetIOErrorHandler XSetIOErrorHandler
74 #define func_XQueryPointer XQueryPointer
76 #endif
78 static gboolean handlers_installed = FALSE;
80 /* This flag is set as soon as an X11 error is reported. Usually that
81 * means that the DISPLAY is not available anymore. We do not try to
82 * reconnect, as that would violate the X11 protocol. */
83 static gboolean lost_connection = FALSE;
85 static jmp_buf x11_exception; /* FIXME: get a better name */
86 static gboolean longjmp_allowed = FALSE;
88 /*** file private functions ********************************************/
90 static int x_io_error_handler (Display *dpy)
92 (void) dpy;
94 lost_connection = TRUE;
95 if (longjmp_allowed) {
96 longjmp_allowed = FALSE;
97 longjmp(x11_exception, 1);
99 return 0;
102 static int x_error_handler (Display *dpy, XErrorEvent *ee)
104 (void) ee;
105 (void) func_XCloseDisplay (dpy);
106 return x_io_error_handler (dpy);
109 static void install_error_handlers(void)
111 if (handlers_installed) return;
113 (void) func_XSetErrorHandler (x_error_handler);
114 (void) func_XSetIOErrorHandler (x_io_error_handler);
115 handlers_installed = TRUE;
118 static gboolean x11_available(void)
120 #ifdef HAVE_GMODULE
121 gchar *x11_module_fname;
123 if (lost_connection)
124 return FALSE;
126 if (x11_module != NULL)
127 return TRUE;
129 x11_module_fname = g_module_build_path (NULL, "X11");
130 x11_module = g_module_open (x11_module_fname, G_MODULE_BIND_LAZY);
131 if (x11_module == NULL)
132 x11_module = g_module_open ("libX11.so.6", G_MODULE_BIND_LAZY);
134 g_free (x11_module_fname);
136 if (x11_module == NULL)
137 return FALSE;
139 if (!g_module_symbol (x11_module, "XOpenDisplay",
140 (void *) &func_XOpenDisplay))
141 goto cleanup;
142 if (!g_module_symbol (x11_module, "XCloseDisplay",
143 (void *) &func_XCloseDisplay))
144 goto cleanup;
145 if (!g_module_symbol (x11_module, "XQueryPointer",
146 (void *) &func_XQueryPointer))
147 goto cleanup;
148 if (!g_module_symbol (x11_module, "XSetErrorHandler",
149 (void *) &func_XSetErrorHandler))
150 goto cleanup;
151 if (!g_module_symbol (x11_module, "XSetIOErrorHandler",
152 (void *) &func_XSetIOErrorHandler))
153 goto cleanup;
155 install_error_handlers();
156 return TRUE;
158 cleanup:
159 func_XOpenDisplay = 0;
160 func_XCloseDisplay = 0;
161 func_XQueryPointer = 0;
162 func_XSetErrorHandler = 0;
163 func_XSetIOErrorHandler = 0;
164 g_module_close (x11_module);
165 x11_module = NULL;
166 return FALSE;
167 #else
168 install_error_handlers();
169 return !(lost_connection);
170 #endif
173 /*** public functions **************************************************/
175 Display *mc_XOpenDisplay (const char *displayname)
177 Display *retval;
179 if (x11_available()) {
180 if (setjmp(x11_exception) == 0) {
181 longjmp_allowed = TRUE;
182 retval = func_XOpenDisplay (displayname);
183 longjmp_allowed = FALSE;
184 return retval;
187 return NULL;
190 int mc_XCloseDisplay (Display *display)
192 int retval;
194 if (x11_available()) {
195 if (setjmp(x11_exception) == 0) {
196 longjmp_allowed = TRUE;
197 retval = func_XCloseDisplay (display);
198 longjmp_allowed = FALSE;
199 return retval;
202 return 0;
205 Bool mc_XQueryPointer (Display *display, Window win, Window *root_return,
206 Window *child_return, int *root_x_return, int *root_y_return,
207 int *win_x_return, int *win_y_return, unsigned int *mask_return)
209 Bool retval;
211 if (x11_available()) {
212 if (setjmp(x11_exception) == 0) {
213 longjmp_allowed = TRUE;
214 retval = func_XQueryPointer (display, win, root_return,
215 child_return, root_x_return, root_y_return,
216 win_x_return, win_y_return, mask_return);
217 longjmp_allowed = FALSE;
218 return retval;
221 *root_return = None;
222 *child_return = None;
223 *root_x_return = 0;
224 *root_y_return = 0;
225 *win_x_return = 0;
226 *win_y_return = 0;
227 *mask_return = 0;
228 return False;
231 #endif /* HAVE_TEXTMODE_X11_SUPPORT */