- "Send text to VM..." dialog: arbitrary Unicode goes through Guest Tools (clipboard paste in the guest, agent v3 /v1/text-input long poll); without Guest Tools only US-keyboard ASCII is typed via QMP. - Host IME / dictation: a low-level keyboard hook (internal/host/ime_*) catches VK_PACKET input aimed at a running VM window, coalesces bursts and forwards them in order through a per-VM queue (type_flow.go). - reims-vgpu-window bridge: new runtime patch reims-vgpu-win32-unicode-input enables SDL text input and posts committed text to /v1/host-text; StartVM passes REIMS_GO_INPUT_URL/TOKEN to the window process. This also adds the patch file that patches/series.txt already listed. - Guest Tools auth returns a field snapshot instead of copying the VM entry (it now holds a mutex and text queues). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
212 lines
5.6 KiB
Go
212 lines
5.6 KiB
Go
//go:build windows
|
|
|
|
package host
|
|
|
|
import (
|
|
"fmt"
|
|
"runtime"
|
|
"sync"
|
|
"sync/atomic"
|
|
"syscall"
|
|
"unsafe"
|
|
)
|
|
|
|
var (
|
|
procSetWindowsHookExW = user32.NewProc("SetWindowsHookExW")
|
|
procUnhookWindowsHookEx = user32.NewProc("UnhookWindowsHookEx")
|
|
procCallNextHookEx = user32.NewProc("CallNextHookEx")
|
|
procGetMessageW = user32.NewProc("GetMessageW")
|
|
procPeekMessageW = user32.NewProc("PeekMessageW")
|
|
procPostThreadMessageW = user32.NewProc("PostThreadMessageW")
|
|
procGetForegroundWindow = user32.NewProc("GetForegroundWindow")
|
|
procGetWindowThreadProcessId = user32.NewProc("GetWindowThreadProcessId")
|
|
imeKernel32 = syscall.NewLazyDLL("kernel32.dll")
|
|
procGetModuleHandleW = imeKernel32.NewProc("GetModuleHandleW")
|
|
procGetCurrentThreadId = imeKernel32.NewProc("GetCurrentThreadId")
|
|
)
|
|
|
|
const (
|
|
whKeyboardLL = 13
|
|
hcAction = 0
|
|
wmKeyDown = 0x0100
|
|
wmKeyUp = 0x0101
|
|
wmSysKeyDown = 0x0104
|
|
wmSysKeyUp = 0x0105
|
|
wmQuit = 0x0012
|
|
vkPacket = 0xE7
|
|
pmNoRemove = 0x0000
|
|
)
|
|
|
|
type kbdLLHookStruct struct {
|
|
vkCode uint32
|
|
scanCode uint32
|
|
flags uint32
|
|
time uint32
|
|
dwExtraInfo uintptr
|
|
}
|
|
|
|
type winMsg struct {
|
|
hwnd uintptr
|
|
message uint32
|
|
wParam uintptr
|
|
lParam uintptr
|
|
time uint32
|
|
ptX int32
|
|
ptY int32
|
|
}
|
|
|
|
var (
|
|
activeIMEBridge atomic.Pointer[IMEBridge]
|
|
imeHookCBOnce sync.Once
|
|
imeHookCB uintptr
|
|
)
|
|
|
|
func getIMEHookCallback() uintptr {
|
|
imeHookCBOnce.Do(func() {
|
|
imeHookCB = syscall.NewCallback(imeLowLevelKeyboardProc)
|
|
})
|
|
return imeHookCB
|
|
}
|
|
|
|
// imeLowLevelKeyboardProc intercepts ONLY VK_PACKET (SendInput KEYEVENTF_UNICODE)
|
|
// events targeted at a running Reims-Go QEMU VM window. Physical keyboard events
|
|
// return immediately via CallNextHookEx with zero latency or interference.
|
|
//
|
|
// lParam is declared as the KBDLLHOOKSTRUCT pointer Windows passes, so no
|
|
// uintptr-to-pointer conversion is needed (go vet's unsafeptr check).
|
|
func imeLowLevelKeyboardProc(nCode int32, wParam uintptr, kbd *kbdLLHookStruct) uintptr {
|
|
if nCode == hcAction && kbd != nil && (wParam == wmKeyDown || wParam == wmSysKeyDown || wParam == wmKeyUp || wParam == wmSysKeyUp) {
|
|
if kbd.vkCode == vkPacket {
|
|
if b := activeIMEBridge.Load(); b != nil && b.enabled.Load() {
|
|
if target, ok := b.matchForegroundTarget(); ok {
|
|
if wParam == wmKeyDown || wParam == wmSysKeyDown {
|
|
b.coalescer.Push(target, uint16(kbd.scanCode))
|
|
}
|
|
return 1
|
|
}
|
|
}
|
|
}
|
|
}
|
|
r, _, _ := procCallNextHookEx.Call(0, uintptr(nCode), wParam, uintptr(unsafe.Pointer(kbd)))
|
|
return r
|
|
}
|
|
|
|
// IMEBridge listens for Windows VK_PACKET (remote IME / voice / emoji /
|
|
// SendInput KEYEVENTF_UNICODE) text directed at running QEMU VM windows.
|
|
type IMEBridge struct {
|
|
enabled atomic.Bool
|
|
targets atomic.Pointer[map[uint32]IMETarget]
|
|
coalescer *IMECoalescer
|
|
threadID uint32
|
|
done chan struct{}
|
|
closeOnce sync.Once
|
|
}
|
|
|
|
// StartIMEBridge installs the Windows low-level keyboard hook on a dedicated
|
|
// message-pumping OS thread. Only one active bridge is supported per process.
|
|
func StartIMEBridge(onText func(target IMETarget, text string)) (*IMEBridge, error) {
|
|
b := &IMEBridge{
|
|
coalescer: NewIMECoalescer(defaultIMEFlushDelay, onText),
|
|
done: make(chan struct{}),
|
|
}
|
|
b.enabled.Store(true)
|
|
empty := make(map[uint32]IMETarget)
|
|
b.targets.Store(&empty)
|
|
activeIMEBridge.Store(b)
|
|
|
|
ready := make(chan error, 1)
|
|
go b.hookThread(ready)
|
|
if err := <-ready; err != nil {
|
|
activeIMEBridge.CompareAndSwap(b, nil)
|
|
b.coalescer.Close()
|
|
return nil, err
|
|
}
|
|
return b, nil
|
|
}
|
|
|
|
func (b *IMEBridge) hookThread(ready chan<- error) {
|
|
runtime.LockOSThread()
|
|
defer runtime.UnlockOSThread()
|
|
defer close(b.done)
|
|
|
|
tid, _, _ := procGetCurrentThreadId.Call()
|
|
b.threadID = uint32(tid)
|
|
|
|
// Force creation of this thread's message queue before signalling ready so
|
|
// PostThreadMessageW(WM_QUIT) from Close() can never race.
|
|
var msg winMsg
|
|
_, _, _ = procPeekMessageW.Call(uintptr(unsafe.Pointer(&msg)), 0, 0, 0, pmNoRemove)
|
|
|
|
hMod, _, _ := procGetModuleHandleW.Call(0)
|
|
hhook, _, err := procSetWindowsHookExW.Call(whKeyboardLL, getIMEHookCallback(), hMod, 0)
|
|
if hhook == 0 {
|
|
ready <- fmt.Errorf("SetWindowsHookExW(WH_KEYBOARD_LL): %w", err)
|
|
return
|
|
}
|
|
defer procUnhookWindowsHookEx.Call(hhook)
|
|
ready <- nil
|
|
|
|
for {
|
|
ret, _, _ := procGetMessageW.Call(uintptr(unsafe.Pointer(&msg)), 0, 0, 0)
|
|
if int32(ret) <= 0 {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
// SetTargets atomically replaces the map of active QEMU VM PIDs.
|
|
func (b *IMEBridge) SetTargets(targets []IMETarget) {
|
|
if b == nil {
|
|
return
|
|
}
|
|
m := make(map[uint32]IMETarget, len(targets))
|
|
for _, t := range targets {
|
|
if t.PID > 0 {
|
|
m[t.PID] = t
|
|
}
|
|
}
|
|
b.targets.Store(&m)
|
|
}
|
|
|
|
// SetEnabled toggles interception without uninstalling the hook.
|
|
func (b *IMEBridge) SetEnabled(enabled bool) {
|
|
if b == nil {
|
|
return
|
|
}
|
|
b.enabled.Store(enabled)
|
|
}
|
|
|
|
func (b *IMEBridge) matchForegroundTarget() (IMETarget, bool) {
|
|
m := b.targets.Load()
|
|
if m == nil || len(*m) == 0 {
|
|
return IMETarget{}, false
|
|
}
|
|
hwnd, _, _ := procGetForegroundWindow.Call()
|
|
if hwnd == 0 {
|
|
return IMETarget{}, false
|
|
}
|
|
var pid uint32
|
|
_, _, _ = procGetWindowThreadProcessId.Call(hwnd, uintptr(unsafe.Pointer(&pid)))
|
|
if pid == 0 {
|
|
return IMETarget{}, false
|
|
}
|
|
t, ok := (*m)[pid]
|
|
return t, ok
|
|
}
|
|
|
|
// Close removes the Windows keyboard hook and shuts down the coalescer cleanly.
|
|
func (b *IMEBridge) Close() {
|
|
if b == nil {
|
|
return
|
|
}
|
|
b.closeOnce.Do(func() {
|
|
b.enabled.Store(false)
|
|
activeIMEBridge.CompareAndSwap(b, nil)
|
|
if b.threadID != 0 {
|
|
_, _, _ = procPostThreadMessageW.Call(uintptr(b.threadID), wmQuit, 0, 0)
|
|
}
|
|
<-b.done
|
|
b.coalescer.Close()
|
|
})
|
|
}
|