mirror of
https://github.com/ghostty-org/ghostty.git
synced 2026-04-18 13:30:29 +00:00
termio: exec uses new flatpak command, no more host-spawn
This commit is contained in:
@@ -22,6 +22,9 @@ pub const app_runtime = std.meta.stringToEnum(
|
||||
/// compiled.
|
||||
pub const devmode_enabled = artifact == .exe and app_runtime == .glfw;
|
||||
|
||||
/// We want to integrate with Flatpak APIs.
|
||||
pub const flatpak = options.flatpak;
|
||||
|
||||
pub const Artifact = enum {
|
||||
/// Standalone executable
|
||||
exe,
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
const std = @import("std");
|
||||
const builtin = @import("builtin");
|
||||
const build_config = @import("build_config.zig");
|
||||
const Allocator = std.mem.Allocator;
|
||||
const ArenaAllocator = std.heap.ArenaAllocator;
|
||||
const internal_os = @import("os/main.zig");
|
||||
@@ -49,8 +50,13 @@ pub fn get(alloc: Allocator) !Entry {
|
||||
// If we're in flatpak then our entry is always empty so we grab it
|
||||
// by shelling out to the host. note that we do HAVE an entry in the
|
||||
// sandbox but only the username is correct.
|
||||
if (internal_os.isFlatpak()) {
|
||||
log.info("flatpak detected, will use host-spawn to get our entry", .{});
|
||||
if (internal_os.isFlatpak()) flatpak: {
|
||||
if (comptime !build_config.flatpak) {
|
||||
log.warn("flatpak detected, but this build doesn't contain flatpak support", .{});
|
||||
break :flatpak;
|
||||
}
|
||||
|
||||
log.info("flatpak detected, will use host command to get our entry", .{});
|
||||
|
||||
// Note: we wrap our getent call in a /bin/sh login shell because
|
||||
// some operating systems (NixOS tested) don't set the PATH for various
|
||||
@@ -104,7 +110,6 @@ pub fn get(alloc: Allocator) !Entry {
|
||||
|
||||
break :output try output.toOwnedSlice(alloc);
|
||||
};
|
||||
log.warn("DONE output={s}", .{output});
|
||||
|
||||
// Shell and home are the last two entries
|
||||
var it = std.mem.splitBackwards(u8, std.mem.trimRight(u8, output, " \r\n"), ":");
|
||||
|
||||
@@ -322,6 +322,10 @@ fn ttyWrite(
|
||||
|
||||
/// Subprocess manages the lifecycle of the shell subprocess.
|
||||
const Subprocess = struct {
|
||||
/// If we build with flatpak support then we have to keep track of
|
||||
/// a potential execution on the host.
|
||||
const FlatpakHostCommand = if (build_config.flatpak) internal_os.FlatpakHostCommand else void;
|
||||
|
||||
arena: std.heap.ArenaAllocator,
|
||||
cwd: ?[]const u8,
|
||||
env: EnvMap,
|
||||
@@ -331,6 +335,7 @@ const Subprocess = struct {
|
||||
screen_size: renderer.ScreenSize,
|
||||
pty: ?Pty = null,
|
||||
command: ?Command = null,
|
||||
flatpak_command: ?FlatpakHostCommand = null,
|
||||
|
||||
/// Initialize the subprocess. This will NOT start it, this only sets
|
||||
/// up the internal state necessary to start it later.
|
||||
@@ -364,8 +369,18 @@ const Subprocess = struct {
|
||||
break :argv0 argv0_buf;
|
||||
} else null;
|
||||
|
||||
// Set our env vars
|
||||
var env = try std.process.getEnvMap(alloc);
|
||||
// Set our env vars. For Flatpak builds running in Flatpak we don't
|
||||
// inherit our environment because the login shell on the host side
|
||||
// will get it.
|
||||
var env = env: {
|
||||
if (comptime build_config.flatpak) {
|
||||
if (internal_os.isFlatpak()) {
|
||||
break :env std.process.EnvMap.init(alloc);
|
||||
}
|
||||
}
|
||||
|
||||
break :env try std.process.getEnvMap(alloc);
|
||||
};
|
||||
errdefer env.deinit();
|
||||
try env.put("TERM", "xterm-256color");
|
||||
try env.put("COLORTERM", "truecolor");
|
||||
@@ -394,13 +409,9 @@ const Subprocess = struct {
|
||||
var args = try std.ArrayList([]const u8).initCapacity(alloc, 8);
|
||||
defer args.deinit();
|
||||
|
||||
// We use host-spawn so the PTY is setup properly.
|
||||
// future: rewrite host-spawn into pure Zig using dbus and
|
||||
// we can run this directly.
|
||||
try args.append("/app/bin/host-spawn");
|
||||
try args.append("-pty");
|
||||
try args.append("-env");
|
||||
try args.append("TERM,COLORTERM");
|
||||
// We run our shell wrapped in a /bin/sh login shell because
|
||||
// some systems do not properly initialize the env vars unless
|
||||
// we start this way (NixOS!)
|
||||
try args.append("/bin/sh");
|
||||
try args.append("-l");
|
||||
try args.append("-c");
|
||||
@@ -451,6 +462,37 @@ const Subprocess = struct {
|
||||
self.args,
|
||||
});
|
||||
|
||||
// In flatpak, we use the HostCommand to execute our shell.
|
||||
if (internal_os.isFlatpak()) flatpak: {
|
||||
if (comptime !build_config.flatpak) {
|
||||
log.warn("flatpak detected, but flatpak support not built-in", .{});
|
||||
break :flatpak;
|
||||
}
|
||||
|
||||
// For flatpak our path and argv[0] must match because that is
|
||||
// used for execution by the dbus API.
|
||||
assert(std.mem.eql(u8, self.path, self.args[0]));
|
||||
|
||||
// Flatpak command must have a stable pointer.
|
||||
self.flatpak_command = .{
|
||||
.argv = self.args,
|
||||
.env = &self.env,
|
||||
.stdin = pty.slave,
|
||||
.stdout = pty.slave,
|
||||
.stderr = pty.slave,
|
||||
};
|
||||
var cmd = &self.flatpak_command.?;
|
||||
const pid = try cmd.spawn(alloc);
|
||||
errdefer killCommandFlatpak(cmd);
|
||||
|
||||
log.info("started subcommand on host via flatpak API path={s} pid={?}", .{
|
||||
self.path,
|
||||
pid,
|
||||
});
|
||||
|
||||
return pty.master;
|
||||
}
|
||||
|
||||
// Build our subcommand
|
||||
var cmd: Command = .{
|
||||
.path = self.path,
|
||||
@@ -489,6 +531,17 @@ const Subprocess = struct {
|
||||
self.command = null;
|
||||
}
|
||||
|
||||
// Kill our Flatpak command
|
||||
if (FlatpakHostCommand != void) {
|
||||
if (self.flatpak_command) |*cmd| {
|
||||
killCommandFlatpak(cmd) catch |err|
|
||||
log.err("error sending SIGHUP to command, may hang: {}", .{err});
|
||||
_ = cmd.wait() catch |err|
|
||||
log.err("error waiting for command to exit: {}", .{err});
|
||||
self.flatpak_command = null;
|
||||
}
|
||||
}
|
||||
|
||||
// Close our PTY. We do this after killing our command because on
|
||||
// macOS, close will block until all blocking operations read/write
|
||||
// are done with it and our reader thread is probably still alive.
|
||||
@@ -547,6 +600,11 @@ const Subprocess = struct {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn killCommandFlatpak(command: *FlatpakHostCommand) !void {
|
||||
// TODO
|
||||
_ = command;
|
||||
}
|
||||
};
|
||||
|
||||
/// The read thread sits in a loop doing the following pseudo code:
|
||||
|
||||
Reference in New Issue
Block a user