xaizek / vifm (License: GPLv2+) (since 2018-12-07)
Vifm is a file manager with curses interface, which provides Vi[m]-like environment for managing objects within file systems, extended with some useful ideas from mutt.
Commit e1c17f2c783a058e691257a66b3068367cfd4599

Implement dynamic loading of the libX11.so
Controlled by the --with-dyn-X11 flag of the configure script.
Author: xaizek
Author date (UTC): 2013-04-21 12:22
Committer name: xaizek
Committer date (UTC): 2013-04-21 12:22
Parent(s): 71864cb78df3fcafc4decebde2115a8600cf9efa
Signing key:
Tree: 609134095b77411e7bc19867c2df0d070cc63af5
File Lines added Lines deleted
config.h.in 6 0
configure 93 1
configure.in 22 2
src/term_title.c 119 1
File config.h.in changed (mode: 100644) (index b4569f7b5..02df46596)
9 9 #undef PACKAGE_DATA_DIR #undef PACKAGE_DATA_DIR
10 10 #undef PACKAGE_SOURCE_DIR #undef PACKAGE_SOURCE_DIR
11 11
12 /* load libX11 dynamically */
13 #undef DYN_X11
14
12 15 /* parsing of .desktop files */ /* parsing of .desktop files */
13 16 #undef ENABLE_DESKTOP_FILES #undef ENABLE_DESKTOP_FILES
14 17
 
31 34 /* Define to 1 if you have the <inttypes.h> header file. */ /* Define to 1 if you have the <inttypes.h> header file. */
32 35 #undef HAVE_INTTYPES_H #undef HAVE_INTTYPES_H
33 36
37 /* Define to 1 if you have the `dl' library (-ldl). */
38 #undef HAVE_LIBDL
39
34 40 /* use gtk to determine mime type */ /* use gtk to determine mime type */
35 41 #undef HAVE_LIBGTK #undef HAVE_LIBGTK
36 42
File configure changed (mode: 100755) (index 7a37355d8..63166b1b0)
... ... with_curses
722 722 with_gtk with_gtk
723 723 with_libmagic with_libmagic
724 724 with_X11 with_X11
725 with_dyn_X11
725 726 enable_extended_keys enable_extended_keys
726 727 enable_desktop_files enable_desktop_files
727 728 ' '
 
... ... Optional Packages:
1370 1371 [default=yes] [default=yes]
1371 1372 --with-X11 use libX11 to determine terminal emulator title --with-X11 use libX11 to determine terminal emulator title
1372 1373 [default=yes] [default=yes]
1374 --with-dyn-X11 load libX11 dynamically [default=yes]
1373 1375
1374 1376 Some influential environment variables: Some influential environment variables:
1375 1377 CC C compiler command CC C compiler command
 
... ... fi
8221 8223
8222 8224
8223 8225
8226 # Check whether --with-dyn-X11 was given.
8227 if test "${with_dyn_X11+set}" = set; then :
8228 withval=$with_dyn_X11; use_dyn_libX11=$withval
8229 else
8230 use_dyn_libX11=yes
8231 fi
8232
8233
8234
8224 8235 if test "$use_gtk" = "yes"; then if test "$use_gtk" = "yes"; then
8225 8236 if test "x$(which pkg-config)" != "x"; then if test "x$(which pkg-config)" != "x"; then
8226 8237 if pkg-config --exists glib-2.0 gtk+-2.0; then if pkg-config --exists glib-2.0 gtk+-2.0; then
 
... ... $as_echo "#define ENABLE_DESKTOP_FILES 1" >>confdefs.h
8418 8429
8419 8430 fi fi
8420 8431
8432 if test "$use_dyn_libX11" = "yes"; then
8433 { $as_echo "$as_me:${as_lineno-$LINENO}: checking for dlopen in -ldl" >&5
8434 $as_echo_n "checking for dlopen in -ldl... " >&6; }
8435 if ${ac_cv_lib_dl_dlopen+:} false; then :
8436 $as_echo_n "(cached) " >&6
8437 else
8438 ac_check_lib_save_LIBS=$LIBS
8439 LIBS="-ldl $LIBS"
8440 cat confdefs.h - <<_ACEOF >conftest.$ac_ext
8441 /* end confdefs.h. */
8442
8443 /* Override any GCC internal prototype to avoid an error.
8444 Use char because int might match the return type of a GCC
8445 builtin and then its argument prototype would still apply. */
8446 #ifdef __cplusplus
8447 extern "C"
8448 #endif
8449 char dlopen ();
8450 int
8451 main ()
8452 {
8453 return dlopen ();
8454 ;
8455 return 0;
8456 }
8457 _ACEOF
8458 if ac_fn_c_try_link "$LINENO"; then :
8459 ac_cv_lib_dl_dlopen=yes
8460 else
8461 ac_cv_lib_dl_dlopen=no
8462 fi
8463 rm -f core conftest.err conftest.$ac_objext \
8464 conftest$ac_exeext conftest.$ac_ext
8465 LIBS=$ac_check_lib_save_LIBS
8466 fi
8467 { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_dl_dlopen" >&5
8468 $as_echo "$ac_cv_lib_dl_dlopen" >&6; }
8469 if test "x$ac_cv_lib_dl_dlopen" = xyes; then :
8470 cat >>confdefs.h <<_ACEOF
8471 #define HAVE_LIBDL 1
8472 _ACEOF
8473
8474 LIBS="-ldl $LIBS"
8475
8476 else
8477 use_dyn_libX11=no
8478 fi
8479
8480 ac_fn_c_check_header_mongrel "$LINENO" "dlfcn.h" "ac_cv_header_dlfcn_h" "$ac_includes_default"
8481 if test "x$ac_cv_header_dlfcn_h" = xyes; then :
8482
8483 else
8484 use_dyn_libX11=no
8485 fi
8486
8487
8488 ac_fn_c_check_func "$LINENO" "dlsym" "ac_cv_func_dlsym"
8489 if test "x$ac_cv_func_dlsym" = xyes; then :
8490
8491 else
8492 use_dyn_libX11=no
8493 fi
8494
8495 ac_fn_c_check_func "$LINENO" "dlclose" "ac_cv_func_dlclose"
8496 if test "x$ac_cv_func_dlclose" = xyes; then :
8497
8498 else
8499 use_dyn_libX11=no
8500 fi
8501
8502 fi
8503
8421 8504 if test "$use_libX11" = "yes"; then if test "$use_libX11" = "yes"; then
8422 8505 { $as_echo "$as_me:${as_lineno-$LINENO}: checking for XOpenDisplay in -lX11" >&5 { $as_echo "$as_me:${as_lineno-$LINENO}: checking for XOpenDisplay in -lX11" >&5
8423 8506 $as_echo_n "checking for XOpenDisplay in -lX11... " >&6; } $as_echo_n "checking for XOpenDisplay in -lX11... " >&6; }
 
... ... fi
8456 8539 { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_X11_XOpenDisplay" >&5 { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_X11_XOpenDisplay" >&5
8457 8540 $as_echo "$ac_cv_lib_X11_XOpenDisplay" >&6; } $as_echo "$ac_cv_lib_X11_XOpenDisplay" >&6; }
8458 8541 if test "x$ac_cv_lib_X11_XOpenDisplay" = xyes; then : if test "x$ac_cv_lib_X11_XOpenDisplay" = xyes; then :
8459 LIBS="$LIBS -lX11"
8542
8460 8543 ac_fn_c_check_header_mongrel "$LINENO" "X11/Xlib.h" "ac_cv_header_X11_Xlib_h" "$ac_includes_default" ac_fn_c_check_header_mongrel "$LINENO" "X11/Xlib.h" "ac_cv_header_X11_Xlib_h" "$ac_includes_default"
8461 8544 if test "x$ac_cv_header_X11_Xlib_h" = xyes; then : if test "x$ac_cv_header_X11_Xlib_h" = xyes; then :
8462 8545
 
... ... fi
8476 8559
8477 8560 $as_echo "#define HAVE_X11 1" >>confdefs.h $as_echo "#define HAVE_X11 1" >>confdefs.h
8478 8561
8562 if test "$use_dyn_libX11" = "yes"; then
8563 LIBS="$LIBS -ldl"
8564
8565 $as_echo "#define DYN_X11 1" >>confdefs.h
8566
8567 else
8568 LIBS="$LIBS -lX11"
8569 fi
8570
8479 8571 fi fi
8480 8572
8481 8573 fi fi
File configure.in changed (mode: 100644) (index 5159290a2..65bbe592b)
... ... AC_ARG_WITH(X11,
443 443 [use_libX11=$withval], [use_libX11=$withval],
444 444 [use_libX11=yes]) [use_libX11=yes])
445 445
446 AC_ARG_WITH(dyn-X11,
447 AS_HELP_STRING([--with-dyn-X11],
448 [load libX11 dynamically @<:@default=yes@:>@]),
449 [use_dyn_libX11=$withval],
450 [use_dyn_libX11=yes])
451
446 452 dnl ---------------------------------------------------------------------------- dnl ----------------------------------------------------------------------------
447 453 dnl checks for mimetype detection dnl checks for mimetype detection
448 454 dnl ---------------------------------------------------------------------------- dnl ----------------------------------------------------------------------------
 
... ... if test "$desktop_files" = "yes"; then
505 511 AC_DEFINE([ENABLE_DESKTOP_FILES], [1], [parsing of .desktop files]) AC_DEFINE([ENABLE_DESKTOP_FILES], [1], [parsing of .desktop files])
506 512 fi fi
507 513
514 if test "$use_dyn_libX11" = "yes"; then
515 AC_CHECK_LIB(dl, dlopen, [], [use_dyn_libX11=no])
516 AC_CHECK_HEADER([dlfcn.h], [], [use_dyn_libX11=no])
517 AC_CHECK_FUNC([dlsym], [], [use_dyn_libX11=no])
518 AC_CHECK_FUNC([dlclose], [], [use_dyn_libX11=no])
519 fi
520
508 521 if test "$use_libX11" = "yes"; then if test "$use_libX11" = "yes"; then
509 522 AC_CHECK_LIB(X11, XOpenDisplay, AC_CHECK_LIB(X11, XOpenDisplay,
510 [LIBS="$LIBS -lX11"
523 [
511 524 AC_CHECK_HEADER([X11/Xlib.h], [], [AC_MSG_ERROR([X11/Xlib.h header not found.])]) AC_CHECK_HEADER([X11/Xlib.h], [], [AC_MSG_ERROR([X11/Xlib.h header not found.])])
512 525 AC_CHECK_HEADER([X11/Xutil.h], [], [AC_MSG_ERROR([X11/Xutil.h header not found.])]) AC_CHECK_HEADER([X11/Xutil.h], [], [AC_MSG_ERROR([X11/Xutil.h header not found.])])
513 AC_DEFINE([HAVE_X11], [1], [use X11 to determine terminal emulator title])])
526 AC_DEFINE([HAVE_X11], [1], [use X11 to determine terminal emulator title])
527 if test "$use_dyn_libX11" = "yes"; then
528 LIBS="$LIBS -ldl"
529 AC_DEFINE([DYN_X11], [1], [load libX11 dynamically])
530 else
531 LIBS="$LIBS -lX11"
532 fi
533 ])
514 534 fi fi
515 535
516 536 dnl Include config.h in all translation units implicitly dnl Include config.h in all translation units implicitly
File src/term_title.c changed (mode: 100644) (index 80c717d7f..286761866)
23 23 #if !defined(_WIN32) && defined(HAVE_X11) #if !defined(_WIN32) && defined(HAVE_X11)
24 24 #include <X11/Xlib.h> #include <X11/Xlib.h>
25 25 #include <X11/Xutil.h> #include <X11/Xutil.h>
26 #ifdef DYN_X11
27 #include <dlfcn.h> /* dlopen() dlsym() dlclose() */
28 #endif
26 29 #endif #endif
27 30
31 #include <stddef.h> /* NULL size_t */
28 32 #include <stdlib.h> /* atol() */ #include <stdlib.h> /* atol() */
29 33
30 34 #include "utils/env.h" #include "utils/env.h"
 
... ... static int x_error_check(Display *dpy, XErrorEvent *error_event);
56 60 #endif #endif
57 61 static void set_terminal_title(const char *path); static void set_terminal_title(const char *path);
58 62
59 /* Add indirections level to functions of the xlib. */
60 63 #if !defined(_WIN32) && defined(HAVE_X11) #if !defined(_WIN32) && defined(HAVE_X11)
64 #ifdef DYN_X11
65
66 static void try_dynload_xlib(void);
67 static int dynload_xlib(void);
68 static void set_xlib_stubs(void);
69 static void unload_xlib(void);
70
71 static Display *XOpenDisplayStub(_Xconst char name[]);
72 static Status XGetWMNameStub(Display *display, Window w,
73 XTextProperty* text_prop_return);
74 static XErrorHandler XSetErrorHandlerStub(XErrorHandler handler);
75 static int XFreeStub(void* data);
76
77 /* Handle of dynamically loaded xlib. */
78 static void *xlib_handle;
79
80 /* Indirection level for functions of the xlib. */
81 static typeof(XOpenDisplay) *XOpenDisplayWrapper;
82 static typeof(XGetWMName) *XGetWMNameWrapper;
83 static typeof(XSetErrorHandler) *XSetErrorHandlerWrapper;
84 static typeof(XFree) *XFreeWrapper;
85
86 #else
87
88 /* Indirection level for functions of the xlib. */
61 89 static typeof(XOpenDisplay) *XOpenDisplayWrapper = &XOpenDisplay; static typeof(XOpenDisplay) *XOpenDisplayWrapper = &XOpenDisplay;
62 90 static typeof(XGetWMName) *XGetWMNameWrapper = &XGetWMName; static typeof(XGetWMName) *XGetWMNameWrapper = &XGetWMName;
63 91 static typeof(XSetErrorHandler) *XSetErrorHandlerWrapper = &XSetErrorHandler; static typeof(XSetErrorHandler) *XSetErrorHandlerWrapper = &XSetErrorHandler;
64 92 static typeof(XFree) *XFreeWrapper = &XFree; static typeof(XFree) *XFreeWrapper = &XFree;
93
94 #endif
65 95 #endif #endif
66 96
67 97 void void
 
... ... save_term_title()
117 147 Display *x11_display = 0; Display *x11_display = 0;
118 148 Window x11_window = 0; Window x11_window = 0;
119 149
150 #ifdef DYN_X11
151 try_dynload_xlib();
152 #endif
153
120 154 /* use X to determine current window title */ /* use X to determine current window title */
121 155 if(get_x11_disp_and_win(&x11_display, &x11_window)) if(get_x11_disp_and_win(&x11_display, &x11_window))
122 156 get_x11_window_title(x11_display, x11_window, title_state.title, get_x11_window_title(x11_display, x11_window, title_state.title,
 
... ... restore_term_title()
135 169 #else #else
136 170 if(title_state.title[0] != '\0') if(title_state.title[0] != '\0')
137 171 printf("\033]2;%s\007", title_state.title); printf("\033]2;%s\007", title_state.title);
172
173 #if defined(HAVE_X11) && defined(DYN_X11)
174 unload_xlib();
175 #endif
138 176 #endif #endif
139 177 } }
140 178
 
... ... set_terminal_title(const char *path)
196 234 #endif #endif
197 235 } }
198 236
237 #if !defined(_WIN32) && defined(HAVE_X11) && defined(DYN_X11)
238
239 /* Tries to load xlib dynamically, falls back to stubs that do nothing in case
240 * of failure. */
241 static void
242 try_dynload_xlib(void)
243 {
244 const int xlib_dynloaded_successfully = dynload_xlib();
245 if(!xlib_dynloaded_successfully)
246 {
247 set_xlib_stubs();
248 }
249 }
250
251 /* Loads libX11 library. Returns non-zero when library is loaded
252 * successfully. */
253 static int
254 dynload_xlib(void)
255 {
256 xlib_handle = dlopen("libX11.so", RTLD_NOW);
257 if(xlib_handle == NULL)
258 {
259 return 0;
260 }
261 XOpenDisplayWrapper = dlsym(xlib_handle, "XOpenDisplay");
262 XGetWMNameWrapper = dlsym(xlib_handle, "XGetWMName");
263 XSetErrorHandlerWrapper = dlsym(xlib_handle, "XSetErrorHandler");
264 XFreeWrapper = dlsym(xlib_handle, "XFree");
265 return XOpenDisplayWrapper != NULL && XGetWMNameWrapper != NULL &&
266 XSetErrorHandlerWrapper != NULL && XFreeWrapper != NULL;
267 }
268
269 /* Sets xlib function pointers to stubs. */
270 static void
271 set_xlib_stubs(void)
272 {
273 XOpenDisplayWrapper = &XOpenDisplayStub;
274 XGetWMNameWrapper = &XGetWMNameStub;
275 XSetErrorHandlerWrapper = &XSetErrorHandlerStub;
276 XFreeWrapper = &XFreeStub;
277 }
278
279 /* Unloads previously loaded libX11 library. */
280 static void
281 unload_xlib(void)
282 {
283 (void)dlclose(xlib_handle);
284 xlib_handle = NULL;
285 }
286
287 /* XOpenDisplay() function stub, which does nothing. */
288 static Display *
289 XOpenDisplayStub(_Xconst char name[])
290 {
291 return NULL;
292 }
293
294 /* XGetWMName() function stub, which does nothing. */
295 static Status
296 XGetWMNameStub(Display *display, Window w, XTextProperty* text_prop_return)
297 {
298 return 0;
299 }
300
301 /* XSetErrorHandler() function stub, which does nothing. */
302 static XErrorHandler
303 XSetErrorHandlerStub(XErrorHandler handler)
304 {
305 return NULL;
306 }
307
308 /* XFree() function stub, which does nothing. */
309 static int
310 XFreeStub(void* data)
311 {
312 return 0;
313 }
314
315 #endif
316
199 317 /* vim: set tabstop=2 softtabstop=2 shiftwidth=2 noexpandtab cinoptions-=(0 : */ /* vim: set tabstop=2 softtabstop=2 shiftwidth=2 noexpandtab cinoptions-=(0 : */
200 318 /* vim: set cinoptions+=t0 : */ /* vim: set cinoptions+=t0 : */
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/vifm

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/vifm

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master