Last active
January 25, 2022 10:37
-
-
Save Nama/dee2515bf6ed8ef889e18ce5c1da57ea to your computer and use it in GitHub Desktop.
We can't have nice things cause of anti-piracy (again). Sound isn't send over HDMI if no video is sent. Auto turning off displays is not convenient. This script starts the screensaver if the HDMI audio output is currently the default device. Enable screensaver (set time to 999) and disable displays turning off. Checks for playback activity.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://stackoverflow.com/a/39319540 | |
Add-Type @' | |
using System; | |
using System.Diagnostics; | |
using System.Runtime.InteropServices; | |
namespace PInvoke.Win32 { | |
public static class UserInput { | |
[DllImport("user32.dll", SetLastError=false)] | |
private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii); | |
[StructLayout(LayoutKind.Sequential)] | |
private struct LASTINPUTINFO { | |
public uint cbSize; | |
public int dwTime; | |
} | |
public static DateTime LastInput { | |
get { | |
DateTime bootTime = DateTime.UtcNow.AddMilliseconds(-Environment.TickCount); | |
DateTime lastInput = bootTime.AddMilliseconds(LastInputTicks); | |
return lastInput; | |
} | |
} | |
public static TimeSpan IdleTime { | |
get { | |
return DateTime.UtcNow.Subtract(LastInput); | |
} | |
} | |
public static int LastInputTicks { | |
get { | |
LASTINPUTINFO lii = new LASTINPUTINFO(); | |
lii.cbSize = (uint)Marshal.SizeOf(typeof(LASTINPUTINFO)); | |
GetLastInputInfo(ref lii); | |
return lii.dwTime; | |
} | |
} | |
} | |
} | |
'@ | |
$IdleTarget = 360 | |
$HDMIAudioDevice = 'TV (NVIDIA High Definition Audio)' | |
$nircmd = 'D:\Patches\nircmd.exe' | |
$debug = $false | |
# Second line of the output from `powercfg -requests` while NO video is playing | |
$ActiveDisplayKey = 'Keine.' | |
$IdleSeconds = [PInvoke.Win32.UserInput]::IdleTime.TotalSeconds | |
Get-AudioDevice -Playback | |
$DefaultSoundDevice = Get-AudioDevice -Playback | Select-Object -Property Name | |
$LockedScreen = Get-Process logonui -erroraction 'silentlycontinue' | |
$ActiveDisplay = powercfg -requests | |
# Debug | |
if ($debug) { | |
$output_file = ( | |
Write-Output "IdleTarget: $IdleTarget" && | |
Write-Output "HDMIAudioDevice: $HDMIAudioDevice" && | |
Write-Output "Dynamic Values:" && | |
Write-Output "IdleSeconds: $IdleSeconds" && | |
Write-Output "DefaultSoundDevice: $($DefaultSoundDevice.Name)" && | |
Write-Output "LockedScreen: $LockedScreen" && | |
Write-Output "ActiveDisplay: $($ActiveDisplay[1])" | |
) | Out-String | |
} | |
Write-Output $output_file | |
if ($LockedScreen) { | |
& $nircmd ('monitor', 'async_off') | |
if ($debug) { | |
Write-Output $output_file >> 'G:\monitor_away_debug.log' | |
Write-Output 'Monitor off - locked screen' >> 'G:\monitor_away_debug.log' | |
} | |
} | |
elseif ($IdleSeconds -ge $IdleTarget) { | |
if ($ActiveDisplay[1] -eq $ActiveDisplayKey) { | |
if ($DefaultSoundDevice.Name -eq $HDMIAudioDevice) { | |
& $nircmd 'screensaver' | |
if ($debug) { | |
Write-Output $output_file >> 'G:\monitor_away_debug.log' | |
Write-Output 'Screensaver on - HDMI Audio' >> 'G:\monitor_away_debug.log' | |
} | |
} | |
else { | |
& $nircmd ('monitor', 'async_off') | |
if ($debug) { | |
Write-Output $output_file >> 'G:\monitor_away_debug.log' | |
Write-Output 'Monitor off - No HDMI Audio' >> 'G:\monitor_away_debug.log' | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment