From 48c58357a7e4447bd960bc805bac91b15acd5a12 Mon Sep 17 00:00:00 2001 From: HighestFidelity <157998857+HighestFidelity@users.noreply.github.com> Date: Fri, 13 Nov 2026 02:04:39 +0000 Subject: [PATCH] fix(service): show the battery right away when the mouse shows up just after Bridge starts The device scanner only triggered a battery read when a mouse appeared versus the previous scan, and the first scan has no previous, so a dongle that enumerated after the startup poll left the tray blank until the next 5 minute poll. The first scan now reads once if a mouse is on the bus and no reading exists; scans during a running poll are judged after it ends instead of spawning a read the poll guard would drop. No retry loop, poll interval unchanged. Refs #79 Co-Authored-By: Claude Opus 5.5 --- src/service.rs | 46 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/src/service.rs b/src/service.rs index dcabf40..08297cf 100644 --- a/src/service.rs +++ b/src/service.rs @@ -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. @@ -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())); } @@ -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>, + ids: &HashSet, + 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); @@ -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 = ["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());