Skip to content

Commit 524f64f

Browse files
committed
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.
1 parent a11c581 commit 524f64f

File tree

3 files changed

+41
-33
lines changed

3 files changed

+41
-33
lines changed

core/cog-utils.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,39 @@ cog_option_entries_from_class (GObjectClass *klass)
383383
return g_steal_pointer (&entries);
384384
}
385385

386+
/**
387+
* cog_key_file_parse_params_string:
388+
* @key_file: A key file
389+
* @group_name: Name of a key file group
390+
* @params_string: Input string
391+
* @error: Where to store an error, if any
392+
*
393+
* Parse parameters string storing values in a key file.
394+
*
395+
* Since: 0.18
396+
*/
397+
void
398+
cog_key_file_parse_params_string(GKeyFile *key_file, const char *group_name, const char *params_string)
399+
{
400+
g_return_if_fail(key_file);
401+
g_return_if_fail(group_name);
402+
g_return_if_fail(params_string);
403+
404+
g_auto(GStrv) params = g_strsplit(params_string, ",", 0);
405+
for (unsigned i = 0; params[i]; i++) {
406+
g_auto(GStrv) kv = g_strsplit(params[i], "=", 2);
407+
if (g_strv_length(kv) != 2) {
408+
g_warning("%s: Invalid parameter syntax '%s'.", __func__, params[i]);
409+
continue;
410+
}
411+
412+
const char *k = g_strstrip(kv[0]);
413+
const char *v = g_strstrip(kv[1]);
414+
415+
g_key_file_set_value(key_file, group_name, k, v);
416+
}
417+
}
418+
386419
/**
387420
* cog_apply_properties_from_key_file:
388421
* @object: An object to set properties on

core/cog-utils.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ char* cog_uri_guess_from_user_input (const char *uri_like,
2828
GError **error);
2929

3030
GOptionEntry* cog_option_entries_from_class (GObjectClass *klass);
31+
32+
void
33+
cog_key_file_parse_params_string(GKeyFile *key_file, const char *group_name, const char *params_string);
34+
3135
gboolean
3236
cog_apply_properties_from_key_file(GObject *object, GKeyFile *key_file, const char *group_name, GError **error);
3337

platform/drm/cog-platform-drm.c

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -238,45 +238,16 @@ static void
238238
init_config(CogDrmPlatform *self, CogShell *shell, const char *params_string)
239239
{
240240
GKeyFile *key_file = cog_shell_get_config_file(shell);
241+
242+
if (params_string)
243+
cog_key_file_parse_params_string(key_file, "drm", params_string);
244+
241245
if (key_file) {
242246
g_autoptr(GError) error = NULL;
243247
if (!cog_apply_properties_from_key_file(G_OBJECT(self), key_file, "drm", &error))
244248
g_warning("Reading config file: %s", error->message);
245249
}
246250

247-
if (params_string) {
248-
g_auto(GStrv) params = g_strsplit(params_string, ",", 0);
249-
for (unsigned i = 0; params[i]; i++) {
250-
g_auto(GStrv) kv = g_strsplit(params[i], "=", 2);
251-
if (g_strv_length(kv) != 2) {
252-
g_warning("Invalid parameter syntax '%s'.", params[i]);
253-
continue;
254-
}
255-
256-
const char *k = g_strstrip(kv[0]);
257-
const char *v = g_strstrip(kv[1]);
258-
259-
if (g_strcmp0(k, "renderer") == 0) {
260-
if (g_strcmp0(v, "modeset") == 0)
261-
self->use_gles = false;
262-
else if (g_strcmp0(v, "gles") == 0)
263-
self->use_gles = true;
264-
else
265-
g_warning("Invalid value '%s' for parameter '%s'.", v, k);
266-
} else if (g_strcmp0(k, "rotation") == 0) {
267-
char *endp = NULL;
268-
const char *str = v ? v : "";
269-
uint64_t val = g_ascii_strtoull(str, &endp, 10);
270-
if (val > 3 || *endp != '\0')
271-
g_warning("Invalid value '%s' for parameter '%s'.", v, k);
272-
else
273-
self->rotation = val;
274-
} else {
275-
g_warning("Invalid parameter '%s'.", k);
276-
}
277-
}
278-
}
279-
280251
float device_scale_factor = cog_shell_get_device_scale_factor(shell);
281252
if (device_scale_factor >= 0.2f) {
282253
g_debug("%s: Overriding CogDrmPlatform<%p>.device-scale-factor = <%.2f> from shell", __func__, self,

0 commit comments

Comments
 (0)