191 lines
5.1 KiB
Go
191 lines
5.1 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")
|
|
procEnumDisplayDevices = user32.NewProc("EnumDisplayDevicesW")
|
|
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 displayDeviceW struct {
|
|
Size uint32
|
|
DeviceName [32]uint16
|
|
DeviceString [128]uint16
|
|
StateFlags uint32
|
|
DeviceID [128]uint16
|
|
DeviceKey [128]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
|
|
}
|
|
|
|
type displayMonitor struct {
|
|
handle uintptr
|
|
info monitorInfoExW
|
|
}
|
|
|
|
var (
|
|
displayEnumMu sync.Mutex
|
|
displayEnumCB = sync.OnceValue(func() uintptr { return syscall.NewCallback(enumDisplayMonitorProc) })
|
|
displayMonitors []displayMonitor
|
|
)
|
|
|
|
func enumDisplayMonitorProc(hMon uintptr, _ uintptr, _ uintptr, _ uintptr) uintptr {
|
|
var mi monitorInfoExW
|
|
mi.Size = uint32(unsafe.Sizeof(mi))
|
|
r, _, _ := procGetMonitorInfoW.Call(hMon, uintptr(unsafe.Pointer(&mi)))
|
|
if r != 0 {
|
|
displayMonitors = append(displayMonitors, displayMonitor{handle: hMon, info: mi})
|
|
}
|
|
return 1
|
|
}
|
|
|
|
// Reuse one callback slot because Go's Windows callback table never frees slots.
|
|
func enumerateDisplayMonitors() ([]displayMonitor, error) {
|
|
displayEnumMu.Lock()
|
|
defer displayEnumMu.Unlock()
|
|
ok, _, err := procEnumDisplayMonitors.Call(0, 0, displayEnumCB(), 0)
|
|
monitors := displayMonitors
|
|
displayMonitors = nil
|
|
if ok == 0 {
|
|
return monitors, err
|
|
}
|
|
return monitors, nil
|
|
}
|
|
|
|
func probeDisplays() []DisplayInfo {
|
|
monitors, _ := enumerateDisplayMonitors()
|
|
var displays []DisplayInfo
|
|
for _, monitor := range monitors {
|
|
display := readDisplayInfo(monitor)
|
|
if display.Width > 0 && display.Height > 0 {
|
|
displays = append(displays, display)
|
|
}
|
|
}
|
|
return displays
|
|
}
|
|
|
|
func readDisplayInfo(monitor displayMonitor) DisplayInfo {
|
|
mi := monitor.info
|
|
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(
|
|
monitor.handle,
|
|
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)
|
|
}
|
|
}
|
|
|
|
// Scale the work area dimensions to the physical display mode.
|
|
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
|
|
}
|
|
var device displayDeviceW
|
|
device.Size = uint32(unsafe.Sizeof(device))
|
|
friendlyName := ""
|
|
if ok, _, _ := procEnumDisplayDevices.Call(uintptr(unsafe.Pointer(&mi.Device[0])), 0, uintptr(unsafe.Pointer(&device)), 0); ok != 0 {
|
|
friendlyName = syscall.UTF16ToString(device.DeviceString[:])
|
|
}
|
|
return DisplayInfo{
|
|
Name: syscall.UTF16ToString(mi.Device[:]),
|
|
FriendlyName: friendlyName,
|
|
Width: w,
|
|
Height: h,
|
|
ScalePct: scalePct,
|
|
RefreshHz: refresh,
|
|
Primary: mi.Flags&monitorInfoPrimary != 0,
|
|
WorkWidth: workW,
|
|
WorkHeight: workH,
|
|
}
|
|
}
|