lumiera_/src/backend/plugin.c

220 lines
4.7 KiB
C
Raw Normal View History

2007-07-15 17:08:45 +02:00
/*
2008-03-10 06:09:44 +01:00
plugin.c - Lumiera Plugin loader
2007-07-15 17:08:45 +02:00
Copyright (C) Lumiera.org
2008, Christian Thaeter <ct@pipapo.org>
2007-07-15 17:08:45 +02:00
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "lib/safeclib.h"
#include "lib/psplay.h"
#include "lib/mutex.h"
#include "lib/error.h"
#include "backend/interfaceregistry.h"
#include "backend/config.h"
#include "backend/plugin.h"
2007-07-15 02:23:37 +02:00
#include <glob.h>
#include <nobug.h>
/**
* @file
* Plugin loader.
*/
extern PSplay lumiera_pluginregistry;
2007-07-17 04:40:07 +02:00
/* TODO should be set by the build system to the actual plugin path */
2007-07-15 02:23:37 +02:00
/* errors */
2008-03-10 06:09:44 +01:00
LUMIERA_ERROR_DEFINE(PLUGIN_DLOPEN, "Could not open plugin");
2007-07-15 02:23:37 +02:00
2007-07-17 04:40:07 +02:00
/*
supported (planned) plugin types and their file extensions
*/
#define LUMIERA_PLUGIN_TYPES \
LUMIERA_PLUGIN_TYPE(DYNLIB, ".so")
/*
only .so dynlibs for now, later we may support some more
LUMIERA_PLUGIN_TYPE(LUA, ".lua")
LUMIERA_PLUGIN_TYPE(CSOURCE, ".c")
*/
#define LUMIERA_PLUGIN_TYPE(type, ext) LUMIERA_PLUGIN_##type,
2008-03-10 06:09:44 +01:00
enum lumiera_plugin_type
2007-07-17 04:40:07 +02:00
{
LUMIERA_PLUGIN_TYPES
LUMIERA_PLUGIN_NONE
2007-07-17 04:40:07 +02:00
};
#undef LUMIERA_PLUGIN_TYPE
2007-07-17 04:40:07 +02:00
#define LUMIERA_PLUGIN_TYPE(type, ext) ext,
static const char* const lumiera_plugin_ext[] =
2007-07-17 04:40:07 +02:00
{
LUMIERA_PLUGIN_TYPES
NULL
2007-07-17 04:40:07 +02:00
};
#undef LUMIERA_PLUGIN_TYPE
2007-07-17 04:40:07 +02:00
struct lumiera_plugin_struct
2007-07-15 02:23:37 +02:00
{
psplaynode node;
2008-03-10 06:09:44 +01:00
/* long names as looked up ("/usr/local/lib/lumiera/plugins/effects/audio/normalize.so") */
const char* name;
2007-07-15 02:23:37 +02:00
/* use count for all interfaces of this plugin */
unsigned refcnt;
2007-07-15 02:23:37 +02:00
/* time when the last open or close action happened */
2007-07-15 02:23:37 +02:00
time_t last;
2007-07-17 04:40:07 +02:00
/* kind of plugin */
2008-03-10 06:09:44 +01:00
enum lumiera_plugin_type type;
2007-07-17 04:40:07 +02:00
2007-07-15 02:23:37 +02:00
/* dlopen handle */
void* handle;
};
2007-07-17 04:40:07 +02:00
2007-07-15 02:23:37 +02:00
2007-07-17 04:40:07 +02:00
int
lumiera_plugin_discover (LumieraPlugin (*callback_load)(const char* plugin),
int (*callback_register) (LumieraPlugin))
{
TRACE (plugin);
REQUIRE (callback_load);
REQUIRE (callback_register);
/* construct glob trail {.so,.c,.foo} ... */
static char* exts_globs = NULL;
if (!exts_globs)
2007-07-17 04:40:07 +02:00
{
size_t exts_sz = 4; /* / * { } \0 less one comma */
const char*const* itr = lumiera_plugin_ext;
while (*itr)
2007-07-17 04:40:07 +02:00
{
exts_sz += strlen (*itr) + 1;
++itr;
}
2007-07-17 04:40:07 +02:00
exts_globs = lumiera_malloc (exts_sz);
*exts_globs = '\0';
2007-07-17 04:40:07 +02:00
itr = lumiera_plugin_ext;
strcat (exts_globs, "/*{");
while (*itr)
{
strcat (exts_globs, *itr);
strcat (exts_globs, ",");
++itr;
2007-07-17 04:40:07 +02:00
}
exts_globs[exts_sz-2] = '}';
TRACE (plugin, "initialized extension glob to '%s'", exts_globs);
2007-07-17 04:40:07 +02:00
}
2007-07-15 02:23:37 +02:00
const char* path;
unsigned i = 0;
int flags = GLOB_PERIOD|GLOB_BRACE|GLOB_TILDE_CHECK;
glob_t globs;
2007-07-15 02:23:37 +02:00
while ((path = lumiera_config_wordlist_get_nth ("plugin.path", i, ":")))
2007-07-15 02:23:37 +02:00
{
path = lumiera_tmpbuf_snprintf (SIZE_MAX,"%s%s", path, exts_globs);
TRACE (plugin, "globbing path '%s'", path);
int ret = glob (path, flags, NULL, &globs);
if (ret == GLOB_NOSPACE)
LUMIERA_DIE (NO_MEMORY);
flags |= GLOB_APPEND;
++i;
}
2007-07-15 02:23:37 +02:00
LUMIERA_RECMUTEX_SECTION (plugin, &lumiera_interface_mutex)
{
for (char** itr = globs.gl_pathv; *itr; ++itr)
{
if (!psplay_find (lumiera_pluginregistry, *itr, 100))
2007-07-17 04:40:07 +02:00
{
TRACE (plugin, "found new plugin '%s'", *itr);
LumieraPlugin plugin = callback_load (*itr);
if (plugin)
callback_register (plugin);
2007-07-17 04:40:07 +02:00
}
}
}
2007-07-15 02:23:37 +02:00
globfree (&globs);
2007-07-15 02:23:37 +02:00
return !lumiera_error_peek ();
}
2007-07-17 04:40:07 +02:00
2007-08-18 05:05:38 +02:00
2007-07-15 02:23:37 +02:00
2007-08-18 05:05:38 +02:00
LumieraPlugin
lumiera_plugin_load (const char* plugin)
{
TRACE (plugin);
UNIMPLEMENTED();
(void) plugin;
2007-07-15 02:23:37 +02:00
return NULL;
}
2007-07-15 02:23:37 +02:00
int
lumiera_plugin_register (LumieraPlugin plugin)
{
TRACE (plugin);
UNIMPLEMENTED();
LUMIERA_RECMUTEX_SECTION (plugin, &lumiera_interface_mutex)
2007-07-15 02:23:37 +02:00
{
(void) plugin;
2007-07-15 02:23:37 +02:00
}
return 0;
2007-07-15 02:23:37 +02:00
}
2007-07-15 02:23:37 +02:00
int
lumiera_plugin_cmp_fn (const void* keya, const void* keyb)
{
return strcmp ((const char*)keya, (const char*)keyb);
}
2007-07-15 02:23:37 +02:00
const void*
lumiera_plugin_key_fn (const PSplaynode node)
{
return ((LumieraPlugin)node)->name;
2007-07-15 02:23:37 +02:00
}