diff options
| author | user <user@clank> | 2026-08-14 14:13:33 +0200 |
|---|---|---|
| committer | user <user@clank> | 2026-08-14 14:13:33 +0200 |
| commit | 6e2786c0ca394a77fe9db22362d1932728c24012 (patch) | |
| tree | e91568a5f9c03fea841e1a7df14c13dbf18517c6 /src | |
| parent | sl0ppty: dashed borders and a deeper push-back while dragging (diff) | |
| download | slosh-6e2786c0ca394a77fe9db22362d1932728c24012.tar.gz slosh-6e2786c0ca394a77fe9db22362d1932728c24012.tar.xz slosh-6e2786c0ca394a77fe9db22362d1932728c24012.zip | |
sl0ppty: positional shaders, and a config block to ask for them
Six passes that are pure functions of where a cell is: vignette, gradient,
zebra, ruler, margin, spotlight. The position was already in the context and
nothing had used it; these are what it was for.
The one worth having on its own merits is `ruler`. A column guide that costs
no columns, needs no support from the program in the pane, and works in
anything — an editor that has never heard of one, a pager, a shell. It marks
the background only: a guide that recoloured the text would be making the code
it marks harder to read, which is the opposite of the job. `margin` is the
same idea from the other side, everything past a column receding so an
over-long line visibly runs out of the pane you meant to use.
`spotlight` is the first thing to need something new in the context. The
cursor belongs to whichever pane is focused, so it is handed only to that
pane — otherwise every other pane's light would chase a cursor somewhere else.
It leaves everything inside its radius completely alone, because dimming the
text you are actually working on to make it easier to find would be silly, and
ramps rather than stepping at the edge.
`vignette` and `spotlight` both measure distance with a row counting double,
because a cell is about twice as tall as it is wide and an effect that claims
to be round has to say so in cells. The layout's own centre-distance already
does this. The test for it is that on a 41x21 pane — square on screen, not in
cells — the top edge and the left edge come out equally dark.
Scanlines are not a seventh shader. A CRT scanline is `zebra` with a band of
one row, and shipping the same arithmetic under two names would be pretending
they are different things.
These are not policy, so they needed a way to be asked for, and a KDL block
whose children are the passes gets it without inventing a syntax: order in the
block is order in the chain, because a chain is a sequence, and listing a
shader twice is how you get two rulers. Unknown names are refused with a
warning rather than guessed at. Empty by default — these are yours to want,
not opinions we hold.
Diffstat (limited to 'src')
| -rw-r--r-- | src/app.c | 26 | ||||
| -rw-r--r-- | src/config.c | 38 | ||||
| -rw-r--r-- | src/config.h | 7 | ||||
| -rw-r--r-- | src/shader.c | 94 | ||||
| -rw-r--r-- | src/shader.h | 24 |
5 files changed, 181 insertions, 8 deletions
@@ -1499,22 +1499,36 @@ static size_t policy_shaders(app_t *a, node_t *n, shader_t *out, size_t cap) { /* A pane with nothing to apply costs nothing: no context is built and no cell * is visited, so an unshaded session emits the same bytes it did before. */ static void shade_leaf(app_t *a, screen_t *s, node_t *n) { - shader_t chain[SHADE_MAX + 2]; + shader_t chain[SHADE_CHAIN_MAX]; size_t nc = 0; - /* Attached first, policy last: a pane's own colour is the thing the - * session's opinion of the moment is then applied to. */ - for (size_t i = 0; i < n->nshaders && nc < SHADE_MAX; i++) + /* Configured first, then this pane's own, then policy last: what you asked + * every pane to look like, adjusted for this pane, and only then the + * session's opinion about this moment — which has to be able to grey out + * whatever the other two produced. */ + for (size_t i = 0; i < CFG.nshaders && nc < SHADE_CHAIN_MAX; i++) + chain[nc++] = CFG.shaders[i]; + for (size_t i = 0; i < n->nshaders && nc < SHADE_CHAIN_MAX; i++) chain[nc++] = n->shaders[i]; - nc += policy_shaders(a, n, &chain[nc], (sizeof chain / sizeof *chain) - nc); + nc += policy_shaders(a, n, &chain[nc], SHADE_CHAIN_MAX - nc); if (!nc) return; + bool focused = n == cur(a)->focus; shade_ctx_t base = { .now_ms = now_ms_(), - .focused = n == cur(a)->focus, + .focused = focused, .default_fg = CFG.default_fg, .default_bg = CFG.default_bg, }; + /* The screen's cursor belongs to whichever pane is focused, so it is only + * this pane's cursor when this pane is the focused one. Handing it to any + * other pane would have its spotlight chasing a cursor somewhere else. */ + if (focused && s->cursor_visible && s->cursor_x >= n->content.x && + s->cursor_y >= n->content.y) { + base.has_cursor = true; + base.cursor_x = (uint16_t)(s->cursor_x - n->content.x); + base.cursor_y = (uint16_t)(s->cursor_y - n->content.y); + } shade_apply(s, chain, nc, n->content.x, n->content.y, n->content.w, n->content.h, &base); } diff --git a/src/config.c b/src/config.c index 6c1650c..92ee560 100644 --- a/src/config.c +++ b/src/config.c @@ -270,6 +270,44 @@ bool config_load(config_t *c, const char *path, char *err, size_t errcap) { c->shell = strdup(sh); } + /* `shaders { vignette amount=60; ruler amount=70 at=80 }` — one node per + * pass, and the order they are written in is the order they run, because a + * chain is a sequence. A block that names nothing known leaves the previous + * set alone rather than silently clearing it. */ + const kdl_node_t *shaders = kdl_child(root, "shaders"); + if (shaders) { + c->nshaders = 0; + for (size_t i = 0; i < shaders->nkids && c->nshaders < SHADE_MAX; i++) { + const kdl_node_t *k = shaders->kids[i]; + if (!k || !k->name) continue; + + long amount = kdl_prop_int(k, "amount", 128); + if (amount < 0) amount = 0; + if (amount > 255) amount = 255; + /* Every shader that takes a number calls it something different, so + * accept each name rather than making you remember which is which. */ + long param = kdl_prop_int(k, "at", -1); + if (param < 0) param = kdl_prop_int(k, "radius", -1); + if (param < 0) param = kdl_prop_int(k, "band", -1); + if (param < 0) param = kdl_prop_int(k, "direction", -1); + if (param < 0) param = 0; + if (param > 65535) param = 65535; + + color_t col = c->frame_focus; /* a sensible default for `ruler` */ + const char *cs = kdl_prop(k, "color", NULL); + if (cs && !parse_color(cs, &col) && err && !err[0]) + snprintf(err, errcap, "bad colour for shader %s: %s", k->name, cs); + + if (!shader_make_p(&c->shaders[c->nshaders], k->name, col, + (uint8_t)amount, (uint16_t)param)) { + if (err && !err[0]) + snprintf(err, errcap, "unknown shader: %s", k->name); + continue; + } + c->nshaders++; + } + } + const kdl_node_t *theme = kdl_child(root, "theme"); if (theme) { struct { diff --git a/src/config.h b/src/config.h index eed50a6..a6bd2f2 100644 --- a/src/config.h +++ b/src/config.h @@ -11,6 +11,7 @@ #include <stddef.h> #include <stdint.h> +#include "shader.h" #include "sl0ppty.h" typedef enum { @@ -66,6 +67,12 @@ typedef struct { uint8_t dim_unfocused; uint8_t drag_grayscale; uint8_t drag_dim; + + /* Colour passes every pane gets, in the order they were written. Ordinary + * shaders rather than policy: the session has no opinion about these, you + * asked for them. */ + shader_t shaders[SHADE_MAX]; + size_t nshaders; bool status_bar; bool focus_follows_mouse; diff --git a/src/shader.c b/src/shader.c index ec49a07..6085eb4 100644 --- a/src/shader.c +++ b/src/shader.c @@ -44,6 +44,87 @@ static void sh_tint(const shader_t *sh, const shade_ctx_t *ctx, cell_t *c) { c->bg = mix(c->bg, sh->color, sh->amount); } +/* ---- positional --------------------------------------------------------- */ + +/* A terminal cell is about twice as tall as it is wide, so any effect that + * claims to be round has to say so in cells: a row counts double, exactly as + * the layout's own centre-distance does. */ +static int dist2(int dx, int dy) { return dx * dx + (dy * 2) * (dy * 2); } + +static void dim_by(cell_t *c, uint8_t a) { + const color_t black = {true, 0, 0, 0}; + c->fg = mix(c->fg, black, a); + c->bg = mix(c->bg, black, a); +} + +/* Darken towards the edges. Falloff is on squared distance, which is both + * cheaper than a square root and the curve a vignette wants anyway: flat + * across the middle, then gathering pace. */ +static void sh_vignette(const shader_t *sh, const shade_ctx_t *ctx, cell_t *c) { + int cx = ctx->cols / 2, cy = ctx->rows / 2; + int far = dist2(cx, cy); + if (far <= 0) return; + int d = dist2((int)ctx->x - cx, (int)ctx->y - cy); + if (d > far) d = far; + dim_by(c, (uint8_t)((int)sh->amount * d / far)); +} + +/* Fade across the pane towards the background, so a pane reads as a surface + * with a light on it rather than a rectangle of text. */ +static void sh_gradient(const shader_t *sh, const shade_ctx_t *ctx, cell_t *c) { + int span, pos; + switch (sh->param) { + case 1: span = ctx->rows; pos = ctx->rows - 1 - ctx->y; break; + case 2: span = ctx->cols; pos = ctx->x; break; + case 3: span = ctx->cols; pos = ctx->cols - 1 - ctx->x; break; + default: span = ctx->rows; pos = ctx->y; break; + } + if (span <= 1) return; + uint8_t a = (uint8_t)((int)sh->amount * pos / (span - 1)); + c->fg = mix(c->fg, ctx->default_bg, a); + c->bg = mix(c->bg, ctx->default_bg, a); +} + +/* Horizontal banding. `param` rows per band, every other band darkened — + * which at one row per band is also what a CRT scanline is, so there is one + * shader here and not two. */ +static void sh_zebra(const shader_t *sh, const shade_ctx_t *ctx, cell_t *c) { + uint16_t band = sh->param ? sh->param : 1; + if (((ctx->y / band) & 1) == 0) return; + dim_by(c, sh->amount); +} + +/* A column guide. Background only: a ruler that recoloured the text would be + * making the code it marks harder to read, which is the opposite of the job. */ +static void sh_ruler(const shader_t *sh, const shade_ctx_t *ctx, cell_t *c) { + if (ctx->x != sh->param) return; + c->bg = mix(c->bg, sh->color, sh->amount); +} + +/* Everything past a column recedes, so an over-long line visibly runs out of + * the part of the pane you meant to use. */ +static void sh_margin(const shader_t *sh, const shade_ctx_t *ctx, cell_t *c) { + if (ctx->x < sh->param) return; + dim_by(c, sh->amount); +} + +/* Brightness falls away from the cursor. Inside the radius nothing happens at + * all, so the text you are working on is never touched; beyond it the dimming + * ramps up over the same distance again rather than switching on at an edge. */ +static void sh_spotlight(const shader_t *sh, const shade_ctx_t *ctx, + cell_t *c) { + if (!ctx->has_cursor) return; + int r = sh->param ? sh->param : 10; + int inner = r * r; + int d = dist2((int)ctx->x - (int)ctx->cursor_x, + (int)ctx->y - (int)ctx->cursor_y); + if (d <= inner) return; + int ramp = inner * 3; + int over = d - inner; + int k = over >= ramp ? 255 : over * 255 / ramp; + dim_by(c, (uint8_t)((int)sh->amount * k / 255)); +} + static const struct { const char *name; shade_fn fn; @@ -51,14 +132,25 @@ static const struct { {"dim", sh_dim}, {"grayscale", sh_grayscale}, {"tint", sh_tint}, + {"vignette", sh_vignette}, + {"gradient", sh_gradient}, + {"zebra", sh_zebra}, + {"ruler", sh_ruler}, + {"margin", sh_margin}, + {"spotlight", sh_spotlight}, }; bool shader_make(shader_t *out, const char *kind, color_t color, uint8_t amount) { + return shader_make_p(out, kind, color, amount, 0); +} + +bool shader_make_p(shader_t *out, const char *kind, color_t color, + uint8_t amount, uint16_t param) { if (!out || !kind) return false; for (size_t i = 0; i < sizeof REGISTRY / sizeof *REGISTRY; i++) { if (strcmp(REGISTRY[i].name, kind) != 0) continue; - *out = (shader_t){REGISTRY[i].name, REGISTRY[i].fn, color, amount}; + *out = (shader_t){REGISTRY[i].name, REGISTRY[i].fn, color, amount, param}; return true; } return false; diff --git a/src/shader.h b/src/shader.h index 74fa2d7..74518fc 100644 --- a/src/shader.h +++ b/src/shader.h @@ -28,8 +28,13 @@ #include "sl0ppty.h" -/* Per pane. Small and fixed: a stack this deep is already a lot of passes. */ +/* Per pane, and separately per config. Small and fixed: a stack this deep is + * already a lot of passes over the same cells. */ #define SHADE_MAX 4 +/* What one pane can end up running: the ones every pane gets from the config, + * the ones attached to this pane, and the one or two the session derived for + * this frame. */ +#define SHADE_CHAIN_MAX (SHADE_MAX * 2 + 2) typedef struct { uint16_t x, y; /* cell position within the content rect, 0-based */ @@ -37,6 +42,12 @@ typedef struct { int64_t now_ms; /* for anything animated */ bool focused; + /* Where the cursor is, in the same rect-relative space as x/y. Only ever + * set for the pane that owns it, so an effect that follows the cursor does + * not chase another pane's. */ + bool has_cursor; + uint16_t cursor_x, cursor_y; + /* What "terminal default" means while shading. A cell whose colour is unset * is drawn in whatever the client's terminal calls default, and we never * learn that RGB — but most terminal text is default-coloured, so a shader @@ -54,11 +65,22 @@ struct shader { shade_fn fn; color_t color; /* the target colour, for shaders that have one */ uint8_t amount; /* strength, 0..255; 0 is identity, 255 is fully applied */ + /* One number whose meaning is the shader's own, because a second parameter + * that is a column for one effect and a radius for another is not really + * two things: + * ruler the column to mark margin first column to dim + * zebra rows per band spotlight radius in columns + * gradient 0 down, 1 up, 2 right, 3 left + */ + uint16_t param; }; /* Build a shader by registry name. False if the name is not a built-in. */ bool shader_make(shader_t *out, const char *kind, color_t color, uint8_t amount); +/* The same, for the shaders that take a number of their own. */ +bool shader_make_p(shader_t *out, const char *kind, color_t color, + uint8_t amount, uint16_t param); /* Iterate the registry: the i'th name, or NULL past the end. */ const char *shader_kind(size_t i); |
