84 lines
2.5 KiB
Go
84 lines
2.5 KiB
Go
//go:build windows
|
|
|
|
package host
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"syscall"
|
|
)
|
|
|
|
func findDisplayMonitor(name string) (monitorInfoExW, error) {
|
|
monitors, err := enumerateDisplayMonitors()
|
|
if err != nil {
|
|
return monitorInfoExW{}, fmt.Errorf("EnumDisplayMonitors: %w", err)
|
|
}
|
|
for _, monitor := range monitors {
|
|
mi := monitor.info
|
|
if name == "" && mi.Flags&monitorInfoPrimary != 0 ||
|
|
name != "" && strings.EqualFold(name, syscall.UTF16ToString(mi.Device[:])) {
|
|
return mi, nil
|
|
}
|
|
}
|
|
return monitorInfoExW{}, fmt.Errorf("%w: %q", ErrDisplayNotFound, name)
|
|
}
|
|
|
|
// MoveProcessWindowToDisplay positions a VM display window in virtual-screen coordinates.
|
|
func MoveProcessWindowToDisplay(pid uint32, displayName string, borderless bool) (bool, error) {
|
|
target, err := findDisplayMonitor(displayName)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
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)
|
|
}
|
|
|
|
if iconic, _, _ := procIsIconic.Call(hwnd); iconic != 0 {
|
|
procShowWindow.Call(hwnd, swRestore)
|
|
if remaining, _, _ := procIsIconic.Call(hwnd); remaining != 0 {
|
|
return false, fmt.Errorf("could not restore display window for pid %d", pid)
|
|
}
|
|
}
|
|
style, err := getWindowLong(hwnd, gwlStyle)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
exStyle, err := getWindowLong(hwnd, gwlExStyle)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
rect, monitor, err := getFullscreenGeometry(hwnd, exStyle, false, savedWindowState{})
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
wasFullscreen := isBorderlessFullscreenRect(style, rect, WindowRect(monitor.Monitor))
|
|
if !borderless && !wasFullscreen {
|
|
work := WindowRect(target.Work)
|
|
bounds := computeWindowedTargetRect(rect, work)
|
|
if err := setWindowRect(hwnd, bounds, swpNoZOrder|swpNoOwnerZOrder|swpFrameChanged); err != nil {
|
|
return false, err
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
placement, err := getWindowPlacement(hwnd)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
current := savedWindowState{pid: pid, style: style, exStyle: exStyle, placement: placement, wasFullscreen: wasFullscreen}
|
|
bounds := WindowRect(target.Monitor)
|
|
if err := resizeWindowFrame(hwnd, style&^windowFrameStyles, exStyle&^windowFrameExStyles, bounds); err != nil {
|
|
return false, rollbackWindowState(hwnd, current, err)
|
|
}
|
|
fullscreenState.remember(hwnd, current, bounds)
|
|
markTaskbarFullscreen(hwnd, true)
|
|
return true, nil
|
|
}
|