From 4e3eb0a396a7f29148b9bebd8da029a957175339 Mon Sep 17 00:00:00 2001 From: Ian Douglas Scott Date: Thu, 13 Jun 2024 15:45:59 -0700 Subject: [PATCH] battery: Use `upower_dbus` instead of including bindings here --- Cargo.lock | 17 +- cosmic-applet-battery/Cargo.toml | 2 + cosmic-applet-battery/src/lib.rs | 1 - cosmic-applet-battery/src/upower.rs | 46 ------ cosmic-applet-battery/src/upower_device.rs | 151 +----------------- .../src/upower_kbdbacklight.rs | 25 +-- 6 files changed, 20 insertions(+), 222 deletions(-) delete mode 100644 cosmic-applet-battery/src/upower.rs diff --git a/Cargo.lock b/Cargo.lock index d4895e11..e88c0bae 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -963,6 +963,7 @@ dependencies = [ "tracing-log", "tracing-subscriber", "udev", + "upower_dbus", "zbus 4.2.2", ] @@ -6057,6 +6058,16 @@ version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c" +[[package]] +name = "upower_dbus" +version = "0.3.2" +source = "git+https://github.com/pop-os/dbus-settings-bindings?branch=upower#eb5cd58587db538fee38da45d560dc92631b8f99" +dependencies = [ + "serde", + "serde_repr", + "zbus 4.2.2", +] + [[package]] name = "url" version = "2.5.0" @@ -6448,7 +6459,7 @@ dependencies = [ "js-sys", "log", "naga", - "parking_lot 0.12.3", + "parking_lot 0.11.2", "profiling", "raw-window-handle", "smallvec", @@ -6475,7 +6486,7 @@ dependencies = [ "log", "naga", "once_cell", - "parking_lot 0.12.3", + "parking_lot 0.11.2", "profiling", "raw-window-handle", "rustc-hash", @@ -6515,7 +6526,7 @@ dependencies = [ "naga", "objc", "once_cell", - "parking_lot 0.12.3", + "parking_lot 0.11.2", "profiling", "range-alloc", "raw-window-handle", diff --git a/cosmic-applet-battery/Cargo.toml b/cosmic-applet-battery/Cargo.toml index 56d04d52..7786d6c5 100644 --- a/cosmic-applet-battery/Cargo.toml +++ b/cosmic-applet-battery/Cargo.toml @@ -19,3 +19,5 @@ tracing-subscriber.workspace = true tracing.workspace = true udev = "0.8" zbus.workspace = true +# TODO branch +upower_dbus = { git = "https://github.com/pop-os/dbus-settings-bindings", branch = "upower" } diff --git a/cosmic-applet-battery/src/lib.rs b/cosmic-applet-battery/src/lib.rs index 9560698e..4499ee1c 100644 --- a/cosmic-applet-battery/src/lib.rs +++ b/cosmic-applet-battery/src/lib.rs @@ -8,7 +8,6 @@ mod backend; mod config; mod dgpu; mod localize; -mod upower; mod upower_device; mod upower_kbdbacklight; diff --git a/cosmic-applet-battery/src/upower.rs b/cosmic-applet-battery/src/upower.rs deleted file mode 100644 index a5cb5a6f..00000000 --- a/cosmic-applet-battery/src/upower.rs +++ /dev/null @@ -1,46 +0,0 @@ -//! # DBus interface proxy for: `org.freedesktop.UPower` -//! -//! This code was generated by `zbus-xmlgen` `2.0.1` from DBus introspection data. -//! Source: `Interface '/org/freedesktop/UPower' from service 'org.freedesktop.UPower' on system bus`. - -use zbus::proxy; - -#[proxy( - default_service = "org.freedesktop.UPower", - interface = "org.freedesktop.UPower", - default_path = "/org/freedesktop/UPower" -)] -trait UPower { - /// EnumerateDevices method - fn enumerate_devices(&self) -> zbus::Result>; - - /// GetCriticalAction method - fn get_critical_action(&self) -> zbus::Result; - - /// GetDisplayDevice method - fn get_display_device(&self) -> zbus::Result; - - /// 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; - - /// LidIsClosed property - #[zbus(property)] - fn lid_is_closed(&self) -> zbus::Result; - - /// LidIsPresent property - #[zbus(property)] - fn lid_is_present(&self) -> zbus::Result; - - /// OnBattery property - #[zbus(property)] - fn on_battery(&self) -> zbus::Result; -} diff --git a/cosmic-applet-battery/src/upower_device.rs b/cosmic-applet-battery/src/upower_device.rs index b99934c5..1dc8e325 100644 --- a/cosmic-applet-battery/src/upower_device.rs +++ b/cosmic-applet-battery/src/upower_device.rs @@ -10,148 +10,8 @@ use cosmic::iced::{ }; use std::{fmt::Debug, hash::Hash}; -use zbus::proxy; -use crate::upower::UPowerProxy; -#[proxy( - default_service = "org.freedesktop.UPower", - interface = "org.freedesktop.UPower.Device" -)] -trait Device { - /// GetHistory method - fn get_history( - &self, - type_: &str, - timespan: u32, - resolution: u32, - ) -> zbus::Result>; - - /// GetStatistics method - fn get_statistics(&self, type_: &str) -> zbus::Result>; - - /// Refresh method - fn refresh(&self) -> zbus::Result<()>; - - /// BatteryLevel property - #[zbus(property)] - fn battery_level(&self) -> zbus::Result; - - /// Capacity property - #[zbus(property)] - fn capacity(&self) -> zbus::Result; - - /// ChargeCycles property - #[zbus(property)] - fn charge_cycles(&self) -> zbus::Result; - - /// Energy property - #[zbus(property)] - fn energy(&self) -> zbus::Result; - - /// EnergyEmpty property - #[zbus(property)] - fn energy_empty(&self) -> zbus::Result; - - /// EnergyFull property - #[zbus(property)] - fn energy_full(&self) -> zbus::Result; - - /// EnergyFullDesign property - #[zbus(property)] - fn energy_full_design(&self) -> zbus::Result; - - /// EnergyRate property - #[zbus(property)] - fn energy_rate(&self) -> zbus::Result; - - /// HasHistory property - #[zbus(property)] - fn has_history(&self) -> zbus::Result; - - /// HasStatistics property - #[zbus(property)] - fn has_statistics(&self) -> zbus::Result; - - /// IconName property - #[zbus(property)] - fn icon_name(&self) -> zbus::Result; - - /// IsPresent property - #[zbus(property)] - fn is_present(&self) -> zbus::Result; - - /// IsRechargeable property - #[zbus(property)] - fn is_rechargeable(&self) -> zbus::Result; - - /// Luminosity property - #[zbus(property)] - fn luminosity(&self) -> zbus::Result; - - /// Model property - #[zbus(property)] - fn model(&self) -> zbus::Result; - - /// NativePath property - #[zbus(property)] - fn native_path(&self) -> zbus::Result; - - /// Online property - #[zbus(property)] - fn online(&self) -> zbus::Result; - - /// Percentage property - #[zbus(property)] - fn percentage(&self) -> zbus::Result; - - /// PowerSupply property - #[zbus(property)] - fn power_supply(&self) -> zbus::Result; - - /// Serial property - #[zbus(property)] - fn serial(&self) -> zbus::Result; - - /// State property - #[zbus(property)] - fn state(&self) -> zbus::Result; - - /// Technology property - #[zbus(property)] - fn technology(&self) -> zbus::Result; - - /// Temperature property - #[zbus(property)] - fn temperature(&self) -> zbus::Result; - - /// TimeToEmpty property - #[zbus(property)] - fn time_to_empty(&self) -> zbus::Result; - - /// TimeToFull property - #[zbus(property)] - fn time_to_full(&self) -> zbus::Result; - - /// Type property - #[zbus(property)] - fn type_(&self) -> zbus::Result; - - /// UpdateTime property - #[zbus(property)] - fn update_time(&self) -> zbus::Result; - - /// Vendor property - #[zbus(property)] - fn vendor(&self) -> zbus::Result; - - /// Voltage property - #[zbus(property)] - fn voltage(&self) -> zbus::Result; - - /// WarningLevel property - #[zbus(property)] - fn warning_level(&self) -> zbus::Result; -} +use upower_dbus::{BatteryType, DeviceProxy, UPowerProxy}; pub fn device_subscription( id: I, @@ -176,12 +36,7 @@ async fn display_device() -> zbus::Result<(UPowerProxy<'static>, DeviceProxy<'st let connection = zbus::Connection::system().await?; let upower: UPowerProxy<'_> = UPowerProxy::new(&connection).await?; let device_path = upower.get_display_device().await?; - DeviceProxy::builder(&connection) - .path(device_path)? - .cache_properties(zbus::CacheProperties::Yes) - .build() - .await - .map(|dp| (upower, dp)) + Ok((upower, device_path)) } async fn start_listening( @@ -201,7 +56,7 @@ async fn start_listening( let Ok(d) = d.build().await else { continue; }; - if d.type_().await.unwrap_or_default() == 2 + if d.type_().await == Ok(BatteryType::Battery) && d.power_supply().await.unwrap_or_default() { has_battery = true; diff --git a/cosmic-applet-battery/src/upower_kbdbacklight.rs b/cosmic-applet-battery/src/upower_kbdbacklight.rs index 81ece37b..2e00cb78 100644 --- a/cosmic-applet-battery/src/upower_kbdbacklight.rs +++ b/cosmic-applet-battery/src/upower_kbdbacklight.rs @@ -6,31 +6,8 @@ use cosmic::iced::{self, futures::SinkExt, subscription}; use std::{fmt::Debug, hash::Hash}; use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender}; -use zbus::proxy; -#[proxy( - default_service = "org.freedesktop.UPower", - interface = "org.freedesktop.UPower.KbdBacklight", - default_path = "/org/freedesktop/UPower/KbdBacklight" -)] -trait KbdBacklight { - /// GetBrightness method - fn get_brightness(&self) -> zbus::Result; - - /// GetMaxBrightness method - fn get_max_brightness(&self) -> zbus::Result; - - /// SetBrightness method - fn set_brightness(&self, value: i32) -> zbus::Result<()>; - - /// BrightnessChanged signal - #[zbus(signal)] - fn brightness_changed(&self, value: i32) -> zbus::Result<()>; - - /// BrightnessChangedWithSource signal - #[zbus(signal)] - fn brightness_changed_with_source(&self, value: i32, source: &str) -> zbus::Result<()>; -} +use upower_dbus::KbdBacklightProxy; pub fn kbd_backlight_subscription( id: I,