Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(linux): address CodeRabbit re-review of the click-capture path

Benjamin Freemancb981dca94e5
diff --git a/electron/native/pipewire-capture/src/input.rs b/electron/native/pipewire-capture/src/input.rs
index dd47ef73e..a1d41c90d 100644
--- a/electron/native/pipewire-capture/src/input.rs
+++ b/electron/native/pipewire-capture/src/input.rs
@@ -24,6 +24,7 @@ use std::collections::HashSet;
use std::io::ErrorKind;
use std::path::PathBuf;
use std::sync::mpsc::Sender;
+use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;
@@ -79,13 +80,13 @@ pub fn spawn_readers(sender: &Sender<Message>) -> ClickCapture {
if std::env::var_os(DISABLE_ENV).is_some() {
return ClickCapture::Disabled;
}
- // Paths already given a reader. Only ever grows, so a device is never
- // double-read; the one case it misses is a device that unplugs and returns
- // on the SAME node path — a replug usually lands on a fresh `eventNN`, which
- // is not in the set and so is picked up.
- let mut opened: HashSet<PathBuf> = HashSet::new();
- scan_once(sender, &mut opened);
- let result = if opened.is_empty() {
+ // Paths with a LIVE reader. Shared with the reader threads: each removes its
+ // own path when it exits, so a device that unplugs and reconnects on the SAME
+ // node path is adopted again by the next scan. A set that only grew skipped
+ // such a replug for the rest of the recording.
+ let opened: Arc<Mutex<HashSet<PathBuf>>> = Arc::new(Mutex::new(HashSet::new()));
+ scan_once(sender, &opened);
+ let result = if opened.lock().unwrap().is_empty() {
ClickCapture::NoDevice
} else {
ClickCapture::Active
@@ -94,23 +95,28 @@ pub fn spawn_readers(sender: &Sender<Message>) -> ClickCapture {
// so a daemon thread re-scans and starts readers for nodes it has not seen.
// Detached, like the reader threads — it ends when the process does.
let watch_sender = sender.clone();
+ let watch_opened = Arc::clone(&opened);
thread::spawn(move || loop {
thread::sleep(RESCAN_INTERVAL);
- scan_once(&watch_sender, &mut opened);
+ scan_once(&watch_sender, &watch_opened);
});
result
}
-/// Spawns a reader for every `BTN_LEFT` device not already in `opened`, recording
-/// each newly opened node's path. Shared by the initial scan and the watcher.
-fn scan_once(sender: &Sender<Message>, opened: &mut HashSet<PathBuf>) {
+/// Spawns a reader for every `BTN_LEFT` device not already being read, recording
+/// each newly opened node's path. Shared by the initial scan and the watcher; the
+/// check-and-insert is one locked step so two scans cannot both adopt one path.
+fn scan_once(sender: &Sender<Message>, opened: &Arc<Mutex<HashSet<PathBuf>>>) {
for (path, device) in evdev::enumerate() {
- if opened.contains(&path) || !device_reports_left_button(&device) {
+ if !device_reports_left_button(&device) {
continue;
}
- opened.insert(path);
+ if !opened.lock().unwrap().insert(path.clone()) {
+ continue; // already has a live reader
+ }
let forward = sender.clone();
- thread::spawn(move || read_device(device, forward));
+ let owned = Arc::clone(opened);
+ thread::spawn(move || read_device(device, path, forward, owned));
}
}
@@ -132,7 +138,18 @@ fn device_reports_left_button(device: &Device) -> bool {
/// next cursor sample. Returns when the device fails terminally (e.g. unplugged)
/// or the loop's channel has closed, so the thread cannot outlive the recording
/// it serves. A transient `EINTR` is retried, not mistaken for an unplug.
-fn read_device(mut device: Device, sender: Sender<Message>) {
+///
+/// On EVERY exit it drops `path` from `opened`, so a device reconnecting on the
+/// same node path is re-adopted by the next scan.
+fn read_device(
+ mut device: Device,
+ path: PathBuf,
+ sender: Sender<Message>,
+ opened: Arc<Mutex<HashSet<PathBuf>>>,
+) {
+ let release = || {
+ opened.lock().unwrap().remove(&path);
+ };
loop {
let events = match device.fetch_events() {
Ok(events) => events,
@@ -142,6 +159,7 @@ fn read_device(mut device: Device, sender: Sender<Message>) {
// than ending silently, so click capture going quiet mid-recording
// is answerable from the log; the watcher re-adopts it on a replug.
Err(err) => {
+ release();
let _ = sender.send(Message::PointerDeviceLost(err.to_string()));
return;
}
@@ -150,6 +168,7 @@ fn read_device(mut device: Device, sender: Sender<Message>) {
if is_left_button_press(event.event_type(), event.code(), event.value())
&& sender.send(Message::PointerButton(timestamp_ms())).is_err()
{
+ release();
return;
}
}
diff --git a/electron/native/pipewire-capture/src/main.rs b/electron/native/pipewire-capture/src/main.rs
index 51ce8e5b4..5da1877e3 100644
--- a/electron/native/pipewire-capture/src/main.rs
+++ b/electron/native/pipewire-capture/src/main.rs
@@ -608,14 +608,17 @@ fn run<W: Write>(
let mut cursor: Option<CursorState> = None;
let mut known_assets: HashSet<String> = HashSet::new();
let mut pending_asset: Option<CursorAsset> = None;
- // The pw_stream has reached `streaming`, i.e. mutter has actually started
- // handing us frames. Presses are ignored until then: before it, the only
- // thing on screen is the portal's own picker, and the click that dismisses
- // it (its "Share" button) would otherwise latch and ride out on the
- // recording's first sample as a phantom click at t≈0. mutter enables its
- // capture source on STREAMING, so this is the exact edge at which a press
- // starts landing on content the recording contains.
- let mut streaming = false;
+ // Wall-clock ms at which the pw_stream last reached `streaming` (mutter began
+ // handing us frames), or `None` when it is not streaming. A press counts only
+ // if its own read time is at or after this: before streaming the only thing on
+ // screen is the portal picker, and the click that dismisses it (its "Share"
+ // button) would otherwise ride out on the first sample as a phantom click at
+ // t≈0. Comparing timestamps rather than a bare flag closes two gaps: the press
+ // and the stream-state arrive on DIFFERENT channels, so a pre-stream press can
+ // be dequeued after the flag flips (it is still dropped, its time is older);
+ // and clearing it on disconnect stops clicks emitting against a stale cursor
+ // after capture has stopped.
+ let mut streaming_since: Option<u64> = None;
let mut reported_cursor_meta = false;
// Allocated up front so the PipeWire callback has somewhere to put frames
// from the very first buffer; `None` in cursor-only mode, which is also what
@@ -680,18 +683,13 @@ fn run<W: Write>(
// stamped with the press time the reader captured: immediate so it is
// not backdated to the next throttled sample, and one sample per press
// so a rapid double-click reads as two clicks rather than collapsing
- // into one. Dropped before the stream is live (see `streaming`): a
- // press while the picker is still up is the click on its "Share"
- // button, not content.
+ // into one. Counted only if the press happened at or after streaming
+ // began (see `streaming_since`): a press while the picker is still up —
+ // the click on its "Share" button — is older, so it is dropped even if
+ // its message is delivered after the stream-state one.
Ok(Message::PointerButton(press_ms)) => {
- if streaming {
- emit_sample(
- emitter,
- &cursor,
- size,
- &mut pending_asset,
- Some(press_ms),
- );
+ if streaming_since.is_some_and(|since| press_ms >= since) {
+ emit_sample(emitter, &cursor, size, &mut pending_asset, Some(press_ms));
}
}
@@ -1116,8 +1114,16 @@ fn run<W: Write>(
});
// Once frames are flowing, presses land on recorded content; the
// picker (and the "Share" click that dismissed it) is behind us.
+ // Stamped so a press read before this instant is dropped by time,
+ // and cleared on disconnect so clicks stop emitting against a stale
+ // cursor after capture ends. Only set on the FIRST streaming edge so
+ // a transient renegotiation `paused`→`streaming` does not re-arm it.
if state == "streaming" {
- streaming = true;
+ if streaming_since.is_none() {
+ streaming_since = Some(timestamp_ms());
+ }
+ } else if state == "unconnected" {
+ streaming_since = None;
}
if let Some(error) = error {
let _ = emitter.emit(&Event::Warning {
diff --git a/website/docs/installation.md b/website/docs/installation.md
index f5deef83d..5d2c7545f 100644
--- a/website/docs/installation.md
+++ b/website/docs/installation.md
@@ -132,7 +132,7 @@ The editing tools are the same everywhere — zooms, backgrounds, crop/trim/spee
| | macOS | Windows | Linux |
|---|---|---|---|
| Capture pipeline | Native (ScreenCaptureKit) | Native (Windows Graphics Capture) | Browser pipeline |
-| Custom cursor themes / click effects | ✅ | ✅ | ❌ (position-only, used for auto-zoom) |
+| Custom cursor themes / click effects | ✅ | ✅ | ✅ on Wayland — click capture needs the `input` group ([details](#mouse-clicks-on-wayland)) |
| Webcam | Native capture | Native capture | Browser capture (still works as PiP) |
| System audio | macOS 13+; permission prompt on 14.2+; not available on macOS 12 and below | Works out of the box | Needs PipeWire (default on Ubuntu 22.04+, Fedora 34+) |
| MP4 export | ✅ | ✅ | ✅ (software encode) |