diff options
| author | Can Celik <ogulcancelik@gmail.com> | 2026-09-10 21:51:57 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-09-10 21:51:57 +0300 |
| commit | 425c86179791cd32e2d4ce0ae7f269940b4b0663 (patch) | |
| tree | e52c4383a6d69216cbe2a32638bf90841d7e2189 /vendor/libghostty-vt/src/terminal/c/kitty_graphics.zig | |
| parent | 90e947a6e26e02f10ec7f1694c4eb5b78a86406b (diff) | |
chore: upgrade libghostty and preserve terminal behavior (#3906)
Diffstat (limited to 'vendor/libghostty-vt/src/terminal/c/kitty_graphics.zig')
| -rw-r--r-- | vendor/libghostty-vt/src/terminal/c/kitty_graphics.zig | 288 |
1 files changed, 227 insertions, 61 deletions
diff --git a/vendor/libghostty-vt/src/terminal/c/kitty_graphics.zig b/vendor/libghostty-vt/src/terminal/c/kitty_graphics.zig index 13a675cc..2bd5e862 100644 --- a/vendor/libghostty-vt/src/terminal/c/kitty_graphics.zig +++ b/vendor/libghostty-vt/src/terminal/c/kitty_graphics.zig @@ -10,6 +10,7 @@ const grid_ref = @import("grid_ref.zig"); const selection_c = @import("selection.zig"); const terminal_c = @import("terminal.zig"); const Terminal = @import("../Terminal.zig"); +const PageList = @import("../PageList.zig"); const Result = @import("result.zig").Result; /// C: GhosttyKittyGraphics @@ -261,8 +262,11 @@ fn imageGetTyped( .height => out.* = image.height, .format => out.* = image.format, .compression => out.* = image.compression, - .data_ptr => out.* = image.data.ptr, - .data_len => out.* = image.data.len, + // For animated images this is the current animation frame's + // data; the image generation changes whenever the current + // frame does, so generation-keyed caches stay coherent. + .data_ptr => out.* = (image.renderData().bytes() orelse return .no_value).ptr, + .data_len => out.* = image.renderData().len(), .generation => out.* = image.generation, } @@ -301,7 +305,7 @@ pub fn placement_iterator_set( if (comptime !build_options.kitty_graphics) return .no_value; if (comptime std.debug.runtime_safety) { - _ = std.meta.intToEnum(PlacementIteratorOption, @intFromEnum(option)) catch { + _ = std.enums.fromInt(PlacementIteratorOption, @intFromEnum(option)) orelse { return .invalid_value; }; } @@ -516,16 +520,12 @@ pub fn placement_source_rect( const entry = iter.entry orelse return .invalid_value; const p = entry.value_ptr; - // Apply "0 = full image dimension" convention, then clamp to image bounds. - const x = @min(p.source_x, image.width); - const y = @min(p.source_y, image.height); - const w = @min(if (p.source_width > 0) p.source_width else image.width, image.width - x); - const h = @min(if (p.source_height > 0) p.source_height else image.height, image.height - y); + const source = p.sourceRect(image.*); - out_x.* = x; - out_y.* = y; - out_width.* = w; - out_height.* = h; + out_x.* = source.x; + out_y.* = source.y; + out_width.* = source.width; + out_height.* = source.height; return .success; } @@ -576,12 +576,11 @@ pub fn placement_render_info( out.viewport_row = vp.row; out.viewport_visible = vp.visible; - const x = @min(p.source_x, image.width); - const y = @min(p.source_y, image.height); - out.source_x = x; - out.source_y = y; - out.source_width = @min(if (p.source_width > 0) p.source_width else image.width, image.width - x); - out.source_height = @min(if (p.source_height > 0) p.source_height else image.height, image.height - y); + const source = p.sourceRect(image.*); + out.source_x = source.x; + out.source_y = source.y; + out.source_width = source.width; + out.source_height = source.height; return .success; } @@ -595,9 +594,9 @@ pub fn placement_render_info( /// the placement's origin has scrolled above the top of the viewport. /// /// A placement is considered not visible if it is a virtual (unicode -/// placeholder) placement, or if it is fully off-screen (its bottom -/// edge is above the viewport or its top edge is at or below the -/// viewport's last row). +/// placeholder) placement, its tracked content has been pruned, or it is +/// fully off-screen (its bottom edge is above the viewport or its top edge is +/// at or below the viewport's last row). fn computeViewportPos( p: *const kitty_storage.ImageStorage.Placement, image: *const Image, @@ -605,11 +604,37 @@ fn computeViewportPos( ) struct { col: i32, row: i32, visible: bool } { // Virtual placements use unicode placeholders and don't have a // screen position — they are rendered inline by the text layout. - const pin = switch (p.location) { - .pin => |pin| pin, + // Relative placements are anchored at the root of their parent + // chain, offset by the accumulated chain offsets. A chain rooted + // at a virtual placement has no resolvable position here: its + // origin is the parent's placeholder cells, which only a renderer + // scanning the screen can locate. + const origin: struct { + pin: *const PageList.Pin, + col_offset: i32 = 0, + row_offset: i32 = 0, + } = switch (p.location) { + .pin => |pin| .{ .pin = pin }, .virtual => return .{ .col = 0, .row = 0, .visible = false }, + .relative => |rel| origin: { + const storage = &t.screens.active.kitty_images; + const chain = storage.resolveChain(rel) orelse + return .{ .col = 0, .row = 0, .visible = false }; + switch (chain.root.location) { + .pin => |root_pin| break :origin .{ + .pin = root_pin, + .col_offset = chain.horizontal_offset, + .row_offset = chain.vertical_offset, + }, + .virtual => return .{ .col = 0, .row = 0, .visible = false }, + .relative => unreachable, // resolveChain roots are never relative + } + }, }; + const pin = origin.pin; + if (pin.garbage) return .{ .col = 0, .row = 0, .visible = false }; + // Convert both the placement's pin and the viewport's top-left // corner to screen-absolute coordinates so we can subtract them // to get viewport-relative coordinates. @@ -622,23 +647,30 @@ fn computeViewportPos( // Subtracting viewport origin from the pin gives us viewport- // relative coordinates. The row can be negative when the - // placement has partially scrolled above the viewport. - const vp_row: i32 = @as(i32, @intCast(pin_screen.screen.y)) - - @as(i32, @intCast(vp_screen.screen.y)); - const vp_col: i32 = @intCast(pin_screen.screen.x); - - // A placement is invisible if its bottom edge (row + height) - // is above the viewport, or its top edge is at or below the - // viewport's last row. + // placement has partially scrolled above the viewport, and both + // can be negative for relative placements with negative offsets. + const vp_row: i32 = (@as(i32, @intCast(pin_screen.screen.y)) - + @as(i32, @intCast(vp_screen.screen.y))) +| origin.row_offset; + const vp_col: i32 = @as(i32, @intCast(pin_screen.screen.x)) +| + origin.col_offset; + + // A placement is invisible if its bottom edge (row + height) is + // above the viewport, or its top edge is at or below the viewport's + // last row. The same applies horizontally: a pin's column is always + // in bounds, but a relative placement's offsets can push it fully + // off either side. const grid_size = p.gridSize(image.*, t); - const rows_i32: i32 = @intCast(grid_size.rows); - const term_rows: i32 = @intCast(t.rows); - const visible = vp_row + rows_i32 > 0 and vp_row < term_rows; + const bottom_row = @as(i64, vp_row) + @as(i64, grid_size.rows); + const right_col = @as(i64, vp_col) + @as(i64, grid_size.cols); + const visible = bottom_row > 0 and vp_row < @as(i32, t.rows) and + right_col > 0 and vp_col < @as(i32, t.cols); return .{ .col = vp_col, .row = vp_row, .visible = visible }; } test "placement_iterator new/free" { + if (comptime !build_options.kitty_graphics) return error.SkipZigTest; + var iter: PlacementIterator = null; try testing.expectEqual(Result.success, placement_iterator_new( &lib.alloc.test_allocator, @@ -659,7 +691,8 @@ test "placement_iterator next on empty storage" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); @@ -688,7 +721,8 @@ test "placement_iterator get before next returns invalid" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); @@ -719,7 +753,8 @@ test "placement_iterator with transmit and display" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); @@ -772,7 +807,8 @@ test "placement_iterator with multiple placements" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); @@ -829,7 +865,8 @@ test "placement_iterator_set layer filter" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); @@ -919,7 +956,8 @@ test "image_get_handle returns null for missing id" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); @@ -940,7 +978,8 @@ test "image_get_handle and image_get with transmitted image" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); @@ -983,6 +1022,57 @@ test "image_get_handle and image_get with transmitted image" { try testing.expect(data_len > 0); } +test "image_get exposes pending metadata without a data pointer" { + if (comptime !build_options.kitty_graphics) return error.SkipZigTest; + + var t: terminal_c.Terminal = null; + try testing.expectEqual(Result.success, terminal_c.new( + &lib.alloc.test_allocator, + &t, + 80, + 24, + )); + defer terminal_c.free(t); + + var graphics: KittyGraphics = undefined; + try testing.expectEqual(Result.success, terminal_c.get( + t, + .kitty_graphics, + @ptrCast(&graphics), + )); + + const alloc = lib.alloc.default(&lib.alloc.test_allocator); + const pending = try graphics.addPendingImage(testing.io, alloc, terminal_c.zigTerminal(t).?.screens.active, .{ + .id = 42, + .number = 7, + .width = 1, + .height = 4, + .format = .rgb, + .data = .{ .pending = 12 }, + }); + + const img = image_get_handle(graphics, 42); + try testing.expect(img != null); + + var number: u32 = 0; + try testing.expectEqual(Result.success, image_get(img, .number, @ptrCast(&number))); + try testing.expectEqual(@as(u32, 7), number); + + var data_len: usize = 0; + try testing.expectEqual(Result.success, image_get(img, .data_len, @ptrCast(&data_len))); + try testing.expectEqual(@as(usize, 12), data_len); + + var data_ptr: [*]const u8 = undefined; + try testing.expectEqual(Result.no_value, image_get(img, .data_ptr, @ptrCast(&data_ptr))); + + const pixels = try alloc.dupe(u8, "*" ** 12); + try testing.expect(pending.complete(graphics, testing.io, pixels)); + const completed_img = image_get_handle(graphics, 42); + try testing.expect(completed_img != null); + try testing.expectEqual(Result.success, image_get(completed_img, .data_ptr, @ptrCast(&data_ptr))); + try testing.expectEqualSlices(u8, pixels, data_ptr[0..data_len]); +} + test "placement_rect with transmit and display" { if (comptime !build_options.kitty_graphics) return error.SkipZigTest; @@ -990,7 +1080,8 @@ test "placement_rect with transmit and display" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); @@ -1051,7 +1142,8 @@ test "placement_pixel_size with transmit and display" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); @@ -1106,7 +1198,8 @@ test "placement_grid_size with transmit and display" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); @@ -1160,7 +1253,8 @@ test "placement_viewport_pos with transmit and display" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); @@ -1205,7 +1299,8 @@ test "placement_viewport_pos fully off-screen above" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 5, .max_scrollback = 100 }, + 80, + 5, )); defer terminal_c.free(t); try testing.expectEqual(Result.success, terminal_c.resize(t, 80, 5, 10, 20)); @@ -1244,7 +1339,8 @@ test "placement_viewport_pos top off-screen" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 5, .max_scrollback = 100 }, + 80, + 5, )); defer terminal_c.free(t); try testing.expectEqual(Result.success, terminal_c.resize(t, 80, 5, 10, 20)); @@ -1287,7 +1383,8 @@ test "placement_viewport_pos bottom off-screen" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 5, .max_scrollback = 0 }, + 80, + 5, )); defer terminal_c.free(t); try testing.expectEqual(Result.success, terminal_c.resize(t, 80, 5, 10, 20)); @@ -1327,7 +1424,8 @@ test "placement_viewport_pos top and bottom off-screen" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 5, .max_scrollback = 100 }, + 80, + 5, )); defer terminal_c.free(t); try testing.expectEqual(Result.success, terminal_c.resize(t, 80, 5, 10, 20)); @@ -1378,7 +1476,8 @@ test "placement_source_rect defaults to full image" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); try testing.expectEqual(Result.success, terminal_c.resize(t, 80, 24, 10, 20)); @@ -1417,7 +1516,8 @@ test "placement_source_rect with explicit source rect" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); try testing.expectEqual(Result.success, terminal_c.resize(t, 80, 24, 10, 20)); @@ -1461,7 +1561,8 @@ test "placement_source_rect clamps to image bounds" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); try testing.expectEqual(Result.success, terminal_c.resize(t, 80, 24, 10, 20)); @@ -1496,6 +1597,15 @@ test "placement_source_rect clamps to image bounds" { try testing.expectEqual(3, y); try testing.expectEqual(1, w); try testing.expectEqual(1, h); + + var pixel_width: u32 = undefined; + var pixel_height: u32 = undefined; + try testing.expectEqual( + Result.success, + placement_pixel_size(iter, img, t, &pixel_width, &pixel_height), + ); + try testing.expectEqual(1, pixel_width); + try testing.expectEqual(1, pixel_height); } test "placement_source_rect null args return invalid_value" { @@ -1522,7 +1632,8 @@ test "placement_render_info returns all fields" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); try testing.expectEqual(Result.success, terminal_c.resize(t, 80, 24, 10, 20)); @@ -1552,6 +1663,53 @@ test "placement_render_info returns all fields" { try testing.expectEqual(0, ri.source_y); try testing.expectEqual(1, ri.source_width); try testing.expectEqual(2, ri.source_height); + + const entry = iter.?.entry.?; + const pin = switch (entry.value_ptr.location) { + .pin => |pin| pin, + .virtual, .relative => unreachable, + }; + pin.garbage = true; + + ri = .{}; + try testing.expectEqual(Result.success, placement_render_info(iter, img, t, &ri)); + try testing.expect(!ri.viewport_visible); + + var rect: selection_c.CSelection = undefined; + try testing.expectEqual(Result.no_value, placement_rect(iter, img, t, &rect)); +} + +test "placement_render_info handles maximum grid dimensions" { + if (comptime !build_options.kitty_graphics) return error.SkipZigTest; + + var t: terminal_c.Terminal = null; + try testing.expectEqual(Result.success, terminal_c.new( + &lib.alloc.test_allocator, + &t, + 80, + 24, + )); + defer terminal_c.free(t); + try testing.expectEqual(Result.success, terminal_c.resize(t, 80, 24, 10, 20)); + + const cmd = "\x1b_Ga=T,t=d,f=24,i=1,p=1,s=1,v=2,c=1,r=4294967295,C=1;////////\x1b\\"; + terminal_c.vt_write(t, cmd.ptr, cmd.len); + + var graphics: KittyGraphics = undefined; + try testing.expectEqual(Result.success, terminal_c.get(t, .kitty_graphics, @ptrCast(&graphics))); + const img = image_get_handle(graphics, 1); + try testing.expect(img != null); + + var iter: PlacementIterator = null; + try testing.expectEqual(Result.success, placement_iterator_new(&lib.alloc.test_allocator, &iter)); + defer placement_iterator_free(iter); + try testing.expectEqual(Result.success, get(graphics, .placement_iterator, @ptrCast(&iter))); + try testing.expect(placement_iterator_next(iter)); + + var ri: PlacementRenderInfo = .{}; + try testing.expectEqual(Result.success, placement_render_info(iter, img, t, &ri)); + try testing.expect(ri.viewport_visible); + try testing.expectEqual(std.math.maxInt(u32), ri.grid_rows); } test "placement_render_info off-screen sets viewport_visible false" { @@ -1561,7 +1719,8 @@ test "placement_render_info off-screen sets viewport_visible false" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 5, .max_scrollback = 100 }, + 80, + 5, )); defer terminal_c.free(t); try testing.expectEqual(Result.success, terminal_c.resize(t, 80, 5, 10, 20)); @@ -1610,7 +1769,8 @@ test "image_get_multi success" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); try testing.expectEqual(Result.success, terminal_c.resize(t, 80, 24, 10, 20)); @@ -1664,7 +1824,8 @@ test "placement_get_multi success" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); try testing.expectEqual(Result.success, terminal_c.resize(t, 80, 24, 10, 20)); @@ -1722,7 +1883,8 @@ test "storage generation via get" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); @@ -1774,7 +1936,8 @@ test "image generation detects same-sized retransmission" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); @@ -1835,7 +1998,8 @@ test "image generation via image_get_multi" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); @@ -1871,7 +2035,8 @@ test "image compression and format always report decoded data" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); @@ -1917,7 +2082,8 @@ test "generation never recurs across resets and screen switches" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); |
