feat(macos): port Ghostty's macOS code paths (Metal, CoreText, Darwin)

Ports the macOS-specific Zig code behind cfg(target_os = "macos"):
- renderer: a GraphicsApi trait with generic::Renderer<G>; Metal backend
  (all renderer/metal/* files, shaders.metal verbatim, runtime library
  compile, IOSurface layer, CVDisplayLink), shadertoy -> MSL via naga;
  OpenGL remains the backend elsewhere.
- font: CoreText face, shaper and discovery with build.rs backend
  selection (coretext default; coretext-freetype / coretext-harfbuzz /
  coretext-noshape features), no fontconfig/freetype/harfbuzz on Apple.
- input: KeymapDarwin via UCKeyTranslate, option-as-alt, mac keycodes.
- os: macos, mach, cf_release_thread, NSProcessInfo args and macOS
  branches; config: macOS defaults, keybinds, Application Support paths.
- termio/core: login(1) wrapping, openpty/execvpe, Darwin child exit
  handling, os_log logging.

Compile-checked for aarch64/x86_64-apple-darwin (clippy -D warnings
clean) using the cross setup in docs/rewrite/notes/macos-M1.md; cannot
run here. The embedded runtime/C API (embedded.zig) is the next step.

Also includes the cross-platform integration edits shared by the crash,
verification, Windows and macOS work (Cargo target-gating, thread
crash-context hooks, apprt-gtk surface/IME refactor, termio/os cfg
gates), a cfg fix so ghostty-config builds on wasm32, and an allow for
the intentional recursion in the crash overflow test.

Test Plan:
- cargo test --workspace (3224 passed)
- cargo clippy --workspace --all-targets -- -D warnings
- cargo check --target aarch64-apple-darwin -p ghostty-renderer (with
  the documented cross env)
This commit is contained in:
ddidderr committed 2026-10-12 06:02:39 +02:00
1 parent 3c8fae1089
commit 452b782149
124 files changed
+14936 -1242

No files matched your search

+50 -6
View File
@@ -1,9 +1,10 @@
//! Creates a temporary directory at runtime that can be safely used to
//! store temporary data and is destroyed on drop.
#[cfg(unix)]
use std::os::unix::fs::DirBuilderExt;
use std::{
io,
os::unix::fs::DirBuilderExt,
path::{Path, PathBuf},
};
@@ -43,6 +44,48 @@ pub struct TempDir {
name: String,
}
/// Fill `buf` with random bytes (Zig: `std.crypto.random.bytes`).
#[cfg(any(target_os = "linux", target_os = "android"))]
fn fill_random(buf: &mut [u8]) -> io::Result<()> {
rustix::rand::getrandom(buf, rustix::rand::GetRandomFlags::empty())?;
Ok(())
}
/// Darwin has no `getrandom` syscall (Zig uses `getentropy`); read the
/// kernel CSPRNG device instead.
#[cfg(all(unix, not(any(target_os = "linux", target_os = "android"))))]
fn fill_random(buf: &mut [u8]) -> io::Result<()> {
use std::io::Read;
std::fs::File::open("/dev/urandom")?.read_exact(buf)
}
/// Windows: the system preferred RNG (`BCryptGenRandom`).
#[cfg(windows)]
#[allow(unsafe_code)]
fn fill_random(buf: &mut [u8]) -> io::Result<()> {
use windows_sys::Win32::Security::Cryptography::{
BCRYPT_USE_SYSTEM_PREFERRED_RNG,
BCryptGenRandom,
};
// SAFETY: `buf` is valid for `len` bytes; a null algorithm handle is
// allowed together with BCRYPT_USE_SYSTEM_PREFERRED_RNG.
let status = unsafe {
BCryptGenRandom(
std::ptr::null_mut(),
buf.as_mut_ptr(),
buf.len() as u32,
BCRYPT_USE_SYSTEM_PREFERRED_RNG,
)
};
if status < 0 {
return Err(io::Error::other(format!(
"BCryptGenRandom failed: {status:#x}"
)));
}
Ok(())
}
impl TempDir {
/// Create the temporary directory.
pub fn init() -> io::Result<TempDir> {
@@ -56,13 +99,14 @@ impl TempDir {
// We now loop forever until we can find a directory that we can create.
loop {
let mut rand_buf = [0u8; RANDOM_BYTES];
rustix::rand::getrandom(&mut rand_buf, rustix::rand::GetRandomFlags::empty())?;
fill_random(&mut rand_buf)?;
let name = b64_encode(&rand_buf);
match std::fs::DirBuilder::new()
.mode(0o755)
.create(parent.join(&name))
{
#[allow(unused_mut)]
let mut builder = std::fs::DirBuilder::new();
#[cfg(unix)]
builder.mode(0o755);
match builder.create(parent.join(&name)) {
Ok(()) => return Ok(TempDir { parent, name }),
Err(e) if e.kind() == io::ErrorKind::AlreadyExists => {}
Err(e) => return Err(e),