Files
Reims-Go/internal/host/ime_coalesce.go
T
NwflowerandClaude Opus 5.5 b2237997f2 feat(input): send Chinese, emoji and host-IME text into the VM
- "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>
2026-11-28 10:52:34 -07:00

198 lines
4.0 KiB
Go

package host
import (
"strings"
"sync"
"time"
"unicode/utf16"
)
// IMETarget identifies a running QEMU VM window that should receive remote IME
// / VK_PACKET Unicode text events when focused.
type IMETarget struct {
VMName string
VMDir string
PID uint32
}
type imeCharEvent struct {
target IMETarget
codeUnit uint16
}
// IMECoalescer decodes a stream of Win32 VK_PACKET UTF-16 code units (including
// surrogate pairs for emoji) and groups rapid remote-IME bursts into phrases
// before invoking onFlush.
type IMECoalescer struct {
flushDelay time.Duration
onFlush func(target IMETarget, text string)
ch chan imeCharEvent
stop chan struct{}
done chan struct{}
closeOnce sync.Once
}
const (
defaultIMEFlushDelay = 40 * time.Millisecond
maxIMECoalesceRunes = 128
)
// NewIMECoalescer starts a background coalescer goroutine.
func NewIMECoalescer(flushDelay time.Duration, onFlush func(target IMETarget, text string)) *IMECoalescer {
if flushDelay <= 0 {
flushDelay = defaultIMEFlushDelay
}
c := &IMECoalescer{
flushDelay: flushDelay,
onFlush: onFlush,
ch: make(chan imeCharEvent, 512),
stop: make(chan struct{}),
done: make(chan struct{}),
}
go c.run()
return c
}
// Push enqueues one UTF-16 code unit without blocking the caller (safe to call
// directly from a Windows WH_KEYBOARD_LL hook).
func (c *IMECoalescer) Push(target IMETarget, codeUnit uint16) {
if c == nil || codeUnit == 0 {
return
}
select {
case c.ch <- imeCharEvent{target: target, codeUnit: codeUnit}:
default:
}
}
// Close flushes any pending text and stops the background goroutine.
func (c *IMECoalescer) Close() {
if c == nil {
return
}
c.closeOnce.Do(func() {
close(c.stop)
<-c.done
})
}
func (c *IMECoalescer) run() {
defer close(c.done)
var (
hasTarget bool
curTarget IMETarget
highSurr uint16
buf strings.Builder
runeCount int
timer *time.Timer
timerCh <-chan time.Time
)
stopTimer := func() {
if timer != nil {
if !timer.Stop() {
select {
case <-timer.C:
default:
}
}
timer = nil
timerCh = nil
}
}
resetTimer := func() {
stopTimer()
timer = time.NewTimer(c.flushDelay)
timerCh = timer.C
}
flush := func() {
stopTimer()
highSurr = 0
if !hasTarget || buf.Len() == 0 {
buf.Reset()
runeCount = 0
return
}
text := buf.String()
target := curTarget
buf.Reset()
runeCount = 0
if c.onFlush != nil {
c.onFlush(target, text)
}
}
for {
select {
case <-c.stop:
// Drain any already-queued events before final flush.
for {
select {
case ev := <-c.ch:
c.consume(&hasTarget, &curTarget, &highSurr, &buf, &runeCount, ev, flush)
default:
flush()
return
}
}
case <-timerCh:
flush()
case ev := <-c.ch:
needFlush := c.consume(&hasTarget, &curTarget, &highSurr, &buf, &runeCount, ev, flush)
if needFlush {
flush()
} else if buf.Len() > 0 || highSurr != 0 {
resetTimer()
}
}
}
}
func (c *IMECoalescer) consume(
hasTarget *bool,
curTarget *IMETarget,
highSurr *uint16,
buf *strings.Builder,
runeCount *int,
ev imeCharEvent,
flush func(),
) bool {
if *hasTarget && curTarget.VMName != ev.target.VMName {
flush()
}
*hasTarget = true
*curTarget = ev.target
u := ev.codeUnit
// High surrogate (0xD800..0xDBFF): wait for matching low surrogate.
if u >= 0xD800 && u <= 0xDBFF {
*highSurr = u
return false
}
// Low surrogate (0xDC00..0xDFFF): combine with pending high surrogate.
if u >= 0xDC00 && u <= 0xDFFF {
if *highSurr != 0 {
r := utf16.DecodeRune(rune(*highSurr), rune(u))
*highSurr = 0
if r != 0xFFFD {
buf.WriteRune(r)
*runeCount++
}
}
return *runeCount >= maxIMECoalesceRunes
}
// Normal BMP code unit: clear any orphaned high surrogate.
*highSurr = 0
if u == '\r' {
u = '\n'
}
r := rune(u)
buf.WriteRune(r)
*runeCount++
// Flush immediately on newline or large batch so chat/terminal submits feel instant.
return r == '\n' || *runeCount >= maxIMECoalesceRunes
}