- Add per-VM `capture_host_shortcuts` option persisted in `.reims-go-keymap.json` (independent from modifier preset and backward-compatible with legacy configs and Wails callers omitting the field). When enabled, unmapped Win and Alt keys route through the existing Guest Tools v8 hidutil carrier (`lcmd`/`rcmd`/`lopt`/ `ropt`) while the VM is focused, preventing host Win/Alt shortcuts (e.g. PixPin Alt+A, Alt+Tab, Alt+F4, Ctrl+Alt+A, Win+...) without modifying explicit custom bindings or bumping Guest Tools version. - Monitor `EVENT_SYSTEM_FOREGROUND` and `EVENT_OBJECT_NAMECHANGE` on the Windows keymap hook thread to immediately release held modifiers on focus loss and promote `WH_KEYBOARD_LL` back to the top of the hook chain on VM focus entry and SDL3 in-window re-capture, with per-key timestamp filtering for delayed focus-loss events, swap-window event deduplication, Win64 32-bit parameter narrowing, and fallback timer recovery. - Serialize `SetVMKeymap` read-merge-save under `keymapMu`, expose the toggle in Settings with zh-CN/en-US i18n, and add Go + frontend unit tests.
158 lines
5.2 KiB
Go
158 lines
5.2 KiB
Go
package app
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/Nwflower/reims-go/internal/driver"
|
|
"github.com/Nwflower/reims-go/internal/guesttools"
|
|
"github.com/Nwflower/reims-go/internal/host"
|
|
"github.com/Nwflower/reims-go/internal/vmstart"
|
|
)
|
|
|
|
const KeymapRequiredAgentVersion = guesttools.KeymapAgentVersion
|
|
const keymapStatusMaxAge = 30 * time.Second
|
|
|
|
type VMKeymapUpdate struct {
|
|
Preset string `json:"preset"`
|
|
Bindings map[string]string `json:"bindings"`
|
|
CaptureHostShortcuts *bool `json:"capture_host_shortcuts,omitempty"`
|
|
}
|
|
|
|
type VMKeymapStatus struct {
|
|
Preset string `json:"preset"`
|
|
Bindings map[string]string `json:"bindings"`
|
|
CaptureHostShortcuts bool `json:"capture_host_shortcuts"`
|
|
NeedsGuestTools bool `json:"needs_guest_tools"`
|
|
GuestConnected bool `json:"guest_connected"`
|
|
GuestAgentVersion int `json:"guest_agent_version,omitempty"`
|
|
RequiredAgentVersion int `json:"required_agent_version"`
|
|
GuestKeymapActive bool `json:"guest_keymap_active"`
|
|
InputServiceAvailable bool `json:"input_service_available"`
|
|
HookActive bool `json:"hook_active"`
|
|
Running bool `json:"running"`
|
|
}
|
|
|
|
func (a *App) GetVMKeymap(name string) (VMKeymapStatus, error) {
|
|
if vm, ok := a.polledVM(name); ok && vm.Dir != "" {
|
|
return a.keymapStatus(name, vm.State == "running", vmstart.ReadVMKeymap(vm.Dir)), nil
|
|
}
|
|
detail, err := a.GetVMDetail(name)
|
|
if err != nil {
|
|
return VMKeymapStatus{}, err
|
|
}
|
|
return a.keymapStatus(name, detail.State == "running", vmstart.ReadVMKeymap(detail.Directory)), nil
|
|
}
|
|
|
|
func (a *App) SetVMKeymap(name string, req VMKeymapUpdate) (VMKeymapStatus, error) {
|
|
detail, err := a.GetVMDetail(name)
|
|
if err != nil {
|
|
return VMKeymapStatus{}, err
|
|
}
|
|
a.keymapMu.Lock()
|
|
capture := vmstart.ReadVMKeymap(detail.Directory).CaptureHostShortcuts
|
|
if req.CaptureHostShortcuts != nil {
|
|
capture = *req.CaptureHostShortcuts
|
|
}
|
|
cfg := vmstart.VMKeymapConfig{
|
|
Preset: vmstart.KeymapPreset(req.Preset),
|
|
Bindings: make(map[vmstart.HostModifier]vmstart.GuestModifier, len(req.Bindings)),
|
|
CaptureHostShortcuts: capture,
|
|
}
|
|
for k, v := range req.Bindings {
|
|
cfg.Bindings[vmstart.HostModifier(k)] = vmstart.GuestModifier(v)
|
|
}
|
|
cfg, err = vmstart.SaveVMKeymap(detail.Directory, cfg)
|
|
if err != nil {
|
|
a.keymapMu.Unlock()
|
|
return VMKeymapStatus{}, err
|
|
}
|
|
a.syncKeymapRulesLocked()
|
|
a.keymapMu.Unlock()
|
|
return a.keymapStatus(name, detail.State == "running", cfg), nil
|
|
}
|
|
|
|
func (a *App) keymapStatus(name string, running bool, cfg vmstart.VMKeymapConfig) VMKeymapStatus {
|
|
status := VMKeymapStatus{
|
|
Preset: string(cfg.Preset),
|
|
Bindings: keymapBindingStrings(vmstart.ResolveKeymapBindings(cfg)),
|
|
CaptureHostShortcuts: cfg.CaptureHostShortcuts,
|
|
NeedsGuestTools: vmstart.NeedsGuestCarrier(vmstart.EffectiveKeymapBindings(cfg)),
|
|
RequiredAgentVersion: KeymapRequiredAgentVersion,
|
|
Running: running,
|
|
}
|
|
a.mu.Lock()
|
|
srv := a.guestTools
|
|
a.mu.Unlock()
|
|
if srv != nil && running {
|
|
guest := srv.Status(name, keymapStatusMaxAge)
|
|
status.GuestConnected = guest.Connected
|
|
status.GuestAgentVersion = guest.AgentVersion
|
|
status.GuestKeymapActive = guest.KeymapActive && guest.AgentVersion >= KeymapRequiredAgentVersion
|
|
}
|
|
svc := a.getInputService()
|
|
status.InputServiceAvailable = svc != nil
|
|
if svc != nil && running {
|
|
status.HookActive = svc.IsKeymapActiveForVM(name)
|
|
}
|
|
return status
|
|
}
|
|
|
|
func keymapBindingStrings(bindings map[vmstart.HostModifier]vmstart.GuestModifier) map[string]string {
|
|
out := make(map[string]string, len(bindings))
|
|
for k, v := range bindings {
|
|
out[string(k)] = string(v)
|
|
}
|
|
return out
|
|
}
|
|
|
|
func (a *App) syncKeymapRules() {
|
|
a.keymapMu.Lock()
|
|
defer a.keymapMu.Unlock()
|
|
a.syncKeymapRulesLocked()
|
|
}
|
|
|
|
func (a *App) syncKeymapRulesLocked() {
|
|
a.mu.Lock()
|
|
vms := append([]driver.VMListItem(nil), a.lastVMs...)
|
|
srv := a.guestTools
|
|
a.mu.Unlock()
|
|
svc := a.getInputService()
|
|
var rules []host.KeymapRule
|
|
var targets []host.IMETarget
|
|
pids := make(map[string]int)
|
|
for _, vm := range vms {
|
|
if vm.State != "running" || vm.PID == nil || *vm.PID <= 0 {
|
|
if srv != nil {
|
|
srv.SetKeymapCarrier(vm.Name, false)
|
|
}
|
|
continue
|
|
}
|
|
dir := vm.Dir
|
|
if dir == "" {
|
|
if detail, err := a.GetVMDetail(vm.Name); err == nil {
|
|
dir = detail.Directory
|
|
}
|
|
}
|
|
targets = append(targets, host.IMETarget{VMName: vm.Name, VMDir: dir, PID: uint32(*vm.PID)})
|
|
pids[vm.Name] = *vm.PID
|
|
cfg := vmstart.ReadVMKeymap(dir)
|
|
effective := vmstart.EffectiveKeymapBindings(cfg)
|
|
rule := host.KeymapRule{VMName: vm.Name, Bindings: keymapBindingStrings(effective)}
|
|
if srv != nil {
|
|
if oldPID := a.keymapPIDs[vm.Name]; oldPID != 0 && oldPID != *vm.PID {
|
|
srv.SetKeymapCarrier(vm.Name, false)
|
|
}
|
|
srv.SetKeymapCarrier(vm.Name, svc != nil && vmstart.NeedsGuestCarrier(effective))
|
|
status := srv.Status(vm.Name, keymapStatusMaxAge)
|
|
rule.CarrierReady = status.Connected && status.KeymapActive && status.AgentVersion >= KeymapRequiredAgentVersion
|
|
}
|
|
if len(effective) != 0 {
|
|
rules = append(rules, rule)
|
|
}
|
|
}
|
|
a.keymapPIDs = pids
|
|
if svc != nil {
|
|
svc.SetTargetsAndKeymapRules(targets, rules)
|
|
}
|
|
}
|