src/plugins.h File Reference

Header for plugin utilities. More...

#include <glib.h>
Include dependency graph for plugins.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  plugin_t
 Describes a Synema plugin. More...

Functions

GList * plugin_list_new (const gchar *)
 Creates a list of plugins available in the given directory.
void plugin_list_free (GList *list)
 Frees a list of plugins.
plugin_tplugin_new (const gchar *, const gchar *)
 Creates a new plugin.
void * plugin_get_symbol (plugin_t *, const char *, gboolean)
 Gets a symbol in a plugin's library file.
void plugin_free (plugin_t *)
 Frees a plugin_t struct.
plugin_tplugin_list_find_custom_list (GList *, const gchar *)
 Find a plugin matching name in a given list of plugins.
plugin_tplugin_list_find (const gchar *)
 Searches the Synema instance for a plugin.
gboolean plugin_exists (const gchar *)
 Checks if a plugin exists.
gchar * plugin_get_display_name (plugin_t *)
 Returns the display name of a plugin.
GList * plugin_get_time_periods (plugin_t *)
 Returns the time periods available for a plugin.

Detailed Description

Header for plugin utilities.

Author:
Steve Dodier <sidnioulz@gmail.com>

This header file contains the plugin_t data structure and plugin related functions.

Definition in file plugins.h.


Function Documentation

gboolean plugin_exists ( const gchar *  name  ) 

Checks if a plugin exists.

This function checks if a plugin exists by calling plugin_list_find and checking if the result is different from NULL.

Parameters:
[in] name the name of the plugin to look for
Returns:
TRUE if the plugin exists, FALSE otherwise

Definition at line 160 of file plugins.c.

00161 {
00162     return plugin_list_find (name) != NULL;
00163 }

void plugin_free ( plugin_t pt  ) 

Frees a plugin_t struct.

This function frees a plugin.

Parameters:
[out] pt the plugin to free

Definition at line 126 of file plugins.c.

00127 {
00128     dlclose (pt->handle);
00129     g_free (pt->name);
00130     g_free (pt);
00131 }

gchar* plugin_get_display_name ( plugin_t pt  ) 

Returns the display name of a plugin.

This function returns the display name of a plugin, as a static string. Don't modify or free it.

Parameters:
[in] pt the plugin
Returns:
the display name of the plugin. Do not modify or free this string

Definition at line 167 of file plugins.c.

00168 {
00169     g_return_val_if_fail (pt != NULL, NULL);
00170 
00171     gchar *(*func)(frame_t *) = NULL;
00172 
00173     *(void **) (&func) = plugin_get_symbol (pt, "get_display_name", TRUE);
00174     if (!func) {
00175         g_warning ("plugin_get_display_name: Failed to get the display name");
00176         return NULL;
00177     }
00178 
00179     return func(NULL);
00180 }

void* plugin_get_symbol ( plugin_t pt,
const char *  symbol_name,
gboolean  display_warnings 
)

Gets a symbol in a plugin's library file.

This function gets a symbol in a plugin. The name of the symbol is given as a parameter.

Parameters:
[in] pt the plugin whose symbol to get
[in] symbol_name the name of the symbol
[in] display_warnings whether to display warnings if the symbol isn't found.
Returns:
the symbol if it was found, or NULL otherwise

Definition at line 106 of file plugins.c.

00107 {
00108     g_return_val_if_fail (pt != NULL, NULL);
00109 
00110     char    *err    = NULL;
00111     void    *symbol = NULL;
00112 
00113     * (void **) &symbol = dlsym (pt->handle, symbol_name);
00114     err = dlerror ();
00115     if (err) {
00116         if (display_warnings)
00117             g_warning ("plugin_get_symbol: %s", err);
00118         return NULL;
00119     }
00120 
00121     return symbol;
00122 }

GList* plugin_get_time_periods ( plugin_t pt  ) 

Returns the time periods available for a plugin.

This function returns a GList of pointers to time_period_t available for a given plugin.

Parameters:
[in] pt the plugin
Returns:
a GList of time periods available for this plugin

Definition at line 184 of file plugins.c.

00185 {
00186     g_return_val_if_fail (pt != NULL, NULL);
00187 
00188     GList *(*func)() = NULL;
00189 
00190     *(void **) (&func) = plugin_get_symbol (pt, "build_time_periods", TRUE);
00191     if (!func) {
00192         g_warning ("plugin_get_time_periods: Failed to get the list of time periods");
00193         return NULL;
00194     }
00195 
00196     return func();
00197 }

plugin_t* plugin_list_find ( const gchar *  name  ) 

Searches the Synema instance for a plugin.

This function is a wrapper for plugin_list_find_custom_list which will use the application instance's list of plugins as a list.

Parameters:
[in] name the name of the wanted plugin
Returns:
the plugin if it was found, NULL otherwise

Definition at line 152 of file plugins.c.

00153 {
00154     synema_instance_t   *inst   = synema_instance ();
00155     return (plugin_list_find_custom_list (inst->plugins_list, name));
00156 }

plugin_t* plugin_list_find_custom_list ( GList *  list,
const gchar *  name 
)

Find a plugin matching name in a given list of plugins.

This function searches through a list of plugin_t structs given as a parameter for a plugin whose name is also given as a parameter.

Parameters:
[in] list the list in which to search
[in] name the name of the plugin to find
Returns:
the plugin if it was found, NULL otherwise

Definition at line 134 of file plugins.c.

00135 {
00136     GList           *iter   = list;
00137     plugin_t        *pt     = NULL;
00138 
00139     if (iter) {
00140         do {
00141             pt = iter->data;
00142             if (g_strcmp0 (name, pt->name) == 0)
00143                 return pt;
00144         } while ((iter = g_list_next (iter)) != NULL);
00145     }
00146 
00147     return NULL;
00148 }

void plugin_list_free ( GList *  list  ) 

Frees a list of plugins.

This function frees the plugin_t structs contained in a given list of plugin_t pointers.

Parameters:
[out] list the list of plugins to free

Definition at line 71 of file plugins.c.

00072 {
00073     if (list) {
00074         GList *iter = list;
00075         do {
00076             plugin_free (iter->data);
00077         } while ((iter = g_list_next (iter)) != NULL);
00078     }
00079 
00080     g_list_free (list);
00081 }

GList* plugin_list_new ( const gchar *  plugins_dir  ) 

Creates a list of plugins available in the given directory.

This function creates a list of plugins based on the .so files available in the directory given as a parameter.

Parameters:
[in] plugins_dir the directory to scan for plugins
Returns:
a list of pointers to plugin_t structs

Definition at line 31 of file plugins.c.

00032 {
00033     const gchar     *filename   = NULL;
00034     gchar           *pluginname = NULL;
00035     GError          *err        = NULL;
00036     GFile           *file       = g_file_new_for_path (plugins_dir);
00037     GFileEnumerator *fileenum   = g_file_enumerate_children (file, "standard::name", G_FILE_QUERY_INFO_NONE, NULL, &err);
00038     GFileInfo       *current    = NULL;
00039     gint            len         = 0;
00040     GList           *list       = NULL;
00041 
00042     if (err) {
00043         g_warning ("plugin_list_new: Error while trying to read the directory containing plugins (%s)", err->message);
00044         g_clear_error (&err);
00045         return NULL;
00046     }
00047 
00048     while ((current = g_file_enumerator_next_file (fileenum, NULL, &err)) != NULL) {
00049         if (err) {
00050             g_warning ("plugin_list_new: Error while reading a file in the plugins directory (%s)", err->message);
00051             g_clear_error (&err);
00052         } else {
00053             filename = g_file_info_get_name (current);
00054             len = g_strrstr (filename, ".so") - filename;
00055 
00056             pluginname = g_strndup (filename, len);
00057             list = g_list_prepend (list, plugin_new (plugins_dir, pluginname));
00058 
00059             g_free (pluginname);
00060             g_object_unref (current);
00061         }
00062     }
00063 
00064     g_object_unref (fileenum);
00065     g_object_unref (file);
00066     return list;
00067 }

plugin_t* plugin_new ( const gchar *  plugins_dir,
const gchar *  name 
)

Creates a new plugin.

This function creates a plugin, with a name given as a parameter. It gets the dlhandle from a .so file which should be in plugins_dir.

Parameters:
[in] plugins_dir the directory in which to look for the .so file matching name
[in] name the name to give to the plugin
Returns:
a new plugin_t that should be freed with plugin_free

Definition at line 85 of file plugins.c.

00086 {
00087     plugin_t    *pt     = g_malloc (sizeof (plugin_t));
00088     gchar       *path   = g_strdup_printf ("%s/%s.so", plugins_dir, name);
00089     char        *err    = NULL;
00090 
00091     pt->name = g_strdup (name); 
00092     pt->handle = dlopen (path, RTLD_NOW);
00093     g_free (path);
00094     
00095     err = dlerror ();
00096     if (err) {
00097         g_warning ("plugin_new: %s", err);
00098         return NULL;
00099     }
00100 
00101     return pt;
00102 }

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines

Generated on Tue Jan 12 00:48:45 2010 for ENSIBSynema by  doxygen 1.6.1