diff options
| author | sl0p <sl0p@sl0p.foo> | 2026-08-13 22:00:03 +0200 |
|---|---|---|
| committer | sl0p <sl0p@sl0p.foo> | 2026-08-13 22:00:03 +0200 |
| commit | 2c1b855c3cf8cf8841bf25cd939e478a7f0bfac8 (patch) | |
| tree | 7cd6274303ba1da1f8bd57371aec336dd687fb58 /src/pane.c | |
| download | slosh-2c1b855c3cf8cf8841bf25cd939e478a7f0bfac8.tar.gz slosh-2c1b855c3cf8cf8841bf25cd939e478a7f0bfac8.tar.xz slosh-2c1b855c3cf8cf8841bf25cd939e478a7f0bfac8.zip | |
sl0ptty: M0 — libghostty-vt vendored, one pane, compositor, diff renderer
Design contract in DESIGN.md. libghostty-vt vendored fresh from ghostty
main@bedb6f2 (zig 0.16), built to a 14MB static lib.
M0: pty spawn (no libutil), a lib-vt terminal per pane, a screen cell buffer,
a diff emitter that repaints only what changed, raw mode, SIGWINCH, and a
~120Hz frame cap that coalesces pty reads.
Also the seed of the headless harness: --headless dumps the composited screen
without a tty, so composition is testable in milliseconds.
tests/live_m0.py covers what headless cannot: raw mode, the diff emitter,
resize propagation, and the quit path. 6/6 green.
Diffstat (limited to 'src/pane.c')
| -rw-r--r-- | src/pane.c | 265 |
1 files changed, 265 insertions, 0 deletions
diff --git a/src/pane.c b/src/pane.c new file mode 100644 index 0000000..ffa2aa4 --- /dev/null +++ b/src/pane.c @@ -0,0 +1,265 @@ +/* A pane: one pty, one libghostty-vt terminal, and the code that composites + * its viewport into the screen. */ +#define _GNU_SOURCE +#include "sl0ptty.h" + +#include <errno.h> +#include <ghostty/vt.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +struct pane { + pty_t pty; + GhosttyTerminal term; + GhosttyRenderState rstate; + GhosttyRenderStateRowIterator rows; + GhosttyRenderStateRowCells cells; + uint16_t cols, rows_n; + bool alive; + bool dirty; + char title[256]; +}; + +/* Terminal replies (DA, cursor position, XTVERSION...) go back to the app. */ +static void on_write_pty(GhosttyTerminal t, void *ud, const uint8_t *data, + size_t len) { + pane_t *p = ud; + if (!p || p->pty.fd < 0) return; + size_t off = 0; + while (off < len) { + ssize_t n = write(p->pty.fd, data + off, len - off); + if (n <= 0) break; + off += (size_t)n; + } +} + +static void on_title_changed(GhosttyTerminal t, void *ud) { + pane_t *p = ud; + GhosttyString s = {0}; + if (ghostty_terminal_get(p->term, GHOSTTY_TERMINAL_DATA_TITLE, &s) != + GHOSTTY_SUCCESS) + return; + size_t n = s.len < sizeof p->title - 1 ? s.len : sizeof p->title - 1; + memcpy(p->title, s.ptr, n); + p->title[n] = 0; + p->dirty = true; +} + +pane_t *pane_new(const char *const argv[], uint16_t cols, uint16_t rows, + const char *cwd) { + pane_t *p = calloc(1, sizeof *p); + p->cols = cols; + p->rows_n = rows; + p->pty.fd = -1; + + if (ghostty_terminal_new(NULL, &p->term, cols, rows) != GHOSTTY_SUCCESS) + goto fail; + if (ghostty_render_state_new(NULL, &p->rstate) != GHOSTTY_SUCCESS) goto fail; + if (ghostty_render_state_row_iterator_new(NULL, &p->rows) != GHOSTTY_SUCCESS) + goto fail; + if (ghostty_render_state_row_cells_new(NULL, &p->cells) != GHOSTTY_SUCCESS) + goto fail; + + void *self = p; + GhosttyTerminalWritePtyFn wfn = on_write_pty; + GhosttyTerminalTitleChangedFn tfn = on_title_changed; + ghostty_terminal_set(p->term, GHOSTTY_TERMINAL_OPT_USERDATA, &self); + ghostty_terminal_set(p->term, GHOSTTY_TERMINAL_OPT_WRITE_PTY, &wfn); + ghostty_terminal_set(p->term, GHOSTTY_TERMINAL_OPT_TITLE_CHANGED, &tfn); + + if (pty_spawn(&p->pty, argv, cols, rows, cwd) != 0) goto fail; + p->alive = true; + p->dirty = true; + return p; + +fail: + pane_free(p); + return NULL; +} + +void pane_free(pane_t *p) { + if (!p) return; + if (p->pty.fd >= 0) pty_close(&p->pty); + if (p->cells) ghostty_render_state_row_cells_free(p->cells); + if (p->rows) ghostty_render_state_row_iterator_free(p->rows); + if (p->rstate) ghostty_render_state_free(p->rstate); + if (p->term) ghostty_terminal_free(p->term); + free(p); +} + +int pane_fd(const pane_t *p) { return p->pty.fd; } +bool pane_alive(const pane_t *p) { return p->alive; } +bool pane_dirty(pane_t *p) { return p->dirty; } +const char *pane_title(const pane_t *p) { return p->title; } + +ssize_t pane_pump(pane_t *p) { + uint8_t buf[65536]; + ssize_t total = 0; + for (;;) { + ssize_t n = read(p->pty.fd, buf, sizeof buf); + if (n > 0) { + /* M4: a side-channel scanner for OSC 5577 hooks in here, before the + * bytes reach the terminal. */ + ghostty_terminal_vt_write(p->term, buf, (size_t)n); + p->dirty = true; + total += n; + if ((size_t)n < sizeof buf) break; /* drained */ + continue; + } + if (n == 0) { + p->alive = false; + return 0; + } + if (errno == EAGAIN || errno == EWOULDBLOCK) break; + if (errno == EINTR) continue; + p->alive = false; + return -1; + } + return total; +} + +void pane_write(pane_t *p, const void *buf, size_t len) { + if (p->pty.fd < 0) return; + size_t off = 0; + while (off < len) { + ssize_t n = write(p->pty.fd, (const char *)buf + off, len - off); + if (n <= 0) { + if (n < 0 && (errno == EINTR || errno == EAGAIN)) continue; + break; + } + off += (size_t)n; + } +} + +void pane_resize(pane_t *p, uint16_t cols, uint16_t rows) { + if (cols == p->cols && rows == p->rows_n) return; + p->cols = cols; + p->rows_n = rows; + ghostty_terminal_resize(p->term, cols, rows, 0, 0); + pty_resize(&p->pty, cols, rows); + p->dirty = true; +} + +static color_t to_color(const GhosttyColorRgb *c, bool ok) { + color_t out = {0}; + if (ok) { + out.set = true; + out.r = c->r; + out.g = c->g; + out.b = c->b; + } + return out; +} + +void pane_compose(pane_t *p, screen_t *s, uint16_t x0, uint16_t y0, + bool focused) { + if (ghostty_render_state_update(p->rstate, p->term) != GHOSTTY_SUCCESS) return; + + if (ghostty_render_state_get(p->rstate, GHOSTTY_RENDER_STATE_DATA_ROW_ITERATOR, + &p->rows) != GHOSTTY_SUCCESS) + return; + + uint16_t y = 0; + while (ghostty_render_state_row_iterator_next(p->rows)) { + if (y >= p->rows_n) break; + if (ghostty_render_state_row_get(p->rows, GHOSTTY_RENDER_STATE_ROW_DATA_CELLS, + &p->cells) != GHOSTTY_SUCCESS) { + y++; + continue; + } + + uint16_t x = 0; + while (ghostty_render_state_row_cells_next(p->cells)) { + if (x >= p->cols) break; + cell_t *dst = screen_at(s, (uint16_t)(x0 + x), (uint16_t)(y0 + y)); + if (!dst) break; + + GhosttyCell raw = {0}; + GhosttyCellWide wide = GHOSTTY_CELL_WIDE_NARROW; + if (ghostty_render_state_row_cells_get( + p->cells, GHOSTTY_RENDER_STATE_ROW_CELLS_DATA_RAW, &raw) == + GHOSTTY_SUCCESS) + ghostty_cell_get(raw, GHOSTTY_CELL_DATA_WIDE, &wide); + + if (wide == GHOSTTY_CELL_WIDE_SPACER_TAIL) { + dst->width = 0; + dst->len = 0; + x++; + continue; + } + + char utf8[16] = {0}; + GhosttyBuffer gb = {.ptr = (uint8_t *)utf8, .cap = sizeof utf8, .len = 0}; + if (ghostty_render_state_row_cells_get( + p->cells, GHOSTTY_RENDER_STATE_ROW_CELLS_DATA_GRAPHEMES_UTF8, + &gb) != GHOSTTY_SUCCESS) + gb.len = 0; + + GhosttyColorRgb fg = {0}, bg = {0}; + bool have_fg = ghostty_render_state_row_cells_get( + p->cells, GHOSTTY_RENDER_STATE_ROW_CELLS_DATA_FG_COLOR, + &fg) == GHOSTTY_SUCCESS; + bool have_bg = ghostty_render_state_row_cells_get( + p->cells, GHOSTTY_RENDER_STATE_ROW_CELLS_DATA_BG_COLOR, + &bg) == GHOSTTY_SUCCESS; + + uint16_t attrs = 0; + bool styled = false; + if (ghostty_render_state_row_cells_get( + p->cells, GHOSTTY_RENDER_STATE_ROW_CELLS_DATA_HAS_STYLING, + &styled) == GHOSTTY_SUCCESS && + styled) { + GhosttyStyle st = GHOSTTY_INIT_SIZED(GhosttyStyle); + if (ghostty_render_state_row_cells_get( + p->cells, GHOSTTY_RENDER_STATE_ROW_CELLS_DATA_STYLE, &st) == + GHOSTTY_SUCCESS) { + if (st.bold) attrs |= ATTR_BOLD; + if (st.faint) attrs |= ATTR_DIM; + if (st.italic) attrs |= ATTR_ITALIC; + if (st.underline) attrs |= ATTR_UNDERLINE; + if (st.blink) attrs |= ATTR_BLINK; + if (st.inverse) attrs |= ATTR_INVERSE; + if (st.invisible) attrs |= ATTR_INVISIBLE; + if (st.strikethrough) attrs |= ATTR_STRIKE; + } + } + + size_t n = gb.len < sizeof dst->text ? gb.len : sizeof dst->text; + memset(dst->text, 0, sizeof dst->text); + if (n) memcpy(dst->text, utf8, n); + else { dst->text[0] = ' '; n = 1; } + dst->len = (uint8_t)n; + dst->width = wide == GHOSTTY_CELL_WIDE_WIDE ? 2 : 1; + dst->attrs = attrs; + dst->fg = to_color(&fg, have_fg); + dst->bg = to_color(&bg, have_bg); + x++; + } + y++; + } + + if (focused) { + bool vis = false, has_pos = false; + uint16_t cxp = 0, cyp = 0; + ghostty_render_state_get(p->rstate, GHOSTTY_RENDER_STATE_DATA_CURSOR_VISIBLE, + &vis); + ghostty_render_state_get( + p->rstate, GHOSTTY_RENDER_STATE_DATA_CURSOR_VIEWPORT_HAS_VALUE, &has_pos); + if (vis && has_pos) { + ghostty_render_state_get(p->rstate, + GHOSTTY_RENDER_STATE_DATA_CURSOR_VIEWPORT_X, &cxp); + ghostty_render_state_get(p->rstate, + GHOSTTY_RENDER_STATE_DATA_CURSOR_VIEWPORT_Y, &cyp); + s->cursor_visible = true; + s->cursor_x = (uint16_t)(x0 + cxp); + s->cursor_y = (uint16_t)(y0 + cyp); + } else { + s->cursor_visible = false; + } + } + + GhosttyRenderStateDirty clean = GHOSTTY_RENDER_STATE_DIRTY_FALSE; + ghostty_render_state_set(p->rstate, GHOSTTY_RENDER_STATE_OPTION_DIRTY, &clean); + p->dirty = false; +} |
