From 9271904be6e6d7528a80bec333d089fc15c3b676 Mon Sep 17 00:00:00 2001 From: "Mr. Error" Date: Sat, 7 Nov 2026 18:40:49 +0800 Subject: [PATCH] fix(privacy): accept the iOS screen-off encoding (#199) The iOS viewer sends PrivateScreenSettingRequest{enable=0, privacy_type=3} when the user turns the display off: its enable bit is the picture-mode switch and the explicit type carries the intent. The host treated enable=0 as off, so the screen was restored instead of turned off. Normalize that combination for iOS controllers only. Android 4.43.0 was verified with the field-1 semantics, so keep Android and desktop viewers unchanged. Record the platform after a desktop session is created, update it on capture_desktop promotion, and cancel a pending UU_PRIVACY_AUTO even when the switch is refused. --- docs/privacy-screen.md | 2 ++ src/input.rs | 23 ++++++++++++++++------ src/main.rs | 2 ++ src/privacy_screen/mod.rs | 15 ++++++++++++++- src/privacy_screen/protocol.rs | 35 ++++++++++++++++++++++++++++++++++ 5 files changed, 70 insertions(+), 7 deletions(-) diff --git a/docs/privacy-screen.md b/docs/privacy-screen.md index a40dcc4..ca393de 100644 --- a/docs/privacy-screen.md +++ b/docs/privacy-screen.md @@ -79,6 +79,8 @@ is aimed at the virtual output's region. Needs GTK3. The host answers before switching (the viewer times out with 9998); a failed switch sends ReportError -7. 成功不发送 ReportError;Mac 4.43.0 的 -9 是系统会话变化提示,不是防窥成功通知。 Success does not send ReportError; -9 in Mac 4.43.0 reports a system session change, not privacy success. +- iOS 控制端选“关闭显示器”发 `enable=0, privacy_type=3`:iOS 的 `enable` 是图片模式开关,关屏意图由 `type=3` 表达;iOS“不启用”发 `enable=0, privacy_type=1`。主机只对 iOS 控制端把 `0/3` 归一化为开屏保关屏(日志 `iOS screen-off encoding normalized`),其他控制端(包括已验收的 Android 4.43.0)保持字段 1 的语义。 + The iOS viewer sends `enable=0, privacy_type=3` for "turn off the display": its `enable` is the picture-mode switch and `type=3` carries the intent; iOS "off" is `enable=0, privacy_type=1`. The host normalizes `0/3` to screen-off for iOS controllers only (log: `iOS screen-off encoding normalized`); other controllers, including the verified Android 4.43.0, keep the field-1 semantics. - RPC 25 `update_screen_saver_req` → RpcResponse 21:开启隐私模式前发送,主机回空成功应答,画面仍用本机壁纸。 Sent before picture mode; answered empty (success), the host keeps its own wallpaper. - FeatureFlag 4 `ff_private_screen` = 2 (ENHANCED_PRIVACY);SettingMetric 4 `private_screen`、7 `private_screen_type` diff --git a/src/input.rs b/src/input.rs index fc5eff3..507945b 100644 --- a/src/input.rs +++ b/src/input.rs @@ -81,15 +81,20 @@ fn finger_held(finger: Finger, now: std::time::Instant) -> Option<(i32,i32)> { /// Viewer platform from the connect header `controller_platform` (website enum: 1 windows, 2 android, /// 3 ios, 4 macos, 5 tv). #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] -pub enum Controller { #[default] Other, Mac, Mobile } +pub enum Controller { #[default] Other, Mac, Mobile, Ios } impl Controller { + /// Touch semantics cover both mobile platforms; the iOS RPC 7 screen-off encoding is its own quirk. + pub fn is_mobile(self) -> bool { matches!(self, Self::Mobile | Self::Ios) } pub fn from_platform(platform: Option<&serde_json::Value>) -> Self { let Some(v) = platform else { return Self::Other }; let text = v.as_str().map(str::to_ascii_lowercase); match v.as_i64().or_else(|| text.as_deref().and_then(|s| s.parse().ok())) { Some(4) => Self::Mac, - Some(2 | 3) => Self::Mobile, - _ if text.is_some_and(|s| s.contains("mac") || s.contains("darwin")) => Self::Mac, + Some(3) => Self::Ios, + Some(2) => Self::Mobile, + _ if text.as_deref().is_some_and(|s| s.contains("mac") || s.contains("darwin")) => Self::Mac, + _ if text.as_deref().is_some_and(|s| s.contains("ios") || s.contains("iphone") || s.contains("ipad")) => Self::Ios, + _ if text.as_deref().is_some_and(|s| s.contains("android")) => Self::Mobile, _ => Self::Other, } } @@ -303,7 +308,7 @@ impl InputInjector { *self.cmd.lock().unwrap() = CmdState::default(); let mut touch = self.touch.lock().unwrap(); self.lift_finger(&mut touch); - let mode = if controller != Controller::Mobile || self.uinput_pointer { TouchMode::Mouse } else { + let mode = if !controller.is_mobile() || self.uinput_pointer { TouchMode::Mouse } else { match std::env::var("UU_TOUCH_INPUT").as_deref() { Ok("0") => TouchMode::Mouse, Ok("touch") => TouchMode::Touch, _ => TouchMode::Gesture } }; *touch = TouchDrag { mode, ..TouchDrag::default() }; @@ -1047,7 +1052,7 @@ mod kernel_pointer_tests { #[test] fn full_uinput_bypasses_portal_binding_and_preserves_move_click_order() { - for controller in [Controller::Other, Controller::Mobile] { + for controller in [Controller::Other, Controller::Mobile, Controller::Ios] { let (devices, mut keyboard, mut pointer) = crate::uinput::Devices::recording_for_test(); let mut input = InputInjector::without_desktop(); input.uinput = Some(devices); @@ -1296,7 +1301,13 @@ mod touch_tests { fn controller_from_platform_numbers_and_mac_strings() { use serde_json::json; assert_eq!(Controller::from_platform(Some(&json!(2))), Controller::Mobile); - assert_eq!(Controller::from_platform(Some(&json!("3"))), Controller::Mobile); + assert_eq!(Controller::from_platform(Some(&json!("3"))), Controller::Ios); + assert_eq!(Controller::from_platform(Some(&json!("ios"))), Controller::Ios); + assert_eq!(Controller::from_platform(Some(&json!("Android"))), Controller::Mobile); + assert_eq!(Controller::from_platform(Some(&json!("iPadOS"))), Controller::Ios); + assert!(Controller::Ios.is_mobile()); + assert!(Controller::Mobile.is_mobile()); + assert!(!Controller::Mac.is_mobile()); assert_eq!(Controller::from_platform(Some(&json!(4))), Controller::Mac); assert_eq!(Controller::from_platform(Some(&json!("MacOS"))), Controller::Mac); assert_eq!(Controller::from_platform(Some(&json!(1))), Controller::Other); diff --git a/src/main.rs b/src/main.rs index 382ff3d..f4428b5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1038,6 +1038,7 @@ async fn serve(mut capture: Arc, input: Arc, shutd if desktop { portal_desktop.clone() } else { None }).await { Ok(mut sess) => { sess.controller = controller; + if desktop { crate::privacy_screen::set_viewer_controller(controller); } if !desktop { let (requests, peer) = (desktop_requests.clone(), ice.clone()); sess.on_desktop_request(move || { let _ = requests.send(peer.clone()); }); @@ -1119,6 +1120,7 @@ async fn serve(mut capture: Arc, input: Arc, shutd } else { None }; video::warm_auto_probe(); video::check_ffmpeg_usable(); input.set_controller(controller); + crate::privacy_screen::set_viewer_controller(controller); input.bind_portal(screen.as_ref().map_or_else(|| sessions[&ice].capture.portal.clone(), |screen| screen.portal.clone())); // One desktop at a time, as for a new desktop offer. Peers offered as desktops // close; a promoted tool peer only stops its desktop and keeps its tools. diff --git a/src/privacy_screen/mod.rs b/src/privacy_screen/mod.rs index b34b021..6861fea 100644 --- a/src/privacy_screen/mod.rs +++ b/src/privacy_screen/mod.rs @@ -25,8 +25,15 @@ static POINTER: Mutex> = Mutex::new(None); /// ends. An auto-enable never overrides it, including an RPC that arrives after the connection /// opens but before `session_started` bumps the counter. static VIEWER_OVERRIDE: AtomicBool = AtomicBool::new(false); +/// iOS viewers encode "screen off" as `enable=0, type=3`; see +/// `protocol::Request::normalize_ios_screen_off`. +static VIEWER_IOS: AtomicBool = AtomicBool::new(false); pub fn supported() -> bool { backend::detect().is_some() } +/// Record the desktop controller's platform for the iOS RPC 7 encoder quirk. +pub fn set_viewer_controller(controller: crate::input::Controller) { + VIEWER_IOS.store(controller == crate::input::Controller::Ios, Ordering::Relaxed); +} pub fn active() -> bool { ACTIVE.load(Ordering::Relaxed) } /// The viewer's privacy_type now in effect. pub fn current_type() -> u8 { @@ -112,7 +119,13 @@ pub struct Accepted { enable: bool, mode: Option } /// (error 9998 + n in its RPC failure handler) and a switch takes longer. A later failure is /// reported as ReportError -7, which the viewer handles by resetting its privacy menu. pub fn rpc_accept(rpc: &[crate::protocol::WireField]) -> Option<(Vec, Option)> { - let (header, request) = protocol::parse(rpc)?; + let (header, mut request) = protocol::parse(rpc)?; + if request.normalize_ios_screen_off(VIEWER_IOS.load(Ordering::Relaxed)) { + // The viewer chose a mode even if the switch is refused below; cancel a pending auto-enable + // as an accepted request would. + VIEWER_OVERRIDE.store(true, Ordering::Relaxed); + log::info!("Privacy request: iOS screen-off encoding normalized (enable=0 type=3)"); + } if let Some(e) = refuse(request.enable, request.mode) { log::warn!("Privacy request refused: enable={} mode={:?}: {e}", request.enable, request.mode); return Some((protocol::response(&header, &request, true, current_type()), None)); diff --git a/src/privacy_screen/protocol.rs b/src/privacy_screen/protocol.rs index 3ebfb67..c63531b 100644 --- a/src/privacy_screen/protocol.rs +++ b/src/privacy_screen/protocol.rs @@ -5,6 +5,20 @@ use super::Mode; pub struct Request { pub enable: bool, pub mode: Option, requested: u64 } +impl Request { + /// iOS viewers encode "turn off the display" as `enable=0, privacy_type=3`: their enable bit is + /// the picture-mode switch, and the explicit screen-off type carries the intent. iOS "off" is + /// `enable=0, type=1` (seen live), so rewrite only this exact combination. Android 4.43.0 was + /// verified with the field-1 semantics, so the rewrite is iOS-only. + pub fn normalize_ios_screen_off(&mut self, ios: bool) -> bool { + if ios && !self.enable && self.mode == Some(Mode::ScreenOff) { + self.enable = true; + return true; + } + false + } +} + pub fn parse(rpc: &[crate::protocol::WireField]) -> Option<(Vec, Request)> { use crate::protocol::{decode_wire, WireValue}; let body = |n| rpc.iter().find_map(|f| match &f.value { WireValue::Bytes(b) if f.number == n => Some(*b), _ => None }); @@ -71,4 +85,25 @@ mod tests { let saver = crate::protocol::decode_wire(&[10, 2, 8, 7, 0xca, 1, 0]).unwrap(); assert_eq!(screensaver_response(&saver).unwrap(), [10, 2, 8, 7, 0xaa, 1, 0]); } + #[test] + fn normalizes_ios_screen_off_only() { + let screen_off = crate::protocol::decode_wire(&[10, 2, 8, 42, 58, 4, 8, 0, 16, 3]).unwrap(); + let (_, mut request) = parse(&screen_off).unwrap(); + assert!(request.normalize_ios_screen_off(true)); + assert!(request.enable); + assert_eq!(request.mode, Some(Mode::ScreenOff)); + let (_, mut request) = parse(&screen_off).unwrap(); + assert!(!request.normalize_ios_screen_off(false)); + assert!(!request.enable); + + // An iOS "off" (type 1) and a Mac-style enable (1,3) must stay untouched. + let off = crate::protocol::decode_wire(&[10, 2, 8, 42, 58, 4, 8, 0, 16, 1]).unwrap(); + let (_, mut request) = parse(&off).unwrap(); + assert!(!request.normalize_ios_screen_off(true)); + assert!(!request.enable); + let mac = crate::protocol::decode_wire(&[10, 2, 8, 42, 58, 4, 8, 1, 16, 3]).unwrap(); + let (_, mut request) = parse(&mac).unwrap(); + assert!(!request.normalize_ios_screen_off(true)); + assert!(request.enable); + } }