2 * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
20 #include "Ecore_Data.h"
21 #include "Ecore_Str.h"
22 #include "ecore_private.h"
25 static Ecore_List *loaded_plugins = NULL;
28 * @defgroup Ecore_Plugin Plugin Functions
30 * Functions that load modules of compiled code into memory.
34 * Loads the specified plugin from the specified path group.
35 * @param group The path group to search for the plugin to load
36 * @param plugin_name The name of the plugin to load.
37 * @param version The interface version of the plugin. With version
38 * equal to NULL the default will be loaded.
39 * @return A pointer to the newly loaded plugin on success, @c NULL on
41 * @ingroup Ecore_Plugin
44 ecore_plugin_load(Ecore_Path_Group *group, const char *plugin_name, const char *version)
52 CHECK_PARAM_POINTER_RETURN("plugin_name", plugin_name, NULL);
54 if (!version || *version == '\0')
55 snprintf(temp, sizeof(temp), "%s" SHARED_LIB_SUFFIX, plugin_name);
57 snprintf(temp, sizeof(temp), "%s" SHARED_LIB_SUFFIX ".%s", plugin_name, version);
59 path = ecore_path_group_find(group, temp);
63 /* if this file doesn't exist try a different order */
64 snprintf(temp, sizeof(temp), "%s.%s" SHARED_LIB_SUFFIX, plugin_name, version);
65 path = ecore_path_group_find(group, temp);
71 handle = dlopen(path, RTLD_LAZY);
79 * Allocate the new plugin and initialize it's fields
81 plugin = malloc(sizeof(Ecore_Plugin));
88 memset(plugin, 0, sizeof(Ecore_Plugin));
90 plugin->handle = handle;
93 * Now add it to the list of the groups loaded plugins
96 loaded_plugins = ecore_list_new();
98 ecore_list_append(loaded_plugins, plugin);
106 * Unloads the given plugin from memory.
107 * @param plugin The given plugin.
108 * @ingroup Ecore_Plugin
111 ecore_plugin_unload(Ecore_Plugin *plugin)
113 CHECK_PARAM_POINTER("plugin", plugin);
118 if (ecore_list_goto(loaded_plugins, plugin))
119 ecore_list_remove(loaded_plugins);
121 if (ecore_list_empty_is(loaded_plugins))
123 ecore_list_destroy(loaded_plugins);
124 loaded_plugins = NULL;
127 dlclose(plugin->handle);
133 * Searches for the specified symbol in the given plugin.
134 * @param plugin The given plugin.
135 * @param symbol_name The symbol to search for.
136 * @return Address of the given symbol if successful. Otherwise, @c NULL.
137 * @ingroup Ecore_Plugin
140 ecore_plugin_symbol_get(Ecore_Plugin *plugin, const char *symbol_name)
144 CHECK_PARAM_POINTER_RETURN("plugin", plugin, NULL);
145 CHECK_PARAM_POINTER_RETURN("symbol_name", symbol_name, NULL);
150 ret = dlsym(plugin->handle, symbol_name);
156 * Retrieves a list of all available plugins in the given path.
157 * @param group_id The identifier for the given path.
158 * @return A pointer to a newly allocated list of all plugins found in the
159 * paths identified by @p group_id. @c NULL otherwise.
160 * @ingroup Ecore_Plugin
163 ecore_plugin_available_get(Ecore_Path_Group *group)
165 Ecore_List *avail = NULL;
166 Ecore_Hash *plugins = NULL;
169 CHECK_PARAM_POINTER_RETURN("group", group, NULL);
171 if (!group->paths || ecore_list_empty_is(group->paths))
174 ecore_list_first_goto(group->paths);
175 plugins = ecore_hash_new(ecore_str_hash, ecore_str_compare);
176 ecore_hash_free_key_cb_set(plugins, free);
178 while ((path = ecore_list_next(group->paths)) != NULL)
184 if (stat(path, &st) < 0)
187 if (!S_ISDIR(st.st_mode))
195 while ((d = readdir(dir)) != NULL)
197 char ppath[PATH_MAX];
200 if (*d->d_name == '.')
203 if (!ecore_str_has_suffix(d->d_name, SHARED_LIB_SUFFIX))
206 snprintf(ppath, PATH_MAX, "%s/%s", path, d->d_name);
210 if (!S_ISREG(st.st_mode))
213 ecore_strlcpy(ppath, d->d_name, sizeof(ppath));
214 ext = strrchr(ppath, '.');
217 if (!ecore_hash_get(plugins, ppath))
222 ecore_hash_set(plugins, key, key);
228 ecore_hash_free_key_cb_set(plugins, NULL);
229 avail = ecore_hash_keys(plugins);
230 ecore_list_free_cb_set(avail, free);
231 ecore_hash_destroy(plugins);