From 63ebb3e78af3d839382307c0503f47973cd92deb Mon Sep 17 00:00:00 2001 From: VallierDev <24893073+VallierDev@users.noreply.github.com> Date: Sun, 18 Oct 2026 09:41:56 +0800 Subject: [PATCH] Release v0.2.7: mobile text_input (#13, #46); platform-aware viewer clipboard Host: accept the official text_input {content} action on any channel and type it on X11 via spare-keycode Unicode keysyms (verified with xev); log non-CONTROL JSON action names. Viewer: Windows-type hosts get only the case-10 text change (a rich format list overwrote it and was never rendered), Mac gets only the rich offer. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 2 +- Cargo.toml | 2 +- c_engine/x11_engine.c | 49 ++++++++++++++++++++++++++++++++++++ docs/release-notes/v0.2.7.md | 25 ++++++++++++++++++ src/control_protocol.rs | 11 ++++++++ src/input.rs | 17 +++++++++++++ src/viewer/clipboard.rs | 29 +++++++++++++++------ src/viewer_main.rs | 10 ++++---- src/webrtc.rs | 18 ++++++++++++- 9 files changed, 148 insertions(+), 15 deletions(-) create mode 100644 docs/release-notes/v0.2.7.md diff --git a/Cargo.lock b/Cargo.lock index 26688ca..33f8270 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2741,7 +2741,7 @@ checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" [[package]] name = "uu-remote-linux" -version = "0.2.6" +version = "0.2.7" dependencies = [ "async-trait", "base64 0.22.1", diff --git a/Cargo.toml b/Cargo.toml index 7beb8cc..e471214 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uu-remote-linux" -version = "0.2.6" +version = "0.2.7" edition = "2021" default-run = "uuremote-server" authors = ["uu-remote-linux contributors"] diff --git a/c_engine/x11_engine.c b/c_engine/x11_engine.c index 936d444..5a3f44b 100644 --- a/c_engine/x11_engine.c +++ b/c_engine/x11_engine.c @@ -3,6 +3,7 @@ * Zero-Wine dependency, zero-overhead, thread-safe. */ #include +#include #include #include #include @@ -218,6 +219,54 @@ int x11_key_symbol(unsigned long symbol, int is_press) { return 0; } +/* Type UTF-8 text the way xdotool does: bind each character's keysym to a spare (unmapped) keycode, press it, + * then restore the keycode. Works for CJK text from mobile input methods. Never logs the text. */ +int x11_type_utf8(const char *text) { + if (!g_display || !text) return -1; + int min_kc = 0, max_kc = 0, per = 0; + XDisplayKeycodes(g_display, &min_kc, &max_kc); + KeySym *map = XGetKeyboardMapping(g_display, (KeyCode)min_kc, max_kc - min_kc + 1, &per); + if (!map) return -2; + int spare = 0; + for (int kc = max_kc; kc >= min_kc && !spare; kc--) { + int empty = 1; + for (int i = 0; i < per; i++) if (map[(kc - min_kc) * per + i] != NoSymbol) { empty = 0; break; } + if (empty) spare = kc; + } + XFree(map); + if (!spare) return -3; + int typed = 0; + const unsigned char *p = (const unsigned char *)text; + while (*p) { + unsigned cp; int n; + if (*p < 0x80) { cp = *p; n = 1; } + else if ((*p & 0xE0) == 0xC0 && (p[1] & 0xC0) == 0x80) { cp = ((p[0] & 0x1F) << 6) | (p[1] & 0x3F); n = 2; } + else if ((*p & 0xF0) == 0xE0 && (p[1] & 0xC0) == 0x80 && (p[2] & 0xC0) == 0x80) { cp = ((p[0] & 0x0F) << 12) | ((p[1] & 0x3F) << 6) | (p[2] & 0x3F); n = 3; } + else if ((*p & 0xF8) == 0xF0 && (p[1] & 0xC0) == 0x80 && (p[2] & 0xC0) == 0x80 && (p[3] & 0xC0) == 0x80) { cp = ((p[0] & 0x07) << 18) | ((p[1] & 0x3F) << 12) | ((p[2] & 0x3F) << 6) | (p[3] & 0x3F); n = 4; } + else { p++; continue; } + p += n; + KeySym sym; + if (cp == '\n') sym = 0xff0d; /* Return */ + else if (cp == '\t') sym = 0xff09; /* Tab */ + else if (cp < 0x20 || cp == 0x7f || (cp >= 0x80 && cp < 0xa0)) continue; + else if (cp < 0x100) sym = cp; /* Latin-1 keysyms equal the code point */ + else sym = 0x1000000 | cp; /* Unicode keysym */ + KeySym syms[2] = { sym, sym }; + XChangeKeyboardMapping(g_display, spare, 2, syms, 1); + XSync(g_display, False); + usleep(8000); /* let clients see the MappingNotify before the key */ + XTestFakeKeyEvent(g_display, spare, True, CurrentTime); + XTestFakeKeyEvent(g_display, spare, False, CurrentTime); + XSync(g_display, False); + usleep(8000); + typed++; + } + KeySym none[2] = { NoSymbol, NoSymbol }; + XChangeKeyboardMapping(g_display, spare, 2, none, 1); + XSync(g_display, False); + return typed; +} + int x11_key_event(int keycode, int is_press) { if (!g_display) return -1; if (is_press) track_injected_key(keycode, 1); diff --git a/docs/release-notes/v0.2.7.md b/docs/release-notes/v0.2.7.md new file mode 100644 index 0000000..09765f3 --- /dev/null +++ b/docs/release-notes/v0.2.7.md @@ -0,0 +1,25 @@ +手机输入法打字(实验);控制端 → 官方 Windows 被控端剪贴板修复。/ Mobile input-method text (experimental); viewer → Windows host clipboard fixed. + +### 主机端 / Host +- **手机自带输入法打字**(#13、#46):官方 Windows 被控端用 `text_input` 动作接收手机输入法的文字。本版主机端支持这个动作, + 在 X11 上把文字(含中文)直接输入到当前窗口;不论它从哪个数据通道到达都会处理。 + ⚠️ 文字注入已在 X11 上实测(中英文、空格、回车均正确);**手机端确实发送 `text_input` 是根据官方 Windows 被控端推断的, + 尚未用手机实测**。Wayland / 门户后端暂不支持,会记录一次警告。 + 诊断:非 CONTROL 通道收到的输入动作会记录动作名(不含内容)。 +- Mobile input-method text via the official `text_input {content}` action, typed on X11 (verified by injection; the mobile + client sending it is inferred from the official Windows host, not yet confirmed with a phone). + +### 控制端修复 / Viewer — fixed +- **控制端 → 官方 Windows 被控端剪贴板**:之前被控端确认收到,但粘贴为空。原因是控制端同时发送的「富剪贴板格式通告」 + 会覆盖刚写入的文本,而 Windows 被控端之后从不请求数据。现在按被控端平台发送:Windows 及本项目主机只发文本变更, + Mac 只发格式通告,平台未知时两者都发。已实测:Windows 被控端「运行」框可粘贴,官方 Mac 被控端正常。 + Viewer → official Windows host clipboard now works (verified live); Mac unchanged and verified. + +### 社区贡献 / Community +- **Mac SSH 粘贴助手**(#45,感谢 @jo32):`contrib/macos-ssh/`,在 Ghostty 里经 UU 端口映射用 SSH 时粘贴 Finder 文件会先上传再插入路径。 + 可选工具,维护者尚未实机验收。Optional contrib tool, not verified by the maintainer. + +### 下载 / Download +`uu-remote-linux-v0.2.7-x86_64-linux.tar.gz`:主机 `uuremote-server`(X11 版)与控制端 `uuremote-viewer`(x86_64,glibc ≥ 2.35;控制端需 SDL2、GTK3、ffmpeg),附带 `contrib/`。 + +**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.** diff --git a/src/control_protocol.rs b/src/control_protocol.rs index 9b2acfc..6f9962f 100644 --- a/src/control_protocol.rs +++ b/src/control_protocol.rs @@ -15,6 +15,9 @@ pub enum ControlInput { MouseAbsolute { x: f64, y: f64 }, MouseRelative { dx: i32, dy: i32 }, MouseScroll { dx: i32, dy: i32 }, + /// Official Windows host `text_input {content}` (KeyboardMouseRunner::execTextInput): text typed by the + /// mobile clients' own input method. Never logged. + TextInput { text: String }, } #[derive(Debug, Clone, PartialEq)] @@ -58,6 +61,11 @@ pub fn decode_control(data: &[u8]) -> Result { "mouse_move_relative" => ControlInput::MouseRelative { dx: integer(&v, "delta_x")?, dy: integer(&v, "delta_y")?, }, + "text_input" => { + let text = v.get("content").and_then(Value::as_str).ok_or("CONTROL field content must be a string")?; + if text.len() > 16 * 1024 { return Err("text_input content exceeds 16 KiB".into()); } + ControlInput::TextInput { text: text.to_owned() } + } "mouse_scroll" => ControlInput::MouseScroll { dx: integer(&v, "delta_x")?, dy: integer(&v, "delta_y")?, }, @@ -137,6 +145,9 @@ mod tests { #[test] fn does_not_invent_actions_for_unknown_json() { assert!(matches!(decode_control(br#"{"action":"unverified"}"#).unwrap(), ControlMessage::Unsupported(_))); + assert_eq!(decode_control("{\"action\":\"text_input\",\"content\":\"你好 a\"}".as_bytes()).unwrap(), + ControlMessage::Input(ControlInput::TextInput { text: "你好 a".into() })); + assert!(decode_control(br#"{"action":"text_input"}"#).is_err()); } } diff --git a/src/input.rs b/src/input.rs index 358dc84..7c78232 100644 --- a/src/input.rs +++ b/src/input.rs @@ -10,9 +10,12 @@ extern "C" { fn x11_key_event(keycode: libc::c_int, is_press: libc::c_int) -> libc::c_int; fn x11_release_all() -> libc::c_int; fn x11_terminal_shortcut_state() -> libc::c_int; + fn x11_type_utf8(text: *const libc::c_char) -> libc::c_int; } #[cfg(not(target_os = "linux"))] unsafe fn x11_terminal_shortcut_state() -> libc::c_int { 0 } +#[cfg(not(target_os = "linux"))] +unsafe fn x11_type_utf8(_: *const libc::c_char) -> libc::c_int { 0 } #[cfg(not(target_os = "linux"))] unsafe fn x11_mouse_move(_: libc::c_int, _: libc::c_int) -> libc::c_int { 0 } @@ -357,6 +360,20 @@ impl InputInjector { if self.backend==crate::desktop_backend::DesktopBackend::Wayland { crate::wayland::key(symbol,pressed) } else { unsafe { x11_key_symbol(symbol as libc::c_ulong,i32::from(pressed)) } } } + /// Mobile input-method text (`text_input`). X11 only for now; other backends log once. + pub fn text_input(&self, text: &str) { + let _serial=self.serial.lock().unwrap(); + let Ok(c_text)=std::ffi::CString::new(text.replace('\0',"")) else { return }; + if self.backend==crate::desktop_backend::DesktopBackend::X11 { + let typed=unsafe { x11_type_utf8(c_text.as_ptr()) }; + log::info!("Desktop text input: chars={} result={}", text.chars().count(), typed); + } else { + static WARNED:std::sync::atomic::AtomicBool=std::sync::atomic::AtomicBool::new(false); + if !WARNED.swap(true,std::sync::atomic::Ordering::Relaxed) { + log::warn!("Mobile text input is not supported on the {:?} backend yet", self.backend); + } + } + } pub fn key_event(&self, keycode: i32, is_press: bool) { let _serial=self.serial.lock().unwrap(); if self.backend!=crate::desktop_backend::DesktopBackend::X11 { return; } diff --git a/src/viewer/clipboard.rs b/src/viewer/clipboard.rs index 0c16b6c..aa3434d 100644 --- a/src/viewer/clipboard.rs +++ b/src/viewer/clipboard.rs @@ -95,7 +95,8 @@ impl CommandBackend { } } #[derive(Default)] -struct SyncState {last:Option,assembly:text::Assembly,rich:rich::State,seq:u64} +// target_mac: Some(true) official Mac host, Some(false) Windows-type host (official or this project), None unknown. +struct SyncState {last:Option,assembly:text::Assembly,rich:rich::State,seq:u64,target_mac:Option} impl SyncState { async fn apply(&mut self,backend:&Backend,value:String)->Result<(),()>{ if backend.write(&value).await? { @@ -114,7 +115,10 @@ impl SyncState { let id=self.next();let payload=self.rich.offer_any_host(id,value.clone())?; let rich=wire::envelope(self.next(),21,&payload); let text=wire::envelope(self.next(),21,&crate::clipboard_protocol::text_request(self.next(),&value)); - self.last=Some(value);Ok(Some(vec![rich,text])) + self.last=Some(value); + // Live-verified: the official Windows host writes case-10 text, then a rich format list replaces it with a + // placeholder it never asks us to render (paste is empty). The Mac host only takes the rich offer. + Ok(Some(match self.target_mac {Some(true)=>vec![rich],Some(false)=>vec![text],None=>vec![rich,text]})) } fn receive(&mut self,data:&[u8])->Result<(Vec>,Option),&'static str>{ if data.len()>MAX_TEXT*2+1024{return Err("clipboard message exceeds limit")} @@ -151,23 +155,24 @@ impl SyncState { async fn send(channel:&RTCDataChannel,message:Vec)->Result<(),()>{ tokio::time::timeout(Duration::from_secs(2),channel.send(&bytes::Bytes::from(message))).await.map_err(|_|())?.map_err(|_|())?;Ok(()) } -pub struct Bridge {backend:Option,pub enabled:bool,tx:Option>>,task:Option>} +pub struct Bridge {backend:Option,pub enabled:bool,tx:Option>>,task:Option>,target_mac:Option} impl Bridge { pub fn new(desktop:bool)->Self{ let backend=if desktop{Backend::detect()}else{None};let enabled=backend.is_some(); - Self{backend,enabled,tx:None,task:None} + Self{backend,enabled,tx:None,task:None,target_mac:None} } #[cfg(feature="viewer-sdl")] pub fn sdl(tx:mpsc::Sender)->Self{ let backend=(std::env::var("UU_VIEWER_CLIPBOARD").as_deref()!=Ok("0")).then_some(Backend::Sdl(tx)); - Self{enabled:backend.is_some(),backend,tx:None,task:None} + Self{enabled:backend.is_some(),backend,tx:None,task:None,target_mac:None} } + pub fn set_target_mac(&mut self,mac:Option){self.target_mac=mac;} pub fn start(&mut self,channel:&Arc,control:&Arc,observed:Arc){ - let Some(backend)=self.backend.take() else{return}; + let Some(backend)=self.backend.take() else{return};let target_mac=self.target_mac; let (tx,mut rx)=mpsc::channel::>(16);self.tx=Some(tx); let channel=Arc::downgrade(channel);let control=Arc::downgrade(control); self.task=Some(tokio::spawn(async move{ - let mut state=SyncState::default();let mut tick=tokio::time::interval(POLL); + let mut state=SyncState{target_mac,..SyncState::default()};let mut tick=tokio::time::interval(POLL); let mut retries=0;let mut last_probe=std::time::Instant::now(); tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip); loop { @@ -325,6 +330,16 @@ impl Drop for Bridge{fn drop(&mut self){self.stop();}} if reverse && received.is_some(){viewer.last=Some(value.clone());assert!(viewer.local(value).unwrap().is_none());} (received,unavailable) } + #[test] fn local_change_follows_target_platform(){ + for (mac,kinds) in [(Some(true),vec![9]),(Some(false),vec![10]),(None,vec![9,10])]{ + let mut viewer=SyncState{target_mac:mac,..SyncState::default()}; + let cases:Vec=viewer.local("text".into()).unwrap().unwrap().iter().map(|m|{ + let rpc=decode_wire(m).unwrap().into_iter().find_map(|f|match (f.number,f.value){(21,WireValue::Bytes(b))=>Some(b.to_vec()),_=>None}).unwrap(); + decode_wire(&rpc).unwrap().iter().map(|f|f.number).find(|n|*n!=1).unwrap() + }).collect(); + assert_eq!(cases,kinds,"{mac:?}"); + } + } #[test] fn local_change_also_sends_windows_text_change_request(){ let mut viewer=SyncState::default(); let messages=viewer.local("windows 中文".into()).unwrap().unwrap();assert_eq!(messages.len(),2); diff --git a/src/viewer_main.rs b/src/viewer_main.rs index 5f12683..ace0547 100644 --- a/src/viewer_main.rs +++ b/src/viewer_main.rs @@ -93,21 +93,21 @@ async fn desktop_command(args:&[String],code:Option)->Result<(),String> }else{None}; let creds=credentials()?; let api=Api::new(auth::UUAuthClient::new(creds.clone()),auth::BASE_URL); - #[cfg(feature="viewer-sdl")] let account_connection=code.is_none(); let room=match code { Some(code)=>api.join_share(&args[1],&code).await, None=>api.join(&args[1],force,true).await, }.map_err(|e|e.to_string())?; + let target_mac=if shape.capture_type==1 {api.target_is_mac(&room,account_connection.then_some(args[1].as_str())).await} else {Err(viewer::api::Error::UnknownKeyboardPlatform)}; #[cfg(feature="viewer-sdl")] if let Some((window,_))=desktop.as_mut().filter(|_|!view_only){ - let device=account_connection.then_some(args[1].as_str()); - window.set_mac(api.target_is_mac(&room,device).await.map_err(|e|e.to_string())?); + window.set_mac(*target_mac.as_ref().map_err(|e|e.to_string())?); } #[cfg(feature="viewer-sdl")] - let clipboard=desktop.as_ref().map(|(window,_)|window.clipboard_bridge()).unwrap_or_else(||clipboard::Bridge::new(false)); + let mut clipboard=desktop.as_ref().map(|(window,_)|window.clipboard_bridge()).unwrap_or_else(||clipboard::Bridge::new(false)); #[cfg(not(feature="viewer-sdl"))] - let clipboard=clipboard::Bridge::new(shape.capture_type==1); + let mut clipboard=clipboard::Bridge::new(shape.capture_type==1); + clipboard.set_target_mac(target_mac.ok()); let options=Options{timeout:Duration::from_secs(12),legacy_prefix:legacy,ready_policy:policy,clipboard}; let mut session=Session::open(&room,&shape,&creds.device_id,&creds.client_id,options,Default::default()).await?; #[cfg(feature="viewer-sdl")] diff --git a/src/webrtc.rs b/src/webrtc.rs index d3a14c6..dd2fb2b 100644 --- a/src/webrtc.rs +++ b/src/webrtc.rs @@ -1041,6 +1041,21 @@ impl WebRtcSession { if let Ok(txt) = std::str::from_utf8(&msg.data) { log::debug!("DataChannel [{}] received UTF-8 data: {} bytes", channel_name, txt.len()); if let Ok(val) = serde_json::from_str::(txt) { + if !channel_name.contains("CONTROL") { + // Input JSON on another channel (mobile input-method text was missing from CONTROL, #13/#46): + // record action names only, once per name, and accept text_input wherever it arrives. + if let Some(action) = val.get("action").and_then(serde_json::Value::as_str) { + static SEEN: std::sync::Mutex> = std::sync::Mutex::new(Vec::new()); + let mut seen = SEEN.lock().unwrap(); + if seen.len() < 32 && action.len() <= 40 && action.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_') && !seen.iter().any(|s| s == action) { + seen.push(action.to_owned()); + log::info!("JSON action on {} channel: action={}", if channel_name.contains("STREAMER") {"STREAMER"} else if channel_name.contains("TEXT") {"TEXT"} else {"other"}, action); + } + } + if let Ok(crate::control_protocol::ControlMessage::Input(crate::control_protocol::ControlInput::TextInput {text})) = crate::control_protocol::decode_control(&msg.data) { + if let Some(session)=message_input_session.as_ref() { session.with_input(|| input_worker.text_input(&text)); } + } + } if channel_name.contains("CONTROL") { match crate::control_protocol::decode_control(&msg.data) { Ok(crate::control_protocol::ControlMessage::Input(event)) => { @@ -1051,7 +1066,7 @@ impl WebRtcSession { static RECEIPTS: [std::sync::atomic::AtomicUsize; 3] = [const { std::sync::atomic::AtomicUsize::new(0) }; 3]; let (slot, kind) = match &event { ControlInput::MouseButton {..} | ControlInput::MouseClick {..} => (0, "button"), - ControlInput::Keyboard {..} | ControlInput::KeyboardClick {..} => (1, "keyboard"), + ControlInput::Keyboard {..} | ControlInput::KeyboardClick {..} | ControlInput::TextInput {..} => (1, "keyboard"), _ => (2, "motion"), }; if RECEIPTS[slot].fetch_add(1, std::sync::atomic::Ordering::Relaxed) < 16 { @@ -1063,6 +1078,7 @@ impl WebRtcSession { ControlInput::MouseRelative {dx,dy} => input_worker.mouse_relative(*dx,*dy), ControlInput::MouseScroll {dx,dy} => input_worker.mouse_scroll(*dx,*dy), ControlInput::Keyboard {key, pressed} => input_worker.windows_key_event(*key, *pressed), + ControlInput::TextInput {text} => input_worker.text_input(text), ControlInput::KeyboardClick {key} => {input_worker.windows_key_event(*key, true);input_worker.windows_key_event(*key, false);}, ControlInput::MouseButton {button,pressed} => input_worker.wire_mouse_button(*button,*pressed), ControlInput::MouseClick {button} => {input_worker.wire_mouse_button(*button,true);input_worker.wire_mouse_button(*button,false);},