diff --git a/CMakeLists.txt b/CMakeLists.txt index 8e8885b4f..8f2a337f1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -163,6 +163,7 @@ set(SRCS src/common/format.c src/common/parsing.c src/common/settings.c + src/common/library.c src/detection/plasma.c src/detection/gtk.c src/detection/terminalShell.c diff --git a/src/common/library.c b/src/common/library.c new file mode 100644 index 000000000..4f34a371c --- /dev/null +++ b/src/common/library.c @@ -0,0 +1,27 @@ +#include "fastfetch.h" + +#include + +//common/libraries.c +void* ffLibraryLoad(const FFstrbuf* userProvidedName, ...) +{ + if(userProvidedName->length > 0) + return dlopen(userProvidedName->chars, RTLD_LAZY); + + va_list defaultNames; + va_start(defaultNames, userProvidedName); + + void* result = NULL; + + while(result == NULL) + { + const char* name = va_arg(defaultNames, const char*); + if(name == NULL) + break; + result = dlopen(name, RTLD_LAZY); + } + + va_end(defaultNames); + + return result; +} diff --git a/src/common/settings.c b/src/common/settings.c index ba3f073df..cd35acee0 100644 --- a/src/common/settings.c +++ b/src/common/settings.c @@ -5,7 +5,7 @@ #ifdef FF_HAVE_GIO #include -#define FF_LIBRARY_DATA_LOAD_INIT(dataObject, libraryName, userLibraryName) \ +#define FF_LIBRARY_DATA_LOAD_INIT(dataObject, userLibraryName, ...) \ static dataObject data; \ static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; \ static FFInitState initState = FF_INITSTATE_UNINITIALIZED; \ @@ -15,8 +15,8 @@ return initState == FF_INITSTATE_SUCCESSFUL ? &data : NULL; \ } \ initState = FF_INITSTATE_SUCCESSFUL; \ - void* libraryHandle = dlopen(userLibraryName.length == 0 ? libraryName : userLibraryName.chars, RTLD_LAZY); \ - if(dlerror() != NULL || libraryHandle == NULL) { \ + void* libraryHandle = ffLibraryLoad(&userLibraryName, __VA_ARGS__, NULL); \ + if(libraryHandle == NULL) { \ initState = FF_INITSTATE_FAILED; \ pthread_mutex_unlock(&mutex); \ return NULL; \ @@ -24,7 +24,7 @@ #define FF_LIBRARY_DATA_LOAD_SYMBOL(symbolName) \ data.ff ## symbolName = dlsym(libraryHandle, #symbolName); \ - if(dlerror() != NULL || data.ff ## symbolName == NULL) { \ + if(data.ff ## symbolName == NULL) { \ dlclose(libraryHandle); \ initState = FF_INITSTATE_FAILED; \ pthread_mutex_unlock(&mutex); \ @@ -80,7 +80,7 @@ typedef struct GSettingsData static const GSettingsData* getGSettingsData(FFinstance* instance) { - FF_LIBRARY_DATA_LOAD_INIT(GSettingsData, "libgio-2.0.so", instance->config.libGIO) + FF_LIBRARY_DATA_LOAD_INIT(GSettingsData, instance->config.libGIO, "libgio-2.0.so", "libgio-2.0.so.0"); FF_LIBRARY_DATA_LOAD_SYMBOL(g_settings_schema_source_lookup) FF_LIBRARY_DATA_LOAD_SYMBOL(g_settings_schema_has_key) @@ -152,7 +152,7 @@ typedef struct DConfData static const DConfData* getDConfData(FFinstance* instance) { - FF_LIBRARY_DATA_LOAD_INIT(DConfData, "libdconf.so", instance->config.libDConf) + FF_LIBRARY_DATA_LOAD_INIT(DConfData, instance->config.libDConf, "libdconf.so", "libdconf.so.1"); FF_LIBRARY_DATA_LOAD_SYMBOL(dconf_client_read_full) FF_LIBRARY_DATA_LOAD_SYMBOL(dconf_client_new) @@ -222,7 +222,7 @@ typedef struct XFConfData static const XFConfData* getXFConfData(FFinstance* instance) { - FF_LIBRARY_DATA_LOAD_INIT(XFConfData, "libxfconf-0.so", instance->config.libXFConf) + FF_LIBRARY_DATA_LOAD_INIT(XFConfData, instance->config.libXFConf, "libxfconf-0.so", "libxfconf-0.so.3"); FF_LIBRARY_DATA_LOAD_SYMBOL(xfconf_channel_get) FF_LIBRARY_DATA_LOAD_SYMBOL(xfconf_channel_has_property) diff --git a/src/detection/displayserver/wayland.c b/src/detection/displayserver/wayland.c index fa8cca1d9..02dd0d167 100644 --- a/src/detection/displayserver/wayland.c +++ b/src/detection/displayserver/wayland.c @@ -78,7 +78,7 @@ void ffdsConnectWayland(const FFinstance* instance, FFDisplayServerResult* resul if(getenv("XDG_RUNTIME_DIR") == NULL) return; - FF_LIBRARY_LOAD(wayland, "libwayland-client.so", instance->config.libWayland,); + FF_LIBRARY_LOAD(wayland, instance->config.libWayland, , "libwayland-client.so", "libwayland-client.so.0") FF_LIBRARY_LOAD_SYMBOL(wayland, wl_display_connect,) FF_LIBRARY_LOAD_SYMBOL(wayland, wl_display_dispatch,) diff --git a/src/detection/displayserver/xcb.c b/src/detection/displayserver/xcb.c index 8b571b6a6..80b432fe3 100644 --- a/src/detection/displayserver/xcb.c +++ b/src/detection/displayserver/xcb.c @@ -89,7 +89,7 @@ static void xcbDetectWMfromEWMH(XcbPropertyData* data, xcb_connection_t* connect void ffdsConnectXcb(const FFinstance* instance, FFDisplayServerResult* result) { - FF_LIBRARY_LOAD(xcb, "libxcb.so", instance->config.libXcb,) + FF_LIBRARY_LOAD(xcb, instance->config.libXcb, , "libxcb.so", "libxcb.so.1") FF_LIBRARY_LOAD_SYMBOL(xcb, xcb_connect,) FF_LIBRARY_LOAD_SYMBOL(xcb, xcb_get_setup,) FF_LIBRARY_LOAD_SYMBOL(xcb, xcb_setup_roots_iterator,) @@ -324,7 +324,7 @@ static void xcbRandrHandleScreen(XcbRandrData* data, xcb_screen_t* screen) void ffdsConnectXcbRandr(const FFinstance* instance, FFDisplayServerResult* result) { - FF_LIBRARY_LOAD(xcbRandr, "libxcb-randr.so", instance->config.libXcbRandr,) + FF_LIBRARY_LOAD(xcbRandr, instance->config.libXcbRandr, , "libxcb-randr.so", "libxcb-randr.so.0") FF_LIBRARY_LOAD_SYMBOL(xcbRandr, xcb_connect,) FF_LIBRARY_LOAD_SYMBOL(xcbRandr, xcb_get_setup,) FF_LIBRARY_LOAD_SYMBOL(xcbRandr, xcb_setup_roots_iterator,) diff --git a/src/detection/displayserver/xlib.c b/src/detection/displayserver/xlib.c index 4717e97a2..87ffe0aab 100644 --- a/src/detection/displayserver/xlib.c +++ b/src/detection/displayserver/xlib.c @@ -52,7 +52,7 @@ static void x11DetectWMFromEWMH(X11PropertyData* data, Display* display, FFDispl void ffdsConnectXlib(const FFinstance* instance, FFDisplayServerResult* result) { - FF_LIBRARY_LOAD(x11, "libX11.so", instance->config.libX11,) + FF_LIBRARY_LOAD(x11, instance->config.libX11, , "libX11.so", "libX11.so.6", "libX11-xcb.so", "libX11-xcb.so.1") FF_LIBRARY_LOAD_SYMBOL(x11, XOpenDisplay,) FF_LIBRARY_LOAD_SYMBOL(x11, XCloseDisplay,) @@ -256,7 +256,7 @@ static void xrandrHandleScreen(XrandrData* data, Screen* screen) void ffdsConnectXrandr(const FFinstance* instance, FFDisplayServerResult* result) { - FF_LIBRARY_LOAD(xrandr, "libXrandr.so", instance->config.libXrandr,); + FF_LIBRARY_LOAD(xrandr, instance->config.libXrandr, , "libXrandr.so", "libXrandr.so.2") FF_LIBRARY_LOAD_SYMBOL(xrandr, XOpenDisplay,) FF_LIBRARY_LOAD_SYMBOL(xrandr, XCloseDisplay,) diff --git a/src/fastfetch.h b/src/fastfetch.h index ea5d5ea63..3e0aad6ea 100644 --- a/src/fastfetch.h +++ b/src/fastfetch.h @@ -292,14 +292,14 @@ typedef enum FFInitState #define FF_LIBRARY_SYMBOL(symbolName) \ __typeof__(&symbolName) ff ## symbolName; -#define FF_LIBRARY_LOAD(libraryObjectName, libraryName, userLibraryName, returnValue) \ - void* libraryObjectName = dlopen(userLibraryName.length == 0 ? libraryName : userLibraryName.chars, RTLD_LAZY); \ - if(dlerror() != NULL || libraryObjectName == NULL) \ +#define FF_LIBRARY_LOAD(libraryObjectName, userLibraryName, returnValue, ...) \ + void* libraryObjectName = ffLibraryLoad(&userLibraryName, __VA_ARGS__, NULL);\ + if(libraryObjectName == NULL) \ return returnValue; #define FF_LIBRARY_LOAD_SYMBOL_ADRESS(library, symbolMapping, symbolName, returnValue) \ symbolMapping = dlsym(library, #symbolName); \ - if(dlerror() != NULL || symbolMapping == NULL) \ + if(symbolMapping == NULL) \ { \ dlclose(library); \ return returnValue; \ @@ -359,6 +359,9 @@ bool ffParsePropFileConfig(const FFinstance* instance, const char* relativeFile, //common/processing.c void ffProcessAppendStdOut(FFstrbuf* buffer, char* const argv[]); +//common/libraries.c +void* ffLibraryLoad(const FFstrbuf* userProvidedName, ...); + //common/logo.c void ffLoadLogoSet(FFinstance* instance, const char* logo); void ffLoadLogo(FFinstance* instance); diff --git a/src/modules/gpu.c b/src/modules/gpu.c index b8030dd32..df79c4956 100644 --- a/src/modules/gpu.c +++ b/src/modules/gpu.c @@ -16,7 +16,7 @@ typedef struct GPUResult static void vulkanFillGPUs(FFinstance* instance, FFlist* results) { - FF_LIBRARY_LOAD(vulkan, "libvulkan.so", instance->config.libVulkan,) + FF_LIBRARY_LOAD(vulkan, instance->config.libVulkan, , "libvulkan.so", "libvulkan.so.1") FF_LIBRARY_LOAD_SYMBOL(vulkan, vkCreateInstance,) FF_LIBRARY_LOAD_SYMBOL(vulkan, vkDestroyInstance,) FF_LIBRARY_LOAD_SYMBOL(vulkan, vkEnumeratePhysicalDevices,) @@ -104,8 +104,7 @@ static void vulkanFillGPUs(FFinstance* instance, FFlist* results) static void pciFillGPUs(FFinstance* instance, FFlist* results) { - FF_LIBRARY_LOAD(pci, "libpci.so", instance->config.libPCI,) - + FF_LIBRARY_LOAD(pci, instance->config.libPCI, , "libpci.so", "libpci.so.3") FF_LIBRARY_LOAD_SYMBOL(pci, pci_alloc,) FF_LIBRARY_LOAD_SYMBOL(pci, pci_init,) FF_LIBRARY_LOAD_SYMBOL(pci, pci_scan_bus,) diff --git a/src/modules/packages.c b/src/modules/packages.c index 08de71977..a7eb69b3e 100644 --- a/src/modules/packages.c +++ b/src/modules/packages.c @@ -14,7 +14,7 @@ static uint32_t getRpmPackageCount(FFinstance* instance) { - FF_LIBRARY_LOAD(rpm, "librpm.so", instance->config.librpm, 0); + FF_LIBRARY_LOAD(rpm, instance->config.libRpm, 0, "librpm.so", "librpm.so.4") FF_LIBRARY_LOAD_SYMBOL(rpm, rpmReadConfigFiles, 0) FF_LIBRARY_LOAD_SYMBOL(rpm, rpmtsCreate, 0) FF_LIBRARY_LOAD_SYMBOL(rpm, rpmtsInitIterator, 0)