x11conn.c 7.6 KB

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