import fs from 'fs' import path from 'path' import { execFileSync, execSync } from 'child_process' import { parseDesktopEnv, type DesktopEnvironment } from '@shared/desktop' import { hostCommand } from './flatpak' /** stdout of a program on the host, stderr dropped; the words are the program and its arguments */ function hostOutput(commandLine: string, timeout: number): string { const [program, ...args] = commandLine.split(' ') const [file, fileArgs] = hostCommand(program, args) return execFileSync(file, fileArgs, { encoding: 'utf8', timeout, stdio: ['ignore', 'pipe', 'ignore'] }) } export type DisplayServer = 'wayland' | 'x11' | 'unknown' export type { DesktopEnvironment } export function detectDisplayServer(): DisplayServer { if (process.platform === 'win32' || process.platform === 'darwin') return 'unknown' if (process.env.WAYLAND_DISPLAY) return 'wayland' if (process.env.DISPLAY) return 'x11' return 'unknown' } export function detectDesktopEnv(): DesktopEnvironment { if (process.platform === 'win32' || process.platform === 'darwin') return 'other' return parseDesktopEnv(process.env.XDG_CURRENT_DESKTOP, process.env) } /** children of an AppImage inherit APPIMAGE and APPDIR too */ export function runningAppImage(): string | undefined { const { APPIMAGE, APPDIR } = process.env return APPIMAGE && APPDIR && process.execPath.startsWith(APPDIR) ? APPIMAGE : undefined } /** Electron often runs under XWayland without WAYLAND_DISPLAY, so the session type counts too. */ export function isWaylandSession(): boolean { return !!process.env.WAYLAND_DISPLAY || process.env.XDG_SESSION_TYPE === 'wayland' } /** * WAYLAND_DISPLAY of the user session. Electron often runs under XWayland * without it set, so fall back to the session type and loginctl. */ export function getWaylandDisplay(): string | undefined { if (process.env.WAYLAND_DISPLAY) return process.env.WAYLAND_DISPLAY if (process.env.XDG_SESSION_TYPE === 'wayland') return 'wayland-0' try { const sessionType = execSync( 'loginctl show-session $(loginctl 2>/dev/null | grep $USER | head -1 | awk \'{print $1}\') -p Type --value 2>/dev/null', { encoding: 'utf8', timeout: 3000 } ).trim() if (sessionType === 'wayland') return 'wayland-0' } catch { } return undefined } export function getXdgRuntimeDir(): string { return process.env.XDG_RUNTIME_DIR || `/run/user/${process.getuid?.() ?? 1000}` } function parseJson(output: string): unknown { try { return JSON.parse(output) } catch { return null } } function isNamed(v: unknown): v is { name: string; [key: string]: unknown } { return typeof v === 'object' && v !== null && typeof (v as { name?: unknown }).name === 'string' && (v as { name: string }).name !== '' } /** `hyprctl monitors -j`: an array of monitors, a disabled one has no output to draw on */ export function parseHyprctlMonitors(output: string): string[] { const monitors = parseJson(output) if (!Array.isArray(monitors)) return [] return monitors .filter(isNamed) .filter(m => m.disabled !== true) .map(m => m.name) } /** `swaymsg -t get_outputs -r`: an array of outputs, inactive ones are switched off */ export function parseSwayOutputs(output: string): string[] { const outputs = parseJson(output) if (!Array.isArray(outputs)) return [] return outputs .filter(isNamed) .filter(o => o.active !== false) .map(o => o.name) } /** `niri msg --json outputs`: an object keyed by output name, a disabled one has no current mode */ export function parseNiriOutputs(output: string): string[] { const outputs = parseJson(output) if (!outputs || typeof outputs !== 'object' || Array.isArray(outputs)) return [] return Object.entries(outputs) .filter(([, o]) => (o as { current_mode?: unknown } | null)?.current_mode !== null) .map(([name]) => name) } const COMPOSITOR_QUERIES: { marker: string; command: string; parse: (output: string) => string[] }[] = [ { marker: 'HYPRLAND_INSTANCE_SIGNATURE', command: 'hyprctl monitors -j', parse: parseHyprctlMonitors }, { marker: 'SWAYSOCK', command: 'swaymsg -t get_outputs -r', parse: parseSwayOutputs }, { marker: 'NIRI_SOCKET', command: 'niri msg --json outputs', parse: parseNiriOutputs } ] /** Outputs that show another output's picture; kscreen-doctor -j gives them a nonzero replicationSource (an output id) */ export function parseKscreenMirrors(json: string): string[] { try { const outputs = (JSON.parse(json) as { outputs?: { name?: string; replicationSource?: number }[] }).outputs ?? [] return outputs.filter((o) => o.name && o.replicationSource).map((o) => o.name as string) } catch { return [] } } export function getMirroredScreens(): string[] { if (process.platform !== 'linux' || !isWaylandSession() || detectDesktopEnv() !== 'kde') return [] try { return parseKscreenMirrors(hostOutput('kscreen-doctor -j', 5000)) } catch { return [] } } export function getConnectedScreens(): string[] { if (process.platform !== 'linux') return [] // On Wayland, we MUST use native Wayland output names (not xrandr/XWayland names) if (isWaylandSession()) { // the compositor's own IPC knows which outputs are enabled for (const { marker, command, parse } of COMPOSITOR_QUERIES) { if (!process.env[marker]) continue try { const output = hostOutput(command, 5000) const screens = parse(output) if (screens.length > 0) return screens } catch { } } try { const output = hostOutput('wlr-randr', 5000) const screens = output .split('\n') .filter(line => /^[A-Za-z]/.test(line) && !line.startsWith(' ')) .map(line => line.split(/\s+/)[0]) .filter(Boolean) if (screens.length > 0) return screens } catch { } try { const output = hostOutput('kscreen-doctor --outputs', 5000) const clean = output.replace(/\x1b\[[0-9;]*m/g, '') const screens = clean .split('\n') .map(line => { const m = line.match(/Output:\s+\d+\s+(\S+)/) return m ? m[1] : null }) .filter((s): s is string => s !== null) if (screens.length > 0) return screens } catch { } try { const output = hostOutput( 'gdbus call --session --dest org.gnome.Mutter.DisplayConfig --object-path /org/gnome/Mutter/DisplayConfig --method org.gnome.Mutter.DisplayConfig.GetCurrentState', 5000 ) // gdbus prints the monitors as ('connector', 'vendor', 'product', 'serial') tuples const screens = [...new Set([...output.matchAll(/\(\('([^']+)',\s*'[^']*',\s*'[^']*',\s*'[^']*'\)/g)].map(m => m[1]))] if (screens.length > 0) return screens } catch { } } try { const output = hostOutput('xrandr --query', 5000) const screens = output .split('\n') .filter(line => / connected/i.test(line)) .map(line => line.split(/\s+/)[0]) .filter(Boolean) if (screens.length > 0) return screens } catch { } return [] } /** `NAME connected [primary] WxH+X+Y` lines of xrandr, as the engine's XxYxWxH */ export function parseXrandrGeometries(output: string): Map { const geometries = new Map() for (const line of output.split('\n')) { const m = line.match(/^(\S+) connected (?:primary )?(\d+)x(\d+)\+(\d+)\+(\d+)/) if (m) geometries.set(m[1], `${m[4]}x${m[5]}x${m[2]}x${m[3]}`) } return geometries } /** Geometry of the named screen, else of the first one */ export function getScreenGeometry(screen?: string): string | null { try { const output = hostOutput('xrandr --query', 5000) const geometries = parseXrandrGeometries(output) return (screen && geometries.get(screen)) || geometries.values().next().value || null } catch { return null } } /** Answer of a NameHasOwner call, from busctl (`b true`) or gdbus (`(true,)`), null when it is neither */ export function parseNameHasOwner(output: string): boolean | null { const m = output.trim().match(/^(?:b\s+|\(\s*)(true|false)\b/) return m ? m[1] === 'true' : null } const NAME_HAS_OWNER_COMMANDS = [ 'busctl --user call org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus NameHasOwner s org.kde.StatusNotifierWatcher', 'gdbus call --session --dest org.freedesktop.DBus --object-path /org/freedesktop/DBus --method org.freedesktop.DBus.NameHasOwner org.kde.StatusNotifierWatcher' ] const TRAY_HOST_CACHE_MS = 15_000 let trayHostCache: { value: boolean; at: number } | null = null /** * Whether something on the session bus can show a StatusNotifier icon. Without one a window hidden * to the tray has no way back. Assumes yes when neither busctl nor gdbus can tell. */ export function hasStatusNotifierHost(maxAgeMs = TRAY_HOST_CACHE_MS): boolean { if (process.platform !== 'linux') return true if (trayHostCache && Date.now() - trayHostCache.at < maxAgeMs) return trayHostCache.value let value = true for (const command of NAME_HAS_OWNER_COMMANDS) { try { const output = hostOutput(command, 2000) const owned = parseNameHasOwner(output) if (owned !== null) { value = owned break } } catch { } } trayHostCache = { value, at: Date.now() } return value } const commandCache = new Map() export function isCommandAvailable(cmd: string): boolean { if (commandCache.has(cmd)) return commandCache.get(cmd)! let found = false if (process.platform === 'win32') { try { execSync(`where ${cmd}`, { stdio: 'ignore' }) found = true } catch { } } else { // a PATH search of our own: minimal systems (openSUSE) ship without which(1) const dirs = [...(process.env.PATH ?? '').split(':'), '/usr/local/sbin', '/usr/sbin', '/sbin'].filter(Boolean) found = dirs.some((dir) => { try { fs.accessSync(path.join(dir, cmd), fs.constants.X_OK) return true } catch { return false } }) } commandCache.set(cmd, found) return found } export function invalidateCommandCache(cmd: string): void { commandCache.delete(cmd) } export function whichCommand(cmd: string): string | undefined { try { const checker = process.platform === 'win32' ? 'where' : 'which' return execSync(`${checker} ${cmd}`, { encoding: 'utf8' }).trim() } catch { return undefined } }