2005-03-10 12:44:36 +03:00
|
|
|
/*
|
|
|
|
X11 support for the Midnight Commander.
|
|
|
|
|
2024-01-01 09:46:17 +03:00
|
|
|
Copyright (C) 2005-2024
|
2014-02-12 10:33:10 +04:00
|
|
|
Free Software Foundation, Inc.
|
2005-03-10 12:44:36 +03:00
|
|
|
|
|
|
|
Written by:
|
|
|
|
Roland Illig <roland.illig@gmx.de>, 2005.
|
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
This file is part of the Midnight Commander.
|
2005-03-10 12:44:36 +03:00
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
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,
|
2005-03-10 12:44:36 +03:00
|
|
|
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
|
2011-10-15 14:56:47 +04:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2005-03-10 12:44:36 +03:00
|
|
|
*/
|
|
|
|
|
2009-02-05 21:28:18 +03:00
|
|
|
/** \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.
|
|
|
|
*/
|
2005-03-10 12:44:36 +03:00
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <setjmp.h>
|
2009-02-06 01:27:37 +03:00
|
|
|
#include <X11/Xlib.h>
|
2005-03-10 12:44:36 +03:00
|
|
|
#ifdef HAVE_GMODULE
|
2010-11-08 13:21:45 +03:00
|
|
|
#include <gmodule.h>
|
2005-03-10 12:44:36 +03:00
|
|
|
#endif
|
|
|
|
|
2010-01-20 18:11:52 +03:00
|
|
|
#include "lib/global.h"
|
2010-01-07 02:57:27 +03:00
|
|
|
#include "x11conn.h"
|
2005-03-10 12:44:36 +03:00
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/*** 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 ****************************************************************/
|
2005-03-10 12:44:36 +03:00
|
|
|
|
|
|
|
typedef int (*mc_XErrorHandler_callback) (Display *, XErrorEvent *);
|
|
|
|
typedef int (*mc_XIOErrorHandler_callback) (Display *);
|
|
|
|
|
Update template for .c files.
Add section for forward declarations of local functions. This section is
located before file scope variables because functions can be used in
strucutres (see find.c for example):
/*** forward declarations (file scope functions) *************************************************/
/* button callbacks */
static int start_stop (WButton * button, int action);
static int find_do_view_file (WButton * button, int action);
static int find_do_edit_file (WButton * button, int action);
/*** file scope variables ************************************************************************/
static struct
{
...
bcback_fn callback;
} fbuts[] =
{
...
{ B_STOP, NORMAL_BUTTON, N_("S&uspend"), 0, 0, NULL, start_stop },
...
{ B_VIEW, NORMAL_BUTTON, N_("&View - F3"), 0, 0, NULL, find_do_view_file },
{ B_VIEW, NORMAL_BUTTON, N_("&Edit - F4"), 0, 0, NULL, find_do_edit_file }
};
Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
2023-02-24 09:27:11 +03:00
|
|
|
/*** forward declarations (file scope functions) *************************************************/
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/*** file scope variables ************************************************************************/
|
2005-03-10 12:44:36 +03:00
|
|
|
|
|
|
|
#ifdef HAVE_GMODULE
|
2009-09-17 02:07:04 +04:00
|
|
|
static Display *(*func_XOpenDisplay) (_Xconst char *);
|
2005-03-10 12:44:36 +03:00
|
|
|
static int (*func_XCloseDisplay) (Display *);
|
2010-11-08 13:21:45 +03:00
|
|
|
static mc_XErrorHandler_callback (*func_XSetErrorHandler) (mc_XErrorHandler_callback);
|
|
|
|
static mc_XIOErrorHandler_callback (*func_XSetIOErrorHandler) (mc_XIOErrorHandler_callback);
|
2005-03-10 12:44:36 +03:00
|
|
|
static Bool (*func_XQueryPointer) (Display *, Window, Window *, Window *,
|
2009-09-17 02:07:04 +04:00
|
|
|
int *, int *, int *, int *, unsigned int *);
|
Update template for .c files.
Add section for forward declarations of local functions. This section is
located before file scope variables because functions can be used in
strucutres (see find.c for example):
/*** forward declarations (file scope functions) *************************************************/
/* button callbacks */
static int start_stop (WButton * button, int action);
static int find_do_view_file (WButton * button, int action);
static int find_do_edit_file (WButton * button, int action);
/*** file scope variables ************************************************************************/
static struct
{
...
bcback_fn callback;
} fbuts[] =
{
...
{ B_STOP, NORMAL_BUTTON, N_("S&uspend"), 0, 0, NULL, start_stop },
...
{ B_VIEW, NORMAL_BUTTON, N_("&View - F3"), 0, 0, NULL, find_do_view_file },
{ B_VIEW, NORMAL_BUTTON, N_("&Edit - F4"), 0, 0, NULL, find_do_edit_file }
};
Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
2023-02-24 09:27:11 +03:00
|
|
|
|
2005-03-10 12:44:36 +03:00
|
|
|
static GModule *x11_module;
|
|
|
|
#endif
|
|
|
|
|
2009-02-06 01:46:07 +03:00
|
|
|
static gboolean handlers_installed = FALSE;
|
2005-03-10 12:44:36 +03:00
|
|
|
|
|
|
|
/* 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. */
|
2009-02-06 01:46:07 +03:00
|
|
|
static gboolean lost_connection = FALSE;
|
2005-03-10 12:44:36 +03:00
|
|
|
|
2009-09-17 02:07:04 +04:00
|
|
|
static jmp_buf x11_exception; /* FIXME: get a better name */
|
2009-02-06 01:46:07 +03:00
|
|
|
static gboolean longjmp_allowed = FALSE;
|
2005-03-10 12:44:36 +03:00
|
|
|
|
Update template for .c files.
Add section for forward declarations of local functions. This section is
located before file scope variables because functions can be used in
strucutres (see find.c for example):
/*** forward declarations (file scope functions) *************************************************/
/* button callbacks */
static int start_stop (WButton * button, int action);
static int find_do_view_file (WButton * button, int action);
static int find_do_edit_file (WButton * button, int action);
/*** file scope variables ************************************************************************/
static struct
{
...
bcback_fn callback;
} fbuts[] =
{
...
{ B_STOP, NORMAL_BUTTON, N_("S&uspend"), 0, 0, NULL, start_stop },
...
{ B_VIEW, NORMAL_BUTTON, N_("&View - F3"), 0, 0, NULL, find_do_view_file },
{ B_VIEW, NORMAL_BUTTON, N_("&Edit - F4"), 0, 0, NULL, find_do_edit_file }
};
Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
2023-02-24 09:27:11 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2010-11-08 13:21:45 +03:00
|
|
|
/*** file scope functions ************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2005-03-10 12:44:36 +03:00
|
|
|
|
2009-09-17 02:07:04 +04:00
|
|
|
static int
|
2024-06-01 21:12:14 +03:00
|
|
|
x_io_error_handler (Display *dpy)
|
2005-03-10 12:44:36 +03:00
|
|
|
{
|
|
|
|
(void) dpy;
|
|
|
|
|
|
|
|
lost_connection = TRUE;
|
2010-11-08 13:21:45 +03:00
|
|
|
if (longjmp_allowed)
|
|
|
|
{
|
2009-09-17 02:07:04 +04:00
|
|
|
longjmp_allowed = FALSE;
|
|
|
|
longjmp (x11_exception, 1);
|
2005-03-10 12:44:36 +03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2009-09-17 02:07:04 +04:00
|
|
|
static int
|
2024-06-01 21:12:14 +03:00
|
|
|
x_error_handler (Display *dpy, XErrorEvent *ee)
|
2005-03-10 12:44:36 +03:00
|
|
|
{
|
|
|
|
(void) ee;
|
2005-04-06 22:04:43 +04:00
|
|
|
(void) func_XCloseDisplay (dpy);
|
2005-03-10 12:44:36 +03:00
|
|
|
return x_io_error_handler (dpy);
|
|
|
|
}
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2009-09-17 02:07:04 +04:00
|
|
|
static void
|
|
|
|
install_error_handlers (void)
|
2005-03-10 12:44:36 +03:00
|
|
|
{
|
2009-09-17 02:07:04 +04:00
|
|
|
if (handlers_installed)
|
|
|
|
return;
|
2005-03-10 12:44:36 +03:00
|
|
|
|
|
|
|
(void) func_XSetErrorHandler (x_error_handler);
|
|
|
|
(void) func_XSetIOErrorHandler (x_io_error_handler);
|
|
|
|
handlers_installed = TRUE;
|
|
|
|
}
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2009-09-17 02:07:04 +04:00
|
|
|
static gboolean
|
|
|
|
x11_available (void)
|
2005-03-10 12:44:36 +03:00
|
|
|
{
|
|
|
|
#ifdef HAVE_GMODULE
|
|
|
|
if (lost_connection)
|
2009-09-17 02:07:04 +04:00
|
|
|
return FALSE;
|
2005-03-10 12:44:36 +03:00
|
|
|
|
|
|
|
if (x11_module != NULL)
|
2009-09-17 02:07:04 +04:00
|
|
|
return TRUE;
|
2005-03-10 12:44:36 +03:00
|
|
|
|
2024-08-09 08:31:04 +03:00
|
|
|
x11_module = g_module_open ("X11", G_MODULE_BIND_LAZY);
|
2008-12-19 14:36:48 +03:00
|
|
|
if (x11_module == NULL)
|
2009-09-17 02:07:04 +04:00
|
|
|
x11_module = g_module_open ("libX11.so.6", G_MODULE_BIND_LAZY);
|
2008-12-19 14:36:48 +03:00
|
|
|
|
2005-03-10 12:44:36 +03:00
|
|
|
if (x11_module == NULL)
|
2009-09-17 02:07:04 +04:00
|
|
|
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 ();
|
2005-03-10 12:44:36 +03:00
|
|
|
return TRUE;
|
|
|
|
|
2009-09-17 02:07:04 +04:00
|
|
|
cleanup:
|
2005-03-10 12:44:36 +03:00
|
|
|
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
|
2009-09-17 02:07:04 +04:00
|
|
|
install_error_handlers ();
|
2005-03-10 12:44:36 +03:00
|
|
|
return !(lost_connection);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/*** public functions ****************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2005-03-10 12:44:36 +03:00
|
|
|
|
2009-09-17 02:07:04 +04:00
|
|
|
Display *
|
|
|
|
mc_XOpenDisplay (const char *displayname)
|
2005-03-10 12:44:36 +03:00
|
|
|
{
|
2010-11-08 13:21:45 +03:00
|
|
|
if (x11_available ())
|
|
|
|
{
|
|
|
|
if (setjmp (x11_exception) == 0)
|
|
|
|
{
|
2013-10-15 10:34:04 +04:00
|
|
|
Display *retval;
|
|
|
|
|
2013-10-10 16:21:26 +04:00
|
|
|
/* cppcheck-suppress redundantAssignment */
|
2009-09-17 02:07:04 +04:00
|
|
|
longjmp_allowed = TRUE;
|
2013-10-10 16:21:26 +04:00
|
|
|
|
2009-09-17 02:07:04 +04:00
|
|
|
retval = func_XOpenDisplay (displayname);
|
2013-10-10 16:21:26 +04:00
|
|
|
|
|
|
|
/* cppcheck-suppress redundantAssignment */
|
2009-09-17 02:07:04 +04:00
|
|
|
longjmp_allowed = FALSE;
|
|
|
|
return retval;
|
|
|
|
}
|
2005-03-10 12:44:36 +03:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2009-09-17 02:07:04 +04:00
|
|
|
int
|
2024-06-01 21:12:14 +03:00
|
|
|
mc_XCloseDisplay (Display *display)
|
2005-03-10 12:44:36 +03:00
|
|
|
{
|
2010-11-08 13:21:45 +03:00
|
|
|
if (x11_available ())
|
|
|
|
{
|
|
|
|
if (setjmp (x11_exception) == 0)
|
|
|
|
{
|
2013-10-15 10:34:04 +04:00
|
|
|
int retval;
|
|
|
|
|
2013-10-10 16:21:26 +04:00
|
|
|
/* cppcheck-suppress redundantAssignment */
|
2009-09-17 02:07:04 +04:00
|
|
|
longjmp_allowed = TRUE;
|
2013-10-10 16:21:26 +04:00
|
|
|
|
2009-09-17 02:07:04 +04:00
|
|
|
retval = func_XCloseDisplay (display);
|
2013-10-10 16:21:26 +04:00
|
|
|
|
|
|
|
/* cppcheck-suppress redundantAssignment */
|
2009-09-17 02:07:04 +04:00
|
|
|
longjmp_allowed = FALSE;
|
2013-10-10 16:21:26 +04:00
|
|
|
|
2009-09-17 02:07:04 +04:00
|
|
|
return retval;
|
|
|
|
}
|
2005-03-10 12:44:36 +03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2009-09-17 02:07:04 +04:00
|
|
|
Bool
|
2024-06-01 21:12:14 +03:00
|
|
|
mc_XQueryPointer (Display *display, Window win, Window *root_return,
|
|
|
|
Window *child_return, int *root_x_return, int *root_y_return,
|
2009-09-17 02:07:04 +04:00
|
|
|
int *win_x_return, int *win_y_return, unsigned int *mask_return)
|
2005-03-10 12:44:36 +03:00
|
|
|
{
|
|
|
|
Bool retval;
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
if (x11_available ())
|
|
|
|
{
|
|
|
|
if (setjmp (x11_exception) == 0)
|
|
|
|
{
|
2013-10-10 16:21:26 +04:00
|
|
|
/* cppcheck-suppress redundantAssignment */
|
2009-09-17 02:07:04 +04:00
|
|
|
longjmp_allowed = TRUE;
|
2013-10-10 16:21:26 +04:00
|
|
|
|
2009-09-17 02:07:04 +04:00
|
|
|
retval = func_XQueryPointer (display, win, root_return,
|
|
|
|
child_return, root_x_return, root_y_return,
|
|
|
|
win_x_return, win_y_return, mask_return);
|
2013-10-10 16:21:26 +04:00
|
|
|
|
|
|
|
/* cppcheck-suppress redundantAssignment */
|
2009-09-17 02:07:04 +04:00
|
|
|
longjmp_allowed = FALSE;
|
2013-10-10 16:21:26 +04:00
|
|
|
|
2009-09-17 02:07:04 +04:00
|
|
|
return retval;
|
|
|
|
}
|
2005-03-10 12:44:36 +03:00
|
|
|
}
|
|
|
|
*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;
|
|
|
|
}
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|