Release input at desktop session boundaries; pipelined native Wayland capture

Squashed from the Codex v0.1.9 branch (reviewed):
- Clear stale input when a desktop session starts or ends and at service
  start/stop: X11 releases every pressed key/button on the display (a stuck
  XTest button blocked all clicks after a VM reboot), uinput resets its own
  devices, Wayland/portal sweep the keys this host can emit. A per-desktop
  lease stops late callbacks of an old connection from touching a new one;
  file/terminal/port-mapping peers never release desktop input.
- Native Wayland (--features wayland): double-buffered screencopy pipeline,
  viewer frame rate drives the capture rate, opt-in dma-buf (UU_WAYLAND_DMABUF=1,
  linear only, falls back to SHM). VM-measured only.
- Internal QA records (docs/validation, contrib/tests) removed from the public
  tree and ignored; they stay in the private project.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
VallierDevandClaude Opus 5.5 committed 2026-10-15 19:45:12 +08:00
1 parent 15bb35573d
commit b5cc5ed736
16 files changed
+1164 -77

No files matched your search

+146 -5
View File
@@ -8,6 +8,7 @@ extern "C" {
fn x11_pointer_state(x:*mut libc::c_int,y:*mut libc::c_int,mask:*mut libc::c_uint)->libc::c_int;
fn x11_key_symbol(symbol: libc::c_ulong, is_press: libc::c_int) -> libc::c_int;
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;
}
#[cfg(not(target_os = "linux"))]
@@ -26,6 +27,9 @@ unsafe fn x11_key_symbol(_: libc::c_ulong, _: libc::c_int) -> libc::c_int { 0 }
#[cfg(not(target_os = "linux"))]
unsafe fn x11_mouse_relative(_: libc::c_int, _: libc::c_int) -> libc::c_int {0}
#[cfg(not(target_os="linux"))]
unsafe fn x11_release_all()->i32 {0}
#[derive(Default)]
struct PendingPointer {
absolute: Option<(i32,i32)>,
@@ -40,7 +44,7 @@ struct HeldInput { buttons: std::collections::BTreeSet<i32>, keys: std::collecti
#[derive(Default)]
struct Shortcuts { modifiers: u8, shifted: std::collections::BTreeSet<i32> }
pub struct InputInjector { shortcuts: std::sync::Mutex<Shortcuts>, uinput: Option<crate::uinput::Devices>, screen: std::sync::Mutex<(u32,u32)>, portal_failures: std::sync::atomic::AtomicUsize, portal_outcomes: [std::sync::atomic::AtomicUsize; 3], portal:std::sync::Mutex<Option<std::sync::Arc<crate::portal::PortalSession>>>, backend: crate::desktop_backend::DesktopBackend, pending: std::sync::Mutex<PendingPointer>, held: std::sync::Mutex<HeldInput> }
pub struct InputInjector { serial:std::sync::Mutex<()>, owner:std::sync::Mutex<DesktopOwner>, shortcuts: std::sync::Mutex<Shortcuts>, uinput: Option<crate::uinput::Devices>, screen: std::sync::Mutex<(u32,u32)>, portal_failures: std::sync::atomic::AtomicUsize, portal_outcomes: [std::sync::atomic::AtomicUsize; 3], portal:std::sync::Mutex<Option<std::sync::Arc<crate::portal::PortalSession>>>, backend: crate::desktop_backend::DesktopBackend, pending: std::sync::Mutex<PendingPointer>, held: std::sync::Mutex<HeldInput> }
impl InputInjector {
pub fn new() -> Self {
@@ -51,7 +55,7 @@ impl InputInjector {
Err(e) => { log::warn!("uinput unavailable ({}); using the desktop input path. See README for the udev rule.", e.kind()); None }
}
} else { None };
Self { shortcuts: std::sync::Mutex::new(Shortcuts::default()), uinput, screen: std::sync::Mutex::new((0,0)), portal_failures: std::sync::atomic::AtomicUsize::new(0), portal_outcomes: std::array::from_fn(|_| std::sync::atomic::AtomicUsize::new(0)), portal:std::sync::Mutex::new(None), backend:crate::desktop_backend::detect().unwrap_or(crate::desktop_backend::DesktopBackend::X11), pending: std::sync::Mutex::new(PendingPointer::default()), held:std::sync::Mutex::new(HeldInput::default()) }
Self { serial:std::sync::Mutex::new(()), owner:std::sync::Mutex::new(DesktopOwner::default()), shortcuts: std::sync::Mutex::new(Shortcuts::default()), uinput, screen: std::sync::Mutex::new((0,0)), portal_failures: std::sync::atomic::AtomicUsize::new(0), portal_outcomes: std::array::from_fn(|_| std::sync::atomic::AtomicUsize::new(0)), portal:std::sync::Mutex::new(None), backend:crate::desktop_backend::detect().unwrap_or(crate::desktop_backend::DesktopBackend::X11), pending: std::sync::Mutex::new(PendingPointer::default()), held:std::sync::Mutex::new(HeldInput::default()) }
}
pub fn bind_portal(&self,session:Option<std::sync::Arc<crate::portal::PortalSession>>) {
@@ -59,6 +63,7 @@ impl InputInjector {
*self.portal.lock().unwrap()=session;
}
pub fn mouse_move(&self, x: i32, y: i32) {
let _serial=self.serial.lock().unwrap();
self.inject_move(x,y,false);
}
@@ -93,6 +98,10 @@ impl InputInjector {
}
pub fn discard_pointer(&self) { *self.pending.lock().unwrap() = PendingPointer::default(); }
pub fn flush_pointer(&self) {
let _serial=self.serial.lock().unwrap();
self.flush_pointer_locked();
}
fn flush_pointer_locked(&self) {
// Hold the queue lock through injection so an older timer tick cannot
// overwrite a newer position flushed before a button transition.
let mut pending = self.pending.lock().unwrap();
@@ -118,7 +127,8 @@ impl InputInjector {
}
}
pub fn mouse_button(&self, button: i32, is_press: bool) {
self.flush_pointer();
let _serial=self.serial.lock().unwrap();
self.flush_pointer_locked();
let result = self.inject_button(button,is_press);
if result == 0 {
let mut held = self.held.lock().unwrap();
@@ -158,6 +168,7 @@ impl InputInjector {
}
pub fn windows_key_event(&self, key: i32, is_press: bool) {
let _serial=self.serial.lock().unwrap();
if self.terminal_shift(key, is_press) {
const SHIFT_L: u64 = 0xffe1;
if is_press {
@@ -187,11 +198,29 @@ impl InputInjector {
}
pub fn release_held(&self) {
let _serial=self.serial.lock().unwrap();
self.discard_pointer();
let mut held=self.held.lock().unwrap();
for button in &held.buttons {self.inject_button(*button,false);}
for key in &held.keys {self.inject_key(*key,false);}
if let Some(uinput)=&self.uinput {
Self::uinput_result(uinput.release_all());
} else if self.backend==crate::desktop_backend::DesktopBackend::X11 {
unsafe {x11_release_all();}
} else if self.backend==crate::desktop_backend::DesktopBackend::Wayland && self.portal.lock().unwrap().is_none() {
crate::wayland::release_all();
} else {
// Portal/Wayland cannot query global state. Sweep every key/button
// that this host can emit through the currently bound session.
let keys:std::collections::BTreeSet<_>=(0..=255).filter_map(crate::input_mapping::windows_vk_to_keysym)
.chain(held.keys.iter().copied()).collect();
// A dead Portal bus can time out per call. Stop on the first
// failure rather than blocking teardown for hundreds of calls;
// the next connected session retries the complete sweep.
let buttons_ok=[1,2,3,8,9].into_iter().all(|button|self.inject_button(button,false)==0);
let keys_ok=buttons_ok && keys.into_iter().all(|key|self.inject_key(key,false)==0);
if !keys_ok {log::warn!("Desktop input reset incomplete: backend unavailable");}
}
*held=HeldInput::default();
*self.shortcuts.lock().unwrap()=Shortcuts::default();
}
// Per-injector budgets cover missing bindings, locked sessions, stale handles
// and D-Bus errors. Never include keys, coordinates, text or portal tokens.
@@ -260,6 +289,7 @@ impl InputInjector {
else { unsafe { x11_key_symbol(symbol as libc::c_ulong,i32::from(pressed)) } }
}
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; }
unsafe { x11_key_event(keycode, if is_press { 1 } else { 0 }) };
}
@@ -333,3 +363,114 @@ mod scroll_tests {
assert_eq!(input.pending.lock().unwrap().scroll,(-5,-5));
}
}
/// Only the newest desktop lease may inject or release input. Tool peers never
/// obtain a lease; delayed callbacks from an old desktop cannot clear a new one.
#[derive(Default)]
struct DesktopOwner { next:u64, current:u64, active:bool, stopped:bool }
impl DesktopOwner {
fn replace(&mut self)->u64 {self.next+=1;self.current=self.next;self.active=false;self.current}
fn owns(&self,id:u64)->bool {id!=0 && self.current==id}
}
pub struct DesktopInputSession {input:std::sync::Arc<InputInjector>,id:u64}
impl InputInjector {
pub fn shutdown(&self) {
let mut owner=self.owner.lock().unwrap();
owner.current=0;owner.active=false;owner.stopped=true;
self.release_held();
}
pub fn desktop_session(self:&std::sync::Arc<Self>)->std::sync::Arc<DesktopInputSession> {
let mut owner=self.owner.lock().unwrap();
let id=if owner.stopped {0} else {self.release_held();owner.replace()};
std::sync::Arc::new(DesktopInputSession {input:self.clone(),id})
}
}
impl DesktopInputSession {
pub fn connected(&self) {
let mut owner=self.input.owner.lock().unwrap();
if owner.owns(self.id) && !owner.active {self.input.release_held();owner.active=true;}
}
pub fn disconnected(&self) {
let mut owner=self.input.owner.lock().unwrap();
if owner.owns(self.id) && owner.active {owner.active=false;self.input.release_held();}
}
pub fn with_input(&self, action:impl FnOnce()) {
let owner=self.input.owner.lock().unwrap();
if owner.owns(self.id) && owner.active {action();}
}
}
impl Drop for DesktopInputSession {fn drop(&mut self) {self.disconnected();}}
#[cfg(test)]
mod lifecycle_tests {
use super::*;
use std::sync::{Arc,atomic::{AtomicUsize,Ordering}};
#[test]
fn cleanup_discards_motion_shortcuts_and_untracked_input_ledger() {
let input=Arc::new(InputInjector::new());
input.held.lock().unwrap().keys.insert(0xffe1);
input.held.lock().unwrap().buttons.insert(1);
input.shortcuts.lock().unwrap().modifiers=2;
input.shortcuts.lock().unwrap().shifted.insert(0x56);
input.mouse_relative(32,0);
input.mouse_scroll(0,1);
input.release_held();
assert!(input.held.lock().unwrap().keys.is_empty());
assert!(input.held.lock().unwrap().buttons.is_empty());
assert_eq!(input.pending.lock().unwrap().relative,(0,0));
assert_eq!(input.pending.lock().unwrap().scroll,(0,0));
assert_eq!(input.shortcuts.lock().unwrap().modifiers,0);
assert!(input.shortcuts.lock().unwrap().shifted.is_empty());
input.release_held(); // idempotent even with an empty ledger
}
#[test]
fn disconnected_or_replaced_desktop_cannot_inject_or_clear_new_input() {
let input=Arc::new(InputInjector::new());
let count=AtomicUsize::new(0);
let old=input.desktop_session();
old.with_input(||{count.fetch_add(1,Ordering::Relaxed);});
assert_eq!(count.load(Ordering::Relaxed),0);
old.connected();
old.with_input(||{count.fetch_add(1,Ordering::Relaxed);});
old.disconnected();
old.with_input(||{count.fetch_add(1,Ordering::Relaxed);});
assert_eq!(count.load(Ordering::Relaxed),1);
input.held.lock().unwrap().buttons.insert(2);
old.disconnected(); // repeated close states must not release later local activity
assert!(input.held.lock().unwrap().buttons.contains(&2));
old.connected(); // same peer can reconnect
assert!(input.held.lock().unwrap().buttons.is_empty());
let new=input.desktop_session();
new.connected();
input.held.lock().unwrap().buttons.insert(1);
old.disconnected();old.connected();drop(old);
assert!(input.held.lock().unwrap().buttons.contains(&1));
new.with_input(||{count.fetch_add(1,Ordering::Relaxed);});
assert_eq!(count.load(Ordering::Relaxed),2);
drop(new);
assert!(input.held.lock().unwrap().buttons.is_empty());
let last=input.desktop_session();last.connected();
input.shutdown();last.connected();
last.with_input(||panic!("shutdown cannot be undone by a stale callback"));
let later=input.desktop_session();later.connected();
later.with_input(||panic!("shutdown must reject newly arriving offers too"));
}
#[test]
fn input_gate_serializes_disconnect_against_inflight_event() {
let input=Arc::new(InputInjector::new());
let session=input.desktop_session();session.connected();
let (started,ready)=std::sync::mpsc::channel();
let (finish,resume)=std::sync::mpsc::channel();
let worker=session.clone();
let event=std::thread::spawn(move ||worker.with_input(|| {
started.send(()).unwrap();resume.recv().unwrap();
worker.input.held.lock().unwrap().buttons.insert(1);
}));
ready.recv().unwrap();
let ending=session.clone();
let teardown=std::thread::spawn(move ||ending.disconnected());
finish.send(()).unwrap();event.join().unwrap();teardown.join().unwrap();
assert!(input.held.lock().unwrap().buttons.is_empty());
session.with_input(||panic!("late event after disconnect"));
}
}
+22 -1
View File
@@ -182,9 +182,27 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
input.mouse_button(1,true); input.mouse_button(1,false);
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
let measure=std::env::var("UU_INPUT_MEASURE").is_ok_and(|v|v=="1");
let cursor=|| -> Result<serde_json::Value,Box<dyn std::error::Error>> {
let output=std::process::Command::new("hyprctl").args(["-j","cursorpos"]).output()?;
if !output.status.success() { return Err("Cursor measurement unavailable".into()); }
Ok(serde_json::from_slice(&output.stdout)?)
};
let before=if measure {Some(cursor()?)} else {None};
input.mouse_relative(32,0);
tokio::time::sleep(std::time::Duration::from_millis(200)).await;
if let Some(before)=before {
println!("Relative pointer measurement: {}",serde_json::json!({"before":before,"after":cursor()?,"requested_delta":[32,0]}));
}
for key in b"UUWAYLAND" { input.windows_key_event(i32::from(*key),true); input.windows_key_event(i32::from(*key),false); }
if std::env::var("UU_INPUT_SYMBOLS").is_ok_and(|v|v=="1") {
// Fixed synthetic symbols exercise the same VK+modifier path as CONTROL.
for (key,shift) in [(0xbd,true),(0xbb,true),(0xbd,false)] {
if shift {input.windows_key_event(0x10,true);}
input.windows_key_event(key,true); input.windows_key_event(key,false);
if shift {input.windows_key_event(0x10,false);}
}
}
tokio::time::sleep(std::time::Duration::from_millis(300)).await;
worker.abort(); input.release_held();
println!("Desktop test events sent; verify the GTK acceptance report");
@@ -253,13 +271,16 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
ScreenCapture::unavailable(error)
}));
let input = Arc::new(InputInjector::new());
input.bind_portal(capture.portal.clone());
input.release_held(); // recover stale input before accepting any peer
let _pointer_worker = input.start_pointer_worker();
#[cfg(target_os="linux")]
{
let shutdown_input=input.clone();
tokio::spawn(async move {
if let Ok(mut term)=tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()) {
term.recv().await; virtual_audio::disable(); shutdown_input.release_held(); terminal::shutdown_all(); std::process::exit(0);
tokio::select! { _=term.recv()=>{}, _=tokio::signal::ctrl_c()=>{} }
shutdown_input.shutdown(); virtual_audio::disable(); terminal::shutdown_all(); std::process::exit(0);
}
});
}
+45
View File
@@ -121,6 +121,16 @@ mod imp {
pub fn key(&self, code: u16, pressed: bool) -> std::io::Result<()> {
self.keyboard.lock().unwrap().emit(&[(EV_KEY, code, i32::from(pressed))])
}
pub fn release_all(&self) -> std::io::Result<()> {
// Release every supported code on our own devices, even if the
// userspace ledger missed a press. Never touch physical devices.
let keys:Vec<_>=(1..=248).map(|code|(EV_KEY,code,0))
.chain([BTN_LEFT,BTN_RIGHT,BTN_MIDDLE].into_iter().map(|code|(EV_KEY,code,0))).collect();
let buttons:Vec<_>=[BTN_LEFT,BTN_RIGHT,BTN_MIDDLE,BTN_SIDE,BTN_EXTRA].into_iter().map(|code|(EV_KEY,code,0)).collect();
let keyboard=self.keyboard.lock().unwrap().emit(&keys);
let pointer=self.pointer.lock().unwrap().emit(&buttons);
keyboard.and(pointer)
}
pub fn move_absolute(&self, x: i32, y: i32) -> std::io::Result<()> {
self.pointer.lock().unwrap().emit(&[(EV_ABS, ABS_X, x.clamp(0, ABS_MAX)), (EV_ABS, ABS_Y, y.clamp(0, ABS_MAX))])
}
@@ -138,6 +148,40 @@ mod imp {
self.pointer.lock().unwrap().emit(&events)
}
}
#[cfg(test)]
mod reset_acceptance {
use super::*;
fn snapshot(device:&Device)->[u8;96] {
let mut name=[0u8;80];
let request=0x8000_0000u64 | (80<<16) | (b'U' as u64)<<8 | 44;
assert!(unsafe {libc::ioctl(device.0.as_raw_fd(),request as libc::c_ulong,name.as_mut_ptr())}>=0);
let end=name.iter().position(|v|*v==0).unwrap();
let directory=std::path::Path::new("/sys/devices/virtual/input").join(std::str::from_utf8(&name[..end]).unwrap());
let event=std::fs::read_dir(directory).unwrap().filter_map(Result::ok)
.find(|e|e.file_name().to_string_lossy().starts_with("event")).unwrap();
let file=File::open(std::path::Path::new("/dev/input").join(event.file_name())).unwrap();
let mut state=[0u8;96];
let request=0x8000_0000u64 | (96<<16) | (b'E' as u64)<<8 | 0x18;
assert!(unsafe {libc::ioctl(file.as_raw_fd(),request as libc::c_ulong,state.as_mut_ptr())}>=0);
state
}
#[test]
#[ignore="requires isolated QEMU VM uinput and evdev permissions"]
fn kernel_devices_release_even_without_an_inputinjector_ledger() {
assert_eq!(std::fs::read_to_string("/proc/sys/kernel/hostname").unwrap().trim(),"uu-hyprland-test");
assert_eq!(std::process::Command::new("systemd-detect-virt").output().unwrap().stdout,b"qemu\n");
let devices=Devices::open().unwrap();
std::thread::sleep(std::time::Duration::from_millis(500));
devices.key(42,true).unwrap();devices.key(30,true).unwrap();
devices.button(BTN_LEFT,true).unwrap();
assert!(snapshot(&devices.keyboard.lock().unwrap()).iter().any(|v|*v!=0));
assert!(snapshot(&devices.pointer.lock().unwrap()).iter().any(|v|*v!=0));
devices.release_all().unwrap();
assert!(snapshot(&devices.keyboard.lock().unwrap()).iter().all(|v|*v==0));
assert!(snapshot(&devices.pointer.lock().unwrap()).iter().all(|v|*v==0));
}
}
}
#[cfg(not(target_os = "linux"))]
@@ -151,6 +195,7 @@ mod imp {
pub struct Devices;
impl Devices {
pub fn open() -> std::io::Result<Self> { Err(std::io::Error::new(std::io::ErrorKind::Unsupported, "uinput requires Linux")) }
pub fn release_all(&self) -> std::io::Result<()> { Ok(()) }
pub fn key(&self, _: u16, _: bool) -> std::io::Result<()> { Ok(()) }
pub fn move_absolute(&self, _: i32, _: i32) -> std::io::Result<()> { Ok(()) }
pub fn move_relative(&self, _: i32, _: i32) -> std::io::Result<()> { Ok(()) }
+19 -5
View File
@@ -202,7 +202,7 @@ fn rate_args(settings: &VideoSettings, encoder: Encoder) -> (String, String) {
async fn read_encoder_with(
size: (u32,u32),display:String,portal:Option<Arc<crate::portal::PortalSession>>,latest:mpsc::Sender<Arc<CompleteFrame>>,encoder:Encoder,sequence:Arc<std::sync::atomic::AtomicU64>,
mut settings: VideoSettings, #[cfg_attr(not(feature = "inproc-av"), allow(unused_variables))] idr: Arc<AtomicBool>,
settings: VideoSettings, #[cfg_attr(not(feature = "inproc-av"), allow(unused_variables))] idr: Arc<AtomicBool>,
)->Result<(),String>{
let kms=display.starts_with("kms:");
let wayland=kms || display.starts_with("wayland:");
@@ -226,8 +226,6 @@ async fn read_encoder_with(
return read_inprocess(config, latest, sequence, idr).await;
}
let size = format!("{}x{}", size.0, size.1);
// The separate wayland-capture process produces frames at its own fixed rate.
if wayland && portal.is_none() { settings.fps = FPS as u32; }
let fps = settings.fps.to_string();
// Keep the IDR interval at 2 s whatever the frame rate.
let gop = gop_frames(settings.fps, false).to_string();
@@ -323,7 +321,7 @@ async fn read_encoder_with(
let executable=std::path::PathBuf::from("/proc/self/exe");
#[cfg(not(target_os="linux"))]
let executable=std::env::current_exe().map_err(|e|format!("Capture executable: {e}"))?;
let mut capture=Command::new(executable).arg("wayland-capture")
let mut capture=wayland_capture_command(&executable,settings.fps)
.kill_on_drop(true).stdout(std::process::Stdio::piped()).stderr(std::process::Stdio::inherit())
.spawn().map_err(|e|format!("Start Wayland capture: {e}"))?;
let mut source=capture.stdout.take().ok_or("Capture stdout missing")?;
@@ -359,6 +357,12 @@ async fn read_encoder_with(
}
}
fn wayland_capture_command(executable:&std::path::Path,fps:u32)->Command {
let mut command=Command::new(executable);
command.arg("wayland-capture").env("UU_WAYLAND_FPS",fps.to_string());
command
}
fn transport_closed(pc: &RTCPeerConnection) -> bool {
matches!(pc.connection_state(), RTCPeerConnectionState::Closed | RTCPeerConnectionState::Failed)
|| pc.sctp().state() == webrtc::sctp_transport::sctp_transport_state::RTCSctpTransportState::Closed
@@ -373,7 +377,9 @@ pub async fn probe(size:(u32,u32),display:String,portal:Option<Arc<crate::portal
};
let (tx,mut rx)=mpsc::channel(4);
let sequence=Arc::new(std::sync::atomic::AtomicU64::new(0));
let _reader=EncoderTask(tokio::spawn(read_encoder(size,display,portal,tx,VideoSettings::default(),sequence,Arc::new(AtomicBool::new(false)))));
let fps=crate::wayland::capture_fps(std::env::var("UU_PROBE_FPS").ok().as_deref())?;
let settings=VideoSettings {fps,..VideoSettings::default()};
let _reader=EncoderTask(tokio::spawn(read_encoder(size,display,portal,tx,settings,sequence,Arc::new(AtomicBool::new(false)))));
let mut count=0;
let mut bytes=0;
// UU_PROBE_FRAMES lengthens the sample for baseline measurements; default is 5 frames.
@@ -463,6 +469,14 @@ pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<TrackLocalSta
#[cfg(test)]
mod tests {
#[test]
fn native_wayland_child_uses_requested_capture_rate() {
for fps in [5,30,60,90,144] {
let command=super::wayland_capture_command(std::path::Path::new("uuremote-server"),fps);
let value=fps.to_string();
assert!(command.as_std().get_envs().any(|(k,v)|k=="UU_WAYLAND_FPS" && v==Some(std::ffi::OsStr::new(&value))));
}
}
use super::*;
#[test]
fn x264_buffer_fits_keyframes() {
+27 -1
View File
@@ -7,6 +7,13 @@ extern "C" {
fn uu_wl_button(button: i32, pressed: i32) -> i32;
fn uu_wl_key(symbol: u64, pressed: i32) -> i32;
fn uu_wl_wake() -> i32;
fn uu_wl_release_all() -> i32;
}
pub fn release_all()->i32 {
#[cfg(all(target_os="linux",feature="wayland"))]
{return unsafe {uu_wl_release_all()};}
#[cfg(not(all(target_os="linux",feature="wayland")))]
-1
}
pub fn init() -> Result<(u32,u32),String> {
#[cfg(all(target_os="linux", feature="wayland"))]
@@ -58,7 +65,8 @@ pub fn capture_stdout() -> Result<(),String> {
let (w,h)=init()?;
let mut frame=vec![0u8;(w as usize)*(h as usize)*4];
let mut stdout=std::io::stdout().lock();
let interval=std::time::Duration::from_nanos(1_000_000_000/crate::video::FPS);
let fps=capture_fps(std::env::var("UU_WAYLAND_FPS").ok().as_deref())?;
let interval=std::time::Duration::from_nanos(1_000_000_000/u64::from(fps));
loop {
let start=std::time::Instant::now();
capture(&mut frame)?;
@@ -67,6 +75,24 @@ pub fn capture_stdout() -> Result<(),String> {
}
}
pub(crate) fn capture_fps(value:Option<&str>)->Result<u32,String> {
match value {
None=>Ok(crate::video::FPS as u32),
Some(value)=>value.parse::<u32>().ok().filter(|fps|(5..=144).contains(fps))
.ok_or_else(||"UU_WAYLAND_FPS must be between 5 and 144".to_owned()),
}
}
#[cfg(test)]
mod tests {
#[test]
fn capture_rate_accepts_viewer_range_and_rejects_invalid_values() {
assert_eq!(super::capture_fps(None),Ok(60));
for fps in [5,15,30,60,90,144] {assert_eq!(super::capture_fps(Some(&fps.to_string())),Ok(fps));}
for value in ["0","4","145","-1","NaN",""] {assert!(super::capture_fps(Some(value)).is_err());}
}
}
/// Use only in an isolated acceptance desktop; emits fixed test events, no logs.
pub fn input_probe()->Result<(),String> {
let (w,h)=init()?;
+17 -2
View File
@@ -86,6 +86,7 @@ impl WebRtcSession {
.map_err(|e| format!("Create PeerConnection failed: {}", e))?,
);
let input_session=desktop.then(||input.desktop_session());
let context = Arc::new(Mutex::new(SessionContext::default()));
crate::network_diagnostics::start(Arc::downgrade(&pc));
@@ -143,7 +144,7 @@ impl WebRtcSession {
}));
let connected_context = context.clone();
let connection_input=input.clone();
let connection_input=input_session.clone();
let connected_signal = signaling_client.clone();
let notified = Arc::new(std::sync::atomic::AtomicBool::new(false));
pc.on_peer_connection_state_change(Box::new(move |state| {
@@ -153,7 +154,10 @@ impl WebRtcSession {
let notified = notified.clone();
Box::pin(async move {
log::info!("PeerConnection State changed: {:?}", state);
if matches!(state,RTCPeerConnectionState::Closed|RTCPeerConnectionState::Failed|RTCPeerConnectionState::Disconnected) {input.release_held();}
if let Some(session)=input.as_ref() {
if state==RTCPeerConnectionState::Connected {session.connected();}
if matches!(state,RTCPeerConnectionState::Closed|RTCPeerConnectionState::Failed|RTCPeerConnectionState::Disconnected) {session.disconnected();}
}
if state == RTCPeerConnectionState::Connected && !notified.swap(true, std::sync::atomic::Ordering::AcqRel) {
tokio::spawn(async move {
let ctx = ctx.lock().await.clone();
@@ -327,6 +331,7 @@ impl WebRtcSession {
log::info!("New DataChannel received: {}", label);
let input_inst = input_clone.clone();
let channel_input_session=input_session.clone();
let screen = control_capture.clone();
let out_sequence = control_sequence.clone();
let shared_clipboard_ready = text_clipboard_ready.clone();
@@ -348,6 +353,13 @@ impl WebRtcSession {
let clipboard_assembly = Arc::new(std::sync::Mutex::new(crate::clipboard_protocol::Assembly::default()));
let rich_state=Arc::new(std::sync::Mutex::new(crate::clipboard_rich::State::default()));
let clipboard_enabled = shared_clipboard_ready.clone();
if label=="CONTROL_DATA_CHANNEL" {
let closing=channel_input_session.clone();
d.on_close(Box::new(move || {
let closing=closing.clone();
Box::pin(async move {if let Some(session)=closing {session.disconnected();}})
}));
}
// Ordered outbound queue for protocol replies produced outside this callback.
let (outbound, mut outbound_rx) = tokio::sync::mpsc::unbounded_channel::<Vec<u8>>();
{
@@ -498,6 +510,7 @@ impl WebRtcSession {
d.on_message(Box::new(move |msg: DataChannelMessage| {
let channel_name = lbl.clone();
let input_worker = inp.clone();
let message_input_session=channel_input_session.clone();
let terminal_worker = terminal_conn.clone();
let rpc_out = outbound.clone();
let reply_chan = d_reply.clone();
@@ -760,6 +773,7 @@ impl WebRtcSession {
if RECEIPTS[slot].fetch_add(1, std::sync::atomic::Ordering::Relaxed) < 16 {
log::info!("CONTROL input received: kind={}", kind);
}
if let Some(session)=message_input_session.as_ref() {session.with_input(|| {
match &event {
ControlInput::MouseAbsolute {x,y} => input_worker.mouse_absolute_normalized(*x,*y,input_size),
ControlInput::MouseRelative {dx,dy} => input_worker.mouse_relative(*dx,*dy),
@@ -770,6 +784,7 @@ impl WebRtcSession {
ControlInput::MouseClick {button} => {input_worker.wire_mouse_button(*button,true);input_worker.wire_mouse_button(*button,false);},
_ => {}
}
});}
},
_ => {}