summaryrefslogtreecommitdiffstats
path: root/bin/tools/common.c
blob: ee3c110d24e48e017c9acdc6621c818126bb3f27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* Copyright (C) 2011-2013 Matthias Vogelgesang <matthias.vogelgesang@kit.edu>
   (Karlsruhe Institute of Technology)

   This library is free software; you can redistribute it and/or modify it
   under the terms of the GNU Lesser General Public License as published by the
   Free Software Foundation; either version 2.1 of the License, or (at your
   option) any later version.

   This library is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
   details.

   You should have received a copy of the GNU Lesser General Public License along
   with this library; if not, write to the Free Software Foundation, Inc., 51
   Franklin St, Fifth Floor, Boston, MA 02110, USA */

#include "common.h"

static gchar **uca_prop_assignment_array = NULL;

static gchar *
get_camera_list (UcaPluginManager *manager)
{
    GList *types;
    GString *str;

    manager = uca_plugin_manager_new ();
    types = uca_plugin_manager_get_available_cameras (manager);
    str = g_string_new ("[ ");

    if (types != NULL) {
        for (GList *it = g_list_first (types); it != NULL; it = g_list_next (it)) {
            gchar *name = (gchar *) it->data;

            if (g_list_next (it) == NULL)
                g_string_append_printf (str, "%s ]", name);
            else
                g_string_append_printf (str, "%s, ", name);
        }
    }
    else {
        g_string_append (str, "]");
    }

    g_list_free_full (types, g_free);
    g_object_unref (manager);
    return g_string_free (str, FALSE);
}

GOptionContext *
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 = uca_prop_assignment_array != NULL ? g_strv_length (uca_prop_assignment_array) : 0;
    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 (&params[i].value, G_TYPE_STRING);
        g_value_set_string (&params[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;
}