summaryrefslogtreecommitdiff
path: root/src/drivers/X11
diff options
context:
space:
mode:
authorManoloFLTK <41016272+ManoloFLTK@users.noreply.github.com>2020-10-04 08:20:50 +0200
committerManoloFLTK <41016272+ManoloFLTK@users.noreply.github.com>2020-10-04 08:21:07 +0200
commit0c55cd1aca19b57a9b8837d1672ae260cfca4d78 (patch)
treea9f5b10b3ec558d7525f24037cafc86ce796d9e5 /src/drivers/X11
parent46598229a9605b25e3da5e0d7ad41343cf429497 (diff)
Create Fl_X11_System_Driver::dlopen_or_dlsym() for run-time addresses.
The intent is to gather in a single place of the X11 platform source code all variable elements when using dlopen() and dlsym() system functions (e.g., .so vs .dylib extension name, is RTLD_DEFAULT available, locations to be sought). Member function Fl_System_Driver::load() is created only to support Fl_Plugin_Manager::load().
Diffstat (limited to 'src/drivers/X11')
-rw-r--r--src/drivers/X11/Fl_X11_Screen_Driver.cxx9
-rw-r--r--src/drivers/X11/Fl_X11_System_Driver.H1
-rw-r--r--src/drivers/X11/Fl_X11_System_Driver.cxx80
3 files changed, 73 insertions, 17 deletions
diff --git a/src/drivers/X11/Fl_X11_Screen_Driver.cxx b/src/drivers/X11/Fl_X11_Screen_Driver.cxx
index a09768975..30482cf8f 100644
--- a/src/drivers/X11/Fl_X11_Screen_Driver.cxx
+++ b/src/drivers/X11/Fl_X11_Screen_Driver.cxx
@@ -1,7 +1,7 @@
//
// Definition of X11 Screen interface
//
-// Copyright 1998-2018 by Bill Spitzak and others.
+// Copyright 1998-2020 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file. If this
@@ -19,7 +19,8 @@
#include "Fl_X11_Screen_Driver.H"
#include "../Xlib/Fl_Font.H"
#include "Fl_X11_Window_Driver.H"
-#include "../../Fl_System_Driver.H"
+#include "Fl_X11_System_Driver.H"
+#include "../Posix/Fl_Posix_System_Driver.H"
#include "../Xlib/Fl_Xlib_Graphics_Driver.H"
#include <FL/Fl.H>
#include <FL/platform.H>
@@ -285,9 +286,7 @@ void Fl_X11_Screen_Driver::init() {
static XRRSizes_type XRRSizes_f = NULL;
if (!XRRSizes_f) {
- void *libxrandr_addr = dlopen("libXrandr.so.2", RTLD_LAZY);
- if (!libxrandr_addr) libxrandr_addr = Fl::system_driver()->dlopen("libXrandr.so");
- if (libxrandr_addr) XRRSizes_f = (XRRSizes_type)dlsym(libxrandr_addr, "XRRSizes");
+ XRRSizes_f = (XRRSizes_type)Fl_X11_System_Driver::dlopen_or_dlsym("libXrandr", "XRRSizes");
}
if (XRRSizes_f) {
int nscreens;
diff --git a/src/drivers/X11/Fl_X11_System_Driver.H b/src/drivers/X11/Fl_X11_System_Driver.H
index f8fddc741..004e474ae 100644
--- a/src/drivers/X11/Fl_X11_System_Driver.H
+++ b/src/drivers/X11/Fl_X11_System_Driver.H
@@ -67,6 +67,7 @@ public:
#if HAVE_DLSYM && HAVE_DLFCN_H
static bool probe_for_GTK(int major, int minor, void **ptr_gtk);
#endif
+ static void *dlopen_or_dlsym(const char *lib_name, const char *func_name = NULL);
};
#endif /* FL_X11_SYSTEM_DRIVER_H */
diff --git a/src/drivers/X11/Fl_X11_System_Driver.cxx b/src/drivers/X11/Fl_X11_System_Driver.cxx
index c99f31f86..88fa416f5 100644
--- a/src/drivers/X11/Fl_X11_System_Driver.cxx
+++ b/src/drivers/X11/Fl_X11_System_Driver.cxx
@@ -2,7 +2,7 @@
// Definition of Posix system driver
// for the Fast Light Tool Kit (FLTK).
//
-// Copyright 2010-2017 by Bill Spitzak and others.
+// Copyright 2010-2020 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file. If this
@@ -365,15 +365,7 @@ void Fl_X11_System_Driver::newUUID(char *uuidBuffer)
static gener_f_type uuid_generate_f = NULL;
if (!looked_for_uuid_generate) {
looked_for_uuid_generate = true;
-# ifdef RTLD_DEFAULT
- uuid_generate_f = (gener_f_type)dlsym(RTLD_DEFAULT, "uuid_generate");
-# endif
- if (!uuid_generate_f) {
- void *libuuid = this->dlopen("libuuid.so");
- if (libuuid) {
- uuid_generate_f = (gener_f_type)dlsym(libuuid, "uuid_generate");
- }
- }
+ uuid_generate_f = (gener_f_type)Fl_X11_System_Driver::dlopen_or_dlsym("libuuid", "uuid_generate");
}
if (uuid_generate_f) {
uuid_generate_f(b);
@@ -572,6 +564,70 @@ int Fl_X11_System_Driver::utf8locale() {
return ret;
}
+#if HAVE_DLSYM && HAVE_DLFCN_H
+static void* quadruple_dlopen(const char *libname)
+{
+ char filename2[FL_PATH_MAX];
+ sprintf(filename2, "%s.so", libname);
+ void *ptr = dlopen(filename2, RTLD_LAZY | RTLD_GLOBAL);
+ if (!ptr) {
+ sprintf(filename2, "%s.so.2", libname);
+ ptr = dlopen(filename2, RTLD_LAZY | RTLD_GLOBAL);
+ if (!ptr) {
+ sprintf(filename2, "%s.so.1", libname);
+ ptr = dlopen(filename2, RTLD_LAZY | RTLD_GLOBAL);
+ if (!ptr) {
+ sprintf(filename2, "%s.so.0", libname);
+ ptr = dlopen(filename2, RTLD_LAZY | RTLD_GLOBAL);
+ }
+ }
+ }
+ return ptr;
+}
+#endif
+
+/**
+ Returns the run-time address of a function or of a shared library.
+ \param lib_name shared library name (without its extension) or NULL to search the function in the running program
+ \param func_name function name or NULL
+ \return the address of the function (when func_name != NULL) or of the shared library, or NULL if not found.
+ */
+void *Fl_X11_System_Driver::dlopen_or_dlsym(const char *lib_name, const char *func_name)
+{
+ void *lib_address = NULL;
+#if HAVE_DLSYM && HAVE_DLFCN_H
+ void *func_ptr = NULL;
+ if (func_name) {
+#ifdef RTLD_DEFAULT
+ func_ptr = dlsym(RTLD_DEFAULT, func_name);
+#else
+ void *p = dlopen(NULL, RTLD_LAZY);
+ func_ptr = dlsym(p, func_name);
+#endif
+ if (func_ptr) return func_ptr;
+ }
+#ifdef __APPLE_CC__ // allows testing on Darwin + XQuartz + fink
+ if (lib_name) {
+ char path[FL_PATH_MAX];
+ sprintf(path, "/opt/X11/lib/%s.dylib", lib_name);
+ lib_address = dlopen(path, RTLD_LAZY | RTLD_GLOBAL);
+ if (!lib_address) {
+ sprintf(path, "/opt/sw/lib/%s.dylib", lib_name);
+ lib_address = dlopen(path, RTLD_LAZY | RTLD_GLOBAL);
+ if (!lib_address) {
+ sprintf(path, "/sw/lib/%s.dylib", lib_name);
+ lib_address = dlopen(path, RTLD_LAZY | RTLD_GLOBAL);
+ }
+ }
+ }
+#else
+ if (lib_name) lib_address = quadruple_dlopen(lib_name);
+#endif // __APPLE_CC__
+ if (func_name && lib_address) return ::dlsym(lib_address, func_name);
+#endif // HAVE_DLFCN_H
+ return lib_address;
+}
+
#if HAVE_DLSYM && HAVE_DLFCN_H && defined(RTLD_DEFAULT)
bool Fl_X11_System_Driver::probe_for_GTK(int major, int minor, void **ptr_gtk) {
@@ -583,14 +639,14 @@ bool Fl_X11_System_Driver::probe_for_GTK(int major, int minor, void **ptr_gtk) {
*ptr_gtk = RTLD_DEFAULT; // Caution: NULL under linux, not-NULL under Darwin
} else {
// Try first with GTK3
- *ptr_gtk = Fl::system_driver()->dlopen("libgtk-3.so");
+ *ptr_gtk = Fl_X11_System_Driver::dlopen_or_dlsym("libgtk-3");
if (*ptr_gtk) {
#ifdef DEBUG
puts("selected GTK-3\n");
#endif
} else {
// Try then with GTK2
- *ptr_gtk = Fl::system_driver()->dlopen("libgtk-x11-2.0.so");
+ *ptr_gtk = Fl_X11_System_Driver::dlopen_or_dlsym("libgtk-x11-2.0");
#ifdef DEBUG
if (*ptr_gtk) {
puts("selected GTK-2\n");