[PATCH] key.c: Dynamically load XOpenDisplay, XQueryPointer and XCloseDisplay

Pavel Tsekov ptsekov at gmx.net
Sun Feb 9 19:54:01 UTC 2003


Hello,

Ok, here comes the new patch. It is using the gmodule library as
Pavel Rosking has suggested. It also includes some changes to the 
configure stuff. Please, review and tell me what needs to be fixed.

Here is a list of changes:

* aclocal.m4 (AC_G_MODULE_SUPPORTED): New macro. Tests if gmodule 
functionality of glib can be used. Sets the variable 
'av_g_module_supported' and the macros 'HAVE_WORKING_GMODULE'.
* config.h.in (HAVE_WORKING_GMODULE): New macro.
* configure.in: Detect if gmodule library is present and can be used. 
Update GLIB_FLAGS and GLIB_LIBS accordingly.
Add necessary X libraries to MCLIBS only if 'av_g_module_supported' is
set to 'no'.
* src/key.c: Include gmodule.h if 'HAVE_WORKING_GMODULE' is defined.
(MODULE_DEFAULT_PREFIX): New macro.
(MODULE_DEFUALT_SUFFIX): Ditto.
(MODX11_DEFAULT_NAME): Ditto. Use 'MODULE_DEFAULT_PREFIX' and 
'MODULE_DEFAULT_SUFFIX' macros.
(XOPENDISPLAY): Define new pointer to function type.
(XCLOSEDISPLAY): Ditto.
(XQUERYPOINTER): Ditto.
(x11_module): New static variable.
(func_XOpenDisplay): Ditto.
(func_XCloseDisplay): Ditto.
(func_XQueryPointer): Ditto.
(init_key): Dynamically load XOpenDisplay, XCloseDisplay and XQueryPointer
functions if 'HAVE_WORKING_GMODULE' is defined.
Use 'func_XOpenDisplay'.
(get_modifier): Use 'func_XQueryPointer'.
(done_key): Use 'func_XCloseDisplay'.
Unload the dynamcally loaded library X11 if 'HAVE_WORKING_GMODULE' is 
defined.
-------------- next part --------------
diff -rup mc-4.6.0/aclocal.m4 mc-4.6.0-mod/aclocal.m4
--- mc-4.6.0/aclocal.m4	2003-02-05 19:08:57.000000000 +0100
+++ mc-4.6.0-mod/aclocal.m4	2003-02-09 19:21:40.000000000 +0100
@@ -3881,3 +3881,35 @@ AC_DEFUN([AM_LC_MESSAGES],
   fi
 ])
 
+dnl
+dnl Check whether the g_module_* family of functions works
+dnl on this system.
+dnl
+AC_DEFUN([AC_G_MODULE_SUPPORTED], [
+av_g_module_supported=no
+AC_MSG_CHECKING([if g_module_* family of functions works])
+ac_save_CFLAGS="$CFLAGS"
+ac_save_LIBS="$LIBS"
+CFLAGS="$CFLAGS $GLIB_CFLAGS"
+LIBS="$GLIB_LIBS $LIBS"
+AC_TRY_RUN([
+#include <gmodule.h>
+
+int main ()
+{
+    int ret = g_module_supported () ? 0 : 1;
+    return ret;
+}
+],[
+AC_DEFINE(HAVE_WORKING_GMODULE, 1,
+	  [Define if we have working gmodule library])
+av_g_module_supported=yes
+],[
+av_g_module_supported=no
+],[
+av_g_module_supported=no
+])
+CFLAGS="$ac_save_CFLAGS"
+LIBS="$ac_save_LIBS"
+AC_MSG_RESULT([$av_g_module_supported])
+])
diff -rup mc-4.6.0/config.h.in mc-4.6.0-mod/config.h.in
--- mc-4.6.0/config.h.in	2003-02-05 19:09:02.000000000 +0100
+++ mc-4.6.0-mod/config.h.in	2003-02-09 18:28:18.000000000 +0100
@@ -668,4 +668,7 @@
 /* Define to `int' if <sys/types.h> does not define. */
 #undef umode_t
 
+/* Define if we can use gmodule functionality */
+#undef HAVE_WORKING_GMODULE
+
 #include <extraconf.h>
diff -rup mc-4.6.0/configure.in mc-4.6.0-mod/configure.in
--- mc-4.6.0/configure.in	2003-02-05 19:03:56.000000000 +0100
+++ mc-4.6.0-mod/configure.in	2003-02-09 19:25:45.000000000 +0100
@@ -38,8 +38,25 @@ if test "x$glib_found" = "xno" ; then
 	AC_ARG_VAR([GLIB_CONFIG], [Path to glib-config (version 1.2.x only)])
 	AM_PATH_GLIB(1.2.6,,[AC_MSG_ERROR([Test for glib failed.
 GNU Midnight Commander requires glib 1.2.6 or above.])])
+	save_GLIB_CFLAGS="$GLIB_CFLAGS"
+        save_GLIB_LIBS="$GLIB_LIBS"
+	AM_PATH_GLIB(1.2.6,,[gmodule_found=no],[gmodule])
+else
+    save_GLIB_CFLAGS="$GLIB_CFLAGS"
+    save_GLIB_LIBS="$GLIB_LIBS"
+    PKG_CHECK_MODULES(GLIB, [gmodule-2.0], , [gmodule_found=no])
 fi
 
+if test "x$gmodule_found" = "xno" ; then
+    GLIB_CFLAGS="$save_GLIB_CFLAGS"
+    GLIB_LIBS="$save_GLIB_LIBS"
+else
+    AC_G_MODULE_SUPPORTED
+    if test "x$av_g_module_supported" = "xno" ; then
+	GLIB_CFLAGS="$save_GLIB_CFLAGS"
+	GLIB_LIBS="$save_GLIB_LIBS"
+    fi
+fi
 
 AC_HEADER_MAJOR
 AC_C_CONST
@@ -214,7 +231,9 @@ if test "x$no_x" = xyes; then
     textmode_x11_support="no"
 else
     CPPFLAGS="$CPPFLAGS $X_CFLAGS"
-    MCLIBS="$X_LIBS $X_PRE_LIBS -lX11 $X_EXTRA_LIBS"
+    if test "x$av_g_module_supported" = "xno" ; then
+        MCLIBS="$X_LIBS $X_PRE_LIBS -lX11 $X_EXTRA_LIBS"
+    fi
     AC_DEFINE(HAVE_TEXTMODE_X11_SUPPORT, 1,
 	      [Define to enable getting events from X Window System])
     textmode_x11_support="yes"
diff -rup mc-4.6.0/src/key.c mc-4.6.0-mod/src/key.c
--- mc-4.6.0/src/key.c	2003-01-27 23:37:56.000000000 +0100
+++ mc-4.6.0-mod/src/key.c	2003-02-09 19:17:50.000000000 +0100
@@ -41,6 +41,9 @@
 #include "../vfs/vfs.h"
 
 #ifdef HAVE_TEXTMODE_X11_SUPPORT
+#ifdef HAVE_WORKING_GMODULE
+#include <gmodule.h>
+#endif /* HAVE_WORKING_GMODULE */
 #include <X11/Xlib.h>
 #endif
 
@@ -247,6 +250,30 @@ define_sequences (key_define_t *kd)
 }
 
 #ifdef HAVE_TEXTMODE_X11_SUPPORT
+#ifdef HAVE_WORKING_GMODULE
+#define MODULE_DEFAULT_PREFIX "lib"
+#if GLIB_CHECK_VERSION(2,0,0)
+#    define MODULE_DEFAULT_SUFFIX "." G_MODULE_SUFFIX
+#else /* glib < 2.x.x */
+#    ifndef __CYGWIN__
+#       define MODULE_DEFAULT_SUFFIX ".so"
+#    else /* __CYGWIN__ */
+#       define MODULE_DEFAULT_SUFFIX ".dll"
+#    endif
+#endif
+
+#define MODX11_DEFAULT_NAME MODULE_DEFAULT_PREFIX "X11" MODULE_DEFAULT_SUFFIX
+
+typedef Display * (* XOPENDISPLAY) (_Xconst char *);
+typedef int (* XCLOSEDISPLAY) (Display *);
+typedef Bool (* XQUERYPOINTER) (Display *, Window, Window *, Window*,
+				int *, int *, int *, int *, unsigned int *);
+
+static GModule *x11_module;
+static XOPENDISPLAY func_XOpenDisplay;
+static XCLOSEDISPLAY func_XCloseDisplay;
+static XQUERYPOINTER func_XQueryPointer;
+#endif /* HAVE_WORKING_GMODULE */
 static Display *x11_display;
 static Window x11_window;
 #endif /* HAVE_TEXTMODE_X11_SUPPORT */
@@ -255,6 +282,9 @@ static Window x11_window;
    calls any define_sequence */
 void init_key (void)
 {
+#if defined(HAVE_TEXTMODE_X11_SUPPORT) && defined(HAVE_WORKING_GMODULE)
+    gchar *x11_module_fname;
+#endif /* HAVE_TEXTMODE_X11_SUPPORT && HAVE_WORKING_GMODULE */
     char *term = (char *) getenv ("TERM");
     
     /* This has to be the first define_sequence */
@@ -290,9 +320,32 @@ void init_key (void)
 #endif /* __QNX__ */
 
 #ifdef HAVE_TEXTMODE_X11_SUPPORT
+#ifdef HAVE_WORKING_GMODULE
+    x11_module_fname = g_module_build_path (NULL, "X11");
+    if (x11_module_fname == NULL)
+	x11_module_fname = g_strdup (MODX11_DEFAULT_NAME);
+
+    if (x11_module_fname != NULL) {
+	x11_module = g_module_open (x11_module_fname, G_MODULE_BIND_LAZY);
+	g_free (x11_module_fname);
+	if (x11_module != NULL) {
+	    if (g_module_symbol (x11_module, "XOpenDisplay",
+				 (gpointer *) &func_XOpenDisplay) &&
+		g_module_symbol (x11_module, "XCloseDisplay",
+				 (gpointer *) &func_XCloseDisplay) &&
+		g_module_symbol (x11_module, "XQueryPointer",
+				 (gpointer *) &func_XQueryPointer)) {
+		x11_display = (*func_XOpenDisplay) (0);
+		if (x11_display)
+		    x11_window = DefaultRootWindow (x11_display);
+	    }
+	}
+    }
+#else
     x11_display = XOpenDisplay (0);
     if (x11_display)
         x11_window = DefaultRootWindow (x11_display);
+#endif /* HAVE_WORKING_GMODULE */
 #endif /* HAVE_TEXTMODE_X11_SUPPORT */
 }
 
@@ -1049,8 +1102,13 @@ get_modifier (void)
 	int win_x, win_y;
 	unsigned int mask;
 
+#ifdef HAVE_WORKING_GMODULE
+	(*func_XQueryPointer) (x11_display, x11_window, &root, &child,
+			       &root_x, &root_y, &win_x, &win_y, &mask);
+#else
 	XQueryPointer (x11_display, x11_window, &root, &child, &root_x,
 		       &root_y, &win_x, &win_y, &mask);
+#endif /* HAVE_WORKING_GMODULE */
 
 	if (mask & ShiftMask)
 	    result |= KEY_M_SHIFT;
@@ -1105,7 +1163,14 @@ void done_key ()
     s_dispose (select_list);
 
 #ifdef HAVE_TEXTMODE_X11_SUPPORT
+#ifdef HAVE_WORKING_GMODULE
+    if (x11_display)
+	(*func_XCloseDisplay) (x11_display);
+    if (x11_module != NULL)
+	g_module_close (x11_module);
+#else
     if (x11_display)
 	XCloseDisplay (x11_display);
+#endif /* HAVE_WORKING_GMODULE */
 #endif /* HAVE_TEXTMODE_X11_SUPPORT */
 }


More information about the mc-devel mailing list