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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
|
/* Copyright (C) 2012 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 */
/**
* SECTION:uca-plugin-manager
* @Short_description: Load an #UcaFilter from a shared object
* @Title: UcaPluginManager
*
* The plugin manager opens shared object modules searched for in locations
* specified with uca_plugin_manager_add_path(). A #UcaCamera can be
* instantiated with uca_plugin_manager_new_camera() with a one-to-one mapping
* between filter name xyz and module name libucaxyz.so. Any errors are reported
* as one of #UcaPluginManagerError codes. To get a list of available camera
* names call uca_plugin_manager_get_available_cameras().
*
* By default, any path listed in the %UCA_CAMERA_PATH environment variable is
* added to the search path.
*
* Since: 1.1
*/
#include <gio/gio.h>
#include <gmodule.h>
#include "uca-plugin-manager.h"
#include "config.h"
G_DEFINE_TYPE (UcaPluginManager, uca_plugin_manager, G_TYPE_OBJECT)
#define UCA_PLUGIN_MANAGER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), UCA_TYPE_PLUGIN_MANAGER, UcaPluginManagerPrivate))
struct _UcaPluginManagerPrivate {
GList *search_paths;
GList *funcs;
};
static const gchar *MODULE_PATTERN = "libuca([A-Za-z]+)";
typedef GType (*GetTypeFunc) (void);
/**
* UcaPluginManagerError:
* @UCA_PLUGIN_MANAGER_ERROR_MODULE_NOT_FOUND: The module could not be found
* @UCA_PLUGIN_MANAGER_ERROR_MODULE_OPEN: Module could not be opened
* @UCA_PLUGIN_MANAGER_ERROR_SYMBOL_NOT_FOUND: Necessary entry symbol was not
* found
*
* Possible errors that uca_plugin_manager_get_filter() can return.
*/
GQuark
uca_plugin_manager_error_quark (void)
{
return g_quark_from_static_string ("uca-plugin-manager-error-quark");
}
/**
* uca_plugin_manager_new:
*
* Create a plugin manager object to instantiate camera objects.
*
* Return value: A new plugin manager object.
*/
UcaPluginManager *
uca_plugin_manager_new ()
{
return UCA_PLUGIN_MANAGER (g_object_new (UCA_TYPE_PLUGIN_MANAGER, NULL));
}
/**
* uca_plugin_manager_add_path:
* @manager: A #UcaPluginManager
* @path: Path to look for camera modules
*
* Add a search path to the plugin manager.
*/
void
uca_plugin_manager_add_path (UcaPluginManager *manager,
const gchar *path)
{
g_return_if_fail (UCA_IS_PLUGIN_MANAGER (manager));
if (g_file_test (path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
UcaPluginManagerPrivate *priv;
priv = manager->priv;
priv->search_paths = g_list_append (priv->search_paths,
g_strdup (path));
}
}
static GList *
get_camera_module_paths (const gchar *path)
{
GRegex *pattern;
GDir *dir;
GList *result = NULL;
pattern = g_regex_new (MODULE_PATTERN, 0, 0, NULL);
dir = g_dir_open (path, 0, NULL);
if (dir != NULL) {
GMatchInfo *match_info = NULL;
const gchar *name = g_dir_read_name (dir);
while (name != NULL) {
if (g_regex_match (pattern, name, 0, &match_info))
result = g_list_append (result, g_build_filename (path, name, NULL));
g_match_info_free (match_info);
name = g_dir_read_name (dir);
}
}
g_dir_close (dir);
g_regex_unref (pattern);
return result;
}
static GList *
scan_search_paths (GList *search_paths)
{
GList *camera_paths = NULL;
for (GList *it = g_list_first (search_paths); it != NULL; it = g_list_next (it)) {
camera_paths = g_list_concat (camera_paths,
get_camera_module_paths ((const gchar*) it->data));
}
return camera_paths;
}
static void
transform_camera_module_path_to_name (gchar *path, GList **result)
{
GRegex *pattern;
GMatchInfo *match_info;
pattern = g_regex_new (MODULE_PATTERN, 0, 0, NULL);
g_regex_match (pattern, path, 0, &match_info);
*result = g_list_append (*result, g_match_info_fetch (match_info, 1));
g_match_info_free (match_info);
g_regex_unref (pattern);
}
/**
* uca_plugin_manager_get_available_cameras:
* @manager: A #UcaPluginManager
*
* Return value: (element-type utf8) (transfer full): A list with strings of
* available camera names. You have to free the individual strings with
* g_list_foreach(list, (GFunc) g_free, NULL) and the list itself with
* g_list_free.
*/
GList *
uca_plugin_manager_get_available_cameras (UcaPluginManager *manager)
{
UcaPluginManagerPrivate *priv;
GList *camera_paths;
GList *camera_names = NULL;
g_return_val_if_fail (UCA_IS_PLUGIN_MANAGER (manager), NULL);
priv = manager->priv;
camera_paths = scan_search_paths (priv->search_paths);
g_list_foreach (camera_paths, (GFunc) transform_camera_module_path_to_name, &camera_names);
g_list_free_full (camera_paths, g_free);
return camera_names;
}
static gchar *
find_camera_module_path (GList *search_paths, const gchar *name)
{
gchar *result = NULL;
GList *paths;
paths = scan_search_paths (search_paths);
for (GList *it = g_list_first (paths); it != NULL; it = g_list_next (it)) {
gchar *path = (gchar *) it->data;
gchar *basename = g_path_get_basename ((gchar *) path);
if (g_strrstr (basename, name)) {
result = g_strdup (path);
g_free (basename);
break;
}
g_free (basename);
}
g_list_free_full (paths, g_free);
return result;
}
static GType
get_camera_type (UcaPluginManagerPrivate *priv,
const gchar *name,
GError **error)
{
GModule *module;
gchar *module_path;
GetTypeFunc *func;
const gchar *symbol_name = "uca_camera_get_type";
module_path = find_camera_module_path (priv->search_paths, name);
if (module_path == NULL) {
g_set_error (error, UCA_PLUGIN_MANAGER_ERROR, UCA_PLUGIN_MANAGER_ERROR_MODULE_NOT_FOUND,
"Camera module `%s' not found", name);
return G_TYPE_NONE;
}
module = g_module_open (module_path, G_MODULE_BIND_LAZY);
g_free (module_path);
if (!module) {
g_set_error (error, UCA_PLUGIN_MANAGER_ERROR, UCA_PLUGIN_MANAGER_ERROR_MODULE_OPEN,
"Camera module `%s' could not be opened: %s", name, g_module_error ());
return G_TYPE_NONE;
}
func = g_malloc0 (sizeof (GetTypeFunc));
if (!g_module_symbol (module, symbol_name, (gpointer *) func)) {
g_set_error (error, UCA_PLUGIN_MANAGER_ERROR, UCA_PLUGIN_MANAGER_ERROR_SYMBOL_NOT_FOUND,
"%s", g_module_error ());
g_free (func);
if (!g_module_close (module))
g_warning ("%s", g_module_error ());
return G_TYPE_NONE;
}
priv->funcs = g_list_append (priv->funcs, func);
return (*func) ();
}
/**
* uca_plugin_manager_get_camerav:
* @manager: A #UcaPluginManager
* @name: Name of the camera module, that maps to libuca<name>.so
* @n_parameters: number of parameters in @parameters
* @parameters: (array length=n_parameters): the parameters to use to construct
* the camera
* @error: Location for a #GError or %NULL
*
* Create a new camera instance with camera @name.
*
* Returns: (transfer full): A new #UcaCamera object.
* Since: 1.2
*/
UcaCamera *
uca_plugin_manager_get_camerav (UcaPluginManager *manager,
const gchar *name,
guint n_parameters,
GParameter *parameters,
GError **error)
{
UcaPluginManagerPrivate *priv;
UcaCamera *camera;
GType type;
g_return_val_if_fail (UCA_IS_PLUGIN_MANAGER (manager) && (name != NULL), NULL);
priv = manager->priv;
type = get_camera_type (priv, name, error);
if (type == G_TYPE_NONE)
return NULL;
camera = (UcaCamera *) g_initable_newv (type, n_parameters, parameters,
NULL, error);
return camera;
}
/**
* uca_plugin_manager_get_camera: (skip)
* @manager: A #UcaPluginManager
* @name: Name of the camera module, that maps to libuca<name>.so
* @error: Location for a #GError
* @first_prop_name: (allow-none): name of the first property, or %NULL if no
* properties
* @...: value of the first property, followed by and other property value
* pairs, and ended by %NULL.
*
* Create a new camera instance with camera @name.
*
* Returns: (transfer full): A new #UcaCamera object.
* Since: 1.2: Pass construction properties.
*/
UcaCamera *
uca_plugin_manager_get_camera (UcaPluginManager *manager,
const gchar *name,
GError **error,
const gchar *first_prop_name,
...)
{
UcaPluginManagerPrivate *priv;
UcaCamera *camera;
GType type;
va_list var_args;
g_return_val_if_fail (UCA_IS_PLUGIN_MANAGER (manager) && (name != NULL), NULL);
priv = manager->priv;
type = get_camera_type (priv, name, error);
if (type == G_TYPE_NONE)
return NULL;
va_start (var_args, first_prop_name);
camera = (UcaCamera *) g_initable_new (type, NULL, error,
first_prop_name, var_args);
va_end (var_args);
return camera;
}
static void
uca_plugin_manager_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec)
{
switch (property_id) {
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
uca_plugin_manager_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec)
{
switch (property_id) {
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
uca_plugin_manager_dispose (GObject *object)
{
G_OBJECT_CLASS (uca_plugin_manager_parent_class)->dispose (object);
}
static void
uca_plugin_manager_finalize (GObject *object)
{
UcaPluginManagerPrivate *priv = UCA_PLUGIN_MANAGER_GET_PRIVATE (object);
g_list_free_full (priv->search_paths, g_free);
g_list_free_full (priv->funcs, g_free);
G_OBJECT_CLASS (uca_plugin_manager_parent_class)->finalize (object);
}
static void
uca_plugin_manager_class_init (UcaPluginManagerClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->get_property = uca_plugin_manager_get_property;
gobject_class->set_property = uca_plugin_manager_set_property;
gobject_class->dispose = uca_plugin_manager_dispose;
gobject_class->finalize = uca_plugin_manager_finalize;
g_type_class_add_private (klass, sizeof (UcaPluginManagerPrivate));
}
static void
uca_plugin_manager_init (UcaPluginManager *manager)
{
UcaPluginManagerPrivate *priv;
const gchar *uca_camera_path;
manager->priv = priv = UCA_PLUGIN_MANAGER_GET_PRIVATE (manager);
priv->search_paths = NULL;
priv->funcs = NULL;
uca_camera_path = g_getenv ("UCA_CAMERA_PATH");
if (uca_camera_path != NULL)
uca_plugin_manager_add_path (manager, uca_camera_path);
uca_plugin_manager_add_path (manager, UCA_PLUGINDIR);
}
|