ghostty/src/os/dbus.zig
Mitchell Hashimoto 5306e7cf56 config: add launched-from to specify launch source
Related to #7433

This extracts our "launched from desktop" logic into a config option.
The default value is detection using the same logic as before, but now
this can be overridden by the user.

This also adds the systemd and dbus activation sources from #7433.

There are a number of reasons why we decided to do this:

  1. It automatically gets us caching since the configuration is only
     loaded once (per reload, a rare occurrence).

  2. It allows us to override the logic when testing. Previously, we
     had to do more complex environment faking to get the same
     behavior.

  3. It forces exhaustive switches in any desktop handling code, which
     will make it easier to ensure valid behaviors if we introduce new
     launch sources (as we are in #7433).

  4. It lowers code complexity since callsites don't need to have N
     `launchedFromX()` checks and can use a single value.
2025-06-02 08:45:02 -07:00

22 lines
884 B
Zig

const std = @import("std");
const builtin = @import("builtin");
/// Returns true if the program was launched by D-Bus activation.
///
/// On Linux GTK, this returns true if the program was launched using D-Bus
/// activation. It will return false if Ghostty was launched any other way.
///
/// For other platforms and app runtimes, this returns false.
pub fn launchedByDbusActivation() bool {
return switch (builtin.os.tag) {
// On Linux, D-Bus activation sets `DBUS_STARTER_ADDRESS` and
// `DBUS_STARTER_BUS_TYPE`. If these environment variables are present
// (no matter the value) we were launched by D-Bus activation.
.linux => std.posix.getenv("DBUS_STARTER_ADDRESS") != null and
std.posix.getenv("DBUS_STARTER_BUS_TYPE") != null,
// No other system supports D-Bus so always return false.
else => false,
};
}