1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-08-17 23:01:04 +02:00

refactor: upgrade to zbus v5

Also drops the deprecated `upower-dbus` crate
This commit is contained in:
Jake Stanger 2025-02-08 01:54:17 +00:00
parent 75375aa341
commit 63f5954837
No known key found for this signature in database
GPG key ID: C51FC8F9CB0BEA61
10 changed files with 388 additions and 336 deletions

View file

@ -165,7 +165,7 @@ impl Clients {
if let Some(client) = &self.network_manager {
Ok(client.clone())
} else {
let client = networkmanager::create_client()?;
let client = await_sync(async move { networkmanager::create_client().await })?;
self.network_manager = Some(client.clone());
Ok(client)
}

View file

@ -1,18 +1,18 @@
use std::sync::Arc;
use crate::{register_fallible_client, spawn};
use color_eyre::Result;
use futures_signals::signal::{Mutable, MutableSignalCloned};
use tracing::error;
use zbus::blocking::fdo::PropertiesProxy;
use zbus::blocking::Connection;
use zbus::export::ordered_stream::OrderedStreamExt;
use zbus::fdo::PropertiesProxy;
use zbus::{
dbus_proxy,
names::InterfaceName,
proxy,
zvariant::{ObjectPath, Str},
Connection,
};
use crate::{register_fallible_client, spawn_blocking};
const DBUS_BUS: &str = "org.freedesktop.NetworkManager";
const DBUS_PATH: &str = "/org/freedesktop/NetworkManager";
const DBUS_INTERFACE: &str = "org.freedesktop.NetworkManager";
@ -36,40 +36,41 @@ pub enum ClientState {
Unknown,
}
#[dbus_proxy(
#[proxy(
default_service = "org.freedesktop.NetworkManager",
interface = "org.freedesktop.NetworkManager",
default_path = "/org/freedesktop/NetworkManager"
)]
trait NetworkManagerDbus {
#[dbus_proxy(property)]
#[zbus(property)]
fn active_connections(&self) -> Result<Vec<ObjectPath>>;
#[dbus_proxy(property)]
#[zbus(property)]
fn devices(&self) -> Result<Vec<ObjectPath>>;
#[dbus_proxy(property)]
#[zbus(property)]
fn networking_enabled(&self) -> Result<bool>;
#[dbus_proxy(property)]
#[zbus(property)]
fn primary_connection(&self) -> Result<ObjectPath>;
#[dbus_proxy(property)]
#[zbus(property)]
fn primary_connection_type(&self) -> Result<Str>;
#[dbus_proxy(property)]
#[zbus(property)]
fn wireless_enabled(&self) -> Result<bool>;
}
impl Client {
fn new() -> Result<Self> {
async fn new() -> Result<Self> {
let client_state = Mutable::new(ClientState::Unknown);
let dbus_connection = Connection::system()?;
let dbus_connection = Connection::system().await?;
let interface_name = InterfaceName::from_static_str(DBUS_INTERFACE)?;
let props_proxy = PropertiesProxy::builder(&dbus_connection)
.destination(DBUS_BUS)?
.path(DBUS_PATH)?
.build()?;
.build()
.await?;
Ok(Self {
client_state,
@ -79,12 +80,12 @@ impl Client {
})
}
fn run(&self) -> Result<()> {
let proxy = NetworkManagerDbusProxyBlocking::new(&self.dbus_connection)?;
async fn run(&self) -> Result<()> {
let proxy = NetworkManagerDbusProxy::new(&self.dbus_connection).await?;
let mut primary_connection = proxy.primary_connection()?;
let mut primary_connection_type = proxy.primary_connection_type()?;
let mut wireless_enabled = proxy.wireless_enabled()?;
let mut primary_connection = proxy.primary_connection().await?;
let mut primary_connection_type = proxy.primary_connection_type().await?;
let mut wireless_enabled = proxy.wireless_enabled().await?;
self.client_state.set(determine_state(
&primary_connection,
@ -92,7 +93,8 @@ impl Client {
wireless_enabled,
));
for change in self.props_proxy.receive_properties_changed()? {
let mut stream = self.props_proxy.receive_properties_changed().await?;
while let Some(change) = stream.next().await {
let args = change.args()?;
if args.interface_name != self.interface_name {
continue;
@ -102,15 +104,15 @@ impl Client {
let mut relevant_prop_changed = false;
if changed_props.contains_key("PrimaryConnection") {
primary_connection = proxy.primary_connection()?;
primary_connection = proxy.primary_connection().await?;
relevant_prop_changed = true;
}
if changed_props.contains_key("PrimaryConnectionType") {
primary_connection_type = proxy.primary_connection_type()?;
primary_connection_type = proxy.primary_connection_type().await?;
relevant_prop_changed = true;
}
if changed_props.contains_key("WirelessEnabled") {
wireless_enabled = proxy.wireless_enabled()?;
wireless_enabled = proxy.wireless_enabled().await?;
relevant_prop_changed = true;
}
@ -131,12 +133,12 @@ impl Client {
}
}
pub fn create_client() -> Result<Arc<Client>> {
let client = Arc::new(Client::new()?);
pub async fn create_client() -> Result<Arc<Client>> {
let client = Arc::new(Client::new().await?);
{
let client = client.clone();
spawn_blocking(move || {
if let Err(error) = client.run() {
spawn(async move {
if let Err(error) = client.run().await {
error!("{}", error);
};
});

View file

@ -20,12 +20,14 @@
//! [Writing a client proxy]: https://dbus2.github.io/zbus/client.html
//! [D-Bus standard interfaces]: https://dbus.freedesktop.org/doc/dbus-specification.html#standard-interfaces,
#[zbus::dbus_proxy(
use zbus::proxy;
#[proxy(
interface = "org.erikreider.swaync.cc",
default_service = "org.erikreider.swaync.cc",
default_path = "/org/erikreider/swaync/cc"
)]
trait SwayNc {
pub trait SwayNc {
/// AddInhibitor method
fn add_inhibitor(&self, application_id: &str) -> zbus::Result<bool>;
@ -90,11 +92,11 @@ trait SwayNc {
fn toggle_visibility(&self) -> zbus::Result<()>;
/// Subscribe signal
#[dbus_proxy(signal)]
#[zbus(signal)]
fn subscribe(&self, count: u32, dnd: bool, cc_open: bool) -> zbus::Result<()>;
/// SubscribeV2 signal
#[dbus_proxy(signal)]
#[zbus(signal)]
fn subscribe_v2(
&self,
count: u32,
@ -104,8 +106,8 @@ trait SwayNc {
) -> zbus::Result<()>;
/// Inhibited property
#[dbus_proxy(property)]
#[zbus(property)]
fn inhibited(&self) -> zbus::Result<bool>;
#[dbus_proxy(property)]
#[zbus(property)]
fn set_inhibited(&self, value: bool) -> zbus::Result<()>;
}

View file

@ -54,7 +54,11 @@ impl Client {
spawn(async move {
while let Some(ev) = stream.next().await {
let ev = ev.body::<Event>().expect("to deserialize");
let ev = ev
.message()
.body()
.deserialize::<Event>()
.expect("to deserialize");
debug!("Received event: {ev:?}");
send!(tx, ev);
}

View file

@ -0,0 +1,122 @@
/// Originally taken from `upower-dbus` crate
/// https://github.com/pop-os/upower-dbus/blob/main/LICENSE
// Copyright 2021 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
use zbus::proxy;
use zbus::zvariant::OwnedValue;
#[derive(Debug, Copy, Clone, PartialEq, Eq, OwnedValue)]
#[repr(u32)]
pub enum BatteryState {
Unknown = 0,
Charging = 1,
Discharging = 2,
Empty = 3,
FullyCharged = 4,
PendingCharge = 5,
PendingDischarge = 6,
}
#[derive(Debug, Copy, Clone, OwnedValue)]
#[repr(u32)]
pub enum BatteryType {
Unknown = 0,
LinePower = 1,
Battery = 2,
Ups = 3,
Monitor = 4,
Mouse = 5,
Keyboard = 6,
Pda = 7,
Phone = 8,
}
#[derive(Debug, Copy, Clone, OwnedValue)]
#[repr(u32)]
pub enum BatteryLevel {
Unknown = 0,
None = 1,
Low = 3,
Critical = 4,
Normal = 6,
High = 7,
Full = 8,
}
#[proxy(
interface = "org.freedesktop.UPower.Device",
default_service = "org.freedesktop.UPower",
assume_defaults = false
)]
pub trait Device {
#[zbus(property)]
fn battery_level(&self) -> zbus::Result<BatteryLevel>;
#[zbus(property)]
fn capacity(&self) -> zbus::Result<f64>;
#[zbus(property)]
fn energy(&self) -> zbus::Result<f64>;
#[zbus(property)]
fn energy_empty(&self) -> zbus::Result<f64>;
#[zbus(property)]
fn energy_full(&self) -> zbus::Result<f64>;
#[zbus(property)]
fn energy_full_design(&self) -> zbus::Result<f64>;
#[zbus(property)]
fn has_history(&self) -> zbus::Result<bool>;
#[zbus(property)]
fn has_statistics(&self) -> zbus::Result<bool>;
#[zbus(property)]
fn icon_name(&self) -> zbus::Result<String>;
#[zbus(property)]
fn is_present(&self) -> zbus::Result<bool>;
#[zbus(property)]
fn is_rechargeable(&self) -> zbus::Result<bool>;
#[zbus(property)]
fn luminosity(&self) -> zbus::Result<f64>;
#[zbus(property)]
fn model(&self) -> zbus::Result<String>;
#[zbus(property)]
fn native_path(&self) -> zbus::Result<String>;
#[zbus(property)]
fn online(&self) -> zbus::Result<bool>;
#[zbus(property)]
fn percentage(&self) -> zbus::Result<f64>;
#[zbus(property)]
fn power_supply(&self) -> zbus::Result<bool>;
fn refresh(&self) -> zbus::Result<()>;
#[zbus(property)]
fn serial(&self) -> zbus::Result<String>;
#[zbus(property)]
fn state(&self) -> zbus::Result<BatteryState>;
#[zbus(property)]
fn temperature(&self) -> zbus::Result<f64>;
#[zbus(property, name = "Type")]
fn type_(&self) -> zbus::Result<BatteryType>;
#[zbus(property)]
fn vendor(&self) -> zbus::Result<String>;
#[zbus(property)]
fn voltage(&self) -> zbus::Result<f64>;
}

View file

@ -1,8 +1,14 @@
mod device;
mod upower;
use crate::clients::ClientResult;
use crate::register_fallible_client;
use std::sync::Arc;
use upower_dbus::UPowerProxy;
use upower::UPowerProxy;
use zbus::fdo::PropertiesProxy;
use zbus::proxy::CacheProperties;
pub use device::BatteryState;
pub async fn create_display_proxy() -> ClientResult<PropertiesProxy<'static>> {
let dbus = Box::pin(zbus::Connection::system()).await?;
@ -11,14 +17,14 @@ pub async fn create_display_proxy() -> ClientResult<PropertiesProxy<'static>> {
let display_device = device_proxy.get_display_device().await?;
let path = display_device.path().to_owned();
let path = display_device.inner().path();
let proxy = PropertiesProxy::builder(&dbus)
.destination("org.freedesktop.UPower")
.expect("failed to set proxy destination address")
.path(path)
.expect("failed to set proxy path")
.cache_properties(zbus::CacheProperties::No)
.cache_properties(CacheProperties::No)
.build()
.await?;

View file

@ -0,0 +1,43 @@
use super::device::DeviceProxy;
/// Originally taken from `upower-dbus` crate
/// https://github.com/pop-os/upower-dbus/blob/main/LICENSE
// Copyright 2021 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
use zbus::proxy;
#[proxy(interface = "org.freedesktop.UPower", assume_defaults = true)]
pub trait UPower {
/// EnumerateDevices method
fn enumerate_devices(&self) -> zbus::Result<Vec<zbus::zvariant::OwnedObjectPath>>;
/// GetCriticalAction method
fn get_critical_action(&self) -> zbus::Result<String>;
/// GetDisplayDevice method
#[zbus(object = "Device")]
fn get_display_device(&self);
/// DeviceAdded signal
#[zbus(signal)]
fn device_added(&self, device: zbus::zvariant::ObjectPath<'_>) -> zbus::Result<()>;
/// DeviceRemoved signal
#[zbus(signal)]
fn device_removed(&self, device: zbus::zvariant::ObjectPath<'_>) -> zbus::Result<()>;
/// DaemonVersion property
#[zbus(property)]
fn daemon_version(&self) -> zbus::Result<String>;
/// LidIsClosed property
#[zbus(property)]
fn lid_is_closed(&self) -> zbus::Result<bool>;
/// LidIsPresent property
#[zbus(property)]
fn lid_is_present(&self) -> zbus::Result<bool>;
/// OnBattery property
#[zbus(property)]
fn on_battery(&self) -> zbus::Result<bool>;
}