Skip to content

Commit 7c8d26d

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 c744ddc commit 7c8d26d

File tree

3 files changed

+44
-35
lines changed

3 files changed

+44
-35
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: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,13 @@ 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-
gboolean
35-
cog_apply_properties_from_key_file(GObject *object, GKeyFile *key_file, const char *group_name, GError **error);
34+
35+
COG_API void cog_key_file_parse_params_string(GKeyFile *key_file, const char *group_name, const char *params_string);
36+
37+
COG_API gboolean cog_apply_properties_from_key_file(GObject *object,
38+
GKeyFile *key_file,
39+
const char *group_name,
40+
GError **error);
3641

3742
static inline const char*
3843
cog_g_enum_get_nick (GType enum_type, int value)

platform/drm/cog-platform-drm.c

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

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

0 commit comments

Comments
 (0)