11import { type ChildProcessByStdio , spawn } from "node:child_process" ;
22import type { Readable , Writable } from "node:stream" ;
33import type { CursorRecordingData } from "../../../src/native/contracts" ;
4+ import { HyprlandCursorTracker } from "../cursor/recording/hyprlandCursorTracker" ;
45import {
56 NdjsonLineReader ,
67 PipeWireCursorAccumulator ,
@@ -52,7 +53,8 @@ const HELPER_OUTPUT_MAX_BYTES = 64 * 1024;
5253
5354export 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