Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

chore(windows-input): 失败行带上失败点与 Win32 状态 (#4291)

Lizi1ccc26a27b49
diff --git a/apps/desktop/native/remote-desktop/windows-input/src/desktop.rs b/apps/desktop/native/remote-desktop/windows-input/src/desktop.rs
index c381db77bf..27c9969cee 100644
--- a/apps/desktop/native/remote-desktop/windows-input/src/desktop.rs
+++ b/apps/desktop/native/remote-desktop/windows-input/src/desktop.rs
@@ -1,6 +1,7 @@
//! Input belongs to the active Windows desktop, not permanently to Default.
//! This does not grant access: the OS still checks the process token. Secure
//! desktops require the session worker to be launched by the SYSTEM broker.
+use windows_sys::Win32::Foundation::GetLastError;
use windows_sys::Win32::System::StationsAndDesktops::*;
pub struct InputDesktop {
@@ -23,8 +24,10 @@ impl InputDesktop {
/// Call only on the input thread, which must never own windows or hooks.
/// Returns true after a transition; callers discard the transition batch
/// so characters intended for the previous screen cannot reach a password
- /// prompt or another user's desktop.
- pub fn bind(&mut self) -> Result<bool, ()> {
+ /// prompt or another user's desktop. The error is the Win32 status of the
+ /// call that failed, so a machine that cannot bind can be told apart in the
+ /// field without a debugger.
+ pub fn bind(&mut self) -> Result<bool, u32> {
unsafe {
let next = OpenInputDesktop(
0,
@@ -32,7 +35,7 @@ impl InputDesktop {
DESKTOP_READOBJECTS | DESKTOP_WRITEOBJECTS | DESKTOP_SWITCHDESKTOP,
);
if next.is_null() {
- return Err(());
+ return Err(GetLastError());
}
let mut name = vec![0u16; 256];
let mut needed = 0;
@@ -44,8 +47,10 @@ impl InputDesktop {
&mut needed,
) == 0
{
+ // Read the status before closing: closing has its own result.
+ let status = GetLastError();
CloseDesktop(next);
- return Err(());
+ return Err(status);
}
name.truncate((needed as usize / 2).min(name.len()));
if !self.handle.is_null() && name == self.name {
@@ -53,8 +58,9 @@ impl InputDesktop {
return Ok(false);
}
if SetThreadDesktop(next) == 0 {
+ let status = GetLastError();
CloseDesktop(next);
- return Err(());
+ return Err(status);
}
let changed = !self.handle.is_null();
if changed {
diff --git a/apps/desktop/native/remote-desktop/windows-input/src/main.rs b/apps/desktop/native/remote-desktop/windows-input/src/main.rs
index 0db8e9f77d..0946aec7d4 100644
--- a/apps/desktop/native/remote-desktop/windows-input/src/main.rs
+++ b/apps/desktop/native/remote-desktop/windows-input/src/main.rs
@@ -5,6 +5,7 @@ use std::{
sync::mpsc,
time::Duration,
};
+use windows_sys::Win32::Foundation::GetLastError;
use windows_sys::Win32::UI::{HiDpi::*, Input::KeyboardAndMouse::*, WindowsAndMessaging::*};
mod desktop;
mod selection;
@@ -12,9 +13,15 @@ mod selection;
static FAILED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
fn send(input: INPUT) -> bool {
let ok = unsafe { SendInput(1, &input, std::mem::size_of::<INPUT>() as i32) == 1 };
- if !ok && !FAILED.swap(true, std::sync::atomic::Ordering::SeqCst) {
- println!("error");
- io::stdout().flush().ok();
+ if !ok {
+ let status = unsafe { GetLastError() };
+ if !FAILED.swap(true, std::sync::atomic::Ordering::SeqCst) {
+ // The parent treats any output line as "input failed" and stops
+ // reading, so the reason rides on the one line it already prints:
+ // which call failed and the Win32 status. No coordinates, no text.
+ println!("error send_input {status}");
+ io::stdout().flush().ok();
+ }
}
ok
}
@@ -187,8 +194,8 @@ fn main() {
let mut keys = HashSet::new();
let mut buttons = HashSet::new();
let mut desktop = desktop::InputDesktop::new();
- if desktop.bind().is_err() {
- println!("error");
+ if let Err(status) = desktop.bind() {
+ println!("error input_desktop {status}");
return;
}
println!("ready");
@@ -205,8 +212,8 @@ fn main() {
break;
}
Ok(false) => (),
- Err(_) => {
- println!("error");
+ Err(status) => {
+ println!("error input_desktop {status}");
io::stdout().flush().ok();
break;
}
diff --git a/docs/remote-desktop.md b/docs/remote-desktop.md
index 669fdd72ac..72d1dbc798 100644
--- a/docs/remote-desktop.md
+++ b/docs/remote-desktop.md
@@ -246,6 +246,13 @@ Windows secure desktop/UAC and elevated applications can reject input, and
macOS lock/login screens and protected surfaces are not guaranteed controllable.
System audio and explicit clipboard transfers are supported when advertised by the host. Virtual displays and remote power-on are not included. The phone keyboard sends committed text directly; the computer keyboard supplies modifiers and special keys.
+The Windows input helper reports a failed call on the single output line its host
+already treats as "input failed": `error send_input <status>` when Win32 rejects
+an injection, and `error input_desktop <status>` when the desktop binding fails,
+with the Win32 status of the failing call. The line carries no coordinates and no
+typed text, and both consumers (Main's output watch and the SYSTEM service
+worker's failure watch) still classify it by the same rules as before.
+
Unit tests cover peer/lease isolation, expiry, revocation during asynchronous
capture, start/stop races, input replay, human/Agent exclusion, portrait/landscape
geometry, keyboard resize, inline viewer parsing, two-finger gestures, and reconnect.