Add ghostty_surface_foreground_pid C API for cmux#46
Conversation
Exposes the foreground process group PID of a surface's pty master via a new ghostty_surface_foreground_pid() C API. Returns the PID as int32_t, or -1 when unavailable (no pty, tcgetpgrp failure, or PID > INT32_MAX). Backed by the existing Surface.getProcessInfo(.foreground_pid) path, which uses tcgetpgrp(master_fd) on macOS. Used by cmux to detect when a CLI tool (Claude Code / Codex / Gemini) is the foreground process so it can switch Enter-key semantics on a per-process basis (cmux-specific).
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughA new public C-ABI function is introduced to expose the foreground process ID of a Ghostty terminal surface. The function declaration is added to the public header file, and its implementation in the embedded runtime queries the process information from the surface core, returning the foreground PID or -1 on failure. Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~5 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Greptile SummaryThis PR exposes the foreground process group ID of a surface's pty master as a new C API function Confidence Score: 5/5Safe to merge — minimal, well-scoped addition that follows established patterns with no correctness issues. The implementation is straightforward, delegates to a well-tested existing code path, correctly handles all failure cases, and matches the C header declaration. The only finding is a P2 style nit on an unnecessary cast. No files require special attention. Important Files Changed
Sequence DiagramsequenceDiagram
participant C as cmux (C caller)
participant API as ghostty_surface_foreground_pid
participant S as Surface.getProcessInfo
participant T as Termio.getProcessInfo
participant E as Exec.getProcessInfo
participant P as PosixPty.tcgetpgrp
C->>API: ghostty_surface_foreground_pid(surface)
API->>S: core_surface.getProcessInfo(.foreground_pid)
S->>T: io.getProcessInfo(.foreground_pid)
T->>E: backend.getProcessInfo(.foreground_pid)
E->>P: subprocess.pty.getProcessInfo(.foreground_pid)
P-->>E: u64 (pgid) or null
E-->>T: ?u64
T-->>S: ?u64
S-->>API: ?u64
API-->>C: int32_t (pgid) or -1
Reviews (1): Last reviewed commit: "Add ghostty_surface_foreground_pid C API..." | Re-trigger Greptile |
| /// pty master, or -1 if unavailable (cmux-specific). | ||
| export fn ghostty_surface_foreground_pid(surface: *Surface) i32 { | ||
| const raw = surface.core_surface.getProcessInfo(.foreground_pid) orelse return -1; | ||
| if (raw > @as(u64, @intCast(std.math.maxInt(i32)))) return -1; |
There was a problem hiding this comment.
Redundant cast in overflow guard
std.math.maxInt(i32) returns a comptime_int, so Zig will coerce it to u64 automatically in the comparison — the @as(u64, @intCast(...)) wrapper is unnecessary. The simpler form is equivalent:
| if (raw > @as(u64, @intCast(std.math.maxInt(i32)))) return -1; | |
| if (raw > std.math.maxInt(i32)) return -1; |
Summary
ghostty_surface_foreground_pid()C API that exposes the foreground process group PID (asint32_t) of a surface's pty master.-1when unavailable (no pty /tcgetpgrpfailure / PID >INT32_MAX).Surface.getProcessInfo(.foreground_pid)path, which usestcgetpgrp(master_fd)on macOS.Motivation
cmux uses this to detect when a CLI tool (Claude Code / Codex / Gemini) is the foreground process of a pty so it can switch Enter-key semantics on a per-process basis (Enter=newline for CLI, Enter=submit otherwise).
Files
include/ghostty.hsrc/apprt/embedded.zigFollows the pattern of the existing cmux-specific exports (
ghostty_surface_select_cursor_cell,ghostty_surface_clear_selection).Test plan
zig build -Demit-xcframework=true -Dxcframework-target=universal -Doptimize=ReleaseFastsucceedsnmon the resultinglibghostty.ashowsT _ghostty_surface_foreground_pid🤖 Generated with Claude Code
Summary by cubic
Adds a C API
ghostty_surface_foreground_pid()to fetch the foreground process group PID for a surface’s pty master. This enablescmuxto switch Enter-key behavior when a CLI tool is in the foreground.int32_tPID;-1if no pty,tcgetpgrpfails, or PID >INT32_MAX.Surface.getProcessInfo(.foreground_pid)under the hood (tcgetpgrpon macOS).Written for commit 22ad48b. Summary will update on new commits.
Summary by CodeRabbit