Skip to content

Commit 006c6a4

Browse files
committed
fix: track pointer holds per device and release on loss
1 parent be9c643 commit 006c6a4

2 files changed

Lines changed: 72 additions & 9 deletions

File tree

‎electron/native/pipewire-capture/src/input.rs‎

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
2323
use std::collections::HashSet;
2424
use std::io::ErrorKind;
25-
use std::path::PathBuf;
25+
use std::path::{Path, PathBuf};
2626
use std::sync::mpsc::Sender;
2727
use std::sync::{Arc, Mutex};
2828
use std::thread;
@@ -162,14 +162,17 @@ fn read_device(
162162
// than ending silently, so click capture going quiet mid-recording
163163
// is answerable from the log; the watcher re-adopts it on a replug.
164164
Err(err) => {
165+
report_device_lost(&sender, &path, err.to_string());
165166
release();
166-
let _ = sender.send(Message::PointerDeviceLost(err.to_string()));
167167
return;
168168
}
169169
};
170170
for event in events {
171171
if let Some(down) = left_button_state(event.event_type(), event.code(), event.value()) {
172-
if sender.send(Message::PointerButton(timestamp_ms(), down)).is_err() {
172+
if sender
173+
.send(Message::PointerButton(path.clone(), timestamp_ms(), down))
174+
.is_err()
175+
{
173176
release();
174177
return;
175178
}
@@ -178,10 +181,37 @@ fn read_device(
178181
}
179182
}
180183

184+
fn report_device_lost(sender: &Sender<Message>, path: &Path, reason: String) {
185+
// The recording loop ignores releases for devices that are not held.
186+
// Send before freeing the path so a replug cannot overtake this release.
187+
let _ = sender.send(Message::PointerButton(path.to_path_buf(), timestamp_ms(), false));
188+
let _ = sender.send(Message::PointerDeviceLost(reason));
189+
}
190+
181191
#[cfg(test)]
182192
mod tests {
183193
use super::*;
184194

195+
#[test]
196+
fn device_loss_releases_its_button_before_reporting_the_error() {
197+
let (sender, receiver) = std::sync::mpsc::channel();
198+
let path = PathBuf::from("/dev/input/event-test");
199+
sender.send(Message::PointerButton(path.clone(), 123, true)).unwrap();
200+
report_device_lost(&sender, &path, "unplugged".into());
201+
assert!(matches!(
202+
receiver.try_recv().unwrap(),
203+
Message::PointerButton(device, 123, true) if device == path
204+
));
205+
assert!(matches!(
206+
receiver.try_recv().unwrap(),
207+
Message::PointerButton(device, _, false) if device == path
208+
));
209+
assert!(matches!(
210+
receiver.try_recv().unwrap(),
211+
Message::PointerDeviceLost(reason) if reason == "unplugged"
212+
));
213+
}
214+
185215
const BTN_LEFT: u16 = KeyCode::BTN_LEFT.0;
186216
const BTN_RIGHT: u16 = KeyCode::BTN_RIGHT.0;
187217

‎electron/native/pipewire-capture/src/main.rs‎

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,12 +223,12 @@ struct AudioSourceConfig {
223223
enum Message {
224224
Portal(Box<Result<portal::PortalStream, portal::PortalError>>),
225225
Stream(StreamEvent),
226-
/// A left mouse-button edge observed on evdev, carrying the event time in
227-
/// milliseconds so the emitted click sample is stamped when it happened
228-
/// rather than at the next throttled sample. See [`input`] for why this is
226+
/// A left mouse-button edge observed on evdev, carrying its device path
227+
/// and time in milliseconds so the emitted click sample is stamped when
228+
/// it happened rather than at the next throttled sample. See [`input`] for why this is
229229
/// the only way to see a button on Wayland, and for its permission and
230230
/// privacy model.
231-
PointerButton(u64, bool),
231+
PointerButton(PathBuf, u64, bool),
232232
/// A pointer reader thread stopped on a device error (typically an unplug);
233233
/// the string is the OS error. Surfaced as a warning so click capture going
234234
/// quiet mid-recording is not silent.
@@ -672,6 +672,7 @@ fn run<W: Write>(
672672
// picker has been answered.
673673
let mut armed = false;
674674
let mut cursor: Option<CursorState> = None;
675+
let mut held_pointer_devices = HashSet::new();
675676
let mut known_assets: HashSet<String> = HashSet::new();
676677
let mut pending_asset: Option<CursorAsset> = None;
677678
// Wall-clock ms at which the pw_stream last reached `streaming` (mutter began
@@ -785,8 +786,9 @@ fn run<W: Write>(
785786
// began (see `streaming_since`): a press while the picker is still up —
786787
// the click on its "Share" button — is older, so it is dropped even if
787788
// its message is delivered after the stream-state one.
788-
Ok(Message::PointerButton(press_ms, down)) => {
789-
if streaming_since.is_some_and(|since| press_ms >= since) {
789+
Ok(Message::PointerButton(device, press_ms, down)) => {
790+
let edge = pointer_button_edge(&mut held_pointer_devices, device, down);
791+
if streaming_since.is_some_and(|since| press_ms >= since) && edge.is_some() {
790792
emit_sample(emitter, &cursor, content_rect(&capture, size), &mut pending_asset, Some((press_ms, down)));
791793
}
792794
}
@@ -1525,6 +1527,37 @@ fn content_rect(capture: &Option<Capture>, size: Option<(i32, i32)>) -> Option<s
15251527
}
15261528
}
15271529

1530+
fn pointer_button_edge(held: &mut HashSet<PathBuf>, device: PathBuf, down: bool) -> Option<bool> {
1531+
if down {
1532+
held.insert(device);
1533+
Some(true)
1534+
} else if held.remove(&device) && held.is_empty() {
1535+
Some(false)
1536+
} else {
1537+
None
1538+
}
1539+
}
1540+
1541+
#[cfg(test)]
1542+
mod pointer_button_tests {
1543+
use super::*;
1544+
1545+
#[test]
1546+
fn one_device_release_does_not_end_another_devices_hold() {
1547+
let mut held = HashSet::new();
1548+
let mouse = PathBuf::from("/dev/input/mouse");
1549+
let touchpad = PathBuf::from("/dev/input/touchpad");
1550+
assert_eq!(pointer_button_edge(&mut held, mouse.clone(), true), Some(true));
1551+
assert_eq!(
1552+
pointer_button_edge(&mut held, touchpad.clone(), true),
1553+
Some(true)
1554+
);
1555+
assert_eq!(pointer_button_edge(&mut held, mouse.clone(), false), None);
1556+
assert_eq!(pointer_button_edge(&mut held, touchpad, false), Some(false));
1557+
assert_eq!(pointer_button_edge(&mut held, mouse, false), None);
1558+
}
1559+
}
1560+
15281561
/// Emits one cursor sample, positioned inside `content`.
15291562
///
15301563
/// `content` is the sub-rectangle of the stream the recording actually shows —

0 commit comments

Comments
 (0)