aboutsummaryrefslogtreecommitdiffstats
path: root/src/config.c
diff options
context:
space:
mode:
authoruser <user@clank>2026-08-14 22:06:57 +0200
committeruser <user@clank>2026-08-14 22:06:57 +0200
commit2ae507a8a331799875e07c327730a1a4ca0363f4 (patch)
tree685b167c2a87f5e9f877cca31d31f43fd4e71734 /src/config.c
parentsl0ppty: expand ~ in a path that a config file wrote (diff)
downloadslosh-2ae507a8a331799875e07c327730a1a4ca0363f4.tar.gz
slosh-2ae507a8a331799875e07c327730a1a4ca0363f4.tar.xz
slosh-2ae507a8a331799875e07c327730a1a4ca0363f4.zip
sl0ppty: shaders can be loaded from shared libraries
Adding an effect meant editing shader.c and rebuilding, which is fine for us and useless to anyone else. Now any `*.so` in the shader directory that exports a plugin table is loaded at startup, and what it declares is named in the config exactly like a built-in -- same syntax, same three parameters, same pass. Default location is a `shaders` directory beside the config file, so dropping the .so next to config.kdl is the whole installation. The interesting part is what a plugin can see, which is one header. shader_abi.h holds cell_t, color_t, shade_ctx_t, shader_t and nothing else: no libghostty-vt, no pty, no layout, no session. A shader is therefore unable to reach anything but the cell it was handed, not by rule but because nothing else is declared where it can find it. cell_t and color_t moved there from sl0ppty.h for the same reason -- that is the one place they are a contract with code compiled outside this tree rather than an internal type. Loading is deliberately narrow: - refused unless the library reports our ABI version *and* matching struct sizes. The version says which contract; the sizes catch someone editing their copy of the header, which a version number alone calls fine and a running session calls garbled colours; - refused if it redefines an existing name, because a config naming `dim` means the `dim` it was written against; - all-or-nothing per file, and per file: a bad library is a line on stderr and everything else still loads (D9 fail-open); - nothing is ever unloaded. A shade_fn may be sitting in a live config or on a pane, so dlclose() is a jump into an unmapped page. A reload picks up newly *added* plugins; replacing one needs a new session. This is the single exception to D9's "a plugin is a subprocess", and it is argued in D15 rather than drifted into: a shader is called per cell, per pass, per frame, which no subprocess can be in the path of, and the measured alternative -- interpreting bytecode per cell -- is 56ns against 8ns for the compiled function. On trust, `shell` and a layout's `command=` already let a file in the config directory run anything as you, so a .so beside them is not a new boundary. The docs say so plainly rather than implying a sandbox. contrib/shader-plugin is a working skeleton: Makefile, two commented effects (one positional, one animated), and the three rules a shader has to keep. It is built and loaded by the test suite, so a broken example is a failing test rather than someone else's afternoon.
Diffstat (limited to 'src/config.c')
-rw-r--r--src/config.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/config.c b/src/config.c
index 6b10d36..2b80a18 100644
--- a/src/config.c
+++ b/src/config.c
@@ -322,6 +322,7 @@ void config_defaults(config_t *c) {
void config_free(config_t *c) {
free(c->binds);
free(c->shell);
+ free(c->shader_dir);
memset(c, 0, sizeof *c);
}
@@ -433,6 +434,35 @@ bool config_load(config_t *c, const char *path, char *err, size_t errcap) {
c->shell = strdup(sh);
}
+ /* Shader plugins, before any shader is named below: a `shaders` block may
+ * use what one of these adds, and a name is looked up as it is parsed. The
+ * default is a directory beside this file, so dropping a `.so` next to the
+ * config is the whole installation procedure. */
+ const char *sdir = kdl_arg(kdl_child(root, "shader_dir"), 0, NULL);
+ if (sdir) {
+ free(c->shader_dir);
+ c->shader_dir = strdup(sdir);
+ }
+ {
+ char dir[1024];
+ if (c->shader_dir) {
+ char buf[1024];
+ snprintf(dir, sizeof dir, "%s",
+ path_expand(c->shader_dir, buf, sizeof buf));
+ } else {
+ snprintf(dir, sizeof dir, "%s", path);
+ char *slash = strrchr(dir, '/');
+ if (slash) slash[1] = 0;
+ else dir[0] = 0;
+ snprintf(dir + strlen(dir), sizeof dir - strlen(dir), "shaders");
+ }
+ char lerr[256] = {0};
+ shader_load_dir(dir, lerr, sizeof lerr);
+ /* A plugin that will not load is worth a line, and worth nothing more:
+ * the config it came with still works, minus that effect (D9). */
+ if (lerr[0] && err && !err[0]) snprintf(err, errcap, "%s", lerr);
+ }
+
/* One node per pass, in the order written, because a chain is a sequence.
* Shared by `shaders { }` and by every state, so a state's chain is exactly
* as expressive as the global one and there is one parser to be wrong. */