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>
140 lines
3.4 KiB
Go
140 lines
3.4 KiB
Go
//go:build windows
|
|
|
|
package host
|
|
|
|
import (
|
|
"runtime"
|
|
"sync"
|
|
"syscall"
|
|
"unsafe"
|
|
)
|
|
|
|
var (
|
|
procEnumWindows = user32.NewProc("EnumWindows")
|
|
procIsWindowVisible = user32.NewProc("IsWindowVisible")
|
|
procIsIconic = user32.NewProc("IsIconic")
|
|
procShowWindow = user32.NewProc("ShowWindow")
|
|
procSetForegroundWin = user32.NewProc("SetForegroundWindow")
|
|
procBringWindowToTop = user32.NewProc("BringWindowToTop")
|
|
procAttachThreadInput = user32.NewProc("AttachThreadInput")
|
|
)
|
|
|
|
const swRestore = 9
|
|
|
|
// ForegroundPID returns the process that owns the foreground window, or 0
|
|
// when there is none (locked screen, desktop focused).
|
|
func ForegroundPID() uint32 {
|
|
hwnd, _, _ := procGetForegroundWindow.Call()
|
|
if hwnd == 0 {
|
|
return 0
|
|
}
|
|
return windowPID(hwnd)
|
|
}
|
|
|
|
func windowPID(hwnd uintptr) uint32 {
|
|
var pid uint32
|
|
_, _, _ = procGetWindowThreadProcessId.Call(hwnd, uintptr(unsafe.Pointer(&pid)))
|
|
return pid
|
|
}
|
|
|
|
var (
|
|
focusEnumMu sync.Mutex
|
|
focusEnumOnce sync.Once
|
|
focusEnumCB uintptr
|
|
focusEnumTargets map[uint32]bool
|
|
focusEnumFound uintptr
|
|
)
|
|
|
|
// ForegroundInPIDs reports whether the foreground window belongs to one of pids.
|
|
func ForegroundInPIDs(pids []uint32) bool {
|
|
fg := ForegroundPID()
|
|
if fg == 0 {
|
|
return false
|
|
}
|
|
for _, pid := range pids {
|
|
if pid == fg {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func enumFocusWindowProc(hwnd uintptr, _ uintptr) uintptr {
|
|
vis, _, _ := procIsWindowVisible.Call(hwnd)
|
|
if vis == 0 {
|
|
return 1
|
|
}
|
|
var winPID uint32
|
|
_, _, _ = procGetWindowThreadProcessId.Call(hwnd, uintptr(unsafe.Pointer(&winPID)))
|
|
if focusEnumTargets[winPID] {
|
|
focusEnumFound = hwnd
|
|
return 0
|
|
}
|
|
return 1
|
|
}
|
|
|
|
// FocusProcessTreeWindow does the same for a launcher and the child processes
|
|
// it started: the first visible top-level window of any of pids is focused.
|
|
func FocusProcessTreeWindow(pids []uint32) bool {
|
|
targets := make(map[uint32]bool, len(pids))
|
|
for _, pid := range pids {
|
|
if pid != 0 {
|
|
targets[pid] = true
|
|
}
|
|
}
|
|
if len(targets) == 0 {
|
|
return false
|
|
}
|
|
focusEnumOnce.Do(func() {
|
|
focusEnumCB = syscall.NewCallback(enumFocusWindowProc)
|
|
})
|
|
focusEnumMu.Lock()
|
|
focusEnumTargets = targets
|
|
focusEnumFound = 0
|
|
_, _, _ = procEnumWindows.Call(focusEnumCB, 0)
|
|
found := focusEnumFound
|
|
focusEnumTargets = nil
|
|
focusEnumMu.Unlock()
|
|
if found == 0 {
|
|
return false
|
|
}
|
|
iconic, _, _ := procIsIconic.Call(found)
|
|
if iconic != 0 {
|
|
_, _, _ = procShowWindow.Call(found, swRestore)
|
|
}
|
|
focusWindow(found)
|
|
return true
|
|
}
|
|
|
|
func focusWindow(hwnd uintptr) {
|
|
runtime.LockOSThread()
|
|
defer runtime.UnlockOSThread()
|
|
fgHwnd, _, _ := procGetForegroundWindow.Call()
|
|
curTid, _, _ := procGetCurrentThreadId.Call()
|
|
var fgTid uintptr
|
|
if fgHwnd != 0 {
|
|
fgTid, _, _ = procGetWindowThreadProcessId.Call(fgHwnd, 0)
|
|
}
|
|
targetTid, _, _ := procGetWindowThreadProcessId.Call(hwnd, 0)
|
|
attachedFg := false
|
|
attachedTarget := false
|
|
if fgTid != 0 && fgTid != curTid {
|
|
if r, _, _ := procAttachThreadInput.Call(curTid, fgTid, 1); r != 0 {
|
|
attachedFg = true
|
|
}
|
|
}
|
|
if targetTid != 0 && targetTid != curTid && targetTid != fgTid {
|
|
if r, _, _ := procAttachThreadInput.Call(curTid, targetTid, 1); r != 0 {
|
|
attachedTarget = true
|
|
}
|
|
}
|
|
_, _, _ = procBringWindowToTop.Call(hwnd)
|
|
_, _, _ = procSetForegroundWin.Call(hwnd)
|
|
if attachedTarget {
|
|
_, _, _ = procAttachThreadInput.Call(curTid, targetTid, 0)
|
|
}
|
|
if attachedFg {
|
|
_, _, _ = procAttachThreadInput.Call(curTid, fgTid, 0)
|
|
}
|
|
}
|