diff options
author | Dennis Kasprzyk <onestone@compiz-fusion.org> | 2008-08-20 21:22:46 +0200 |
---|---|---|
committer | Dennis kasprzyk <onestone@compiz-fusion.org> | 2008-08-20 21:22:46 +0200 |
commit | 3808dcc20b40c441f2c3b72bba151a6404d1f322 (patch) | |
tree | 09303902f99dfe1cf803eaa87d1c69f605cc27a1 /src/plugin.cpp | |
parent | 7dee054a80fec3640fa5b4621325ce768008a152 (diff) | |
download | compiz-with-glib-mainloop-3808dcc20b40c441f2c3b72bba151a6404d1f322.tar.gz compiz-with-glib-mainloop-3808dcc20b40c441f2c3b72bba151a6404d1f322.tar.bz2 |
Move Plugin related function into CompPlugin header.
Diffstat (limited to 'src/plugin.cpp')
-rw-r--r-- | src/plugin.cpp | 299 |
1 files changed, 118 insertions, 181 deletions
diff --git a/src/plugin.cpp b/src/plugin.cpp index 88e0cbf..c46b672 100644 --- a/src/plugin.cpp +++ b/src/plugin.cpp @@ -28,13 +28,17 @@ #include <string.h> #include <dlfcn.h> #include <dirent.h> +#include <list> + +#include <boost/foreach.hpp> +#define foreach BOOST_FOREACH #include <compiz-core.h> #include <compobject.h> -CompPlugin *plugins = 0; +std::list<CompPlugin *> plugins; -class CorePluginVTable : public CompPluginVTable +class CorePluginVTable : public CompPlugin::VTable { public: @@ -103,27 +107,25 @@ CorePluginVTable::setObjectOption (CompObject *object, RETURN_DISPATCH (object, dispTab, ARRAY_SIZE (dispTab), false, (object, name, value)); } - - - +// CorePluginVTable coreVTable; -static Bool +static bool cloaderLoadPlugin (CompPlugin *p, const char *path, const char *name) { if (path) - return FALSE; + return false; if (strcmp (name, coreVTable.name ())) - return FALSE; + return false; p->vTable = &coreVTable; p->devPrivate.ptr = NULL; p->devType = "cloader"; - return TRUE; + return true; } static void @@ -131,32 +133,20 @@ cloaderUnloadPlugin (CompPlugin *p) { } -static char ** -cloaderListPlugins (const char *path, - int *n) +static CompStringList +cloaderListPlugins (const char *path) { - char **list; + CompStringList rv; if (path) - return 0; - - list = (char **) malloc (sizeof (char *)); - if (!list) - return 0; - - *list = strdup (coreVTable.name ()); - if (!*list) - { - free (list); - return 0; - } + return CompStringList (); - *n = 1; + rv.push_back (CompString (coreVTable.name ())); - return list; + return rv; } -static Bool +static bool dlloaderLoadPlugin (CompPlugin *p, const char *path, const char *name) @@ -166,7 +156,7 @@ dlloaderLoadPlugin (CompPlugin *p, file = (char *) malloc ((path ? strlen (path) : 0) + strlen (name) + 8); if (!file) - return FALSE; + return false; if (path) sprintf (file, "%s/lib%s.so", path, name); @@ -205,7 +195,7 @@ dlloaderLoadPlugin (CompPlugin *p, dlclose (dlhand); free (file); - return FALSE; + return false; } } else @@ -213,7 +203,7 @@ dlloaderLoadPlugin (CompPlugin *p, dlclose (dlhand); free (file); - return FALSE; + return false; } } else @@ -228,13 +218,13 @@ dlloaderLoadPlugin (CompPlugin *p, p->devPrivate.ptr = dlhand; p->devType = "dlloader"; - return TRUE; + return true; } static void dlloaderUnloadPlugin (CompPlugin *p) { - if (strcmp (p->devType, "dlloader") == 0) + if (p->devType.compare ("dlloader") == 0) dlclose (p->devPrivate.ptr); else cloaderUnloadPlugin (p); @@ -255,54 +245,33 @@ dlloaderFilter (const struct dirent *name) return 1; } -static char ** -dlloaderListPlugins (const char *path, - int *n) +static CompStringList +dlloaderListPlugins (const char *path) { struct dirent **nameList; - char **list, **cList; - char *name; - int length, nFile, i, j = 0; + char name[1024]; + int length, nFile, i; - cList = cloaderListPlugins (path, n); - if (cList) - j = *n; + CompStringList rv = cloaderListPlugins (path); if (!path) path = "."; nFile = scandir (path, &nameList, dlloaderFilter, alphasort); if (!nFile) - return cList; - - list = (char **) realloc (cList, (j + nFile) * sizeof (char *)); - if (!list) - return cList; + return rv; for (i = 0; i < nFile; i++) { length = strlen (nameList[i]->d_name); - name = (char *) malloc ((length - 5) * sizeof (char)); - if (name) - { - strncpy (name, nameList[i]->d_name + 3, length - 6); - name[length - 6] = '\0'; + strncpy (name, nameList[i]->d_name + 3, length - 6); + name[length - 6] = '\0'; - list[j++] = name; - } + rv.push_back (CompString (name)); } - if (j) - { - *n = j; - - return list; - } - - free (list); - - return NULL; + return rv; } LoadPluginProc loaderLoadPlugin = dlloaderLoadPlugin; @@ -423,50 +392,50 @@ finiPlugin (CompPlugin *p) } bool -objectInitPlugins (CompObject *o) +CompPlugin::objectInitPlugins (CompObject *o) { InitObjectContext ctx; - CompPlugin *p; - int i, j = 0; - ctx.object = NULL; + CompPlugin::List::reverse_iterator rit = plugins.rbegin (); - for (p = plugins; p; p = p->next) - j++; + ctx.object = NULL; - while (j--) + while (rit != plugins.rend ()) { - i = 0; - for (p = plugins; i < j; p = p->next) - i++; - ctx.plugin = p; + ctx.plugin = (*rit); if (!initObjectTree (o, (void *) &ctx)) { - for (p = p->next; p; p = p->next) + if (rit == plugins.rbegin ()) + return false; + --rit; + for (; rit != plugins.rbegin (); --rit) { - ctx.plugin = p; + ctx.plugin = (*rit); finiObjectTree (o, (void *) &ctx); } + ctx.plugin = (*rit); + + finiObjectTree (o, (void *) &ctx); return false; } + rit++; } return true; } void -objectFiniPlugins (CompObject *o) +CompPlugin::objectFiniPlugins (CompObject *o) { InitObjectContext ctx; - CompPlugin *p; ctx.object = NULL; - for (p = plugins; p; p = p->next) + foreach (CompPlugin *p, plugins) { ctx.plugin = p; @@ -475,40 +444,37 @@ objectFiniPlugins (CompObject *o) } CompPlugin * -findActivePlugin (const char *name) +CompPlugin::find (const char *name) { - CompPlugin *p; - - for (p = plugins; p; p = p->next) + foreach (CompPlugin *p, plugins) { if (strcmp (p->vTable->name (), name) == 0) return p; } - return 0; + return NULL; } void -unloadPlugin (CompPlugin *p) +CompPlugin::unload (CompPlugin *p) { (*loaderUnloadPlugin) (p); - free (p); + delete p; } CompPlugin * -loadPlugin (const char *name) +CompPlugin::load (const char *name) { CompPlugin *p; char *home, *plugindir; Bool status; - p = (CompPlugin *) malloc (sizeof (CompPlugin)); + p = new CompPlugin (); if (!p) return 0; - p->next = 0; p->devPrivate.uval = 0; - p->devType = NULL; + p->devType = ""; p->vTable = 0; home = getenv ("HOME"); @@ -540,75 +506,70 @@ loadPlugin (const char *name) return 0; } -Bool -pushPlugin (CompPlugin *p) +bool +CompPlugin::push (CompPlugin *p) { - if (findActivePlugin (p->vTable->name ())) + if (find (p->vTable->name ())) { compLogMessage (NULL, "core", CompLogLevelWarn, "Plugin '%s' already active", p->vTable->name ()); - return FALSE; + return false; } - p->next = plugins; - plugins = p; + plugins.push_front (p); if (!initPlugin (p)) { compLogMessage (NULL, "core", CompLogLevelError, "Couldn't activate plugin '%s'", p->vTable->name ()); - plugins = p->next; - return FALSE; + plugins.pop_front (); + + return false; } - return TRUE; + return true; } CompPlugin * -popPlugin (void) +CompPlugin::pop (void) { - CompPlugin *p = plugins; + CompPlugin *p = plugins.front (); if (!p) return 0; finiPlugin (p); - plugins = p->next; + plugins.pop_front (); return p; } -CompPlugin * -getPlugins (void) +CompPlugin::List & +CompPlugin::getPlugins (void) { return plugins; } -static Bool -stringExist (char **list, - int nList, - char *s) +static bool +stringExist (CompStringList &list, + CompString s) { - int i; - - for (i = 0; i < nList; i++) - if (strcmp (list[i], s) == 0) - return TRUE; + foreach (CompString &l, list) + if (s.compare (l) == 0) + return true; - return FALSE; + return false; } -char ** -availablePlugins (int *n) +CompStringList +CompPlugin::availablePlugins () { char *home, *plugindir; - char **list, **currentList, **pluginList, **homeList = NULL; - int nCurrentList, nPluginList, nHomeList; - int count, i, j; + CompStringList list, currentList, pluginList, homeList; home = getenv ("HOME"); if (home) @@ -617,66 +578,42 @@ availablePlugins (int *n) if (plugindir) { sprintf (plugindir, "%s/%s", home, HOME_PLUGINDIR); - homeList = (*loaderListPlugins) (plugindir, &nHomeList); + homeList = (*loaderListPlugins) (plugindir); free (plugindir); } } - pluginList = (*loaderListPlugins) (PLUGINDIR, &nPluginList); - currentList = (*loaderListPlugins) (NULL, &nCurrentList); + pluginList = (*loaderListPlugins) (PLUGINDIR); + currentList = (*loaderListPlugins) (NULL); - count = 0; - if (homeList) - count += nHomeList; - if (pluginList) - count += nPluginList; - if (currentList) - count += nCurrentList; - - if (!count) - return NULL; - - list = (char **) malloc (count * sizeof (char *)); - if (!list) - return NULL; - - j = 0; - if (homeList) + if (!homeList.empty ()) { - for (i = 0; i < nHomeList; i++) - if (!stringExist (list, j, homeList[i])) - list[j++] = homeList[i]; - - free (homeList); + foreach (CompString &s, homeList) + if (!stringExist (list, s)) + list.push_back (s); } - if (pluginList) + if (!pluginList.empty ()) { - for (i = 0; i < nPluginList; i++) - if (!stringExist (list, j, pluginList[i])) - list[j++] = pluginList[i]; - - free (pluginList); + foreach (CompString &s, pluginList) + if (!stringExist (list, s)) + list.push_back (s); } - if (currentList) + if (!currentList.empty ()) { - for (i = 0; i < nCurrentList; i++) - if (!stringExist (list, j, currentList[i])) - list[j++] = currentList[i]; - - free (currentList); + foreach (CompString &s, currentList) + if (!stringExist (list, s)) + list.push_back (s); } - *n = j; - return list; } int -getPluginABI (const char *name) +CompPlugin::getPluginABI (const char *name) { - CompPlugin *p = findActivePlugin (name); + CompPlugin *p = find (name); if (!p) return 0; @@ -688,9 +625,9 @@ getPluginABI (const char *name) return CompOption::getIntOptionNamed (options, "abi"); } -Bool -checkPluginABI (const char *name, - int abi) +bool +CompPlugin::checkPluginABI (const char *name, + int abi) { int pluginABI; @@ -699,7 +636,7 @@ checkPluginABI (const char *name, { compLogMessage (NULL, "core", CompLogLevelError, "Plugin '%s' not loaded.\n", name); - return FALSE; + return false; } else if (pluginABI != abi) { @@ -707,65 +644,65 @@ checkPluginABI (const char *name, "Plugin '%s' has ABI version '%d', expected " "ABI version '%d'.\n", name, pluginABI, abi); - return FALSE; + return false; } - return TRUE; + return true; } -Bool -getPluginDisplayIndex (CompDisplay *d, - const char *name, - int *index) +bool +CompPlugin::getPluginDisplayIndex (CompDisplay *d, + const char *name, + int *index) { - CompPlugin *p = findActivePlugin (name); + CompPlugin *p = find (name); int value; if (!p) - return FALSE; + return false; CompOption::Vector &options = p->vTable->getObjectOptions (d); value = CompOption::getIntOptionNamed (options, "index", -1); if (value < 0) - return FALSE; + return false; *index = value; - return TRUE; + return true; } -CompPluginVTable::~CompPluginVTable () +CompPlugin::VTable::~VTable () { } CompMetadata * -CompPluginVTable::getMetadata () +CompPlugin::VTable::getMetadata () { return NULL; } bool -CompPluginVTable::initObject (CompObject *object) +CompPlugin::VTable::initObject (CompObject *object) { return true; } void -CompPluginVTable::finiObject (CompObject *object) +CompPlugin::VTable::finiObject (CompObject *object) { } CompOption::Vector & -CompPluginVTable::getObjectOptions (CompObject *object) +CompPlugin::VTable::getObjectOptions (CompObject *object) { return noOptions; } bool -CompPluginVTable::setObjectOption (CompObject *object, +CompPlugin::VTable::setObjectOption (CompObject *object, const char *name, CompOption::Value &value) { |