- 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.
176 lines
4.9 KiB
Go
176 lines
4.9 KiB
Go
package vmstart
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
type KeymapPreset string
|
|
type HostModifier string
|
|
type GuestModifier string
|
|
|
|
const (
|
|
KeymapOff KeymapPreset = "off"
|
|
KeymapWinToCmd KeymapPreset = "win_to_cmd"
|
|
KeymapSwapAltWin KeymapPreset = "swap_alt_win"
|
|
KeymapCustom KeymapPreset = "custom"
|
|
keymapConfigFile = ".reims-go-keymap.json"
|
|
)
|
|
|
|
type VMKeymapConfig struct {
|
|
Preset KeymapPreset `json:"preset"`
|
|
Bindings map[HostModifier]GuestModifier `json:"bindings,omitempty"`
|
|
CaptureHostShortcuts bool `json:"capture_host_shortcuts,omitempty"`
|
|
}
|
|
|
|
func DefaultVMKeymapConfig() VMKeymapConfig { return VMKeymapConfig{Preset: KeymapOff} }
|
|
|
|
func NormalizeKeymapPreset(p KeymapPreset) KeymapPreset {
|
|
switch p = KeymapPreset(normalizeKeymapValue(string(p))); p {
|
|
case KeymapWinToCmd, KeymapSwapAltWin, KeymapCustom:
|
|
return p
|
|
default:
|
|
return KeymapOff
|
|
}
|
|
}
|
|
|
|
func normalizeKeymapValue(s string) string { return strings.ToLower(strings.TrimSpace(s)) }
|
|
|
|
func PresetBindings(p KeymapPreset) map[HostModifier]GuestModifier {
|
|
switch NormalizeKeymapPreset(p) {
|
|
case KeymapWinToCmd:
|
|
return map[HostModifier]GuestModifier{"lwin": "lcmd", "rwin": "rcmd"}
|
|
case KeymapSwapAltWin:
|
|
return map[HostModifier]GuestModifier{"lalt": "lcmd", "ralt": "rcmd", "lwin": "lopt", "rwin": "ropt"}
|
|
default:
|
|
return map[HostModifier]GuestModifier{}
|
|
}
|
|
}
|
|
|
|
func validHostModifier(k HostModifier) bool {
|
|
switch k {
|
|
case "lwin", "rwin", "lalt", "ralt", "lctrl", "rctrl", "capslock":
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func validGuestModifier(k GuestModifier) bool {
|
|
switch k {
|
|
case "", "default", "lcmd", "rcmd", "lopt", "ropt", "lctrl", "rctrl", "esc", "disabled":
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func ResolveKeymapBindings(cfg VMKeymapConfig) map[HostModifier]GuestModifier {
|
|
bindings := PresetBindings(cfg.Preset)
|
|
if NormalizeKeymapPreset(cfg.Preset) != KeymapCustom {
|
|
return bindings
|
|
}
|
|
for rawHost, rawGuest := range cfg.Bindings {
|
|
k := HostModifier(normalizeKeymapValue(string(rawHost)))
|
|
v := GuestModifier(normalizeKeymapValue(string(rawGuest)))
|
|
if validHostModifier(k) && validGuestModifier(v) && v != "" && v != "default" && string(k) != string(v) {
|
|
bindings[k] = v
|
|
}
|
|
}
|
|
return bindings
|
|
}
|
|
|
|
func EffectiveKeymapBindings(cfg VMKeymapConfig) map[HostModifier]GuestModifier {
|
|
bindings := ResolveKeymapBindings(cfg)
|
|
if !cfg.CaptureHostShortcuts {
|
|
return bindings
|
|
}
|
|
for host, guest := range map[HostModifier]GuestModifier{
|
|
"lwin": "lcmd", "rwin": "rcmd", "lalt": "lopt", "ralt": "ropt",
|
|
} {
|
|
if _, bound := bindings[host]; !bound {
|
|
bindings[host] = guest
|
|
}
|
|
}
|
|
return bindings
|
|
}
|
|
|
|
func NeedsGuestCarrier(bindings map[HostModifier]GuestModifier) bool {
|
|
for _, v := range bindings {
|
|
switch v {
|
|
case "lcmd", "rcmd", "lopt", "ropt":
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func ReadVMKeymap(vmDir string) VMKeymapConfig {
|
|
if strings.TrimSpace(vmDir) == "" {
|
|
return DefaultVMKeymapConfig()
|
|
}
|
|
data, err := os.ReadFile(filepath.Join(vmDir, keymapConfigFile))
|
|
var cfg VMKeymapConfig
|
|
if err != nil || json.Unmarshal(data, &cfg) != nil {
|
|
return DefaultVMKeymapConfig()
|
|
}
|
|
cfg.Preset = NormalizeKeymapPreset(cfg.Preset)
|
|
if cfg.Preset == KeymapCustom {
|
|
cfg.Bindings = ResolveKeymapBindings(cfg)
|
|
} else {
|
|
cfg.Bindings = nil
|
|
}
|
|
return cfg
|
|
}
|
|
|
|
func SaveVMKeymap(vmDir string, cfg VMKeymapConfig) (VMKeymapConfig, error) {
|
|
if strings.TrimSpace(vmDir) == "" {
|
|
return VMKeymapConfig{}, errors.New("VM directory is empty")
|
|
}
|
|
cfg.Preset = KeymapPreset(normalizeKeymapValue(string(cfg.Preset)))
|
|
if cfg.Preset != KeymapOff && NormalizeKeymapPreset(cfg.Preset) == KeymapOff {
|
|
return VMKeymapConfig{}, fmt.Errorf("invalid keymap preset %q", cfg.Preset)
|
|
}
|
|
seen := make(map[HostModifier]bool, len(cfg.Bindings))
|
|
for rawHost, rawGuest := range cfg.Bindings {
|
|
k := HostModifier(normalizeKeymapValue(string(rawHost)))
|
|
v := GuestModifier(normalizeKeymapValue(string(rawGuest)))
|
|
if !validHostModifier(k) || !validGuestModifier(v) || seen[k] {
|
|
return VMKeymapConfig{}, fmt.Errorf("invalid keymap binding %q → %q", rawHost, rawGuest)
|
|
}
|
|
seen[k] = true
|
|
}
|
|
dst := filepath.Join(vmDir, keymapConfigFile)
|
|
if cfg.Preset == KeymapOff && !cfg.CaptureHostShortcuts {
|
|
err := os.Remove(dst)
|
|
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
|
return VMKeymapConfig{}, err
|
|
}
|
|
return DefaultVMKeymapConfig(), nil
|
|
}
|
|
if cfg.Preset == KeymapCustom {
|
|
cfg.Bindings = ResolveKeymapBindings(cfg)
|
|
} else {
|
|
cfg.Bindings = nil
|
|
}
|
|
data, err := json.MarshalIndent(cfg, "", " ")
|
|
if err != nil {
|
|
return VMKeymapConfig{}, err
|
|
}
|
|
tmp, err := os.CreateTemp(vmDir, keymapConfigFile+".*")
|
|
if err != nil {
|
|
return VMKeymapConfig{}, err
|
|
}
|
|
defer os.Remove(tmp.Name())
|
|
_, writeErr := tmp.Write(data)
|
|
if err := errors.Join(writeErr, tmp.Close()); err != nil {
|
|
return VMKeymapConfig{}, err
|
|
}
|
|
if err := os.Rename(tmp.Name(), dst); err != nil {
|
|
return VMKeymapConfig{}, err
|
|
}
|
|
return cfg, nil
|
|
}
|