Skip to content

Commit

Permalink
core: Factor out params string parsing
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aperezdc committed Oct 6, 2022
1 parent b3575ed commit 904da7c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 33 deletions.
33 changes: 33 additions & 0 deletions core/cog-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,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
Expand Down
4 changes: 4 additions & 0 deletions core/cog-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ char* cog_uri_guess_from_user_input (const char *uri_like,
GError **error);

GOptionEntry* cog_option_entries_from_class (GObjectClass *klass);

void
cog_key_file_parse_params_string(GKeyFile *key_file, const char *group_name, const char *params_string);

gboolean
cog_apply_properties_from_key_file(GObject *object, GKeyFile *key_file, const char *group_name, GError **error);

Expand Down
37 changes: 4 additions & 33 deletions platform/drm/cog-platform-drm.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,45 +238,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,
Expand Down

0 comments on commit 904da7c

Please sign in to comment.