1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-09-18 12:46:58 +02:00

fix(networkmanager): notify upon new device from watch_device()

Additionally:
- Pass the device proxy to watch_device_state() now that we've had to
  create one anyway
- Improve event received logging in module widget
This commit is contained in:
Reinout Meliesie 2025-09-04 20:41:55 +02:00
commit a106f41b0a
Signed by: zedfrigg
GPG key ID: 3AFCC06481308BC6
2 changed files with 33 additions and 20 deletions

View file

@ -109,7 +109,7 @@ impl ClientInner {
for added_device_path in added_device_paths { for added_device_path in added_device_paths {
debug_assert!(!watchers.contains_key(added_device_path)); debug_assert!(!watchers.contains_key(added_device_path));
let watcher = self.watch_device(added_device_path.clone()); let watcher = self.watch_device(added_device_path.clone()).await?;
watchers.insert(added_device_path.clone(), watcher); watchers.insert(added_device_path.clone(), watcher);
} }
@ -162,23 +162,15 @@ impl ClientInner {
Ok(()) Ok(())
} }
fn watch_device(&'static self, path: ObjectPath<'static>) -> DeviceWatcher { async fn watch_device(&'static self, path: ObjectPath<'_>) -> Result<DeviceWatcher> {
let state_watcher = Arc::new(spawn(self.watch_device_state(path))); let dbus_connection = &self.dbus_connection().await?;
let proxy = DeviceDbusProxy::new(dbus_connection, path.to_owned()).await?;
DeviceWatcher { state_watcher }
}
async fn watch_device_state(&'static self, path: ObjectPath<'_>) -> Result<()> {
debug!("D-Bus device state watcher for {} starting", path);
let dbus_connection = Connection::system().await?;
let device = DeviceDbusProxy::new(&dbus_connection, path.clone()).await?;
let number = get_number_from_dbus_path(&path); let number = get_number_from_dbus_path(&path);
let r#type = device.device_type().await?; let r#type = proxy.device_type().await?;
let new_state = proxy.state().await?;
// Send an event communicating the initial state // Notify modules that the device exists even if its properties don't change
let new_state = device.state().await?;
self.controller_sender self.controller_sender
.send(ClientToModuleEvent::DeviceChanged { .send(ClientToModuleEvent::DeviceChanged {
number, number,
@ -186,9 +178,22 @@ impl ClientInner {
new_state, new_state,
})?; })?;
let mut state_changes = device.receive_state_changed().await; let state_watcher = Arc::new(spawn(self.watch_device_state(proxy)));
while let Some(state_change) = state_changes.next().await {
let new_state = state_change.get().await?; Ok(DeviceWatcher { state_watcher })
}
async fn watch_device_state(&'static self, proxy: DeviceDbusProxy<'_>) -> Result<()> {
let path = proxy.inner().path();
debug!("D-Bus device state watcher for {} starting", path);
let number = get_number_from_dbus_path(path);
let r#type = proxy.device_type().await?;
let mut changes = proxy.receive_state_changed().await;
while let Some(change) = changes.next().await {
let new_state = change.get().await?;
self.controller_sender self.controller_sender
.send(ClientToModuleEvent::DeviceChanged { .send(ClientToModuleEvent::DeviceChanged {
number, number,

View file

@ -100,6 +100,11 @@ async fn handle_update_events(
r#type, r#type,
new_state, new_state,
} => { } => {
debug!(
"Module widget received DeviceChanged event for number {}",
number
);
let icon: &_ = icons.entry(number).or_insert_with(|| { let icon: &_ = icons.entry(number).or_insert_with(|| {
debug!("Adding icon for device {}", number); debug!("Adding icon for device {}", number);
@ -124,13 +129,16 @@ async fn handle_update_events(
} }
} }
ClientToModuleEvent::DeviceRemoved { number } => { ClientToModuleEvent::DeviceRemoved { number } => {
debug!(
"Module widget received DeviceRemoved event for number {}",
number
);
let icon = icons let icon = icons
.get(&number) .get(&number)
.expect("The icon for {} was about to be removed but was not present"); .expect("The icon for {} was about to be removed but was not present");
container.remove(icon); container.remove(icon);
icons.remove(&number); icons.remove(&number);
debug!("Removed icon for device {}", number);
} }
} }
} }