aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authoruser <user@clank>2026-08-15 19:38:38 +0200
committeruser <user@clank>2026-08-15 19:38:38 +0200
commite275a776406343a3a0476d557a5d8c724ccf3593 (patch)
tree79c5bdebe41461c361fb1e823a2e9a53276df342 /src
parentsl0ppty: decode the PNGs programs actually send (diff)
downloadslosh-e275a776406343a3a0476d557a5d8c724ccf3593.tar.gz
slosh-e275a776406343a3a0476d557a5d8c724ccf3593.tar.xz
slosh-e275a776406343a3a0476d557a5d8c724ccf3593.zip
sl0ppty: answer the terminal when it is asked how big it is
XTWINOPS (CSI 14/16/18 t) went unanswered. A program that draws images needs the cell size to keep an aspect ratio; the pty's pixel fields are the first way to get it (D17) and this query is the second, which is what a program reaches for when those come back zeroed -- as they do on the far side of an ssh hop. Unanswered, the query times out and the program falls back to a guess: ida-tui guesses 10x20, and against a real 9x22 cell that is every image stretched by a fifth, which looks like a bug in the program rather than in us. Found alongside the png bug and from the same program, which asks `CSI 16 t` in the same round trip as its graphics query -- so once the png decoded, the picture would have appeared at the wrong shape. lib-vt encodes and writes the reply once the callback says what the numbers are; they come from the pane's own geometry, so a program is told about the pane it is in rather than the window. Mode 2048 (the in-band report on resize) already worked, because that one is answered from the size passed to ghostty_terminal_resize. Also notes in graphics.c what re-emission actually costs: we send decoded pixels because that is the only form lib-vt keeps, so an 819KB png reaches the client as 3.3MB of base64, once per image and again after a reattach clears `sent`. Fine for a splash, and written down so the next person to wonder does not have to measure it again.
Diffstat (limited to 'src')
-rw-r--r--src/graphics.c11
-rw-r--r--src/pane.c27
2 files changed, 37 insertions, 1 deletions
diff --git a/src/graphics.c b/src/graphics.c
index bb8edd4..8ee0177 100644
--- a/src/graphics.c
+++ b/src/graphics.c
@@ -112,7 +112,16 @@ static gfx_image_t *img_find(graphics_t *g, uint32_t pane, uint32_t src_id) {
return NULL;
}
-/* a=t: transmit without placing, so the same bytes can be placed repeatedly */
+/* a=t: transmit without placing, so the same bytes can be placed repeatedly.
+ *
+ * What goes out is always *decoded* pixels, because that is the only form
+ * lib-vt keeps: a PNG is decoded on the way in (D18) and we never see the
+ * original bytes again. So a program that uploads an 819KB png costs 3.3MB of
+ * base64 on the wire here -- once per image, and again after a reattach,
+ * which calls gfx_reset() and clears `sent`. Fine for a splash screen, not
+ * fine for anything that uploads a stream of photographs; if that ever shows
+ * up, the fix is to keep the source bytes ourselves and pass f=100 straight
+ * through rather than to make this loop cleverer. Nobody has felt it yet. */
static void transmit(graphics_t *g, gfx_image_t *img, uint32_t px_w,
uint32_t px_h, int format, int compression,
const uint8_t *data, size_t len) {
diff --git a/src/pane.c b/src/pane.c
index c2c1cc0..ae0c1bf 100644
--- a/src/pane.c
+++ b/src/pane.c
@@ -149,6 +149,31 @@ static void on_osc5577(const char *verb, const char *payload, void *ud) {
}
}
+/* XTWINOPS size queries (CSI 14/16/18 t): how big is the text area in pixels,
+ * how big is a cell, how big is it in characters.
+ *
+ * A program that draws images has to know the cell size to keep an aspect
+ * ratio, and asking the terminal is the only way that works through a
+ * multiplexer -- `TIOCGWINSZ` carries pixels too (D17) but a program on the
+ * far side of an ssh hop often finds them zeroed. Unanswered, the query just
+ * times out and the program falls back to a guess like 10x20; against a real
+ * 9x22 cell that is a picture stretched by a fifth, which looks like a bug in
+ * the program rather than in us.
+ *
+ * lib-vt encodes and writes the reply itself once we say what the numbers
+ * are. Mode 2048 (in-band reports on resize) already worked, because that one
+ * is answered from the size passed to ghostty_terminal_resize. */
+static bool on_size_report(GhosttyTerminal t, void *ud,
+ GhosttySizeReportSize *out) {
+ pane_t *p = ud;
+ if (!p || !out) return false;
+ *out = (GhosttySizeReportSize){.rows = p->rows_n,
+ .columns = p->cols,
+ .cell_width = p->cell_w,
+ .cell_height = p->cell_h};
+ return true;
+}
+
/* 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) {
@@ -297,6 +322,8 @@ pane_t *pane_new(const char *const argv[], uint16_t cols, uint16_t rows,
ghostty_terminal_set(p->term, GHOSTTY_TERMINAL_OPT_USERDATA, p);
ghostty_terminal_set(p->term, GHOSTTY_TERMINAL_OPT_WRITE_PTY,
(const void *)(uintptr_t)on_write_pty);
+ ghostty_terminal_set(p->term, GHOSTTY_TERMINAL_OPT_SIZE,
+ (const void *)(uintptr_t)on_size_report);
ghostty_terminal_set(p->term, GHOSTTY_TERMINAL_OPT_BELL,
(const void *)(uintptr_t)on_bell);
ghostty_terminal_set(p->term, GHOSTTY_TERMINAL_OPT_TITLE_CHANGED,