From b83e7a311580e9c0ed7bd58637b01c10d4b4f8a3 Mon Sep 17 00:00:00 2001 From: Matthias Vogelgesang Date: Fri, 1 Apr 2016 16:23:27 +0200 Subject: cli: pass -p/--property assignment to constructor --- bin/tools/common.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) (limited to 'bin/tools/common.c') diff --git a/bin/tools/common.c b/bin/tools/common.c index 3721ff0..8de5e87 100644 --- a/bin/tools/common.c +++ b/bin/tools/common.c @@ -17,6 +17,7 @@ #include "common.h" +static gchar **uca_prop_assignment_array = NULL; static gchar * get_camera_list (UcaPluginManager *manager) @@ -48,14 +49,65 @@ get_camera_list (UcaPluginManager *manager) } GOptionContext * -uca_option_context_new (UcaPluginManager *manager) +uca_common_context_new (UcaPluginManager *manager) { GOptionContext *context; + GOptionGroup *common; gchar *camera_list; + static GOptionEntry entries[] = { + { "prop", 'p', 0, G_OPTION_ARG_STRING_ARRAY, &uca_prop_assignment_array, "Property assignment via `name=value'", NULL }, + { NULL } + }; + camera_list = get_camera_list (manager); context = g_option_context_new (camera_list); g_free (camera_list); + common = g_option_group_new ("properties", "Property options", "Show help for property assignment", NULL, NULL); + g_option_group_add_entries (common, entries); + g_option_context_add_group (context, common); + return context; } + +UcaCamera * +uca_common_get_camera (UcaPluginManager *manager, const gchar *name, GError **error) +{ + UcaCamera *camera; + GParameter *params; + guint n_props; + + n_props = g_strv_length (uca_prop_assignment_array); + params = g_new0 (GParameter, n_props); + + for (guint i = 0; i < n_props; i++) { + gchar **split; + + split = g_strsplit (uca_prop_assignment_array[i], "=", 2); + + if (g_strv_length (split) < 2) + goto cleanup; + + params[i].name = g_strdup (split[0]); + + /* We cannot check the type before instantiation, classic chicken-egg + * situation ... so, let's try string. */ + g_value_init (¶ms[i].value, G_TYPE_STRING); + g_value_set_string (¶ms[i].value, split[1]); + +cleanup: + g_strfreev (split); + } + + camera = uca_plugin_manager_get_camerav (manager, name, n_props, params, error); + + for (guint i = 0; i < n_props; i++) { + /* cast is legit, because we created the string */ + g_free ((gchar *) params[i].name); + } + + g_free (params); + + return camera; +} -- cgit v1.2.3