Files
fastfetch/src/common/settings.c
T

279 lines
9.7 KiB
C
Raw Normal View History

2021-05-04 19:48:23 +02:00
#include "fastfetch.h"
#include <pthread.h>
2021-05-11 20:06:17 +02:00
#define FF_LIBRARY_DATA_LOAD_INIT(dataObject, userLibraryName, ...) \
2021-11-13 14:10:30 +01:00
static dataObject data; \
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; \
static FFInitState initState = FF_INITSTATE_UNINITIALIZED; \
pthread_mutex_lock(&mutex); \
if(initState != FF_INITSTATE_UNINITIALIZED) {\
2021-05-11 20:06:17 +02:00
pthread_mutex_unlock(&mutex); \
2021-11-13 14:10:30 +01:00
return initState == FF_INITSTATE_SUCCESSFUL ? &data : NULL; \
} \
initState = FF_INITSTATE_SUCCESSFUL; \
void* libraryHandle = ffLibraryLoad(&userLibraryName, __VA_ARGS__, NULL); \
if(libraryHandle == NULL) { \
2021-11-13 14:10:30 +01:00
initState = FF_INITSTATE_FAILED; \
pthread_mutex_unlock(&mutex); \
return NULL; \
2021-11-06 22:50:52 +08:00
} \
2021-11-13 14:10:30 +01:00
#define FF_LIBRARY_DATA_LOAD_SYMBOL(symbolName) \
data.ff ## symbolName = dlsym(libraryHandle, #symbolName); \
if(data.ff ## symbolName == NULL) { \
2021-11-13 14:10:30 +01:00
dlclose(libraryHandle); \
initState = FF_INITSTATE_FAILED; \
pthread_mutex_unlock(&mutex); \
return NULL; \
}
2021-11-06 22:50:52 +08:00
2021-11-13 14:10:30 +01:00
#define FF_LIBRARY_DATA_LOAD_RETURN \
initState = FF_INITSTATE_SUCCESSFUL; \
pthread_mutex_unlock(&mutex); \
return &data;
2021-05-12 20:13:56 +02:00
2021-11-13 14:10:30 +01:00
#define FF_LIBRARY_DATA_LOAD_ERROR \
{ \
dlclose(libraryHandle); \
initState = FF_INITSTATE_FAILED; \
pthread_mutex_unlock(&mutex); \
return NULL; \
}
2022-01-10 20:45:58 +01:00
#ifdef FF_HAVE_GIO
#include <gio/gio.h>
2021-05-08 17:56:56 +02:00
typedef struct GVariantGetters
{
2021-11-13 14:10:30 +01:00
FF_LIBRARY_SYMBOL(g_variant_get_string)
FF_LIBRARY_SYMBOL(g_variant_get_boolean)
FF_LIBRARY_SYMBOL(g_variant_get_int32)
2021-05-08 17:56:56 +02:00
} GVariantGetters;
2021-11-13 14:10:30 +01:00
static FFvariant getGVariantValue(GVariant* variant, FFvarianttype type, const GVariantGetters* variantGetters)
2021-05-08 17:56:56 +02:00
{
if(variant == NULL)
return FF_VARIANT_NULL;
2021-06-20 21:04:55 +02:00
else if(type == FF_VARIANT_TYPE_STRING)
return (FFvariant) {.strValue = variantGetters->ffg_variant_get_string(variant, NULL)};
else if(type == FF_VARIANT_TYPE_BOOL)
return (FFvariant) {.boolValue = (bool) variantGetters->ffg_variant_get_boolean(variant), .boolValueSet = true};
else if(type == FF_VARIANT_TYPE_INT)
return (FFvariant) {.intValue = variantGetters->ffg_variant_get_int32(variant)};
else
return FF_VARIANT_NULL;
2021-05-08 17:56:56 +02:00
}
2021-05-04 19:48:23 +02:00
typedef struct GSettingsData
{
2021-11-13 14:10:30 +01:00
FF_LIBRARY_SYMBOL(g_settings_schema_source_lookup)
FF_LIBRARY_SYMBOL(g_settings_schema_has_key)
FF_LIBRARY_SYMBOL(g_settings_new_full)
FF_LIBRARY_SYMBOL(g_settings_get_value)
FF_LIBRARY_SYMBOL(g_settings_get_user_value)
FF_LIBRARY_SYMBOL(g_settings_get_default_value)
FF_LIBRARY_SYMBOL(g_settings_schema_source_get_default)
2021-05-04 19:48:23 +02:00
GSettingsSchemaSource* schemaSource;
2021-05-08 17:56:56 +02:00
GVariantGetters variantGetters;
2021-05-04 19:48:23 +02:00
} GSettingsData;
2021-11-13 14:10:30 +01:00
static const GSettingsData* getGSettingsData(FFinstance* instance)
{
2022-04-11 17:41:21 +02:00
FF_LIBRARY_DATA_LOAD_INIT(GSettingsData, instance->config.libGIO, "libgio-2.0.so", 1);
2021-11-13 14:10:30 +01:00
FF_LIBRARY_DATA_LOAD_SYMBOL(g_settings_schema_source_lookup)
FF_LIBRARY_DATA_LOAD_SYMBOL(g_settings_schema_has_key)
FF_LIBRARY_DATA_LOAD_SYMBOL(g_settings_new_full)
FF_LIBRARY_DATA_LOAD_SYMBOL(g_settings_get_value)
FF_LIBRARY_DATA_LOAD_SYMBOL(g_settings_get_user_value)
FF_LIBRARY_DATA_LOAD_SYMBOL(g_settings_get_default_value)
FF_LIBRARY_DATA_LOAD_SYMBOL(g_settings_schema_source_get_default)
#define data data.variantGetters
FF_LIBRARY_DATA_LOAD_SYMBOL(g_variant_get_string)
FF_LIBRARY_DATA_LOAD_SYMBOL(g_variant_get_boolean)
FF_LIBRARY_DATA_LOAD_SYMBOL(g_variant_get_int32)
#undef data
data.schemaSource = data.ffg_settings_schema_source_get_default();
if(data.schemaSource == NULL)
FF_LIBRARY_DATA_LOAD_ERROR
FF_LIBRARY_DATA_LOAD_RETURN
}
FFvariant ffSettingsGetGSettings(FFinstance* instance, const char* schemaName, const char* path, const char* key, FFvarianttype type)
2021-05-04 19:48:23 +02:00
{
2021-11-13 14:10:30 +01:00
const GSettingsData* data = getGSettingsData(instance);
if(data == NULL)
2021-05-08 17:56:56 +02:00
return FF_VARIANT_NULL;
2021-05-04 19:48:23 +02:00
GSettingsSchema* schema = data->ffg_settings_schema_source_lookup(data->schemaSource, schemaName, true);
if(schema == NULL)
2021-05-08 17:56:56 +02:00
return FF_VARIANT_NULL;
2021-05-04 19:48:23 +02:00
2021-11-13 14:10:30 +01:00
if(data->ffg_settings_schema_has_key(schema, key) == false)
2021-05-08 17:56:56 +02:00
return FF_VARIANT_NULL;
2021-05-04 19:48:23 +02:00
GSettings* settings = data->ffg_settings_new_full(schema, NULL, path);
if(settings == NULL)
2021-05-08 17:56:56 +02:00
return FF_VARIANT_NULL;
2021-05-04 19:48:23 +02:00
GVariant* variant = data->ffg_settings_get_value(settings, key);
2021-05-11 20:06:17 +02:00
if(variant != NULL)
return getGVariantValue(variant, type, &data->variantGetters);
2021-05-04 19:48:23 +02:00
2021-05-11 20:06:17 +02:00
variant = data->ffg_settings_get_user_value(settings, key);
if(variant != NULL)
return getGVariantValue(variant, type, &data->variantGetters);
2021-05-04 19:48:23 +02:00
2021-05-11 20:06:17 +02:00
variant = data->ffg_settings_get_default_value(settings, key);
2021-05-08 17:56:56 +02:00
return getGVariantValue(variant, type, &data->variantGetters);
2021-05-04 19:48:23 +02:00
}
#else //FF_HAVE_GIO
FFvariant ffSettingsGetGSettings(FFinstance* instance, const char* schemaName, const char* path, const char* key, FFvarianttype type)
{
2021-10-19 19:34:55 +02:00
FF_UNUSED(instance, schemaName, path, key, type)
return FF_VARIANT_NULL;
}
#endif //FF_HAVE_GIO
#ifdef FF_HAVE_DCONF
#include <dconf.h>
typedef struct DConfData
{
2021-11-13 14:10:30 +01:00
FF_LIBRARY_SYMBOL(dconf_client_read_full)
FF_LIBRARY_SYMBOL(dconf_client_new)
GVariantGetters variantGetters;
DConfClient* client;
} DConfData;
2021-11-13 14:10:30 +01:00
static const DConfData* getDConfData(FFinstance* instance)
{
2022-04-11 17:41:21 +02:00
FF_LIBRARY_DATA_LOAD_INIT(DConfData, instance->config.libDConf, "libdconf.so", 2);
2021-11-13 14:10:30 +01:00
FF_LIBRARY_DATA_LOAD_SYMBOL(dconf_client_read_full)
FF_LIBRARY_DATA_LOAD_SYMBOL(dconf_client_new)
#define data data.variantGetters
FF_LIBRARY_DATA_LOAD_SYMBOL(g_variant_get_string)
FF_LIBRARY_DATA_LOAD_SYMBOL(g_variant_get_boolean)
FF_LIBRARY_DATA_LOAD_SYMBOL(g_variant_get_int32)
#undef data
data.client = data.ffdconf_client_new();
if(data.client == NULL)
FF_LIBRARY_DATA_LOAD_ERROR
FF_LIBRARY_DATA_LOAD_RETURN
}
FFvariant ffSettingsGetDConf(FFinstance* instance, const char* key, FFvarianttype type)
{
const DConfData* data = getDConfData(instance);
if(data == NULL)
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);
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);
}
#else //FF_HAVE_DCONF
FFvariant ffSettingsGetDConf(FFinstance* instance, const char* key, FFvarianttype type)
{
2021-10-19 19:34:55 +02:00
FF_UNUSED(instance, key, type)
return FF_VARIANT_NULL;
}
#endif //FF_HAVE_DCONF
2021-05-08 17:56:56 +02:00
FFvariant ffSettingsGet(FFinstance* instance, const char* dconfKey, const char* gsettingsSchemaName, const char* gsettingsPath, const char* gsettingsKey, FFvarianttype type)
2021-05-04 19:48:23 +02:00
{
2021-06-20 21:04:55 +02:00
FFvariant gsettings = ffSettingsGetGSettings(instance, gsettingsSchemaName, gsettingsPath, gsettingsKey, type);
if(
(type == FF_VARIANT_TYPE_BOOL && gsettings.boolValueSet) ||
(type != FF_VARIANT_TYPE_BOOL && gsettings.strValue != NULL)
) return gsettings;
2021-05-04 19:48:23 +02:00
2021-05-20 20:06:08 +02:00
return ffSettingsGetDConf(instance, dconfKey, type);
2021-05-04 19:48:23 +02:00
}
2021-05-08 17:56:56 +02:00
#ifdef FF_HAVE_XFCONF
#include <xfconf/xfconf.h>
2021-05-11 20:06:17 +02:00
typedef struct XFConfData
{
2021-11-13 14:10:30 +01:00
FF_LIBRARY_SYMBOL(xfconf_channel_get)
FF_LIBRARY_SYMBOL(xfconf_channel_has_property)
FF_LIBRARY_SYMBOL(xfconf_channel_get_string)
FF_LIBRARY_SYMBOL(xfconf_channel_get_bool)
FF_LIBRARY_SYMBOL(xfconf_channel_get_int)
FF_LIBRARY_SYMBOL(xfconf_init)
2021-05-11 20:06:17 +02:00
} XFConfData;
2021-11-13 14:10:30 +01:00
static const XFConfData* getXFConfData(FFinstance* instance)
{
2022-04-11 17:41:21 +02:00
FF_LIBRARY_DATA_LOAD_INIT(XFConfData, instance->config.libXFConf, "libxfconf-0.so", 4);
2021-11-13 14:10:30 +01:00
FF_LIBRARY_DATA_LOAD_SYMBOL(xfconf_channel_get)
FF_LIBRARY_DATA_LOAD_SYMBOL(xfconf_channel_has_property)
FF_LIBRARY_DATA_LOAD_SYMBOL(xfconf_channel_get_string)
FF_LIBRARY_DATA_LOAD_SYMBOL(xfconf_channel_get_bool)
FF_LIBRARY_DATA_LOAD_SYMBOL(xfconf_channel_get_int)
FF_LIBRARY_DATA_LOAD_SYMBOL(xfconf_init)
2022-01-10 20:45:58 +01:00
if(data.ffxfconf_init(NULL) == false)
2021-11-13 14:10:30 +01:00
FF_LIBRARY_DATA_LOAD_ERROR
FF_LIBRARY_DATA_LOAD_RETURN
}
FFvariant ffSettingsGetXFConf(FFinstance* instance, const char* channelName, const char* propertyName, FFvarianttype type)
2021-05-11 20:06:17 +02:00
{
2021-11-13 14:10:30 +01:00
const XFConfData* data = getXFConfData(instance);
if(data == NULL)
2021-05-11 20:06:17 +02:00
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;
2021-06-20 21:04:55 +02:00
if(type == FF_VARIANT_TYPE_INT)
return (FFvariant) {.intValue = data->ffxfconf_channel_get_int(channel, propertyName, 0)};
2021-05-11 20:06:17 +02:00
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;
}
#else //FF_HAVE_XFCONF
FFvariant ffSettingsGetXFConf(FFinstance* instance, const char* channelName, const char* propertyName, FFvarianttype type)
{
2021-10-19 19:34:55 +02:00
FF_UNUSED(instance, channelName, propertyName, type)
return FF_VARIANT_NULL;
}
#endif //FF_HAVE_XFCONF
2021-10-20 06:04:55 +00:00
#ifdef __ANDROID__
#include <sys/system_properties.h>
2022-01-10 15:20:51 +01:00
bool ffSettingsGetAndroidProperty(const char* propName, FFstrbuf* result) {
uint32_t originalLength = result->length;
ffStrbufEnsureFree(result, PROP_VALUE_MAX);
result->length += (uint32_t) __system_property_get(propName, result->chars + result->length);
return result->length > originalLength;
2021-10-20 06:04:55 +00:00
}
#endif