Files
Reims-Go/internal/host/display_windows.go
T
NwflowerandClaude Opus 5.5 181ad64618 fix(display): mark HiDPI presets by reims-vgpu's own rule
Presets were flagged HiDPI by screen width alone, so a 2880x1800 preset
read "HiDPI" on a 100% screen where macOS runs it at 1x, and a VM left at
the 1920x1080 default ran 1x on a 200% screen.

- GuestStartsHiDPI follows upstream reims-vgpu 3731afd
  (RgfxDisplayIdentity.hpp, reims-vgpu-window features.md): HiDPI from a
  2048x1536 window above 160 ppi (150% scaling or more), or from
  3840x2160 at any scale.
- Presets are the 16:9 or 16:10 sizes that fit the screen's work area
  below a title bar, largest four; the largest HiDPI one is recommended.
  The display probe now reports the work area.
- When no preset is HiDPI, the resolution row says how to get there:
  borderless full screen, 150% Windows scaling, or not on this screen.

On this host (3072x1920 at 200%) the presets are 2560x1600 (HiDPI,
recommended), 1920x1200, 1680x1050 and 1280x800.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 23:48:52 -07:00

142 lines
3.8 KiB
Go

//go:build windows
package host
import (
"syscall"
"unsafe"
)
var (
user32 = syscall.NewLazyDLL("user32.dll")
procEnumDisplayMonitors = user32.NewProc("EnumDisplayMonitors")
procGetMonitorInfoW = user32.NewProc("GetMonitorInfoW")
procEnumDisplaySettings = user32.NewProc("EnumDisplaySettingsW")
procGetDC = user32.NewProc("GetDC")
procReleaseDC = user32.NewProc("ReleaseDC")
gdi32 = syscall.NewLazyDLL("gdi32.dll")
procGetDeviceCaps = gdi32.NewProc("GetDeviceCaps")
shcore = syscall.NewLazyDLL("shcore.dll")
procGetDpiForMonitor = shcore.NewProc("GetDpiForMonitor")
)
const (
monitorInfoPrimary = 0x00000001
enumCurrentSettings = 0xFFFFFFFF
mdtEffectiveDPI = 0
logPixelsX = 88
)
type winRect struct {
Left, Top, Right, Bottom int32
}
type monitorInfoExW struct {
Size uint32
Monitor winRect
Work winRect
Flags uint32
Device [32]uint16
}
type devModeW struct {
DeviceName [32]uint16
SpecVersion uint16
DriverVersion uint16
Size uint16
DriverExtra uint16
Fields uint32
PositionX int32
PositionY int32
DisplayOrientation uint32
DisplayFixedOutput uint32
Color int16
Duplex int16
YResolution int16
TTOption int16
Collate int16
FormName [32]uint16
LogPixels uint16
BitsPerPel uint32
PelsWidth uint32
PelsHeight uint32
DisplayFlags uint32
DisplayFrequency uint32
Extra [8]uint32
}
func probeDisplays() []DisplayInfo {
var displays []DisplayInfo
cb := syscall.NewCallback(func(hMon uintptr, hdc uintptr, lprc uintptr, data uintptr) uintptr {
var mi monitorInfoExW
mi.Size = uint32(unsafe.Sizeof(mi))
r, _, _ := procGetMonitorInfoW.Call(hMon, uintptr(unsafe.Pointer(&mi)))
if r == 0 {
return 1
}
name := syscall.UTF16ToString(mi.Device[:])
w := int(mi.Monitor.Right - mi.Monitor.Left)
h := int(mi.Monitor.Bottom - mi.Monitor.Top)
refresh := 0
// Query real physical mode dimensions via EnumDisplaySettingsW so DPI
// virtualization never under-reports a 4K/2K display as 1080p.
var dm devModeW
dm.Size = uint16(unsafe.Sizeof(dm))
if ok, _, _ := procEnumDisplaySettings.Call(
uintptr(unsafe.Pointer(&mi.Device[0])),
uintptr(enumCurrentSettings),
uintptr(unsafe.Pointer(&dm)),
); ok != 0 && dm.PelsWidth > 0 && dm.PelsHeight > 0 {
w = int(dm.PelsWidth)
h = int(dm.PelsHeight)
refresh = int(dm.DisplayFrequency)
}
scalePct := 100
if procGetDpiForMonitor.Find() == nil {
var dpiX, dpiY uint32
hr, _, _ := procGetDpiForMonitor.Call(
hMon,
uintptr(mdtEffectiveDPI),
uintptr(unsafe.Pointer(&dpiX)),
uintptr(unsafe.Pointer(&dpiY)),
)
if hr == 0 && dpiX >= 96 {
scalePct = int((dpiX*100 + 48) / 96)
}
} else if hdcScreen, _, _ := procGetDC.Call(0); hdcScreen != 0 {
dpi, _, _ := procGetDeviceCaps.Call(hdcScreen, uintptr(logPixelsX))
procReleaseDC.Call(0, hdcScreen)
if dpi >= 96 {
scalePct = int((dpi*100 + 48) / 96)
}
}
if w > 0 && h > 0 {
// The work area in the same units as the monitor rectangle (pixels for a
// DPI-aware process, scaled points otherwise), carried over to w x h.
var workW, workH int
if monW, monH := int(mi.Monitor.Right-mi.Monitor.Left), int(mi.Monitor.Bottom-mi.Monitor.Top); monW > 0 && monH > 0 {
workW = int(mi.Work.Right-mi.Work.Left) * w / monW
workH = int(mi.Work.Bottom-mi.Work.Top) * h / monH
}
displays = append(displays, DisplayInfo{
Name: name,
Width: w,
Height: h,
ScalePct: scalePct,
RefreshHz: refresh,
Primary: mi.Flags&monitorInfoPrimary != 0,
WorkWidth: workW,
WorkHeight: workH,
})
}
return 1
})
_, _, _ = procEnumDisplayMonitors.Call(0, 0, cb, 0)
return displays
}