changed: the way how plugins work

all your plugin functions can have signature either:
void my_function(PluginInfo & info); or
void my_function();
only the main Init should have:
extern "C" void Init(PluginFunction & info);

added: directory 'plugins' for plugins
added: 'stats' plugin
		  


git-svn-id: svn://ttmath.org/publicrep/winix/trunk@624 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
2010-07-27 20:41:56 +00:00
parent e4683b9a05
commit 031ace3fe5
33 changed files with 1268 additions and 399 deletions

View File

@@ -21,7 +21,7 @@ size_t i;
slots.clear();
for(i=0 ; i<plugins.size() ; ++i)
dlclose(plugins[i]);
dlclose(plugins[i].handle);
plugins.clear();
}
@@ -54,88 +54,158 @@ void Plugin::LoadPlugin(const std::string & filename)
}
void Plugin::LoadPlugin(const char * filename)
void * Plugin::LoadInitFun(const char * filename, Fun1 & fun_init)
{
void * p = dlopen(filename, RTLD_NOW | RTLD_LOCAL);
if( !p )
{
log << log1 << "Pl: cannot load a plugin: " << filename << logend;
/*
const char * t = dlerror();
if( t )
log << log1 << t << logend;
*/
return;
log << log1 << "Plugin: cannot load a plugin: " << filename << logend;
return 0;
}
Fun fun = (Fun)dlfunc(p, "Init");
fun_init = (Fun1)dlfunc(p, "Init");
if( !fun )
if( !fun_init )
{
log << log1 << "Pl: cannot load a plugin: " << filename << " (there is no Init() function)" << logend;
log << log1 << "Plugin: cannot load a plugin: " << filename
<< " (there is no Init() function)" << logend;
dlclose(p);
return 0;
}
log << log1 << "Plugin: plugin loaded"
<< ", file: " << filename
<< ", index: " << plugins.size() << logend;
return p;
}
void Plugin::LoadPlugin(const char * filename)
{
Fun1 fun_init;
void * plugin_handle;
int old_current_plugin;
if( !(plugin_handle = LoadInitFun(filename, fun_init)) )
return;
}
arg.Clear();
int old_current_plugin = current_plugin;
info.Clear();
old_current_plugin = current_plugin;
current_plugin = (int)plugins.size();
info.plugin_id = current_plugin;
current_plugin = (int)plugins.size();
arg.plugin_id = current_plugin;
fun_init(info);
if( fun(&arg) )
{
log << log1 << "Pl: plugin loaded: " << filename << ", index: " << plugins.size() << logend;
plugins.push_back(p);
}
else
{
log << log1 << "Pl: plugin Init() returned false (" << filename << ") " << logend;
dlclose(p);
}
PluginsItem item;
item.handle = plugin_handle;
item.plugin_name = (const char *)info.p1;
plugins.push_back(item);
current_plugin = old_current_plugin;
}
void Plugin::Call(int message, Slots::iterator & slot)
{
current_plugin = slot->second.index;
info.plugin_id = current_plugin;
Arg * Plugin::Call(int message, void * a, void * a2, void * a3)
if( request.session && current_plugin != -1 )
info.plugin_data_base = request.session->plugin_data.Get(current_plugin);
else
info.plugin_data_base = 0;
if( !slot->second.is_running )
{
slot->second.is_running = true;
if( slot->second.fun1 )
slot->second.fun1(info);
if( slot->second.fun2 )
slot->second.fun2();
slot->second.is_running = false;
}
else
{
log << log1 << "Plugin: id: " << slot->second.index
<< ", message: " << message
<< ", recurrences are not allowed" << logend;
}
}
void Plugin::Call(int message, void * p1_, void * p2_, long l1_, long l2_)
{
Slots::iterator i = slots.lower_bound(message);
arg.Clear();
int old_current_plugin = current_plugin;
for( ; i!=slots.end() && i->first==message ; ++i )
{
arg.app = a;
arg.app2 = a2;
arg.app3 = a3;
current_plugin = i->second.index;
arg.plugin_id = current_plugin;
info.Clear();
info.p1 = p1_;
info.p2 = p2_;
info.l1 = l1_;
info.l2 = l2_;
if( request.session && current_plugin != -1 )
arg.plugin_data_base = request.session->plugin_data.Get(current_plugin);
else
arg.plugin_data_base = 0;
if( i->second.fun(&arg) )
arg.ret_true++;
else
arg.ret_false++;
Call(message, i);
}
current_plugin = old_current_plugin;
return &arg;
}
void Plugin::Call(int message)
{
Call(message, 0, 0, 0, 0);
}
void Plugin::Call(int message, void * p1_)
{
Call(message, p1_, 0, 0, 0);
}
void Plugin::Call(int message, void * p1_, void * p2_)
{
Call(message, p1_, p2_, 0, 0);
}
void Plugin::Call(int message, long l1_)
{
Call(message, 0, 0, l1_, 0);
}
void Plugin::Call(int message, long l1_, long l2_)
{
Call(message, 0, 0, l1_, l2_);
}
void Plugin::Call(int message, void * p1_, long l1_)
{
Call(message, p1_, 0, l1_, 0);
}
void Plugin::Call(int message, void * p1_, long l1_, long l2_)
{
Call(message, p1_, 0, l1_, l2_);
}
void Plugin::Call(int message, void * p1_, void * p2_, long l1_)
{
Call(message, p1_, p2_, l1_, 0);
}
size_t Plugin::Size()
{
return plugins.size();
@@ -143,14 +213,26 @@ size_t Plugin::Size()
void Plugin::Assign(int message, Fun fun)
void Plugin::Assign(int message, Fun1 fun1)
{
Slot s;
Slot s;
s.fun = fun;
s.fun1 = fun1;
s.index = current_plugin;
slots.insert( std::make_pair(message, s) );
log << log3 << "Plugin: added function for message: " << message << ", plugin index: " << s.index << logend;
}
void Plugin::Assign(int message, Fun2 fun2)
{
Slot s;
s.fun2 = fun2;
s.index = current_plugin;
slots.insert( std::make_pair(message, s) );
log << log3 << "Plugin: added function for message: " << message << ", plugin index: " << s.index << logend;
}