Files
ghostty-rs/crates/ghostty-termio/src/wakeup.rs
T
ddidderr 3266108c10 feat(core): port App/Surface and wire up the runnable ghostty binary
This is the integration step that turns the ported crates into a working
terminal: `cargo run` now opens a GTK window running the user's shell.

ghostty-core ports App.zig, Surface.zig (split into keyboard, mouse,
binding, clipboard and config submodules), surface_mouse.zig and
global.zig, and implements the runtime traits. perform_binding_action
handles every binding action.

Cross-crate integration:
- The core<->runtime traits (CoreApp, CoreSurface, RtApp, RtSurface,
  Waker) move from apprt-gtk into ghostty_apprt::runtime_api so the core
  stays runtime-agnostic; Scrollbar/RendererHealth stand-ins are replaced
  by the real terminal/renderer types.
- termio now uses ghostty_renderer::state::{State, SharedState}; the
  renderer calls kitty_update; image-storage-limit is applied at init
  and on config change.
- The search thread locks the terminal through a TerminalLock trait so
  it can share the render state mutex; termio's Wakeup gained a hook to
  wake the renderer thread.
- apprt-gtk: GL proc addresses are resolved through libepoxy's
  epoxy_glFoo pointer variables (plain glFoo symbols resolved to null),
  default DPI fixed to 96, libadwaita version read without GTK init so
  --version works headless.

The root binary dispatches +actions to ghostty-cli (list-fonts,
show-face and new-window are now wired) and otherwise runs the GTK app
with the real core.

Verified at runtime on Wayland and X11: shell prompt and colored output
render, child exit closes the window cleanly. Not yet verified: keyboard
and mouse input, clipboard, IME, tabs and splits (no key injection was
possible in this environment). GHOSTTY_DEBUG_SCREENSHOT=<png> dumps the
renderer's last frame for headless checks.

Test Plan:
- cargo test --workspace (2890 passed)
- cargo clippy --workspace --all-targets -- -D warnings
- GSETTINGS_BACKEND=memory GHOSTTY_RESOURCES_DIR=/usr/share/ghostty \
  cargo run
2026-10-12 01:33:44 +02:00

115 lines
3.4 KiB
Rust

//! A tiny cross-thread wakeup primitive (replaces `xev.Async`).
//!
//! Backed by a non-blocking `eventfd`, so it can be included in a `poll(2)`
//! set next to a pty fd or a pidfd. [`Wakeup::notify`] may be called from any
//! thread, any number of times; the waiter calls [`Wakeup::drain`] after
//! `poll` reports the fd readable.
use std::io;
use std::os::fd::{AsFd, BorrowedFd, OwnedFd};
use std::sync::Arc;
use rustix::event::{EventfdFlags, eventfd};
/// See the module docs. Cloning shares the same underlying eventfd.
#[derive(Clone)]
pub struct Wakeup(Arc<OwnedFd>, Option<Arc<dyn Fn() + Send + Sync>>);
impl std::fmt::Debug for Wakeup {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("Wakeup").field(&self.0).finish()
}
}
impl Wakeup {
pub fn new() -> io::Result<Wakeup> {
let fd = eventfd(0, EventfdFlags::CLOEXEC | EventfdFlags::NONBLOCK)?;
Ok(Wakeup(Arc::new(fd), None))
}
/// A wakeup that calls `hook` instead of signalling an eventfd. Used to
/// bridge into waiters that are not `poll(2)`-based (the renderer
/// thread's channel). `as_fd`/`drain` are meaningless for these.
pub fn with_hook(hook: Arc<dyn Fn() + Send + Sync>) -> io::Result<Wakeup> {
let fd = eventfd(0, EventfdFlags::CLOEXEC | EventfdFlags::NONBLOCK)?;
Ok(Wakeup(Arc::new(fd), Some(hook)))
}
/// Wake up the waiter. Multiple notifications coalesce.
pub fn notify(&self) -> io::Result<()> {
if let Some(hook) = &self.1 {
hook();
return Ok(());
}
loop {
match rustix::io::write(&*self.0, &1u64.to_ne_bytes()) {
Ok(_) => return Ok(()),
Err(rustix::io::Errno::INTR) => {}
// Counter would overflow: the waiter is already guaranteed
// to wake up.
Err(rustix::io::Errno::AGAIN) => return Ok(()),
Err(e) => return Err(e.into()),
}
}
}
/// Reset the notification state. Returns true if a notification was
/// pending.
pub fn drain(&self) -> bool {
let mut buf = [0u8; 8];
loop {
match rustix::io::read(&*self.0, &mut buf) {
Ok(_) => return true,
Err(rustix::io::Errno::INTR) => {}
Err(_) => return false,
}
}
}
/// The fd to poll for `POLLIN`.
pub fn as_fd(&self) -> BorrowedFd<'_> {
self.0.as_fd()
}
}
#[cfg(test)]
mod tests {
use super::*;
use rustix::event::{PollFd, PollFlags, poll};
fn readable(w: &Wakeup) -> bool {
let mut fds = [PollFd::from_borrowed_fd(w.as_fd(), PollFlags::IN)];
poll(
&mut fds,
Some(&rustix::time::Timespec {
tv_sec: 0,
tv_nsec: 0,
}),
)
.unwrap()
> 0
}
#[test]
fn notify_coalesces_and_drains() {
let w = Wakeup::new().unwrap();
assert!(!readable(&w));
w.notify().unwrap();
w.notify().unwrap();
assert!(readable(&w));
assert!(w.drain());
assert!(!readable(&w));
assert!(!w.drain());
}
#[test]
fn notify_from_other_thread() {
let w = Wakeup::new().unwrap();
let w2 = w.clone();
std::thread::spawn(move || w2.notify().unwrap())
.join()
.unwrap();
assert!(readable(&w));
}
}