@clipy/cli 0.18.0 → 0.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE-APACHE +202 -0
- package/README.md +210 -9
- package/dist/context/importCmd.js +1 -1
- package/dist/context/ytdlp.js +1 -1
- package/dist/context-core/arec.js +1 -1
- package/dist/index.js +1042 -88
- package/dist/local/arec.js +186 -0
- package/dist/local/commands.js +1103 -0
- package/dist/local/deps.js +42 -0
- package/dist/local/media.js +154 -0
- package/dist/local/pipeline.js +530 -0
- package/dist/local/ports.js +1 -0
- package/dist/local/speechEngine.js +260 -0
- package/dist/local/transcriptFile.js +54 -0
- package/dist/local/vendor/capture/capture.js +442 -0
- package/dist/local/vendor/capture/check.js +103 -0
- package/dist/local/vendor/capture/children.js +69 -0
- package/dist/local/vendor/capture/ffmpegArgs.js +108 -0
- package/dist/local/vendor/capture/index.js +36 -0
- package/dist/local/vendor/capture/proc.js +71 -0
- package/dist/local/vendor/capture/scripts.js +107 -0
- package/dist/local/vendor/capture/sidecar.js +199 -0
- package/dist/local/vendor/capture/sources.js +272 -0
- package/dist/local/vendor/capture/types.js +1 -0
- package/dist/local/vendor/capture/virtualDisplay.js +170 -0
- package/dist/local/vendor/capture/xauth.js +68 -0
- package/dist/local/vendor/config/errors.js +12 -0
- package/dist/local/vendor/config/index.js +241 -0
- package/dist/local/vendor/config/paths.js +37 -0
- package/dist/local/vendor/config/presets.js +61 -0
- package/dist/local/vendor/config/schema.js +66 -0
- package/dist/local/vendor/moments/generated/drawStrokes.js +127 -0
- package/dist/local/vendor/moments/generated/keyMomentsCursorContext.js +568 -0
- package/dist/local/vendor/moments/generated/keyMomentsFusion.js +406 -0
- package/dist/local/vendor/moments/generated/localMomentPlan.js +178 -0
- package/dist/local/vendor/moments/generated/types.js +1 -0
- package/dist/local/vendor/moments/index.js +147 -0
- package/dist/local/vendor/moments/llm.js +158 -0
- package/dist/local/vendor/transcribe/audio.js +251 -0
- package/dist/local/vendor/transcribe/chunking.js +250 -0
- package/dist/local/vendor/transcribe/index.js +89 -0
- package/dist/local/vendor/transcribe/local.js +221 -0
- package/dist/local/vendor/transcribe/localWorker.js +79 -0
- package/dist/local/vendor/transcribe/models.js +369 -0
- package/dist/local/vendor/transcribe/providers/assemblyai.js +61 -0
- package/dist/local/vendor/transcribe/providers/common.js +105 -0
- package/dist/local/vendor/transcribe/providers/deepgram.js +59 -0
- package/dist/local/vendor/transcribe/providers/elevenlabs.js +43 -0
- package/dist/local/vendor/transcribe/providers/gemini.js +102 -0
- package/dist/local/vendor/transcribe/providers/http.js +214 -0
- package/dist/local/vendor/transcribe/providers/openai.js +48 -0
- package/dist/local/vendor/transcribe/tar.js +53 -0
- package/dist/local/vendor/transcribe/types.js +1 -0
- package/dist/macBridge.js +78 -9
- package/dist/skill.js +46 -1
- package/package.json +19 -9
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/** H.264 and yuv420p both need even dimensions. */
|
|
2
|
+
export function even(n) {
|
|
3
|
+
return Math.max(2, Math.floor(n / 2) * 2);
|
|
4
|
+
}
|
|
5
|
+
/** Pick the H.264 encoder from `ffmpeg -encoders` output. */
|
|
6
|
+
export function pickH264Encoder(encodersText, platform) {
|
|
7
|
+
if (/^\s*V\S*\s+libx264\s/m.test(encodersText))
|
|
8
|
+
return "libx264";
|
|
9
|
+
if (platform === "darwin" && /^\s*V\S*\s+h264_videotoolbox\s/m.test(encodersText))
|
|
10
|
+
return "h264_videotoolbox";
|
|
11
|
+
if (platform === "win32" && /^\s*V\S*\s+h264_mf\s/m.test(encodersText))
|
|
12
|
+
return "h264_mf";
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
function videoInputArgs(v, fps) {
|
|
16
|
+
switch (v.backend) {
|
|
17
|
+
case "x11grab": {
|
|
18
|
+
const args = ["-thread_queue_size", "1024", "-f", "x11grab", "-draw_mouse", "1", "-framerate", String(fps)];
|
|
19
|
+
if (v.windowId !== undefined) {
|
|
20
|
+
args.push("-window_id", `0x${v.windowId.toString(16)}`);
|
|
21
|
+
}
|
|
22
|
+
else if (v.rect) {
|
|
23
|
+
args.push("-video_size", `${even(v.rect.width)}x${even(v.rect.height)}`, "-grab_x", String(Math.max(0, v.rect.x)), "-grab_y", String(Math.max(0, v.rect.y)));
|
|
24
|
+
}
|
|
25
|
+
args.push("-i", v.xDisplay);
|
|
26
|
+
return args;
|
|
27
|
+
}
|
|
28
|
+
case "avfoundation":
|
|
29
|
+
// The audio half of the device string is filled in by buildFfmpegArgs.
|
|
30
|
+
return ["-thread_queue_size", "1024", "-f", "avfoundation", "-capture_cursor", "1", "-capture_mouse_clicks", "1", "-framerate", String(fps)];
|
|
31
|
+
case "ddagrab":
|
|
32
|
+
return ["-thread_queue_size", "1024", "-f", "lavfi", "-i", `ddagrab=output_idx=${v.outputIndex}:framerate=${fps}:draw_mouse=1`];
|
|
33
|
+
case "gdigrab": {
|
|
34
|
+
const args = ["-thread_queue_size", "1024", "-f", "gdigrab", "-draw_mouse", "1", "-framerate", String(fps)];
|
|
35
|
+
if (v.rect) {
|
|
36
|
+
args.push("-offset_x", String(v.rect.x), "-offset_y", String(v.rect.y), "-video_size", `${even(v.rect.width)}x${even(v.rect.height)}`);
|
|
37
|
+
}
|
|
38
|
+
args.push("-i", "desktop");
|
|
39
|
+
return args;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function audioInputArgs(a) {
|
|
44
|
+
switch (a.backend) {
|
|
45
|
+
case "pulse":
|
|
46
|
+
return ["-thread_queue_size", "1024", "-f", "pulse", "-i", a.device];
|
|
47
|
+
case "alsa":
|
|
48
|
+
return ["-thread_queue_size", "1024", "-f", "alsa", "-i", a.device];
|
|
49
|
+
case "dshow":
|
|
50
|
+
return ["-thread_queue_size", "1024", "-f", "dshow", "-i", `audio=${a.device}`];
|
|
51
|
+
case "avfoundation":
|
|
52
|
+
return [];
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function encoderArgs(encoder, fps) {
|
|
56
|
+
// A keyframe every two seconds: each MP4 fragment starts on one, so a crash
|
|
57
|
+
// loses at most the last couple of seconds.
|
|
58
|
+
const gop = ["-g", String(Math.max(1, Math.round(fps * 2)))];
|
|
59
|
+
switch (encoder) {
|
|
60
|
+
case "libx264":
|
|
61
|
+
return ["-c:v", "libx264", "-preset", "veryfast", "-crf", "23", "-pix_fmt", "yuv420p", ...gop];
|
|
62
|
+
case "h264_videotoolbox":
|
|
63
|
+
return ["-c:v", "h264_videotoolbox", "-realtime", "1", "-b:v", "8M", "-pix_fmt", "yuv420p", ...gop];
|
|
64
|
+
case "h264_mf":
|
|
65
|
+
return ["-c:v", "h264_mf", "-b:v", "8M", "-pix_fmt", "yuv420p", ...gop];
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/** Frames to yuv420p at even sizes; ddagrab frames live on the GPU first. */
|
|
69
|
+
export function videoFilter(backend) {
|
|
70
|
+
const common = "scale=trunc(iw/2)*2:trunc(ih/2)*2,format=yuv420p";
|
|
71
|
+
return backend === "ddagrab" ? `hwdownload,format=bgra,${common}` : common;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* The full ffmpeg command line for one capture. Output is a fragmented MP4
|
|
75
|
+
* (moov first, a fragment per keyframe): playable even if ffmpeg is killed.
|
|
76
|
+
*/
|
|
77
|
+
export function buildFfmpegArgs(plan) {
|
|
78
|
+
const args = ["-hide_banner", "-loglevel", "info", "-nostats", "-y"];
|
|
79
|
+
const video = videoInputArgs(plan.video, plan.fps);
|
|
80
|
+
args.push(...video);
|
|
81
|
+
let audioMap = null;
|
|
82
|
+
if (plan.video.backend === "avfoundation") {
|
|
83
|
+
const audio = plan.audio?.backend === "avfoundation" ? plan.audio.device : "none";
|
|
84
|
+
args.push("-i", `${plan.video.device}:${audio}`);
|
|
85
|
+
if (plan.audio?.backend === "avfoundation")
|
|
86
|
+
audioMap = "0:a:0";
|
|
87
|
+
}
|
|
88
|
+
else if (plan.audio) {
|
|
89
|
+
if (plan.audio.backend === "avfoundation")
|
|
90
|
+
throw new Error("avfoundation audio needs the avfoundation video backend");
|
|
91
|
+
args.push(...audioInputArgs(plan.audio));
|
|
92
|
+
audioMap = "1:a:0";
|
|
93
|
+
}
|
|
94
|
+
args.push("-map", "0:v:0");
|
|
95
|
+
if (audioMap)
|
|
96
|
+
args.push("-map", audioMap);
|
|
97
|
+
args.push("-vf", videoFilter(plan.video.backend));
|
|
98
|
+
args.push(...encoderArgs(plan.encoder, plan.fps));
|
|
99
|
+
if (audioMap)
|
|
100
|
+
args.push("-c:a", "aac", "-b:a", "128k", "-ac", "2");
|
|
101
|
+
if (plan.maxSeconds !== undefined && plan.maxSeconds > 0)
|
|
102
|
+
args.push("-t", String(plan.maxSeconds));
|
|
103
|
+
// A still screen compresses to almost nothing, so without flushing the
|
|
104
|
+
// header and fragments can sit in ffmpeg's output buffer for many seconds
|
|
105
|
+
// and a crash would leave a file with no moov.
|
|
106
|
+
args.push("-flush_packets", "1", "-movflags", "+frag_keyframe+empty_moov+default_base_moof", "-f", "mp4", plan.output);
|
|
107
|
+
return args;
|
|
108
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// GENERATED from oss/packages/capture/src/index.ts by cli/scripts/sync-local.mjs. Do not edit here.
|
|
3
|
+
/**
|
|
4
|
+
* @clipy/capture: record the screen with ffmpeg on macOS, Windows and Linux,
|
|
5
|
+
* with a pointer and click sidecar where the OS allows it.
|
|
6
|
+
*
|
|
7
|
+
* - Linux: ffmpeg x11grab (a display, or one window by id), microphone via
|
|
8
|
+
* PulseAudio or ALSA, pointer via python3 + libX11. startVirtualDisplay
|
|
9
|
+
* gives an agent a private Xvfb display to record.
|
|
10
|
+
* - macOS: ffmpeg avfoundation (displays), microphone as the avfoundation
|
|
11
|
+
* audio device, pointer via osascript JXA. Needs Screen Recording
|
|
12
|
+
* permission for the terminal.
|
|
13
|
+
* - Windows: ffmpeg ddagrab (Desktop Duplication), falling back to gdigrab,
|
|
14
|
+
* microphone via dshow, pointer via PowerShell.
|
|
15
|
+
*
|
|
16
|
+
* Output is H.264 yuv420p in a fragmented MP4, so a crash still leaves a
|
|
17
|
+
* playable file.
|
|
18
|
+
*/
|
|
19
|
+
import { checkCapture as check } from "./check.js";
|
|
20
|
+
import { listSources as list } from "./sources.js";
|
|
21
|
+
export { startCapture } from "./capture.js";
|
|
22
|
+
export { startVirtualDisplay } from "./virtualDisplay.js";
|
|
23
|
+
// Child processes (ffmpeg, the pointer helper, Xvfb) are signalled when the
|
|
24
|
+
// process exits. An application with its own SIGINT/SIGTERM handler can call
|
|
25
|
+
// this before exiting; calling process.exit does it too.
|
|
26
|
+
export { stopAllChildren } from "./children.js";
|
|
27
|
+
export function checkCapture(platform) {
|
|
28
|
+
return check(platform);
|
|
29
|
+
}
|
|
30
|
+
/** Linux: pass `{ xDisplay }` to list a display other than $DISPLAY. */
|
|
31
|
+
export function listSources(platform, opts) {
|
|
32
|
+
return list(platform, opts);
|
|
33
|
+
}
|
|
34
|
+
// Lower-level pieces, for tools that build their own command lines.
|
|
35
|
+
export { buildFfmpegArgs, pickH264Encoder, videoFilter } from "./ffmpegArgs.js";
|
|
36
|
+
export { samplesToEvents, parseSampleLine } from "./sidecar.js";
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// GENERATED from oss/packages/capture/src/proc.ts by cli/scripts/sync-local.mjs. Do not edit here.
|
|
3
|
+
import { execFile } from "node:child_process";
|
|
4
|
+
import { accessSync, constants, statSync } from "node:fs";
|
|
5
|
+
import { delimiter, join } from "node:path";
|
|
6
|
+
export function currentPlatform() {
|
|
7
|
+
const p = process.platform;
|
|
8
|
+
if (p === "darwin" || p === "win32" || p === "linux")
|
|
9
|
+
return p;
|
|
10
|
+
throw new Error(`@clipy/capture supports macOS, Windows and Linux, not ${p}`);
|
|
11
|
+
}
|
|
12
|
+
export const realRunner = (cmd, args, env = process.env) => new Promise((resolve) => {
|
|
13
|
+
execFile(cmd, args, { env, encoding: "utf8", timeout: 15_000, maxBuffer: 8 * 1024 * 1024, windowsHide: true }, (error, stdout, stderr) => {
|
|
14
|
+
const code = error ? error.code : 0;
|
|
15
|
+
// A non-zero exit still has output worth parsing (ffmpeg -list_devices
|
|
16
|
+
// always exits 1); only a spawn failure means the command is missing.
|
|
17
|
+
const status = typeof code === "number" ? code : error ? (typeof code === "string" ? null : 1) : 0;
|
|
18
|
+
resolve({ status, stdout: String(stdout ?? ""), stderr: String(stderr ?? "") });
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
/** Look a program up on PATH (and PATHEXT on Windows) without spawning it. */
|
|
22
|
+
export function findExecutable(name, platform = currentPlatform(), env = process.env) {
|
|
23
|
+
if (name.includes("/") || (platform === "win32" && name.includes("\\")))
|
|
24
|
+
return isExecutable(name, platform) ? name : null;
|
|
25
|
+
const path = env.PATH ?? env.Path ?? "";
|
|
26
|
+
const sep = platform === "win32" ? ";" : delimiter;
|
|
27
|
+
const exts = platform === "win32" ? (env.PATHEXT ?? ".EXE;.CMD;.BAT;.COM").split(";").filter(Boolean) : [""];
|
|
28
|
+
for (const dir of path.split(sep)) {
|
|
29
|
+
if (!dir)
|
|
30
|
+
continue;
|
|
31
|
+
for (const ext of exts) {
|
|
32
|
+
const candidate = join(dir, name + (platform === "win32" && !name.toLowerCase().endsWith(ext.toLowerCase()) ? ext : ""));
|
|
33
|
+
if (isExecutable(candidate, platform))
|
|
34
|
+
return candidate;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
function isExecutable(path, platform) {
|
|
40
|
+
try {
|
|
41
|
+
if (!statSync(path).isFile())
|
|
42
|
+
return false;
|
|
43
|
+
if (platform !== "win32")
|
|
44
|
+
accessSync(path, constants.X_OK);
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/** ffmpeg, honouring CLIPY_FFMPEG. */
|
|
52
|
+
export function ffmpegPath(platform = currentPlatform(), env = process.env) {
|
|
53
|
+
if (env.CLIPY_FFMPEG)
|
|
54
|
+
return findExecutable(env.CLIPY_FFMPEG, platform, env);
|
|
55
|
+
return findExecutable("ffmpeg", platform, env);
|
|
56
|
+
}
|
|
57
|
+
/** ffprobe, honouring CLIPY_FFPROBE, else next to ffmpeg, else on PATH. */
|
|
58
|
+
export function ffprobePath(platform = currentPlatform(), env = process.env) {
|
|
59
|
+
if (env.CLIPY_FFPROBE)
|
|
60
|
+
return findExecutable(env.CLIPY_FFPROBE, platform, env);
|
|
61
|
+
const ffmpeg = ffmpegPath(platform, env);
|
|
62
|
+
if (ffmpeg) {
|
|
63
|
+
const sibling = ffmpeg.replace(/ffmpeg(\.exe)?$/i, (_m, ext) => `ffprobe${ext ?? ""}`);
|
|
64
|
+
if (sibling !== ffmpeg && findExecutable(sibling, platform, env))
|
|
65
|
+
return sibling;
|
|
66
|
+
}
|
|
67
|
+
return findExecutable("ffprobe", platform, env);
|
|
68
|
+
}
|
|
69
|
+
export function sleep(ms) {
|
|
70
|
+
return new Promise((r) => setTimeout(r, ms));
|
|
71
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// GENERATED from oss/packages/capture/src/scripts.ts by cli/scripts/sync-local.mjs. Do not edit here.
|
|
3
|
+
/**
|
|
4
|
+
* The pointer helpers, one per OS, each using only what ships with it.
|
|
5
|
+
* See sidecar.ts for the line protocol they print.
|
|
6
|
+
*/
|
|
7
|
+
/** python3 -c <this> <interval seconds>. Needs only libX11 (always present with X). */
|
|
8
|
+
export const LINUX_POINTER_SCRIPT = String.raw `
|
|
9
|
+
import ctypes, ctypes.util, os, sys, time
|
|
10
|
+
lib = ctypes.util.find_library("X11") or "libX11.so.6"
|
|
11
|
+
try:
|
|
12
|
+
x = ctypes.cdll.LoadLibrary(lib)
|
|
13
|
+
except OSError:
|
|
14
|
+
print("E libX11 not found", flush=True); sys.exit(2)
|
|
15
|
+
x.XOpenDisplay.restype = ctypes.c_void_p
|
|
16
|
+
x.XOpenDisplay.argtypes = [ctypes.c_char_p]
|
|
17
|
+
x.XDefaultRootWindow.restype = ctypes.c_ulong
|
|
18
|
+
x.XDefaultRootWindow.argtypes = [ctypes.c_void_p]
|
|
19
|
+
x.XDefaultScreen.argtypes = [ctypes.c_void_p]
|
|
20
|
+
x.XDisplayWidth.argtypes = [ctypes.c_void_p, ctypes.c_int]
|
|
21
|
+
x.XDisplayHeight.argtypes = [ctypes.c_void_p, ctypes.c_int]
|
|
22
|
+
P = ctypes.POINTER
|
|
23
|
+
x.XQueryPointer.argtypes = [ctypes.c_void_p, ctypes.c_ulong, P(ctypes.c_ulong), P(ctypes.c_ulong),
|
|
24
|
+
P(ctypes.c_int), P(ctypes.c_int), P(ctypes.c_int), P(ctypes.c_int), P(ctypes.c_uint)]
|
|
25
|
+
d = x.XOpenDisplay(None)
|
|
26
|
+
if not d:
|
|
27
|
+
print("E cannot open X display " + os.environ.get("DISPLAY", "(unset)"), flush=True); sys.exit(2)
|
|
28
|
+
root = x.XDefaultRootWindow(d)
|
|
29
|
+
scr = x.XDefaultScreen(d)
|
|
30
|
+
print('S [{"name":"screen","x":0,"y":0,"width":%d,"height":%d,"primary":true}]' % (x.XDisplayWidth(d, scr), x.XDisplayHeight(d, scr)), flush=True)
|
|
31
|
+
iv = float(sys.argv[1]) if len(sys.argv) > 1 else 0.05
|
|
32
|
+
parent = os.getppid()
|
|
33
|
+
rr = ctypes.c_ulong(); cr = ctypes.c_ulong()
|
|
34
|
+
rx = ctypes.c_int(); ry = ctypes.c_int(); wx = ctypes.c_int(); wy = ctypes.c_int(); m = ctypes.c_uint()
|
|
35
|
+
while os.getppid() == parent:
|
|
36
|
+
x.XQueryPointer(d, root, ctypes.byref(rr), ctypes.byref(cr), ctypes.byref(rx), ctypes.byref(ry),
|
|
37
|
+
ctypes.byref(wx), ctypes.byref(wy), ctypes.byref(m))
|
|
38
|
+
b = (1 if m.value & 256 else 0) | (2 if m.value & 1024 else 0) | (4 if m.value & 512 else 0)
|
|
39
|
+
sys.stdout.write("P %d %d %d %d\n" % (int(time.time() * 1000), rx.value, ry.value, b))
|
|
40
|
+
sys.stdout.flush()
|
|
41
|
+
time.sleep(iv)
|
|
42
|
+
`;
|
|
43
|
+
/** osascript -l JavaScript -e <this> <interval seconds>. */
|
|
44
|
+
export const MAC_POINTER_SCRIPT = String.raw `
|
|
45
|
+
ObjC.import("AppKit");
|
|
46
|
+
function out(s) {
|
|
47
|
+
$.NSFileHandle.fileHandleWithStandardOutput.writeData(
|
|
48
|
+
$.NSString.alloc.initWithUTF8String(s + "\n").dataUsingEncoding($.NSUTF8StringEncoding));
|
|
49
|
+
}
|
|
50
|
+
function run(argv) {
|
|
51
|
+
var iv = parseFloat(argv[0]) || 0.05;
|
|
52
|
+
var screens = $.NSScreen.screens;
|
|
53
|
+
var mainH = screens.objectAtIndex(0).frame.size.height;
|
|
54
|
+
var list = [];
|
|
55
|
+
for (var i = 0; i < screens.count; i++) {
|
|
56
|
+
var f = screens.objectAtIndex(i).frame;
|
|
57
|
+
list.push({ name: "Display " + (i + 1), x: f.origin.x, y: mainH - (f.origin.y + f.size.height),
|
|
58
|
+
width: f.size.width, height: f.size.height, primary: i === 0 });
|
|
59
|
+
}
|
|
60
|
+
out("S " + JSON.stringify(list));
|
|
61
|
+
while (true) {
|
|
62
|
+
var p = $.NSEvent.mouseLocation;
|
|
63
|
+
var b = $.NSEvent.pressedMouseButtons;
|
|
64
|
+
out("P " + Date.now() + " " + Math.round(p.x) + " " + Math.round(mainH - p.y) + " " + (b & 7));
|
|
65
|
+
delay(iv);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
`;
|
|
69
|
+
const WINDOWS_TYPES = String.raw `
|
|
70
|
+
$ErrorActionPreference = 'Stop'
|
|
71
|
+
Add-Type -Namespace ClipyCapture -Name User32 -MemberDefinition @'
|
|
72
|
+
[DllImport("user32.dll")] public static extern bool SetProcessDPIAware();
|
|
73
|
+
[DllImport("user32.dll")] public static extern bool GetCursorPos(out POINT p);
|
|
74
|
+
[DllImport("user32.dll")] public static extern short GetAsyncKeyState(int vKey);
|
|
75
|
+
public struct POINT { public int X; public int Y; }
|
|
76
|
+
'@
|
|
77
|
+
[void][ClipyCapture.User32]::SetProcessDPIAware()
|
|
78
|
+
Add-Type -AssemblyName System.Windows.Forms
|
|
79
|
+
$screens = @([System.Windows.Forms.Screen]::AllScreens | ForEach-Object {
|
|
80
|
+
[pscustomobject]@{ name = $_.DeviceName; x = $_.Bounds.X; y = $_.Bounds.Y; width = $_.Bounds.Width; height = $_.Bounds.Height; primary = $_.Primary }
|
|
81
|
+
})
|
|
82
|
+
[Console]::Out.WriteLine('S ' + (ConvertTo-Json -Compress -InputObject $screens))
|
|
83
|
+
[Console]::Out.Flush()
|
|
84
|
+
`;
|
|
85
|
+
export function windowsScreensScript() {
|
|
86
|
+
return WINDOWS_TYPES;
|
|
87
|
+
}
|
|
88
|
+
export function windowsPointerScript(intervalMs) {
|
|
89
|
+
return (WINDOWS_TYPES +
|
|
90
|
+
String.raw `
|
|
91
|
+
$p = New-Object ClipyCapture.User32+POINT
|
|
92
|
+
while ($true) {
|
|
93
|
+
[void][ClipyCapture.User32]::GetCursorPos([ref]$p)
|
|
94
|
+
$b = 0
|
|
95
|
+
if ([ClipyCapture.User32]::GetAsyncKeyState(1) -band 0x8000) { $b += 1 }
|
|
96
|
+
if ([ClipyCapture.User32]::GetAsyncKeyState(2) -band 0x8000) { $b += 2 }
|
|
97
|
+
if ([ClipyCapture.User32]::GetAsyncKeyState(4) -band 0x8000) { $b += 4 }
|
|
98
|
+
[Console]::Out.WriteLine("P $([DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds()) $($p.X) $($p.Y) $b")
|
|
99
|
+
[Console]::Out.Flush()
|
|
100
|
+
Start-Sleep -Milliseconds ${Math.max(10, Math.round(intervalMs))}
|
|
101
|
+
}
|
|
102
|
+
`);
|
|
103
|
+
}
|
|
104
|
+
/** PowerShell -EncodedCommand takes base64 of UTF-16LE. */
|
|
105
|
+
export function encodePowerShell(script) {
|
|
106
|
+
return Buffer.from(script, "utf16le").toString("base64");
|
|
107
|
+
}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// GENERATED from oss/packages/capture/src/sidecar.ts by cli/scripts/sync-local.mjs. Do not edit here.
|
|
3
|
+
/**
|
|
4
|
+
* The pointer sidecar: where the pointer was and when a button went down or
|
|
5
|
+
* up while recording, so the moment rules have something to anchor on.
|
|
6
|
+
*
|
|
7
|
+
* Each OS gets a tiny helper that uses only what ships with it, polls the
|
|
8
|
+
* pointer every ~50 ms and prints one line per sample:
|
|
9
|
+
*
|
|
10
|
+
* P <epoch ms> <x> <y> <buttons> buttons: 1 left, 2 right, 4 middle
|
|
11
|
+
* S <json> the screens, once, at start
|
|
12
|
+
* E <message> the helper cannot work here
|
|
13
|
+
*
|
|
14
|
+
* - Linux: python3 with ctypes over libX11 (XQueryPointer). No Xlib module,
|
|
15
|
+
* xdotool or xinput needed. Coordinates are X screen pixels.
|
|
16
|
+
* - macOS: osascript JXA reading NSEvent.mouseLocation and
|
|
17
|
+
* pressedMouseButtons. Coordinates are global points, top-left origin.
|
|
18
|
+
* - Windows: PowerShell calling GetCursorPos and GetAsyncKeyState from a
|
|
19
|
+
* DPI-aware process. Coordinates are virtual-desktop pixels.
|
|
20
|
+
*
|
|
21
|
+
* Polling sees a button state, not the button event itself, so a click
|
|
22
|
+
* shorter than one interval can be missed. Scroll and keys are not sampled.
|
|
23
|
+
*/
|
|
24
|
+
import { spawn } from "node:child_process";
|
|
25
|
+
import { trackChild } from "./children.js";
|
|
26
|
+
import { xEnv } from "./xauth.js";
|
|
27
|
+
import { createInterface } from "node:readline";
|
|
28
|
+
import { findExecutable } from "./proc.js";
|
|
29
|
+
import { encodePowerShell, LINUX_POINTER_SCRIPT, MAC_POINTER_SCRIPT, windowsPointerScript } from "./scripts.js";
|
|
30
|
+
import { parseScreensLine } from "./sources.js";
|
|
31
|
+
export const SAMPLE_INTERVAL_MS = 50;
|
|
32
|
+
// ---------------------------------------------------------------------------
|
|
33
|
+
// Parsing and conversion (pure)
|
|
34
|
+
// ---------------------------------------------------------------------------
|
|
35
|
+
export function parseSampleLine(line) {
|
|
36
|
+
const m = /^P (\d+) (-?\d+(?:\.\d+)?) (-?\d+(?:\.\d+)?) (\d+)\s*$/.exec(line);
|
|
37
|
+
if (!m)
|
|
38
|
+
return null;
|
|
39
|
+
return { t: Number(m[1]), x: Math.round(Number(m[2])), y: Math.round(Number(m[3])), buttons: Number(m[4]) & 7 };
|
|
40
|
+
}
|
|
41
|
+
const BUTTONS = [
|
|
42
|
+
[1, 1],
|
|
43
|
+
[2, 2],
|
|
44
|
+
[4, 3],
|
|
45
|
+
];
|
|
46
|
+
/**
|
|
47
|
+
* Turn polled samples into interaction events on the recording clock.
|
|
48
|
+
*
|
|
49
|
+
* - Times are ms since `startedAt`; samples after `endedAt` are dropped.
|
|
50
|
+
* - A move is emitted only when the position changes. The position held when
|
|
51
|
+
* recording starts is emitted once at time 0.
|
|
52
|
+
* - A button that goes down during the recording emits `down`, and `up` when
|
|
53
|
+
* it is released. A button already held at start emits nothing until it is
|
|
54
|
+
* pressed again, so every `up` has its `down`.
|
|
55
|
+
*/
|
|
56
|
+
export function samplesToEvents(samples, startedAt, endedAt) {
|
|
57
|
+
const sorted = [...samples].sort((a, b) => a.t - b.t);
|
|
58
|
+
const events = [];
|
|
59
|
+
let last = null;
|
|
60
|
+
let buttons = null;
|
|
61
|
+
const down = new Set();
|
|
62
|
+
let preStart = null;
|
|
63
|
+
for (const s of sorted) {
|
|
64
|
+
if (s.t < startedAt) {
|
|
65
|
+
preStart = s;
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
if (endedAt !== undefined && s.t > endedAt)
|
|
69
|
+
break;
|
|
70
|
+
if (buttons === null) {
|
|
71
|
+
if (preStart) {
|
|
72
|
+
last = { x: preStart.x, y: preStart.y };
|
|
73
|
+
events.push({ kind: "move", time_ms: 0, x: preStart.x, y: preStart.y });
|
|
74
|
+
buttons = preStart.buttons;
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
buttons = s.buttons;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
const time = Math.round(s.t - startedAt);
|
|
81
|
+
if (!last || last.x !== s.x || last.y !== s.y) {
|
|
82
|
+
events.push({ kind: "move", time_ms: time, x: s.x, y: s.y });
|
|
83
|
+
last = { x: s.x, y: s.y };
|
|
84
|
+
}
|
|
85
|
+
for (const [bit, button] of BUTTONS) {
|
|
86
|
+
const was = (buttons & bit) !== 0;
|
|
87
|
+
const is = (s.buttons & bit) !== 0;
|
|
88
|
+
if (!was && is) {
|
|
89
|
+
events.push({ kind: "down", time_ms: time, x: s.x, y: s.y, button });
|
|
90
|
+
down.add(button);
|
|
91
|
+
}
|
|
92
|
+
else if (was && !is && down.has(button)) {
|
|
93
|
+
events.push({ kind: "up", time_ms: time, x: s.x, y: s.y, button });
|
|
94
|
+
down.delete(button);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
buttons = s.buttons;
|
|
98
|
+
}
|
|
99
|
+
return events;
|
|
100
|
+
}
|
|
101
|
+
function helperCommand(platform, intervalMs, env) {
|
|
102
|
+
const seconds = (intervalMs / 1000).toFixed(3);
|
|
103
|
+
if (platform === "linux") {
|
|
104
|
+
const python = findExecutable("python3", platform, env);
|
|
105
|
+
if (!python)
|
|
106
|
+
return { missing: "pointer positions need python3 on Linux; recording without them" };
|
|
107
|
+
return { cmd: python, args: ["-c", LINUX_POINTER_SCRIPT, seconds] };
|
|
108
|
+
}
|
|
109
|
+
if (platform === "darwin") {
|
|
110
|
+
const osascript = findExecutable("osascript", platform, env) ?? "/usr/bin/osascript";
|
|
111
|
+
return { cmd: osascript, args: ["-l", "JavaScript", "-e", MAC_POINTER_SCRIPT, seconds] };
|
|
112
|
+
}
|
|
113
|
+
const powershell = findExecutable("powershell.exe", platform, env) ?? findExecutable("pwsh.exe", platform, env);
|
|
114
|
+
if (!powershell)
|
|
115
|
+
return { missing: "pointer positions need PowerShell on Windows; recording without them" };
|
|
116
|
+
return {
|
|
117
|
+
cmd: powershell,
|
|
118
|
+
args: ["-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-EncodedCommand", encodePowerShell(windowsPointerScript(intervalMs))],
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
export function startPointerSampler(platform, opts = {}) {
|
|
122
|
+
const base = { ...(opts.env ?? process.env) };
|
|
123
|
+
const env = platform === "linux" ? xEnv(base, opts.xDisplay) : base;
|
|
124
|
+
const intervalMs = opts.intervalMs ?? SAMPLE_INTERVAL_MS;
|
|
125
|
+
const samples = [];
|
|
126
|
+
const warnings = [];
|
|
127
|
+
let screens = [];
|
|
128
|
+
const command = helperCommand(platform, intervalMs, env);
|
|
129
|
+
if ("missing" in command) {
|
|
130
|
+
warnings.push(command.missing);
|
|
131
|
+
return { screens: () => screens, ready: Promise.resolve(), stop: async () => ({ samples, warnings }) };
|
|
132
|
+
}
|
|
133
|
+
let child = null;
|
|
134
|
+
let errorLine = null;
|
|
135
|
+
let stderr = "";
|
|
136
|
+
let markReady = () => { };
|
|
137
|
+
const ready = new Promise((r) => {
|
|
138
|
+
markReady = r;
|
|
139
|
+
});
|
|
140
|
+
const readyTimer = setTimeout(() => markReady(), 3000);
|
|
141
|
+
const exited = new Promise((resolve) => {
|
|
142
|
+
try {
|
|
143
|
+
child = trackChild(spawn(command.cmd, command.args, { env, stdio: ["ignore", "pipe", "pipe"], windowsHide: true }));
|
|
144
|
+
}
|
|
145
|
+
catch (err) {
|
|
146
|
+
errorLine = err.message;
|
|
147
|
+
resolve();
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
const c = child;
|
|
151
|
+
c.on("error", (err) => {
|
|
152
|
+
errorLine ??= err.message;
|
|
153
|
+
resolve();
|
|
154
|
+
});
|
|
155
|
+
c.on("exit", () => resolve());
|
|
156
|
+
c.stderr?.on("data", (chunk) => {
|
|
157
|
+
if (stderr.length < 4096)
|
|
158
|
+
stderr += chunk.toString("utf8");
|
|
159
|
+
});
|
|
160
|
+
createInterface({ input: c.stdout }).on("line", (line) => {
|
|
161
|
+
markReady();
|
|
162
|
+
const sample = parseSampleLine(line);
|
|
163
|
+
if (sample)
|
|
164
|
+
samples.push(sample);
|
|
165
|
+
else if (line.startsWith("S "))
|
|
166
|
+
screens = parseScreensLine(line);
|
|
167
|
+
else if (line.startsWith("E "))
|
|
168
|
+
errorLine ??= line.slice(2).trim();
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
void exited.then(() => markReady());
|
|
172
|
+
let stopped = null;
|
|
173
|
+
const stop = async () => {
|
|
174
|
+
clearTimeout(readyTimer);
|
|
175
|
+
const c = child;
|
|
176
|
+
if (c && c.exitCode === null && c.signalCode === null) {
|
|
177
|
+
c.kill();
|
|
178
|
+
const killTimer = setTimeout(() => c.kill("SIGKILL"), 2000);
|
|
179
|
+
await exited;
|
|
180
|
+
clearTimeout(killTimer);
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
await exited;
|
|
184
|
+
}
|
|
185
|
+
if (samples.length === 0) {
|
|
186
|
+
const why = errorLine ?? stderr.trim().split("\n").pop() ?? "the helper printed nothing";
|
|
187
|
+
warnings.push(`no pointer positions were recorded (${why || "the helper printed nothing"})`);
|
|
188
|
+
}
|
|
189
|
+
return { samples, warnings };
|
|
190
|
+
};
|
|
191
|
+
return {
|
|
192
|
+
screens: () => screens,
|
|
193
|
+
ready,
|
|
194
|
+
stop() {
|
|
195
|
+
stopped ??= stop();
|
|
196
|
+
return stopped;
|
|
197
|
+
},
|
|
198
|
+
};
|
|
199
|
+
}
|