Files
splinter/plugins/clock/clock.c
T
Konrad e4f910cc4e splinter: plugin ABI, widget sidebar, libsplinter
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.
2026-11-07 11:47:47 +00:00

84 lines
1.7 KiB
C

/*
* splinter -- example plugin: a clock widget in the sidebar.
*
* Copyright (c) 2026 Konrad Lother
* SPDX-License-Identifier: BSD-3-Clause
*
* Shows the three parts of the ABI that matter:
* - init() registers a widget,
* - tick() runs on the host's worker thread and only publishes text,
* - run() adds a turn-level tool the harness can call.
*
* Build: cc -std=c99 -shared -fPIC -o clock.so clock.c -lsplinter
*/
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "splinter.h"
#include "splinter_plugin.h"
static int
clock_tick(struct sp_host *h, const char *name, void *ctx)
{
char out[128];
time_t now = time(NULL);
struct tm tm;
(void)ctx;
localtime_r(&now, &tm);
snprintf(out, sizeof(out), "%02d:%02d:%02d", tm.tm_hour, tm.tm_min,
tm.tm_sec);
return sp_host_widget_text(h, name, out);
}
static int
clock_tool(const char *args, struct sp_buf *out, void *ctx)
{
char line[64];
time_t now = time(NULL);
struct tm tm;
(void)args;
(void)ctx;
localtime_r(&now, &tm);
snprintf(line, sizeof(line), "%04d-%02d-%02d %02d:%02d:%02d",
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min,
tm.tm_sec);
return sp_buf_puts(out, line);
}
static int
clock_init(struct sp_host *h)
{
static const struct sp_widget w = {
"clock", clock_tick, NULL, NULL, 1000
};
struct sp_tool t;
if (sp_host_add_widget(h, &w) != 0)
return -1;
memset(&t, 0, sizeof(t));
t.name = "clock";
t.description = "Report the local date and time.";
t.auth = SP_AUTH_READ;
t.run = clock_tool;
return sp_host_add_tool(h, &t);
}
static const struct sp_plugin clock_plugin = {
SP_PLUGIN_ABI,
"clock",
"1.0.0",
clock_init,
NULL,
NULL
};
const struct sp_plugin *
sp_plugin_describe(void)
{
return &clock_plugin;
}