summaryrefslogtreecommitdiff
path: root/src/snapshot.c
diff options
context:
space:
mode:
authorBerke Güzel <wenekar1@gmail.com>2026-10-15 13:04:01 +0300
committerBerke Güzel <wenekar1@gmail.com>2026-10-15 13:04:01 +0300
commitdcebf9c7f954a55350b68d0ead1935612c4e2e05 (patch)
tree3eb3e9f6154c6760bcb89a565f8ddc8f8b66cb90 /src/snapshot.c
parentbd8222e615134c3ec5e7a07b6b1fa97c9bd4a471 (diff)
Rename the library to wsap_vtcore
ncurses installs <term.h> and defines struct term. In a file that includes both headers, an ncurses TERMINAL * and this library's terminal are silently the same type. The name wsap_vtcore avoids the clash. The public header is now <wsap_vtcore/wsap_vtcore.h>. Every global symbol uses the wsap_vtcore_ prefix, every macro uses WSAP_VTCORE_, and the shared library is libwsap_vtcore.so.0. The default terminal name is now "wsap_vtcore". The ISA override variable is WSAP_VTCORE_ISA. No other behavior changes. The amalgamation writes wsap_vtcore.c and wsap_vtcore.h. Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
Diffstat (limited to 'src/snapshot.c')
-rw-r--r--src/snapshot.c106
1 files changed, 53 insertions, 53 deletions
diff --git a/src/snapshot.c b/src/snapshot.c
index 6f396e2..f7e906c 100644
--- a/src/snapshot.c
+++ b/src/snapshot.c
@@ -10,16 +10,16 @@
* Cluster cells refer to an arena of [length, code points...] records that
* the snapshot owns, so a renderer never reads the terminal's cluster table.
*/
-struct term_snapshot {
- struct term_view view;
- uint32_t palette[TERM_PALETTE_SIZE];
+struct wsap_vtcore_snapshot {
+ struct wsap_vtcore_view view;
+ uint32_t palette[WSAP_VTCORE_PALETTE_SIZE];
uint64_t source; /* serial of the terminal it copied */
- struct term_cell *cells; /* rows * cols */
- struct term_cell *stage; /* rows * cols: changed rows, before apply() */
+ struct wsap_vtcore_cell *cells; /* rows * cols */
+ struct wsap_vtcore_cell *stage; /* rows * cols: changed rows, before apply() */
uint64_t *keys; /* per row */
uint32_t *row_clusters; /* arena records each row holds */
- struct term_span *selection; /* per row */
+ struct wsap_vtcore_span *selection; /* per row */
size_t cell_cap;
int row_cap;
@@ -30,15 +30,15 @@ struct term_snapshot {
uint32_t spare_cap;
};
-struct term_snapshot *term_snapshot_new(void)
+struct wsap_vtcore_snapshot *wsap_vtcore_snapshot_new(void)
{
- struct term_snapshot *s = calloc(1, sizeof(*s));
+ struct wsap_vtcore_snapshot *s = calloc(1, sizeof(*s));
if (s)
s->view.palette = s->palette;
return s;
}
-void term_snapshot_free(struct term_snapshot *s)
+void wsap_vtcore_snapshot_free(struct wsap_vtcore_snapshot *s)
{
if (!s)
return;
@@ -54,15 +54,15 @@ void term_snapshot_free(struct term_snapshot *s)
/* Make room for cols x rows. A failed call keeps the old contents, so the
* snapshot stays readable. */
-static bool reserve(struct term_snapshot *s, int cols, int rows)
+static bool reserve(struct wsap_vtcore_snapshot *s, int cols, int rows)
{
size_t cells = (size_t)cols * (size_t)rows;
if (cells > s->cell_cap) {
- struct term_cell *c = realloc(s->cells, cells * sizeof(*c));
+ struct wsap_vtcore_cell *c = realloc(s->cells, cells * sizeof(*c));
if (!c)
return false;
s->cells = c;
- struct term_cell *st = realloc(s->stage, cells * sizeof(*st));
+ struct wsap_vtcore_cell *st = realloc(s->stage, cells * sizeof(*st));
if (!st)
return false;
s->stage = st;
@@ -75,7 +75,7 @@ static bool reserve(struct term_snapshot *s, int cols, int rows)
uint32_t *rc = realloc(s->row_clusters, (size_t)rows * sizeof(*rc));
if (rc)
s->row_clusters = rc;
- struct term_span *sel = realloc(s->selection, (size_t)rows * sizeof(*sel));
+ struct wsap_vtcore_span *sel = realloc(s->selection, (size_t)rows * sizeof(*sel));
if (sel)
s->selection = sel;
if (!k || !rc || !sel)
@@ -95,14 +95,14 @@ struct arena_out {
};
/* Make room for need words. False when memory runs out or an offset would not
- * fit below TERM_TEXT_CLUSTER. */
+ * fit below WSAP_VTCORE_TEXT_CLUSTER. */
static bool arena_reserve(struct arena_out *a, uint64_t need)
{
- if (need > TERM_TEXT_CLUSTER)
+ if (need > WSAP_VTCORE_TEXT_CLUSTER)
return false;
if (need <= *a->cap)
return true;
- uint32_t cap = term_grow_cap32(*a->cap ? *a->cap * 2 : 256, (uint32_t)need);
+ uint32_t cap = wsap_vtcore_grow_cap32(*a->cap ? *a->cap * 2 : 256, (uint32_t)need);
uint32_t *grown = realloc(*a->buf, cap * sizeof(*grown));
if (!grown)
return false;
@@ -116,8 +116,8 @@ static bool arena_reserve(struct arena_out *a, uint64_t need)
static bool arena_add(struct arena_out *a, const struct gstore *g, uint32_t handle,
uint32_t *text)
{
- uint32_t cps[TERM_GRAPHEME_MAX_LEN];
- int n = term_gstore_expand(g, handle, cps, TERM_GRAPHEME_MAX_LEN);
+ uint32_t cps[WSAP_VTCORE_GRAPHEME_MAX_LEN];
+ int n = wsap_vtcore_gstore_expand(g, handle, cps, WSAP_VTCORE_GRAPHEME_MAX_LEN);
if (n <= 0) {
*text = ' ';
return true;
@@ -129,24 +129,24 @@ static bool arena_add(struct arena_out *a, const struct gstore *g, uint32_t hand
(*a->buf)[off] = (uint32_t)n;
memcpy(&(*a->buf)[off + 1], cps, (size_t)n * sizeof(*cps));
a->len = (uint32_t)need;
- *text = TERM_TEXT_CLUSTER | off;
+ *text = WSAP_VTCORE_TEXT_CLUSTER | off;
return true;
}
/* Convert one stored row into dst. False when a cluster record does not fit. */
static bool convert_row(struct arena_out *a, const struct gstore *g,
- struct term_cell *dst, const struct cell *src, int cols)
+ struct wsap_vtcore_cell *dst, const struct cell *src, int cols)
{
for (int x = 0; x < cols; x++) {
const struct cell *c = &src[x];
uint16_t f = c->flags;
uint32_t text = c->c;
- if (TERM_EXPECT(text < 0x20, 0))
+ if (WSAP_VTCORE_EXPECT(text < 0x20, 0))
text = text ? ' ' : 0;
- else if (TERM_EXPECT(term_grapheme_is_handle(text), 0) &&
+ else if (WSAP_VTCORE_EXPECT(wsap_vtcore_grapheme_is_handle(text), 0) &&
!arena_add(a, g, text, &text))
return false;
- dst[x] = (struct term_cell){
+ dst[x] = (struct wsap_vtcore_cell){
.text = text,
.fg = c->fg,
.bg = c->bg,
@@ -175,7 +175,7 @@ struct plan {
};
/* Phase 2: write the plan into the snapshot. Nothing here can fail. */
-static void apply(struct term_snapshot *s, const struct term *t, const struct plan *p)
+static void apply(struct wsap_vtcore_snapshot *s, const struct wsap_vtcore *t, const struct plan *p)
{
int cols = t->cols, rows = t->rows;
int k = p->scrolled;
@@ -190,7 +190,7 @@ static void apply(struct term_snapshot *s, const struct term *t, const struct pl
s->arena_len = p->arena_len;
bool changed = p->all;
- size_t row_bytes = (size_t)cols * sizeof(struct term_cell);
+ size_t row_bytes = (size_t)cols * sizeof(struct wsap_vtcore_cell);
/* Staged rows: copy the smaller part. Either the kept rows go into the
* stage, which then becomes the cells, or the staged rows go into the
* cells. A scroll moves every kept row anyway, so it swaps. */
@@ -201,7 +201,7 @@ static void apply(struct term_snapshot *s, const struct term *t, const struct pl
for (int y = 0; y < rows; y++)
if (p->words[y] == ROW_KEPT)
memcpy(s->stage + (size_t)y * cols, s->cells + (size_t)(y + k) * cols, row_bytes);
- struct term_cell *cells = s->cells;
+ struct wsap_vtcore_cell *cells = s->cells;
s->cells = s->stage;
s->stage = cells;
}
@@ -225,7 +225,7 @@ static void apply(struct term_snapshot *s, const struct term *t, const struct pl
/* Phase 1 reserved room for the worst case, so this cannot fail. */
struct arena_out a = { &s->arena, &s->arena_cap, s->arena_len };
(void)convert_row(&a, t->gstore, s->cells + (size_t)y * cols,
- term_screen_view_row(t, y), cols);
+ wsap_vtcore_screen_view_row(t, y), cols);
s->row_clusters[y] = a.len - s->arena_len;
s->arena_len = a.len;
} else {
@@ -238,19 +238,19 @@ static void apply(struct term_snapshot *s, const struct term *t, const struct pl
}
for (int y = 0; y < rows; y++) {
- struct term_span span = p->c1[y] >= p->c0[y] ? (struct term_span){ p->c0[y], p->c1[y] + 1 }
- : (struct term_span){ 0 };
+ struct wsap_vtcore_span span = p->c1[y] >= p->c0[y] ? (struct wsap_vtcore_span){ p->c0[y], p->c1[y] + 1 }
+ : (struct wsap_vtcore_span){ 0 };
if (p->all || span.begin != s->selection[y].begin || span.end != s->selection[y].end)
changed = true;
s->selection[y] = span;
}
- struct term_cursor cursor = term_cursor(t);
- int view_offset = term_view_offset(t);
+ struct wsap_vtcore_cursor cursor = wsap_vtcore_cursor(t);
+ int view_offset = wsap_vtcore_view_offset(t);
cursor.pos.y += view_offset;
if (cursor.pos.y >= rows)
cursor.visible = false;
- const struct term_cursor *old = &s->view.cursor;
+ const struct wsap_vtcore_cursor *old = &s->view.cursor;
if (cursor.pos.x != old->pos.x || cursor.pos.y != old->pos.y ||
cursor.style != old->style || cursor.visible != old->visible ||
cursor.blink != old->blink ||
@@ -264,7 +264,7 @@ static void apply(struct term_snapshot *s, const struct term *t, const struct pl
}
s->source = t->serial;
- s->view.size = (struct term_vector2i){ .x = cols, .y = rows };
+ s->view.size = (struct wsap_vtcore_vector2i){ .x = cols, .y = rows };
s->view.cursor = cursor;
s->view.scroll_offset = view_offset;
s->view.scrolled = k;
@@ -275,7 +275,7 @@ static void apply(struct term_snapshot *s, const struct term *t, const struct pl
/* Phase 1 for one update. Allocates and checks everything that can fail,
* then hands the plan to apply(). A false return leaves the content as it was. */
-static bool term_snapshot_update_inner(struct term_snapshot *s, struct term *t)
+static bool wsap_vtcore_snapshot_update_inner(struct wsap_vtcore_snapshot *s, struct wsap_vtcore *t)
{
if (!s || !t)
return false;
@@ -311,12 +311,12 @@ static bool term_snapshot_update_inner(struct term_snapshot *s, struct term *t)
if (s->arena_dead > 4096 && s->arena_dead * 2 > s->arena_len)
all = true;
- term_hist_prepare_rows(&t->hist, cols);
- if (!term_hist_view_sync(&t->view, &t->hist, cols))
+ wsap_vtcore_hist_prepare_rows(&t->hist, cols);
+ if (!wsap_vtcore_hist_view_sync(&t->view, &t->hist, cols))
goto done;
- term_hist_view_refine(&t->view, &t->hist, cols, rows);
+ wsap_vtcore_hist_view_refine(&t->view, &t->hist, cols, rows);
for (int y = 0; y < rows; y++)
- new_keys[y] = term_screen_view_row_key(t, y);
+ new_keys[y] = wsap_vtcore_screen_view_row_key(t, y);
if (!all && rows > 1) {
int cand_k = 0;
@@ -377,7 +377,7 @@ static bool term_snapshot_update_inner(struct term_snapshot *s, struct term *t)
* records after the live ones, or in the spare arena for a rebuild. */
struct arena_out a = all ? (struct arena_out){ &s->spare, &s->spare_cap, 0 }
: (struct arena_out){ &s->arena, &s->arena_cap, s->arena_len };
- uint64_t worst = (uint64_t)p.staged * (uint64_t)cols * (1 + TERM_GRAPHEME_MAX_LEN);
+ uint64_t worst = (uint64_t)p.staged * (uint64_t)cols * (1 + WSAP_VTCORE_GRAPHEME_MAX_LEN);
if (!all && worst <= DIRECT_MAX_WORDS) {
if (!arena_reserve(&a, a.len + worst))
goto done;
@@ -388,13 +388,13 @@ static bool term_snapshot_update_inner(struct term_snapshot *s, struct term *t)
continue;
uint32_t before = a.len;
if (!convert_row(&a, t->gstore, s->stage + (size_t)y * cols,
- term_screen_view_row(t, y), cols))
+ wsap_vtcore_screen_view_row(t, y), cols))
goto done;
words[y] = a.len - before;
}
}
- if (!term_selection_view_spans(t, c0, c1))
+ if (!wsap_vtcore_selection_view_spans(t, c0, c1))
goto done;
p.all = all;
p.scrolled = scrolled;
@@ -407,52 +407,52 @@ done:
return ok;
}
-bool term_snapshot_update(struct term_snapshot *s, struct term *t)
+bool wsap_vtcore_snapshot_update(struct wsap_vtcore_snapshot *s, struct wsap_vtcore *t)
{
if (!s || !t)
return false;
- term_write_begin(t);
- bool ok = term_snapshot_update_inner(s, t);
- term_write_end(t);
+ wsap_vtcore_write_begin(t);
+ bool ok = wsap_vtcore_snapshot_update_inner(s, t);
+ wsap_vtcore_write_end(t);
return ok;
}
-const struct term_view *term_snapshot_view(const struct term_snapshot *s)
+const struct wsap_vtcore_view *wsap_vtcore_snapshot_view(const struct wsap_vtcore_snapshot *s)
{
return s ? &s->view : NULL;
}
-const struct term_cell *term_snapshot_row(const struct term_snapshot *s, int y)
+const struct wsap_vtcore_cell *wsap_vtcore_snapshot_row(const struct wsap_vtcore_snapshot *s, int y)
{
if (!s || y < 0 || y >= s->view.size.y)
return NULL;
return s->cells + (size_t)y * s->view.size.x;
}
-uint64_t term_snapshot_row_key(const struct term_snapshot *s, int y)
+uint64_t wsap_vtcore_snapshot_row_key(const struct wsap_vtcore_snapshot *s, int y)
{
if (!s || y < 0 || y >= s->view.size.y)
return 0;
return s->keys[y];
}
-struct term_span term_snapshot_row_selection(const struct term_snapshot *s, int y)
+struct wsap_vtcore_span wsap_vtcore_snapshot_row_selection(const struct wsap_vtcore_snapshot *s, int y)
{
if (!s || y < 0 || y >= s->view.size.y)
- return (struct term_span){ 0 };
+ return (struct wsap_vtcore_span){ 0 };
return s->selection[y];
}
-int term_snapshot_cluster(const struct term_snapshot *s, uint32_t text, uint32_t *cps, int cap)
+int wsap_vtcore_snapshot_cluster(const struct wsap_vtcore_snapshot *s, uint32_t text, uint32_t *cps, int cap)
{
if (!s)
return 0;
- if (!(text & TERM_TEXT_CLUSTER)) {
+ if (!(text & WSAP_VTCORE_TEXT_CLUSTER)) {
if (cps && cap > 0)
cps[0] = text;
return 1;
}
- uint32_t off = text & ~TERM_TEXT_CLUSTER;
+ uint32_t off = text & ~WSAP_VTCORE_TEXT_CLUSTER;
if (off >= s->arena_len)
return 0;
int n = (int)s->arena[off];