-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathtelemetry.ts
More file actions
330 lines (286 loc) · 9.62 KB
/
Copy pathtelemetry.ts
File metadata and controls
330 lines (286 loc) · 9.62 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import fs from "node:fs/promises";
import {
CURSOR_SAMPLE_INTERVAL_MS,
CURSOR_TELEMETRY_VERSION,
MAX_CURSOR_SAMPLES,
} from "../constants";
import {
activeCursorSamples,
currentCursorVisualType,
cursorCaptureAccumulatedPausedMs,
cursorCaptureInterval,
cursorCapturePauseStartedAtMs,
cursorCaptureStartTimeMs,
isCursorCaptureActive,
linuxCursorScreenPoint,
pendingCursorSamples,
selectedSource,
selectedWindowBounds,
setActiveCursorSamples,
setCursorCaptureAccumulatedPausedMs,
setCursorCaptureInterval,
setCursorCapturePauseStartedAtMs,
setPendingCursorSamples,
} from "../state";
import type { CursorInteractionType, CursorTelemetryPoint, CursorVisualType } from "../types";
import { getScreen, getTelemetryPathForVideo } from "../utils";
export function clamp(value: number, min: number, max: number) {
return Math.min(max, Math.max(min, value));
}
export function normalizeCursorTelemetrySamples(rawSamples: unknown): CursorTelemetryPoint[] {
const samples = Array.isArray(rawSamples)
? rawSamples
: Array.isArray((rawSamples as { samples?: unknown[] } | null | undefined)?.samples)
? ((rawSamples as { samples: unknown[] }).samples ?? [])
: [];
const boundedSamples = samples.slice(0, MAX_CURSOR_SAMPLES);
return boundedSamples
.filter((sample: unknown) => Boolean(sample && typeof sample === "object"))
.map((sample: unknown) => {
const point = sample as Partial<CursorTelemetryPoint>;
return {
timeMs:
typeof point.timeMs === "number" && Number.isFinite(point.timeMs)
? Math.max(0, point.timeMs)
: 0,
cx:
typeof point.cx === "number" && Number.isFinite(point.cx)
? clamp(point.cx, 0, 1)
: 0.5,
cy:
typeof point.cy === "number" && Number.isFinite(point.cy)
? clamp(point.cy, 0, 1)
: 0.5,
interactionType:
point.interactionType === "click" ||
point.interactionType === "double-click" ||
point.interactionType === "right-click" ||
point.interactionType === "middle-click" ||
point.interactionType === "move" ||
point.interactionType === "mouseup"
? point.interactionType
: undefined,
cursorType:
point.cursorType === "arrow" ||
point.cursorType === "text" ||
point.cursorType === "pointer" ||
point.cursorType === "crosshair" ||
point.cursorType === "open-hand" ||
point.cursorType === "closed-hand" ||
point.cursorType === "resize-ew" ||
point.cursorType === "resize-ns" ||
point.cursorType === "not-allowed"
? point.cursorType
: undefined,
};
})
.sort((a, b) => a.timeMs - b.timeMs);
}
export async function writeCursorTelemetry(videoPath: string, samples: unknown) {
const telemetryPath = getTelemetryPathForVideo(videoPath);
const normalizedSamples = normalizeCursorTelemetrySamples(samples);
if (normalizedSamples.length === 0) {
await fs.rm(telemetryPath, { force: true });
return normalizedSamples;
}
await fs.writeFile(
telemetryPath,
JSON.stringify(
{ version: CURSOR_TELEMETRY_VERSION, samples: normalizedSamples },
null,
2,
),
"utf-8",
);
return normalizedSamples;
}
export function stopCursorCapture() {
if (cursorCaptureInterval) {
clearTimeout(cursorCaptureInterval);
setCursorCaptureInterval(null);
}
}
export function resetCursorCaptureClock() {
setCursorCaptureAccumulatedPausedMs(0);
setCursorCapturePauseStartedAtMs(null);
}
export function isCursorCapturePaused() {
return cursorCapturePauseStartedAtMs !== null;
}
export function pauseCursorCapture(pausedAtMs: number) {
if (cursorCapturePauseStartedAtMs !== null) {
return;
}
setCursorCapturePauseStartedAtMs(pausedAtMs);
}
export function pauseCursorCaptureAtBoundary(pausedAtMs: number) {
if (cursorCapturePauseStartedAtMs !== null) {
return;
}
const pausedElapsedMs = getCursorCaptureElapsedMs(pausedAtMs);
setActiveCursorSamples(
activeCursorSamples.filter((sample) => sample.timeMs <= pausedElapsedMs),
);
setCursorCapturePauseStartedAtMs(pausedAtMs);
}
export function resumeCursorCapture(resumedAtMs: number) {
if (cursorCapturePauseStartedAtMs === null) {
return;
}
const pauseDurationMs = Math.max(0, resumedAtMs - cursorCapturePauseStartedAtMs);
setCursorCaptureAccumulatedPausedMs(
cursorCaptureAccumulatedPausedMs + pauseDurationMs,
);
setCursorCapturePauseStartedAtMs(null);
}
export function getCursorCaptureElapsedMs(nowMs = Date.now()) {
if (!Number.isFinite(cursorCaptureStartTimeMs) || cursorCaptureStartTimeMs <= 0) {
return 0;
}
const safeNowMs = Math.max(cursorCaptureStartTimeMs, nowMs);
const activePauseDurationMs =
cursorCapturePauseStartedAtMs === null
? 0
: Math.max(0, safeNowMs - cursorCapturePauseStartedAtMs);
return Math.max(
0,
safeNowMs -
cursorCaptureStartTimeMs -
Math.max(0, cursorCaptureAccumulatedPausedMs) -
activePauseDurationMs,
);
}
export function getNormalizedCursorPoint() {
const fallbackCursor = getScreen().getCursorScreenPoint();
const linuxCursorCache = process.platform === "linux" ? linuxCursorScreenPoint : null;
const isLinuxCacheFresh = !!linuxCursorCache && Date.now() - linuxCursorCache.updatedAt <= 1000;
const primarySf =
process.platform !== "darwin" ? getScreen().getPrimaryDisplay().scaleFactor || 1 : 1;
const cursor = isLinuxCacheFresh
? { x: linuxCursorCache.x / primarySf, y: linuxCursorCache.y / primarySf }
: fallbackCursor;
const windowBounds = selectedSource?.id?.startsWith("window:") ? selectedWindowBounds : null;
if (windowBounds) {
const sf =
process.platform !== "darwin"
? getScreen().getDisplayNearestPoint({
x: windowBounds.x / primarySf,
y: windowBounds.y / primarySf,
}).scaleFactor || 1
: 1;
const width = Math.max(1, windowBounds.width / sf);
const height = Math.max(1, windowBounds.height / sf);
return {
cx: clamp((cursor.x - windowBounds.x / sf) / width, 0, 1),
cy: clamp((cursor.y - windowBounds.y / sf) / height, 0, 1),
};
}
const sourceDisplayId = Number(selectedSource?.display_id);
const sourceDisplay = Number.isFinite(sourceDisplayId)
? (getScreen()
.getAllDisplays()
.find((display) => display.id === sourceDisplayId) ?? null)
: null;
const display = sourceDisplay ?? getScreen().getDisplayNearestPoint(cursor);
const bounds = display.bounds;
const width = Math.max(1, bounds.width);
const height = Math.max(1, bounds.height);
const cx = clamp((cursor.x - bounds.x) / width, 0, 1);
const cy = clamp((cursor.y - bounds.y) / height, 0, 1);
return { cx, cy };
}
export function getHookCursorScreenPoint(
event: { x?: number; y?: number; data?: { x?: number; y?: number; screenX?: number; screenY?: number }; screenX?: number; screenY?: number } | null | undefined,
): { x: number; y: number } | null {
const rawX = event?.x ?? event?.data?.x ?? event?.screenX ?? event?.data?.screenX;
const rawY = event?.y ?? event?.data?.y ?? event?.screenY ?? event?.data?.screenY;
if (
typeof rawX !== "number" ||
!Number.isFinite(rawX) ||
typeof rawY !== "number" ||
!Number.isFinite(rawY)
) {
return null;
}
return { x: rawX, y: rawY };
}
export function pushCursorSample(
cx: number,
cy: number,
timeMs: number,
interactionType: CursorInteractionType = "move",
cursorType?: CursorVisualType,
) {
activeCursorSamples.push({
timeMs: Math.max(0, timeMs),
cx,
cy,
interactionType,
cursorType: cursorType ?? currentCursorVisualType,
} as CursorTelemetryPoint);
if (activeCursorSamples.length > MAX_CURSOR_SAMPLES) {
activeCursorSamples.shift();
}
}
export function sampleCursorPoint() {
const point = getNormalizedCursorPoint();
pushCursorSample(point.cx, point.cy, getCursorCaptureElapsedMs(), "move");
}
export async function persistPendingCursorTelemetry(videoPath: string) {
const telemetryPath = getTelemetryPathForVideo(videoPath);
if (pendingCursorSamples.length > 0) {
await fs.writeFile(
telemetryPath,
JSON.stringify(
{ version: CURSOR_TELEMETRY_VERSION, samples: pendingCursorSamples },
null,
2,
),
"utf-8",
);
}
setPendingCursorSamples([]);
}
export function snapshotCursorTelemetryForPersistence() {
if (activeCursorSamples.length === 0) {
return;
}
if (pendingCursorSamples.length === 0) {
setPendingCursorSamples([...activeCursorSamples]);
} else {
const lastPendingTimeMs =
pendingCursorSamples[pendingCursorSamples.length - 1]?.timeMs ?? -1;
setPendingCursorSamples([
...pendingCursorSamples,
...activeCursorSamples.filter((sample) => sample.timeMs > lastPendingTimeMs),
]);
}
activeCursorSamples.length = 0;
}
export function startCursorSampling() {
stopCursorCapture();
// Use recursive setTimeout with drift compensation instead of setInterval.
// Under CPU load setInterval bunches or skips callbacks, creating large gaps
// in telemetry data. This approach measures wall-clock drift each tick and
// adjusts the next delay so samples stay close to the target interval.
let nextExpectedMs = Date.now() + CURSOR_SAMPLE_INTERVAL_MS;
const tick = () => {
if (isCursorCaptureActive && !isCursorCapturePaused()) {
sampleCursorPoint();
}
const now = Date.now();
const drift = now - nextExpectedMs;
nextExpectedMs += CURSOR_SAMPLE_INTERVAL_MS;
// If we fell behind by more than one full interval, reset the baseline
// so we don't try to "catch up" with a burst of rapid samples.
if (drift > CURSOR_SAMPLE_INTERVAL_MS) {
nextExpectedMs = now + CURSOR_SAMPLE_INTERVAL_MS;
}
const delay = Math.max(1, nextExpectedMs - now);
setCursorCaptureInterval(setTimeout(tick, delay));
};
setCursorCaptureInterval(setTimeout(tick, CURSOR_SAMPLE_INTERVAL_MS));
}
export { CURSOR_SAMPLE_INTERVAL_MS } from "../constants";
// Re-export for consumers that use it from this module
export { getTelemetryPathForVideo } from "../utils";