Delete functions that no product code calls, and fold "simple plus WithOptions" pairs into the one signature callers actually use. Rename the live macOS lifecycle functions that were labelled Legacy. The architecture tests now share one source loader and reject functions without a product caller and catalog messages nobody reads. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
398 lines
13 KiB
Go
398 lines
13 KiB
Go
//go:build windows
|
|
|
|
package host
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"runtime"
|
|
"sync"
|
|
"syscall"
|
|
"unsafe"
|
|
|
|
"golang.org/x/sys/windows"
|
|
)
|
|
|
|
var (
|
|
procIsWindow = user32.NewProc("IsWindow")
|
|
procGetWindow = user32.NewProc("GetWindow")
|
|
procGetClassNameW = user32.NewProc("GetClassNameW")
|
|
procGetClientRect = user32.NewProc("GetClientRect")
|
|
procGetWindowRect = user32.NewProc("GetWindowRect")
|
|
procGetWindowLongW = user32.NewProc("GetWindowLongW")
|
|
procSetWindowLongW = user32.NewProc("SetWindowLongW")
|
|
procGetWindowPlacement = user32.NewProc("GetWindowPlacement")
|
|
procSetWindowPlacement = user32.NewProc("SetWindowPlacement")
|
|
procSetWindowPos = user32.NewProc("SetWindowPos")
|
|
procMonitorFromWindow = user32.NewProc("MonitorFromWindow")
|
|
procGetDpiForWindow = user32.NewProc("GetDpiForWindow")
|
|
procGetMenu = user32.NewProc("GetMenu")
|
|
procAdjustWindowRectExForDPI = user32.NewProc("AdjustWindowRectExForDpi")
|
|
|
|
taskbarOle32 = windows.NewLazySystemDLL("ole32.dll")
|
|
procCoInitializeEx = taskbarOle32.NewProc("CoInitializeEx")
|
|
procCoUninitialize = taskbarOle32.NewProc("CoUninitialize")
|
|
procCoCreateInstance = taskbarOle32.NewProc("CoCreateInstance")
|
|
clsidTaskbarList = windows.GUID{Data1: 0x56fdf344, Data2: 0xfd6d, Data3: 0x11d0, Data4: [8]byte{0x95, 0x8a, 0x00, 0x60, 0x97, 0xc9, 0xa0, 0x90}}
|
|
iidTaskbarList2 = windows.GUID{Data1: 0x602d4995, Data2: 0xb13a, Data3: 0x429b, Data4: [8]byte{0xa6, 0x6e, 0x19, 0x35, 0xe4, 0x4f, 0x43, 0x17}}
|
|
)
|
|
|
|
const (
|
|
gwOwner = 4
|
|
gwlStyle = -16
|
|
gwlExStyle = -20
|
|
monitorDefaultToNearest = 2
|
|
hwndTop = 0
|
|
swpNoSize = 0x0001
|
|
swpNoMove = 0x0002
|
|
swpNoZOrder = 0x0004
|
|
swpFrameChanged = 0x0020
|
|
swpShowWindow = 0x0040
|
|
swpNoOwnerZOrder = 0x0200
|
|
)
|
|
|
|
var (
|
|
fullscreenState = fullscreenWindows{
|
|
locks: make(map[uintptr]*fullscreenWindowLock),
|
|
states: make(map[uintptr]savedWindowState),
|
|
}
|
|
fullscreenEnumMu sync.Mutex
|
|
displayWindowCB = sync.OnceValue(func() uintptr { return syscall.NewCallback(enumDisplayWindow) })
|
|
displayWindowTargets map[uint32]bool // protected by fullscreenEnumMu during EnumWindows
|
|
displayWindowMatches []uintptr // protected by fullscreenEnumMu during EnumWindows
|
|
)
|
|
|
|
func enumDisplayWindow(hwnd, _ uintptr) uintptr {
|
|
if displayWindowTargets[windowPID(hwnd)] {
|
|
displayWindowMatches = append(displayWindowMatches, hwnd)
|
|
}
|
|
return 1
|
|
}
|
|
|
|
func isProcessDisplayWindow(hwnd uintptr, pid uint32) bool {
|
|
if hwnd == 0 || windowPID(hwnd) != pid {
|
|
return false
|
|
}
|
|
if visible, _, _ := procIsWindowVisible.Call(hwnd); visible == 0 {
|
|
return false
|
|
}
|
|
if owner, _, _ := procGetWindow.Call(hwnd, gwOwner); owner != 0 {
|
|
return false
|
|
}
|
|
var className [32]uint16
|
|
n, _, _ := procGetClassNameW.Call(hwnd, uintptr(unsafe.Pointer(&className[0])), uintptr(len(className)))
|
|
if n == 0 || syscall.UTF16ToString(className[:n]) == "#32770" {
|
|
return false
|
|
}
|
|
return hasDisplayClientArea(hwnd)
|
|
}
|
|
|
|
func IsForegroundDisplayWindow(hwnd uintptr, pid uint32) bool {
|
|
fg, _, _ := procGetForegroundWindow.Call()
|
|
return fg == hwnd && isProcessDisplayWindow(hwnd, pid)
|
|
}
|
|
|
|
func hasDisplayClientArea(hwnd uintptr) bool {
|
|
var client WindowRect
|
|
if iconic, _, _ := procIsIconic.Call(hwnd); iconic == 0 {
|
|
ok, _, _ := procGetClientRect.Call(hwnd, uintptr(unsafe.Pointer(&client)))
|
|
return ok != 0 && client.Right >= 480 && client.Bottom >= 300
|
|
}
|
|
|
|
// A minimized window reports an icon-sized client area. Check the size
|
|
// of its restored client area without activating auxiliary windows.
|
|
placement, err := getWindowPlacement(hwnd)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
style, err := getWindowLong(hwnd, gwlStyle)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
exStyle, err := getWindowLong(hwnd, gwlExStyle)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
dpi, _, _ := procGetDpiForWindow.Call(hwnd)
|
|
var hasMenu uintptr
|
|
if menu, _, _ := procGetMenu.Call(hwnd); menu != 0 {
|
|
hasMenu = 1
|
|
}
|
|
ok, _, _ := procAdjustWindowRectExForDPI.Call(uintptr(unsafe.Pointer(&client)), style, hasMenu, exStyle, dpi)
|
|
return ok != 0 &&
|
|
(placement.Normal.Right-placement.Normal.Left)-(client.Right-client.Left) >= 480 &&
|
|
(placement.Normal.Bottom-placement.Normal.Top)-(client.Bottom-client.Top) >= 300
|
|
}
|
|
|
|
// ProcessDisplayWindow finds the VM's display window, preferring the foreground display.
|
|
func ProcessDisplayWindow(pid uint32) uintptr {
|
|
return ProcessTreeDisplayWindow([]uint32{pid})
|
|
}
|
|
|
|
// ProcessTreeDisplayWindow finds the display window of a VM whose processes
|
|
// are pids (a launcher and the processes it started), preferring the
|
|
// foreground display.
|
|
func ProcessTreeDisplayWindow(pids []uint32) uintptr {
|
|
targets := make(map[uint32]bool, len(pids))
|
|
for _, pid := range pids {
|
|
if pid != 0 {
|
|
targets[pid] = true
|
|
}
|
|
}
|
|
if len(targets) == 0 {
|
|
return 0
|
|
}
|
|
fg, _, _ := procGetForegroundWindow.Call()
|
|
if fg != 0 && targets[windowPID(fg)] && isProcessDisplayWindow(fg, windowPID(fg)) {
|
|
return fg
|
|
}
|
|
fullscreenEnumMu.Lock()
|
|
displayWindowTargets = targets
|
|
procEnumWindows.Call(displayWindowCB(), 0)
|
|
matches := displayWindowMatches
|
|
displayWindowMatches = nil
|
|
displayWindowTargets = nil
|
|
fullscreenEnumMu.Unlock()
|
|
for _, hwnd := range matches {
|
|
if isProcessDisplayWindow(hwnd, windowPID(hwnd)) {
|
|
return hwnd
|
|
}
|
|
}
|
|
return 0
|
|
}
|
|
|
|
func liveWindowPID(hwnd uintptr) uint32 {
|
|
if ok, _, _ := procIsWindow.Call(hwnd); ok == 0 {
|
|
return 0
|
|
}
|
|
return windowPID(hwnd)
|
|
}
|
|
|
|
// IsProcessTreeWindowFullscreen is IsProcessWindowFullscreen for a process tree.
|
|
func IsProcessTreeWindowFullscreen(pids []uint32) bool {
|
|
fullscreenState.prune(liveWindowPID)
|
|
hwnd := ProcessTreeDisplayWindow(pids)
|
|
if hwnd == 0 {
|
|
return false
|
|
}
|
|
pid := windowPID(hwnd)
|
|
unlock := fullscreenState.lock(hwnd)
|
|
defer unlock()
|
|
if liveWindowPID(hwnd) != pid {
|
|
return false
|
|
}
|
|
style, err := getWindowLong(hwnd, gwlStyle)
|
|
if err != nil || style&(wsCaption|wsThickFrame) != 0 {
|
|
return false
|
|
}
|
|
exStyle, err := getWindowLong(hwnd, gwlExStyle)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
iconic, _, _ := procIsIconic.Call(hwnd)
|
|
saved, _ := fullscreenState.lookup(hwnd, pid)
|
|
rect, monitor, err := getFullscreenGeometry(hwnd, exStyle, iconic != 0, saved)
|
|
return err == nil && isBorderlessFullscreenRect(style, rect, WindowRect(monitor.Monitor))
|
|
}
|
|
|
|
// ToggleProcessTreeWindowFullscreen is ToggleProcessWindowFullscreen for a
|
|
// process tree: the display window may belong to any of pids.
|
|
func ToggleProcessTreeWindowFullscreen(pids []uint32) (bool, error) {
|
|
fullscreenState.prune(liveWindowPID)
|
|
hwnd := ProcessTreeDisplayWindow(pids)
|
|
if hwnd == 0 {
|
|
return false, fmt.Errorf("%w for pids %v", ErrDisplayWindowNotFound, pids)
|
|
}
|
|
pid := windowPID(hwnd)
|
|
unlock := fullscreenState.lock(hwnd)
|
|
defer unlock()
|
|
if liveWindowPID(hwnd) != pid {
|
|
return false, fmt.Errorf("%w for pid %d", ErrDisplayWindowNotFound, pid)
|
|
}
|
|
style, err := getWindowLong(hwnd, gwlStyle)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
exStyle, err := getWindowLong(hwnd, gwlExStyle)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
iconic, _, _ := procIsIconic.Call(hwnd)
|
|
saved, hasSaved := fullscreenState.lookup(hwnd, pid)
|
|
rect, monitor, err := getFullscreenGeometry(hwnd, exStyle, iconic != 0, saved)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
monitorRect := WindowRect(monitor.Monitor)
|
|
wasFullscreen := isBorderlessFullscreenRect(style, rect, monitorRect)
|
|
if iconic != 0 {
|
|
if liveWindowPID(hwnd) != pid {
|
|
return false, fmt.Errorf("%w for pid %d", ErrDisplayWindowNotFound, pid)
|
|
}
|
|
procShowWindow.Call(hwnd, swRestore)
|
|
if remaining, _, _ := procIsIconic.Call(hwnd); remaining != 0 {
|
|
return false, fmt.Errorf("could not restore display window for pid %d", pid)
|
|
}
|
|
// Save the restored styles, without WS_MINIMIZE, for rollback and exit.
|
|
style, err = getWindowLong(hwnd, gwlStyle)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
}
|
|
placement, err := getWindowPlacement(hwnd)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if liveWindowPID(hwnd) != pid {
|
|
return false, fmt.Errorf("%w for pid %d", ErrDisplayWindowNotFound, pid)
|
|
}
|
|
current := savedWindowState{pid: pid, style: style, exStyle: exStyle, placement: placement}
|
|
if wasFullscreen {
|
|
if hasSaved && !saved.wasFullscreen {
|
|
err = restoreWindowState(hwnd, saved)
|
|
} else {
|
|
err = resizeWindowFrame(hwnd, windowFrameStyles|wsVisible, exStyle, computeFallbackWindowRect(WindowRect(monitor.Work)))
|
|
}
|
|
if err != nil {
|
|
return false, rollbackWindowState(hwnd, current, err)
|
|
}
|
|
fullscreenState.forget(hwnd)
|
|
markTaskbarFullscreen(hwnd, false)
|
|
return false, nil
|
|
}
|
|
|
|
if err := resizeWindowFrame(hwnd, style&^windowFrameStyles, exStyle&^windowFrameExStyles, monitorRect); err != nil {
|
|
return false, rollbackWindowState(hwnd, current, err)
|
|
}
|
|
fullscreenState.remember(hwnd, current, monitorRect)
|
|
markTaskbarFullscreen(hwnd, true)
|
|
focusWindow(hwnd)
|
|
return true, nil
|
|
}
|
|
|
|
func getWindowLong(hwnd uintptr, index int32) (uintptr, error) {
|
|
value, _, err := procGetWindowLongW.Call(hwnd, uintptr(index))
|
|
// Go's Windows syscall trampoline clears last-error before calling Win32.
|
|
if value == 0 && err != syscall.Errno(0) {
|
|
return 0, fmt.Errorf("GetWindowLongW(%d): %w", index, err)
|
|
}
|
|
return uintptr(uint32(value)), nil
|
|
}
|
|
|
|
func setWindowLong(hwnd uintptr, index int32, value uintptr) error {
|
|
prev, _, err := procSetWindowLongW.Call(hwnd, uintptr(index), value)
|
|
if prev == 0 && err != syscall.Errno(0) {
|
|
return fmt.Errorf("SetWindowLongW(%d): %w", index, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func setWindowFrameStyles(hwnd, style, exStyle uintptr) error {
|
|
if err := setWindowLong(hwnd, gwlStyle, style); err != nil {
|
|
return err
|
|
}
|
|
return setWindowLong(hwnd, gwlExStyle, exStyle)
|
|
}
|
|
|
|
func getWindowPlacement(hwnd uintptr) (windowPlacement, error) {
|
|
var placement windowPlacement
|
|
placement.Size = uint32(unsafe.Sizeof(placement))
|
|
if ok, _, err := procGetWindowPlacement.Call(hwnd, uintptr(unsafe.Pointer(&placement))); ok == 0 {
|
|
return placement, fmt.Errorf("GetWindowPlacement: %w", err)
|
|
}
|
|
return placement, nil
|
|
}
|
|
|
|
func getFullscreenGeometry(hwnd, exStyle uintptr, minimized bool, saved savedWindowState) (WindowRect, monitorInfoExW, error) {
|
|
var rect WindowRect
|
|
var monitor monitorInfoExW
|
|
hmon, _, _ := procMonitorFromWindow.Call(hwnd, monitorDefaultToNearest)
|
|
monitor.Size = uint32(unsafe.Sizeof(monitor))
|
|
if ok, _, err := procGetMonitorInfoW.Call(hmon, uintptr(unsafe.Pointer(&monitor))); ok == 0 {
|
|
return rect, monitor, fmt.Errorf("GetMonitorInfoW: %w", err)
|
|
}
|
|
if minimized {
|
|
placement, err := getWindowPlacement(hwnd)
|
|
if err != nil {
|
|
return rect, monitor, err
|
|
}
|
|
return minimizedWindowRect(placement.Normal, exStyle, WindowRect(monitor.Monitor), WindowRect(monitor.Work), saved), monitor, nil
|
|
}
|
|
if ok, _, err := procGetWindowRect.Call(hwnd, uintptr(unsafe.Pointer(&rect))); ok == 0 {
|
|
return rect, monitor, fmt.Errorf("GetWindowRect: %w", err)
|
|
}
|
|
return rect, monitor, nil
|
|
}
|
|
|
|
type taskbarList2VTable struct {
|
|
queryInterface, addRef, release uintptr
|
|
hrInit, addTab, deleteTab, activateTab, setActiveAlt uintptr
|
|
markFullscreenWindow uintptr
|
|
}
|
|
|
|
func markTaskbarFullscreen(hwnd uintptr, fullscreen bool) {
|
|
const (
|
|
coinitApartmentThreaded = 2
|
|
clsctxInprocServer = 1
|
|
rpcEChangedMode = 0x80010106
|
|
)
|
|
runtime.LockOSThread()
|
|
defer runtime.UnlockOSThread()
|
|
hr, _, _ := procCoInitializeEx.Call(0, coinitApartmentThreaded)
|
|
if int32(hr) >= 0 {
|
|
defer procCoUninitialize.Call()
|
|
} else if uint32(hr) != rpcEChangedMode {
|
|
return
|
|
}
|
|
var taskbar *struct{ vtable *taskbarList2VTable }
|
|
hr, _, _ = procCoCreateInstance.Call(uintptr(unsafe.Pointer(&clsidTaskbarList)), 0, clsctxInprocServer,
|
|
uintptr(unsafe.Pointer(&iidTaskbarList2)), uintptr(unsafe.Pointer(&taskbar)))
|
|
if int32(hr) < 0 {
|
|
return
|
|
}
|
|
defer syscall.SyscallN(taskbar.vtable.release, uintptr(unsafe.Pointer(taskbar)))
|
|
if hr, _, _ := syscall.SyscallN(taskbar.vtable.hrInit, uintptr(unsafe.Pointer(taskbar))); int32(hr) < 0 {
|
|
return
|
|
}
|
|
var fullscreenFlag uintptr
|
|
if fullscreen {
|
|
fullscreenFlag = 1
|
|
}
|
|
syscall.SyscallN(taskbar.vtable.markFullscreenWindow, uintptr(unsafe.Pointer(taskbar)), hwnd, fullscreenFlag)
|
|
}
|
|
|
|
func resizeWindowFrame(hwnd, style, exStyle uintptr, rect WindowRect) error {
|
|
if err := setWindowFrameStyles(hwnd, style, exStyle); err != nil {
|
|
return err
|
|
}
|
|
return setWindowRect(hwnd, rect, swpNoOwnerZOrder|swpFrameChanged|swpShowWindow)
|
|
}
|
|
|
|
func restoreWindowState(hwnd uintptr, state savedWindowState) error {
|
|
if err := setWindowFrameStyles(hwnd, state.style, state.exStyle); err != nil {
|
|
return err
|
|
}
|
|
if ok, _, err := procSetWindowPlacement.Call(hwnd, uintptr(unsafe.Pointer(&state.placement))); ok == 0 {
|
|
return fmt.Errorf("SetWindowPlacement: %w", err)
|
|
}
|
|
return setWindowRect(hwnd, WindowRect{}, swpNoMove|swpNoSize|swpNoZOrder|swpNoOwnerZOrder|swpFrameChanged)
|
|
}
|
|
|
|
func setWindowRect(hwnd uintptr, rect WindowRect, flags uintptr) error {
|
|
ok, _, err := procSetWindowPos.Call(hwnd, hwndTop, uintptr(rect.Left), uintptr(rect.Top),
|
|
uintptr(rect.Right-rect.Left), uintptr(rect.Bottom-rect.Top), flags)
|
|
if ok == 0 {
|
|
return fmt.Errorf("SetWindowPos: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func rollbackWindowState(hwnd uintptr, state savedWindowState, cause error) error {
|
|
if liveWindowPID(hwnd) == state.pid {
|
|
if err := restoreWindowState(hwnd, state); err != nil {
|
|
return errors.Join(cause, fmt.Errorf("restore display window after failure: %w", err))
|
|
}
|
|
}
|
|
return cause
|
|
}
|