Skip to content

Commit

Permalink
Merge pull request #16 from Lenivaya/filter-battery-events-in-monitor
Browse files Browse the repository at this point in the history
feat: add event filtering for battery-related events
  • Loading branch information
Lenivaya authored Nov 13, 2024
2 parents 3d9fe32 + 0ebc365 commit 331d786
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/udev_power_monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,35 @@ impl UdevPowerMonitor {
for event in events.iter() {
if let UDEV = event.token() {
if let Some(udev_event) = socket.iter().next() {
self.handle_event(&udev_event);
if self.filter_event(&udev_event) {
self.handle_event(&udev_event);
}
}
}
}
}
}

/// Filters out events that are not related to battery directly
fn filter_event(&self, event: &udev::Event) -> bool {
let sysname = event.sysname().to_str().unwrap_or("");

sysname.starts_with("BAT")
|| sysname.starts_with("AC")
|| self.filter_event_properties(event)
}

fn filter_event_properties(&self, event: &udev::Event) -> bool {
event
.properties()
.any(|p| self.filter_event_property(&p))
}

fn filter_event_property(&self, property: &udev::Entry) -> bool {
(property.name() == "POWER_SUPPLY_TYPE" && property.value() == "Battery")
|| (property.name() == "POWER_SUPPLY_NAME" && property.value() == "AC")
}

fn handle_event(&self, event: &udev::Event) {
let power_state = PowerState::from(event.attribute_value("online"));

Expand Down

0 comments on commit 331d786

Please sign in to comment.