x11conn.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. /*** file scope type declarations **************************************/
  35. typedef int (*mc_XErrorHandler_callback) (Display *, XErrorEvent *);
  36. typedef int (*mc_XIOErrorHandler_callback) (Display *);
  37. /*** file scope variables **********************************************/
  38. #ifdef HAVE_GMODULE
  39. static Display *(*func_XOpenDisplay) (_Xconst char *);
  40. static int (*func_XCloseDisplay) (Display *);
  41. static mc_XErrorHandler_callback (*func_XSetErrorHandler)
  42. (mc_XErrorHandler_callback);
  43. static mc_XIOErrorHandler_callback (*func_XSetIOErrorHandler)
  44. (mc_XIOErrorHandler_callback);
  45. static Bool (*func_XQueryPointer) (Display *, Window, Window *, Window *,
  46. int *, int *, int *, int *, unsigned int *);
  47. static GModule *x11_module;
  48. #else
  49. #define func_XOpenDisplay XOpenDisplay
  50. #define func_XCloseDisplay XCloseDisplay
  51. #define func_XSetErrorHandler XSetErrorHandler
  52. #define func_XSetIOErrorHandler XSetIOErrorHandler
  53. #define func_XQueryPointer XQueryPointer
  54. #endif
  55. static gboolean handlers_installed = FALSE;
  56. /* This flag is set as soon as an X11 error is reported. Usually that
  57. * means that the DISPLAY is not available anymore. We do not try to
  58. * reconnect, as that would violate the X11 protocol. */
  59. static gboolean lost_connection = FALSE;
  60. static jmp_buf x11_exception; /* FIXME: get a better name */
  61. static gboolean longjmp_allowed = FALSE;
  62. /*** file private functions ********************************************/
  63. static int
  64. x_io_error_handler (Display * dpy)
  65. {
  66. (void) dpy;
  67. lost_connection = TRUE;
  68. if (longjmp_allowed) {
  69. longjmp_allowed = FALSE;
  70. longjmp (x11_exception, 1);
  71. }
  72. return 0;
  73. }
  74. static int
  75. x_error_handler (Display * dpy, XErrorEvent * ee)
  76. {
  77. (void) ee;
  78. (void) func_XCloseDisplay (dpy);
  79. return x_io_error_handler (dpy);
  80. }
  81. static void
  82. install_error_handlers (void)
  83. {
  84. if (handlers_installed)
  85. return;
  86. (void) func_XSetErrorHandler (x_error_handler);
  87. (void) func_XSetIOErrorHandler (x_io_error_handler);
  88. handlers_installed = TRUE;
  89. }
  90. static gboolean
  91. x11_available (void)
  92. {
  93. #ifdef HAVE_GMODULE
  94. gchar *x11_module_fname;
  95. if (lost_connection)
  96. return FALSE;
  97. if (x11_module != NULL)
  98. return TRUE;
  99. x11_module_fname = g_module_build_path (NULL, "X11");
  100. x11_module = g_module_open (x11_module_fname, G_MODULE_BIND_LAZY);
  101. if (x11_module == NULL)
  102. x11_module = g_module_open ("libX11.so.6", G_MODULE_BIND_LAZY);
  103. g_free (x11_module_fname);
  104. if (x11_module == NULL)
  105. return FALSE;
  106. if (!g_module_symbol (x11_module, "XOpenDisplay", (void *) &func_XOpenDisplay))
  107. goto cleanup;
  108. if (!g_module_symbol (x11_module, "XCloseDisplay", (void *) &func_XCloseDisplay))
  109. goto cleanup;
  110. if (!g_module_symbol (x11_module, "XQueryPointer", (void *) &func_XQueryPointer))
  111. goto cleanup;
  112. if (!g_module_symbol (x11_module, "XSetErrorHandler", (void *) &func_XSetErrorHandler))
  113. goto cleanup;
  114. if (!g_module_symbol (x11_module, "XSetIOErrorHandler", (void *) &func_XSetIOErrorHandler))
  115. goto cleanup;
  116. install_error_handlers ();
  117. return TRUE;
  118. cleanup:
  119. func_XOpenDisplay = 0;
  120. func_XCloseDisplay = 0;
  121. func_XQueryPointer = 0;
  122. func_XSetErrorHandler = 0;
  123. func_XSetIOErrorHandler = 0;
  124. g_module_close (x11_module);
  125. x11_module = NULL;
  126. return FALSE;
  127. #else
  128. install_error_handlers ();
  129. return !(lost_connection);
  130. #endif
  131. }
  132. /*** public functions **************************************************/
  133. Display *
  134. mc_XOpenDisplay (const char *displayname)
  135. {
  136. Display *retval;
  137. if (x11_available ()) {
  138. if (setjmp (x11_exception) == 0) {
  139. longjmp_allowed = TRUE;
  140. retval = func_XOpenDisplay (displayname);
  141. longjmp_allowed = FALSE;
  142. return retval;
  143. }
  144. }
  145. return NULL;
  146. }
  147. int
  148. mc_XCloseDisplay (Display * display)
  149. {
  150. int retval;
  151. if (x11_available ()) {
  152. if (setjmp (x11_exception) == 0) {
  153. longjmp_allowed = TRUE;
  154. retval = func_XCloseDisplay (display);
  155. longjmp_allowed = FALSE;
  156. return retval;
  157. }
  158. }
  159. return 0;
  160. }
  161. Bool
  162. mc_XQueryPointer (Display * display, Window win, Window * root_return,
  163. Window * child_return, int *root_x_return, int *root_y_return,
  164. int *win_x_return, int *win_y_return, unsigned int *mask_return)
  165. {
  166. Bool retval;
  167. if (x11_available ()) {
  168. if (setjmp (x11_exception) == 0) {
  169. longjmp_allowed = TRUE;
  170. retval = func_XQueryPointer (display, win, root_return,
  171. child_return, root_x_return, root_y_return,
  172. win_x_return, win_y_return, mask_return);
  173. longjmp_allowed = FALSE;
  174. return retval;
  175. }
  176. }
  177. *root_return = None;
  178. *child_return = None;
  179. *root_x_return = 0;
  180. *root_y_return = 0;
  181. *win_x_return = 0;
  182. *win_y_return = 0;
  183. *mask_return = 0;
  184. return False;
  185. }
  186. #endif /* HAVE_TEXTMODE_X11_SUPPORT */