- "Send text to VM..." dialog: arbitrary Unicode goes through Guest Tools (clipboard paste in the guest, agent v3 /v1/text-input long poll); without Guest Tools only US-keyboard ASCII is typed via QMP. - Host IME / dictation: a low-level keyboard hook (internal/host/ime_*) catches VK_PACKET input aimed at a running VM window, coalesces bursts and forwards them in order through a per-VM queue (type_flow.go). - reims-vgpu-window bridge: new runtime patch reims-vgpu-win32-unicode-input enables SDL text input and posts committed text to /v1/host-text; StartVM passes REIMS_GO_INPUT_URL/TOKEN to the window process. This also adds the patch file that patches/series.txt already listed. - Guest Tools auth returns a field snapshot instead of copying the VM entry (it now holds a mutex and text queues). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
239 lines
9.0 KiB
Diff
239 lines
9.0 KiB
Diff
diff --git a/crates/reims-vgpu-window/src/native/sdl_thread/ui.rs b/crates/reims-vgpu-window/src/native/sdl_thread/ui.rs
|
|
index 6f70e07..074917c 100644
|
|
--- a/crates/reims-vgpu-window/src/native/sdl_thread/ui.rs
|
|
+++ b/crates/reims-vgpu-window/src/native/sdl_thread/ui.rs
|
|
@@ -183,6 +183,8 @@ fn start_video() -> bool {
|
|
);
|
|
return false;
|
|
}
|
|
+ #[cfg(target_os = "windows")]
|
|
+ super::super::windows::install_unicode_input_hook();
|
|
// SAFETY: SDL's video runs.
|
|
let kind = unsafe { SDL_RegisterEvents(1) };
|
|
WAKE.store(kind, Ordering::Release);
|
|
diff --git a/crates/reims-vgpu-window/src/native/windows.rs b/crates/reims-vgpu-window/src/native/windows.rs
|
|
index cc456c5..4ce46a6 100644
|
|
--- a/crates/reims-vgpu-window/src/native/windows.rs
|
|
+++ b/crates/reims-vgpu-window/src/native/windows.rs
|
|
@@ -42,12 +42,19 @@
|
|
//! taskbar then goes to the bottom of the z-order while the window is active, and that taking
|
|
//! the mark away leaves the window to the shell's own detection.
|
|
|
|
+use std::cell::Cell;
|
|
use std::ffi::c_void;
|
|
+use std::io::Write;
|
|
+use std::net::{SocketAddr, TcpStream};
|
|
use std::ptr::{NonNull, null, null_mut};
|
|
+use std::sync::OnceLock;
|
|
+use std::sync::mpsc::{self, RecvTimeoutError, SyncSender};
|
|
+use std::time::Duration;
|
|
|
|
use sdl3_sys::everything::{
|
|
- SDL_GetPointerProperty, SDL_GetWindowFlags, SDL_GetWindowProperties,
|
|
- SDL_PROP_WINDOW_WIN32_HWND_POINTER, SDL_WINDOW_FULLSCREEN, SDL_Window,
|
|
+ MSG as SDL_MSG, SDL_GetPointerProperty, SDL_GetWindowFlags, SDL_GetWindowProperties,
|
|
+ SDL_PROP_WINDOW_WIN32_HWND_POINTER, SDL_SetWindowsMessageHook, SDL_WINDOW_FULLSCREEN,
|
|
+ SDL_Window,
|
|
};
|
|
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;
|
|
/// 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;
|
|
|
|
+/// Win32 MSG layout matching winuser.h for SDL_SetWindowsMessageHook.
|
|
+#[repr(C)]
|
|
+struct Win32Msg {
|
|
+ /// Target window handle.
|
|
+ _hwnd: HWND,
|
|
+ /// Message identifier (WM_*).
|
|
+ message: u32,
|
|
+ /// WPARAM.
|
|
+ w_param: usize,
|
|
+ /// LPARAM.
|
|
+ _l_param: isize,
|
|
+ /// Message timestamp (GetMessageTime).
|
|
+ time: u32,
|
|
+ /// Cursor position (POINT).
|
|
+ _pt: [i32; 2],
|
|
+}
|
|
+
|
|
+/// WM_KEYDOWN.
|
|
+const WM_KEYDOWN: u32 = 0x0100;
|
|
+/// WM_CHAR.
|
|
+const WM_CHAR: u32 = 0x0102;
|
|
+/// WM_SYSKEYDOWN.
|
|
+const WM_SYSKEYDOWN: u32 = 0x0104;
|
|
+/// WM_UNICHAR.
|
|
+const WM_UNICHAR: u32 = 0x0109;
|
|
+/// WM_IME_CHAR.
|
|
+const WM_IME_CHAR: u32 = 0x0286;
|
|
+/// UNICODE_NOCHAR.
|
|
+const UNICODE_NOCHAR: usize = 0xFFFF;
|
|
+/// VK_PROCESSKEY (host IME intercepted key).
|
|
+const VK_PROCESSKEY: u16 = 0x00E5;
|
|
+/// VK_PACKET (SendInput with KEYEVENTF_UNICODE).
|
|
+const VK_PACKET: u16 = 0x00E7;
|
|
+
|
|
+/// Non-blocking channel carrying UTF-16 code units from the SDL UI thread to the HTTP forwarder.
|
|
+static UNICODE_TX: OnceLock<SyncSender<u16>> = OnceLock::new();
|
|
+
|
|
+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.
|
|
+fn parse_loopback_input_url(raw: &str) -> Option<(SocketAddr, String)> {
|
|
+ let rest = raw.trim().strip_prefix("http://127.0.0.1:")?;
|
|
+ let (port_str, path) = match rest.find('/') {
|
|
+ Some(idx) => (&rest[..idx], &rest[idx..]),
|
|
+ None => (rest, "/v1/host-text"),
|
|
+ };
|
|
+ let port: u16 = port_str.parse().ok()?;
|
|
+ if port == 0 || path.contains(['\r', '\n', ' ']) {
|
|
+ return None;
|
|
+ }
|
|
+ let addr = SocketAddr::from(([127, 0, 0, 1], port));
|
|
+ Some((addr, path.to_owned()))
|
|
+}
|
|
+
|
|
+/// Installs a Win32 message hook on SDL's UI thread when REIMS_GO_INPUT_URL and
|
|
+/// REIMS_GO_INPUT_TOKEN are set, forwarding non-keyboard WM_CHAR / WM_UNICHAR /
|
|
+/// WM_IME_CHAR text (remote IMEs, dictation, host IME commits) to the local host bridge.
|
|
+pub(super) fn install_unicode_input_hook() {
|
|
+ let Ok(url) = std::env::var("REIMS_GO_INPUT_URL") else {
|
|
+ return;
|
|
+ };
|
|
+ let Ok(token) = std::env::var("REIMS_GO_INPUT_TOKEN") else {
|
|
+ return;
|
|
+ };
|
|
+ if token.is_empty() || token.contains(['\r', '\n']) {
|
|
+ return;
|
|
+ }
|
|
+ let Some((addr, path)) = parse_loopback_input_url(&url) else {
|
|
+ log::warn!(target: "reims_vgpu_window", "ignoring non-loopback REIMS_GO_INPUT_URL");
|
|
+ return;
|
|
+ };
|
|
+ let (tx, rx) = mpsc::sync_channel::<u16>(512);
|
|
+ if UNICODE_TX.set(tx).is_err() {
|
|
+ return;
|
|
+ }
|
|
+ let _ = std::thread::Builder::new()
|
|
+ .name("reims-vgpu.ime".into())
|
|
+ .spawn(move || forward_unicode_loop(rx, addr, path, token));
|
|
+ // SAFETY: called on SDL's UI thread right after SDL_Init(SDL_INIT_VIDEO).
|
|
+ unsafe { SDL_SetWindowsMessageHook(Some(unicode_message_hook), null_mut()) };
|
|
+}
|
|
+
|
|
+/// Enqueues a UTF-16 code unit without blocking the Win32 message loop.
|
|
+fn enqueue_utf16(unit: u16) {
|
|
+ if unit != 0
|
|
+ && let Some(tx) = UNICODE_TX.get()
|
|
+ {
|
|
+ let _ = tx.try_send(unit);
|
|
+ }
|
|
+}
|
|
+
|
|
+/// SDL Win32 message hook: forwards non-physical WM_CHAR / WM_UNICHAR / WM_IME_CHAR
|
|
+/// events to the local input bridge while leaving all physical keyboard events untouched.
|
|
+unsafe extern "C" fn unicode_message_hook(_userdata: *mut c_void, raw_msg: *mut SDL_MSG) -> bool {
|
|
+ if raw_msg.is_null() {
|
|
+ return true;
|
|
+ }
|
|
+ // SAFETY: SDL passes a valid pointer to Win32 MSG for the duration of the callback.
|
|
+ let msg = unsafe { &*raw_msg.cast::<Win32Msg>() };
|
|
+ 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);
|
|
+ }
|
|
+ true
|
|
+ }
|
|
+ WM_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) {
|
|
+ enqueue_utf16(unit);
|
|
+ return false;
|
|
+ }
|
|
+ true
|
|
+ }
|
|
+ WM_UNICHAR => {
|
|
+ if msg.w_param != UNICODE_NOCHAR
|
|
+ && let Ok(code) = u32::try_from(msg.w_param)
|
|
+ && let Some(ch) = char::from_u32(code)
|
|
+ && (ch >= ' ' || ch == '\r' || ch == '\t')
|
|
+ {
|
|
+ let mut pair = [0u16; 2];
|
|
+ for &mut u in ch.encode_utf16(&mut pair) {
|
|
+ enqueue_utf16(u);
|
|
+ }
|
|
+ return false;
|
|
+ }
|
|
+ true
|
|
+ }
|
|
+ _ => true,
|
|
+ }
|
|
+}
|
|
+
|
|
+/// Decodes UTF-16 code units, coalesces bursts, and POSTs UTF-8 text to the loopback bridge.
|
|
+fn forward_unicode_loop(rx: mpsc::Receiver<u16>, addr: SocketAddr, path: String, token: String) {
|
|
+ let mut units: Vec<u16> = Vec::with_capacity(64);
|
|
+ loop {
|
|
+ let first = match rx.recv() {
|
|
+ Ok(u) => u,
|
|
+ Err(_) => return,
|
|
+ };
|
|
+ units.clear();
|
|
+ units.push(first);
|
|
+ if first != 0x0D {
|
|
+ while units.len() < 128 {
|
|
+ match rx.recv_timeout(Duration::from_millis(35)) {
|
|
+ Ok(u) => {
|
|
+ units.push(u);
|
|
+ if u == 0x0D {
|
|
+ break;
|
|
+ }
|
|
+ }
|
|
+ Err(RecvTimeoutError::Timeout) => break,
|
|
+ Err(RecvTimeoutError::Disconnected) => break,
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ let text: String = char::decode_utf16(units.iter().copied())
|
|
+ .filter_map(Result::ok)
|
|
+ .map(|c| if c == '\r' { '\n' } else { c })
|
|
+ .collect();
|
|
+ if !text.is_empty() {
|
|
+ post_loopback_text(addr, &path, &token, &text);
|
|
+ }
|
|
+ }
|
|
+}
|
|
+
|
|
+/// Sends a minimal HTTP/1.1 POST with UTF-8 ext 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;
|
|
+ };
|
|
+ let _ = stream.set_write_timeout(Some(Duration::from_millis(500)));
|
|
+ let req = format!(
|
|
+ "POST {path} HTTP/1.1\r\nHost: 127.0.0.1\r\nAuthorization: Bearer {token}\r\nContent-Type: text/plain; charset=utf-8\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
|
|
+ text.len()
|
|
+ );
|
|
+ let _ = stream.write_all(req.as_bytes());
|
|
+ let _ = stream.write_all(text.as_bytes());
|
|
+}
|
|
+
|
|
/// The GPU the compositor composites SDL's window on: none known on Windows (the module doc).
|
|
///
|
|
/// # Safety
|