317 lines
8.0 KiB
Go
317 lines
8.0 KiB
Go
package host
|
|
|
|
import (
|
|
"maps"
|
|
"reflect"
|
|
"strings"
|
|
"sync"
|
|
"sync/atomic"
|
|
)
|
|
|
|
// InputFilterRule is a host-evaluated declarative input filter contributed by
|
|
// L1 plugins. No plugin code ever runs inside the low-level Win32 hook callback.
|
|
type InputFilterRule struct {
|
|
ID string `json:"id"`
|
|
PluginID string `json:"plugin_id"`
|
|
Event string `json:"event"` // "mouse_wheel"
|
|
Axis string `json:"axis"` // "vertical" | "horizontal" | "both"
|
|
Transform string `json:"transform"` // "invert"
|
|
Scope string `json:"scope"` // "vm_foreground"
|
|
VMNames map[string]bool `json:"vm_names,omitempty"` // empty = all VMs; non-empty = targeted VMs
|
|
Vertical bool `json:"vertical"`
|
|
Horizontal bool `json:"horizontal"`
|
|
}
|
|
|
|
// EvalWheelFilter evaluates declarative wheel-invert rules for vmName and axis.
|
|
// Pure Go function so rule evaluation is deterministically unit-tested on every OS.
|
|
func EvalWheelFilter(rules []InputFilterRule, vmName string, horizontal bool, delta int16) (int16, bool) {
|
|
if delta == 0 || len(rules) == 0 {
|
|
return delta, false
|
|
}
|
|
for _, r := range rules {
|
|
if !strings.EqualFold(r.Event, "mouse_wheel") || !strings.EqualFold(r.Transform, "invert") {
|
|
continue
|
|
}
|
|
if len(r.VMNames) > 0 && !r.VMNames[vmName] {
|
|
continue
|
|
}
|
|
axis := strings.ToLower(strings.TrimSpace(r.Axis))
|
|
matchAxis := false
|
|
if horizontal {
|
|
matchAxis = r.Horizontal || axis == "horizontal" || axis == "both"
|
|
} else {
|
|
matchAxis = r.Vertical || axis == "vertical" || axis == "both" || (axis == "" && !r.Horizontal)
|
|
}
|
|
if matchAxis {
|
|
// Guard against int16(-32768) overflow on negation.
|
|
if delta == -32768 {
|
|
return 32767, true
|
|
}
|
|
return -delta, true
|
|
}
|
|
}
|
|
return delta, false
|
|
}
|
|
|
|
// hasMatchingWheelTarget returns true when at least one running target VM is
|
|
// matched by at least one active mouse_wheel filter rule (§5.4 on-demand hook).
|
|
func hasMatchingWheelTarget(rules []InputFilterRule, targets []IMETarget) bool {
|
|
if len(rules) == 0 || len(targets) == 0 {
|
|
return false
|
|
}
|
|
for _, t := range targets {
|
|
if t.PID == 0 {
|
|
continue
|
|
}
|
|
for _, r := range rules {
|
|
if !strings.EqualFold(r.Event, "mouse_wheel") || !strings.EqualFold(r.Transform, "invert") {
|
|
continue
|
|
}
|
|
if !r.Vertical && !r.Horizontal && r.Axis == "" {
|
|
continue
|
|
}
|
|
if len(r.VMNames) == 0 || r.VMNames[t.VMName] {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// InputService unifies low-level keyboard (IME bridge) and on-demand low-level
|
|
// mouse (declarative inputFilters) hooks behind one reversible host service (§5.4).
|
|
type InputService struct {
|
|
mu sync.Mutex
|
|
onIMEText func(target IMETarget, text string)
|
|
imeEnabled bool
|
|
imeBridge *IMEBridge
|
|
targets []IMETarget
|
|
targetByPID atomic.Pointer[map[uint32]IMETarget]
|
|
rules atomic.Pointer[[]InputFilterRule]
|
|
mouseHook *mouseWheelHook
|
|
keymapRules []KeymapRule
|
|
keymapState atomic.Pointer[keymapSnapshot]
|
|
keymapHook *keymapHook
|
|
closed bool
|
|
}
|
|
|
|
// NewInputService constructs an inactive InputService. No OS hooks are installed
|
|
// until EnableIME or SetFilterRules + matching targets require them (§8.1).
|
|
func NewInputService(onIMEText func(target IMETarget, text string)) *InputService {
|
|
s := &InputService{
|
|
onIMEText: onIMEText,
|
|
}
|
|
emptyTargets := make(map[uint32]IMETarget)
|
|
s.targetByPID.Store(&emptyTargets)
|
|
emptyRules := []InputFilterRule{}
|
|
s.rules.Store(&emptyRules)
|
|
return s
|
|
}
|
|
|
|
// EnableIME starts or stops the low-level keyboard hook for remote IME bridging.
|
|
func (s *InputService) EnableIME(enabled bool) error {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
if s.closed {
|
|
return nil
|
|
}
|
|
s.imeEnabled = enabled
|
|
if enabled {
|
|
if s.imeBridge == nil {
|
|
b, err := StartIMEBridge(s.onIMEText)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
b.SetTargets(s.targets)
|
|
s.imeBridge = b
|
|
}
|
|
return nil
|
|
}
|
|
if s.imeBridge != nil {
|
|
s.imeBridge.Close()
|
|
s.imeBridge = nil
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// SetTargets updates the active running VM PIDs and reconciles the on-demand
|
|
// mouse hook so it is installed ONLY while a matching VM is running (§5.4).
|
|
func (s *InputService) SetTargets(targets []IMETarget) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
if s.closed {
|
|
return
|
|
}
|
|
s.setTargetsLocked(targets)
|
|
s.reconcileKeymapLocked()
|
|
}
|
|
|
|
func (s *InputService) setTargetsLocked(targets []IMETarget) {
|
|
cp := append([]IMETarget(nil), targets...)
|
|
s.targets = cp
|
|
m := make(map[uint32]IMETarget, len(cp))
|
|
for _, t := range cp {
|
|
if t.PID > 0 {
|
|
m[t.PID] = t
|
|
}
|
|
}
|
|
s.targetByPID.Store(&m)
|
|
if s.imeBridge != nil {
|
|
s.imeBridge.SetTargets(cp)
|
|
}
|
|
s.reconcileMouseHookLocked()
|
|
}
|
|
|
|
type keymapSnapshot struct {
|
|
rules []KeymapRule
|
|
targets map[uint32]string
|
|
}
|
|
|
|
func (s *InputService) SetKeymapRules(rules []KeymapRule) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
if s.closed {
|
|
return
|
|
}
|
|
s.setKeymapRulesLocked(rules)
|
|
s.reconcileKeymapLocked()
|
|
}
|
|
|
|
func (s *InputService) SetTargetsAndKeymapRules(targets []IMETarget, rules []KeymapRule) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
if s.closed {
|
|
return
|
|
}
|
|
s.setTargetsLocked(targets)
|
|
s.setKeymapRulesLocked(rules)
|
|
s.reconcileKeymapLocked()
|
|
}
|
|
|
|
func (s *InputService) setKeymapRulesLocked(rules []KeymapRule) {
|
|
s.keymapRules = make([]KeymapRule, len(rules))
|
|
for i, rule := range rules {
|
|
rule.Bindings = maps.Clone(rule.Bindings)
|
|
s.keymapRules[i] = rule
|
|
}
|
|
}
|
|
|
|
func (s *InputService) IsKeymapHookInstalled() bool {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
return s.keymapHook != nil
|
|
}
|
|
|
|
func (s *InputService) IsKeymapActiveForVM(vmName string) bool {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
if s.keymapHook == nil {
|
|
return false
|
|
}
|
|
for _, name := range s.keymapState.Load().targets {
|
|
if name == vmName {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (s *InputService) reconcileKeymapLocked() {
|
|
active := make(map[string]bool)
|
|
for _, rule := range s.keymapRules {
|
|
active[rule.VMName] = rule.active()
|
|
}
|
|
next := &keymapSnapshot{rules: s.keymapRules, targets: make(map[uint32]string)}
|
|
for _, target := range s.targets {
|
|
if target.PID != 0 && active[target.VMName] {
|
|
next.targets[target.PID] = target.VMName
|
|
}
|
|
}
|
|
if !reflect.DeepEqual(next, s.keymapState.Load()) {
|
|
s.keymapState.Store(next)
|
|
if s.keymapHook != nil {
|
|
s.keymapHook.refresh()
|
|
}
|
|
}
|
|
if len(next.targets) == 0 && s.keymapHook != nil {
|
|
s.keymapHook.Close()
|
|
s.keymapHook = nil
|
|
} else if len(next.targets) != 0 && s.keymapHook == nil {
|
|
s.keymapHook = startKeymapHook(s)
|
|
if s.keymapHook != nil {
|
|
prioritizeFullscreenHotkeyHook()
|
|
}
|
|
}
|
|
}
|
|
|
|
// SetFilterRules replaces the active declarative input filters and installs or
|
|
// uninstalls the low-level mouse hook on demand (§5.4).
|
|
func (s *InputService) SetFilterRules(rules []InputFilterRule) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
if s.closed {
|
|
return
|
|
}
|
|
cp := append([]InputFilterRule(nil), rules...)
|
|
s.rules.Store(&cp)
|
|
s.reconcileMouseHookLocked()
|
|
}
|
|
|
|
// IsKeyboardHookInstalled reports whether the IME keyboard hook is active.
|
|
func (s *InputService) IsKeyboardHookInstalled() bool {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
return s.imeBridge != nil
|
|
}
|
|
|
|
// IsMouseHookInstalled reports whether the on-demand mouse wheel hook is active.
|
|
func (s *InputService) IsMouseHookInstalled() bool {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
return s.mouseHook != nil
|
|
}
|
|
|
|
func (s *InputService) reconcileMouseHookLocked() {
|
|
rulesPtr := s.rules.Load()
|
|
var rules []InputFilterRule
|
|
if rulesPtr != nil {
|
|
rules = *rulesPtr
|
|
}
|
|
need := hasMatchingWheelTarget(rules, s.targets)
|
|
if need && s.mouseHook == nil {
|
|
if mh, err := startMouseWheelHook(s); err == nil {
|
|
s.mouseHook = mh
|
|
}
|
|
} else if !need && s.mouseHook != nil {
|
|
s.mouseHook.Close()
|
|
s.mouseHook = nil
|
|
}
|
|
}
|
|
|
|
// Close tears down both keyboard and mouse hooks cleanly with zero residue.
|
|
func (s *InputService) Close() {
|
|
s.mu.Lock()
|
|
if s.closed {
|
|
s.mu.Unlock()
|
|
return
|
|
}
|
|
s.closed = true
|
|
ib := s.imeBridge
|
|
mh := s.mouseHook
|
|
kh := s.keymapHook
|
|
s.keymapHook = nil
|
|
s.imeBridge = nil
|
|
s.mouseHook = nil
|
|
s.mu.Unlock()
|
|
|
|
if ib != nil {
|
|
ib.Close()
|
|
}
|
|
if mh != nil {
|
|
mh.Close()
|
|
}
|
|
if kh != nil {
|
|
kh.Close()
|
|
}
|
|
}
|