param( [string]$Distro = "Ubuntu", [string]$Name = "ScreenShare", [switch]$NoHostSuffix, [switch]$LowLatency, [switch]$UseVsync, [switch]$HardwareDecode, [switch]$SoftwareDecode, [string]$VideoSink = "gtksink", [switch]$AutoVideo, [int]$PortBase = 35000, [switch]$Quiet, [string]$LogPath = "" ) $ErrorActionPreference = "Stop" $configPath = Join-Path $PSScriptRoot "config.ps1" if (Test-Path $configPath) { try { . $configPath } catch { } } if (-not $PSBoundParameters.ContainsKey("Name") -and $DefaultName) { $Name = $DefaultName } if ([string]::IsNullOrWhiteSpace($Name)) { $Name = "ScreenShare" } function Reset-OutputCursor { try { if ([Console]::CursorLeft -ne 0) { [Console]::WriteLine("") } [Console]::CursorLeft = 0 } catch { try { $pos = $Host.UI.RawUI.CursorPosition if ($pos.X -ne 0) { Write-Host "" } $Host.UI.RawUI.CursorPosition = New-Object Management.Automation.Host.Coordinates 0 $Host.UI.RawUI.CursorPosition.Y } catch { Write-Host "" } } } try { if ($Host.UI.RawUI.CursorPosition.X -ne 0) { Write-Host "" } } catch { } if (-not (Get-Command wsl.exe -ErrorAction SilentlyContinue)) { throw "WSL is not installed. Run scripts\\setup-wsl-airplay.ps1 first." } $distros = @() try { $distros = wsl -l -q 2>$null } catch { $distros = @() } if (-not $distros -or ($distros -notcontains $Distro)) { throw "WSL distro '$Distro' not found. Run scripts\\setup-wsl-airplay.ps1 first." } $uxplayPath = (wsl -d $Distro -- bash -lc "command -v uxplay" 2>$null).Trim() if (-not $uxplayPath) { throw "UxPlay is not installed in WSL. Run scripts\\setup-wsl-airplay.ps1 first." } if ($PortBase -ne 0) { if ($PortBase -lt 1024 -or $PortBase -gt 65533) { throw "PortBase must be between 1024 and 65533." } } $escapedName = $Name -replace '"', '\\"' $cmd = "uxplay -n `"$escapedName`"" if ($NoHostSuffix) { $cmd += " -nh" } $useLowLatency = $true if ($UseVsync) { $useLowLatency = $false } if ($LowLatency) { $useLowLatency = $true } $useHardwareDecode = $true if ($SoftwareDecode) { $useHardwareDecode = $false } if ($HardwareDecode) { $useHardwareDecode = $true } if (-not $useHardwareDecode) { $cmd += " -avdec" } if ($useLowLatency) { $cmd += " -vsync no" } if (-not $AutoVideo -and $VideoSink) { $cmd += " -vs $VideoSink" } if ($PortBase -ne 0) { $cmd += " -p $PortBase" } Reset-OutputCursor [Console]::WriteLine("Starting ScreenShare. Logs follow below.") $redirectLogs = $Quiet if ($redirectLogs -and [string]::IsNullOrWhiteSpace($LogPath)) { $LogPath = Join-Path $PSScriptRoot "screenshare.log" } $nativePrefSet = $false $nativePrefValue = $null if (Get-Variable -Name PSNativeCommandUseErrorActionPreference -ErrorAction SilentlyContinue) { $nativePrefSet = $true $nativePrefValue = $PSNativeCommandUseErrorActionPreference $PSNativeCommandUseErrorActionPreference = $false } $savedErrorActionPreference = $ErrorActionPreference if ($redirectLogs) { $ErrorActionPreference = "SilentlyContinue" } else { $ErrorActionPreference = "Continue" } try { if ($redirectLogs) { & wsl -d $Distro -- bash -lc $cmd 1>$LogPath 2>&1 Reset-OutputCursor [Console]::WriteLine("ScreenShare logs written to: $LogPath") } else { & wsl -d $Distro -- bash -lc $cmd } } finally { $ErrorActionPreference = $savedErrorActionPreference if ($nativePrefSet) { $PSNativeCommandUseErrorActionPreference = $nativePrefValue } }