From df2ee425214273d6cc0b53b313098930f2134be6 Mon Sep 17 00:00:00 2001 From: Adrian Perez de Castro Date: Fri, 30 Sep 2022 00:14:55 +0300 Subject: [PATCH] core: Factor out params string parsing Move the routine from the DRM plugin used to parse a comma-separated key=value set of parameters into cog_key_file_parse_params_string() which sets values into a GKeyFile. This allows building up the final set of options into the configuration object held by CogShell, then using that as canonical source for reading options. --- core/cog-utils.c | 33 +++++++++++++++++++++++++++++ core/cog-utils.h | 9 ++++++-- platform/drm/cog-platform-drm.c | 37 ++++----------------------------- 3 files changed, 44 insertions(+), 35 deletions(-) diff --git a/core/cog-utils.c b/core/cog-utils.c index 8fa72f71..223e7bd4 100644 --- a/core/cog-utils.c +++ b/core/cog-utils.c @@ -384,6 +384,39 @@ cog_option_entries_from_class (GObjectClass *klass) return g_steal_pointer (&entries); } +/** + * cog_key_file_parse_params_string: + * @key_file: A key file + * @group_name: Name of a key file group + * @params_string: Input string + * @error: Where to store an error, if any + * + * Parse parameters string storing values in a key file. + * + * Since: 0.18 + */ +void +cog_key_file_parse_params_string(GKeyFile *key_file, const char *group_name, const char *params_string) +{ + g_return_if_fail(key_file); + g_return_if_fail(group_name); + g_return_if_fail(params_string); + + g_auto(GStrv) params = g_strsplit(params_string, ",", 0); + for (unsigned i = 0; params[i]; i++) { + g_auto(GStrv) kv = g_strsplit(params[i], "=", 2); + if (g_strv_length(kv) != 2) { + g_warning("%s: Invalid parameter syntax '%s'.", __func__, params[i]); + continue; + } + + const char *k = g_strstrip(kv[0]); + const char *v = g_strstrip(kv[1]); + + g_key_file_set_value(key_file, group_name, k, v); + } +} + /** * cog_apply_properties_from_key_file: * @object: An object to set properties on diff --git a/core/cog-utils.h b/core/cog-utils.h index fff83523..03e7ff1e 100644 --- a/core/cog-utils.h +++ b/core/cog-utils.h @@ -31,8 +31,13 @@ char* cog_uri_guess_from_user_input (const char *uri_like, COG_API GOptionEntry* cog_option_entries_from_class (GObjectClass *klass); -gboolean -cog_apply_properties_from_key_file(GObject *object, GKeyFile *key_file, const char *group_name, GError **error); + +COG_API void cog_key_file_parse_params_string(GKeyFile *key_file, const char *group_name, const char *params_string); + +COG_API gboolean cog_apply_properties_from_key_file(GObject *object, + GKeyFile *key_file, + const char *group_name, + GError **error); static inline const char* cog_g_enum_get_nick (GType enum_type, int value) diff --git a/platform/drm/cog-platform-drm.c b/platform/drm/cog-platform-drm.c index d5bc07a2..16fafd9b 100644 --- a/platform/drm/cog-platform-drm.c +++ b/platform/drm/cog-platform-drm.c @@ -239,45 +239,16 @@ static void init_config(CogDrmPlatform *self, CogShell *shell, const char *params_string) { GKeyFile *key_file = cog_shell_get_config_file(shell); + + if (params_string) + cog_key_file_parse_params_string(key_file, "drm", params_string); + if (key_file) { g_autoptr(GError) error = NULL; if (!cog_apply_properties_from_key_file(G_OBJECT(self), key_file, "drm", &error)) g_warning("Reading config file: %s", error->message); } - if (params_string) { - g_auto(GStrv) params = g_strsplit(params_string, ",", 0); - for (unsigned i = 0; params[i]; i++) { - g_auto(GStrv) kv = g_strsplit(params[i], "=", 2); - if (g_strv_length(kv) != 2) { - g_warning("Invalid parameter syntax '%s'.", params[i]); - continue; - } - - const char *k = g_strstrip(kv[0]); - const char *v = g_strstrip(kv[1]); - - if (g_strcmp0(k, "renderer") == 0) { - if (g_strcmp0(v, "modeset") == 0) - self->use_gles = false; - else if (g_strcmp0(v, "gles") == 0) - self->use_gles = true; - else - g_warning("Invalid value '%s' for parameter '%s'.", v, k); - } else if (g_strcmp0(k, "rotation") == 0) { - char *endp = NULL; - const char *str = v ? v : ""; - uint64_t val = g_ascii_strtoull(str, &endp, 10); - if (val > 3 || *endp != '\0') - g_warning("Invalid value '%s' for parameter '%s'.", v, k); - else - self->rotation = val; - } else { - g_warning("Invalid parameter '%s'.", k); - } - } - } - float device_scale_factor = cog_shell_get_device_scale_factor(shell); if (device_scale_factor >= 0.2f) { g_debug("%s: Overriding CogDrmPlatform<%p>.device-scale-factor = <%.2f> from shell", __func__, self,