/* This is a first attempt in implementation of plugins to Trisurf-ng. */
|
|
/*
|
1. Capabilities of Plugin
|
-------------------------
|
|
Every plugin should list it's capabilities -- that is what subrutines are available in it. Capabilities list for the moment are:
|
|
* PLUGIN_PROGRAM_START (requires subrutine void at_start(void)): runs at startup
|
* PLUGIN_VESICLE_INITIALIZED (requires subrutine void after_vesicle_init(vesicle *vesicle):
|
runs after vesicle gets initialized.
|
|
* PLUGIN_VERTEX_MOVE (requires subrutines:
|
|
ts_bool vm_hard_constraint(vesicle *vesicle, vtx *vtx, vtx *old_vtx)
|
ts_double vm_energy_before(vesicle *vesicle, vtx *vtx)
|
ts_double vm_energy_after(vesicle *vesicle, vtx *vtx)
|
ts_double vm_new_state_accepted(vesicle *vesicle, vtx *vtx)
|
ts_double vm_new_state_rejected(vesicle *vesicle, vtx *vtx)
|
|
)
|
|
these subrutines are added into the vertex move into right places
|
|
* PLUGIN_BOND_FLIP ( N/A yet)
|
*/
|
#include "plugins.h"
|
#include <stdlib.h>
|
#include <dlfcn.h>
|
#include <string.h>
|
#include "general.h"
|
|
ts_plugin *init_plugin(ts_char *filename){
|
ts_plugin *plugin = (ts_plugin *)calloc(1, sizeof(ts_plugin));
|
plugin->filename=(ts_char *)calloc(strlen(filename),sizeof(ts_char));
|
strcpy(plugin->filename,filename);
|
plugin->libhandle = dlopen(plugin->filename, RTLD_NOW);
|
if(!plugin->libhandle){
|
ts_fprintf(stderr,"Error loading plugin: %s\n", filename);
|
fatal("Exiting",235);
|
}
|
ts_plugin_details* (*get_plugin_details)(void) = dlsym(plugin->libhandle, "init");
|
plugin->details = get_plugin_details();
|
plugin->function = (ts_plugin_function *)calloc(1,sizeof(ts_plugin_function));
|
|
plugin->function->cleanup = dlsym(plugin->libhandle, "cleanup");
|
plugin->function->at_start = dlsym(plugin->libhandle, "at_start");
|
plugin->function->after_vesicle_init = dlsym(plugin->libhandle, "after_vesicle_init");
|
plugin->function->vm_hard_constraint = dlsym(plugin->libhandle, "vm_hard_constraint");
|
plugin->function->vm_energy_before = dlsym(plugin->libhandle, "vm_energy_before");
|
plugin->function->vm_energy_after = dlsym(plugin->libhandle, "vm_energy_after");
|
plugin->function->vm_new_state_rejected = dlsym(plugin->libhandle, "vm_new_state_rejected");
|
plugin->function->vm_new_state_accepted = dlsym(plugin->libhandle, "vm_new_state_accepted");
|
|
return plugin;
|
}
|
|
|
ts_plugin_list *init_plugin_list(ts_char **plugin_filenames, ts_uint number_of_plugins){
|
ts_plugin_list *plist=(ts_plugin_list *)calloc(1, sizeof(ts_plugin_list));
|
plist->plugin=(ts_plugin **)calloc(number_of_plugins, sizeof(ts_plugin *));
|
for(int i=0;i<number_of_plugins;i++){
|
plist->plugin[i]=init_plugin(plugin_filenames[i]);
|
}
|
return plist;
|
}
|
|
void free_plugin(ts_plugin *plugin){
|
plugin->function->cleanup();
|
free(plugin->function);
|
free(plugin->details);
|
free(plugin->filename);
|
dlclose(plugin->libhandle);
|
free(plugin);
|
}
|
|
void free_plugin_list(ts_plugin_list *plist){
|
for(int i=0;i<plist->n;i++){
|
free_plugin(plist->plugin[i]);
|
}
|
free(plist);
|
}
|