Repository navigation
Expand file tree
/
Copy pathlinux-env.ts
More file actions
65 lines (55 loc) · 2.07 KB
/
Copy pathlinux-env.ts
File metadata and controls
65 lines (55 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { commandHostEnvironment } from '@agent-device/host-kit/command';
/**
* Shared Linux environment detection — display server and input tool.
*
* Results are cached after the first probe so that every action
* (press, type, scroll…) does not re-run `which` on every call.
*/
import { AppError } from '@agent-device/kernel/errors';
import { resolveLinuxToolProvider, type LinuxToolProvider } from './tool-provider.ts';
export type DisplayServer = 'wayland' | 'x11';
export type InputTool = 'xdotool' | 'ydotool';
function detectDisplayServer(): DisplayServer {
const environment = commandHostEnvironment().env;
if (environment['WAYLAND_DISPLAY']) return 'wayland';
if (environment['XDG_SESSION_TYPE'] === 'wayland') return 'wayland';
return 'x11';
}
// ── Cached input tool resolution ───────────────────────────────────────
let cachedInputTool: {
tool: InputTool;
display: DisplayServer;
provider: LinuxToolProvider;
} | null = null;
export async function ensureInputTool(): Promise<{
tool: InputTool;
display: DisplayServer;
}> {
const provider = resolveLinuxToolProvider();
if (cachedInputTool?.provider === provider) return cachedInputTool;
const display = detectDisplayServer();
if (display === 'wayland') {
if (await provider.whichCommand('ydotool')) {
cachedInputTool = { tool: 'ydotool', display, provider };
return cachedInputTool;
}
throw new AppError(
'TOOL_MISSING',
'ydotool is required for input synthesis on Wayland (xdotool does not work on Wayland). Install it via your package manager.',
);
}
if (await provider.whichCommand('xdotool')) {
cachedInputTool = { tool: 'xdotool', display, provider };
return cachedInputTool;
}
throw new AppError(
'TOOL_MISSING',
'xdotool is required for input synthesis on X11. Install it via your package manager.',
);
}
/**
* @internal Test isolation hook for the cached Linux input tool.
*/
export function resetInputToolCache(): void {
cachedInputTool = null;
}