From 039b69f4a7ddcf9ebbab20c7f100fa3a4518498a Mon Sep 17 00:00:00 2001 From: Linus Dierheimer Date: Tue, 11 May 2021 20:06:17 +0200 Subject: [PATCH] better ffSettings* functions --- completions/bash | 1 + src/common/detectGTK.c | 12 +-- src/common/init.c | 1 + src/common/settings.c | 190 +++++++++++++++++++++++++---------------- src/fastfetch.c | 3 + src/fastfetch.h | 3 +- src/modules/wmtheme.c | 23 +++-- 7 files changed, 149 insertions(+), 84 deletions(-) diff --git a/completions/bash b/completions/bash index 0b1045ffa..6848c005f 100644 --- a/completions/bash +++ b/completions/bash @@ -184,6 +184,7 @@ __fastfetch_completion() "--lib-gio" "--lib-DConf" "--lib-wayland" + "--lib-XFConf" "--battery-dir" "--load-config" ) diff --git a/src/common/detectGTK.c b/src/common/detectGTK.c index 574dd08bd..6f20af89f 100644 --- a/src/common/detectGTK.c +++ b/src/common/detectGTK.c @@ -48,18 +48,18 @@ static void detectGTKFromDConf(FFinstance* instance, FFGTKResult* result) if(ffStrbufIgnCaseCompS(&wmde->dePrettyName, "Cinnamon") == 0) { - themeName = ffSettingsGetStr(instance, "/org/cinnamon/desktop/interface/gtk-theme", "org.cinnamon.desktop.interface", NULL, "gtk-theme"); - iconsName = ffSettingsGetStr(instance, "/org/cinnamon/desktop/interface/icon-theme", "org.cinnamon.desktop.interface", NULL, "icon-theme"); - fontName = ffSettingsGetStr(instance, "/org/cinnamon/desktop/interface/font-name", "org.cinnamon.desktop.interface", NULL, "font-name"); + themeName = ffSettingsGet(instance, "/org/cinnamon/desktop/interface/gtk-theme", "org.cinnamon.desktop.interface", NULL, "gtk-theme", FF_VARIANT_TYPE_STRING).strValue; + iconsName = ffSettingsGet(instance, "/org/cinnamon/desktop/interface/icon-theme", "org.cinnamon.desktop.interface", NULL, "icon-theme", FF_VARIANT_TYPE_STRING).strValue; + fontName = ffSettingsGet(instance, "/org/cinnamon/desktop/interface/font-name", "org.cinnamon.desktop.interface", NULL, "font-name", FF_VARIANT_TYPE_STRING).strValue; } //Fallback + Gnome impl if(themeName == NULL) - themeName = ffSettingsGetStr(instance, "/org/gnome/desktop/interface/gtk-theme", "org.gnome.desktop.interface", NULL, "gtk-theme"); + themeName = ffSettingsGet(instance, "/org/gnome/desktop/interface/gtk-theme", "org.gnome.desktop.interface", NULL, "gtk-theme", FF_VARIANT_TYPE_STRING).strValue; if(iconsName == NULL) - iconsName = ffSettingsGetStr(instance, "/org/gnome/desktop/interface/icon-theme", "org.gnome.desktop.interface", NULL, "icon-theme"); + iconsName = ffSettingsGet(instance, "/org/gnome/desktop/interface/icon-theme", "org.gnome.desktop.interface", NULL, "icon-theme", FF_VARIANT_TYPE_STRING).strValue; if(fontName == NULL) - fontName = ffSettingsGetStr(instance, "/org/gnome/desktop/interface/font-name", "org.gnome.desktop.interface", NULL, "font-name"); + fontName = ffSettingsGet(instance, "/org/gnome/desktop/interface/font-name", "org.gnome.desktop.interface", NULL, "font-name", FF_VARIANT_TYPE_STRING).strValue; pthread_mutex_unlock(&mutex); applyGTKDConfSettings(result, themeName, iconsName, fontName); diff --git a/src/common/init.c b/src/common/init.c index 3f5976d7b..9d056c3d9 100644 --- a/src/common/init.c +++ b/src/common/init.c @@ -160,6 +160,7 @@ static void defaultConfig(FFconfig* config) ffStrbufInitA(&config->libGIO, 1); ffStrbufInitA(&config->libDConf, 1); ffStrbufInitA(&config->libWayland, 1); + ffStrbufInitA(&config->libXFConf, 1); ffStrbufInitA(&config->diskFolders, 1); diff --git a/src/common/settings.c b/src/common/settings.c index 1e744955d..656fa02a3 100644 --- a/src/common/settings.c +++ b/src/common/settings.c @@ -2,8 +2,30 @@ #include #include -#include -#include +#include // Also included gio/gio.h + +typedef void* DynamicLibrary; + +#define FF_VARIANT_NULL ((FFvariant){.strValue = NULL}) + +#define FF_LIBRARY_LOAD(libraryNameUser, libraryNameDefault, mutex) dlopen(libraryNameUser.length == 0 ? libraryNameDefault : libraryNameUser.chars, RTLD_LAZY); \ + if(dlerror() != NULL) { \ + pthread_mutex_unlock(&mutex); \ + return FF_VARIANT_NULL; \ + } + +#define FF_LIBRARY_LOAD_SYMBOL(library, symbolName, mutex) dlsym(library, symbolName); \ + if(dlerror() != NULL) { \ + pthread_mutex_unlock(&mutex); \ + dlclose(library); \ + return FF_VARIANT_NULL; \ + } + +#define FF_LIBRARY_ERROR_RETURN(library, mutex) { \ + pthread_mutex_unlock(&mutex); \ + dlclose(library); \ + return FF_VARIANT_NULL; \ +} typedef struct GVariantGetters { @@ -11,23 +33,19 @@ typedef struct GVariantGetters gboolean(*ffg_variant_get_boolean)(GVariant*); } GVariantGetters; -static inline void initGVariantGetters(void* library, GVariantGetters* variantGetters) -{ - variantGetters->ffg_variant_get_string = dlsym(library, "g_variant_get_string"); - variantGetters->ffg_variant_get_boolean = dlsym(library, "g_variant_get_boolean"); -} - -#define FF_VARIANT_NULL (FFvariant)(const char*)NULL +#define FF_LIBRARY_GVARIANT_GETTERS_INIT(library, object, mutex) \ + object.ffg_variant_get_string = FF_LIBRARY_LOAD_SYMBOL(library, "g_variant_get_string", mutex); \ + object.ffg_variant_get_boolean = FF_LIBRARY_LOAD_SYMBOL(library, "g_variant_get_boolean", mutex); static FFvariant getGVariantValue(GVariant* variant, FFvarianttype type, GVariantGetters* variantGetters) { if(variant == NULL) return FF_VARIANT_NULL; - if(type == FF_VARIANT_TYPE_STRING && variantGetters->ffg_variant_get_string != NULL) + if(type == FF_VARIANT_TYPE_STRING) return (FFvariant) variantGetters->ffg_variant_get_string(variant, NULL); - if(type == FF_VARIANT_TYPE_BOOL && variantGetters->ffg_variant_get_boolean != NULL) + if(type == FF_VARIANT_TYPE_BOOL) return (FFvariant) { .boolValue = (bool) variantGetters->ffg_variant_get_boolean(variant), .boolValueSet = true}; return FF_VARIANT_NULL; @@ -46,13 +64,14 @@ static FFvariant getDConfValue(DConfData* data, const char* key, FFvarianttype t return FF_VARIANT_NULL; GVariant* variant = data->ffdconf_client_read_full(data->client, key, DCONF_READ_FLAGS_NONE, NULL); + if(variant != NULL) + return getGVariantValue(variant, type, &data->variantGetters); - if(variant == NULL) - variant = data->ffdconf_client_read_full(data->client, key, DCONF_READ_USER_VALUE, NULL); - - if(variant == NULL) - variant = data->ffdconf_client_read_full(data->client, key, DCONF_READ_DEFAULT_VALUE, NULL); + variant = data->ffdconf_client_read_full(data->client, key, DCONF_READ_USER_VALUE, NULL); + if(variant != NULL) + return getGVariantValue(variant, type, &data->variantGetters); + variant = data->ffdconf_client_read_full(data->client, key, DCONF_READ_DEFAULT_VALUE, NULL); return getGVariantValue(variant, type, &data->variantGetters); } @@ -70,31 +89,18 @@ FFvariant ffSettingsGetDConf(FFinstance* instance, const char* key, FFvarianttyp pthread_mutex_unlock(&mutex); return getDConfValue(&data, key, type); } - init = true; data.client = NULL; //error indicator - void* library = dlopen(instance->config.libDConf.length == 0 ? "libdconf.so" : instance->config.libDConf.chars, RTLD_LAZY); - if(library == NULL) - { - pthread_mutex_unlock(&mutex); - return FF_VARIANT_NULL; - } + DynamicLibrary library = FF_LIBRARY_LOAD(instance->config.libDConf, "libdocnf.so", mutex); - data.ffdconf_client_read_full = dlsym(library, "dconf_client_read_full"); - initGVariantGetters(library, &data.variantGetters); + data.ffdconf_client_read_full = FF_LIBRARY_LOAD_SYMBOL(library, "dconf_client_read_full", mutex); + FF_LIBRARY_GVARIANT_GETTERS_INIT(library, data.variantGetters, mutex); - DConfClient*(*ffdconf_client_new)(void) = dlsym(library, "dconf_client_new"); - - if( - data.ffdconf_client_read_full == NULL || - (data.client = ffdconf_client_new()) == NULL - ) { - pthread_mutex_unlock(&mutex); - dlclose(library); - return FF_VARIANT_NULL; - } + DConfClient*(*ffdconf_client_new)(void) = FF_LIBRARY_LOAD_SYMBOL(library, "dconf_client_new", mutex); + if((data.client = ffdconf_client_new()) == NULL) + FF_LIBRARY_ERROR_RETURN(library, mutex); pthread_mutex_unlock(&mutex); return getDConfValue(&data, key, type); @@ -129,13 +135,14 @@ static FFvariant getGSettingsValue(GSettingsData* data, const char* schemaName, return FF_VARIANT_NULL; GVariant* variant = data->ffg_settings_get_value(settings, key); + if(variant != NULL) + return getGVariantValue(variant, type, &data->variantGetters); - if(variant == NULL) - variant = data->ffg_settings_get_user_value(settings, key); - - if(variant == NULL) - variant = data->ffg_settings_get_default_value(settings, key); + variant = data->ffg_settings_get_user_value(settings, key); + if(variant != NULL) + return getGVariantValue(variant, type, &data->variantGetters); + variant = data->ffg_settings_get_default_value(settings, key); return getGVariantValue(variant, type, &data->variantGetters); } @@ -153,42 +160,23 @@ FFvariant ffSettingsGetGsettings(FFinstance* instance, const char* schemaName, c pthread_mutex_unlock(&mutex); return getGSettingsValue(&data, schemaName, path, key, type); } - init = true; data.schemaSource = NULL; //error indicator - void* library = dlopen(instance->config.libGIO.length == 0 ? "libgio-2.0.so" : instance->config.libGIO.chars, RTLD_LAZY); - if(library == NULL) - { - pthread_mutex_unlock(&mutex); - return FF_VARIANT_NULL; - } + DynamicLibrary library = FF_LIBRARY_LOAD(instance->config.libGIO, "libgio-2.0.so", mutex); - data.ffg_settings_schema_source_lookup = dlsym(library, "g_settings_schema_source_lookup"); - data.ffg_settings_schema_has_key = dlsym(library, "g_settings_schema_has_key"); - data.ffg_settings_new_full = dlsym(library, "g_settings_new_full"); - data.ffg_settings_get_value = dlsym(library, "g_settings_get_value"); - data.ffg_settings_get_user_value = dlsym(library, "g_settings_get_user_value"); - data.ffg_settings_get_default_value = dlsym(library, "g_settings_get_default_value"); - initGVariantGetters(library, &data.variantGetters); + data.ffg_settings_schema_source_lookup = FF_LIBRARY_LOAD_SYMBOL(library, "g_settings_schema_source_lookup", mutex); + data.ffg_settings_schema_has_key = FF_LIBRARY_LOAD_SYMBOL(library, "g_settings_schema_has_key", mutex); + data.ffg_settings_new_full = FF_LIBRARY_LOAD_SYMBOL(library, "g_settings_new_full", mutex); + data.ffg_settings_get_value = FF_LIBRARY_LOAD_SYMBOL(library, "g_settings_get_value", mutex); + data.ffg_settings_get_user_value = FF_LIBRARY_LOAD_SYMBOL(library, "g_settings_get_user_value", mutex); + data.ffg_settings_get_default_value = FF_LIBRARY_LOAD_SYMBOL(library, "g_settings_get_default_value", mutex); + FF_LIBRARY_GVARIANT_GETTERS_INIT(library, data.variantGetters, mutex); - GSettingsSchemaSource*(*ffg_settings_schema_source_get_default)(void) = dlsym(library, "g_settings_schema_source_get_default"); - - if( - data.ffg_settings_schema_source_lookup == NULL || - data.ffg_settings_schema_has_key == NULL || - data.ffg_settings_new_full == NULL || - data.ffg_settings_get_value == NULL || - data.ffg_settings_get_user_value == NULL || - data.ffg_settings_get_default_value == NULL || - ffg_settings_schema_source_get_default == NULL || - (data.schemaSource = ffg_settings_schema_source_get_default()) == NULL - ) { - pthread_mutex_unlock(&mutex); - dlclose(library); - return FF_VARIANT_NULL; - } + GSettingsSchemaSource*(*ffg_settings_schema_source_get_default)(void) = FF_LIBRARY_LOAD_SYMBOL(library, "g_settings_schema_source_get_default", mutex); + if((data.schemaSource = ffg_settings_schema_source_get_default()) == NULL) + FF_LIBRARY_ERROR_RETURN(library, mutex); pthread_mutex_unlock(&mutex); return getGSettingsValue(&data, schemaName, path, key, type); @@ -203,9 +191,67 @@ FFvariant ffSettingsGet(FFinstance* instance, const char* dconfKey, const char* return ffSettingsGetGsettings(instance, gsettingsSchemaName, gsettingsPath, gsettingsKey, FF_VARIANT_TYPE_STRING); } -const char* ffSettingsGetStr(FFinstance* instance, const char* dconfKey, const char* gsettingsSchemaName, const char* gsettingsPath, const char* gsettingsKey) +typedef struct _XfconfChannel XfconfChannel; // /usr/include/xfce4/xfconf-0/xfconf/xfconf-channel.h#L39 + +typedef struct XFConfData { - return ffSettingsGet(instance, dconfKey, gsettingsSchemaName, gsettingsPath, gsettingsKey, FF_VARIANT_TYPE_STRING).strValue; + bool init; + XfconfChannel*(*ffxfconf_channel_get)(const gchar*); + gboolean(*ffxfconf_channel_has_property)(XfconfChannel*, const gchar*); + gchar*(*ffxfconf_channel_get_string)(XfconfChannel*, const gchar*, const gchar*); + gboolean(*ffxfconf_channel_get_bool)(XfconfChannel*, const gchar*, gboolean); +} XFConfData; + +static FFvariant getXFConfValue(XFConfData* data, const char* channelName, const char* propertyName, FFvarianttype type) +{ + if(!data->init) + return FF_VARIANT_NULL; + + XfconfChannel* channel = data->ffxfconf_channel_get(channelName); // Never fails according to documentation but rather returns an empty channel + + if(!data->ffxfconf_channel_has_property(channel, propertyName)) + return FF_VARIANT_NULL; + + if(type == FF_VARIANT_TYPE_STRING) + return (FFvariant) {.strValue = data->ffxfconf_channel_get_string(channel, propertyName, NULL)}; + + if(type == FF_VARIANT_TYPE_BOOL) + return (FFvariant) {.boolValue = data->ffxfconf_channel_get_bool(channel, propertyName, false), .boolValueSet = true}; + + return FF_VARIANT_NULL; +} + +FFvariant ffSettingsGetXFConf(FFinstance* instance, const char* channelName, const char* propertyName, FFvarianttype type) +{ + static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; + static bool init = false; + + static XFConfData data; + + pthread_mutex_lock(&mutex); + + if(init) + { + pthread_mutex_unlock(&mutex); + return getXFConfValue(&data, channelName, propertyName, type); + } + init = true; + + data.init = false; //error indicator + + DynamicLibrary library = FF_LIBRARY_LOAD(instance->config.libXFConf, "libxfconf-0.so", mutex); + + data.ffxfconf_channel_get = FF_LIBRARY_LOAD_SYMBOL(library, "xfconf_channel_get", mutex); + data.ffxfconf_channel_has_property = FF_LIBRARY_LOAD_SYMBOL(library, "xfconf_channel_has_property", mutex); + data.ffxfconf_channel_get_string = FF_LIBRARY_LOAD_SYMBOL(library, "xfconf_channel_get_string", mutex); + data.ffxfconf_channel_get_bool = FF_LIBRARY_LOAD_SYMBOL(library, "xfconf_channel_get_bool", mutex); + + gboolean(*ffxfconf_init)(GError **) = FF_LIBRARY_LOAD_SYMBOL(library, "xfconf_init", mutex); + if((data.init = ffxfconf_init(NULL)) == FALSE) + FF_LIBRARY_ERROR_RETURN(library, mutex); + + pthread_mutex_unlock(&mutex); + return getXFConfValue(&data, channelName, propertyName, type); } #undef FF_VARIANT_NULL diff --git a/src/fastfetch.c b/src/fastfetch.c index b1d7d2fb5..b7cbb257c 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -117,6 +117,7 @@ static inline void printHelp() " --lib-gio \n" " --lib-DConf \n" " --lib-wayland \n" + " --lib-XFConf \n" "\n" "Module specific options:\n" " --disk-folders : A colon seperated list of folder paths for the disk output. Default is \"/:/home\"\n" @@ -884,6 +885,8 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con optionParseString(key, value, &instance->config.libDConf); else if(strcasecmp(key, "--lib-wayland") == 0) optionParseString(key, value, &instance->config.libWayland); + else if(strcasecmp(key, "--lib-XFConf") == 0) + optionParseString(key, value, &instance->config.libXFConf); else if(strcasecmp(key, "--disk-folders") == 0) optionParseString(key, value, &instance->config.diskFolders); else if(strcasecmp(key, "--battery-dir") == 0) diff --git a/src/fastfetch.h b/src/fastfetch.h index 7c86bc1f1..f2a741dc4 100644 --- a/src/fastfetch.h +++ b/src/fastfetch.h @@ -97,6 +97,7 @@ typedef struct FFconfig FFstrbuf libWayland; FFstrbuf libGIO; FFstrbuf libDConf; + FFstrbuf libXFConf; FFstrbuf diskFolders; @@ -275,7 +276,7 @@ void ffGetFontPretty(FFstrbuf* buffer, const FFstrbuf* name, double size); FFvariant ffSettingsGetDConf(FFinstance* instance, const char* key, FFvarianttype type); FFvariant ffSettingsGetGsettings(FFinstance* instance, const char* schemaName, const char* path, const char* key, FFvarianttype type); FFvariant ffSettingsGet(FFinstance* instance, const char* dconfKey, const char* gsettingsSchemaName, const char* gsettingsPath, const char* gsettingsKey, FFvarianttype type); -const char* ffSettingsGetStr(FFinstance* instance, const char* dconfKey, const char* gsettingsSchemaName, const char* gsettingsPath, const char* gsettingsKey); +FFvariant ffSettingsGetXFConf(FFinstance* instance, const char* channelName, const char* propertyName, FFvarianttype type); //common/detectPlasma.c const FFPlasmaResult* ffDetectPlasma(FFinstance* instance); diff --git a/src/modules/wmtheme.c b/src/modules/wmtheme.c index 9b9a5d189..c36684873 100644 --- a/src/modules/wmtheme.c +++ b/src/modules/wmtheme.c @@ -46,10 +46,10 @@ static void printWMThemeFromConfigFile(FFinstance* instance, const char* configF static void printMutter(FFinstance* instance) { - const char* theme = ffSettingsGetStr(instance, "/org/gnome/shell/extensions/user-theme/name", "org.gnome.shell.extensions.user-theme", NULL, "name"); + const char* theme = ffSettingsGet(instance, "/org/gnome/shell/extensions/user-theme/name", "org.gnome.shell.extensions.user-theme", NULL, "name", FF_VARIANT_TYPE_STRING).strValue; if(theme == NULL) - theme = ffSettingsGetStr(instance, "/org/gnome/desktop/wm/preferences/theme", "org.gnome.desktop.wm.preferences", NULL, "theme"); + theme = ffSettingsGet(instance, "/org/gnome/desktop/wm/preferences/theme", "org.gnome.desktop.wm.preferences", NULL, "theme", FF_VARIANT_TYPE_STRING).strValue; if(theme == NULL) { @@ -62,8 +62,8 @@ static void printMutter(FFinstance* instance) static void printMuffin(FFinstance* instance) { - const char* name = ffSettingsGetStr(instance, "/org/cinnamon/theme/name", "org.cinnamon.theme", NULL, "name"); - const char* theme = ffSettingsGetStr(instance, "/org/cinnamon/desktop/wm/preferences/theme", "org.cinnamon.desktop.wm.preferences", NULL, "theme"); + const char* name = ffSettingsGet(instance, "/org/cinnamon/theme/name", "org.cinnamon.theme", NULL, "name", FF_VARIANT_TYPE_STRING).strValue; + const char* theme = ffSettingsGet(instance, "/org/cinnamon/desktop/wm/preferences/theme", "org.cinnamon.desktop.wm.preferences", NULL, "theme", FF_VARIANT_TYPE_STRING).strValue; if(name == NULL && theme == NULL) { @@ -90,6 +90,19 @@ static void printMuffin(FFinstance* instance) } } +static void printXFWM4(FFinstance* instance) +{ + const char* theme = ffSettingsGetXFConf(instance, "xfwm4", "/general/theme", FF_VARIANT_TYPE_STRING).strValue; + + if(theme == NULL) + { + ffPrintError(instance, FF_WMTHEME_MODULE_NAME, 0, &instance->config.wmThemeKey, &instance->config.wmThemeFormat, FF_WMTHEME_NUM_FORMAT_ARGS, "Couldn't find xfwm4::/general/theme in XFConf"); + return; + } + + printWMTheme(instance, theme); +} + static void printOpenbox(FFinstance* instance, const FFstrbuf* dePrettyName) { FFstrbuf absolutePath; @@ -167,7 +180,7 @@ void ffPrintWMTheme(FFinstance* instance) if(ffStrbufIgnCaseCompS(&result->wmPrettyName, "KWin") == 0 || ffStrbufIgnCaseCompS(&result->wmPrettyName, "KDE") == 0 || ffStrbufIgnCaseCompS(&result->wmPrettyName, "Plasma") == 0) printWMThemeFromConfigFile(instance, "kwinrc", " theme=%s", "Breeze"); else if(ffStrbufIgnCaseCompS(&result->wmPrettyName, "Xfwm4") == 0 || ffStrbufIgnCaseCompS(&result->wmPrettyName, "Xfwm") == 0) - printWMThemeFromConfigFile(instance, "xfce4/xfconf/xfce-perchannel-xml/xfwm4.xml", " wmPrettyName, "Mutter") == 0 || ffStrbufIgnCaseCompS(&result->wmPrettyName, "Gnome") == 0 || ffStrbufIgnCaseCompS(&result->wmPrettyName, "Ubuntu") == 0) printMutter(instance); else if(ffStrbufIgnCaseCompS(&result->wmPrettyName, "Muffin") == 0)