core: detect what desktop environment the user is using (#4343)

This commit is contained in:
Mitchell Hashimoto
2025-01-02 16:44:16 -08:00
committed by GitHub
3 changed files with 29 additions and 0 deletions

View File

@ -3,6 +3,7 @@ const build_options = @import("build_options");
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
const builtin = @import("builtin"); const builtin = @import("builtin");
const build_config = @import("../build_config.zig"); const build_config = @import("../build_config.zig");
const internal_os = @import("../os/main.zig");
const xev = @import("xev"); const xev = @import("xev");
const renderer = @import("../renderer.zig"); const renderer = @import("../renderer.zig");
const gtk = if (build_config.app_runtime == .gtk) @import("../apprt/gtk/c.zig").c else void; const gtk = if (build_config.app_runtime == .gtk) @import("../apprt/gtk/c.zig").c else void;
@ -37,6 +38,7 @@ pub fn run(alloc: Allocator) !u8 {
try stdout.print(" - renderer : {}\n", .{renderer.Renderer}); try stdout.print(" - renderer : {}\n", .{renderer.Renderer});
try stdout.print(" - libxev : {}\n", .{xev.backend}); try stdout.print(" - libxev : {}\n", .{xev.backend});
if (comptime build_config.app_runtime == .gtk) { if (comptime build_config.app_runtime == .gtk) {
try stdout.print(" - desktop env: {s}\n", .{@tagName(internal_os.desktopEnvironment())});
try stdout.print(" - GTK version:\n", .{}); try stdout.print(" - GTK version:\n", .{});
try stdout.print(" build : {d}.{d}.{d}\n", .{ try stdout.print(" build : {d}.{d}.{d}\n", .{
gtk.GTK_MAJOR_VERSION, gtk.GTK_MAJOR_VERSION,

View File

@ -59,3 +59,29 @@ pub fn launchedFromDesktop() bool {
else => @compileError("unsupported platform"), else => @compileError("unsupported platform"),
}; };
} }
pub const DesktopEnvironment = enum {
gnome,
macos,
other,
windows,
};
/// Detect what desktop environment we are running under. This is mainly used on
/// Linux to enable or disable GTK client-side decorations but there may be more
/// uses in the future.
pub fn desktopEnvironment() DesktopEnvironment {
return switch (comptime builtin.os.tag) {
.macos => .macos,
.windows => .windows,
.linux => de: {
if (@inComptime()) @compileError("Checking for the desktop environment on Linux must be done at runtime.");
// 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=
const de = posix.getenv("XDG_SESSION_DESKTOP") orelse break :de .other;
if (std.ascii.eqlIgnoreCase("gnome", de)) break :de .gnome;
break :de .other;
},
else => .other,
};
}

View File

@ -32,6 +32,7 @@ pub const getenv = env.getenv;
pub const setenv = env.setenv; pub const setenv = env.setenv;
pub const unsetenv = env.unsetenv; pub const unsetenv = env.unsetenv;
pub const launchedFromDesktop = desktop.launchedFromDesktop; pub const launchedFromDesktop = desktop.launchedFromDesktop;
pub const desktopEnvironment = desktop.desktopEnvironment;
pub const rlimit = file.rlimit; pub const rlimit = file.rlimit;
pub const fixMaxFiles = file.fixMaxFiles; pub const fixMaxFiles = file.fixMaxFiles;
pub const restoreMaxFiles = file.restoreMaxFiles; pub const restoreMaxFiles = file.restoreMaxFiles;