fix(input,runtime): prevent physical keystroke duplication on Win32 unicode bridge
This commit is contained in:
1 parent
2b8ea23074
commit
9bd4ac67ef
9 files changed
+149
-22
No files matched your search
+4
-2
@@ -1400,8 +1400,10 @@ func (a *App) StartVM(name string) error {
|
||||
}
|
||||
} else {
|
||||
a.syncGuestTools(c, name, &curDetail)
|
||||
if env := a.inputBridgeEnv(name, curDetail.Directory); len(env) > 0 {
|
||||
c.Env = append(c.Env, env...)
|
||||
if prep.Runtime.Manifest.HasCapability("win32-unicode-input") {
|
||||
if env := a.inputBridgeEnv(name, curDetail.Directory); len(env) > 0 {
|
||||
c.Env = append(c.Env, env...)
|
||||
}
|
||||
}
|
||||
prep.Prepare(context.Background(), name, rep, &curDetail)
|
||||
if rep.NeedsColdRebootWorkaround {
|
||||
|
||||
@@ -46,6 +46,12 @@ func (a *App) startGuestToolsServer() (srv *guesttools.Server, port int, created
|
||||
srv = guesttools.NewServer(nil)
|
||||
}
|
||||
srv.SetHostTextHandler(func(vmName, text string) {
|
||||
a.mu.Lock()
|
||||
mgr := a.pluginMgr
|
||||
a.mu.Unlock()
|
||||
if mgr != nil && !mgr.Registry().IsActive(builtin.IDInput) {
|
||||
return
|
||||
}
|
||||
a.injectRemoteText(vmName, "", text)
|
||||
})
|
||||
a.guestTools = srv
|
||||
|
||||
@@ -1119,7 +1119,7 @@ func (s *Server) handleHostText(w http.ResponseWriter, r *http.Request) {
|
||||
fn := s.onHostText
|
||||
s.mu.RUnlock()
|
||||
if fn != nil {
|
||||
go fn(vmSnap.name, string(body))
|
||||
fn(vmSnap.name, string(body))
|
||||
}
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
}
|
||||
|
||||
@@ -162,8 +162,10 @@ func defGuestTools(ver string, cb HostCallbacks, desiredFn func(string, bool) bo
|
||||
gtCfg = cb.ValidateDriveConfig(gtCfg)
|
||||
}
|
||||
s.RegisterVM(gtCfg)
|
||||
intent.Env["REIMS_GO_INPUT_URL"] = fmt.Sprintf("http://127.0.0.1:%d/v1/host-text", port)
|
||||
intent.Env["REIMS_GO_INPUT_TOKEN"] = gtCfg.Token
|
||||
if sc.Registry().IsActive(IDInput) && hasCap(p.RuntimeCaps, "win32-unicode-input") {
|
||||
intent.Env["REIMS_GO_INPUT_URL"] = fmt.Sprintf("http://127.0.0.1:%d/v1/host-text", port)
|
||||
intent.Env["REIMS_GO_INPUT_TOKEN"] = gtCfg.Token
|
||||
}
|
||||
}
|
||||
if reims.HasGuestToolsArgs(p.ExtraArgs) && cb.GuestToolsImg != nil && err == nil {
|
||||
imgPath := cb.GuestToolsImg(p.VMName)
|
||||
|
||||
@@ -139,3 +139,62 @@ func TestBuiltinInstallGuideLifecycleZeroResidue(t *testing.T) {
|
||||
t.Fatal("expected a fresh ui.install-guide service instance after re-enable")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGuestToolsInputBridgeEnvGating(t *testing.T) {
|
||||
reg := plugin.NewRegistry(nil)
|
||||
gtSrv := guesttools.NewTransportServer(nil)
|
||||
if _, err := gtSrv.Start(-1); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() { _ = gtSrv.Close(context.Background()) }()
|
||||
|
||||
cb := HostCallbacks{
|
||||
GetConfig: func() config.Config { return config.Config{DataRoot: t.TempDir()} },
|
||||
EnsureGuestTools: func() (*guesttools.Server, int, error) {
|
||||
return gtSrv, gtSrv.Port(), nil
|
||||
},
|
||||
}
|
||||
if err := RegisterAll(reg, cb, nil); err != nil {
|
||||
t.Fatalf("RegisterAll: %v", err)
|
||||
}
|
||||
reg.Reconcile(context.Background())
|
||||
|
||||
vmDir := t.TempDir()
|
||||
dispatch := func(caps []string) map[string]string {
|
||||
intents, err := reg.Hooks().DispatchBeforeStart(context.Background(), plugin.BeforeStartPayload{
|
||||
VMName: "mac",
|
||||
VMDir: vmDir,
|
||||
DriverID: IDDriverReimsMacOS,
|
||||
RuntimeCaps: caps,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("DispatchBeforeStart: %v", err)
|
||||
}
|
||||
for _, in := range intents {
|
||||
if in.PluginID == IDGuestTools {
|
||||
return in.Env
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Runtime without safe win32-unicode-input capability (e.g. runtime-3731afd-p3)
|
||||
// must NOT receive REIMS_GO_INPUT_URL / REIMS_GO_INPUT_TOKEN.
|
||||
if env := dispatch(nil); env["REIMS_GO_INPUT_URL"] != "" || env["REIMS_GO_INPUT_TOKEN"] != "" {
|
||||
t.Fatalf("without capability: unexpected input bridge env %v", env)
|
||||
}
|
||||
|
||||
// Fixed runtime + active builtin.input plugin -> env vars injected.
|
||||
env := dispatch([]string{"win32-unicode-input"})
|
||||
if env["REIMS_GO_INPUT_URL"] == "" || env["REIMS_GO_INPUT_TOKEN"] == "" {
|
||||
t.Fatalf("with capability and active input plugin: missing input bridge env %v", env)
|
||||
}
|
||||
|
||||
// Disabling builtin.input stops injecting REIMS_GO_INPUT_URL / TOKEN even on fixed runtime.
|
||||
if err := reg.SetDesired(context.Background(), IDInput, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if env := dispatch([]string{"win32-unicode-input"}); env["REIMS_GO_INPUT_URL"] != "" || env["REIMS_GO_INPUT_TOKEN"] != "" {
|
||||
t.Fatalf("with input plugin disabled: unexpected input bridge env %v", env)
|
||||
}
|
||||
}
|
||||
@@ -130,6 +130,13 @@ func (m Manifest) EffectiveRuntimeAPI() string {
|
||||
return DefaultRuntimeAPI
|
||||
}
|
||||
|
||||
// buggyWin32UnicodeInputPatchSHA256 is the SHA-256 of the initial
|
||||
// reims-vgpu-win32-unicode-input.patch shipped in runtime-3731afd-p2/p3,
|
||||
// which compared WM_CHAR.time against WM_KEYDOWN.time and duplicated
|
||||
// physical keystrokes (issue #14). Runtimes built with this patch hash must
|
||||
// NOT advertise the "win32-unicode-input" capability.
|
||||
const buggyWin32UnicodeInputPatchSHA256 = "0b34f7c0ebdf3ca537c7f4523af97967a06924aedba7058dbc978870f561b366"
|
||||
|
||||
// EffectiveCapabilities returns declared capabilities plus inferred legacy
|
||||
// capabilities derived from Patches so callers no longer match raw patch filenames (§5.2).
|
||||
func (m Manifest) EffectiveCapabilities() []string {
|
||||
@@ -156,6 +163,10 @@ func (m Manifest) EffectiveCapabilities() []string {
|
||||
add("win10-compat")
|
||||
case strings.Contains(low, "coroutine-fiber"):
|
||||
add("win32-coroutine-fiber")
|
||||
case strings.Contains(low, "win32-unicode-input"):
|
||||
if sha := strings.TrimSpace(p.SHA256); sha != "" && !strings.EqualFold(sha, buggyWin32UnicodeInputPatchSHA256) {
|
||||
add("win32-unicode-input")
|
||||
}
|
||||
}
|
||||
}
|
||||
return out
|
||||
|
||||
@@ -39,6 +39,48 @@ func TestKindRegistryAndCapabilities(t *testing.T) {
|
||||
if !m.HasCapability("nested-virt-nonfatal") || !m.HasCapability("win10-startup-ipis") {
|
||||
t.Fatalf("expected inferred capabilities, got %v", m.EffectiveCapabilities())
|
||||
}
|
||||
|
||||
// Buggy p2/p3 win32-unicode-input patch hash (issue #14) and empty hash must
|
||||
// not advertise the win32-unicode-input capability; fixed patch hashes and
|
||||
// explicit capability declarations must advertise it.
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
m Manifest
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
name: "buggy-p3-hash",
|
||||
m: Manifest{Patches: []Patch{{
|
||||
Name: "reims-vgpu-win32-unicode-input.patch",
|
||||
SHA256: buggyWin32UnicodeInputPatchSHA256,
|
||||
}}},
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "missing-hash",
|
||||
m: Manifest{Patches: []Patch{{
|
||||
Name: "reims-vgpu-win32-unicode-input.patch",
|
||||
}}},
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "fixed-patch-hash",
|
||||
m: Manifest{Patches: []Patch{{
|
||||
Name: "reims-vgpu-win32-unicode-input.patch",
|
||||
SHA256: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
||||
}}},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "explicit-capability",
|
||||
m: Manifest{Capabilities: []string{"win32-unicode-input"}},
|
||||
want: true,
|
||||
},
|
||||
} {
|
||||
if got := tc.m.HasCapability("win32-unicode-input"); got != tc.want {
|
||||
t.Fatalf("%s: HasCapability(win32-unicode-input) = %v, want %v", tc.name, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMultiKindCoexistenceAndCompatibility(t *testing.T) {
|
||||
|
||||
+1
-1
@@ -34,7 +34,7 @@
|
||||
### 1. `reims-vgpu-win32-unicode-input.patch`
|
||||
- **目标**:`steelbrain/reims-vgpu`(`crates/reims-vgpu-window`:`native/windows.rs`、`native/sdl_thread/ui.rs`)
|
||||
- **根因**:SDL3 窗口层未启用文本输入且仅转发 `0x04..=0xE7` 物理扫描码,扫描码为 0 的 `VK_PACKET` 及 `WM_CHAR`/`WM_UNICHAR`/`WM_IME_CHAR`(宿主机 IME、中文、语音与远程输入)全部被丢弃。
|
||||
- **最小修复**:仅在检测到 `REIMS_GO_INPUT_URL`(限定回环地址)时挂接 `SDL_SetWindowsMessageHook` 异步转发非物理字符,对物理键码路径零影响。
|
||||
- **最小修复**:仅在检测到 `REIMS_GO_INPUT_URL`(限定回环地址)时挂接 `SDL_SetWindowsMessageHook` 异步转发非物理字符;对 `WM_CHAR` 按 `lParam` 硬件扫描码(`scan_code == 0`)过滤(不比对 `msg.time`,避免 `TranslateMessage` 写入当前 `GetTickCount()` 导致物理按键被二次转发),对物理键码路径零影响。
|
||||
|
||||
### 2. `reims-ctl-win32-random-bytes.patch`
|
||||
- **目标**:`steelbrain/reims-vgpu`(`crates/reims-ctl/src/vm.rs:849-864`)
|
||||
|
||||
@@ -37,7 +37,7 @@ index cc456c5..4ce46a6 100644
|
||||
};
|
||||
use windows_sys::Win32::Foundation::{FALSE, HWND, RPC_E_CHANGED_MODE, S_FALSE, S_OK, TRUE};
|
||||
use windows_sys::Win32::System::Com::{
|
||||
@@ -62,6 +69,198 @@ use crate::capture::HostOs;
|
||||
@@ -62,6 +69,203 @@ use crate::capture::HostOs;
|
||||
/// The OS whose names the title gives the release chord's keys: Ctrl+Alt+Win (the module doc).
|
||||
pub(super) const HOST_OS: HostOs = HostOs::Windows;
|
||||
|
||||
@@ -51,9 +51,9 @@ index cc456c5..4ce46a6 100644
|
||||
+ /// WPARAM.
|
||||
+ w_param: usize,
|
||||
+ /// LPARAM.
|
||||
+ _l_param: isize,
|
||||
+ l_param: isize,
|
||||
+ /// Message timestamp (GetMessageTime).
|
||||
+ time: u32,
|
||||
+ _time: u32,
|
||||
+ /// Cursor position (POINT).
|
||||
+ _pt: [i32; 2],
|
||||
+}
|
||||
@@ -81,8 +81,6 @@ index cc456c5..4ce46a6 100644
|
||||
+thread_local! {
|
||||
+ /// Whether the most recent WM_KEYDOWN was unmapped (VK_PACKET or VK_PROCESSKEY).
|
||||
+ static LAST_KEY_UNMAPPED: Cell<bool> = const { Cell::new(false) };
|
||||
+ /// Timestamp of the most recent normal WM_KEYDOWN that SDL translates to a USB key.
|
||||
+ static LAST_MAPPED_KEY_TIME: Cell<u32> = const { Cell::new(0) };
|
||||
+}
|
||||
+
|
||||
+/// Parses a loopback http://127.0.0.1:<port><path> endpoint from REIMS_GO_INPUT_URL.
|
||||
@@ -148,20 +146,27 @@ index cc456c5..4ce46a6 100644
|
||||
+ match msg.message {
|
||||
+ WM_KEYDOWN | WM_SYSKEYDOWN => {
|
||||
+ let vk = (msg.w_param & 0xFFFF) as u16;
|
||||
+ if vk == VK_PACKET || vk == VK_PROCESSKEY {
|
||||
+ LAST_KEY_UNMAPPED.set(true);
|
||||
+ } else {
|
||||
+ LAST_KEY_UNMAPPED.set(false);
|
||||
+ LAST_MAPPED_KEY_TIME.set(msg.time);
|
||||
+ LAST_KEY_UNMAPPED.set(vk == VK_PACKET || vk == VK_PROCESSKEY);
|
||||
+ true
|
||||
+ }
|
||||
+ WM_CHAR => {
|
||||
+ let unit = (msg.w_param & 0xFFFF) as u16;
|
||||
+ let scan_code = (msg.l_param.cast_unsigned() >> 16) & 0xFF;
|
||||
+ // Any WM_CHAR carrying a hardware scan code came from TranslateMessage on
|
||||
+ // a physical WM_KEYDOWN that SDL already forwarded as a USB HID key.
|
||||
+ // Never compare msg.time against WM_KEYDOWN.time: TranslateMessage stamps
|
||||
+ // WM_CHAR with the current GetTickCount(), not the keydown's time.
|
||||
+ let non_physical = scan_code == 0
|
||||
+ && (LAST_KEY_UNMAPPED.get() || unit >= 0x80 || msg.l_param == 0);
|
||||
+ if non_physical && (unit >= 0x20 || unit == 0x0D || unit == 0x09) {
|
||||
+ enqueue_utf16(unit);
|
||||
+ return false;
|
||||
+ }
|
||||
+ true
|
||||
+ }
|
||||
+ WM_CHAR | WM_IME_CHAR => {
|
||||
+ WM_IME_CHAR => {
|
||||
+ let unit = (msg.w_param & 0xFFFF) as u16;
|
||||
+ let non_physical = unit >= 0x80
|
||||
+ || LAST_KEY_UNMAPPED.get()
|
||||
+ || msg.time != LAST_MAPPED_KEY_TIME.get();
|
||||
+ if non_physical && (unit >= 0x20 || unit == 0x0D || unit == 0x09) {
|
||||
+ if unit >= 0x20 || unit == 0x0D || unit == 0x09 {
|
||||
+ enqueue_utf16(unit);
|
||||
+ return false;
|
||||
+ }
|
||||
@@ -219,7 +224,7 @@ index cc456c5..4ce46a6 100644
|
||||
+ }
|
||||
+}
|
||||
+
|
||||
+/// Sends a minimal HTTP/1.1 POST with UTF-8 ext to the local host bridge.
|
||||
+/// Sends a minimal HTTP/1.1 POST with UTF-8 text to the local host bridge.
|
||||
+fn post_loopback_text(addr: SocketAddr, path: &str, token: &str, text: &str) {
|
||||
+ let Ok(mut stream) = TcpStream::connect_timeout(&addr, Duration::from_millis(500)) else {
|
||||
+ return;
|
||||
|
||||
Reference in new issue
Block a user