Skip to content
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions src/drivers/lamzu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,11 @@ pub(super) fn read_status() -> Result<Option<NativeStatus>> {
.find(|(id, _)| *id == interface.product_id)
.map(|(_, name)| (*name).to_owned());
return Ok(Some(NativeStatus {
battery: NativeBattery {
battery: Some(NativeBattery {
percent,
charging,
reported_name: name,
},
}),
dpi: None,
polling_rate_hz: None,
switch_mode: None,
Expand Down
67 changes: 50 additions & 17 deletions src/drivers/native_hid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ pub struct NativeBattery {
/// battery plus the settings currently in effect.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct NativeStatus {
pub battery: NativeBattery,
/// `None` for a mouse that reports its settings but no battery (the
/// 8BitDo Retro R8 through its receiver).
pub battery: Option<NativeBattery>,
pub dpi: Option<u32>,
pub polling_rate_hz: Option<u32>,
/// Logitech's LightForce switch mode; `None` for brands without one.
Expand Down Expand Up @@ -230,16 +232,21 @@ pub(super) fn read_settings(
)))
}

/// The reply's battery and settings, or `None` when it carries neither. A
/// driver fills in 0 for DPI and polling rate it could not read (a mouse
/// asleep behind its receiver), so 0 reads as unknown.
fn parse_status(reply: StatusReply) -> Option<NativeStatus> {
let battery = parse_battery(&reply)?;
Some(NativeStatus {
battery,
dpi: coerce_dpi(reply.dpi.as_ref()),
let status = NativeStatus {
battery: parse_battery(&reply),
dpi: coerce_dpi(reply.dpi.as_ref()).filter(|dpi| *dpi > 0),
polling_rate_hz: reply
.polling_rate_hz
.and_then(|rate| u32::try_from(rate.round() as i64).ok()),
.and_then(|rate| u32::try_from(rate.round() as i64).ok())
.filter(|rate| *rate > 0),
switch_mode: reply.switch_mode.filter(|mode| !mode.trim().is_empty()),
})
};
let has_settings = status.dpi.is_some() || status.polling_rate_hz.is_some();
(status.battery.is_some() || has_settings).then_some(status)
}

fn parse_battery(reply: &StatusReply) -> Option<NativeBattery> {
Expand Down Expand Up @@ -555,11 +562,11 @@ mod tests {
assert_eq!(
parse_status(reply(Some(41.0), Some("Discharging"))),
Some(NativeStatus {
battery: NativeBattery {
battery: Some(NativeBattery {
percent: 41,
charging: false,
reported_name: None,
},
}),
dpi: None,
polling_rate_hz: None,
switch_mode: None,
Expand All @@ -568,38 +575,42 @@ mod tests {
assert_eq!(
parse_status(reply(Some(87.6), Some("Charging slowly"))),
Some(NativeStatus {
battery: NativeBattery {
battery: Some(NativeBattery {
percent: 88,
charging: true,
reported_name: None,
},
}),
dpi: None,
polling_rate_hz: None,
switch_mode: None,
})
);
assert_eq!(parse_status(reply(None, Some("Unknown"))), None);
assert_eq!(
parse_status(reply(Some(140.0), None)).map(|status| status.battery.percent),
parse_status(reply(Some(140.0), None))
.and_then(|status| status.battery)
.map(|battery| battery.percent),
Some(100)
);
// The driver's model name rides along for better tray labels.
assert_eq!(
parse_status(named_reply("G Pro X Superlight 2")),
Some(NativeStatus {
battery: NativeBattery {
battery: Some(NativeBattery {
percent: 41,
charging: false,
reported_name: Some("G Pro X Superlight 2".into()),
},
}),
dpi: None,
polling_rate_hz: None,
switch_mode: None,
})
);
// A blank name is treated as no name.
assert_eq!(
parse_status(named_reply(" ")).and_then(|status| status.battery.reported_name),
parse_status(named_reply(" "))
.and_then(|status| status.battery)
.and_then(|battery| battery.reported_name),
None
);
}
Expand All @@ -614,11 +625,11 @@ mod tests {
assert_eq!(
parse_status(logitech),
Some(NativeStatus {
battery: NativeBattery {
battery: Some(NativeBattery {
percent: 81,
charging: false,
reported_name: Some("G Pro X Superlight 2".into()),
},
}),
dpi: Some(800),
polling_rate_hz: Some(1000),
switch_mode: Some("Optical".into()),
Expand All @@ -634,6 +645,28 @@ mod tests {
assert_eq!(status.switch_mode, None);
}

#[test]
fn keeps_the_settings_of_a_mouse_without_a_battery() {
// The 8BitDo Retro R8 through its receiver: settings, no battery.
let mut r8 = reply(None, Some("Unknown"));
r8.dpi = Some(serde_json::json!(1200));
r8.polling_rate_hz = Some(500.0);
assert_eq!(
parse_status(r8),
Some(NativeStatus {
battery: None,
dpi: Some(1200),
polling_rate_hz: Some(500),
switch_mode: None,
})
);
// Asleep, the driver fills both in with 0: nothing was read.
let mut asleep = reply(None, Some("Unknown"));
asleep.dpi = Some(serde_json::json!(0));
asleep.polling_rate_hz = Some(0.0);
assert_eq!(parse_status(asleep), None);
}

#[test]
fn a_release_build_uses_the_checkout_only_from_inside_it() {
let exe = std::env::current_exe().unwrap();
Expand Down
4 changes: 2 additions & 2 deletions src/drivers/pulsar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ pub fn read_status() -> Result<Option<NativeStatus>> {
return Ok(None);
}
Ok(Some(NativeStatus {
battery: NativeBattery {
battery: Some(NativeBattery {
percent,
charging: response[6] == 1,
// The report-8 battery query carries no model name; the HID
// product string discovery already read is the best
// available.
reported_name: None,
},
}),
dpi,
polling_rate_hz,
// The report-8 protocol has no LightForce equivalent.
Expand Down
89 changes: 78 additions & 11 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{
applications::{self, ApplicationInfo},
config::{self, ApplicationProfile, BridgeConfig, GameConfig, LocalApplication},
devices::{self, DeviceStore, KnownDevice},
drivers,
drivers::{self, NativeStatus},
games::GameDetector,
platform,
};
Expand Down Expand Up @@ -1539,6 +1539,15 @@ impl BridgeService {
Ok(alert)
}

/// Records a native read of a mouse that reports its settings but no
/// battery: its row shows them and it counts as awake. The battery list,
/// and so the tray and the low-battery alert, stay as they were.
pub async fn record_native_settings(&self, update: KnownDevice) {
let mut state = self.inner.write().await;
state.asleep.remove(&update.id);
remember_known(&mut state.known, update);
}

/// Reads the battery of every mouse in the saved profiles on an interval
/// and records it like a reading from the control panel, which raises the
/// low-battery notification. Polling pauses while the control panel holds
Expand Down Expand Up @@ -2112,15 +2121,44 @@ async fn poll_devices(service: &BridgeService, recorded: &mut usize, failed: &mu
};
let outcome = read.await;
match outcome {
Ok(Ok(Some(status))) => {
Ok(Ok(Some(NativeStatus {
battery: None,
dpi,
polling_rate_hz,
switch_mode,
}))) => {
// Settings but no battery: the mouse answered, so its row
// shows them, while the tray and the battery alerts have
// nothing to go on.
service
.record_native_settings(KnownDevice {
id: device_id.clone(),
name: device_name,
last_seen: devices::unix_now(),
percent: None,
charging: None,
dpi,
polling_rate_hz,
switch_mode,
via: read_through(&device_id, &discovered),
native: true,
})
.await;
*recorded += 1;
}
Ok(Ok(Some(NativeStatus {
battery: Some(battery),
dpi,
polling_rate_hz,
switch_mode,
}))) => {
// The web panel reports batteries as `<brand>:<model>` (the
// driver's own model name), while discovery ids use the HID
// descriptor's tail (a LIGHTSPEED receiver reads
// `Logitech:046d:c547`). Recording under the panel's name
// for the same physical mouse keeps one entry instead of
// duplicate rows; the discovery-shaped entry is dropped.
let record_id = status
.battery
let record_id = battery
.reported_name
.as_ref()
.map(|name| canonical_battery_identity(&device_id, name))
Expand All @@ -2133,15 +2171,15 @@ async fn poll_devices(service: &BridgeService, recorded: &mut usize, failed: &mu
save_known(&mut state.known);
}
}
let record_name = status.battery.reported_name.clone().unwrap_or(device_name);
let record_name = battery.reported_name.unwrap_or(device_name);
let reading = BatteryReading {
device_id: record_id,
device_name: record_name,
percent: status.battery.percent,
charging: status.battery.charging,
dpi: status.dpi,
polling_rate_hz: status.polling_rate_hz,
switch_mode: status.switch_mode,
percent: battery.percent,
charging: battery.charging,
dpi,
polling_rate_hz,
switch_mode,
via: read_through(&device_id, &discovered),
native: true,
};
Expand All @@ -2152,7 +2190,7 @@ async fn poll_devices(service: &BridgeService, recorded: &mut usize, failed: &mu
}
}
Ok(Ok(None)) => {
tracing::debug!(%device_id, "No native battery reader for this device");
tracing::debug!(%device_id, "No native battery or settings reader for this device");
}
Ok(Err(error)) => {
tracing::debug!(%error, %device_id, "Could not read the mouse battery");
Expand Down Expand Up @@ -3351,6 +3389,35 @@ mod tests {
assert_eq!(known[0].percent, Some(40));
}

#[tokio::test]
async fn a_mouse_without_a_battery_shows_its_settings_but_no_battery() {
let bridge = service(BridgeConfig::default());
bridge.inner.write().await.asleep.insert("8BitDo:r8".into());
bridge
.record_native_settings(KnownDevice {
id: "8BitDo:r8".into(),
name: "8BitDo Retro R8 Mouse Adapter".into(),
last_seen: devices::unix_now(),
percent: None,
charging: None,
dpi: Some(1200),
polling_rate_hz: Some(500),
switch_mode: None,
via: None,
native: true,
})
.await;
let snapshot = bridge.snapshot().await;
// Nothing for the tray or the low-battery alert to show.
assert!(snapshot.batteries.is_empty());
let known = &snapshot.known_devices;
assert_eq!(known.len(), 1);
assert_eq!(known[0].state, KnownDeviceState::Connected);
assert_eq!(known[0].percent, None);
assert_eq!(known[0].dpi, Some(1200));
assert_eq!(known[0].polling_rate_hz, Some(500));
}

#[tokio::test]
async fn forgetting_a_mouse_removes_it_from_devices_json() {
let path = temp_config_path();
Expand Down