summaryrefslogtreecommitdiffstats
path: root/lib/tty/x11conn.c
blob: 20e201bc78da7b393990d5ada65ab8743ecdceb8 (plain)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
   X11 support for the Midnight Commander.

   Copyright (C) 2005-2023
   Free Software Foundation, Inc.

   Written by:
   Roland Illig <roland.illig@gmx.de>, 2005.

   This file is part of the Midnight Commander.

   The Midnight Commander is free software: you can redistribute it
   and/or modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation, either version 3 of the License,
   or (at your option) any later version.

   The Midnight Commander is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/** \file x11conn.c
 *  \brief Source: X11 support
 *  \warning This code uses setjmp() and longjmp(). Before you modify _anything_ here,
 *  please read the relevant sections of the C standard.
 */

#include <config.h>

#include <setjmp.h>
#include <X11/Xlib.h>
#ifdef HAVE_GMODULE
#include <gmodule.h>
#endif

#include "lib/global.h"
#include "x11conn.h"

/*** global variables ****************************************************************************/

/*** file scope macro definitions ****************************************************************/

#ifndef HAVE_GMODULE
#define func_XOpenDisplay       XOpenDisplay
#define func_XCloseDisplay      XCloseDisplay
#define func_XSetErrorHandler   XSetErrorHandler
#define func_XSetIOErrorHandler XSetIOErrorHandler
#define func_XQueryPointer      XQueryPointer
#endif

/*** file scope type declarations ****************************************************************/

typedef int (*mc_XErrorHandler_callback) (Display *, XErrorEvent *);
typedef int (*mc_XIOErrorHandler_callback) (Display *);

/*** forward declarations (file scope functions) *************************************************/

/*** file scope variables ************************************************************************/

#ifdef HAVE_GMODULE
static Display *(*func_XOpenDisplay) (_Xconst char *);
static int (*func_XCloseDisplay) (Display *);
static mc_XErrorHandler_callback (*func_XSetErrorHandler) (mc_XErrorHandler_callback);
static mc_XIOErrorHandler_callback (*func_XSetIOErrorHandler) (mc_XIOErrorHandler_callback);
static Bool (*func_XQueryPointer) (Display *, Window, Window *, Window *,
                                   int *, int *, int *, int *, unsigned int *);

static GModule *x11_module;
#endif

static gboolean handlers_installed = FALSE;

/* This flag is set as soon as an X11 error is reported. Usually that
 * means that the DISPLAY is not available anymore. We do not try to
 * reconnect, as that would violate the X11 protocol. */
static gboolean lost_connection = FALSE;

static jmp_buf x11_exception;   /* FIXME: get a better name */
static gboolean longjmp_allowed = FALSE;

/* --------------------------------------------------------------------------------------------- */
/*** file scope functions ************************************************************************/
/* --------------------------------------------------------------------------------------------- */

static int
x_io_error_handler (Display * dpy)
{
    (void) dpy;

    lost_connection = TRUE;
    if (longjmp_allowed)
    {
        longjmp_allowed = FALSE;
        longjmp (x11_exception, 1);
    }
    return 0;
}

/* --------------------------------------------------------------------------------------------- */

static int
x_error_handler (Display * dpy, XErrorEvent * ee)
{
    (void) ee;
    (void) func_XCloseDisplay (dpy);
    return x_io_error_handler (dpy);
}

/* --------------------------------------------------------------------------------------------- */

static void
install_error_handlers (void)
{
    if (handlers_installed)
        return;

    (void) func_XSetErrorHandler (x_error_handler);
    (void) func_XSetIOErrorHandler (x_io_error_handler);
    handlers_installed = TRUE;
}

/* --------------------------------------------------------------------------------------------- */

static gboolean
x11_available (void)
{
#ifdef HAVE_GMODULE
    gchar *x11_module_fname;

    if (lost_connection)
        return FALSE;

    if (x11_module != NULL)
        return TRUE;

    x11_module_fname = g_module_build_path (NULL, "X11");
    x11_module = g_module_open (x11_module_fname, G_MODULE_BIND_LAZY);
    if (x11_module == NULL)
        x11_module = g_module_open ("libX11.so.6", G_MODULE_BIND_LAZY);

    g_free (x11_module_fname);

    if (x11_module == NULL)
        return FALSE;

    if (!g_module_symbol (x11_module, "XOpenDisplay", (void *) &func_XOpenDisplay))
        goto cleanup;
    if (!g_module_symbol (x11_module, "XCloseDisplay", (void *) &func_XCloseDisplay))
        goto cleanup;
    if (!g_module_symbol (x11_module, "XQueryPointer", (void *) &func_XQueryPointer))
        goto cleanup;
    if (!g_module_symbol (x11_module, "XSetErrorHandler", (void *) &func_XSetErrorHandler))
        goto cleanup;
    if (!g_module_symbol (x11_module, "XSetIOErrorHandler", (void *) &func_XSetIOErrorHandler))
        goto cleanup;

    install_error_handlers ();
    return TRUE;

  cleanup:
    func_XOpenDisplay = 0;
    func_XCloseDisplay = 0;
    func_XQueryPointer = 0;
    func_XSetErrorHandler = 0;
    func_XSetIOErrorHandler = 0;
    g_module_close (x11_module);
    x11_module = NULL;
    return FALSE;
#else
    install_error_handlers ();
    return !(lost_connection);
#endif
}

/* --------------------------------------------------------------------------------------------- */
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */

Display *
mc_XOpenDisplay (const char *displayname)
{
    if (x11_available ())
    {
        if (setjmp (x11_exception) == 0)
        {
            Display *retval;

            /* cppcheck-suppress redundantAssignment */
            longjmp_allowed = TRUE;

            retval = func_XOpenDisplay (displayname);

            /* cppcheck-suppress redundantAssignment */
            longjmp_allowed = FALSE;
            return retval;
        }
    }
    return NULL;
}

/* --------------------------------------------------------------------------------------------- */

int
mc_XCloseDisplay (Display * display)
{
    if (x11_available ())
    {
        if (setjmp (x11_exception) == 0)
        {
            int retval;

            /* cppcheck-suppress redundantAssignment */
            longjmp_allowed = TRUE;

            retval = func_XCloseDisplay (display);

            /* cppcheck-suppress redundantAssignment */
            longjmp_allowed = FALSE;

            return retval;
        }
    }
    return 0;
}

/* --------------------------------------------------------------------------------------------- */

Bool
mc_XQueryPointer (Display * display, Window win, Window * root_return,
                  Window * child_return, int *root_x_return, int *root_y_return,
                  int *win_x_return, int *win_y_return, unsigned int *mask_return)
{
    Bool retval;

    if (x11_available ())
    {
        if (setjmp (x11_exception) == 0)
        {
            /* cppcheck-suppress redundantAssignment */
            longjmp_allowed = TRUE;

            retval = func_XQueryPointer (display, win, root_return,
                                         child_return, root_x_return, root_y_return,
                                         win_x_return, win_y_return, mask_return);

            /* cppcheck-suppress redundantAssignment */
            longjmp_allowed = FALSE;

            return retval;
        }
    }
    *root_return = None;
    *child_return = None;
    *root_x_return = 0;
    *root_y_return = 0;
    *win_x_return = 0;
    *win_y_return = 0;
    *mask_return = 0;
    return False;
}

/* --------------------------------------------------------------------------------------------- */