/* * 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 #include #include #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.params = "{\"type\":\"object\",\"properties\":{}}"; 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; }