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