Three things the screen and the goal were getting wrong. Colours. The theme had one accent for the user and grey for everything else. theme.h now carries agent, think, tool and notice colours. A CDE palette maps them to sets 7 (answer), 3 (reasoning), 6 (tool) and 8 (status); Xresources uses colour10/11/14/13 on dark and 2/3/6/5 on light. Role colours keep their hue and are pushed from the surface only when they would disappear; each role has a slightly different minimum, so even a monochrome palette yields distinguishable steps. The 256-colour mapping now takes the nearest cube/ramp entry instead of rounding each channel down, which turned a dark orange into grey 240. Usage. sp_usage_get() only counted requests inside a declared window. A provider without limit= therefore always read "0 calls" in the sidebar, although every call was in its log. Calls are now counted when no window is declared; test_usage covers it. Goal. /goal <text> set a variable and answered "goal set" without starting a turn. The engine now starts the recap prompt: the model writes a short recap, the focus plugin remembers that answer and injects it every seventh turn (SPLINTER_FOCUS_EVERY still overrides). /recap is gone as a command; its old behaviour is what /goal does now. A palette and a wiki page are not enough to make that visible: the prompt, the confirmation entry and the sidebar goal all show up in the chat. Tests: palette role separation and indexed roles in test_theme, four unlimited calls in test_usage, recap capture/injection and the missing /recap in test_focus, and a settle delay so the UI tests type after ui_read_line() owns the keyboard. make check: all tests passed.
467 lines
12 KiB
C
467 lines
12 KiB
C
/*
|
|
* splinter -- CDE palettes as terminal themes.
|
|
*
|
|
* Copyright (c) 2026 Konrad Lother
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
*/
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <strings.h>
|
|
|
|
#include "theme.h"
|
|
#include "palettes.h"
|
|
|
|
static int
|
|
luma(const unsigned char *c)
|
|
{
|
|
/* integer Rec.601 luma, so the ordering is deterministic and testable */
|
|
return 299 * c[0] + 587 * c[1] + 114 * c[2];
|
|
}
|
|
|
|
#define LIGHT 127500 /* luma above this: a light surface, black text */
|
|
|
|
/* CDE's rule for the text on a colour set: black on light, white on dark */
|
|
static void
|
|
cde_fg(const unsigned char bg[3], unsigned char fg[3])
|
|
{
|
|
unsigned char v = luma(bg) > LIGHT ? 0 : 255;
|
|
|
|
fg[0] = fg[1] = fg[2] = v;
|
|
}
|
|
|
|
static const struct sp_palette *
|
|
find_palette(const char *name)
|
|
{
|
|
size_t i;
|
|
|
|
for (i = 0; i < sp_palette_count; i++) {
|
|
if (strcasecmp(sp_palettes[i].name, name) == 0)
|
|
return &sp_palettes[i];
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
/* the roles of a CDE palette, by the colour set the desktop uses for each */
|
|
static void
|
|
load_cde(const struct sp_palette *p, struct sp_theme *out)
|
|
{
|
|
memset(out, 0, sizeof(*out));
|
|
out->name = p->name;
|
|
memcpy(out->bg, p->c[4], 3); /* set 5: main windows */
|
|
cde_fg(out->bg, out->fg);
|
|
memcpy(out->accent, p->c[0], 3); /* set 1: active frame, user */
|
|
memcpy(out->border, p->c[1], 3); /* set 2: inactive frame */
|
|
out->think[0] = p->c[2][0]; /* set 3: reasoning */
|
|
out->think[1] = p->c[2][1];
|
|
out->think[2] = p->c[2][2];
|
|
memcpy(out->input_bg, p->c[3], 3); /* set 4: text areas */
|
|
cde_fg(out->input_bg, out->input_fg);
|
|
memcpy(out->status, p->c[5], 3); /* set 6: dialogs, menus */
|
|
cde_fg(out->status, out->status_fg);
|
|
out->tool[0] = p->c[5][0]; /* set 6: tool calls */
|
|
out->tool[1] = p->c[5][1];
|
|
out->tool[2] = p->c[5][2];
|
|
out->agent[0] = p->c[6][0]; /* set 7: the model's answer */
|
|
out->agent[1] = p->c[6][1];
|
|
out->agent[2] = p->c[6][2];
|
|
out->notice[0] = p->c[7][0]; /* set 8: status and notes */
|
|
out->notice[1] = p->c[7][1];
|
|
out->notice[2] = p->c[7][2];
|
|
if (luma(out->bg) > LIGHT) {
|
|
out->error[0] = 0xa0; out->error[1] = 0x10; out->error[2] = 0x10;
|
|
} else {
|
|
out->error[0] = 0xff; out->error[1] = 0x70; out->error[2] = 0x70;
|
|
}
|
|
/*
|
|
* A frame colour as text on the window colour: legible, or pushed. The
|
|
* role colours keep their hue; only their distance from the surface is
|
|
* adjusted, so a palette's character survives.
|
|
*/
|
|
/*
|
|
* The role colours only move when the palette puts them too close to
|
|
* the surface. A high minimum turned a whole light palette into greys;
|
|
* these thresholds keep the hue and differ slightly so a monochrome
|
|
* palette still gets distinguishable steps.
|
|
*/
|
|
sp_color_separate(out->accent, out->bg, 30000);
|
|
sp_color_separate(out->border, out->bg, 50000);
|
|
sp_color_separate(out->agent, out->bg, 32000);
|
|
sp_color_separate(out->think, out->bg, 28000);
|
|
sp_color_separate(out->tool, out->bg, 34000);
|
|
sp_color_separate(out->notice, out->bg, 36000);
|
|
sp_color_separate(out->error, out->bg, 60000);
|
|
}
|
|
|
|
/* "#rgb", "#rrggbb", "#rrrrggggbbbb" or "rgb:r/g/b"; 0 when it is none */
|
|
static int
|
|
parse_color(const char *v, unsigned char c[3])
|
|
{
|
|
unsigned int x[3];
|
|
size_t n, i, w;
|
|
char part[8];
|
|
|
|
while (*v == ' ' || *v == '\t')
|
|
v++;
|
|
if (strncmp(v, "rgb:", 4) == 0) {
|
|
const char *q = v + 4;
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
n = strspn(q, "0123456789abcdefABCDEF");
|
|
if (n == 0 || n > 4)
|
|
return 0;
|
|
memcpy(part, q, n);
|
|
part[n] = '\0';
|
|
x[i] = (unsigned int)strtoul(part, NULL, 16);
|
|
/* scale 1..4 hex digits to 8 bits */
|
|
x[i] = (unsigned int)(x[i] * 255 / ((1u << (4 * n)) - 1));
|
|
q += n;
|
|
if (i < 2 && *q++ != '/')
|
|
return 0;
|
|
}
|
|
} else if (*v == '#') {
|
|
v++;
|
|
n = strspn(v, "0123456789abcdefABCDEF");
|
|
if (n != 3 && n != 6 && n != 12)
|
|
return 0;
|
|
w = n / 3;
|
|
for (i = 0; i < 3; i++) {
|
|
memcpy(part, v + i * w, w);
|
|
part[w] = '\0';
|
|
x[i] = (unsigned int)strtoul(part, NULL, 16);
|
|
x[i] = (unsigned int)(x[i] * 255 / ((1u << (4 * w)) - 1));
|
|
}
|
|
} else
|
|
return 0;
|
|
for (i = 0; i < 3; i++)
|
|
c[i] = (unsigned char)x[i];
|
|
return 1;
|
|
}
|
|
|
|
/*
|
|
* One resource line: "*background:\t#1d1f21". Only the generic names count
|
|
* ("*name" or "*.name"); "URxvt.background" belongs to one program.
|
|
*/
|
|
static int
|
|
xrdb_line(const char *line, char *name, size_t ncap, const char **val,
|
|
int *tight)
|
|
{
|
|
const char *colon = strchr(line, ':');
|
|
size_t n;
|
|
|
|
if (line[0] != '*' || colon == NULL)
|
|
return 0;
|
|
line++;
|
|
*tight = *line == '.';
|
|
if (*tight)
|
|
line++;
|
|
n = (size_t)(colon - line);
|
|
while (n > 0 && (line[n - 1] == ' ' || line[n - 1] == '\t'))
|
|
n--;
|
|
if (n == 0 || n >= ncap || memchr(line, '.', n) != NULL ||
|
|
memchr(line, '*', n) != NULL)
|
|
return 0;
|
|
memcpy(name, line, n);
|
|
name[n] = '\0';
|
|
*val = colon + 1;
|
|
return 1;
|
|
}
|
|
|
|
int
|
|
sp_theme_from_xrdb(const char *text, struct sp_theme *out)
|
|
{
|
|
unsigned char col[16][3], fg[3], bg[3];
|
|
int have[16], hfg = 0, hbg = 0, any = 0, i, dark, tight;
|
|
/*
|
|
* "*.background" beats "*background" (a tight binding is the closer
|
|
* match), whatever order the lines come in -- xrdb -query sorts them.
|
|
* have/hfg/hbg: 0 none, 1 from a loose line, 2 from a tight one.
|
|
*/
|
|
const char *p = text;
|
|
|
|
memset(out, 0, sizeof(*out));
|
|
memset(have, 0, sizeof(have));
|
|
out->name = SP_THEME_XRESOURCES;
|
|
while (p != NULL && *p != '\0') {
|
|
const char *nl = strchr(p, '\n'), *val;
|
|
char line[512], name[64];
|
|
size_t n = nl != NULL ? (size_t)(nl - p) : strlen(p);
|
|
|
|
if (n >= sizeof(line))
|
|
n = sizeof(line) - 1;
|
|
memcpy(line, p, n);
|
|
line[n] = '\0';
|
|
p = nl != NULL ? nl + 1 : NULL;
|
|
if (!xrdb_line(line, name, sizeof(name), &val, &tight))
|
|
continue;
|
|
if (strcasecmp(name, "foreground") == 0) {
|
|
if (hfg < 1 + tight && parse_color(val, fg))
|
|
hfg = 1 + tight;
|
|
} else if (strcasecmp(name, "background") == 0) {
|
|
if (hbg < 1 + tight && parse_color(val, bg))
|
|
hbg = 1 + tight;
|
|
} else if (strncasecmp(name, "color", 5) == 0 &&
|
|
name[5] >= '0' && name[5] <= '9') {
|
|
int k = atoi(name + 5);
|
|
unsigned char c[3];
|
|
|
|
if (k >= 0 && k < 16 && have[k] < 1 + tight &&
|
|
parse_color(val, c)) {
|
|
memcpy(col[k], c, 3);
|
|
have[k] = 1 + tight;
|
|
any = 1;
|
|
}
|
|
}
|
|
if (hfg || hbg)
|
|
any = 1;
|
|
}
|
|
if (!any) {
|
|
/* the terminal's own colours, by index: its palette shows */
|
|
out->indexed = 1;
|
|
out->idx_fg = 7;
|
|
out->idx_bg = 0;
|
|
out->idx_border = 8;
|
|
out->idx_accent = 12;
|
|
out->idx_agent = 10;
|
|
out->idx_think = 11;
|
|
out->idx_tool = 14;
|
|
out->idx_notice = 13;
|
|
out->idx_error = 9;
|
|
return 0;
|
|
}
|
|
if (!hbg) {
|
|
if (have[0])
|
|
memcpy(bg, col[0], 3);
|
|
else
|
|
memset(bg, 0, 3);
|
|
}
|
|
if (!hfg) {
|
|
if (have[7])
|
|
memcpy(fg, col[7], 3);
|
|
else
|
|
memset(fg, 0xc0, 3);
|
|
}
|
|
dark = luma(bg) <= LIGHT;
|
|
memcpy(out->bg, bg, 3);
|
|
memcpy(out->fg, fg, 3);
|
|
memcpy(out->input_bg, bg, 3);
|
|
memcpy(out->input_fg, fg, 3);
|
|
/* the bright blue on a dark background, the plain one on a light one */
|
|
i = dark ? (have[12] ? 12 : 4) : (have[4] ? 4 : 12);
|
|
if (have[i])
|
|
memcpy(out->accent, col[i], 3);
|
|
else
|
|
memcpy(out->accent, fg, 3);
|
|
i = have[8] ? 8 : 7;
|
|
if (have[i])
|
|
memcpy(out->border, col[i], 3);
|
|
else
|
|
memcpy(out->border, fg, 3);
|
|
i = dark ? (have[9] ? 9 : 1) : (have[1] ? 1 : 9);
|
|
if (have[i])
|
|
memcpy(out->error, col[i], 3);
|
|
else {
|
|
out->error[0] = 0xff; out->error[1] = 0x50; out->error[2] = 0x50;
|
|
}
|
|
/* the role colours: bright on dark, plain on light, always the same role */
|
|
i = dark ? (have[10] ? 10 : 2) : (have[2] ? 2 : 10);
|
|
if (have[i])
|
|
memcpy(out->agent, col[i], 3);
|
|
else
|
|
memcpy(out->agent, out->accent, 3);
|
|
i = dark ? (have[11] ? 11 : 3) : (have[3] ? 3 : 11);
|
|
if (have[i])
|
|
memcpy(out->think, col[i], 3);
|
|
else
|
|
memcpy(out->think, out->accent, 3);
|
|
i = dark ? (have[14] ? 14 : 6) : (have[6] ? 6 : 14);
|
|
if (have[i])
|
|
memcpy(out->tool, col[i], 3);
|
|
else
|
|
memcpy(out->tool, out->accent, 3);
|
|
i = dark ? (have[13] ? 13 : 5) : (have[5] ? 5 : 13);
|
|
if (have[i])
|
|
memcpy(out->notice, col[i], 3);
|
|
else
|
|
memcpy(out->notice, out->accent, 3);
|
|
i = dark ? (have[8] ? 8 : 0) : (have[7] ? 7 : 15);
|
|
if (have[i])
|
|
memcpy(out->status, col[i], 3);
|
|
else
|
|
memcpy(out->status, fg, 3);
|
|
memcpy(out->status_fg, fg, 3);
|
|
sp_color_separate(out->status_fg, out->status, 90000);
|
|
sp_color_separate(out->accent, out->bg, 30000);
|
|
sp_color_separate(out->border, out->bg, 40000);
|
|
sp_color_separate(out->agent, out->bg, 32000);
|
|
sp_color_separate(out->think, out->bg, 28000);
|
|
sp_color_separate(out->tool, out->bg, 34000);
|
|
sp_color_separate(out->notice, out->bg, 36000);
|
|
sp_color_separate(out->error, out->bg, 50000);
|
|
return 0;
|
|
}
|
|
|
|
/* the resource text: the X server's database, or the file it is loaded from */
|
|
static char *
|
|
xrdb_text(void)
|
|
{
|
|
struct sp_buf b;
|
|
char chunk[4096], path[700];
|
|
const char *home = getenv("HOME");
|
|
FILE *f = NULL;
|
|
size_t n;
|
|
int k;
|
|
|
|
if (sp_buf_init(&b) != 0)
|
|
return NULL;
|
|
if (getenv("DISPLAY") != NULL && *getenv("DISPLAY") != '\0') {
|
|
f = popen("xrdb -query 2>/dev/null </dev/null", "r");
|
|
if (f != NULL) {
|
|
while ((n = fread(chunk, 1, sizeof(chunk), f)) > 0)
|
|
(void)sp_buf_add(&b, chunk, n);
|
|
(void)pclose(f);
|
|
}
|
|
}
|
|
for (k = 0; b.len == 0 && home != NULL && k < 2; k++) {
|
|
snprintf(path, sizeof(path), "%s/%s", home,
|
|
k == 0 ? ".Xresources" : ".Xdefaults");
|
|
if ((f = fopen(path, "r")) == NULL)
|
|
continue;
|
|
while ((n = fread(chunk, 1, sizeof(chunk), f)) > 0)
|
|
(void)sp_buf_add(&b, chunk, n);
|
|
fclose(f);
|
|
}
|
|
if (b.len == 0) {
|
|
sp_buf_free(&b);
|
|
return NULL;
|
|
}
|
|
return b.p; /* the caller frees it */
|
|
}
|
|
|
|
int
|
|
sp_theme_load(const char *name, struct sp_theme *out, struct sp_buf *err)
|
|
{
|
|
const struct sp_palette *p;
|
|
|
|
if (name == NULL || *name == '\0')
|
|
name = SP_THEME_DEFAULT;
|
|
if (strcasecmp(name, SP_THEME_XRESOURCES) == 0) {
|
|
char *text = xrdb_text();
|
|
int rc = sp_theme_from_xrdb(text, out);
|
|
|
|
free(text);
|
|
return rc;
|
|
}
|
|
p = find_palette(name);
|
|
if (p == NULL) {
|
|
sp_buf_printf(err, "theme: unknown theme '%s' (Xresources or "
|
|
"one of %zu CDE palettes; --list-themes)", name,
|
|
sp_palette_count);
|
|
return -1;
|
|
}
|
|
load_cde(p, out);
|
|
return 0;
|
|
}
|
|
|
|
size_t
|
|
sp_theme_list(const char ***names, size_t *count)
|
|
{
|
|
static const char *list[257];
|
|
size_t i, n = 0;
|
|
|
|
list[n++] = SP_THEME_XRESOURCES;
|
|
for (i = 0; i < sp_palette_count && n < 257; i++)
|
|
list[n++] = sp_palettes[i].name;
|
|
*names = list;
|
|
*count = n;
|
|
return n;
|
|
}
|
|
|
|
void
|
|
sp_color_darken(unsigned char c[3], int target)
|
|
{
|
|
int guard = 0;
|
|
|
|
while (luma(c) > target && guard++ < 40) {
|
|
int i;
|
|
|
|
for (i = 0; i < 3; i++)
|
|
c[i] = (unsigned char)(c[i] * 3 / 4);
|
|
}
|
|
}
|
|
|
|
void
|
|
sp_color_separate(unsigned char c[3], const unsigned char ref[3], int min_delta)
|
|
{
|
|
int guard = 0;
|
|
int up = luma(c) >= luma(ref);
|
|
|
|
while (abs(luma(c) - luma(ref)) < min_delta && guard++ < 64) {
|
|
int i;
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
if (up)
|
|
c[i] = (unsigned char)(c[i] + (255 - c[i]) / 4 + 1);
|
|
else
|
|
c[i] = (unsigned char)(c[i] * 3 / 4);
|
|
}
|
|
}
|
|
}
|
|
|
|
short
|
|
sp_color_rgb(unsigned char r, unsigned char g, unsigned char b, short colors)
|
|
{
|
|
static const unsigned char base[16][3] = {
|
|
{ 0, 0, 0}, {128, 0, 0}, { 0, 128, 0}, {128, 128, 0},
|
|
{ 0, 0, 128}, {128, 0, 128}, { 0, 128, 128}, {192, 192, 192},
|
|
{128, 128, 128}, {255, 0, 0}, { 0, 255, 0}, {255, 255, 0},
|
|
{ 0, 0, 255}, {255, 0, 255}, { 0, 255, 255}, {255, 255, 255}
|
|
};
|
|
static const int level[6] = { 0, 95, 135, 175, 215, 255 };
|
|
int i, best = 0, bestd = 1 << 30;
|
|
|
|
if (colors >= 256) {
|
|
/*
|
|
* Nearest of the 240 cube/ramp colours, not the cube corner the
|
|
* channels happen to share. Rounding each channel down made a
|
|
* dark orange a grey (240 instead of a brown cube entry) and the
|
|
* palette colours lost their hue on the screen.
|
|
*/
|
|
best = 16;
|
|
bestd = 1 << 30;
|
|
for (i = 16; i < 256; i++) {
|
|
int cr, cg, cb, dr, dg, db, d;
|
|
|
|
if (i < 232) {
|
|
int k = i - 16;
|
|
|
|
cr = level[k / 36];
|
|
cg = level[(k / 6) % 6];
|
|
cb = level[k % 6];
|
|
} else {
|
|
cr = cg = cb = 8 + (i - 232) * 10;
|
|
}
|
|
dr = r - cr;
|
|
dg = g - cg;
|
|
db = b - cb;
|
|
d = dr * dr + dg * dg + db * db;
|
|
if (d < bestd) {
|
|
bestd = d;
|
|
best = i;
|
|
}
|
|
}
|
|
return (short)best;
|
|
}
|
|
|
|
for (i = 0; i < (colors >= 16 ? 16 : 8); i++) {
|
|
int dr = r - base[i][0], dg = g - base[i][1], db = b - base[i][2];
|
|
int d = dr * dr + dg * dg + db * db;
|
|
|
|
if (d < bestd) {
|
|
bestd = d;
|
|
best = i;
|
|
}
|
|
}
|
|
return (short)best;
|
|
}
|