Path: blob/main/src/vs/base/node/osDisplayProtocolInfo.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { constants as FSConstants, promises as FSPromises } from 'fs';6import { join } from '../common/path.js';7import { env } from '../common/process.js';89const XDG_SESSION_TYPE = 'XDG_SESSION_TYPE';10const WAYLAND_DISPLAY = 'WAYLAND_DISPLAY';11const XDG_RUNTIME_DIR = 'XDG_RUNTIME_DIR';1213const enum DisplayProtocolType {14Wayland = 'wayland',15XWayland = 'xwayland',16X11 = 'x11',17Unknown = 'unknown'18}1920export async function getDisplayProtocol(errorLogger: (error: any) => void): Promise<DisplayProtocolType> {21const xdgSessionType = env[XDG_SESSION_TYPE];2223if (xdgSessionType) {24// If XDG_SESSION_TYPE is set, return its value if it's either 'wayland' or 'x11'.25// We assume that any value other than 'wayland' or 'x11' is an error or unexpected,26// hence 'unknown' is returned.27return xdgSessionType === DisplayProtocolType.Wayland || xdgSessionType === DisplayProtocolType.X11 ? xdgSessionType : DisplayProtocolType.Unknown;28} else {29const waylandDisplay = env[WAYLAND_DISPLAY];3031if (!waylandDisplay) {32// If WAYLAND_DISPLAY is empty, then the session is x11.33return DisplayProtocolType.X11;34} else {35const xdgRuntimeDir = env[XDG_RUNTIME_DIR];3637if (!xdgRuntimeDir) {38// If XDG_RUNTIME_DIR is empty, then the session can only be guessed.39return DisplayProtocolType.Unknown;40} else {41// Check for the presence of the file $XDG_RUNTIME_DIR/wayland-0.42const waylandServerPipe = join(xdgRuntimeDir, 'wayland-0');4344try {45await FSPromises.access(waylandServerPipe, FSConstants.R_OK);4647// If the file exists, then the session is wayland.48return DisplayProtocolType.Wayland;49} catch (err) {50// If the file does not exist or an error occurs, we guess 'unknown'51// since WAYLAND_DISPLAY was set but no wayland-0 pipe could be confirmed.52errorLogger(err);53return DisplayProtocolType.Unknown;54}55}56}57}58}596061export function getCodeDisplayProtocol(displayProtocol: DisplayProtocolType, ozonePlatform: string | undefined): DisplayProtocolType {62if (!ozonePlatform) {63return displayProtocol === DisplayProtocolType.Wayland ? DisplayProtocolType.XWayland : DisplayProtocolType.X11;64} else {65switch (ozonePlatform) {66case 'auto':67return displayProtocol;68case 'x11':69return displayProtocol === DisplayProtocolType.Wayland ? DisplayProtocolType.XWayland : DisplayProtocolType.X11;70case 'wayland':71return DisplayProtocolType.Wayland;72default:73return DisplayProtocolType.Unknown;74}75}76}777879