diff options
author | fiaxh <git@mx.ax.lt> | 2017-03-10 19:53:24 +0100 |
---|---|---|
committer | fiaxh <git@mx.ax.lt> | 2017-03-10 19:53:24 +0100 |
commit | 24b55d83a1f9e21b028c8d5ac9778e75565f417d (patch) | |
tree | 195b0f332e32d08e1a18fc3d8a5a6725c5ed11d7 /libdino/src/plugin.vala | |
parent | 4c48c5c16d5f5b010c2e5dfb1affcb59f937510c (diff) | |
parent | 29ca70a6d534e1cd79963718c793ae740318cff1 (diff) | |
download | dino-24b55d83a1f9e21b028c8d5ac9778e75565f417d.tar.gz dino-24b55d83a1f9e21b028c8d5ac9778e75565f417d.zip |
Merge branch 'master' of github.com:dino/dino
Diffstat (limited to 'libdino/src/plugin.vala')
-rw-r--r-- | libdino/src/plugin.vala | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/libdino/src/plugin.vala b/libdino/src/plugin.vala new file mode 100644 index 00000000..60a99bc2 --- /dev/null +++ b/libdino/src/plugin.vala @@ -0,0 +1,64 @@ +namespace Dino { + +public errordomain PluginError { + NOT_SUPPORTED, + UNEXPECTED_TYPE, + NO_REGISTRATION_FUNCTION, + FAILED +} + +public interface PluginIface : Object { + public abstract void registered(Dino.Ui.Application app); +} + +private class PluginInfo : Object { + public Module module; + public Type gtype; + + public PluginInfo(Type type, owned Module module) { + this.module = (owned) module; + this.gtype = type; + } +} + +public class PluginLoader : Object { + [CCode (has_target = false)] + private delegate Type RegisterPluginFunction (Module module); + + private PluginIface[] plugins = new PluginIface[0]; + private PluginInfo[] infos = new PluginInfo[0]; + + public PluginIface load(string name, Dino.Ui.Application app) throws PluginError { + if (Module.supported () == false) { + throw new PluginError.NOT_SUPPORTED ("Plugins are not supported"); + } + + Module module = Module.open ("plugins/" + name, ModuleFlags.BIND_LAZY); + if (module == null) { + throw new PluginError.FAILED (Module.error ()); + } + + void* function; + module.symbol ("register_plugin", out function); + if (function == null) { + throw new PluginError.NO_REGISTRATION_FUNCTION ("register_plugin () not found"); + } + + RegisterPluginFunction register_plugin = (RegisterPluginFunction) function; + Type type = register_plugin (module); + if (type.is_a (typeof (PluginIface)) == false) { + throw new PluginError.UNEXPECTED_TYPE ("Unexpected type"); + } + + PluginInfo info = new PluginInfo (type, (owned) module); + infos += info; + + PluginIface plugin = (PluginIface) Object.new (type); + plugins += plugin; + plugin.registered (app); + + return plugin; + } +} + +}
\ No newline at end of file |