aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authoruser <user@clank>2026-08-14 23:13:56 +0200
committeruser <user@clank>2026-08-14 23:13:56 +0200
commit4b380e9b4cbd24dca99bef4991541aa1aece847f (patch)
treefcd41dfdf23f17027523336dbe448ee683e72c16 /src
parentvendor: keep kitty images across a screen clear (diff)
downloadslosh-4b380e9b4cbd24dca99bef4991541aa1aece847f.tar.gz
slosh-4b380e9b4cbd24dca99bef4991541aa1aece847f.tar.xz
slosh-4b380e9b4cbd24dca99bef4991541aa1aece847f.zip
sl0ppty: carry the terminal's cell size, so images can have a size
"Kitty graphics pass through" was true of the tests and not of anything else. Every test transmitted and placed in one command with explicit c=/r=; real programs upload once with `a=t` and place per frame with `a=p`, saying nothing about cells, and that path was broken from end to end. A placement that does not say how many cells it covers is sized from the image's pixels and the cell size. We never had a cell size: - pty_spawn and pty_resize left ws_xpixel/ws_ypixel at zero, so a program asking its own tty how big a cell is got nothing and fell back to guessing; - ghostty_terminal_resize was called with 0, 0 for the cell size, so lib-vt computed zero cells for such a placement -- and a placement covering zero cells is skipped, which on screen is an image that simply is not there. So the size now runs the whole way through. The client reads it from TIOCGWINSZ (re-read on every resize, because a window can move to a monitor with different metrics), MSG_HELLO and MSG_RESIZE carry it -- a 4-byte payload still means "I did not say", so an older client still attaches -- the app hands it to every pane in every tab, and each pane gives it to both its pty and its terminal. Where nobody knows, the default is 8x16 rather than 0. That is the whole lesson of this bug: a plausible cell size means an image appears at roughly the right size, and a zero means it does not appear at all. `{"cmd":"resize"}` takes optional cell_w/cell_h, so a script can be a client that knows its own metrics, and so the tests can be one. The tests now use the protocol the way programs use it, including a check that a program can read real pixel dimensions out of its own pty -- the thing that was silently zero. The remaining half of the fix is one layer down, in vendor/patches/0001.
Diffstat (limited to 'src')
-rw-r--r--src/app.c34
-rw-r--r--src/app.h6
-rw-r--r--src/client.c32
-rw-r--r--src/cmd.c6
-rw-r--r--src/pane.c31
-rw-r--r--src/proto.h9
-rw-r--r--src/pty.c19
-rw-r--r--src/server.c8
-rw-r--r--src/sl0ppty.h12
9 files changed, 135 insertions, 22 deletions
diff --git a/src/app.c b/src/app.c
index 3ea5a55..d1a8fcd 100644
--- a/src/app.c
+++ b/src/app.c
@@ -180,6 +180,12 @@ struct app {
uint32_t next_tab_id;
uint32_t next_id;
uint16_t cols, rows;
+ /* The client's cell size in pixels, as reported when it attached. Panes are
+ * told so that a program can size an image, and so lib-vt can work out how
+ * many cells an image covers when the program did not say. Defaulted rather
+ * than zeroed: 0 means "no image can be placed", which is not a good thing
+ * for a headless session or a terminal that will not say. */
+ uint16_t cell_w, cell_h;
bool prefix;
bool quit;
bool detach;
@@ -675,6 +681,7 @@ static node_t *leaf_new_ex(app_t *a, const char *const argv[], const char *cwd,
pane_set_osc_handler(p, on_pane_osc, a);
pane_set_clipboard_handler(p, on_pane_clipboard, a);
pane_set_notify_handler(p, on_pane_notify, a);
+ pane_set_cell_px(p, a->cell_w, a->cell_h);
return n;
}
@@ -689,6 +696,7 @@ static node_t *leaf_new(app_t *a) {
pane_set_osc_handler(p, on_pane_osc, a);
pane_set_clipboard_handler(p, on_pane_clipboard, a);
pane_set_notify_handler(p, on_pane_notify, a);
+ pane_set_cell_px(p, a->cell_w, a->cell_h);
return n;
}
@@ -735,6 +743,11 @@ app_t *app_new(const char *const argv[], uint16_t cols, uint16_t rows) {
a->argv = argv;
a->cols = cols;
a->rows = rows;
+ /* A plausible cell until a client says otherwise, because zero is not a
+ * neutral default here: it is the value that makes every naturally-sized
+ * image cover no cells and quietly not appear. */
+ a->cell_w = 8;
+ a->cell_h = 16;
a->gfx = gfx_new();
tab_add(a, "");
a->cur = 0;
@@ -1205,6 +1218,27 @@ void app_resize(app_t *a, uint16_t cols, uint16_t rows) {
layout(a);
}
+static void set_cell_px_cb(node_t *n, void *ud) {
+ const uint16_t *px = ud;
+ pane_set_cell_px(n->pane, px[0], px[1]);
+}
+
+void app_set_cell_px(app_t *a, uint16_t w, uint16_t h) {
+ if (!w || !h || (w == a->cell_w && h == a->cell_h)) return;
+ a->cell_w = w;
+ a->cell_h = h;
+ uint16_t px[2] = {w, h};
+ /* Every pane, not just the visible ones: a background tab's program may be
+ * drawing images too, and it should not have to be looked at to find out
+ * how big a cell is. */
+ walk_all(a, set_cell_px_cb, px);
+}
+
+void app_cell_px(const app_t *a, uint16_t *w, uint16_t *h) {
+ if (w) *w = a->cell_w;
+ if (h) *h = a->cell_h;
+}
+
/* ---- tree edits --------------------------------------------------------- */
static void replace_child(node_t *parent, node_t *old, node_t *new_) {
diff --git a/src/app.h b/src/app.h
index e118691..f220eb6 100644
--- a/src/app.h
+++ b/src/app.h
@@ -42,6 +42,12 @@ char *app_take_clipboard(app_t *a);
/* Re-read the config file. Keeps the working one if the new file is broken. */
bool app_reload_config(char *err, size_t errcap);
void app_resize(app_t *a, uint16_t cols, uint16_t rows);
+/* The attached client's cell size in pixels, passed on to every pane's pty and
+ * terminal. Images need it: a placement that does not say how many cells it
+ * covers is sized from the image's pixels and this. Ignored if either is 0,
+ * so a client that cannot find out leaves the default standing. */
+void app_set_cell_px(app_t *a, uint16_t w, uint16_t h);
+void app_cell_px(const app_t *a, uint16_t *w, uint16_t *h);
/* Pump every pane that has data. Returns true if anything changed. */
bool app_pump_fd(app_t *a, int fd);
diff --git a/src/client.c b/src/client.c
index a3ad73d..2c22af9 100644
--- a/src/client.c
+++ b/src/client.c
@@ -41,20 +41,32 @@ static void restore(void) {
(void)r;
}
-static void term_size(uint16_t *cols, uint16_t *rows) {
+/* The terminal's size, in cells and — when it will say — in pixels per cell.
+ * Most terminals fill in the pixel fields; the ones that do not report zero,
+ * which is passed on as "I do not know" rather than as a size. */
+static void term_size(uint16_t *cols, uint16_t *rows, uint16_t *cell_w,
+ uint16_t *cell_h) {
struct winsize ws;
+ *cell_w = *cell_h = 0;
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == 0 && ws.ws_col && ws.ws_row) {
*cols = ws.ws_col;
*rows = ws.ws_row;
+ if (ws.ws_xpixel && ws.ws_ypixel) {
+ *cell_w = (uint16_t)(ws.ws_xpixel / ws.ws_col);
+ *cell_h = (uint16_t)(ws.ws_ypixel / ws.ws_row);
+ }
} else {
*cols = 80;
*rows = 24;
}
}
-static void send_size(int fd, uint8_t type, uint16_t cols, uint16_t rows) {
- uint8_t b[4] = {(uint8_t)(cols >> 8), (uint8_t)cols, (uint8_t)(rows >> 8),
- (uint8_t)rows};
+static void send_size(int fd, uint8_t type, uint16_t cols, uint16_t rows,
+ uint16_t cell_w, uint16_t cell_h) {
+ uint8_t b[8] = {(uint8_t)(cols >> 8), (uint8_t)cols,
+ (uint8_t)(rows >> 8), (uint8_t)rows,
+ (uint8_t)(cell_w >> 8), (uint8_t)cell_w,
+ (uint8_t)(cell_h >> 8), (uint8_t)cell_h};
msg_send(fd, type, b, sizeof b);
}
@@ -71,8 +83,8 @@ static void write_all(int fd, const void *buf, size_t len) {
}
int client_run(int fd) {
- uint16_t cols, rows;
- term_size(&cols, &rows);
+ uint16_t cols, rows, cell_w, cell_h;
+ term_size(&cols, &rows, &cell_w, &cell_h);
if (tcgetattr(STDIN_FILENO, &g_saved) != 0) {
fprintf(stderr, "sl0ppty: not a terminal\n");
@@ -93,7 +105,7 @@ int client_run(int fd) {
sigaction(SIGHUP, &sa, NULL);
signal(SIGPIPE, SIG_IGN);
- send_size(fd, MSG_HELLO, cols, rows);
+ send_size(fd, MSG_HELLO, cols, rows, cell_w, cell_h);
msg_reader_t reader;
msg_reader_init(&reader);
@@ -108,8 +120,10 @@ int client_run(int fd) {
if (g_winch) {
g_winch = 0;
- term_size(&cols, &rows);
- send_size(fd, MSG_RESIZE, cols, rows);
+ /* A resize can also be a move to another monitor, so the cell size is
+ * re-read rather than remembered from the first frame. */
+ term_size(&cols, &rows, &cell_w, &cell_h);
+ send_size(fd, MSG_RESIZE, cols, rows, cell_w, cell_h);
}
if (n < 0) {
if (errno == EINTR) continue;
diff --git a/src/cmd.c b/src/cmd.c
index f23c6f4..2292b52 100644
--- a/src/cmd.c
+++ b/src/cmd.c
@@ -127,6 +127,12 @@ static char *cmd_json(app_t *a, screen_t *s, input_parser_t *in,
if (!c || !r) return jerr("bad size");
screen_resize(s, c, r);
app_resize(a, c, r);
+ /* The cell size in pixels, which a real client reports when it attaches.
+ * Here so that a script -- and the test suite -- can be a client that
+ * knows its own metrics, since images are sized against these. */
+ uint16_t cw = (uint16_t)jv_geti(req, "cell_w", 0);
+ uint16_t ch = (uint16_t)jv_geti(req, "cell_h", 0);
+ app_set_cell_px(a, cw, ch);
return jok_int(NULL, 0);
}
if (strcmp(cmd, "split") == 0) {
diff --git a/src/pane.c b/src/pane.c
index eac7f92..38df2ed 100644
--- a/src/pane.c
+++ b/src/pane.c
@@ -26,6 +26,12 @@ struct pane {
GhosttyMouseEncoder menc;
GhosttyMouseEvent mev;
uint16_t cols, rows_n;
+ /* The client's cell size in pixels. A default rather than a zero, because
+ * zero is what makes an image vanish: a placement given no explicit cell
+ * count is sized from the image's pixels and this, and 0 covers no cells.
+ * 8x16 is a plausible terminal cell, and any client that knows better says
+ * so the moment it attaches. */
+ uint16_t cell_w, cell_h;
bool alive;
/* How the program ended. `exit_known` is separate from `!alive` because the
* two really are different: EOF on the master says the pane is over, and
@@ -225,7 +231,7 @@ bool pane_start(pane_t *p) {
if (!p->suspended) return false;
p->suspended = false;
if (pty_spawn(&p->pty, (const char *const *)p->argv, p->cols, p->rows_n,
- p->cwd) != 0) {
+ p->cwd, p->cell_w, p->cell_h) != 0) {
p->alive = false;
return false;
}
@@ -263,6 +269,8 @@ pane_t *pane_new(const char *const argv[], uint16_t cols, uint16_t rows,
pane_t *p = calloc(1, sizeof *p);
p->cols = cols;
p->rows_n = rows;
+ p->cell_w = 8;
+ p->cell_h = 16;
p->pty.fd = -1;
if (ghostty_terminal_new(NULL, &p->term, cols, rows) != GHOSTTY_SUCCESS)
@@ -313,7 +321,8 @@ pane_t *pane_new(const char *const argv[], uint16_t cols, uint16_t rows,
p->argv = argv_dup(argv);
p->cwd = cwd ? strdup(cwd) : NULL;
- if (pty_spawn(&p->pty, argv, cols, rows, cwd) != 0) goto fail;
+ if (pty_spawn(&p->pty, argv, cols, rows, cwd, p->cell_w, p->cell_h) != 0)
+ goto fail;
p->alive = true;
p->dirty = true;
return p;
@@ -423,7 +432,7 @@ bool pane_restart(pane_t *p) {
p->exit_code = 0;
if (pty_spawn(&p->pty, (const char *const *)p->argv, p->cols, p->rows_n,
- p->cwd) != 0)
+ p->cwd, p->cell_w, p->cell_h) != 0)
return false;
/* The terminal is deliberately not cleared: the run that ended, and the
* line saying it ended, stay above this one in the scrollback. That is the
@@ -789,8 +798,20 @@ 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);
- if (p->pty.fd >= 0) pty_resize(&p->pty, cols, rows);
+ /* The cell size goes in every time: it is what lets lib-vt work out how
+ * many cells an image covers when the program did not say. Passing 0 here
+ * is why kitty graphics looked implemented and drew nothing. */
+ ghostty_terminal_resize(p->term, cols, rows, p->cell_w, p->cell_h);
+ if (p->pty.fd >= 0) pty_resize(&p->pty, cols, rows, p->cell_w, p->cell_h);
+ p->dirty = true;
+}
+
+void pane_set_cell_px(pane_t *p, uint16_t w, uint16_t h) {
+ if (!w || !h || (w == p->cell_w && h == p->cell_h)) return;
+ p->cell_w = w;
+ p->cell_h = h;
+ ghostty_terminal_resize(p->term, p->cols, p->rows_n, w, h);
+ if (p->pty.fd >= 0) pty_resize(&p->pty, p->cols, p->rows_n, w, h);
p->dirty = true;
}
diff --git a/src/proto.h b/src/proto.h
index ac68d44..3f39d0e 100644
--- a/src/proto.h
+++ b/src/proto.h
@@ -11,10 +11,15 @@
#include <stddef.h>
#include <stdint.h>
+/* MSG_HELLO and MSG_RESIZE carry u16 cols, u16 rows, and since the graphics
+ * fix two more: u16 cell_w, u16 cell_h, the client's cell size in pixels.
+ * A four-byte payload is still accepted and means "I did not say", which is
+ * what an older client sends and what a terminal that reports no pixel size
+ * leads to. */
enum {
- MSG_HELLO = 1, /* client -> server: u16 cols, u16 rows */
+ MSG_HELLO = 1, /* client -> server: u16 cols, rows[, cell_w, cell_h] */
MSG_INPUT = 2, /* client -> server: raw bytes from the terminal */
- MSG_RESIZE = 3, /* client -> server: u16 cols, u16 rows */
+ MSG_RESIZE = 3, /* client -> server: u16 cols, rows[, cell_w, cell_h] */
MSG_DETACH = 4, /* client -> server: leave, but keep running */
MSG_OUTPUT = 5, /* server -> client: bytes for the terminal */
MSG_EXIT = 6, /* server -> client: u8 reason */
diff --git a/src/pty.c b/src/pty.c
index 09d7ea6..59448df 100644
--- a/src/pty.c
+++ b/src/pty.c
@@ -15,7 +15,7 @@
#include <unistd.h>
int pty_spawn(pty_t *p, const char *const argv[], uint16_t cols, uint16_t rows,
- const char *cwd) {
+ const char *cwd, uint16_t cell_w, uint16_t cell_h) {
int master = posix_openpt(O_RDWR | O_NOCTTY);
if (master < 0) return -1;
if (grantpt(master) < 0 || unlockpt(master) < 0) {
@@ -29,7 +29,14 @@ int pty_spawn(pty_t *p, const char *const argv[], uint16_t cols, uint16_t rows,
return -1;
}
- struct winsize ws = {.ws_col = cols, .ws_row = rows};
+ /* The pixel fields matter: a program drawing images asks the tty how big a
+ * cell is, and a winsize that says zero is a program that cannot size an
+ * image. They are the *terminal's* pixels, so they are cols/rows times the
+ * cell size the client reported. */
+ struct winsize ws = {.ws_col = cols,
+ .ws_row = rows,
+ .ws_xpixel = (unsigned short)(cols * cell_w),
+ .ws_ypixel = (unsigned short)(rows * cell_h)};
ioctl(master, TIOCSWINSZ, &ws);
pid_t pid = fork();
@@ -71,8 +78,12 @@ int pty_spawn(pty_t *p, const char *const argv[], uint16_t cols, uint16_t rows,
return 0;
}
-int pty_resize(pty_t *p, uint16_t cols, uint16_t rows) {
- struct winsize ws = {.ws_col = cols, .ws_row = rows};
+int pty_resize(pty_t *p, uint16_t cols, uint16_t rows, uint16_t cell_w,
+ uint16_t cell_h) {
+ struct winsize ws = {.ws_col = cols,
+ .ws_row = rows,
+ .ws_xpixel = (unsigned short)(cols * cell_w),
+ .ws_ypixel = (unsigned short)(rows * cell_h)};
return ioctl(p->fd, TIOCSWINSZ, &ws);
}
diff --git a/src/server.c b/src/server.c
index 9bddd0d..498b83f 100644
--- a/src/server.c
+++ b/src/server.c
@@ -346,6 +346,14 @@ int server_run(const char *name, const char *const argv[], uint16_t cols,
screen_resize(&s.screen, c, rr);
app_resize(s.app, c, rr);
}
+ /* The cell size arrived with it, from a client new enough to
+ * send one and a terminal willing to say. Zero means neither,
+ * and app_set_cell_px leaves the default standing. */
+ if (m.len >= 8) {
+ uint16_t cw = (uint16_t)(m.data[4] << 8 | m.data[5]);
+ uint16_t ch = (uint16_t)(m.data[6] << 8 | m.data[7]);
+ app_set_cell_px(s.app, cw, ch);
+ }
}
s.screen.force_full = true; /* a fresh client knows nothing */
app_graphics_reset(s.app); /* including any image we had sent */
diff --git a/src/sl0ppty.h b/src/sl0ppty.h
index f7b7ee6..2e56577 100644
--- a/src/sl0ppty.h
+++ b/src/sl0ppty.h
@@ -84,9 +84,13 @@ typedef struct {
pid_t pid;
} pty_t;
+/* `cell_w`/`cell_h` are the client's cell size in pixels, so the pty's
+ * winsize carries real pixel dimensions: a program that draws images asks the
+ * tty how big a cell is, and zeroes there mean it cannot size one. */
int pty_spawn(pty_t *p, const char *const argv[], uint16_t cols, uint16_t rows,
- const char *cwd);
-int pty_resize(pty_t *p, uint16_t cols, uint16_t rows);
+ const char *cwd, uint16_t cell_w, uint16_t cell_h);
+int pty_resize(pty_t *p, uint16_t cols, uint16_t rows, uint16_t cell_w,
+ uint16_t cell_h);
void pty_close(pty_t *p);
/* ---- pane: a pty + a libghostty-vt terminal ---------------------------- */
@@ -144,6 +148,10 @@ void pane_send_key(pane_t *p, const input_event_t *ev);
void pane_send_mouse(pane_t *p, const input_event_t *ev);
void pane_send_paste(pane_t *p, const char *text, size_t len);
void pane_resize(pane_t *p, uint16_t cols, uint16_t rows);
+/* The client's cell size in pixels. Everything about images depends on it:
+ * without it a placement that asks for its natural size covers zero cells and
+ * is dropped, which is a picture that silently does not appear. */
+void pane_set_cell_px(pane_t *p, uint16_t w, uint16_t h);
bool pane_dirty(pane_t *p);
/* Scrollback. Negative delta scrolls up (towards older output). */
void pane_scroll(pane_t *p, int delta);