Skip to content

Commit 9cea940

Browse files
authored
Create plugins developer guide (#66)
* Add Type Modules Plugin info in developer guides * Add Libpeas Plugin info in developer guides
1 parent 29bc420 commit 9cea940

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Plugins
2+
=======
3+
4+
.. toctree::
5+
:glob:
6+
:maxdepth: 1
7+
8+
plugins/*
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
Type Modules
2+
============
3+
4+
This example shows you how to implement a GLib.TypeModule based plugin in Vala. It also shows the usage of static construct/destruct block.
5+
6+
.. code-block:: vala
7+
8+
// plugin.vala
9+
public class MyClass : Object
10+
{
11+
static construct
12+
{
13+
message("MyClass init");
14+
}
15+
16+
static ~MyClass()
17+
{
18+
message("MyClass deinit");
19+
}
20+
}
21+
22+
[ModuleInit]
23+
Type plugin_init(GLib.TypeModule type_modul)
24+
{
25+
return typeof(MyClass);
26+
}
27+
28+
29+
.. code-block:: vala
30+
31+
// loader.vala
32+
class MyModule : TypeModule
33+
{
34+
[CCode (has_target = false)]
35+
private delegate Type PluginInitFunc(TypeModule module);
36+
37+
private GLib.Module module = null;
38+
39+
private string name = null;
40+
41+
public MyModule(string name)
42+
{
43+
this.name = name;
44+
}
45+
46+
public override bool load()
47+
{
48+
string path = Module.build_path(null, name);
49+
module = Module.open(path, GLib.ModuleFlags.BIND_LAZY);
50+
if(null == module) {
51+
error("Module not found");
52+
}
53+
54+
void * plugin_init = null;
55+
if(! module.symbol("plugin_init", out plugin_init)) {
56+
error("No such symbol");
57+
}
58+
59+
((PluginInitFunc) plugin_init)(this);
60+
61+
return true;
62+
}
63+
64+
public override void unload()
65+
{
66+
module = null;
67+
68+
message("Library unloaded");
69+
}
70+
}
71+
72+
// Never unref instance of GTypeModule
73+
// http://www.lanedo.com/~mitch/module-system-talk-guadec-2006/Module-System-Talk-Guadec-2006.pdf
74+
static TypeModule module = null;
75+
76+
int main()
77+
{
78+
module = new MyModule("plugin");
79+
module.load();
80+
81+
var o = GLib.Object.new(Type.from_name("MyClass"));
82+
83+
// free last instance, plugin unload
84+
o = null;
85+
86+
return 0;
87+
}
88+
89+
**Build**:
90+
91+
.. code-block:: console
92+
93+
$ valac -o loader loader.vala --pkg=gmodule-2.0
94+
$ valac --ccode plugin.vala
95+
$ gcc -fPIC -shared -o libplugin.so plugin.c $(pkg-config --libs --cflags gobject-2.0 gmodule-2.0)
96+
97+
**Run**:
98+
99+
.. code-block:: console
100+
101+
$ LD_LIBRARY_PATH=$PWD ./loader
102+
103+
Which should output something like this:
104+
105+
.. code-block:: output
106+
107+
** Message: plugin.vala:5: MyClass init
108+
** Message: plugin.vala:10: MyClass deinit
109+
** Message: loader.vala:37: Library unloaded
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Libpeas
2+
=======
3+
4+
In advanced scenarios, you may need to create a plugin engine to manage plugins in your application.
5+
6+
You can save time by using Libpeas, a library that handles that logic of making your application extensible with plugins.
7+
8+
**Links**:
9+
10+
* `Project Repository <https://gitlab.gnome.org/GNOME/libpeas>`_
11+
* `libpeas-2 - Valadoc Package page <https://valadoc.org/libpeas-2/index.htm>`_
12+
* `libpeas-2 - C Documentation <https://gnome.pages.gitlab.gnome.org/libpeas/libpeas-2/>`_

0 commit comments

Comments
 (0)