From 16f6028d35ff9b4d4802fb14da71ed600859cc92 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 5 Nov 2026 14:37:24 +0000 Subject: [PATCH] feat(host): bridge the Windows clipboard in the headless host Text clipboard sync only worked when the Flutter app was the host, since its Dart shell is what polls the OS clipboard. The svc-supervised streambeam-host.exe worker (what a real Windows box runs) never touched the clipboard, so sync did nothing in either direction there. Add a platform-neutral ClipboardBridge over a SystemClipboard trait with a Win32 CF_UNICODETEXT backend, run on its own thread from run_cli. It syncs only while a viewer owns the stream and, under the SYSTEM worker, only while the input desktop is Default (never on the lock screen). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AHutN2ijFMPqmJ98kBAEH3 --- crates/host/Cargo.toml | 4 + crates/host/src/clipboard.rs | 362 +++++++++++++++++++++++++++++++++ crates/host/src/desktop_win.rs | 12 ++ crates/host/src/lib.rs | 1 + crates/host/src/main.rs | 6 +- crates/host/src/server.rs | 4 +- docs/PROGRESS.md | 2 + docs/PROTOCOL.md | 4 +- 8 files changed, 391 insertions(+), 4 deletions(-) create mode 100644 crates/host/src/clipboard.rs diff --git a/crates/host/Cargo.toml b/crates/host/Cargo.toml index f81e816..72431d5 100644 --- a/crates/host/Cargo.toml +++ b/crates/host/Cargo.toml @@ -28,6 +28,10 @@ audio = ["dep:opus"] [target.'cfg(windows)'.dependencies] windows-sys = { version = "0.61", features = [ "Win32_Foundation", + # Text clipboard bridge for the headless worker (clipboard.rs). + "Win32_System_DataExchange", + "Win32_System_Memory", + "Win32_System_Ole", "Win32_UI_Input_KeyboardAndMouse", "Win32_UI_WindowsAndMessaging", ] } diff --git a/crates/host/src/clipboard.rs b/crates/host/src/clipboard.rs new file mode 100644 index 0000000..3baf29c --- /dev/null +++ b/crates/host/src/clipboard.rs @@ -0,0 +1,362 @@ +//! OS clipboard bridge for the headless host binary. +//! +//! The wire half of text clipboard sync lives in [`HostControls`]: client +//! updates land in `take_remote_clipboard`, and `publish_clipboard` fans host +//! text out to the viewer. When the Flutter app is the host, its Dart shell +//! polls the platform clipboard and drives those two calls. The standalone +//! `streambeam-host.exe`, which is what `streambeam-svc` runs on a real +//! Windows box, has no Dart shell, so nothing ever touched the Windows +//! clipboard and sync silently did nothing in either direction. This module +//! is that missing poller. +//! +//! The sync logic ([`ClipboardBridge`]) is platform-neutral and tested here +//! against a fake; only [`SystemClipboard`] has a Windows implementation. + +use std::sync::Arc; +use std::time::Duration; + +use crate::server::HostControls; + +/// Same cadence as the app shell's clipboard poll. +pub const POLL_INTERVAL: Duration = Duration::from_millis(750); + +/// Plain-text access to the machine's clipboard. +pub trait SystemClipboard { + /// A counter that changes whenever the clipboard does, so an unchanged + /// clipboard costs no read. `None` means "unknown, read every tick". + fn change_count(&mut self) -> Option { + None + } + /// Current text, or `None` if it holds no text or can't be opened. + fn read_text(&mut self) -> Option; + /// Replace the clipboard with `text`. `false` means try again later + /// (another process may hold the clipboard open). + fn write_text(&mut self, text: &str) -> bool; +} + +/// One side of the two-way sync: applies viewer text locally and publishes +/// local changes, de-duplicating against the last value either way so a +/// value never echoes back to where it came from. +pub struct ClipboardBridge { + clipboard: C, + last_synced: Option, + last_change: Option, + pending_write: Option, +} + +impl ClipboardBridge { + pub fn new(clipboard: C) -> Self { + Self { + clipboard, + last_synced: None, + last_change: None, + pending_write: None, + } + } + + /// One poll. `active` is false when nobody is watching or the host is on + /// a desktop whose clipboard must not leave the machine (lock screen); + /// then the clipboard is neither read nor written. A viewer update that + /// arrives meanwhile is kept and applied once sync is active again. + pub fn tick(&mut self, controls: &HostControls, active: bool) { + if let Some(remote) = controls.take_remote_clipboard() { + self.pending_write = Some(remote); + } + if !active { + return; + } + + if let Some(remote) = self.pending_write.take() { + if self.last_synced.as_deref() == Some(remote.as_str()) { + // Already what we have; nothing to do. + } else if self.clipboard.write_text(&remote) { + self.last_synced = Some(remote); + // Our own write bumps the change counter; skip re-reading it. + self.last_change = self.clipboard.change_count(); + return; + } else { + self.pending_write = Some(remote); + return; + } + } + + let change = self.clipboard.change_count(); + if change.is_some() && change == self.last_change { + return; + } + let Some(text) = self.clipboard.read_text() else { + // Couldn't open it: leave `last_change` so the next tick retries. + return; + }; + self.last_change = change; + if text.is_empty() || self.last_synced.as_deref() == Some(text.as_str()) { + return; + } + self.last_synced = Some(text.clone()); + controls.publish_clipboard(text); + } +} + +/// Run the bridge for the life of the process on its own thread. Windows-only; +/// elsewhere the headless binary is a dev tool and the app shell owns sync. +pub fn spawn(controls: Arc) { + #[cfg(windows)] + { + let spawned = std::thread::Builder::new() + .name("streambeam-clipboard".into()) + .spawn(move || { + let mut bridge = ClipboardBridge::new(windows_impl::WinClipboard); + loop { + let active = + !controls.viewers().is_empty() && windows_impl::clipboard_may_sync(); + bridge.tick(&controls, active); + std::thread::sleep(POLL_INTERVAL); + } + }); + if let Err(e) = spawned { + log::warn!("clipboard sync disabled: {e}"); + } + } + #[cfg(not(windows))] + drop(controls); +} + +#[cfg(windows)] +mod windows_impl { + #![allow(unsafe_op_in_unsafe_fn)] + + use windows_sys::Win32::Foundation::GlobalFree; + use windows_sys::Win32::System::DataExchange::{ + CloseClipboard, EmptyClipboard, GetClipboardData, GetClipboardSequenceNumber, + IsClipboardFormatAvailable, OpenClipboard, SetClipboardData, + }; + use windows_sys::Win32::System::Memory::{ + GMEM_MOVEABLE, GlobalAlloc, GlobalLock, GlobalSize, GlobalUnlock, + }; + use windows_sys::Win32::System::Ole::CF_UNICODETEXT; + + use super::SystemClipboard; + + /// The SYSTEM service worker follows the input desktop onto the lock + /// screen. The clipboard is per window station, so it would still be + /// reachable there; don't sync while locked, so a locked machine never + /// hands out what was copied before it locked. + pub fn clipboard_may_sync() -> bool { + !crate::desktop_win::secure_desktop_worker() + || crate::desktop_win::input_desktop_name().as_deref() == Some("Default") + } + + pub struct WinClipboard; + + /// Holds the clipboard open; closes it on drop, including early returns. + struct Open; + + impl Open { + fn new() -> Option { + // Null owner: fine for reading, and for writing as long as we + // EmptyClipboard first (ownership then goes to no window). + (unsafe { OpenClipboard(std::ptr::null_mut()) } != 0).then_some(Open) + } + } + + impl Drop for Open { + fn drop(&mut self) { + unsafe { CloseClipboard() }; + } + } + + const FORMAT: u32 = CF_UNICODETEXT as u32; + + impl SystemClipboard for WinClipboard { + fn change_count(&mut self) -> Option { + Some(unsafe { GetClipboardSequenceNumber() } as u64) + } + + fn read_text(&mut self) -> Option { + unsafe { + if IsClipboardFormatAvailable(FORMAT) == 0 { + // Non-text content (an image, files): nothing to sync, and + // not an error worth retrying. + return Some(String::new()); + } + let _open = Open::new()?; + let handle = GetClipboardData(FORMAT); + if handle.is_null() { + return None; + } + let ptr = GlobalLock(handle) as *const u16; + if ptr.is_null() { + return None; + } + // Bounded by the allocation, not just the terminator, so a + // malformed producer can't make us read past its buffer. + let max = GlobalSize(handle) / 2; + let slice = std::slice::from_raw_parts(ptr, max); + let len = slice.iter().position(|&c| c == 0).unwrap_or(max); + let text = String::from_utf16_lossy(&slice[..len]); + GlobalUnlock(handle); + Some(text) + } + } + + fn write_text(&mut self, text: &str) -> bool { + let wide: Vec = text.encode_utf16().chain(std::iter::once(0)).collect(); + unsafe { + let Some(_open) = Open::new() else { + return false; + }; + let mem = GlobalAlloc(GMEM_MOVEABLE, wide.len() * 2); + if mem.is_null() { + return false; + } + let dst = GlobalLock(mem) as *mut u16; + if dst.is_null() { + GlobalFree(mem); + return false; + } + std::ptr::copy_nonoverlapping(wide.as_ptr(), dst, wide.len()); + GlobalUnlock(mem); + EmptyClipboard(); + if SetClipboardData(FORMAT, mem).is_null() { + // Ownership didn't transfer; the memory is still ours. + GlobalFree(mem); + return false; + } + true + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[derive(Default)] + struct Fake { + text: Option, + seq: u64, + writes: usize, + reads: usize, + fail_writes: bool, + } + + impl SystemClipboard for &mut Fake { + fn change_count(&mut self) -> Option { + Some(self.seq) + } + fn read_text(&mut self) -> Option { + self.reads += 1; + Some(self.text.clone().unwrap_or_default()) + } + fn write_text(&mut self, text: &str) -> bool { + if self.fail_writes { + return false; + } + self.writes += 1; + self.seq += 1; + self.text = Some(text.into()); + true + } + } + + fn copy(fake: &mut Fake, text: &str) { + fake.seq += 1; + fake.text = Some(text.into()); + } + + fn published(controls: &HostControls) -> (u64, String) { + controls.clipboard_to_client.borrow().clone() + } + + #[test] + fn local_copy_is_published_once() { + let controls = HostControls::new(); + let mut fake = Fake::default(); + copy(&mut fake, "host text"); + let mut bridge = ClipboardBridge::new(&mut fake); + + bridge.tick(&controls, true); + bridge.tick(&controls, true); + + assert_eq!(published(&controls), (1, "host text".into())); + drop(bridge); + assert_eq!(fake.reads, 1, "unchanged sequence must not re-read"); + } + + #[test] + fn viewer_text_is_applied_and_not_echoed_back() { + let controls = HostControls::new(); + let mut fake = Fake::default(); + let mut bridge = ClipboardBridge::new(&mut fake); + + controls.receive_remote_clipboard("from phone".into()); + bridge.tick(&controls, true); + bridge.tick(&controls, true); + + assert_eq!(published(&controls).0, 0, "must not echo to the viewer"); + drop(bridge); + assert_eq!(fake.text.as_deref(), Some("from phone")); + assert_eq!(fake.writes, 1); + } + + #[test] + fn nothing_moves_while_inactive_and_pending_text_lands_later() { + let controls = HostControls::new(); + let mut fake = Fake::default(); + copy(&mut fake, "secret before lock"); + let mut bridge = ClipboardBridge::new(&mut fake); + + controls.receive_remote_clipboard("typed while locked".into()); + bridge.tick(&controls, false); + assert_eq!(published(&controls).0, 0); + + bridge.tick(&controls, true); + drop(bridge); + assert_eq!(fake.text.as_deref(), Some("typed while locked")); + assert_eq!(published(&controls).0, 0); + } + + #[test] + fn failed_write_is_retried() { + let controls = HostControls::new(); + let mut fake = Fake { + fail_writes: true, + ..Fake::default() + }; + let mut bridge = ClipboardBridge::new(&mut fake); + + controls.receive_remote_clipboard("retry me".into()); + bridge.tick(&controls, true); + assert_eq!(bridge.clipboard.writes, 0); + bridge.clipboard.fail_writes = false; + bridge.tick(&controls, true); + + drop(bridge); + assert_eq!(fake.text.as_deref(), Some("retry me")); + } + + #[test] + fn host_change_after_viewer_text_is_published() { + let controls = HostControls::new(); + let mut fake = Fake::default(); + let mut bridge = ClipboardBridge::new(&mut fake); + + controls.receive_remote_clipboard("from phone".into()); + bridge.tick(&controls, true); + copy(bridge.clipboard, "edited on host"); + bridge.tick(&controls, true); + + assert_eq!(published(&controls), (1, "edited on host".into())); + } + + #[test] + fn empty_or_non_text_clipboard_is_not_published() { + let controls = HostControls::new(); + let mut fake = Fake::default(); + copy(&mut fake, ""); + let mut bridge = ClipboardBridge::new(&mut fake); + bridge.tick(&controls, true); + assert_eq!(published(&controls).0, 0); + } +} diff --git a/crates/host/src/desktop_win.rs b/crates/host/src/desktop_win.rs index 1e38d6e..6433229 100644 --- a/crates/host/src/desktop_win.rs +++ b/crates/host/src/desktop_win.rs @@ -56,6 +56,18 @@ pub fn desktop_diagnosis() -> String { } } +/// Name of the desktop currently receiving input (`Default`, `Winlogon`, …), +/// or `None` when it can't be opened, which is itself what a locked session +/// looks like from a non-SYSTEM process. +pub fn input_desktop_name() -> Option { + unsafe { + let desk = OpenInputDesktop(DESKTOP_CONTROL_FLAGS(0), false, DESKTOP_GENERIC_ALL).ok()?; + let name = desktop_name(desk); + let _ = CloseDesktop(desk); + Some(name) + } +} + fn desktop_name(desk: HDESK) -> String { unsafe { let mut buf = [0u16; 64]; diff --git a/crates/host/src/lib.rs b/crates/host/src/lib.rs index d646d68..2bfa318 100644 --- a/crates/host/src/lib.rs +++ b/crates/host/src/lib.rs @@ -10,6 +10,7 @@ pub mod audio; pub mod audio_capture_win; #[cfg(windows)] pub mod capture; +pub mod clipboard; pub mod config; pub mod data_dir; #[cfg(windows)] diff --git a/crates/host/src/main.rs b/crates/host/src/main.rs index b2bc16a..0d54006 100644 --- a/crates/host/src/main.rs +++ b/crates/host/src/main.rs @@ -13,6 +13,7 @@ pub mod audio; pub mod audio_capture_win; #[cfg(windows)] mod capture; +pub mod clipboard; pub mod config; pub mod data_dir; #[cfg(windows)] @@ -144,7 +145,10 @@ fn build_config( fn run_cli(opt: Opt) -> Result<(), Box> { let controls = std::sync::Arc::new(server::HostControls::new()); - let config = build_config(opt, None, None, controls); + let config = build_config(opt, None, None, controls.clone()); + // No Dart shell here to poll the OS clipboard (the app host has one), so + // the headless worker runs its own bridge. + clipboard::spawn(controls); eprintln!( "StreamBeam host {} (mock encoder — real capture in M2)", env!("CARGO_PKG_VERSION") diff --git a/crates/host/src/server.rs b/crates/host/src/server.rs index 9d56189..0a26d77 100644 --- a/crates/host/src/server.rs +++ b/crates/host/src/server.rs @@ -153,7 +153,7 @@ pub struct HostControls { /// wakes it if it's currently parked on `endpoint.accept()`. shutdown_requested: AtomicBool, shutdown: tokio::sync::Notify, - clipboard_to_client: tokio::sync::watch::Sender<(u64, String)>, + pub(crate) clipboard_to_client: tokio::sync::watch::Sender<(u64, String)>, clipboard_from_client: Mutex>, clipboard_revision: AtomicU64, } @@ -205,7 +205,7 @@ impl HostControls { self.clipboard_from_client.lock().unwrap().take() } - fn receive_remote_clipboard(&self, text: String) { + pub(crate) fn receive_remote_clipboard(&self, text: String) { if text.len() <= 60_000 { *self.clipboard_from_client.lock().unwrap() = Some(text); } diff --git a/docs/PROGRESS.md b/docs/PROGRESS.md index f5f057d..8ef8d9a 100644 --- a/docs/PROGRESS.md +++ b/docs/PROGRESS.md @@ -119,6 +119,8 @@ Per-platform beam/watch/control status lives in [PLATFORMS.md](PLATFORMS.md). ## Decisions log +- 2026-11-05 — **The headless Windows host now bridges the OS clipboard.** Text clipboard sync (2026-07-17) only ever worked when the Flutter app was the host, because its Dart shell is what polls the platform clipboard and calls `publish_clipboard`/`take_remote_clipboard`. A real Windows box like Mancave runs the `streambeam-svc`-supervised `streambeam-host.exe` worker, and the in-app host stands down while that service runs, so on the deployment that matters both directions silently did nothing: viewer text was queued and never taken, and host copies were never published. New `crates/host/src/clipboard.rs` is the missing poller for that binary: a platform-neutral `ClipboardBridge` (750 ms, same as the app shell; de-duplicates against the last value to stop echo; retries a write when another process holds the clipboard) over a `SystemClipboard` trait, with a Win32 `CF_UNICODETEXT` backend that skips reads while `GetClipboardSequenceNumber` is unchanged. It runs only while a viewer owns the stream, and under the SYSTEM worker only while the input desktop is `Default`: the clipboard is per window station, so the lock screen could otherwise hand out what was copied before the machine locked. Viewer text that arrives while locked is kept and applied after unlock. No wire change. Bridge logic has six unit tests; the Win32 FFI type-checks against `windows-sys` 0.61 for `x86_64-pc-windows-msvc`, but the full host crate could not be checked for Windows from Linux (`ring` needs MSVC headers) and nothing is verified on real hardware yet. + - 2026-10-21 — **Record user-confirmed Proton Pass storage.** The publisher confirmed saving all four prepared files: Apple-compatible distribution P12, its password, App Store provisioning profile and private backup inventory. No vault restore test was performed. The missing App Store Connect API `.p8` is not backed up and still blocks upload access; retain local originals. - 2026-10-21 — **Deploy the verified Windows artifact with an explicitly approved process-only exception.** The publisher approved one execution-policy exception for the prepared updater; persistent AllSigned remains unchanged. Installed exact `5a99a27` artifact `12348791793` on Mancave with a rollback copy retained. Worker SHA-256 `30f42e412d10e3aad0e8d7c7a8b2b981c654080ca868d97aea0895028e2eb94d`, app and Rust DLL match the artifact; the existing supervisor, worker configuration, host identity and pairings are unchanged. The supervisor confirmed the new worker stayed up, with UDP 47989 listening. A real updated-Mac to updated-Mancave stream displayed nonblank 3840×2160 AV1; stopping returned to Ready with MANCAVE still Saved. No remote-input or subjective-audio acceptance is claimed. Proton Pass is the chosen key-backup destination; login and vault selection remain pending. diff --git a/docs/PROTOCOL.md b/docs/PROTOCOL.md index cc42473..a36c70d 100644 --- a/docs/PROTOCOL.md +++ b/docs/PROTOCOL.md @@ -192,7 +192,9 @@ unprivileged AccessibilityService cannot inject a native mouse cursor. Text clipboard sync is active only for the authenticated device currently in a stream session. Client→host updates use S1; host→client updates use S0. Both -sides de-duplicate the last applied value to prevent echo loops. Images, files, +sides de-duplicate the last applied value to prevent echo loops. A Windows +host does not read or write its clipboard while the secure desktop (lock or +login screen) has input; viewer text received meanwhile is applied after unlock. Images, files, and rich clipboard formats are not part of v3. ## Discovery