diff options
| author | Can Celik <ogulcancelik@gmail.com> | 2026-09-10 21:51:57 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-09-10 21:51:57 +0300 |
| commit | 425c86179791cd32e2d4ce0ae7f269940b4b0663 (patch) | |
| tree | e52c4383a6d69216cbe2a32638bf90841d7e2189 /vendor/libghostty-vt/src/os/desktop.zig | |
| parent | 90e947a6e26e02f10ec7f1694c4eb5b78a86406b (diff) | |
chore: upgrade libghostty and preserve terminal behavior (#3906)
Diffstat (limited to 'vendor/libghostty-vt/src/os/desktop.zig')
| -rw-r--r-- | vendor/libghostty-vt/src/os/desktop.zig | 97 |
1 files changed, 53 insertions, 44 deletions
diff --git a/vendor/libghostty-vt/src/os/desktop.zig b/vendor/libghostty-vt/src/os/desktop.zig index 93bfb74b..60ec51a5 100644 --- a/vendor/libghostty-vt/src/os/desktop.zig +++ b/vendor/libghostty-vt/src/os/desktop.zig @@ -1,7 +1,7 @@ const std = @import("std"); const builtin = @import("builtin"); const build_config = @import("../build_config.zig"); -const posix = std.posix; +const global = @import("../global.zig"); const c = @cImport({ @cInclude("unistd.h"); @@ -26,7 +26,7 @@ pub fn launchedFromDesktop() bool { // was launched from the desktop. if (build_config.artifact == .lib) lib: { const env = "GHOSTTY_MAC_LAUNCH_SOURCE"; - const source = posix.getenv(env) orelse break :lib; + const source = global.environ().getPosix(env) orelse break :lib; // Source can be "app", "cli", or "zig_run". We assume // its the desktop only if its "app". We may want to do @@ -44,7 +44,7 @@ pub fn launchedFromDesktop() bool { // another terminal was launched from a desktop file and then launches // Ghostty and Ghostty inherits the env. .linux, .freebsd => ul: { - const gio_pid_str = posix.getenv("GIO_LAUNCHED_DESKTOP_FILE_PID") orelse + const gio_pid_str = global.environ().getPosix("GIO_LAUNCHED_DESKTOP_FILE_PID") orelse break :ul false; const pid = c.getpid(); @@ -80,7 +80,7 @@ pub const DesktopEnvironment = enum { /// Detect what desktop environment we are running under. This is mainly used /// on Linux and BSD to enable or disable certain features but there may be more uses in /// the future. -pub fn desktopEnvironment() DesktopEnvironment { +pub fn desktopEnvironment(environ_map: *const std.process.Environ.Map) DesktopEnvironment { return switch (comptime builtin.os.tag) { .macos => .macos, .windows => .windows, @@ -89,7 +89,7 @@ pub fn desktopEnvironment() DesktopEnvironment { // Use $XDG_SESSION_DESKTOP to determine what DE we are using on Linux // https://www.freedesktop.org/software/systemd/man/latest/pam_systemd.html#desktop= - if (posix.getenv("XDG_SESSION_DESKTOP")) |sd| { + if (environ_map.get("XDG_SESSION_DESKTOP")) |sd| { if (std.ascii.eqlIgnoreCase("gnome", sd)) break :de .gnome; if (std.ascii.eqlIgnoreCase("gnome-xorg", sd)) break :de .gnome; } @@ -99,7 +99,7 @@ pub fn desktopEnvironment() DesktopEnvironment { // colon-separated list of up to three desktop names, although we // only look at the first. // https://specifications.freedesktop.org/desktop-entry-spec/latest/recognized-keys.html - if (posix.getenv("XDG_CURRENT_DESKTOP")) |cd| { + if (environ_map.get("XDG_CURRENT_DESKTOP")) |cd| { var cd_it = std.mem.splitScalar(u8, cd, ':'); const cd_first = cd_it.first(); if (std.ascii.eqlIgnoreCase(cd_first, "gnome")) break :de .gnome; @@ -114,46 +114,55 @@ pub fn desktopEnvironment() DesktopEnvironment { test "desktop environment" { const testing = std.testing; + // Always run these tests with a blank evironment map so that we don't get any + // failures from values in the "real" evironment leaking in. switch (builtin.os.tag) { - .macos => try testing.expectEqual(.macos, desktopEnvironment()), - .windows => try testing.expectEqual(.windows, desktopEnvironment()), + inline .macos, .windows => |tag| { + var environ_map: std.process.Environ.Map = .init(testing.allocator); + defer environ_map.deinit(); + try testing.expectEqual(@tagName(tag), @tagName(desktopEnvironment(&environ_map))); + }, .linux, .freebsd => { - const getenv = std.posix.getenv; - const setenv = @import("env.zig").setenv; - const unsetenv = @import("env.zig").unsetenv; - - const xdg_current_desktop = getenv("XDG_CURRENT_DESKTOP"); - defer if (xdg_current_desktop) |v| { - _ = setenv("XDG_CURRENT_DESKTOP", v); - } else { - _ = unsetenv("XDG_CURRENT_DESKTOP"); - }; - _ = unsetenv("XDG_CURRENT_DESKTOP"); - - const xdg_session_desktop = getenv("XDG_SESSION_DESKTOP"); - defer if (xdg_session_desktop) |v| { - _ = setenv("XDG_SESSION_DESKTOP", v); - } else { - _ = unsetenv("XDG_SESSION_DESKTOP"); - }; - _ = unsetenv("XDG_SESSION_DESKTOP"); - - _ = setenv("XDG_SESSION_DESKTOP", "gnome"); - try testing.expectEqual(.gnome, desktopEnvironment()); - _ = setenv("XDG_SESSION_DESKTOP", "gnome-xorg"); - try testing.expectEqual(.gnome, desktopEnvironment()); - _ = setenv("XDG_SESSION_DESKTOP", "foobar"); - try testing.expectEqual(.other, desktopEnvironment()); - - _ = unsetenv("XDG_SESSION_DESKTOP"); - try testing.expectEqual(.other, desktopEnvironment()); - - _ = setenv("XDG_CURRENT_DESKTOP", "GNOME"); - try testing.expectEqual(.gnome, desktopEnvironment()); - _ = setenv("XDG_CURRENT_DESKTOP", "FOOBAR"); - try testing.expectEqual(.other, desktopEnvironment()); - _ = unsetenv("XDG_CURRENT_DESKTOP"); - try testing.expectEqual(.other, desktopEnvironment()); + { + var environ_map: std.process.Environ.Map = .init(testing.allocator); + defer environ_map.deinit(); + try environ_map.put("XDG_SESSION_DESKTOP", "gnome"); + try testing.expectEqual(.gnome, desktopEnvironment(&environ_map)); + } + + { + var environ_map: std.process.Environ.Map = .init(testing.allocator); + defer environ_map.deinit(); + try environ_map.put("XDG_SESSION_DESKTOP", "gnome-xorg"); + try testing.expectEqual(.gnome, desktopEnvironment(&environ_map)); + } + + { + var environ_map: std.process.Environ.Map = .init(testing.allocator); + defer environ_map.deinit(); + try environ_map.put("XDG_SESSION_DESKTOP", "foobar"); + try testing.expectEqual(.other, desktopEnvironment(&environ_map)); + } + + { + var environ_map: std.process.Environ.Map = .init(testing.allocator); + defer environ_map.deinit(); + try environ_map.put("XDG_CURRENT_DESKTOP", "GNOME"); + try testing.expectEqual(.gnome, desktopEnvironment(&environ_map)); + } + + { + var environ_map: std.process.Environ.Map = .init(testing.allocator); + defer environ_map.deinit(); + try environ_map.put("XDG_CURRENT_DESKTOP", "FOOBAR"); + try testing.expectEqual(.other, desktopEnvironment(&environ_map)); + } + + { + var environ_map: std.process.Environ.Map = .init(testing.allocator); + defer environ_map.deinit(); + try testing.expectEqual(.other, desktopEnvironment(&environ_map)); + } }, else => try testing.expectEqual(.other, DesktopEnvironment()), } |
