1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# ghostty: a host cannot be the one that reads a kitty image's medium
A note for upstream (https://github.com/ghostty-org/ghostty), against the
commit pardes pins (83fb95624b04, 1.4.0, `build.zig.zon`). pardes patches
its fetched copy at build time (build/patches.zig, `ghostty_kitty`, and
build/patches/ghostty_kitty.zig) until upstream has a way, if it wants one.
## What a host cannot do
A kitty graphics transmission by file (`t=f`), temporary file (`t=t`) or
shared memory (`t=s`) is read inside `LoadingImage.init`
(`src/terminal/kitty/graphics_image.zig`): `readFile` opens, checks and
reads the path, and deletes a temporary file; `readSharedMemory` opens,
maps, copies and unlinks the object. A host of libghostty-vt can allow or
refuse each medium (`LoadingImage.Limits`), and nothing else: it cannot
be the one that has the bytes.
pardes needs to. A session's terminal is parsed by its own emulator and,
the same bytes in the same order, by the emulator of every client of the
session, each on its own machine. A file or a shared memory object names
something on the session's machine, and a temporary file or an object is
gone once its first reader has read it. So a client must not read the
medium: it must be handed the bytes the session read, or the failure the
session met, and the program must be answered once, by the session. If a
client refuses the medium instead, a shown image (`a=T`) moves the
session's cursor past it and not the client's, and the two terminals part.
## The patch
`LoadingImage.read_medium`, a hook a host may set (null: ghostty's read
as it is). When set, `init` hands it ghostty's own read as a function and
a context, and the allocator the bytes are to be in; what it returns
becomes the image's data:
```zig
pub var read_medium: ?*const fn (ctx: *const anyopaque, read: *const fn (ctx: *const anyopaque) MediumError![]u8, alloc: Allocator) MediumError![]u8 = null;
```
The read itself is ghostty's, unchanged (`readFile`, `readSharedMemory`,
their checks, the deletion and the unlink), behind a small struct appended
to the file (`PardesMedium.read`). pardes's hook calls it on the session
and records what it returned; a client's hook returns the recorded bytes
(or error) without calling it. Everything before the read (the limits,
the PNG check, the path's NUL check) and after it (decompression,
decoding, the size checks of `complete`, the answer) runs on every side
alike.
## Why not rewrite the transmission instead
The other way is to turn `t=f`, `t=t` and `t=s` into direct data before
the emulator sees it. That parses the APC outside ghostty, repeats its
rules (the temporary directory and the name a temporary file must have,
the `/proc`, `/sys` and device refusals, the shared memory range by
format, offset and size, `S` meaning an exact length), and re-encodes the
bytes as base64 a third larger, chunked to the protocol's limits. It also
changes what the emulator sees, so its answer to the program has to be
checked to match. The hook keeps all of that in ghostty.
|