feat(viewer): opt-in native gamepad and rumble for Windows hosts (#142)

SDL viewer forwards one mapped controller (gpd_connect/gpd_update/gpd_disconnect) and receives two-motor gpd_vibration over the existing CONTROL channel. Default off (UU_VIEWER_NATIVE_GAMEPAD=1).
This commit is contained in:
AZsaber authored and GitHub committed 2026-11-01 01:04:21 +08:00
1 parent 6bf05a6361
commit 1ff3383de7
10 files changed
+404 -9

No files matched your search

+2
View File
@@ -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;
+141
View File
@@ -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<Value>{
let out=if self.connected{vec![State::default().json(),disconnect()]}else{vec![]};
*self=Self::default();out
}
fn step(&mut self,state:Option<State>,active:bool,now:Instant)->Vec<Value>{
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<sdl2::controller::GameController>,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<Self,String>{
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<Value>{
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<Value>{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()]);
}
}
+97
View File
@@ -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<Motors>{
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<Mutex<Slot>>);
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<Instant>}
impl Playback {
pub fn step(&mut self,incoming:Option<(Motors,Instant)>,active:bool,now:Instant)->Option<Motors>{
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<Motors>{
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<u8>{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()));
}
}
+10 -6
View File
@@ -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<RTCPeerConnection>,
pub channels:Vec<Arc<RTCDataChannel>>,
pub observed:Arc<Observed>,
@@ -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<String,String> {
if let Some(s)=body.get("sdp").and_then(Value::as_str) {if s.len()<=1024*1024{return Ok(s.into())}}
+14
View File
@@ -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<TypeJob>=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
}
+20 -2
View File
@@ -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<std::sync::Mutex<Option<Arc<webrtc::data_channel::RTCDataChannel>>>>=Default::default();
let control:Arc<std::sync::Mutex<Option<Arc<webrtc::data_channel::RTCDataChannel>>>>=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::<Value>(&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::<Value>(&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();
+1 -1
View File
@@ -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,
},
}