375 lines
12 KiB
Go
375 lines
12 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) })
|
|
displayWindowMatches []uintptr // protected by fullscreenEnumMu during EnumWindows
|
|
)
|
|
|
|
func enumDisplayWindow(hwnd, pid uintptr) uintptr {
|
|
if windowPID(hwnd) == uint32(pid) {
|
|
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 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 {
|
|
if pid == 0 {
|
|
return 0
|
|
}
|
|
fg, _, _ := procGetForegroundWindow.Call()
|
|
if isProcessDisplayWindow(fg, pid) {
|
|
return fg
|
|
}
|
|
fullscreenEnumMu.Lock()
|
|
procEnumWindows.Call(displayWindowCB(), uintptr(pid))
|
|
matches := displayWindowMatches
|
|
displayWindowMatches = nil
|
|
fullscreenEnumMu.Unlock()
|
|
for _, hwnd := range matches {
|
|
if isProcessDisplayWindow(hwnd, pid) {
|
|
return hwnd
|
|
}
|
|
}
|
|
return 0
|
|
}
|
|
|
|
func liveWindowPID(hwnd uintptr) uint32 {
|
|
if ok, _, _ := procIsWindow.Call(hwnd); ok == 0 {
|
|
return 0
|
|
}
|
|
return windowPID(hwnd)
|
|
}
|
|
|
|
// IsProcessWindowFullscreen reports whether the selected display window fills its monitor without a frame.
|
|
func IsProcessWindowFullscreen(pid uint32) bool {
|
|
fullscreenState.prune(liveWindowPID)
|
|
hwnd := ProcessDisplayWindow(pid)
|
|
if hwnd == 0 {
|
|
return false
|
|
}
|
|
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))
|
|
}
|
|
|
|
// ToggleProcessWindowFullscreen switches the selected display window between
|
|
// borderless fullscreen and windowed mode.
|
|
func ToggleProcessWindowFullscreen(pid uint32) (bool, error) {
|
|
fullscreenState.prune(liveWindowPID)
|
|
hwnd := ProcessDisplayWindow(pid)
|
|
if hwnd == 0 {
|
|
return false, fmt.Errorf("%w for pid %d", ErrDisplayWindowNotFound, pid)
|
|
}
|
|
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
|
|
}
|