165 lines
4.4 KiB
Go
165 lines
4.4 KiB
Go
//go:build windows
|
|
|
|
package host
|
|
|
|
import (
|
|
"sync"
|
|
"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
|
|
}
|
|
|
|
var (
|
|
displayEnumMu sync.Mutex
|
|
displayEnumOnce sync.Once
|
|
displayEnumCB uintptr
|
|
displayEnumResult *[]DisplayInfo
|
|
)
|
|
|
|
func enumDisplayMonitorProc(hMon uintptr, _ uintptr, _ uintptr, _ uintptr) uintptr {
|
|
out := displayEnumResult
|
|
if out == nil {
|
|
return 0
|
|
}
|
|
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
|
|
}
|
|
*out = append(*out, DisplayInfo{
|
|
Name: name,
|
|
Width: w,
|
|
Height: h,
|
|
ScalePct: scalePct,
|
|
RefreshHz: refresh,
|
|
Primary: mi.Flags&monitorInfoPrimary != 0,
|
|
WorkWidth: workW,
|
|
WorkHeight: workH,
|
|
})
|
|
}
|
|
return 1
|
|
}
|
|
|
|
// probeDisplays enumerates connected monitors reusing a single syscall.NewCallback
|
|
// slot for the lifetime of the process (Go's Windows callback table has a fixed
|
|
// 2000-slot limit and never frees allocated callbacks).
|
|
func probeDisplays() []DisplayInfo {
|
|
displayEnumOnce.Do(func() {
|
|
displayEnumCB = syscall.NewCallback(enumDisplayMonitorProc)
|
|
})
|
|
displayEnumMu.Lock()
|
|
defer displayEnumMu.Unlock()
|
|
var displays []DisplayInfo
|
|
displayEnumResult = &displays
|
|
defer func() { displayEnumResult = nil }()
|
|
_, _, _ = procEnumDisplayMonitors.Call(0, 0, displayEnumCB, 0)
|
|
return displays
|
|
}
|