mirror of
https://github.com/ghostty-org/ghostty.git
synced 2026-04-06 07:38:21 +00:00
vt: expose size_report encoding in the C API
Add ghostty_size_report_encode() to libghostty-vt, following the same pattern as focus encoding: a single stateless function that writes a terminal size report escape sequence into a caller-provided buffer. The size_report.zig Style enum and Size struct now use lib.Enum and lib.Struct so the types are automatically C-compatible when building with c_abi, eliminating the need for duplicate type definitions in the C wrapper. The C wrapper in c/size_report.zig re-exports these types directly and provides the callconv(.c) encode entry point. Supports mode 2048 in-band reports and XTWINOPS responses (CSI 14 t, CSI 16 t, CSI 18 t).
This commit is contained in:
@@ -102,6 +102,7 @@ extern "C" {
|
||||
#include <ghostty/vt/modes.h>
|
||||
#include <ghostty/vt/mouse.h>
|
||||
#include <ghostty/vt/paste.h>
|
||||
#include <ghostty/vt/size_report.h>
|
||||
#include <ghostty/vt/wasm.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
126
include/ghostty/vt/size_report.h
Normal file
126
include/ghostty/vt/size_report.h
Normal file
@@ -0,0 +1,126 @@
|
||||
/**
|
||||
* @file size_report.h
|
||||
*
|
||||
* Size report encoding - encode terminal size reports into escape sequences.
|
||||
*/
|
||||
|
||||
#ifndef GHOSTTY_VT_SIZE_REPORT_H
|
||||
#define GHOSTTY_VT_SIZE_REPORT_H
|
||||
|
||||
/** @defgroup size_report Size Report Encoding
|
||||
*
|
||||
* Utilities for encoding terminal size reports into escape sequences,
|
||||
* supporting in-band size reports (mode 2048) and XTWINOPS responses
|
||||
* (CSI 14 t, CSI 16 t, CSI 18 t).
|
||||
*
|
||||
* ## Basic Usage
|
||||
*
|
||||
* Use ghostty_size_report_encode() to encode a size report into a
|
||||
* caller-provided buffer. If the buffer is too small, the function
|
||||
* returns GHOSTTY_OUT_OF_SPACE and sets the required size in the
|
||||
* output parameter.
|
||||
*
|
||||
* ## Example
|
||||
*
|
||||
* @code{.c}
|
||||
* #include <stdio.h>
|
||||
* #include <ghostty/vt.h>
|
||||
*
|
||||
* int main() {
|
||||
* GhosttySizeReportSize size = {
|
||||
* .rows = 24,
|
||||
* .columns = 80,
|
||||
* .cell_width = 9,
|
||||
* .cell_height = 18,
|
||||
* };
|
||||
*
|
||||
* char buf[64];
|
||||
* size_t written = 0;
|
||||
*
|
||||
* GhosttyResult result = ghostty_size_report_encode(
|
||||
* GHOSTTY_SIZE_REPORT_MODE_2048, size, buf, sizeof(buf), &written);
|
||||
*
|
||||
* if (result == GHOSTTY_SUCCESS) {
|
||||
* printf("Encoded %zu bytes: ", written);
|
||||
* fwrite(buf, 1, written, stdout);
|
||||
* printf("\n");
|
||||
* }
|
||||
*
|
||||
* return 0;
|
||||
* }
|
||||
* @endcode
|
||||
*
|
||||
* @{
|
||||
*/
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <ghostty/vt/types.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Size report style.
|
||||
*
|
||||
* Determines the output format for the terminal size report.
|
||||
*/
|
||||
typedef enum {
|
||||
/** In-band size report (mode 2048): ESC [ 48 ; rows ; cols ; height ; width t */
|
||||
GHOSTTY_SIZE_REPORT_MODE_2048 = 0,
|
||||
/** XTWINOPS text area size in pixels: ESC [ 4 ; height ; width t */
|
||||
GHOSTTY_SIZE_REPORT_CSI_14_T = 1,
|
||||
/** XTWINOPS cell size in pixels: ESC [ 6 ; height ; width t */
|
||||
GHOSTTY_SIZE_REPORT_CSI_16_T = 2,
|
||||
/** XTWINOPS text area size in characters: ESC [ 8 ; rows ; cols t */
|
||||
GHOSTTY_SIZE_REPORT_CSI_18_T = 3,
|
||||
} GhosttySizeReportStyle;
|
||||
|
||||
/**
|
||||
* Terminal size information for encoding size reports.
|
||||
*/
|
||||
typedef struct {
|
||||
/** Terminal row count in cells. */
|
||||
uint16_t rows;
|
||||
/** Terminal column count in cells. */
|
||||
uint16_t columns;
|
||||
/** Width of a single terminal cell in pixels. */
|
||||
uint32_t cell_width;
|
||||
/** Height of a single terminal cell in pixels. */
|
||||
uint32_t cell_height;
|
||||
} GhosttySizeReportSize;
|
||||
|
||||
/**
|
||||
* Encode a terminal size report into an escape sequence.
|
||||
*
|
||||
* Encodes a size report in the format specified by @p style into the
|
||||
* provided buffer.
|
||||
*
|
||||
* If the buffer is too small, the function returns GHOSTTY_OUT_OF_SPACE
|
||||
* and writes the required buffer size to @p out_written. The caller can
|
||||
* then retry with a sufficiently sized buffer.
|
||||
*
|
||||
* @param style The size report format to encode
|
||||
* @param size Terminal size information
|
||||
* @param buf Output buffer to write the encoded sequence into (may be NULL)
|
||||
* @param buf_len Size of the output buffer in bytes
|
||||
* @param[out] out_written On success, the number of bytes written. On
|
||||
* GHOSTTY_OUT_OF_SPACE, the required buffer size.
|
||||
* @return GHOSTTY_SUCCESS on success, GHOSTTY_OUT_OF_SPACE if the buffer
|
||||
* is too small
|
||||
*/
|
||||
GhosttyResult ghostty_size_report_encode(
|
||||
GhosttySizeReportStyle style,
|
||||
GhosttySizeReportSize size,
|
||||
char* buf,
|
||||
size_t buf_len,
|
||||
size_t* out_written);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
/** @} */
|
||||
|
||||
#endif /* GHOSTTY_VT_SIZE_REPORT_H */
|
||||
@@ -53,6 +53,7 @@ pub const RenderState = terminal.RenderState;
|
||||
pub const Screen = terminal.Screen;
|
||||
pub const ScreenSet = terminal.ScreenSet;
|
||||
pub const Selection = terminal.Selection;
|
||||
pub const size_report = terminal.size_report;
|
||||
pub const SizeReportStyle = terminal.SizeReportStyle;
|
||||
pub const StringMap = terminal.StringMap;
|
||||
pub const Style = terminal.Style;
|
||||
@@ -167,6 +168,7 @@ comptime {
|
||||
@export(&c.focus_encode, .{ .name = "ghostty_focus_encode" });
|
||||
@export(&c.mode_report_encode, .{ .name = "ghostty_mode_report_encode" });
|
||||
@export(&c.paste_is_safe, .{ .name = "ghostty_paste_is_safe" });
|
||||
@export(&c.size_report_encode, .{ .name = "ghostty_size_report_encode" });
|
||||
@export(&c.color_rgb_get, .{ .name = "ghostty_color_rgb_get" });
|
||||
@export(&c.sgr_new, .{ .name = "ghostty_sgr_new" });
|
||||
@export(&c.sgr_free, .{ .name = "ghostty_sgr_free" });
|
||||
|
||||
@@ -9,6 +9,7 @@ pub const mouse_event = @import("mouse_event.zig");
|
||||
pub const mouse_encode = @import("mouse_encode.zig");
|
||||
pub const paste = @import("paste.zig");
|
||||
pub const sgr = @import("sgr.zig");
|
||||
pub const size_report = @import("size_report.zig");
|
||||
pub const terminal = @import("terminal.zig");
|
||||
|
||||
// The full C API, unexported.
|
||||
@@ -87,6 +88,8 @@ pub const mouse_encoder_encode = mouse_encode.encode;
|
||||
|
||||
pub const paste_is_safe = paste.is_safe;
|
||||
|
||||
pub const size_report_encode = size_report.encode;
|
||||
|
||||
pub const terminal_new = terminal.new;
|
||||
pub const terminal_free = terminal.free;
|
||||
pub const terminal_reset = terminal.reset;
|
||||
@@ -108,6 +111,7 @@ test {
|
||||
_ = mouse_encode;
|
||||
_ = paste;
|
||||
_ = sgr;
|
||||
_ = size_report;
|
||||
_ = terminal;
|
||||
|
||||
// We want to make sure we run the tests for the C allocator interface.
|
||||
|
||||
81
src/terminal/c/size_report.zig
Normal file
81
src/terminal/c/size_report.zig
Normal file
@@ -0,0 +1,81 @@
|
||||
const std = @import("std");
|
||||
const terminal_size_report = @import("../size_report.zig");
|
||||
const Result = @import("result.zig").Result;
|
||||
|
||||
/// C: GhosttySizeReportStyle
|
||||
pub const Style = terminal_size_report.Style;
|
||||
|
||||
/// C: GhosttySizeReportSize
|
||||
pub const Size = terminal_size_report.Size;
|
||||
|
||||
pub fn encode(
|
||||
style: Style,
|
||||
size: Size,
|
||||
out_: ?[*]u8,
|
||||
out_len: usize,
|
||||
out_written: *usize,
|
||||
) callconv(.c) Result {
|
||||
var writer: std.Io.Writer = .fixed(if (out_) |out| out[0..out_len] else &.{});
|
||||
terminal_size_report.encode(&writer, style, size) catch |err| switch (err) {
|
||||
error.WriteFailed => {
|
||||
var discarding: std.Io.Writer.Discarding = .init(&.{});
|
||||
terminal_size_report.encode(&discarding.writer, style, size) catch unreachable;
|
||||
out_written.* = @intCast(discarding.count);
|
||||
return .out_of_space;
|
||||
},
|
||||
};
|
||||
|
||||
out_written.* = writer.end;
|
||||
return .success;
|
||||
}
|
||||
|
||||
test "encode mode 2048" {
|
||||
var buf: [64]u8 = undefined;
|
||||
var written: usize = 0;
|
||||
const result = encode(.mode_2048, .{
|
||||
.rows = 24,
|
||||
.columns = 80,
|
||||
.cell_width = 9,
|
||||
.cell_height = 18,
|
||||
}, &buf, buf.len, &written);
|
||||
try std.testing.expectEqual(.success, result);
|
||||
try std.testing.expectEqualStrings("\x1B[48;24;80;432;720t", buf[0..written]);
|
||||
}
|
||||
|
||||
test "encode csi 14 t" {
|
||||
var buf: [64]u8 = undefined;
|
||||
var written: usize = 0;
|
||||
const result = encode(.csi_14_t, .{
|
||||
.rows = 24,
|
||||
.columns = 80,
|
||||
.cell_width = 9,
|
||||
.cell_height = 18,
|
||||
}, &buf, buf.len, &written);
|
||||
try std.testing.expectEqual(.success, result);
|
||||
try std.testing.expectEqualStrings("\x1b[4;432;720t", buf[0..written]);
|
||||
}
|
||||
|
||||
test "encode with insufficient buffer" {
|
||||
var buf: [1]u8 = undefined;
|
||||
var written: usize = 0;
|
||||
const result = encode(.csi_18_t, .{
|
||||
.rows = 24,
|
||||
.columns = 80,
|
||||
.cell_width = 9,
|
||||
.cell_height = 18,
|
||||
}, &buf, buf.len, &written);
|
||||
try std.testing.expectEqual(.out_of_space, result);
|
||||
try std.testing.expect(written > 1);
|
||||
}
|
||||
|
||||
test "encode with null buffer" {
|
||||
var written: usize = 0;
|
||||
const result = encode(.csi_18_t, .{
|
||||
.rows = 24,
|
||||
.columns = 80,
|
||||
.cell_width = 9,
|
||||
.cell_height = 18,
|
||||
}, null, 0, &written);
|
||||
try std.testing.expectEqual(.out_of_space, result);
|
||||
try std.testing.expect(written > 0);
|
||||
}
|
||||
@@ -1,23 +1,23 @@
|
||||
const std = @import("std");
|
||||
const build_options = @import("terminal_options");
|
||||
const lib = @import("../lib/main.zig");
|
||||
const lib_target: lib.Target = if (build_options.c_abi) .c else .zig;
|
||||
const CellCountInt = @import("size.zig").CellCountInt;
|
||||
|
||||
/// Output formats for terminal size reports written to the PTY.
|
||||
pub const Style = enum {
|
||||
/// In-band size reports (mode 2048)
|
||||
mode_2048,
|
||||
|
||||
/// XTWINOPS: report text area size in pixels
|
||||
csi_14_t,
|
||||
|
||||
/// XTWINOPS: report cell size in pixels
|
||||
csi_16_t,
|
||||
|
||||
/// XTWINOPS: report text area size in characters
|
||||
csi_18_t,
|
||||
};
|
||||
pub const Style = lib.Enum(lib_target, &.{
|
||||
// In-band size reports (mode 2048)
|
||||
"mode_2048",
|
||||
// XTWINOPS: report text area size in pixels
|
||||
"csi_14_t",
|
||||
// XTWINOPS: report cell size in pixels
|
||||
"csi_16_t",
|
||||
// XTWINOPS: report text area size in characters
|
||||
"csi_18_t",
|
||||
});
|
||||
|
||||
/// Runtime size values used to encode terminal size reports.
|
||||
pub const Size = struct {
|
||||
pub const Size = lib.Struct(lib_target, struct {
|
||||
/// Terminal row count in cells.
|
||||
rows: CellCountInt,
|
||||
|
||||
@@ -29,15 +29,15 @@ pub const Size = struct {
|
||||
|
||||
/// Height of a single terminal cell in pixels.
|
||||
cell_height: u32,
|
||||
});
|
||||
|
||||
pub fn widthPixels(self: Size) u64 {
|
||||
return @as(u64, self.columns) * @as(u64, self.cell_width);
|
||||
}
|
||||
fn widthPixels(s: Size) u64 {
|
||||
return @as(u64, s.columns) * @as(u64, s.cell_width);
|
||||
}
|
||||
|
||||
pub fn heightPixels(self: Size) u64 {
|
||||
return @as(u64, self.rows) * @as(u64, self.cell_height);
|
||||
}
|
||||
};
|
||||
fn heightPixels(s: Size) u64 {
|
||||
return @as(u64, s.rows) * @as(u64, s.cell_height);
|
||||
}
|
||||
|
||||
/// Encode a terminal size report sequence.
|
||||
pub fn encode(
|
||||
@@ -51,16 +51,16 @@ pub fn encode(
|
||||
.{
|
||||
size.rows,
|
||||
size.columns,
|
||||
size.heightPixels(),
|
||||
size.widthPixels(),
|
||||
heightPixels(size),
|
||||
widthPixels(size),
|
||||
},
|
||||
),
|
||||
|
||||
.csi_14_t => try writer.print(
|
||||
"\x1b[4;{};{}t",
|
||||
.{
|
||||
size.heightPixels(),
|
||||
size.widthPixels(),
|
||||
heightPixels(size),
|
||||
widthPixels(size),
|
||||
},
|
||||
),
|
||||
|
||||
|
||||
Reference in New Issue
Block a user