diff options
author | Matthias Vogelgesang <matthias.vogelgesang@gmail.com> | 2012-08-31 23:14:16 +0200 |
---|---|---|
committer | Matthias Vogelgesang <matthias.vogelgesang@gmail.com> | 2012-09-19 15:11:06 +0200 |
commit | 034204d3d8d1a32b1a20e50697c5f81db6fb20cf (patch) | |
tree | d2c8265bacbd591aa53865310437a6bfa4366c45 /src | |
parent | 539b6cebd615d71b377941e250117ebb99e49c27 (diff) | |
download | uca-034204d3d8d1a32b1a20e50697c5f81db6fb20cf.tar.gz uca-034204d3d8d1a32b1a20e50697c5f81db6fb20cf.tar.bz2 uca-034204d3d8d1a32b1a20e50697c5f81db6fb20cf.tar.xz uca-034204d3d8d1a32b1a20e50697c5f81db6fb20cf.zip |
Initial plugin manager
Diffstat (limited to 'src')
-rw-r--r-- | src/CMakeLists.txt | 12 | ||||
-rw-r--r-- | src/uca-camera.c | 199 | ||||
-rw-r--r-- | src/uca-plugin-manager.c | 260 | ||||
-rw-r--r-- | src/uca-plugin-manager.h | 65 |
4 files changed, 435 insertions, 101 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9508c5c..1b814aa 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 2.8) # --- Set sources ------------------------------------------------------------- set(uca_SRCS uca-camera.c + uca-plugin-manager.c ) set(uca_HDRS @@ -10,6 +11,8 @@ set(uca_HDRS set(cameras) +set(LIB_INSTALL_DIR "lib${LIB_SUFFIX}") + # --- Find packages and libraries --------------------------------------------- set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake) @@ -107,9 +110,12 @@ if (PYLON_FOUND) endif() if (HAVE_MOCK_CAMERA) - list(APPEND uca_SRCS cameras/uca-mock-camera.c) - list(APPEND uca_HDRS cameras/uca-mock-camera.h) - list(APPEND cameras "Mock") + add_library(ucamock SHARED cameras/uca-mock-camera.c) + #list(APPEND uca_SRCS cameras/uca-mock-camera.c) + ##list(APPEND uca_HDRS cameras/uca-mock-camera.h) + #list(APPEND cameras "Mock") + install(TARGETS ucamock + LIBRARY DESTINATION ${LIB_INSTALL_DIR}/uca) endif() # --- Generate enum file diff --git a/src/uca-camera.c b/src/uca-camera.c index 78adae3..59ab5c7 100644 --- a/src/uca-camera.c +++ b/src/uca-camera.c @@ -20,25 +20,25 @@ #include "uca-camera.h" #include "uca-enums.h" -#ifdef HAVE_PCO_CL -#include "cameras/uca-pco-camera.h" -#endif +/* #ifdef HAVE_PCO_CL */ +/* #include "cameras/uca-pco-camera.h" */ +/* #endif */ -#ifdef HAVE_PYLON_CAMERA -#include "cameras/uca-pylon-camera.h" -#endif +/* #ifdef HAVE_PYLON_CAMERA */ +/* #include "cameras/uca-pylon-camera.h" */ +/* #endif */ -#ifdef HAVE_MOCK_CAMERA -#include "cameras/uca-mock-camera.h" -#endif +/* #ifdef HAVE_MOCK_CAMERA */ +/* #include "cameras/uca-mock-camera.h" */ +/* #endif */ -#ifdef HAVE_UFO_CAMERA -#include "cameras/uca-ufo-camera.h" -#endif +/* #ifdef HAVE_UFO_CAMERA */ +/* #include "cameras/uca-ufo-camera.h" */ +/* #endif */ -#ifdef HAVE_PHOTON_FOCUS -#include "cameras/uca-pf-camera.h" -#endif +/* #ifdef HAVE_PHOTON_FOCUS */ +/* #include "cameras/uca-pf-camera.h" */ +/* #endif */ #define UCA_CAMERA_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), UCA_TYPE_CAMERA, UcaCameraPrivate)) @@ -65,24 +65,24 @@ GQuark uca_camera_error_quark() return g_quark_from_static_string("uca-camera-error-quark"); } -static gchar *uca_camera_types[] = { -#ifdef HAVE_PCO_CL - "pco", -#endif -#ifdef HAVE_PYLON_CAMERA - "pylon", -#endif -#ifdef HAVE_MOCK_CAMERA - "mock", -#endif -#ifdef HAVE_UFO_CAMERA - "ufo", -#endif -#ifdef HAVE_PHOTON_FOCUS - "pf", -#endif - NULL -}; +/* static gchar *uca_camera_types[] = { */ +/* #ifdef HAVE_PCO_CL */ +/* "pco", */ +/* #endif */ +/* #ifdef HAVE_PYLON_CAMERA */ +/* "pylon", */ +/* #endif */ +/* #ifdef HAVE_MOCK_CAMERA */ +/* "mock", */ +/* #endif */ +/* #ifdef HAVE_UFO_CAMERA */ +/* "ufo", */ +/* #endif */ +/* #ifdef HAVE_PHOTON_FOCUS */ +/* "pf", */ +/* #endif */ +/* NULL */ +/* }; */ enum { LAST_SIGNAL @@ -126,7 +126,8 @@ struct _UcaCameraPrivate { gboolean transfer_async; }; -static void uca_camera_set_property(GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) +static void +uca_camera_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) { UcaCameraPrivate *priv = UCA_CAMERA_GET_PRIVATE(object); @@ -145,7 +146,8 @@ static void uca_camera_set_property(GObject *object, guint property_id, const GV } } -static void uca_camera_get_property(GObject *object, guint property_id, GValue *value, GParamSpec *pspec) +static void +uca_camera_get_property(GObject *object, guint property_id, GValue *value, GParamSpec *pspec) { UcaCameraPrivate *priv = UCA_CAMERA_GET_PRIVATE(object); @@ -167,7 +169,8 @@ static void uca_camera_get_property(GObject *object, guint property_id, GValue * } } -static void uca_camera_class_init(UcaCameraClass *klass) +static void +uca_camera_class_init(UcaCameraClass *klass) { GObjectClass *gobject_class = G_OBJECT_CLASS(klass); gobject_class->set_property = uca_camera_set_property; @@ -370,48 +373,48 @@ static void uca_camera_init(UcaCamera *camera) */ } -static UcaCamera *uca_camera_new_from_type(const gchar *type, GError **error) -{ -#ifdef HAVE_MOCK_CAMERA - if (!g_strcmp0(type, "mock")) - return UCA_CAMERA(uca_mock_camera_new(error)); -#endif - -#ifdef HAVE_PCO_CL - if (!g_strcmp0(type, "pco")) - return UCA_CAMERA(uca_pco_camera_new(error)); -#endif - -#ifdef HAVE_PYLON_CAMERA - if (!g_strcmp0(type, "pylon")) - return UCA_CAMERA(uca_pylon_camera_new(error)); -#endif - -#ifdef HAVE_UFO_CAMERA - if (!g_strcmp0(type, "ufo")) - return UCA_CAMERA(uca_ufo_camera_new(error)); -#endif - -#ifdef HAVE_PHOTON_FOCUS - if (!g_strcmp0(type, "pf")) - return UCA_CAMERA(uca_pf_camera_new(error)); -#endif - - return NULL; -} - -/** - * uca_camera_get_types: - * - * Enumerate all camera types that can be instantiated with uca_camera_new(). - * - * Returns: An array of strings with camera types. The list should be freed with - * g_strfreev(). - */ -gchar **uca_camera_get_types() -{ - return g_strdupv(uca_camera_types); -} +/* static UcaCamera *uca_camera_new_from_type(const gchar *type, GError **error) */ +/* { */ +/* #ifdef HAVE_MOCK_CAMERA */ +/* if (!g_strcmp0(type, "mock")) */ +/* return UCA_CAMERA(uca_mock_camera_new(error)); */ +/* #endif */ + +/* #ifdef HAVE_PCO_CL */ +/* if (!g_strcmp0(type, "pco")) */ +/* return UCA_CAMERA(uca_pco_camera_new(error)); */ +/* #endif */ + +/* #ifdef HAVE_PYLON_CAMERA */ +/* if (!g_strcmp0(type, "pylon")) */ +/* return UCA_CAMERA(uca_pylon_camera_new(error)); */ +/* #endif */ + +/* #ifdef HAVE_UFO_CAMERA */ +/* if (!g_strcmp0(type, "ufo")) */ +/* return UCA_CAMERA(uca_ufo_camera_new(error)); */ +/* #endif */ + +/* #ifdef HAVE_PHOTON_FOCUS */ +/* if (!g_strcmp0(type, "pf")) */ +/* return UCA_CAMERA(uca_pf_camera_new(error)); */ +/* #endif */ + +/* return NULL; */ +/* } */ + +/* /** */ +/* * uca_camera_get_types: */ +/* * */ +/* * Enumerate all camera types that can be instantiated with uca_camera_new(). */ +/* * */ +/* * Returns: An array of strings with camera types. The list should be freed with */ +/* * g_strfreev(). */ +/* *1/ */ +/* gchar **uca_camera_get_types() */ +/* { */ +/* return g_strdupv(uca_camera_types); */ +/* } */ /** * uca_camera_new: @@ -423,26 +426,26 @@ gchar **uca_camera_get_types() * * Returns: A new #UcaCamera of the correct type or %NULL if type was not found */ -UcaCamera *uca_camera_new(const gchar *type, GError **error) -{ - UcaCamera *camera = NULL; - GError *tmp_error = NULL; - - camera = uca_camera_new_from_type(type, &tmp_error); - - if (tmp_error != NULL) { - g_propagate_error(error, tmp_error); - return NULL; - } - - if ((tmp_error == NULL) && (camera == NULL)) { - g_set_error(error, UCA_CAMERA_ERROR, UCA_CAMERA_ERROR_NOT_FOUND, - "Camera type %s not found", type); - return NULL; - } - - return camera; -} +/* UcaCamera *uca_camera_new(const gchar *type, GError **error) */ +/* { */ +/* UcaCamera *camera = NULL; */ +/* GError *tmp_error = NULL; */ + +/* camera = uca_camera_new_from_type(type, &tmp_error); */ + +/* if (tmp_error != NULL) { */ +/* g_propagate_error(error, tmp_error); */ +/* return NULL; */ +/* } */ + +/* if ((tmp_error == NULL) && (camera == NULL)) { */ +/* g_set_error(error, UCA_CAMERA_ERROR, UCA_CAMERA_ERROR_NOT_FOUND, */ +/* "Camera type %s not found", type); */ +/* return NULL; */ +/* } */ + +/* return camera; */ +/* } */ /** * uca_camera_start_recording: diff --git a/src/uca-plugin-manager.c b/src/uca-plugin-manager.c new file mode 100644 index 0000000..7788678 --- /dev/null +++ b/src/uca-plugin-manager.c @@ -0,0 +1,260 @@ +/** + * 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_paths(). An #UcaFilter can be + * instantiated with uca_plugin_manager_get_filter() with a one-to-one mapping + * between filter name xyz and module name libfilterxyz.so. Any errors are + * reported as one of #UcaPluginManagerError codes. + */ +#include <gmodule.h> +#include "uca-plugin-manager.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; +}; + +/** + * 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: + * @config: (allow-none): A #UcaConfiguration object or %NULL. + * + * Create a plugin manager object to instantiate filter objects. When a config + * object is passed to the constructor, its search-path property is added to the + * internal search paths. + * + * Return value: A new plugin manager object. + */ +UcaPluginManager * +uca_plugin_manager_new () +{ + return UCA_PLUGIN_MANAGER (g_object_new (UCA_TYPE_PLUGIN_MANAGER, NULL)); +} + +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_names_from_directory (const gchar *path) +{ + static const gchar *pattern_string = "libuca([A-Za-z]+)"; + GRegex *pattern; + GDir *dir; + GList *result = NULL; + + pattern = g_regex_new (pattern_string, 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) { + g_regex_match (pattern, name, 0, &match_info); + + if (g_match_info_matches (match_info)) { + gchar *word = g_match_info_fetch (match_info, 1); + result = g_list_append (result, word); + } + + name = g_dir_read_name (dir); + } + } + + return result; +} + +GList * +uca_plugin_manager_get_available_cameras (UcaPluginManager *manager) +{ + UcaPluginManagerPrivate *priv; + GList *camera_names; + + g_return_val_if_fail (UCA_IS_PLUGIN_MANAGER (manager), NULL); + + priv = manager->priv; + + for (GList *it = g_list_first (priv->search_paths); it != NULL; it = g_list_next (it)) { + camera_names = g_list_concat (camera_names, + get_camera_names_from_directory ((const gchar*) it->data)); + } + + return camera_names; +} + +UcaCamera * +uca_plugin_manager_new_camera (UcaPluginManager *manager, + const gchar *name, + GError **error) +{ + return NULL; +} + +/** + * uca_plugin_manager_get_filter: + * @manager: A #UcaPluginManager + * @name: Name of the plugin. + * @error: return location for a GError or %NULL + * + * Load a #UcaFilter module and return an instance. The shared object name must + * be * constructed as "libfilter@name.so". + * + * Returns: (transfer none) (allow-none): #UcaFilter or %NULL if module cannot be found + * + * Since: 0.2, the error parameter is available + */ +/* UcaFilter * */ +/* uca_plugin_manager_get_filter (UcaPluginManager *manager, const gchar *name, GError **error) */ +/* { */ +/* g_return_val_if_fail (UCA_IS_PLUGIN_MANAGER (manager) && (name != NULL), NULL); */ +/* UcaPluginManagerPrivate *priv = UCA_PLUGIN_MANAGER_GET_PRIVATE (manager); */ +/* UcaFilter *filter; */ +/* GetFilterFunc *func = NULL; */ +/* GModule *module = NULL; */ +/* gchar *module_name = NULL; */ +/* const gchar *entry_symbol_name = "uca_filter_plugin_new"; */ + +/* func = g_hash_table_lookup (priv->filter_funcs, name); */ + +/* if (func == NULL) { */ +/* module_name = g_strdup_printf ("libucafilter%s.so", name); */ +/* gchar *path = plugin_manager_get_path (priv, module_name); */ + +/* if (path == NULL) { */ +/* g_set_error (error, UCA_PLUGIN_MANAGER_ERROR, UCA_PLUGIN_MANAGER_ERROR_MODULE_NOT_FOUND, */ +/* "Module %s not found", module_name); */ +/* goto handle_error; */ +/* } */ + +/* module = g_module_open (path, G_MODULE_BIND_LAZY); */ +/* g_free (path); */ + +/* if (!module) { */ +/* g_set_error (error, UCA_PLUGIN_MANAGER_ERROR, UCA_PLUGIN_MANAGER_ERROR_MODULE_OPEN, */ +/* "Module %s could not be opened: %s", module_name, g_module_error ()); */ +/* goto handle_error; */ +/* } */ + +/* func = g_malloc0 (sizeof (GetFilterFunc)); */ + +/* if (!g_module_symbol (module, entry_symbol_name, (gpointer *) func)) { */ +/* g_set_error (error, UCA_PLUGIN_MANAGER_ERROR, UCA_PLUGIN_MANAGER_ERROR_SYMBOL_NOT_FOUND, */ +/* "%s is not exported by module %s: %s", entry_symbol_name, module_name, g_module_error ()); */ +/* g_free (func); */ + +/* if (!g_module_close (module)) */ +/* g_warning ("%s", g_module_error ()); */ + +/* goto handle_error; */ +/* } */ + +/* priv->modules = g_slist_append (priv->modules, module); */ +/* g_hash_table_insert (priv->filter_funcs, g_strdup (name), func); */ +/* g_free (module_name); */ +/* } */ + +/* filter = (*func) (); */ +/* uca_filter_set_plugin_name (filter, name); */ +/* g_message ("UcaPluginManager: Created %s-%p", name, (gpointer) filter); */ + +/* return filter; */ + +/* handle_error: */ +/* g_free (module_name); */ +/* return NULL; */ +/* } */ + +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_foreach (priv->search_paths, (GFunc) g_free, NULL); + g_list_free (priv->search_paths); + + 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; + + manager->priv = priv = UCA_PLUGIN_MANAGER_GET_PRIVATE (manager); + priv->search_paths = NULL; + + uca_plugin_manager_add_path (manager, "/usr/lib/uca"); + uca_plugin_manager_add_path (manager, "/usr/lib64/uca"); + uca_plugin_manager_add_path (manager, "/usr/local/lib/uca"); + uca_plugin_manager_add_path (manager, "/usr/local/lib64/uca"); +} diff --git a/src/uca-plugin-manager.h b/src/uca-plugin-manager.h new file mode 100644 index 0000000..9291857 --- /dev/null +++ b/src/uca-plugin-manager.h @@ -0,0 +1,65 @@ +#ifndef __UCA_PLUGIN_MANAGER_H +#define __UCA_PLUGIN_MANAGER_H + +#include <glib-object.h> +#include "uca-camera.h" + +G_BEGIN_DECLS + +#define UCA_TYPE_PLUGIN_MANAGER (uca_plugin_manager_get_type()) +#define UCA_PLUGIN_MANAGER(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), UCA_TYPE_PLUGIN_MANAGER, UcaPluginManager)) +#define UCA_IS_PLUGIN_MANAGER(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), UCA_TYPE_PLUGIN_MANAGER)) +#define UCA_PLUGIN_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), UCA_TYPE_PLUGIN_MANAGER, UcaPluginManagerClass)) +#define UCA_IS_PLUGIN_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), UCA_TYPE_PLUGIN_MANAGER)) +#define UCA_PLUGIN_MANAGER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), UCA_TYPE_PLUGIN_MANAGER, UcaPluginManagerClass)) + +#define UCA_PLUGIN_MANAGER_ERROR uca_plugin_manager_error_quark() +GQuark uca_plugin_manager_error_quark(void); + +typedef enum { + UCA_PLUGIN_MANAGER_ERROR_MODULE_NOT_FOUND, + UCA_PLUGIN_MANAGER_ERROR_MODULE_OPEN, + UCA_PLUGIN_MANAGER_ERROR_SYMBOL_NOT_FOUND +} UcaPluginManagerError; + +typedef struct _UcaPluginManager UcaPluginManager; +typedef struct _UcaPluginManagerClass UcaPluginManagerClass; +typedef struct _UcaPluginManagerPrivate UcaPluginManagerPrivate; + +/** + * UcaPluginManager: + * + * Creates #UcaFilter instances by loading corresponding shared objects. The + * contents of the #UcaPluginManager structure are private and should only be + * accessed via the provided API. + */ +struct _UcaPluginManager { + /*< private >*/ + GObject parent_instance; + + UcaPluginManagerPrivate *priv; +}; + +/** + * UcaPluginManagerClass: + * + * #UcaPluginManager class + */ +struct _UcaPluginManagerClass { + /*< private >*/ + GObjectClass parent_class; +}; + +UcaPluginManager *uca_plugin_manager_new (void); +void uca_plugin_manager_add_path (UcaPluginManager *manager, + const gchar *path); +GList *uca_plugin_manager_get_available_cameras + (UcaPluginManager *manager); +UcaCamera *uca_plugin_manager_new_camera (UcaPluginManager *manager, + const gchar *name, + GError **error); +GType uca_plugin_manager_get_type (void); + +G_END_DECLS + +#endif |