x11conn.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. X11 support for the Midnight Commander.
  3. Copyright (C) 2005-2019
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Roland Illig <roland.illig@gmx.de>, 2005.
  7. This file is part of the Midnight Commander.
  8. The Midnight Commander is free software: you can redistribute it
  9. and/or modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation, either version 3 of the License,
  11. or (at your option) any later version.
  12. The Midnight Commander is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /** \file x11conn.c
  20. * \brief Source: X11 support
  21. * \warning This code uses setjmp() and longjmp(). Before you modify _anything_ here,
  22. * please read the relevant sections of the C standard.
  23. */
  24. #include <config.h>
  25. #include <setjmp.h>
  26. #include <X11/Xlib.h>
  27. #ifdef HAVE_GMODULE
  28. #include <gmodule.h>
  29. #endif
  30. #include "lib/global.h"
  31. #include "x11conn.h"
  32. /*** global variables ****************************************************************************/
  33. /*** file scope macro definitions ****************************************************************/
  34. #ifndef HAVE_GMODULE
  35. #define func_XOpenDisplay XOpenDisplay
  36. #define func_XCloseDisplay XCloseDisplay
  37. #define func_XSetErrorHandler XSetErrorHandler
  38. #define func_XSetIOErrorHandler XSetIOErrorHandler
  39. #define func_XQueryPointer XQueryPointer
  40. #endif
  41. /*** file scope type declarations ****************************************************************/
  42. typedef int (*mc_XErrorHandler_callback) (Display *, XErrorEvent *);
  43. typedef int (*mc_XIOErrorHandler_callback) (Display *);
  44. /*** file scope variables ************************************************************************/
  45. #ifdef HAVE_GMODULE
  46. static Display *(*func_XOpenDisplay) (_Xconst char *);
  47. static int (*func_XCloseDisplay) (Display *);
  48. static mc_XErrorHandler_callback (*func_XSetErrorHandler) (mc_XErrorHandler_callback);
  49. static mc_XIOErrorHandler_callback (*func_XSetIOErrorHandler) (mc_XIOErrorHandler_callback);
  50. static Bool (*func_XQueryPointer) (Display *, Window, Window *, Window *,
  51. int *, int *, int *, int *, unsigned int *);
  52. static GModule *x11_module;
  53. #endif
  54. static gboolean handlers_installed = FALSE;
  55. /* This flag is set as soon as an X11 error is reported. Usually that
  56. * means that the DISPLAY is not available anymore. We do not try to
  57. * reconnect, as that would violate the X11 protocol. */
  58. static gboolean lost_connection = FALSE;
  59. static jmp_buf x11_exception; /* FIXME: get a better name */
  60. static gboolean longjmp_allowed = FALSE;
  61. /*** file scope functions ************************************************************************/
  62. /* --------------------------------------------------------------------------------------------- */
  63. static int
  64. x_io_error_handler (Display * dpy)
  65. {
  66. (void) dpy;
  67. lost_connection = TRUE;
  68. if (longjmp_allowed)
  69. {
  70. longjmp_allowed = FALSE;
  71. longjmp (x11_exception, 1);
  72. }
  73. return 0;
  74. }
  75. /* --------------------------------------------------------------------------------------------- */
  76. static int
  77. x_error_handler (Display * dpy, XErrorEvent * ee)
  78. {
  79. (void) ee;
  80. (void) func_XCloseDisplay (dpy);
  81. return x_io_error_handler (dpy);
  82. }
  83. /* --------------------------------------------------------------------------------------------- */
  84. static void
  85. install_error_handlers (void)
  86. {
  87. if (handlers_installed)
  88. return;
  89. (void) func_XSetErrorHandler (x_error_handler);
  90. (void) func_XSetIOErrorHandler (x_io_error_handler);
  91. handlers_installed = TRUE;
  92. }
  93. /* --------------------------------------------------------------------------------------------- */
  94. static gboolean
  95. x11_available (void)
  96. {
  97. #ifdef HAVE_GMODULE
  98. gchar *x11_module_fname;
  99. if (lost_connection)
  100. return FALSE;
  101. if (x11_module != NULL)
  102. return TRUE;
  103. x11_module_fname = g_module_build_path (NULL, "X11");
  104. x11_module = g_module_open (x11_module_fname, G_MODULE_BIND_LAZY);
  105. if (x11_module == NULL)
  106. x11_module = g_module_open ("libX11.so.6", G_MODULE_BIND_LAZY);
  107. g_free (x11_module_fname);
  108. if (x11_module == NULL)
  109. return FALSE;
  110. if (!g_module_symbol (x11_module, "XOpenDisplay", (void *) &func_XOpenDisplay))
  111. goto cleanup;
  112. if (!g_module_symbol (x11_module, "XCloseDisplay", (void *) &func_XCloseDisplay))
  113. goto cleanup;
  114. if (!g_module_symbol (x11_module, "XQueryPointer", (void *) &func_XQueryPointer))
  115. goto cleanup;
  116. if (!g_module_symbol (x11_module, "XSetErrorHandler", (void *) &func_XSetErrorHandler))
  117. goto cleanup;
  118. if (!g_module_symbol (x11_module, "XSetIOErrorHandler", (void *) &func_XSetIOErrorHandler))
  119. goto cleanup;
  120. install_error_handlers ();
  121. return TRUE;
  122. cleanup:
  123. func_XOpenDisplay = 0;
  124. func_XCloseDisplay = 0;
  125. func_XQueryPointer = 0;
  126. func_XSetErrorHandler = 0;
  127. func_XSetIOErrorHandler = 0;
  128. g_module_close (x11_module);
  129. x11_module = NULL;
  130. return FALSE;
  131. #else
  132. install_error_handlers ();
  133. return !(lost_connection);
  134. #endif
  135. }
  136. /* --------------------------------------------------------------------------------------------- */
  137. /*** public functions ****************************************************************************/
  138. /* --------------------------------------------------------------------------------------------- */
  139. Display *
  140. mc_XOpenDisplay (const char *displayname)
  141. {
  142. if (x11_available ())
  143. {
  144. if (setjmp (x11_exception) == 0)
  145. {
  146. Display *retval;
  147. /* cppcheck-suppress redundantAssignment */
  148. longjmp_allowed = TRUE;
  149. retval = func_XOpenDisplay (displayname);
  150. /* cppcheck-suppress redundantAssignment */
  151. longjmp_allowed = FALSE;
  152. return retval;
  153. }
  154. }
  155. return NULL;
  156. }
  157. /* --------------------------------------------------------------------------------------------- */
  158. int
  159. mc_XCloseDisplay (Display * display)
  160. {
  161. if (x11_available ())
  162. {
  163. if (setjmp (x11_exception) == 0)
  164. {
  165. int retval;
  166. /* cppcheck-suppress redundantAssignment */
  167. longjmp_allowed = TRUE;
  168. retval = func_XCloseDisplay (display);
  169. /* cppcheck-suppress redundantAssignment */
  170. longjmp_allowed = FALSE;
  171. return retval;
  172. }
  173. }
  174. return 0;
  175. }
  176. /* --------------------------------------------------------------------------------------------- */
  177. Bool
  178. mc_XQueryPointer (Display * display, Window win, Window * root_return,
  179. Window * child_return, int *root_x_return, int *root_y_return,
  180. int *win_x_return, int *win_y_return, unsigned int *mask_return)
  181. {
  182. Bool retval;
  183. if (x11_available ())
  184. {
  185. if (setjmp (x11_exception) == 0)
  186. {
  187. /* cppcheck-suppress redundantAssignment */
  188. longjmp_allowed = TRUE;
  189. retval = func_XQueryPointer (display, win, root_return,
  190. child_return, root_x_return, root_y_return,
  191. win_x_return, win_y_return, mask_return);
  192. /* cppcheck-suppress redundantAssignment */
  193. longjmp_allowed = FALSE;
  194. return retval;
  195. }
  196. }
  197. *root_return = None;
  198. *child_return = None;
  199. *root_x_return = 0;
  200. *root_y_return = 0;
  201. *win_x_return = 0;
  202. *win_y_return = 0;
  203. *mask_return = 0;
  204. return False;
  205. }
  206. /* --------------------------------------------------------------------------------------------- */