-- ─────────────────────────────────────────────────────────────────────────────
-- Environment
--
-- hl.env() sets variables before the display server initializes, which is what
-- makes it valid for AQ_* and cursor variables.
--
-- NOTE for uwsm sessions: if you log in via "Hyprland (uwsm-managed)", uwsm
-- prefers these live in ~/.config/uwsm/env and ~/.config/uwsm/env-hyprland
-- instead. They still work here; see the session notes in autostart.lua.
-- ─────────────────────────────────────────────────────────────────────────────

local prefs = require("prefs")

-- ── GPU selection ───────────────────────────────────────────────────────────
-- This box has a discrete RX 7800 XT (0000:03:00.0) and a Granite Ridge iGPU
-- (0000:12:00.0). The monitor hangs off the dGPU, so the dGPU must render.
--
-- /dev/dri/cardN numbering is not stable across boots, and the by-path names
-- contain colons -- which are AQ_DRM_DEVICES' own separator, so they cannot be
-- used directly. Panama installs a udev rule creating colon-free stable
-- symlinks (/dev/dri/amd-dgpu, /dev/dri/amd-igpu); see
-- config/copy/etc/udev/rules.d/99-panama-gpu.rules
--
-- First entry is the primary renderer. The iGPU stays in the list so anything
-- plugged into its outputs still lights up.
--
-- Guarded on the symlink actually existing: this config is shared across
-- machines, and pointing AQ_DRM_DEVICES at a device that isn't there stops
-- Hyprland from starting at all. If the udev rule hasn't been installed (or
-- this is a different box), we say nothing and let aquamarine choose.
local function exists(path)
    local f = io.open(path, "r")
    if f then f:close() return true end
    return false
end

if exists("/dev/dri/amd-dgpu") then
    local devices = "/dev/dri/amd-dgpu"
    if exists("/dev/dri/amd-igpu") then
        devices = devices .. ":/dev/dri/amd-igpu"
    end
    hl.env("AQ_DRM_DEVICES", devices)
end

-- ── Cursor ──────────────────────────────────────────────────────────────────
-- Theme carried over from GNOME.
--
-- Deliberately no HYPRCURSOR_THEME: oreo_blue_cursors is a plain XCursor theme
-- (it has cursors/ + index.theme and no hyprcursors/ directory or manifest.hl),
-- so setting it would point hyprcursor at nothing. Hyprland falls back to the
-- XCursor path, which is what we want.
hl.env("XCURSOR_THEME", prefs.get("cursorTheme", "oreo_blue_cursors"))
hl.env("XCURSOR_SIZE", "24")

-- ── Toolkits ────────────────────────────────────────────────────────────────
-- Wayland first, X11 only as a fallback for apps that genuinely need it.
hl.env("QT_QPA_PLATFORM", "wayland;xcb")

-- "gtk3" makes Qt derive its palette from the live GTK3 theme via the qgtk3
-- platform plugin, so Qt apps match adw-gtk3-dark exactly rather than
-- approximating it -- and it gives them GTK file/font/color dialogs. It also
-- covers Qt5 and Qt6 with one value.
--
-- Note this is a single value, NOT a fallback list: Qt splits on ":" and uses
-- only the first token, so the commonly-copied "qt5ct:qt6ct" silently means
-- just "qt5ct".
hl.env("QT_QPA_PLATFORMTHEME", "gtk3")
hl.env("QT_WAYLAND_DISABLE_WINDOWDECORATION", "1")
hl.env("QT_AUTO_SCREEN_SCALE_FACTOR", "1")
hl.env("GDK_BACKEND", "wayland,x11")
hl.env("SDL_VIDEODRIVER", "wayland")
hl.env("CLUTTER_BACKEND", "wayland")
hl.env("MOZ_ENABLE_WAYLAND", "1")

-- Electron apps (Slack, Obsidian, Claude, Codex, BlueBubbles, Podman Desktop)
-- pick Wayland from this rather than needing per-app flags.
hl.env("ELECTRON_OZONE_PLATFORM_HINT", "auto")

-- ── Desktop identity ────────────────────────────────────────────────────────
-- Portals and some GNOME apps key off these. uwsm sets them itself, so these
-- are only load-bearing for the plain (non-uwsm) session.
hl.env("XDG_CURRENT_DESKTOP", "Hyprland")
hl.env("XDG_SESSION_TYPE", "wayland")
hl.env("XDG_SESSION_DESKTOP", "Hyprland")

return true
