Files
ghostty-rs/crates/ghostty-apprt-gtk/src/cgroup.rs
T
ddidderr 88cc13871c feat(apprt-gtk): port the GTK4/libadwaita app runtime
Ports ZIG/src/apprt/gtk into ghostty-apprt-gtk: every GObject class
(application, window, tab, split tree, surface + scrolled window,
child-exited banner, search/key-state/resize overlays, command palette,
global shortcuts, all dialogs, config wrapper) as glib subclasses with
the same GType names, properties, signals and template children; GDK
key translation; X11 and Wayland window-protocol helpers (blur, CSD,
quick terminal via gtk4-layer-shell); single-instance D-Bus IPC;
gsettings; cgroup scope creation.

build.rs compiles Ghostty's blueprints with blueprint-compiler and
bundles UI, CSS and icons into a gresource, as Ghostty's build does.

The core is not ported yet, so the crate defines the boundary it needs
in src/core.rs (CoreApp, CoreSurface, RtApp, RtSurface, Waker); the
spec is in docs/rewrite/notes/ghostty-apprt-gtk.md. ghostty-core will
implement these traits (the crate no longer depends on core to avoid a
cycle). examples/demo.rs runs the app with a dummy core that clears the
GLArea; it launches on Wayland without GTK criticals.

Stubs/open items: toggle_visibility, float_window, secure_input and
close_all_windows return false; inspector is a no-op; X11/Wayland
protocol code is untested against real compositors; gtk4-rs doesn't
expose the GLArea framebuffer id, so the renderer must query it.

Also registers ghostty-apprt and ghostty-cli in workspace.dependencies.

Test Plan:
- cargo test -p ghostty-apprt-gtk (36 tests)
- DEMO_QUIT_AFTER_MS=3000 cargo run -p ghostty-apprt-gtk --example demo
2026-10-11 22:57:15 +02:00

87 lines
2.7 KiB
Rust

//! Transient systemd scope creation over D-Bus (port of `apprt/gtk/cgroup.zig`).
use gtk::gio;
use gtk::glib;
use gtk::prelude::*;
/// Properties for the transient scope.
#[derive(Debug, Clone, Copy, Default)]
pub struct Options {
/// `MemoryHigh` in bytes.
pub memory_high: Option<u64>,
/// `TasksMax`.
pub tasks_max: Option<u64>,
}
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("D-Bus call failed")]
DbusCallFailed,
}
/// The scope unit name for the given PID.
pub fn fmt_scope(pid: u32) -> String {
format!("app-ghostty-surface-transient-{pid}.scope")
}
/// Create a transient systemd scope containing `pid` via
/// `org.freedesktop.systemd1.Manager.StartTransientUnit`.
pub fn create_scope(dbus: &gio::DBusConnection, pid: u32, options: Options) -> Result<(), Error> {
// The unit name needs to be unique. We use the PID for this.
let name = fmt_scope(pid);
// Properties
let mut properties: Vec<(String, glib::Variant)> = Vec::new();
if let Some(v) = options.memory_high {
properties.push(("MemoryHigh".into(), v.to_variant()));
}
if let Some(v) = options.tasks_max {
properties.push(("TasksMax".into(), v.to_variant()));
}
// https://www.freedesktop.org/software/systemd/man/latest/systemd-oomd.service.html
properties.push(("ManagedOOMMemoryPressure".into(), "kill".to_variant()));
// PID to move into the unit
properties.push(("PIDs".into(), vec![pid].to_variant()));
// Aux - unused but must be present
let aux: Vec<(String, Vec<(String, glib::Variant)>)> = Vec::new();
let params = (name, "fail".to_owned(), properties, aux).to_variant();
dbus.call_sync(
Some("org.freedesktop.systemd1"),
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"StartTransientUnit",
Some(&params),
Some(glib::VariantTy::new("(o)").map_err(|_| Error::DbusCallFailed)?),
gio::DBusCallFlags::NONE,
-1,
gio::Cancellable::NONE,
)
.map(|_| ())
.map_err(|e| {
log::error!("creating transient cgroup scope failed err={e}");
Error::DbusCallFailed
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn fmt_scope_test() {
assert_eq!(fmt_scope(42), "app-ghostty-surface-transient-42.scope");
assert!(fmt_scope(u32::MAX).len() < 256);
}
#[test]
fn scope_params_signature() {
let props: Vec<(String, glib::Variant)> = vec![("PIDs".into(), vec![1u32].to_variant())];
let aux: Vec<(String, Vec<(String, glib::Variant)>)> = Vec::new();
let v = ("n".to_owned(), "fail".to_owned(), props, aux).to_variant();
assert_eq!(v.type_().as_str(), "(ssa(sv)a(sa(sv)))");
}
}