A plugin is a .so against libsplinter.so, exporting sp_plugin_describe(). The layout changes so there is exactly one copy of each symbol in the process: all logic lives in libsplinter.so and both the binary and every plugin link against it. Two copies would have meant two registries. What the ABI offers: - tools with the authorization classes the loop already enforces, so the decision stays in the harness and not in the plugin; - widgets rendered in the sidebar, one line per widget (name: text); - a worker thread per widget with tick(), which may block, read files, wait on sockets - and which cannot touch curses at all; - sp_host_widget_text() as the only thread-safe write path: the host copies the string under a mutex, so a stack buffer is safe and the UI never holds a pointer a worker could free. Error paths are part of the design: a wrong ABI number is refused with both numbers in the message, a failed init is reported and skipped, a duplicate widget name is refused, a tool without a run function is not registered, and nothing is dlclose()d while a thread might be inside it. Layout: chat on three quarters, sidebar on one (session, transport, theme, last-turn numbers, widgets), status bar underneath. The clock example plugin proves the path end to end - its widget publishes 11:45:23 into the sidebar from its own thread. Seven test binaries green; test_plugin loads the real clock.so with dlopen and checks the worker actually published, plus every refusal above.
3.0 KiB
Plugins
A splinter plugin is a shared object that links against libsplinter.so and
exports one symbol:
const struct sp_plugin *sp_plugin_describe(void);
The struct, the ABI number and the host functions are in
include/splinter_plugin.h. Nothing else is stable. SP_PLUGIN_ABI changes
whenever that header changes in a way that breaks an old plugin, and a plugin
with the wrong number is refused at load time with the two numbers in the
message.
Build
cc -std=c99 -shared -fPIC -o myplugin.so myplugin.c -L. -lsplinter \
-Wl,-rpath,'$ORIGIN'
splinter --plugin ./myplugin.so --chat loads it before the first turn. A
plugin that fails to load is reported on stderr and skipped; the harness keeps
working.
What a plugin may do
| hook | thread | may block | may touch curses |
|---|---|---|---|
init() |
main | no | no |
run() |
main | yes | no |
tick() |
its own | yes | no |
draw() |
main (UI) | no | no |
tick() runs on a thread the host creates and can stop. It may read files,
wait on sockets, sleep. It must not assume it is the only thread.
Anything that should appear in the sidebar is published:
sp_host_widget_text(host, "mywidget", "42 items");
The host copies the string under a mutex, so a stack buffer is fine and the worker never hands the UI a pointer it could free.
Widgets
static const struct sp_widget w = {
.name = "clock",
.tick = clock_tick, /* worker thread, publishes text */
.draw = NULL, /* NULL: render the published text */
.ctx = NULL,
.interval_ms = 1000, /* 0: tick once */
};
sp_host_add_widget(host, &w);
The sidebar lists one line per widget, name: text. draw() exists for
widgets that need more than one line; it runs on the UI thread and must be
fast, because it holds up every other pane while it runs.
Threads, honestly
- One thread per widget with a
tick. A widget withouttickgets no thread. - The host joins every thread in
sp_host_free(), setting a stop flag first. Atickthat never returns therefore hangs the exit; that is a bug in the plugin, not something the host can fix from outside. - No plugin is
dlclose()d. Unmapping code that a thread might still be inside is how a harness dies in a way nobody can debug. - The host pointer is thread-safe only for
sp_host_widget_text()andsp_host_note(). Everything else belongs to the main thread. That is a rule, not an accident.
Tools
A plugin registers tools with the same authorization classes the builtins use:
struct sp_tool t = { 0 };
t.name = "clock";
t.description = "Report the local date and time.";
t.auth = SP_AUTH_READ;
t.run = clock_tool;
sp_host_add_tool(host, &t);
SP_AUTH_READ runs. SP_AUTH_MUTATE and SP_AUTH_DESTRUCTIVE ask the
operator unless the harness was started with --autonomous. The loop decides
this, not the plugin.