Skip to content

Commit 92e8c4e

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 254b37f commit 92e8c4e

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
@@ -384,6 +384,39 @@ cog_option_entries_from_class (GObjectClass *klass)
384384
return g_steal_pointer (&entries);
385385
}
386386

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

3232
COG_API
3333
GOptionEntry* cog_option_entries_from_class (GObjectClass *klass);
34+
35+
void
36+
cog_key_file_parse_params_string(GKeyFile *key_file, const char *group_name, const char *params_string);
37+
3438
gboolean
3539
cog_apply_properties_from_key_file(GObject *object, GKeyFile *key_file, const char *group_name, GError **error);
3640

platform/drm/cog-platform-drm.c

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

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

0 commit comments

Comments
 (0)