diff options
| author | Berke Güzel <wenekar1@gmail.com> | 2026-10-15 13:04:01 +0300 |
|---|---|---|
| committer | Berke Güzel <wenekar1@gmail.com> | 2026-10-15 13:04:01 +0300 |
| commit | dcebf9c7f954a55350b68d0ead1935612c4e2e05 (patch) | |
| tree | 3eb3e9f6154c6760bcb89a565f8ddc8f8b66cb90 /src/screen.c | |
| parent | bd8222e615134c3ec5e7a07b6b1fa97c9bd4a471 (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/screen.c')
| -rw-r--r-- | src/screen.c | 308 |
1 files changed, 154 insertions, 154 deletions
diff --git a/src/screen.c b/src/screen.c index ab6989a..8b1b5ce 100644 --- a/src/screen.c +++ b/src/screen.c @@ -10,7 +10,7 @@ static void *grow(struct grow_buf *b, size_t bytes) { if (bytes > b->cap) { - size_t cap = term_grow_cap(b->cap ? b->cap : 256, bytes); + size_t cap = wsap_vtcore_grow_cap(b->cap ? b->cap : 256, bytes); void *data = realloc(b->data, cap); if (!data) return NULL; @@ -52,7 +52,7 @@ static bool screen_setup(struct screen *s, int cols, int rows, uint64_t *next_ui s->text_slab = text_slab; s->text_cap = bytes; } - fill_cells(s->cells_slab, (int)cells, TERM_COLOR_FG, TERM_COLOR_BG); + fill_cells(s->cells_slab, (int)cells, WSAP_VTCORE_COLOR_FG, WSAP_VTCORE_COLOR_BG); s->rows = rows; s->cols = cols; s->top = 0; @@ -79,7 +79,7 @@ static bool screen_setup(struct screen *s, int cols, int rows, uint64_t *next_ui return true; } -static void screen_push_row_inner(struct term *t, struct srow *row) +static void screen_push_row_inner(struct wsap_vtcore *t, struct srow *row) { srow_materialize_text(row); bool wrapped = (row->flags & RF_WRAPPED) != 0; @@ -93,27 +93,27 @@ static void screen_push_row_inner(struct term *t, struct srow *row) memset(tr->text + tr->text_len, ' ', (size_t)fill); text_commit(row, fill, fill, fill, HLINE_NARROW, (struct text_pen){ tr->fill.fg, tr->fill.bg, 0 }); } - term_hist_push_text(&t->hist, tr, wrapped); + wsap_vtcore_hist_push_text(&t->hist, tr, wrapped); return; } /* The rest of a wrapped row is part of the line: blanks up to the edge. */ - if (TERM_EXPECT(wrapped && row->len < cols, 0)) { + if (WSAP_VTCORE_EXPECT(wrapped && row->len < cols, 0)) { srow_settle(row, cols); if (!(row->flags & RF_TAIL_PADDED)) - fill_cells(row->cells + row->len, cols - row->len, TERM_COLOR_FG, TERM_COLOR_BG); + fill_cells(row->cells + row->len, cols - row->len, WSAP_VTCORE_COLOR_FG, WSAP_VTCORE_COLOR_BG); row->flags |= RF_TAIL_PADDED; row->len = (uint16_t)cols; } - term_hist_push_row(&t->hist, row->cells, row->len, wrapped, t->gstore); + wsap_vtcore_hist_push_row(&t->hist, row->cells, row->len, wrapped, t->gstore); } -void term_screen_push_row(struct term *t, struct srow *row) +void wsap_vtcore_screen_push_row(struct wsap_vtcore *t, struct srow *row) { screen_push_row_inner(t, row); /* The push can drop more rows than it adds, so the view learns the new * history size here and not before. */ - if (TERM_EXPECT(t->view.anchor.active, 0)) - term_hist_view_note_push(&t->view, &t->hist, t->cols); + if (WSAP_VTCORE_EXPECT(t->view.anchor.active, 0)) + wsap_vtcore_hist_view_note_push(&t->view, &t->hist, t->cols); } static void screen_free(struct screen *s) @@ -127,35 +127,35 @@ static void screen_free(struct screen *s) } /* Read the history count without changing its rows. */ -int term_screen_history_rows(const struct term *t) +int wsap_vtcore_screen_history_rows(const struct wsap_vtcore *t) { if (!t || !screen_has_history(t)) return 0; - uint64_t rows = term_hist_view_rows(&t->view, &t->hist, t->cols); + uint64_t rows = wsap_vtcore_hist_view_rows(&t->view, &t->hist, t->cols); return rows > INT_MAX ? INT_MAX : (int)rows; } -int term_history_rows(const struct term *t) +int wsap_vtcore_history_rows(const struct wsap_vtcore *t) { if (!t) return 0; - term_write_begin((struct term *)t); - int rows = term_screen_history_rows(t); - term_write_end((struct term *)t); + wsap_vtcore_write_begin((struct wsap_vtcore *)t); + int rows = wsap_vtcore_screen_history_rows(t); + wsap_vtcore_write_end((struct wsap_vtcore *)t); return rows; } -const struct cell *term_screen_view_row(const struct term *t, int y) +const struct cell *wsap_vtcore_screen_view_row(const struct wsap_vtcore *t, int y) { if (!t || y < 0 || y >= t->rows) return NULL; struct hist_view_pos pos; - if (term_hist_view_row_pos(&t->view, &t->hist, t->cols, t->rows, screen_top_line(t), y, &pos)) { + if (wsap_vtcore_hist_view_row_pos(&t->view, &t->hist, t->cols, t->rows, screen_top_line(t), y, &pos)) { struct cell *out = t->view_row_scratch; - int got = term_hist_decode_segment(&t->hist, pos.id, pos.seg, t->cols, out, t->gstore); + int got = wsap_vtcore_hist_decode_segment(&t->hist, pos.id, pos.seg, t->cols, out, t->gstore); if (got < t->cols) { - fill_cells(out + got, t->cols - got, TERM_COLOR_FG, TERM_COLOR_BG); - int line_rows = term_hist_line_rows(&t->hist, pos.id, t->cols); + fill_cells(out + got, t->cols - got, WSAP_VTCORE_COLOR_FG, WSAP_VTCORE_COLOR_BG); + int line_rows = wsap_vtcore_hist_line_rows(&t->hist, pos.id, t->cols); if (pos.seg + 1 < line_rows || (pos.id + 1 == hist_end_id(&t->hist) && hist_is_open(&t->hist))) set_cells_wrap_pad(out + got, t->cols - got); } @@ -170,7 +170,7 @@ const struct cell *term_screen_view_row(const struct term *t, int y) return srow->cells; if (srow->len > 0) memcpy(out, srow->cells, (size_t)srow->len * sizeof(struct cell)); - fill_cells(out + srow->len, t->cols - srow->len, TERM_COLOR_FG, TERM_COLOR_BG); + fill_cells(out + srow->len, t->cols - srow->len, WSAP_VTCORE_COLOR_FG, WSAP_VTCORE_COLOR_BG); return out; } text_row_cells(srow, out, t->cols); @@ -186,14 +186,14 @@ static inline uint64_t mix64(uint64_t x) return x ^ (x >> 33); } -uint64_t term_screen_view_row_key(const struct term *t, int y) +uint64_t wsap_vtcore_screen_view_row_key(const struct wsap_vtcore *t, int y) { if (!t || y < 0 || y >= t->rows) return 0; struct hist_view_pos pos; - if (term_hist_view_row_pos(&t->view, &t->hist, t->cols, t->rows, screen_top_line(t), y, &pos)) { + if (wsap_vtcore_hist_view_row_pos(&t->view, &t->hist, t->cols, t->rows, screen_top_line(t), y, &pos)) { uint64_t line_ver = (pos.id + 1 == hist_end_id(&t->hist) && hist_is_open(&t->hist)) - ? (term_hist_line_cells(&t->hist, pos.id) << 32) : 0; + ? (wsap_vtcore_hist_line_cells(&t->hist, pos.id) << 32) : 0; if (pos.id == t->hist.base_cell_id) line_ver ^= mix64(t->hist.base_cell); return mix64(((uint64_t)pos.id << 16) ^ (uint64_t)pos.seg ^ line_ver ^ 0x517cc1b727220a95ull); @@ -204,62 +204,62 @@ uint64_t term_screen_view_row_key(const struct term *t, int y) return mix64(row->uid ^ ((uint64_t)row->version << 32)); } -int term_view_offset(const struct term *t) +int wsap_vtcore_view_offset(const struct wsap_vtcore *t) { if (!t) return 0; - return term_hist_view_scroll_offset(&t->view, &t->hist, t->cols); + return wsap_vtcore_hist_view_scroll_offset(&t->view, &t->hist, t->cols); } -void term_scroll_to(struct term *t, int offset) +void wsap_vtcore_scroll_to(struct wsap_vtcore *t, int offset) { if (!t) return; - term_write_begin(t); + wsap_vtcore_write_begin(t); if (t->alternate || offset < 0) offset = 0; struct view_anchor old = t->view.anchor; - term_hist_view_scroll_to(&t->view, &t->hist, t->cols, screen_top_line(t), offset); + wsap_vtcore_hist_view_scroll_to(&t->view, &t->hist, t->cols, screen_top_line(t), offset); if (old.active != t->view.anchor.active || old.line_id != t->view.anchor.line_id || old.seg != t->view.anchor.seg) t->full_gen++; - term_write_end(t); + wsap_vtcore_write_end(t); } -void term_scroll(struct term *t, int lines) +void wsap_vtcore_scroll(struct wsap_vtcore *t, int lines) { if (!t) return; /* Read the alternate flag inside the writer section. A read before - * term_write_begin races with a feed on another thread. */ - term_write_begin(t); + * wsap_vtcore_write_begin races with a feed on another thread. */ + wsap_vtcore_write_begin(t); if (t->alternate) { - term_write_end(t); + wsap_vtcore_write_end(t); return; } struct view_anchor old = t->view.anchor; - term_hist_view_scroll(&t->view, &t->hist, t->cols, screen_top_line(t), lines); + wsap_vtcore_hist_view_scroll(&t->view, &t->hist, t->cols, screen_top_line(t), lines); if (old.active != t->view.anchor.active || old.line_id != t->view.anchor.line_id || old.seg != t->view.anchor.seg) t->full_gen++; - term_write_end(t); + wsap_vtcore_write_end(t); } -struct term_scrollback term_scrollback(const struct term *t) +struct wsap_vtcore_scrollback wsap_vtcore_scrollback(const struct wsap_vtcore *t) { if (!t) - return (struct term_scrollback){ 0 }; - term_write_begin((struct term *)t); - struct term_scrollback sb = { 0 }; + return (struct wsap_vtcore_scrollback){ 0 }; + wsap_vtcore_write_begin((struct wsap_vtcore *)t); + struct wsap_vtcore_scrollback sb = { 0 }; if (!t->alternate) { - sb.offset = term_view_offset(t); - sb.total = term_screen_history_rows(t); + sb.offset = wsap_vtcore_view_offset(t); + sb.total = wsap_vtcore_screen_history_rows(t); } - term_write_end((struct term *)t); + wsap_vtcore_write_end((struct wsap_vtcore *)t); return sb; } -void term_screen_erase_line(struct term *t, int mode) +void wsap_vtcore_screen_erase_line(struct wsap_vtcore *t, int mode) { t->pending_wrap = false; int p = phys_row(t, t->cursor_y); @@ -272,7 +272,7 @@ void term_screen_erase_line(struct term *t, int mode) } } -void term_screen_delete_chars(struct term *t, int n) +void wsap_vtcore_screen_delete_chars(struct wsap_vtcore *t, int n) { t->pending_wrap = false; if (t->cursor_x >= t->cols) @@ -283,7 +283,7 @@ void term_screen_delete_chars(struct term *t, int n) n = cols - x; int p = phys_row(t, t->cursor_y); struct cell *row = row_cells(t, p); - struct srow *srow = term_row(t, t->cursor_y); + struct srow *srow = wsap_vtcore_row(t, t->cursor_y); int len = srow->len; clear_wide_edges(row, x, x + n, len); if (len > cols) { @@ -304,7 +304,7 @@ void term_screen_delete_chars(struct term *t, int n) int from = cols - n; if (from < x) from = x; - term_row_fill_gap(srow, from); + wsap_vtcore_row_fill_gap(srow, from); fill_cells(&row[from], cols - from, t->cur_fg, t->cur_bg); srow->len = (uint16_t)cols; } @@ -313,7 +313,7 @@ void term_screen_delete_chars(struct term *t, int n) srow->version++; } -void term_screen_insert_chars(struct term *t, int n) +void wsap_vtcore_screen_insert_chars(struct wsap_vtcore *t, int n) { t->pending_wrap = false; if (t->cursor_x >= t->cols) @@ -324,8 +324,8 @@ void term_screen_insert_chars(struct term *t, int n) n = cols - x; int p = phys_row(t, t->cursor_y); struct cell *row = row_cells(t, p); - struct srow *srow = term_row(t, t->cursor_y); - term_row_fill_gap(srow, x); + struct srow *srow = wsap_vtcore_row(t, t->cursor_y); + wsap_vtcore_row_fill_gap(srow, x); int len = srow->len; if (x > 0 && x < len && (row[x].flags & CELL_TRAIL)) { fill_cells(row + x - 1, 2, t->cur_fg, t->cur_bg); @@ -355,7 +355,7 @@ void term_screen_insert_chars(struct term *t, int n) srow->version++; } -void term_screen_erase_all(struct term *t) +void wsap_vtcore_screen_erase_all(struct wsap_vtcore *t) { t->pending_wrap = false; struct screen *s = &t->scr; @@ -369,9 +369,9 @@ void term_screen_erase_all(struct term *t) t->full_gen++; } -#if defined(TERM_KERNEL_AVX2) +#if defined(WSAP_VTCORE_KERNEL_AVX2) /* The inner columns [1, len - 1): the callers probed both ends. */ -TERM_TARGET_AVX2 +WSAP_VTCORE_TARGET_AVX2 static bool text_has_text_avx2(const uint8_t *text, int len) { int x = 1; @@ -388,7 +388,7 @@ static bool text_has_text_avx2(const uint8_t *text, int len) return false; } -TERM_TARGET_AVX2 +WSAP_VTCORE_TARGET_AVX2 static bool cells_has_text_avx2(const struct cell *cells, int len) { int x = 1; @@ -433,8 +433,8 @@ static inline bool text_has_text(const uint8_t *text, int len) return true; } #elif defined(__x86_64__) -#if defined(TERM_KERNEL_AVX2) - if (term_use_avx2()) +#if defined(WSAP_VTCORE_KERNEL_AVX2) + if (wsap_vtcore_use_avx2()) return text_has_text_avx2(text, len); #endif const __m128i vspace = _mm_set1_epi8(' '); @@ -473,8 +473,8 @@ static inline bool cells_has_text(const struct cell *cells, int len) if (vmaxvq_u32(cmp) != 0) return true; } -#elif defined(TERM_KERNEL_AVX2) - if (term_use_avx2()) +#elif defined(WSAP_VTCORE_KERNEL_AVX2) + if (wsap_vtcore_use_avx2()) return cells_has_text_avx2(cells, len); #endif for (; x < len - 1; x++) @@ -493,14 +493,14 @@ static inline bool srow_has_text(const struct srow *r, int cols) return cells_has_text(r->cells, len); } -void term_screen_erase(struct term *t, int mode) +void wsap_vtcore_screen_erase(struct wsap_vtcore *t, int mode) { t->pending_wrap = false; if (mode == 3) { - term_hist_clear(&t->hist); + wsap_vtcore_hist_clear(&t->hist); t->view.anchor = (struct view_anchor){0}; t->view.index_cols = 0; - term_select_clear(t); + wsap_vtcore_select_clear(t); t->full_gen++; return; } @@ -514,8 +514,8 @@ void term_screen_erase(struct term *t, int mode) for (int y = rows - 1; y >= 0; y--) { const struct srow *r = &slots[p]; p = (p == 0) ? rows - 1 : p - 1; - if (TERM_EXPECT(r->len == 0, 1)) { - if (TERM_EXPECT((r->flags & RF_WRAPPED) != 0, 0)) + if (WSAP_VTCORE_EXPECT(r->len == 0, 1)) { + if (WSAP_VTCORE_EXPECT((r->flags & RF_WRAPPED) != 0, 0)) tail_clean = false; continue; } @@ -526,13 +526,13 @@ void term_screen_erase(struct term *t, int mode) tail_clean = false; } if (last_active >= 0 && !t->alternate) { - term_screen_scroll_region(t, 0, rows - 1, last_active + 1, false, true); + wsap_vtcore_screen_scroll_region(t, 0, rows - 1, last_active + 1, false, true); if (tail_clean) { t->view.anchor = (struct view_anchor){0}; return; } } - term_screen_erase_all(t); + wsap_vtcore_screen_erase_all(t); t->view.anchor = (struct view_anchor){0}; t->full_gen++; } else if (mode == 0) { @@ -561,7 +561,7 @@ void term_screen_erase(struct term *t, int mode) } /* Reset a recycled row's contents and identity; callers choose its version. */ -static inline void reset_scrolled_row(struct term *t, struct srow *row, bool def) +static inline void reset_scrolled_row(struct wsap_vtcore *t, struct srow *row, bool def) { row->kind = ROW_CELLS; row->uid = ++t->next_row_uid; @@ -575,7 +575,7 @@ static inline void reset_scrolled_row(struct term *t, struct srow *row, bool def } } -static void rotate_and_clear_srows(struct term *t, struct srow *base, int height, +static void rotate_and_clear_srows(struct wsap_vtcore *t, struct srow *base, int height, int count, bool down, bool def) { struct srow tmp[64]; @@ -600,7 +600,7 @@ static void rotate_and_clear_srows(struct term *t, struct srow *base, int height free(saved); } -void term_screen_scroll_region(struct term *t, int top, int bottom, int count, +void wsap_vtcore_screen_scroll_region(struct wsap_vtcore *t, int top, int bottom, int count, bool down, bool history) { t->pending_wrap = false; @@ -619,7 +619,7 @@ void term_screen_scroll_region(struct term *t, int top, int bottom, int count, if (!down) { bool has_hist = history && screen_has_history(t); - if (TERM_EXPECT(t->prompt_y >= 0, 0) && !t->alternate) { + if (WSAP_VTCORE_EXPECT(t->prompt_y >= 0, 0) && !t->alternate) { if (history) { if (t->prompt_y > 0) t->prompt_y = t->prompt_y > count ? t->prompt_y - count : 0; @@ -635,8 +635,8 @@ void term_screen_scroll_region(struct term *t, int top, int bottom, int count, int p = orig_top + i; if (p >= rows) p -= rows; struct srow *row = &s->slot[p]; - if (TERM_EXPECT(has_hist, 1)) - term_screen_push_row(t, row); + if (WSAP_VTCORE_EXPECT(has_hist, 1)) + wsap_vtcore_screen_push_row(t, row); else screen_drop_top(s, row); if (history) { @@ -683,9 +683,9 @@ void term_screen_scroll_region(struct term *t, int top, int bottom, int count, if (!down && history && top == 0 && !t->alternate) { for (int i = 0; i < count; i++) { - term_screen_scroll_up(t); + wsap_vtcore_screen_scroll_up(t); if (bottom + 1 < t->rows) - term_screen_scroll_region(t, bottom, t->rows - 1, 1, true, false); + wsap_vtcore_screen_scroll_region(t, bottom, t->rows - 1, 1, true, false); } return; } @@ -734,9 +734,9 @@ void term_screen_scroll_region(struct term *t, int top, int bottom, int count, t->full_gen++; } -static bool term_resize_internal(struct term *t, int cols, int rows); +static bool wsap_vtcore_resize_internal(struct wsap_vtcore *t, int cols, int rows); -void term_screen_set_alternate(struct term *t, bool enabled) +void wsap_vtcore_screen_set_alternate(struct wsap_vtcore *t, bool enabled) { if (t->alternate == enabled) return; @@ -759,7 +759,7 @@ void term_screen_set_alternate(struct term *t, bool enabled) struct screen tmp_scr = t->scr; t->scr = t->alt_scr; t->alt_scr = tmp_scr; - t->normal_saved_cursor = term_cursor_save(t); + t->normal_saved_cursor = wsap_vtcore_cursor_save(t); t->normal_saved_decsc = t->saved; t->normal_cursor_visible = t->cursor_visible; t->normal_cursor_style = (uint8_t)t->cursor_style; @@ -776,7 +776,7 @@ void term_screen_set_alternate(struct term *t, bool enabled) t->right_margin = t->cols - 1; /* The alternate screen has no history to show. */ t->view.anchor = (struct view_anchor){0}; - term_select_clear(t); + wsap_vtcore_select_clear(t); t->full_gen++; } else { int cols = t->cols; @@ -789,29 +789,29 @@ void term_screen_set_alternate(struct term *t, bool enabled) t->alternate = false; t->saved = t->normal_saved_decsc; t->cursor_visible = t->normal_cursor_visible; - t->cursor_style = (enum term_cursor_style)t->normal_cursor_style; + t->cursor_style = (enum wsap_vtcore_cursor_style)t->normal_cursor_style; t->cursor_blink = t->normal_cursor_blink; - term_cursor_apply(t, &t->normal_saved_cursor); + wsap_vtcore_cursor_apply(t, &t->normal_saved_cursor); t->cursor_x = t->normal_saved_cursor.x; t->cursor_y = t->normal_saved_cursor.y; t->pending_wrap = t->normal_saved_cursor.pending_wrap; if (cols != t->cols || rows != t->rows) - term_resize_internal(t, cols, rows); + wsap_vtcore_resize_internal(t, cols, rows); t->scroll_top = 0; t->scroll_bottom = t->rows - 1; t->left_margin = 0; t->right_margin = t->cols - 1; /* The alternate screen has no history to show. */ t->view.anchor = (struct view_anchor){0}; - term_select_clear(t); + wsap_vtcore_select_clear(t); t->full_gen++; } } -/* The caller has checked that requested is in [0, TERM_SCROLLBACK_MAX]. */ +/* The caller has checked that requested is in [0, WSAP_VTCORE_SCROLLBACK_MAX]. */ static int scrollback_lines(int requested) { - return requested == 0 ? TERM_SCROLLBACK_DEFAULT : requested; + return requested == 0 ? WSAP_VTCORE_SCROLLBACK_DEFAULT : requested; } static atomic_int tables_state; @@ -824,16 +824,16 @@ static void tables_init(void) return; int expected = 0; if (atomic_compare_exchange_strong(&tables_state, &expected, 1)) { - term_isa_init(); - term_text_tables_init(); + wsap_vtcore_isa_init(); + wsap_vtcore_text_tables_init(); atomic_store_explicit(&tables_state, 2, memory_order_release); return; } while (atomic_load_explicit(&tables_state, memory_order_acquire) != 2) - term_cpu_relax(); + wsap_vtcore_cpu_relax(); } -bool term_tables_ready(void) +bool wsap_vtcore_tables_ready(void) { return atomic_load_explicit(&tables_state, memory_order_relaxed) == 2; } @@ -849,31 +849,31 @@ static bool name_valid(const char *s) return true; } -struct term *term_new(const struct term_options *opt) +struct wsap_vtcore *wsap_vtcore_new(const struct wsap_vtcore_options *opt) { tables_init(); - static const struct term_options defaults; + static const struct wsap_vtcore_options defaults; if (!opt) opt = &defaults; for (size_t i = 0; i < sizeof(opt->reserved) / sizeof(opt->reserved[0]); i++) if (opt->reserved[i]) return NULL; - if (opt->size.x < 0 || opt->size.y < 0 || opt->size.x > TERM_MAX_COLS) + if (opt->size.x < 0 || opt->size.y < 0 || opt->size.x > WSAP_VTCORE_MAX_COLS) return NULL; - if (opt->scrollback < 0 || opt->scrollback > TERM_SCROLLBACK_MAX || - (unsigned)opt->cursor_style > TERM_CURSOR_BAR) + if (opt->scrollback < 0 || opt->scrollback > WSAP_VTCORE_SCROLLBACK_MAX || + (unsigned)opt->cursor_style > WSAP_VTCORE_CURSOR_BAR) return NULL; - const char *name = opt->name && *opt->name ? opt->name : "term"; + const char *name = opt->name && *opt->name ? opt->name : "wsap_vtcore"; if (!name_valid(name)) return NULL; int cols = opt->size.x == 0 ? 80 : opt->size.x; int rows = opt->size.y == 0 ? 24 : opt->size.y; static _Atomic uint64_t next_serial = 1; - struct term *t = calloc(1, sizeof(*t)); + struct wsap_vtcore *t = calloc(1, sizeof(*t)); if (!t) return NULL; - if (!term_mutex_init(&t->write_lock)) { + if (!wsap_vtcore_mutex_init(&t->write_lock)) { free(t); return NULL; } @@ -888,35 +888,35 @@ struct term *term_new(const struct term_options *opt) t->prompt_y = -1; t->sb_capacity = scrollback_lines(opt->scrollback); t->cb = opt->callbacks; - term_palette_init(&t->palette, &opt->theme); + wsap_vtcore_palette_init(&t->palette, &opt->theme); t->next_row_uid = 1; if (!screen_setup(&t->scr, cols, rows, &t->next_row_uid)) { - term_mutex_destroy(&t->write_lock); + wsap_vtcore_mutex_destroy(&t->write_lock); free(t); return NULL; } - if (!term_hist_init(&t->hist, t->sb_capacity, 0)) { + if (!wsap_vtcore_hist_init(&t->hist, t->sb_capacity, 0)) { screen_free(&t->scr); - term_mutex_destroy(&t->write_lock); + wsap_vtcore_mutex_destroy(&t->write_lock); free(t); return NULL; } t->hist.wrap_cols = (uint16_t)cols; - t->gstore = term_gstore_new(t); + t->gstore = wsap_vtcore_gstore_new(t); t->view_scratch_cap = cols > 256 ? cols : 256; t->view_row_scratch = calloc((size_t)t->view_scratch_cap, sizeof(struct cell)); t->tabs_cap = cols > 256 ? cols : 256; t->tabs = calloc((size_t)t->tabs_cap, 1); if (!t->gstore || !t->view_row_scratch || !t->tabs) { - term_free(t); + wsap_vtcore_free(t); return NULL; } - t->cur_fg = TERM_COLOR_FG; - t->cur_bg = TERM_COLOR_BG; + t->cur_fg = WSAP_VTCORE_COLOR_FG; + t->cur_bg = WSAP_VTCORE_COLOR_BG; t->cursor_visible = true; t->default_cursor_style = opt->cursor_style; t->default_cursor_blink = !opt->cursor_steady; @@ -924,16 +924,16 @@ struct term *term_new(const struct term_options *opt) t->cursor_blink = t->default_cursor_blink; t->autowrap = true; t->charsets[0] = t->charsets[1] = 'B'; - t->saved = term_saved_cursor_initial(); + t->saved = wsap_vtcore_saved_cursor_initial(); memcpy(t->tcap_name, t->name, sizeof(t->name)); - term_tabs_default(t); + wsap_vtcore_tabs_default(t); t->full_gen = 1; memset(t->text_width.lo, 0xFF, sizeof(t->text_width.lo)); return t; } -void term_free(struct term *t) +void wsap_vtcore_free(struct wsap_vtcore *t) { if (!t) return; @@ -949,13 +949,13 @@ void term_free(struct term *t) free(t->resize.lens.data); free(t->ascii_ends.data); free(t->ascii_tail.data); - term_hist_free(&t->hist); - term_hist_view_free(&t->view); + wsap_vtcore_hist_free(&t->hist); + wsap_vtcore_hist_view_free(&t->view); free(t->view_row_scratch); free(t->tabs); - term_osc_cleanup(t); - term_gstore_free(t->gstore); - term_mutex_destroy(&t->write_lock); + wsap_vtcore_osc_cleanup(t); + wsap_vtcore_gstore_free(t->gstore); + wsap_vtcore_mutex_destroy(&t->write_lock); free(t); } @@ -1005,7 +1005,7 @@ static bool reflow_target_end_row(struct reflow_target *r, bool wrapped) int len = r->x > r->cols ? r->cols : r->x; memcpy(dst_cells, r->cells, (size_t)len * sizeof(struct cell)); if (len < r->cols) - fill_cells(dst_cells + len, r->cols - len, TERM_COLOR_FG, TERM_COLOR_BG); + fill_cells(dst_cells + len, r->cols - len, WSAP_VTCORE_COLOR_FG, WSAP_VTCORE_COLOR_BG); dst->len = len; dst->wrapped = wrapped; r->x = 0; @@ -1025,7 +1025,7 @@ static bool reflow_append_span(struct reflow_target *r, const struct cell *cells int take = len - i < room ? len - i : room; if (i + take < len && (cells[i + take].flags & CELL_TRAIL)) { if (--take == 0) { - r->cells[r->x] = (struct cell){' ', TERM_COLOR_FG, TERM_COLOR_BG, CELL_WRAP_PAD, 0}; + r->cells[r->x] = (struct cell){' ', WSAP_VTCORE_COLOR_FG, WSAP_VTCORE_COLOR_BG, CELL_WRAP_PAD, 0}; r->x++; if (!reflow_target_end_row(r, true)) return false; @@ -1075,7 +1075,7 @@ static bool reflow_append_wide(struct reflow_target *r, const struct cell *cells int n = (cells[i].flags & CELL_WIDE) && r->cols > 1 ? 2 : 1; if (r->x + n > r->cols) { if (r->x < r->cols) { - r->cells[r->x] = (struct cell){' ', TERM_COLOR_FG, TERM_COLOR_BG, CELL_WRAP_PAD, 0}; + r->cells[r->x] = (struct cell){' ', WSAP_VTCORE_COLOR_FG, WSAP_VTCORE_COLOR_BG, CELL_WRAP_PAD, 0}; r->x++; } if (!reflow_target_end_row(r, true)) @@ -1140,26 +1140,26 @@ static bool reflow_append(struct reflow_target *r, const struct cell *cells, int /* Move up to max_pull rows from the end of history to the top of dst. A line * that does not fit whole leaves its first rows in history, still open. */ -static int pull_history_rows(struct term *t, struct screen *dst_scr, int cols, int max_pull) +static int pull_history_rows(struct wsap_vtcore *t, struct screen *dst_scr, int cols, int max_pull) { struct hist *h = &t->hist; int pull = 0; for (uint64_t id = hist_end_id(h); pull < max_pull && id > hist_first_id(h); id--) - pull += term_hist_line_rows(h, id - 1, cols); + pull += wsap_vtcore_hist_line_rows(h, id - 1, cols); if (pull > max_pull) pull = max_pull; int slot = pull; while (slot > 0) { uint64_t id = hist_end_id(h) - 1; - int line_rows = term_hist_line_rows(h, id, cols); + int line_rows = wsap_vtcore_hist_line_rows(h, id, cols); bool open = hist_is_open(h); int take = slot < line_rows ? slot : line_rows; int first = line_rows - take; struct cell *pull_buf = grow(&t->resize.pull, (size_t)take * (size_t)cols * sizeof(*pull_buf)); int *pull_lens = grow(&t->resize.lens, (size_t)take * sizeof(*pull_lens)); if (pull_buf && pull_lens) - term_hist_decode_segments(h, id, first, take, cols, pull_buf, pull_lens, t->gstore); + wsap_vtcore_hist_decode_segments(h, id, first, take, cols, pull_buf, pull_lens, t->gstore); for (int seg = line_rows - 1; seg >= first; seg--) { struct srow *dst = &dst_scr->slot[--slot]; int idx = seg - first; @@ -1168,7 +1168,7 @@ static int pull_history_rows(struct term *t, struct screen *dst_scr, int cols, i memcpy(dst->cells, pull_buf + (size_t)idx * cols, (size_t)cols * sizeof(struct cell)); bool wrapped = seg + 1 < line_rows || open; if (got < cols) { - fill_cells(dst->cells + got, cols - got, TERM_COLOR_FG, TERM_COLOR_BG); + fill_cells(dst->cells + got, cols - got, WSAP_VTCORE_COLOR_FG, WSAP_VTCORE_COLOR_BG); if (wrapped) { set_cells_wrap_pad(dst->cells + got, cols - got); got = cols; @@ -1181,20 +1181,20 @@ static int pull_history_rows(struct term *t, struct screen *dst_scr, int cols, i dst->uid = ++t->next_row_uid; } if (first == 0) { - term_hist_pop_line(h, NULL, 0, t->gstore); + wsap_vtcore_hist_pop_line(h, NULL, 0, t->gstore); continue; } struct cell *rest = grow(&t->resize.rest, (size_t)first * (size_t)cols * sizeof(*rest)); int *lens = grow(&t->resize.lens, (size_t)first * sizeof(*lens)); if (rest && lens) - term_hist_decode_segments(h, id, 0, first, cols, rest, lens, t->gstore); - term_hist_pop_line(h, NULL, 0, t->gstore); + wsap_vtcore_hist_decode_segments(h, id, 0, first, cols, rest, lens, t->gstore); + wsap_vtcore_hist_pop_line(h, NULL, 0, t->gstore); if (rest && lens) { for (int seg = 0; seg < first; seg++) { struct cell *row = rest + (size_t)seg * cols; if (lens[seg] < cols) - fill_cells(row + lens[seg], cols - lens[seg], TERM_COLOR_FG, TERM_COLOR_BG); - term_hist_push_row(h, row, cols, true, t->gstore); + fill_cells(row + lens[seg], cols - lens[seg], WSAP_VTCORE_COLOR_FG, WSAP_VTCORE_COLOR_BG); + wsap_vtcore_hist_push_row(h, row, cols, true, t->gstore); } } } @@ -1202,7 +1202,7 @@ static int pull_history_rows(struct term *t, struct screen *dst_scr, int cols, i } /* The spare screen, set up as cols x rows, for a resize to build into. */ -static bool resize_target(struct term *t, struct screen *out, int cols, int rows, +static bool resize_target(struct wsap_vtcore *t, struct screen *out, int cols, int rows, const struct screen *from) { *out = t->spare; @@ -1250,9 +1250,9 @@ static void copy_srow_resized(struct srow *dst, struct srow *src, int old_cols, dst->version = src->version + 1; } -static bool term_resize_internal(struct term *t, int cols, int rows) +static bool wsap_vtcore_resize_internal(struct wsap_vtcore *t, int cols, int rows) { - term_gstore_pin(t->gstore); + wsap_vtcore_gstore_pin(t->gstore); if (t->alternate || cols == t->scr.cols) { struct screen *active_scr = &t->scr; @@ -1261,7 +1261,7 @@ static bool term_resize_internal(struct term *t, int cols, int rows) struct screen new_scr; if (!resize_target(t, &new_scr, cols, rows, active_scr)) { - term_gstore_unpin(t->gstore); + wsap_vtcore_gstore_unpin(t->gstore); return false; } @@ -1270,7 +1270,7 @@ static bool term_resize_internal(struct term *t, int cols, int rows) if (rows < old_rows && t->cursor_y >= rows) { int shift = t->cursor_y - rows + 1; for (int i = 0; i < shift; i++) - term_screen_scroll_up(t); + wsap_vtcore_screen_scroll_up(t); t->cursor_y -= shift; t->saved.y = t->saved.y >= shift ? t->saved.y - shift : 0; } @@ -1322,9 +1322,9 @@ static bool term_resize_internal(struct term *t, int cols, int rows) t->scroll_bottom = rows - 1; t->left_margin = 0; t->right_margin = cols - 1; - term_hist_view_rewrap(&t->view, &t->hist, cols); + wsap_vtcore_hist_view_rewrap(&t->view, &t->hist, cols); t->full_gen++; - term_gstore_unpin(t->gstore); + wsap_vtcore_gstore_unpin(t->gstore); return true; } @@ -1333,7 +1333,7 @@ static bool term_resize_internal(struct term *t, int cols, int rows) struct screen new_scr; if (!resize_target(t, &new_scr, cols, rows, &t->scr)) { - term_gstore_unpin(t->gstore); + wsap_vtcore_gstore_unpin(t->gstore); return false; } @@ -1357,17 +1357,17 @@ static bool term_resize_internal(struct term *t, int cols, int rows) }; if (!r.cells) { t->spare = new_scr; - term_gstore_unpin(t->gstore); + wsap_vtcore_gstore_unpin(t->gstore); return false; } /* The open history line reflows with the screen. It is read here and * removed only after the reflow succeeds. The prompt line never rewraps. */ if (hist_is_open(&t->hist) && t->prompt_y != 0) { - uint64_t n = term_hist_line_cells(&t->hist, hist_end_id(&t->hist) - 1); + uint64_t n = wsap_vtcore_hist_line_cells(&t->hist, hist_end_id(&t->hist) - 1); struct cell *pulled = n ? grow(&t->resize.pull, n * sizeof(*pulled)) : NULL; if (n && !pulled) goto reflow_failed; - int got = term_hist_peek_last_line(&t->hist, pulled, pulled ? (int)n : 0, t->gstore); + int got = wsap_vtcore_hist_peek_last_line(&t->hist, pulled, pulled ? (int)n : 0, t->gstore); if (pulled && got > 0 && !reflow_append(&r, pulled, got, -1, -1)) goto reflow_failed; } @@ -1425,14 +1425,14 @@ static bool term_resize_internal(struct term *t, int cols, int rows) if (len > src->len && src->len < old_cols && !(src->flags & RF_TAIL_PADDED)) { fill_cells(src->cells + src->len, old_cols - src->len, - TERM_COLOR_FG, TERM_COLOR_BG); + WSAP_VTCORE_COLOR_FG, WSAP_VTCORE_COLOR_BG); src->flags |= RF_TAIL_PADDED; } const struct cell *in_cells = cells; if (len > old_cols && line_buf && (size_t)len <= line_cap) { memcpy(line_buf, cells, (size_t)old_cols * sizeof(struct cell)); - fill_cells(line_buf + old_cols, len - old_cols, TERM_COLOR_FG, TERM_COLOR_BG); + fill_cells(line_buf + old_cols, len - old_cols, WSAP_VTCORE_COLOR_FG, WSAP_VTCORE_COLOR_BG); in_cells = line_buf; } else if (len > old_cols) { len = old_cols; @@ -1453,11 +1453,11 @@ static bool term_resize_internal(struct term *t, int cols, int rows) * nothing below can fail. */ if (hist_is_open(&t->hist)) { if (t->prompt_y == 0) - term_hist_close_open_line(&t->hist); + wsap_vtcore_hist_close_open_line(&t->hist); else - term_hist_pop_line(&t->hist, NULL, 0, t->gstore); + wsap_vtcore_hist_pop_line(&t->hist, NULL, 0, t->gstore); } - term_hist_set_width(&t->hist, cols); + wsap_vtcore_hist_set_width(&t->hist, cols); int visible = rows - (cols > old_cols ? old_rows - active : 0); if (visible < 1) @@ -1471,7 +1471,7 @@ static bool term_resize_internal(struct term *t, int cols, int rows) screen_start = r.cursor_y; for (int i = 0; i < screen_start; i++) { int push_len = (r.out[i].wrapped && r.out[i].len < cols) ? cols : r.out[i].len; - term_hist_push_row(&t->hist, reflow_out_row_cells(&r, i), push_len, + wsap_vtcore_hist_push_row(&t->hist, reflow_out_row_cells(&r, i), push_len, r.out[i].wrapped, t->gstore); } } else if (r.count < visible && t->hist.lines > 0) { @@ -1487,7 +1487,7 @@ static bool term_resize_internal(struct term *t, int cols, int rows) int len = r.out[i].len; memcpy(dst->cells, reflow_out_row_cells(&r, i), (size_t)len * sizeof(struct cell)); if (len < cols) - fill_cells(dst->cells + len, cols - len, TERM_COLOR_FG, TERM_COLOR_BG); + fill_cells(dst->cells + len, cols - len, WSAP_VTCORE_COLOR_FG, WSAP_VTCORE_COLOR_BG); dst->kind = ROW_CELLS; dst->len = (uint16_t)len; dst->flags = (r.out[i].wrapped ? RF_WRAPPED : 0) | RF_TAIL_PADDED; @@ -1530,28 +1530,28 @@ static bool term_resize_internal(struct term *t, int cols, int rows) t->scroll_bottom = rows - 1; t->left_margin = 0; t->right_margin = cols - 1; - term_hist_view_rewrap(&t->view, &t->hist, cols); + wsap_vtcore_hist_view_rewrap(&t->view, &t->hist, cols); t->full_gen++; - term_gstore_unpin(t->gstore); + wsap_vtcore_gstore_unpin(t->gstore); return true; reflow_failed: t->spare = new_scr; - term_gstore_unpin(t->gstore); + wsap_vtcore_gstore_unpin(t->gstore); return false; } -bool term_resize(struct term *t, struct term_vector2i size) +bool wsap_vtcore_resize(struct wsap_vtcore *t, struct wsap_vtcore_vector2i size) { if (!t) return false; int cols = size.x; int rows = size.y; - if (cols < 1 || rows < 1 || cols > TERM_MAX_COLS) + if (cols < 1 || rows < 1 || cols > WSAP_VTCORE_MAX_COLS) return false; - term_write_begin(t); + wsap_vtcore_write_begin(t); if (cols == t->cols && rows == t->rows) { - term_write_end(t); + wsap_vtcore_write_end(t); return true; } @@ -1559,7 +1559,7 @@ bool term_resize(struct term *t, struct term_vector2i size) int new_cap = cols + 64; uint8_t *new_tabs = realloc(t->tabs, (size_t)new_cap); if (!new_tabs) { - term_write_end(t); + wsap_vtcore_write_end(t); return false; } memset(new_tabs + t->tabs_cap, 0, (size_t)(new_cap - t->tabs_cap)); @@ -1579,7 +1579,7 @@ bool term_resize(struct term *t, struct term_vector2i size) int new_cap = cols + 64; struct cell *new_scratch = realloc(t->view_row_scratch, (size_t)new_cap * sizeof(struct cell)); if (!new_scratch) { - term_write_end(t); + wsap_vtcore_write_end(t); return false; } memset(new_scratch + t->view_scratch_cap, 0, (size_t)(new_cap - t->view_scratch_cap) * sizeof(struct cell)); @@ -1587,7 +1587,7 @@ bool term_resize(struct term *t, struct term_vector2i size) t->view_scratch_cap = new_cap; } - bool ok = term_resize_internal(t, cols, rows); - term_write_end(t); + bool ok = wsap_vtcore_resize_internal(t, cols, rows); + wsap_vtcore_write_end(t); return ok; } |
