igneum/tools/windows/dpi-check.ps1

36 lines
3.1 KiB
PowerShell

# dpi-check.ps1 (dpi-23, 7 October 2026): READ-ONLY. Reports the installed Igneum Miner window's DPI awareness, its rectangle in
# physical pixels, its monitor's DPI and scale, and the logical size that follows; the PC job runs it at 100, 150, 200 and 250
# percent after the user sets the scale (the script sets nothing). With -Expect PerMonitorV2 it exits 1 when the awareness
# differs (known-failed on the 0.3.22 host, which is Unaware, so Windows stretched its render). Touches nothing: no click, no
# message to the window, no setting; a job never quits or restarts the installed app.
param([string]$Expect = "")
Add-Type @"
using System; using System.Runtime.InteropServices;
public static class Dpi {
[StructLayout(LayoutKind.Sequential)] public struct RECT { public int Left, Top, Right, Bottom; }
[DllImport("user32.dll")] public static extern IntPtr FindWindowW([MarshalAs(UnmanagedType.LPWStr)] string cls, [MarshalAs(UnmanagedType.LPWStr)] string title);
[DllImport("user32.dll")] public static extern bool GetWindowRect(IntPtr h, out RECT r);
[DllImport("user32.dll")] public static extern IntPtr GetWindowDpiAwarenessContext(IntPtr h);
[DllImport("user32.dll")] public static extern int GetAwarenessFromDpiAwarenessContext(IntPtr ctx);
[DllImport("user32.dll")] public static extern bool AreDpiAwarenessContextsEqual(IntPtr a, IntPtr b);
[DllImport("user32.dll")] public static extern IntPtr MonitorFromWindow(IntPtr h, uint flags);
[DllImport("shcore.dll")] public static extern int GetDpiForMonitor(IntPtr mon, int type, out uint x, out uint y);
[DllImport("user32.dll")] public static extern bool SetProcessDpiAwarenessContext(IntPtr ctx);
}
"@
[void][Dpi]::SetProcessDpiAwarenessContext([IntPtr](-4)) # this script reads physical pixels itself
$h = [Dpi]::FindWindowW("IgneumMinerWindow", $null)
if ($h -eq [IntPtr]::Zero) { Write-Output "dpi-check: no Igneum Miner window (class IgneumMinerWindow) on this desktop"; exit 2 }
$ctx = [Dpi]::GetWindowDpiAwarenessContext($h)
$aw = [Dpi]::GetAwarenessFromDpiAwarenessContext($ctx) # 0 Unaware, 1 System, 2 PerMonitor
$v2 = [Dpi]::AreDpiAwarenessContextsEqual($ctx, [IntPtr](-4))
$word = if ($v2) { "PerMonitorV2" } elseif ($aw -eq 2) { "PerMonitor" } elseif ($aw -eq 1) { "System" } else { "Unaware" }
$r = New-Object Dpi+RECT; [void][Dpi]::GetWindowRect($h, [ref]$r)
$mon = [Dpi]::MonitorFromWindow($h, 2); $x = [uint32]96; $y = [uint32]96; [void][Dpi]::GetDpiForMonitor($mon, 0, [ref]$x, [ref]$y)
$w = $r.Right - $r.Left; $hh = $r.Bottom - $r.Top; $scale = [math]::Round($x / 96.0, 2)
$lw = [math]::Round($w * 96 / $x); $lh = [math]::Round($hh * 96 / $x)
Write-Output ("dpi-check: awareness {0}; window {1}x{2} physical at {3},{4}; monitor {5} dpi ({6} percent); logical {7}x{8}" -f $word, $w, $hh, $r.Left, $r.Top, $x, [int]($scale * 100), $lw, $lh)
$reg = Get-ItemProperty -Path "HKCU:\Software\Igneum\Miner" -ErrorAction SilentlyContinue
if ($reg) { Write-Output ("dpi-check: remembered WindowW {0} WindowH {1} WindowUnits {2}" -f $reg.WindowW, $reg.WindowH, $reg.WindowUnits) }
if ($Expect -ne "" -and $word -ne $Expect) { Write-Output ("dpi-check: FAIL, expected {0}" -f $Expect); exit 1 }
exit 0