Files
ghostty-rs/crates/ghostty-vt-capi/src/color.rs
T
ddidderr 0915f7b935 feat(vt-capi): port the libghostty-vt C API
Ports ZIG/src/lib_vt.zig and terminal/c/* as crates/ghostty-vt-capi
(cdylib + staticlib + rlib, soname libghostty-vt.so) implementing the
unchanged include/ghostty/vt.h + vt/*.h ABI: OSC parser, SGR parser,
key events and key encoding, paste safety, colors, result codes - all
38 native symbols, plus the ghostty_wasm_* helpers on wasm32.

Verification:
- tests/abi.rs generates a C probe from the headers, compiled with cc,
  that checks sizeof/alignof/offsetof of every struct/union, every
  enumerator and #define against Rust, and links every declared symbol;
  nm -D exports equal the declared functions.
- Ghostty's example/c-vt* programs compile unchanged against the Rust
  library (shared and static) and produce the expected output.
- The wasm32 build runs Ghostty's wasm-key-encode/wasm-sgr example
  scripts unmodified under node (upstream's wasm-sgr tag table is stale
  vs sgr.h; a regenerated copy checks full output).

Test Plan:
- cargo test -p ghostty-vt-capi (22 unit + 2 ABI + 5 C + 3 wasm tests)
2026-10-12 05:16:41 +02:00

57 lines
1.2 KiB
Rust

//! `ghostty_color_rgb_get` (ZIG/src/terminal/c/color.zig).
#![allow(unsafe_code)]
/// C: `GhosttyColorRgb`
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[repr(C)]
pub struct GhosttyColorRgb {
pub r: u8,
pub g: u8,
pub b: u8,
}
impl From<ghostty_vt::color::Rgb> for GhosttyColorRgb {
fn from(c: ghostty_vt::color::Rgb) -> Self {
Self {
r: c.r,
g: c.g,
b: c.b,
}
}
}
/// Extract the RGB components of a color.
///
/// # Safety
/// `r`, `g` and `b` must be valid for writing a `u8` (Zig: non-optional
/// pointers).
#[unsafe(no_mangle)]
pub unsafe extern "C" fn ghostty_color_rgb_get(
color: GhosttyColorRgb,
r: *mut u8,
g: *mut u8,
b: *mut u8,
) {
// SAFETY: the caller guarantees all three pointers are valid for writes.
unsafe {
*r = color.r;
*g = color.g;
*b = color.b;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rgb_get() {
let (mut r, mut g, mut b) = (0u8, 0u8, 0u8);
// SAFETY: pointers to live locals.
unsafe {
ghostty_color_rgb_get(GhosttyColorRgb { r: 1, g: 2, b: 3 }, &mut r, &mut g, &mut b);
}
assert_eq!((r, g, b), (1, 2, 3));
}
}