From 1093ccecb45300f160402ad2e688523c525d182b Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 6 Jun 2024 10:01:32 -0700 Subject: [PATCH] xdg-terminal-exec invocations set title based on command Fixes #1724 See background in #1724. The general idea is that for Ghostty invocations via xdg-terminal-exec, we set the initial title to the command automatically so that window managers can modify the styling. We only do this for xdg-terminal-exec because that protocol/spec is specifically for the scenario that the terminal is being used to launch a command from the desktop environment. --- src/Surface.zig | 22 +++++++++++++++++++++- src/config/Config.zig | 12 ++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/Surface.zig b/src/Surface.zig index 2673b9722..354059921 100644 --- a/src/Surface.zig +++ b/src/Surface.zig @@ -513,7 +513,27 @@ pub fn init( }; } - if (config.title) |title| try rt_surface.setTitle(title); + if (config.title) |title| { + try rt_surface.setTitle(title); + } else if ((comptime builtin.os.tag == .linux) and + config.@"_xdg-terminal-exec") + xdg: { + // For xdg-terminal-exec execution we special-case and set the window + // title to the command being executed. This allows window managers + // to set custom styling based on the command being executed. + const command = config.command orelse break :xdg; + if (command.len > 0) { + const title = alloc.dupeZ(u8, command) catch |err| { + log.warn( + "error copying command for title, title will not be set err={}", + .{err}, + ); + break :xdg; + }; + defer alloc.free(title); + try rt_surface.setTitle(title); + } + } } pub fn deinit(self: *Surface) void { diff --git a/src/config/Config.zig b/src/config/Config.zig index fdaa1d0c4..83931fb52 100644 --- a/src/config/Config.zig +++ b/src/config/Config.zig @@ -1108,6 +1108,9 @@ _errors: ErrorList = .{}, /// as loadTheme which has more details on why. _replay_steps: std.ArrayListUnmanaged(Replay.Step) = .{}, +/// Set to true if Ghostty was executed as xdg-terminal-exec on Linux. +@"_xdg-terminal-exec": bool = false, + pub fn deinit(self: *Config) void { if (self._arena) |arena| arena.deinit(); self.* = undefined; @@ -1654,6 +1657,14 @@ pub fn loadCliArgs(self: *Config, alloc_gpa: Allocator) !void { // On Linux, we have a special case where if the executing // program is "xdg-terminal-exec" then we treat all CLI // args as if they are a command to execute. + // + // In this mode, we also behave slightly differently: + // + // - The initial window title is set to the full command. This + // can be used with window managers to modify positioning, + // styling, etc. based on the command. + // + // See: https://github.com/Vladimir-csp/xdg-terminal-exec if (comptime builtin.os.tag == .linux) xdg: { if (!std.mem.eql( u8, @@ -1679,6 +1690,7 @@ pub fn loadCliArgs(self: *Config, alloc_gpa: Allocator) !void { try command.append(' '); } + self.@"_xdg-terminal-exec" = true; self.command = command.items[0 .. command.items.len - 1]; return; }