feat(lib): port libghostty, Ghostty's embedded C API

Ports apprt/embedded.zig, main_c.zig and config/CApi.zig + c_get.zig as
crates/ghostty-lib (cdylib/staticlib/rlib `libghostty`), exporting every
function of the unchanged include/ghostty.h - the ABI Ghostty's Swift
macOS/iOS app links against. The embedded runtime implements the
runtime traits over the host's C callbacks, so the real core drives it.

- ghostty-core selects the renderer per target: Metal on Apple (surface
  NSView/UIView via RtSurface::platform_view), OpenGL elsewhere.
- ghostty_config_get follows Zig's c_get rules via a CValue type (143
  supported keys, checked against the Zig field types).
- The inspector C API is complete; its Metal backend replaces ImGui's
  Metal renderer and has not run.

Verification: generated C ABI probe (sizeof/alignof/offsetof of every
struct/union, 755 enum values, every function linked, nm exports, headers
byte-identical to upstream); a C host on Linux covering init, CLI
actions, config, app, keys, focus and color scheme; an opt-in
test-platform feature creating real surfaces on Linux with headless EGL
and a pty (13 tests). Apple targets are clippy-clean but not run; the
Swift app has not been built against it.

Test Plan:
- cargo test -p ghostty-lib (and --features test-platform)
- cargo clippy --target aarch64-apple-darwin -p ghostty-lib (cross env
  in docs/rewrite/notes/macos-M1.md)
This commit is contained in:
ddidderr committed 2026-10-12 06:38:33 +02:00
1 parent 4787565397
commit 49d4ecefae
47 files changed
+10296 -34

No files matched your search

+28 -4
View File
@@ -38,6 +38,13 @@ impl Logging {
/// invalid value (including an empty part) yields the default.
#[must_use]
pub fn parse(v: &str) -> Logging {
Logging::parse_with(v, Logging::default())
}
/// [`Logging::parse`] with an explicit default (lib mode defaults to no
/// stderr logging).
#[must_use]
pub fn parse_with(v: &str, default: Logging) -> Logging {
match v {
"1" | "t" | "T" | "true" => {
return Logging {
@@ -53,7 +60,7 @@ impl Logging {
}
_ => {}
}
let mut out = Logging::default();
let mut out = default;
for part in v.split(',') {
let part = part.trim_matches([' ', '\t']);
let (name, value) = match part.strip_prefix("no-") {
@@ -63,7 +70,7 @@ impl Logging {
match name {
"stderr" => out.stderr = value,
"macos" => out.macos = value,
_ => return Logging::default(),
_ => return default,
}
}
out
@@ -134,8 +141,25 @@ pub fn state() -> &'static GlobalState {
/// Initialize the global state: logging, rlimits, resources dir and i18n.
/// Call once, early (Zig: `GlobalState.init`).
pub fn init(opts: InitOptions) -> &'static GlobalState {
init_with(opts, Logging::default())
}
/// [`init`] for the embedded library (`ghostty_init`): Zig's
/// `GlobalState.Logging.stderr` defaults to `build_config.app_runtime !=
/// .none`, i.e. off for the lib artifact.
pub fn init_lib(opts: InitOptions) -> &'static GlobalState {
init_with(
opts,
Logging {
stderr: false,
..Logging::default()
},
)
}
fn init_with(opts: InitOptions, default_logging: Logging) -> &'static GlobalState {
STATE.get_or_init(|| {
let mut logging = Logging::default();
let mut logging = default_logging;
// If we have an action executing, we disable logging by default
// since we write to stderr we don't want logs messing up our
@@ -146,7 +170,7 @@ pub fn init(opts: InitOptions) -> &'static GlobalState {
// Env vars are useful for logging too because they are easy to set.
if let Ok(v) = std::env::var("GHOSTTY_LOG") {
logging = Logging::parse(&v);
logging = Logging::parse_with(&v, default_logging);
}
init_logger(logging);