Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 40 additions & 6 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1573,10 +1573,7 @@ impl BridgeService {
.iter()
.map(|mouse| mouse.device_id.clone())
.collect();
let appeared = previous
.as_ref()
.is_some_and(|before| ids.iter().any(|id| !before.contains(id)));
let recheck = {
let (recheck, have_reading) = {
let mut state = service.inner.write().await;
remember_detected(&mut state.known, &detected);
// A mouse whose receiver left is disconnected, not asleep.
Expand All @@ -1588,9 +1585,18 @@ impl BridgeService {
})
});
state.detected = Some(detected);
!state.asleep.is_empty() && last_recheck.elapsed() >= ASLEEP_RECHECK
(
!state.asleep.is_empty() && last_recheck.elapsed() >= ASLEEP_RECHECK,
!state.battery.is_empty(),
)
};
if appeared || recheck {
// A read started now would be dropped by the running poll's
// guard, so judge this scan after that poll ends: `previous`
// stays put and the first scan sees the startup poll's result.
if service.battery_polling.load(Ordering::Acquire) {
continue;
}
if scan_wants_read(previous.as_ref(), &ids, have_reading) || recheck {
last_recheck = Instant::now();
tokio::spawn(poll_batteries(service.clone()));
}
Expand Down Expand Up @@ -1961,6 +1967,20 @@ fn remember_detected(known: &mut DeviceStore, discovered: &[drivers::DetectedMou
}
}

/// Whether a device scan should read the batteries now: a mouse appeared on
/// the bus since the last scan, or, on the first scan, a mouse is on the bus
/// but the startup poll left no reading (#79).
fn scan_wants_read(
previous: Option<&HashSet<String>>,
ids: &HashSet<String>,
have_reading: bool,
) -> bool {
match previous {
Some(before) => ids.iter().any(|id| !before.contains(id)),
None => !have_reading && !ids.is_empty(),
}
}

/// Clears the polling flag when a poll ends, even by panic.
struct PollGuard(Arc<AtomicBool>);

Expand Down Expand Up @@ -3233,6 +3253,20 @@ mod tests {
.map(|device| device.state)
}

#[test]
fn the_first_scan_reads_a_mouse_already_on_the_bus_when_no_reading_landed() {
let ids: HashSet<String> = ["Logitech:046d:c547".to_string()].into();
let none = HashSet::new();
// #79: the dongle is on the bus at the first scan but the
// startup poll got nothing, so the tray stayed blank for 5 minutes.
assert!(scan_wants_read(None, &ids, false));
assert!(!scan_wants_read(None, &ids, true));
assert!(!scan_wants_read(None, &none, false));
// Later scans: only a newly appeared mouse triggers a read.
assert!(scan_wants_read(Some(&none), &ids, true));
assert!(!scan_wants_read(Some(&ids), &ids, false));
}

#[tokio::test]
async fn a_mouse_read_through_its_receiver_replaces_it_and_sleeps_and_wakes() {
let bridge = BridgeService::new(BridgeConfig::default(), temp_config_path());
Expand Down
Loading