Skip to content

Commit bc51499

Browse files
author
Abhijeet Arch
committed
feat(linux): add Hyprland cursor tracking and position zoom
1 parent d8889c6 commit bc51499

16 files changed

Lines changed: 979 additions & 46 deletions

File tree

‎crates/compositor/src/frame_geometry.rs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9279,6 +9279,22 @@ mod tests {
92799279
assert_eq!(fx(2.5).cursor, Some(drawn.placement.upright_center()), "là où le curseur se dessine");
92809280
}
92819281

9282+
/// Hyprland position-only sidecars have no bitmap assets or cursor type. The
9283+
/// renderer must still select OpenScreen's bundled arrow sprite, with its
9284+
/// configured hotspot, for both preview and export.
9285+
#[test]
9286+
fn position_only_track_uses_the_bundled_arrow_sprite() {
9287+
let scene = model_scene();
9288+
let cfg = crate::config::all().pop().expect("cfg");
9289+
let fg = full_frame_geometry();
9290+
let track = still_track(vec![]);
9291+
let plan = plan_cursor(&fg, &cursor_input(&scene, &cfg, &track, 1.0)).expect("visible");
9292+
9293+
assert_eq!(plan.cursor_type, None, "position-only samples have no native shape");
9294+
assert!(plan.placement.upright_center()[0] > 0.0);
9295+
assert!(plan.placement.upright_center()[1] > 0.0);
9296+
}
9297+
92829298
/// Masqué par l'utilisateur, le curseur n'est plus dessiné, mais les effets gardent la pointe
92839299
/// et les clics : un halo suit toujours la souris, une onde part toujours du clic. Quand c'est
92849300
/// l'enregistrement qui cache la pointe, les effets la perdent, et gardent les clics d'avant.

‎electron/ipc/handlers.ts‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2664,7 +2664,11 @@ export function registerIpcHandlers(
26642664

26652665
const session = new LinuxNativeCaptureSession({
26662666
outputPath,
2667-
cursorMode: portalCursorMode(cursorCaptureMode),
2667+
cursorMode: portalCursorMode(
2668+
cursorCaptureMode,
2669+
Boolean(process.env.HYPRLAND_INSTANCE_SIGNATURE),
2670+
),
2671+
sampleIntervalMs: CURSOR_SAMPLE_INTERVAL_MS,
26682672
fps: request.video.fps,
26692673
...(request.video.bitrate ? { bitrate: request.video.bitrate } : {}),
26702674
audio: {
@@ -2750,7 +2754,11 @@ export function registerIpcHandlers(
27502754
prepared ??
27512755
new LinuxNativeCaptureSession({
27522756
outputPath,
2753-
cursorMode: portalCursorMode(cursorCaptureMode),
2757+
cursorMode: portalCursorMode(
2758+
cursorCaptureMode,
2759+
Boolean(process.env.HYPRLAND_INSTANCE_SIGNATURE),
2760+
),
2761+
sampleIntervalMs: CURSOR_SAMPLE_INTERVAL_MS,
27542762
fps: request.video.fps,
27552763
...(request.video.bitrate ? { bitrate: request.video.bitrate } : {}),
27562764
audio: {
@@ -2853,7 +2861,11 @@ export function registerIpcHandlers(
28532861
// The helper collects cursor samples itself, from the same portal
28542862
// session that produced the pixels, so there is no separate sampler
28552863
// to stop and no clock offset to correct — the two are one recording.
2856-
if (cursorCaptureMode === "editable-overlay" && result.cursor.samples.length > 0) {
2864+
// Keep the sidecar even when a run produced zero samples. An empty
2865+
// sidecar is an explicit native-capture diagnostic; omitting it made a
2866+
// failed Hyprland IPC session indistinguishable from the Chromium
2867+
// fallback and hid the real cause of an invisible cursor.
2868+
if (cursorCaptureMode === "editable-overlay") {
28572869
await fs.writeFile(
28582870
`${result.path}.cursor.json`,
28592871
JSON.stringify(result.cursor, null, 2),

‎electron/native-bridge/capture/linuxNativeCaptureSession.ts‎

Lines changed: 94 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { type ChildProcessByStdio, spawn } from "node:child_process";
22
import type { Readable, Writable } from "node:stream";
33
import type { CursorRecordingData } from "../../../src/native/contracts";
4+
import { HyprlandCursorTracker } from "../cursor/recording/hyprlandCursorTracker";
45
import {
56
NdjsonLineReader,
67
PipeWireCursorAccumulator,
@@ -52,7 +53,8 @@ const HELPER_OUTPUT_MAX_BYTES = 64 * 1024;
5253

5354
export interface LinuxCaptureConfig {
5455
outputPath: string;
55-
cursorMode: "metadata" | "embedded";
56+
cursorMode: "auto" | "metadata" | "embedded" | "hidden";
57+
sampleIntervalMs?: number;
5658
fps: number;
5759
bitrate?: number;
5860
audio: {
@@ -114,6 +116,9 @@ export class LinuxNativeCaptureSession {
114116
private lastError: string | null = null;
115117
private paused = false;
116118
private helperOutputText = "";
119+
private effectiveCursorMode: "metadata" | "embedded" | "hidden" = "metadata";
120+
private cursorMetadataSupported: boolean | null = null;
121+
private hyprlandCursor: HyprlandCursorTracker | null = null;
117122

118123
constructor(private readonly config: LinuxCaptureConfig) {
119124
this.cursor = new PipeWireCursorAccumulator(config.maxCursorSamples);
@@ -306,6 +311,7 @@ export class LinuxNativeCaptureSession {
306311
return;
307312
}
308313
this.paused = true;
314+
this.hyprlandCursor?.pause();
309315
this.write("pause");
310316
}
311317

@@ -314,6 +320,7 @@ export class LinuxNativeCaptureSession {
314320
return;
315321
}
316322
this.paused = false;
323+
this.hyprlandCursor?.resume();
317324
this.write("resume");
318325
}
319326

@@ -416,6 +423,8 @@ export class LinuxNativeCaptureSession {
416423
}
417424

418425
private kill() {
426+
this.hyprlandCursor?.stop();
427+
this.hyprlandCursor = null;
419428
const child = this.process;
420429
if (!child || child.killed) {
421430
return;
@@ -447,9 +456,35 @@ export class LinuxNativeCaptureSession {
447456

448457
private handleEvent(payload: PipeWireHelperEvent) {
449458
switch (payload.event) {
450-
case "ready":
459+
case "ready": {
460+
this.cursorMetadataSupported =
461+
typeof payload.cursorMetadataSupported === "boolean"
462+
? payload.cursorMetadataSupported
463+
: null;
464+
this.effectiveCursorMode =
465+
payload.cursorMode === "hidden"
466+
? "hidden"
467+
: payload.cursorMode === "embedded"
468+
? "embedded"
469+
: "metadata";
470+
const useHyprlandPositionTelemetry =
471+
this.config.cursorMode === "auto" &&
472+
(this.effectiveCursorMode !== "metadata" || this.cursorMetadataSupported === false);
473+
this.ensureHyprlandCursorTracker(
474+
useHyprlandPositionTelemetry || this.effectiveCursorMode === "hidden",
475+
);
476+
console.info(
477+
"[capture-linux] cursor negotiation",
478+
JSON.stringify({
479+
requested: this.config.cursorMode,
480+
effective: this.effectiveCursorMode,
481+
metadataSupported: payload.cursorMetadataSupported ?? null,
482+
hyprlandTracker: Boolean(this.hyprlandCursor),
483+
}),
484+
);
451485
this.resolveReady();
452486
return;
487+
}
453488

454489
case "source-selected":
455490
if (payload.sourceKind) {
@@ -483,6 +518,24 @@ export class LinuxNativeCaptureSession {
483518
sourceKind: payload.sourceKind ?? null,
484519
}),
485520
);
521+
// Some helper/portal combinations report the negotiated mode before
522+
// the stream details are available. Re-evaluate here so hidden-cursor
523+
// capture cannot silently lose its Hyprland position telemetry.
524+
this.ensureHyprlandCursorTracker(
525+
(this.config.cursorMode === "auto" &&
526+
(this.effectiveCursorMode !== "metadata" || this.cursorMetadataSupported === false)) ||
527+
this.effectiveCursorMode === "hidden",
528+
);
529+
if (this.hyprlandCursor && !this.hyprlandCursor.hasCaptureGeometry()) {
530+
this.hyprlandCursor.setCaptureGeometry({
531+
position:
532+
payload.positionX != null && payload.positionY != null
533+
? { x: payload.positionX, y: payload.positionY }
534+
: null,
535+
width: payload.logicalWidth ?? payload.width,
536+
height: payload.logicalHeight ?? payload.height,
537+
});
538+
}
486539
return;
487540

488541
case "audio-source":
@@ -518,6 +571,22 @@ export class LinuxNativeCaptureSession {
518571
// telemetry is re-based onto it and anything from during the
519572
// picker is dropped rather than left pinned to the start.
520573
this.cursor.rebase(payload.timestampMs);
574+
this.ensureHyprlandCursorTracker(
575+
(this.config.cursorMode === "auto" &&
576+
(this.effectiveCursorMode !== "metadata" || this.cursorMetadataSupported === false)) ||
577+
this.effectiveCursorMode === "hidden",
578+
);
579+
this.hyprlandCursor?.rebase();
580+
// Older helper builds and some portal backends may omit stream-started
581+
// geometry. The encoded frame dimensions are still sufficient for the
582+
// monitor-size/origin fallback in HyprlandCursorTracker.
583+
if (this.hyprlandCursor && !this.hyprlandCursor.hasCaptureGeometry()) {
584+
this.hyprlandCursor.setCaptureGeometry({
585+
position: null,
586+
width: payload.width,
587+
height: payload.height,
588+
});
589+
}
521590
this.capturing = true;
522591
console.info(
523592
"[capture-linux] capture started",
@@ -529,16 +598,18 @@ export class LinuxNativeCaptureSession {
529598
return;
530599

531600
case "cursor-sample":
532-
this.cursor.addSample(payload);
601+
if (this.effectiveCursorMode === "metadata") this.cursor.addSample(payload);
533602
return;
534603

535-
case "capture-stopped":
604+
case "capture-stopped": {
605+
const cursor = this.hyprlandCursor?.stop() ?? this.cursor.toRecordingData();
606+
this.hyprlandCursor = null;
536607
this.stopped = {
537608
path: payload.path,
538609
durationMs: payload.durationMs,
539610
frames: payload.frames,
540611
droppedFrames: payload.dropped,
541-
cursor: this.cursor.toRecordingData(),
612+
cursor,
542613
...(this.sourceKind ? { sourceKind: this.sourceKind } : {}),
543614
...(this.videoEncoder ? { videoEncoder: this.videoEncoder } : {}),
544615
};
@@ -551,9 +622,12 @@ export class LinuxNativeCaptureSession {
551622
convertMs: payload.convertMs,
552623
uploadMs: payload.uploadMs,
553624
encodeMs: payload.encodeMs,
625+
cursorProvider: cursor.provider,
626+
cursorSamples: cursor.samples.length,
554627
}),
555628
);
556629
return;
630+
}
557631

558632
case "warning":
559633
console.warn(`[capture-linux] ${payload.code}: ${payload.message}`);
@@ -576,6 +650,21 @@ export class LinuxNativeCaptureSession {
576650
}
577651
}
578652

653+
private ensureHyprlandCursorTracker(needed: boolean) {
654+
if (!needed || this.hyprlandCursor) return;
655+
if (!HyprlandCursorTracker.isAvailable()) {
656+
console.warn(
657+
"[capture-linux] position-only cursor capture requested, but the Hyprland IPC socket is unavailable",
658+
);
659+
return;
660+
}
661+
this.hyprlandCursor = new HyprlandCursorTracker({
662+
sampleIntervalMs: this.config.sampleIntervalMs ?? 33,
663+
maxSamples: this.config.maxCursorSamples,
664+
});
665+
this.hyprlandCursor.start();
666+
}
667+
579668
private resolveReady() {
580669
const resolve = this.readyResolve;
581670
this.clearReadyState();

0 commit comments

Comments
 (0)