diff --git a/README.en.md b/README.en.md index b5094d5..e45348c 100644 --- a/README.en.md +++ b/README.en.md @@ -648,3 +648,11 @@ assistance and ISO / JIS keyboards have not been tested live. "—" = not tested yet. Only use reboot/shutdown on machines you can restart. **Not tested live**: H.265 / 4:4:4 with official hosts, multiple monitors. Logs carry field numbers, sizes and states only, never keystrokes, clipboard or file contents, codes or credentials. + +### Native gamepad and rumble (experimental) + +The SDL viewer forwards one mapped controller and ordinary two-motor rumble through the existing desktop CONTROL channel to a Windows host. +Default OFF: set `UU_VIEWER_NATIVE_GAMEPAD=1` to enable input; `UU_VIEWER_NATIVE_RUMBLE=0` disables feedback independently. +Guide forwarding, multiple controllers and adaptive triggers are unsupported; suspend, forced exit and network recovery are not comprehensively verified. +One reporting user tested input, phone-hotspot WAN, reconnect and rumble on one Windows/Steam Deck pair. +See [protocol evidence, design and limits](docs/deckpad-native-gamepad.md). diff --git a/README.md b/README.md index 48250a0..5f0f406 100644 --- a/README.md +++ b/README.md @@ -568,3 +568,11 @@ Mac 目标使用 macOS 原生键码,Windows / Linux 目标保留 Windows VK。 | Wayland 下的桌面窗口与剪贴板 | — | — | ✅ Hyprland、KDE;GNOME 窗口正常,剪贴板已于 v0.2.3 改为 SDL 直连(#37,最终版本未重新实机验收) | 「—」表示尚未测试。重启/关机只应在可以重启的机器上使用。**未实测**:官方被控端的 H.265 / 4:4:4、多显示器。日志只记录字段号、大小和状态,不记录键盘、剪贴板、文件内容、验证码或凭据。 + +### 原生手柄与震动(实验性) + +SDL 控制端可通过现有桌面 CONTROL 通道向 Windows 被控端转发一个映射手柄,并接收普通双路震动。 +默认关闭:`UU_VIEWER_NATIVE_GAMEPAD=1` 启用手柄,`UU_VIEWER_NATIVE_RUMBLE=0` 可独立关闭震动。 +不支持 Guide 键、多手柄和自适应扳机;休眠、强制退出及断网恢复尚未全面验证。 +一名报告用户在一组 Windows + Steam Deck 上测试了输入、手机热点、重连与震动。 +详见[协议证据、设计与限制](docs/deckpad-native-gamepad.md)。 diff --git a/docs/deckpad-native-gamepad.md b/docs/deckpad-native-gamepad.md new file mode 100644 index 0000000..09dce59 --- /dev/null +++ b/docs/deckpad-native-gamepad.md @@ -0,0 +1,103 @@ +# UU native gamepad experiment / 原生手柄实验 + +Status: experimental and default OFF; opt in with `UU_VIEWER_NATIVE_GAMEPAD=1`. +One reporting user tested input, phone-hotspot WAN, reconnect and rumble on one +Windows/Steam Deck pair; the upstream maintainer has not independently tested it. +This does not establish compatibility with every official UU version. + +## Evidence (2026-10-31) + +Official Windows installer 4.45.0.4318, obtained from NetEase's release endpoint +`https://api.nrd.nie.163.com/api/v1/release/dl/1?channel=gwqd`. +Only inspected locally; no NetEase binaries/assets are included in this project or deliverable. + +- `GameViewerServer.exe` SHA256 `af3a15c3887e06bfa9506971e01cd8ec58f41efe8ac9eb5831e31dae1fc1cef8` +- `GameViewer.exe` SHA256 `ef7112e173cdc6d74735c50292cf9750082ce351cdea60dc068fc2b60f3aa92e` +- All addresses below are RVAs, not absolute process addresses. Image base is 0x140000000. +- Lead: https://github.com/djkcyl/uu-enhance/blob/main/src/hooks.cpp identifies older + GamepadManager Connect/Disconnect/Update log strings. Its server.cpp identifies corresponding + GamepadManagerServer handlers. No hook code or DLL injection is used here. + +Receiver static inspection: + +| RVA | Observation | +| --- | --- | +| 0x5dea50 | Raw JSON handler skips heartbeat messages, reads `action`, routes prefix `gpd_` to dispatcher through thunk 0x657d0. | +| 0x5dde60 | Dispatcher recognizes `gpd_update`, `gpd_connect`, `gpd_disconnect`. Connect/disconnect read `index` as a byte. | +| 0x5ddb30 | State decoder requires `index`, `buttons`, `left_trigger`, `right_trigger`, `left_thumb_x`, `left_thumb_y`, `right_thumb_x`, `right_thumb_y`. | +| 0x5dfba0 | ViGEm output: low 16 bits of buttons, two 8-bit triggers and four 16-bit stick values copied into an XUSB report; target selected by index. Missing target attempts auto-connect; missing driver drops updates. | +| 0x5df5d0 | Vibration JSON builder: `action: gpd_vibration`, `index`, `left_motor`, `right_motor`; all three numeric values read as bytes. | +| 0x5dfe80 | ViGEm feedback callback constructs index/left/right byte record, calls vibration serializer, then 0x5df580 (via thunk 0x1c1e3) to forward JSON through its session interface. | + +Official viewer corroboration: + +- 0x10369d0 builds JSON object with `action: gpd_connect`, numeric `index`. +- 0x1036d20 builds disconnect object. +- 0x1037070 builds update object using exactly the receiver fields above, unsigned numeric + buttons/index/triggers and signed thumb values. +- Viewer 0xc1c630 dispatches raw JSON action `gpd_vibration` alongside cursor notifications. + Viewer 0x10331a0 parses index/left_motor/right_motor into three bytes. + +This supports sending JSON through the existing binary CONTROL input path (`Peer::send_input`), +with input reported working by the reporting user on their Windows/Deck pair. +No guessed new protobuf feature flags are advertised. Existing desktop negotiation is retained. + +## Implementation and limits + +`src/viewer/native_gamepad.rs` reads a single SDL GameController inside the desktop viewer. +It maps SDL buttons to XInput and reverses Y axes (including -32768 saturation), converts triggers +0..32767 to 0..255, uses slot 0, sends changes and a 100 ms state refresh. Guide button is omitted. +More than one mapped controller at discovery is deliberately ambiguous and none is opened. + +Focus loss, local control menu, view-only, unplug or normal shutdown: send neutral then disconnect. +On entry/re-entry, require released buttons/triggers/sticks before arming. Back+Start toggles pause. +Normal cleanup uses bounded send attempts. Host behavior after SIGKILL/network loss remains +unverified; no host-side input watchdog interval has been established. +Very short taps entirely between SDL samples can be missed. Multi-controller is not implemented. +Logged markers describe local activity only; CONNECT_QUEUED is not an ACK. + +## Rumble / 震动回传 + +`native_rumble.rs` parses the verified flat JSON `{action:"gpd_vibration",index:0,left_motor:0..255,right_motor:0..255}` +on the existing peer's CONTROL channel. It rejects malformed/duplicate fields, wrong types/ranges, +nonzero slot and messages over 512 bytes. The exact shape is backed by static inspection; receipt +on the CONTROL hook must still be verified on other Windows/Deck configurations; the reporting user has reported successful rumble on their pair. +Only the current focused, armed SDL controller enables its peer-local inbox; a single latest-value slot +prevents a backlog from replaying after a pause. Feedback for inactive sessions is discarded. + +Motor strength scales by 257 into SDL's 0..65535 range. SDL `set_rumble` receives left/low-frequency +and right/high-frequency values. Normal zero feedback stops immediately. Sustained rumble refreshes +every 250 ms with a finite 1000 ms hardware effect, because the host may send only state changes. +An active viewer keeps the last desired state until a new message or focus/connection/pause change; +there is no unsupported assumption that a quiet host must periodically repeat feedback. +Pending nonzero commands older than 500 ms are discarded; stop commands are always honored. +Pause, focus loss, view-only, unplug, detected peer disconnection and normal exit stop rumble and +discard the queue. A blocked/crashed viewer cannot keep renewing the 1000 ms SDL effect. Physical +network loss detection still depends on WebRTC; this is not a one-second network watchdog. +Output failure disables rumble until controller rediscovery/restart while preserving input. + +SDL API reference: https://wiki.libsdl.org/SDL2/SDL_GameControllerRumble . A successful SDL call +does not prove the hardware moved. Steam Input/SteamOS rumble configuration and physical Deck +feedback remain dependent on each installation. This implements ordinary two-motor rumble, not adaptive +triggers or arbitrary touchpad haptic waveforms. `UU_VIEWER_NATIVE_RUMBLE=0` disables feedback. + +Markers (no payload values): RUMBLE_SUPPORTED/UNAVAILABLE describe SDL capabilities; +RUMBLE_RECEIVED means a valid active-session message arrived; RUMBLE_APPLIED means SDL returned +success for a nonzero effect; RUMBLE_OUTPUT_FAILED means the output API failed. All have DECKPAD_ prefix. + +## Validation / 验证 + +Use the CI checks on the current PR head as the automated validation record. +The tests cover protocol fields and ranges, activation/release gating, bounded +feedback delivery, finite rumble effects and shared CONTROL delivery alongside +file operations. Automated tests do not exercise physical controller feedback +or establish official-host compatibility. + +自动验证以当前 PR 提交的 CI 结果为准;实机结果来自报告用户,不能视为上游维护者 +已验收,也不涵盖全部主机版本、休眠、强制退出和断网恢复场景。 + +## Attribution / AI assistance + +The native gamepad and rumble implementation, regression tests and this evidence document were +prepared with OpenAI Codex assistance. The user supplied requirements and reported the hardware +acceptance results. Existing project design, code and attribution belong to the upstream contributors. diff --git a/src/viewer/mod.rs b/src/viewer/mod.rs index ed0cd04..fd8438c 100644 --- a/src/viewer/mod.rs +++ b/src/viewer/mod.rs @@ -21,6 +21,8 @@ pub mod thumbs; #[cfg(uu_gtk)] pub mod host; pub mod tools; +pub mod native_gamepad; +pub mod native_rumble; pub mod clipboard; pub mod power; diff --git a/src/viewer/native_gamepad.rs b/src/viewer/native_gamepad.rs new file mode 100644 index 0000000..0de0d17 --- /dev/null +++ b/src/viewer/native_gamepad.rs @@ -0,0 +1,141 @@ +//! Opt-in official UU gamepad JSON, recovered from Windows 4.45.0.4318. +//! Evidence/RVAs and limits: docs/deckpad-native-gamepad.md. No port mapping or helper. +use serde_json::{json,Value}; +use std::time::{Duration,Instant}; + +#[derive(Clone,Copy,Debug,Default,PartialEq,Eq)] +pub struct State {pub buttons:u16,pub lx:i16,pub ly:i16,pub rx:i16,pub ry:i16,pub lt:u8,pub rt:u8} +impl State { + pub fn json(self)->Value{json!({"action":"gpd_update","index":0,"buttons":self.buttons, + "left_trigger":self.lt,"right_trigger":self.rt,"left_thumb_x":self.lx, + "left_thumb_y":self.ly,"right_thumb_x":self.rx,"right_thumb_y":self.ry})} + fn released(self)->bool{self.buttons==0 && self.lt<8 && self.rt<8 && + [self.lx,self.ly,self.rx,self.ry].iter().all(|v|i32::from(*v).abs()<5000)} +} +pub fn enabled()->bool{std::env::var("UU_VIEWER_NATIVE_GAMEPAD").as_deref()==Ok("1")} +pub fn connect()->Value{json!({"action":"gpd_connect","index":0})} +pub fn disconnect()->Value{json!({"action":"gpd_disconnect","index":0})} +fn y_axis(v:i16)->i16{(-i32::from(v)).clamp(-32768,32767) as i16} +fn trigger(v:i16)->u8{((i32::from(v).max(0)*255+16383)/32767) as u8} + +/// Local state machine: focus loss sends neutral then detach; re-entry requires release. +/// The caller stops the session on any send failure rather than queueing old states. +#[derive(Default)] +struct Gate {connected:bool,armed:bool,last:Option<(State,Instant)>} +impl Gate { + fn close(&mut self)->Vec{ + let out=if self.connected{vec![State::default().json(),disconnect()]}else{vec![]}; + *self=Self::default();out + } + fn step(&mut self,state:Option,active:bool,now:Instant)->Vec{ + let Some(state)=state.filter(|_|active) else{return self.close()}; + let mut out=Vec::new(); + if !self.connected{out.push(connect());out.push(State::default().json());self.connected=true;} + if !self.armed {if state.released(){self.armed=true;}else{return out}} + if self.last.is_none_or(|(s,t)|s!=state||now.duration_since(t)>=Duration::from_millis(100)){ + out.push(state.json());self.last=Some((state,now)); + } + out + } +} + +#[cfg(feature="viewer-sdl")] +pub struct NativeGamepad {sub:sdl2::GameControllerSubsystem,pad:Option,gate:Gate,next_scan:Instant,paused:bool,chord:bool, + feedback:super::native_rumble::Feedback,rumble:super::native_rumble::Playback,rumble_enabled:bool,rumble_failed:bool,received_once:bool,applied_once:bool} +#[cfg(feature="viewer-sdl")] +impl NativeGamepad { + pub fn new(sdl:&sdl2::Sdl,feedback:super::native_rumble::Feedback)->Result{ + let sub=sdl.game_controller()?; + eprintln!("DECKPAD_NATIVE_ENABLED"); + let rumble_enabled=std::env::var("UU_VIEWER_NATIVE_RUMBLE").as_deref()!=Ok("0"); + if rumble_enabled {eprintln!("DECKPAD_RUMBLE_ENABLED");} + Ok(Self{sub,pad:None,gate:Gate::default(),next_scan:Instant::now(),paused:false,chord:false, + feedback,rumble:Default::default(),rumble_enabled,rumble_failed:false,received_once:false,applied_once:false}) + } + pub fn poll(&mut self,active:bool)->Vec{ + use sdl2::controller::{Axis as A,Button as B}; + let now=Instant::now(); + if self.pad.as_ref().is_some_and(|p|!p.attached()) { + self.stop_feedback(); + self.pad=None;self.chord=false; + // Detach before discovery, even if a replacement is already available. + return self.gate.close(); + } + if self.pad.is_none() && now>=self.next_scan { + self.next_scan=now+Duration::from_secs(1); + if let Ok(n)=self.sub.num_joysticks(){ + let ids:Vec<_>=(0..n).filter(|&i|self.sub.is_game_controller(i)).collect(); + if ids.len()==1 {self.pad=self.sub.open(ids[0]).ok();if let Some(pad)=self.pad.as_ref(){ + self.rumble_failed=false;self.received_once=false;self.applied_once=false; + eprintln!("DECKPAD_NATIVE_CONTROLLER_OPEN"); + if self.rumble_enabled {eprintln!("{}",if pad.has_rumble(){"DECKPAD_RUMBLE_SUPPORTED"}else{"DECKPAD_RUMBLE_UNAVAILABLE"});} + }} + } + } + let state=self.pad.as_ref().map(|p|{ + let mut buttons=0; + for (b,mask) in [(B::A,0x1000),(B::B,0x2000),(B::X,0x4000),(B::Y,0x8000), + (B::Back,0x20),(B::Start,0x10),(B::LeftStick,0x40),(B::RightStick,0x80), + (B::LeftShoulder,0x100),(B::RightShoulder,0x200),(B::DPadUp,1),(B::DPadDown,2),(B::DPadLeft,4),(B::DPadRight,8)] { + if p.button(b){buttons|=mask;} + } + State{buttons,lx:p.axis(A::LeftX),ly:y_axis(p.axis(A::LeftY)),rx:p.axis(A::RightX),ry:y_axis(p.axis(A::RightY)),lt:trigger(p.axis(A::TriggerLeft)),rt:trigger(p.axis(A::TriggerRight))} + }); + let chord=state.is_some_and(|s|s.buttons&0x30==0x30); + if active && chord && !self.chord {self.paused=!self.paused;eprintln!("{}",if self.paused{"DECKPAD_NATIVE_PAUSED"}else{"DECKPAD_NATIVE_RESUMING"});} + self.chord=chord; + let connected=self.gate.connected; + let out=self.gate.step(state,active&&!self.paused&&!chord,now); + if self.gate.connected&&!connected {eprintln!("DECKPAD_NATIVE_CONNECT_QUEUED");} + if !self.gate.connected&&connected {eprintln!("DECKPAD_NATIVE_RELEASE_QUEUED");} + let feedback_active=self.gate.connected&&self.gate.armed&&self.rumble_enabled&&!self.rumble_failed; + self.feedback.set_active(feedback_active); + let incoming=self.feedback.take(); + if incoming.is_some()&&!self.received_once {self.received_once=true;eprintln!("DECKPAD_RUMBLE_RECEIVED");} + if let Some(m)=self.rumble.step(incoming,feedback_active,now){ + if let Some(pad)=self.pad.as_mut(){ + if pad.set_rumble(m.left,m.right,super::native_rumble::EFFECT_MS).is_err(){ + self.rumble_failed=true;self.feedback.set_active(false);self.rumble.stop(); + let _=pad.set_rumble(0,0,0);eprintln!("DECKPAD_RUMBLE_OUTPUT_FAILED"); + }else if m!=super::native_rumble::Motors::default()&&!self.applied_once{ + self.applied_once=true;eprintln!("DECKPAD_RUMBLE_APPLIED"); + } + } + } + out + } + fn stop_feedback(&mut self){ + self.feedback.set_active(false);self.rumble.stop(); + if let Some(pad)=self.pad.as_mut(){let _=pad.set_rumble(0,0,0);} + } + pub fn close(&mut self)->Vec{self.stop_feedback();self.gate.close()} +} +#[cfg(feature="viewer-sdl")] +impl Drop for NativeGamepad {fn drop(&mut self){self.stop_feedback();}} + +#[cfg(test)] mod tests{ + use super::*; + #[test] fn official_shape_and_axis_boundaries(){ + assert_eq!(connect(),json!({"action":"gpd_connect","index":0})); + let s=State{buttons:0x1000,lx:-32768,ly:32767,rx:100,ry:-100,lt:128,rt:255}.json(); + assert_eq!(s,json!({"action":"gpd_update","index":0,"buttons":4096,"left_trigger":128,"right_trigger":255,"left_thumb_x":-32768,"left_thumb_y":32767,"right_thumb_x":100,"right_thumb_y":-100})); + assert_eq!((y_axis(-32768),y_axis(32767)),(32767,-32767)); + assert_eq!([trigger(-32768),trigger(0),trigger(16384),trigger(32767)],[0,0,128,255]); + } + #[test] fn held_on_entry_waits_for_release_then_focus_loss_detaches(){ + let mut g=Gate::default();let t=Instant::now();let held=State{buttons:0x1000,..Default::default()}; + assert_eq!(g.step(Some(held),true,t),vec![connect(),State::default().json()]); + assert!(g.step(Some(held),true,t).is_empty()); + assert_eq!(g.step(Some(State::default()),true,t),vec![State::default().json()]); + assert_eq!(g.step(Some(held),true,t),vec![held.json()]); + assert_eq!(g.step(Some(held),false,t),vec![State::default().json(),disconnect()]); + assert!(g.close().is_empty()); + assert_eq!(g.step(Some(held),true,t),vec![connect(),State::default().json()]); + } + #[test] fn unplug_neutral_and_idle_refresh(){ + let mut g=Gate::default();let t=Instant::now();let z=State::default();g.step(Some(z),true,t); + assert!(g.step(Some(z),true,t+Duration::from_millis(50)).is_empty()); + assert_eq!(g.step(Some(z),true,t+Duration::from_millis(100)),vec![z.json()]); + assert_eq!(g.step(None,true,t+Duration::from_millis(101)),vec![z.json(),disconnect()]); + } +} diff --git a/src/viewer/native_rumble.rs b/src/viewer/native_rumble.rs new file mode 100644 index 0000000..ed0adb8 --- /dev/null +++ b/src/viewer/native_rumble.rs @@ -0,0 +1,97 @@ +//! Official Windows 4.45: gpd_vibration {index:u8,left_motor:u8,right_motor:u8}. +//! Server RVAs 0x5df5d0/0x5dfe80; viewer 0x10331a0. See protocol evidence doc. +use serde::Deserialize; +use std::{sync::{Arc,Mutex},time::{Duration,Instant}}; + +#[derive(Clone,Copy,Debug,Default,PartialEq,Eq)] +pub struct Motors {pub left:u16,pub right:u16} +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct Message {action:String,index:u8,left_motor:u8,right_motor:u8} +pub fn decode(data:&[u8])->Option{ + if data.len()>512 {return None} + let m:Message=serde_json::from_slice(data).ok()?; + if m.action!="gpd_vibration" || m.index!=0 {return None} + Some(Motors{left:u16::from(m.left_motor)*257,right:u16::from(m.right_motor)*257}) +} + +#[derive(Default)] +struct Slot {active:bool,latest:Option<(Motors,Instant)>} +/// One slot per peer: bounded memory, latest update wins (including zero/stop). +/// Disabled while unfocused/unarmed; never replay feedback accumulated during pause. +#[derive(Clone,Default)] +pub struct Feedback(Arc>); +impl Feedback { + pub fn set_active(&self,active:bool){ + let mut slot=self.0.lock().unwrap(); + if slot.active!=active {slot.latest=None;slot.active=active;} + } + pub fn receive(&self,data:&[u8]){ + let mut slot=self.0.lock().unwrap(); + if slot.active {if let Some(m)=decode(data){slot.latest=Some((m,Instant::now()));}} + } + pub fn take(&self)->Option<(Motors,Instant)>{self.0.lock().unwrap().latest.take()} +} + +/// Renew finite SDL effects while active; if the process stalls/dies, the hardware +/// effect expires. UU sends state changes, so a quiet network is NOT a stop message. +pub const EFFECT_MS:u32=1000; +const REFRESH:Duration=Duration::from_millis(250); +#[derive(Default)] +pub struct Playback {desired:Motors,next:Option} +impl Playback { + pub fn step(&mut self,incoming:Option<(Motors,Instant)>,active:bool,now:Instant)->Option{ + if !active {return self.stop()} + let previous=self.desired; + if let Some((m,received))=incoming { + // Do not play a pulse left pending while decoding or scheduling stalled. + if m==Motors::default() || now.saturating_duration_since(received)<=Duration::from_millis(500){self.desired=m;} + } + let running=self.desired!=Motors::default(); + if previous!=self.desired || (running&&self.next.is_none_or(|t|now>=t)) { + self.next=running.then_some(now+REFRESH);return Some(self.desired) + } + None + } + pub fn stop(&mut self)->Option{ + let running=self.desired!=Motors::default();*self=Self::default(); + running.then_some(Motors::default()) + } +} + +#[cfg(test)] mod tests { + use super::*; + fn data(left:u8,right:u8)->Vec{format!(r#"{{"action":"gpd_vibration","index":0,"left_motor":{left},"right_motor":{right}}}"#).into_bytes()} + #[test] fn official_bytes_scale_to_full_sdl_range(){ + assert_eq!(decode(&data(255,128)),Some(Motors{left:65535,right:32896})); + assert_eq!(decode(&data(0,0)),Some(Motors::default())); + } + #[test] fn reject_other_slots_malformed_ranges_duplicates_and_other_actions(){ + let good=String::from_utf8(data(1,2)).unwrap(); + for bad in [good.replace("\"index\":0","\"index\":1"),good.replace("vibration","update"), + good.replace("\"left_motor\":1","\"left_motor\":256"),good.replace("\"left_motor\":1","\"left_motor\":-1"), + good.replace("\"left_motor\":1","\"left_motor\":1.5"),good.replace("\"index\":0","\"index\":0,\"index\":0"), + good.replace(",\"right_motor\":2",""),"{}".into()," ".repeat(513)] {assert_eq!(decode(bad.as_bytes()),None);} + } + #[test] fn peer_isolation_pause_and_latest_stop_wins(){ + let a=Feedback::default();let b=Feedback::default(); + a.receive(&data(255,0));assert!(a.take().is_none()); + a.set_active(true);b.set_active(true); + for _ in 0..1000 {a.receive(&data(255,255));}a.receive(&data(0,0)); + assert_eq!(a.take().unwrap().0,Motors::default());assert!(b.take().is_none()); + a.receive(&data(255,0));a.set_active(false);a.set_active(true);assert!(a.take().is_none()); + } + #[test] fn sustained_effect_refresh_stop_focus_loss_and_stale_pulse(){ + let t=Instant::now();let mut p=Playback::default();let m=Motors{left:100,right:200}; + assert_eq!(p.step(Some((m,t)),true,t),Some(m)); + assert_eq!(p.step(None,true,t+Duration::from_millis(249)),None); + assert_eq!(p.step(None,true,t+REFRESH),Some(m)); + assert_eq!(p.step(Some((Motors::default(),t)),true,t+REFRESH),Some(Motors::default())); + assert_eq!(p.step(Some((m,t)),true,t),Some(m)); + assert_eq!(p.step(None,false,t),Some(Motors::default())); + assert_eq!(p.step(None,true,t),None); + assert_eq!(p.step(Some((m,t)),true,t+Duration::from_millis(501)),None); + p.step(Some((m,t)),true,t); + assert_eq!(p.step(Some((Motors::default(),t)),true,t+Duration::from_secs(5)),Some(Motors::default())); + } +} diff --git a/src/viewer/rtc.rs b/src/viewer/rtc.rs index 8b2b9c2..4d5a67a 100644 --- a/src/viewer/rtc.rs +++ b/src/viewer/rtc.rs @@ -44,6 +44,7 @@ pub struct Observed { pub struct Peer { clipboard:super::clipboard::Bridge, pub tools:super::tools::Inbox, + pub rumble:super::native_rumble::Feedback, pub pc:Arc, pub channels:Vec>, pub observed:Arc, @@ -78,6 +79,7 @@ impl Peer { let api=APIBuilder::new().with_media_engine(engine).with_setting_engine(settings).with_interceptor_registry(registry).build(); let pc=Arc::new(api.new_peer_connection(config).await.map_err(|e|format!("Peer creation failed: {}",crate::auth::transport_error(&e)))?); let tools:super::tools::Inbox=Arc::new(std::sync::Mutex::new(None)); + let rumble=super::native_rumble::Feedback::default(); let inbox=tools.clone(); pc.on_peer_connection_state_change(Box::new(move |state|{ use webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState; @@ -197,15 +199,17 @@ impl Peer { } }))); } - let closed=tools.clone();let state=observed.clone(); - channel.on_close(Box::new(move ||{let closed=closed.clone();let state=state.clone();Box::pin(async move { - if index==0 {state.control_open.store(false,Ordering::Release);} + let closed=tools.clone();let state=observed.clone();let rumble_closed=rumble.clone(); + channel.on_close(Box::new(move ||{let closed=closed.clone();let state=state.clone();let rumble_closed=rumble_closed.clone();Box::pin(async move { + if index==0 {state.control_open.store(false,Ordering::Release);rumble_closed.set_active(false);} if [0,3,4].contains(&index){closed.lock().unwrap().take();} })})); let state=observed.clone();let inbox=tools.clone();let clipboard_tx=clipboard.sender(); + let feedback=rumble.clone(); let control_for_echo=if index==0 {Arc::downgrade(&channel)} else {std::sync::Weak::new()}; - channel.on_message(Box::new(move |message| {let state=state.clone();let inbox=inbox.clone();let control_for_echo=control_for_echo.clone();let clipboard_tx=clipboard_tx.clone();Box::pin(async move { + channel.on_message(Box::new(move |message| {let state=state.clone();let inbox=inbox.clone();let control_for_echo=control_for_echo.clone();let clipboard_tx=clipboard_tx.clone();let feedback=feedback.clone();Box::pin(async move { if index==0 { + feedback.receive(&message.data); let (codecs,chroma)=wire::media_control(&message.data); if let Some(codecs)=codecs {state.encoder_codecs.store(codecs,Ordering::Release);} if let Some(chroma)=chroma { @@ -253,7 +257,7 @@ impl Peer { Ok::<_,String>((channels,tools,clipboard)) }.await; match result { - Ok((channels,tools,clipboard))=>Ok(Self{clipboard,tools,pc,channels,observed,desktop,audio,answered:false,early:Vec::new(),app_id:app_id.into(),ice_id:ice_id.into(),remote_client:remote_client.into()}), + Ok((channels,tools,clipboard))=>Ok(Self{clipboard,tools,rumble,pc,channels,observed,desktop,audio,answered:false,early:Vec::new(),app_id:app_id.into(),ice_id:ice_id.into(),remote_client:remote_client.into()}), Err(e)=>{let _=pc.close().await;Err(e)}, } } @@ -329,7 +333,7 @@ impl Peer { let rtt=(pair.current_round_trip_time>0.).then(||(pair.current_round_trip_time*1000.).round() as u32); (rtt,format!("{} · {}",if tcp{"TCP"}else{"UDP"},if relay{"relay"}else{"direct"})) } - pub async fn close(&self) {self.clipboard.stop();self.tools.lock().unwrap().take();let _=self.pc.close().await;} + pub async fn close(&self) {self.rumble.set_active(false);self.clipboard.stop();self.tools.lock().unwrap().take();let _=self.pc.close().await;} } fn answer_sdp(body:&Value)->Result { if let Some(s)=body.get("sdp").and_then(Value::as_str) {if s.len()<=1024*1024{return Ok(s.into())}} diff --git a/src/viewer/sdl.rs b/src/viewer/sdl.rs index d8dfcdd..13448e1 100644 --- a/src/viewer/sdl.rs +++ b/src/viewer/sdl.rs @@ -399,6 +399,10 @@ pub async fn run(session:&mut Session,window:&mut Window,decoder:&mut dyn super: } Err(e)=>{ log::warn!("Type IPC not started: {e}"); None } }; + let mut native_gamepad=if super::native_gamepad::enabled(){ + if window.mac{return Err(crate::i18n::s("原生手柄实验仅支持 Windows 被控端","Native gamepad experiment requires a Windows host").into())} + Some(super::native_gamepad::NativeGamepad::new(&window._sdl,session.peer.rumble.clone())?) + }else{None}; let mut command_open=true;let mut requested=false;let mut type_requested=false;let mut restored=false; let mut type_job:Option=None; let mut terminate=tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()).map_err(|_|"Cannot watch termination")?; @@ -537,6 +541,13 @@ pub async fn run(session:&mut Session,window:&mut Window,decoder:&mut dyn super: type_job=None; } } + if let Some(pad)=native_gamepad.as_mut(){ + let active=!quit&&window.focused&&input.enabled()&&!window.chrome.menu.open&&type_job.is_none() + &&session.peer.ready(super::rtc::ReadyPolicy::EchoOnly,false) + &&session.peer.observed.control_open.load(std::sync::atomic::Ordering::Acquire) + &&session.peer.pc.connection_state()==webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState::Connected; + messages.extend(pad.poll(active)); + } if session.peer.ready(super::rtc::ReadyPolicy::EchoOnly,false){for m in messages{session.peer.send_input(&m).await?;}} else {input.release_all();} if quit{return Ok(())} @@ -558,6 +569,9 @@ pub async fn run(session:&mut Session,window:&mut Window,decoder:&mut dyn super: if let Some(frame)=window.last_frame.as_ref() {if let Err(e)=super::thumbs::save(device,frame){log::debug!("Viewer thumbnail not saved: {e}");}} #[cfg(uu_gtk)] if let Some(f)=files {f.abort();let _=f.await;super::tools_ui::hide();session.peer.tools.lock().unwrap().take();} if let Some(t)=type_ipc {t.abort();let _=t.await;} + if let Some(pad)=native_gamepad.as_mut(){for m in pad.close(){ + let _=tokio::time::timeout(std::time::Duration::from_millis(300),session.peer.send_input(&m)).await; + }} for m in input.release_all(){let _=session.peer.send_input(&m).await;} result } diff --git a/src/viewer/tests.rs b/src/viewer/tests.rs index 7861dab..5cc95bb 100644 --- a/src/viewer/tests.rs +++ b/src/viewer/tests.rs @@ -250,12 +250,14 @@ async fn desktop_file_window_reattaches_without_replacing_peer(){ let mut engine=MediaEngine::default();engine.register_default_codecs().unwrap(); let host=Arc::new(APIBuilder::new().with_media_engine(engine).with_setting_engine(host_settings).build().new_peer_connection(RTCConfiguration::default()).await.unwrap()); let file:Arc>>>=Default::default(); + let control:Arc>>>=Default::default(); + let control_out=control.clone(); let(tx,mut received)=mpsc::channel(32); let opened=Arc::new(AtomicUsize::new(0));let count=opened.clone(); - host.on_data_channel(Box::new(move|channel|{let(tx,file,count)=(tx.clone(),file.clone(),count.clone());Box::pin(async move{ + host.on_data_channel(Box::new(move|channel|{let(tx,file,count,control)=(tx.clone(),file.clone(),count.clone(),control.clone());Box::pin(async move{ count.fetch_add(1,Ordering::Relaxed); if channel.label()==wire::CHANNELS[tools::FILE]{*file.lock().unwrap()=Some(channel.clone());} - if channel.label()==wire::CHANNELS[tools::CONTROL]{channel.on_message(Box::new(move|m|{ + if channel.label()==wire::CHANNELS[tools::CONTROL]{*control.lock().unwrap()=Some(channel.clone());channel.on_message(Box::new(move|m|{ let(tx,file)=(tx.clone(),file.clone());Box::pin(async move{ if let Some(r)=tools::blob(&m.data,21){ if tools::blob(r,8).and_then(|b|tools::blob(b,10)).is_some(){ @@ -277,6 +279,7 @@ async fn desktop_file_window_reattaches_without_replacing_peer(){ peer.pc.set_remote_description(host.local_description().await.unwrap()).await.unwrap(); while opened.load(Ordering::Relaxed)!=5 || peer.channels.iter().any(|c|c.ready_state()!=webrtc::data_channel::data_channel_state::RTCDataChannelState::Open){tokio::time::sleep(Duration::from_millis(10)).await;} let pc=peer.pc.clone(); + peer.rumble.set_active(true); for _ in 0..2{ let(io,rx)=tools::attach(&peer).unwrap();assert!(tools::attach(&peer).is_err()); let(f,_)=Files::new(io,rx);assert!(f.list_dir(":/").await.unwrap().is_empty());drop(f); @@ -284,6 +287,21 @@ async fn desktop_file_window_reattaches_without_replacing_peer(){ assert!(Arc::ptr_eq(&pc,&peer.pc)); let heartbeat=super::input::Input::heartbeat();peer.send_input(&heartbeat).await.unwrap(); loop{if serde_json::from_slice::(&received.recv().await.unwrap()).ok()==Some(heartbeat.clone()){break}} + // Native gamepad shares CONTROL and must not open a new channel/peer. + use super::native_gamepad::{self,State}; + for message in [native_gamepad::connect(),State{buttons:0x1000,..Default::default()}.json(), + State::default().json(),native_gamepad::disconnect()] { + peer.send_input(&message).await.unwrap(); + loop {if serde_json::from_slice::(&received.recv().await.unwrap()).ok()==Some(message.clone()){break}} + } + // Host feedback shares the same peer/channel; file detach must not steal it. + let control=control_out.lock().unwrap().clone().unwrap(); + for (left,right) in [(255u8,128u8),(0,0)] { + let feedback=json!({"action":"gpd_vibration","index":0,"left_motor":left,"right_motor":right}); + control.send(&bytes::Bytes::from(feedback.to_string())).await.unwrap(); + let received=loop {if let Some((m,_))=peer.rumble.take(){break m}tokio::time::sleep(Duration::from_millis(5)).await;}; + assert_eq!(received,super::native_rumble::Motors{left:u16::from(left)*257,right:u16::from(right)*257}); + } } assert_eq!(opened.load(Ordering::Relaxed),5); peer.close().await;host.close().await.unwrap();router.lock().await.stop().await.unwrap(); diff --git a/src/viewer/ui.rs b/src/viewer/ui.rs index 613f1f6..26068b8 100644 --- a/src/viewer/ui.rs +++ b/src/viewer/ui.rs @@ -32,7 +32,7 @@ fn watch_session(mut child: tokio::process::Child, mut cancel: tokio::sync::ones } status = child.wait() => break status, line = async { match lines.as_mut() { Some(l) => l.next_line().await, None => std::future::pending().await } } => match line { - Ok(Some(l)) => { if !l.starts_with('[') && !l.trim().is_empty() { last = l.chars().take(160).collect(); } } + Ok(Some(l)) => { if l.starts_with("DECKPAD_"){eprintln!("{l}");} else if !l.starts_with('[') && !l.trim().is_empty() { last = l.chars().take(160).collect(); } } _ => lines = None, }, }