mirror of
https://github.com/VallierDev/uu-remote-linux.git
synced 2026-11-17 01:00:41 +01:00
feat(portal): follow the viewer's mouse mode with the embedded cursor (#203)
* feat(portal): follow the viewer's mouse mode with the embedded cursor The portal stream hides the compositor cursor. A viewer that drives the pointer with relative motion (UU's controlled-end mouse, also the Mac trackpad mode) has no absolute position to draw its own pointer at, and the game's cursor never reaches the picture, so the mouse can be moved and clicked but not seen. Request cursor_mode=embedded while relative motion arrives, and keep it hidden while absolute motion arrives: there the viewer draws its own pointer and an embedded copy would trail it. The portal reads cursor_mode only when the session is created, so switch by rebuilding the session with the saved consent, the same path the privacy screen uses. A run has to last five events and 200 ms before it counts, so a burst at connect or an alternating controller does not rebuild; a failed rebuild backs off for five seconds. The rebuilt session is checked against the monitor the previous one captured, so a restored consent cannot silently move the share. The engine keeps the mode for later opens (recovery, privacy, monitor switches) and skips the switch when an older libuu-portal.so lacks uu_portal_set_cursor_mode. UU_PORTAL_CURSOR_MODE=embedded sets the startup default. * fix(portal): keep the cursor rebuild off locks and held input Do not rebuild while the desktop is locked, and cancel an open that is already waiting when it locks: GNOME closes the portal session on lock, so the request would wait for consent that cannot be answered while holding `recovery` against the post-unlock recovery. Keys and buttons held through the portal input path are dropped when the session is replaced, so wait for their release. Monitor selection takes the same `recovery` lock, so it cannot install a stale source over the cursor rebuild or the other way round, and the rebuilt session is revalidated against the source it started from. * fix(portal): carry the clipboard across cursor rebuilds The engine keeps the viewer's text only in the session that provided it, and the host re-sends it only for a new viewer update. A replacement session therefore had no clipboard ownership, so changing the mouse mode made previously synchronized text unpasteable until the viewer sent it again. Offer the text to the replacement before the old session closes, but only while that session still owns the selection: a local copy that has taken the clipboard since is left alone. Touch consent upgrades replace the handle too, so they take the same `recovery` lock as recovery, monitor selection and the cursor rebuild and cannot install a handle opened under an older cursor mode. * fix(portal): serialize clipboard writes with the cursor swap The clipboard bridge calls clipboard_set from its own thread. Installing the replacement handle and then carrying the old text over left a window where an update could land on the new session and be overwritten by the older text, with no pending update left to restore it. Take a clipboard write lock before the handle is installed and hold it until the carry-over finishes; clipboard_set takes the same lock. A write that starts before the swap is included in the text carried over, a write that starts after waits and wins. * fix(portal): cancel a waiting rebuild when the desktop ends The desktop ends its share when the last controller leaves (end_idle), but an open that is still waiting for consent is not the installed handle, so it survived that shutdown and installed a live share afterwards. Bump an end generation in Session::end(), snapshot it before the open, and cancel the request when it moves. The generation is checked again under the handle lock before installing, so an end that races the swap either aborts the rebuild or ends the handle it just installed. * fix(portal): keep session swaps consistent with the viewer's state Addresses the ownership race reported on the previous commit and the paths that share its failure mode. uu_portal_clipboard_set publishes the new text before SetSelection so a transfer during the call serves it, restores the old text when the call fails, and ignores a stale revision so a newer owner wins. The engine reports the cursor mode it actually requested and the modes the backend advertised; the host follows that instead of assuming the request was applied and stops retrying where an embedded cursor is unavailable. The end generation is sampled before the rebuild queues for `recovery`, a rebuild refuses to start while the session is no longer alive, and cancellation now covers virtual output opens. Portal-bound keys and buttons are tracked separately, so an input held through the portal defers the swap on every routing path, and the check is repeated after the open. Every handle replacement (privacy screen, monitor switch, touch consent, recovery after lock or stream loss) carries the viewer's clipboard under the clipboard write lock, and the rebuilt session is checked against the captured geometry. * fix(portal): stop the swap bookkeeping from outliving a deferred rebuild open_inner publishes the replacement's cursor mode before the swap. When held input defers the rebuild, that session is never installed, so the host belief described the discarded handle and later motion looked like it already matched. Restore the belief from the installed session on every abort path. The extra portal releases in release_held now stop at the first failure like the sweep above; with a dead portal bus each remaining input waited out the three-second timeout while the input and desktop-owner locks were held. * fix(portal): invalidate clipboard ownership when a session ends A closed session stops receiving owner changes, so a cached clipboard_owns survived into the recovery paths and could republish old viewer text over a newer local copy. Clear the claim in uu_portal_end and the Closed handler, and let uu_portal_clipboard_copy refuse a source that is no longer alive. Mark the embedded cursor unavailable when the engine did not apply it: with AvailableCursorModes unreadable the session opens hidden, and retrying on every motion run rebuilt it every 200 ms without changing the mode. * fix(portal): retire held portal input when the devices are replaced A key or button held through the native portal path is recorded when its injection succeeds. If the session dies first, the key-up fails and the record survives; recovery then installs new devices while the tracker still says an input is held, so pointer-mode detection keeps deferring. Keep the tracker in module state and clear it whenever the portal replaces or ends its devices: the rebuild, both recovery paths and Session::end. * fix(portal): scope held-input retirement to the retired handle A press delivered to the replacement after the swap could be recorded while the old handle was still being retired, and the unconditional reset then deleted it. Hold the recording lock from before the swap until the reset finishes, so only inputs sent to the retired devices are dropped. A portal touchscreen contact was not tracked at all: the swap retired the active contact while TouchDrag stayed down, so the following move reached a session that never saw the press. Track contact down/up and include it in the deferral state. * fix(portal): serialize touch injection with handle replacement inject_touch wrote the contact state outside the recording lock the handle swap holds, so a contact delivered to the replacement between the swap and the reset could be cleared, and the following phase-1 would fail on new devices that never saw the press. Take the same lock across the injection and the state update. * fix(portal): decide the swap under the held-input lock The final held-input check ran before install_replacement() took the serial lock, so an input could be injected and recorded against the old handle while the install waited for the clipboard lock. The install then retired the records and swapped, destroying a contact the new devices never saw. Repeat the check under the serial lock that recording uses, and defer the rebuild when it hits. * fix(portal): hold the input lock across native injection, refresh privacy geometry inject_button and inject_key injected through the portal and only took the recording lock afterwards, so a swap could take the lock, see no held input and replace the devices that had just received the press. Take the serial lock across the injection and its record, as the touch path does; the record helpers no longer take it themselves. A cursor rebuild that replaces the privacy virtual output invalidates the cached pointer region: recompute it from the settled layout so absolute input keeps landing inside the captured share.
This commit is contained in:
1 parent
c489f3056f
commit
21e9e690ae
11 files changed
+592
-38
No files matched your search
+79
-4
@@ -43,6 +43,28 @@ struct PendingPointer {
|
||||
unsafe fn x11_pointer_state(_: *mut libc::c_int,_:*mut libc::c_int,_:*mut libc::c_uint)->libc::c_int {-1}
|
||||
#[derive(Default)]
|
||||
struct HeldInput { buttons: std::collections::BTreeSet<i32>, keys: std::collections::BTreeSet<u64> }
|
||||
/// Inputs held through the portal session are dropped when that session is replaced; the
|
||||
/// cursor-mode rebuild waits until they are released. uinput devices survive the swap.
|
||||
static PORTAL_INPUT_HELD: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
|
||||
/// An active contact on the portal touchscreen is also lost when the session is replaced.
|
||||
static PORTAL_TOUCH_HELD: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
|
||||
pub fn portal_input_held() -> bool {
|
||||
PORTAL_INPUT_HELD.load(std::sync::atomic::Ordering::Relaxed) || PORTAL_TOUCH_HELD.load(std::sync::atomic::Ordering::Relaxed)
|
||||
}
|
||||
/// Inputs currently bound to the portal session; a replacement tears those devices down.
|
||||
static PORTAL_HELD: std::sync::Mutex<HeldInput> = std::sync::Mutex::new(HeldInput { buttons: std::collections::BTreeSet::new(), keys: std::collections::BTreeSet::new() });
|
||||
/// Serializes recording an input with retiring the records of a replaced handle.
|
||||
static PORTAL_HELD_SERIAL: std::sync::Mutex<()> = std::sync::Mutex::new(());
|
||||
pub fn lock_portal_held() -> std::sync::MutexGuard<'static, ()> {
|
||||
PORTAL_HELD_SERIAL.lock().unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
}
|
||||
/// The portal replaced its native devices: records of held portal inputs no longer apply.
|
||||
/// The caller holds `lock_portal_held()`, so an input recorded against the new handle stays.
|
||||
pub fn portal_session_replaced() {
|
||||
if let Ok(mut held)=PORTAL_HELD.lock() { *held=HeldInput::default(); }
|
||||
PORTAL_INPUT_HELD.store(false, std::sync::atomic::Ordering::Relaxed);
|
||||
PORTAL_TOUCH_HELD.store(false, std::sync::atomic::Ordering::Relaxed);
|
||||
}
|
||||
/// Held modifier VKs (bit 0 Ctrl, 1 Shift, 2 Alt, 3 Win) and keys that got a synthetic Shift.
|
||||
#[derive(Default)]
|
||||
struct Shortcuts { modifiers: u8, shifted: std::collections::BTreeSet<i32> }
|
||||
@@ -388,8 +410,20 @@ impl InputInjector {
|
||||
});
|
||||
}
|
||||
fn inject_touch(&self, phase: i32, x: i32, y: i32) -> i32 {
|
||||
// Serialize the contact with handle replacement: a contact sent to the new devices
|
||||
let Some(portal) = self.portal.lock().unwrap().clone() else { return -1 };
|
||||
self.portal_result("touch", portal.touch(phase, f64::from(x), f64::from(y)))
|
||||
// Serialize with handle replacement while the contact is sent; portal then recording
|
||||
// lock, the same order the button and key recorders use.
|
||||
let _serial = lock_portal_held();
|
||||
let result = self.portal_result("touch", portal.touch(phase, f64::from(x), f64::from(y)));
|
||||
if result == 0 {
|
||||
match phase {
|
||||
0 => PORTAL_TOUCH_HELD.store(true, std::sync::atomic::Ordering::Relaxed),
|
||||
2 => PORTAL_TOUCH_HELD.store(false, std::sync::atomic::Ordering::Relaxed),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
fn terminal_focused(&self) -> bool {
|
||||
@@ -504,6 +538,16 @@ impl InputInjector {
|
||||
if !keys_ok {log::warn!("Desktop input reset incomplete: backend unavailable");}
|
||||
}
|
||||
*held=HeldInput::default();
|
||||
// Inputs bound to the portal session are not covered by the uinput release above.
|
||||
let (portal_buttons, portal_keys) = {
|
||||
let _serial=lock_portal_held();
|
||||
let mut pending=PORTAL_HELD.lock().unwrap();
|
||||
(std::mem::take(&mut pending.buttons), std::mem::take(&mut pending.keys))
|
||||
};
|
||||
// Stop on the first failure like the sweep above: each call can wait out the portal timeout.
|
||||
let buttons_ok = portal_buttons.into_iter().all(|button| self.inject_button(button,false)==0);
|
||||
if buttons_ok { for key in portal_keys { if self.inject_key(key,false)!=0 { break; } } }
|
||||
PORTAL_INPUT_HELD.store(false,std::sync::atomic::Ordering::Relaxed);
|
||||
*self.shortcuts.lock().unwrap()=Shortcuts::default();
|
||||
*self.cmd.lock().unwrap()=CmdState::default();
|
||||
}
|
||||
@@ -537,7 +581,22 @@ impl InputInjector {
|
||||
}
|
||||
}
|
||||
}
|
||||
/// Inputs bound to the portal session are dropped when that session is replaced.
|
||||
fn record_portal_button(&self, button: i32, pressed: bool) {
|
||||
// Callers hold the serial lock across the injection they are recording.
|
||||
let mut held = PORTAL_HELD.lock().unwrap();
|
||||
if pressed { held.buttons.insert(button); } else { held.buttons.remove(&button); }
|
||||
PORTAL_INPUT_HELD.store(!held.buttons.is_empty() || !held.keys.is_empty(), std::sync::atomic::Ordering::Relaxed);
|
||||
}
|
||||
fn record_portal_key(&self, symbol: u64, pressed: bool) {
|
||||
let mut held = PORTAL_HELD.lock().unwrap();
|
||||
if pressed { held.keys.insert(symbol); } else { held.keys.remove(&symbol); }
|
||||
PORTAL_INPUT_HELD.store(!held.buttons.is_empty() || !held.keys.is_empty(), std::sync::atomic::Ordering::Relaxed);
|
||||
}
|
||||
fn inject_move(&self,x:i32,y:i32,relative:bool)->i32 {
|
||||
// The viewer's mouse mode decides whether the stream must carry the pointer (see
|
||||
// portal::note_pointer_motion); only the portal backend can change that.
|
||||
if self.backend==crate::desktop_backend::DesktopBackend::Portal { crate::portal::note_pointer_motion(relative); }
|
||||
if self.uinput_pointer && self.backend != crate::desktop_backend::DesktopBackend::X11 {
|
||||
if let Some(uinput) = &self.uinput {
|
||||
if relative { return Self::uinput_result(uinput.move_relative(x,y)); }
|
||||
@@ -607,14 +666,25 @@ impl InputInjector {
|
||||
// Portal: clicks follow absolute motion on the same session, so a tap cannot land
|
||||
// before the compositor has moved the pointer there (uinput is a separate queue).
|
||||
if !self.uinput_pointer && self.backend==crate::desktop_backend::DesktopBackend::Portal && !self.virtual_uinput() {
|
||||
if let Some(portal)=self.portal.lock().unwrap().as_ref() { return self.portal_result("button", portal.button(button,pressed)); }
|
||||
if let Some(portal)=self.portal.lock().unwrap().as_ref() {
|
||||
// The injection and its record must both predate or both postdate a swap.
|
||||
let _serial=lock_portal_held();
|
||||
let result=self.portal_result("button", portal.button(button,pressed));
|
||||
if result==0 { self.record_portal_button(button,pressed); }
|
||||
return result;
|
||||
}
|
||||
}
|
||||
if let Some(uinput)=&self.uinput {
|
||||
if let Some((v,h))=crate::uinput::wheel_step(button) { return if pressed { Self::uinput_result(uinput.wheel(v,h)) } else { 0 }; }
|
||||
if let Some(code)=crate::uinput::button_code(button) { return Self::uinput_result(uinput.button(code,pressed)); }
|
||||
}
|
||||
if self.backend==crate::desktop_backend::DesktopBackend::Kms { return -1; }
|
||||
if let Some(portal)=self.portal.lock().unwrap().as_ref() { return self.portal_result("button", portal.button(button,pressed)); }
|
||||
if let Some(portal)=self.portal.lock().unwrap().as_ref() {
|
||||
let _serial=lock_portal_held();
|
||||
let result=self.portal_result("button", portal.button(button,pressed));
|
||||
if result==0 { self.record_portal_button(button,pressed); }
|
||||
return result;
|
||||
}
|
||||
if self.backend==crate::desktop_backend::DesktopBackend::Portal {return self.portal_result("button", Err("Portal input session is not bound".into()));}
|
||||
if self.backend==crate::desktop_backend::DesktopBackend::Wayland { crate::wayland::button(button,pressed) }
|
||||
else { unsafe { x11_mouse_button(button,i32::from(pressed)) } }
|
||||
@@ -624,7 +694,12 @@ impl InputInjector {
|
||||
if let Some(code)=crate::uinput::keysym_to_evdev(symbol) { return Self::uinput_result(uinput.key(code,pressed)); }
|
||||
}
|
||||
if self.backend==crate::desktop_backend::DesktopBackend::Kms { return -1; }
|
||||
if let Some(portal)=self.portal.lock().unwrap().as_ref() { return self.portal_result("keyboard", portal.key(symbol,pressed)); }
|
||||
if let Some(portal)=self.portal.lock().unwrap().as_ref() {
|
||||
let _serial=lock_portal_held();
|
||||
let result=self.portal_result("keyboard", portal.key(symbol,pressed));
|
||||
if result==0 { self.record_portal_key(symbol,pressed); }
|
||||
return result;
|
||||
}
|
||||
if self.backend==crate::desktop_backend::DesktopBackend::Portal {return self.portal_result("keyboard", Err("Portal input session is not bound".into()));}
|
||||
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)) } }
|
||||
|
||||
Reference in new issue
Block a user