battery: Use upower_dbus instead of including bindings here

This commit is contained in:
Ian Douglas Scott 2024-06-13 15:45:59 -07:00 committed by Ian Douglas Scott
parent df6232090e
commit 4e3eb0a396
6 changed files with 20 additions and 222 deletions

View file

@ -8,7 +8,6 @@ mod backend;
mod config;
mod dgpu;
mod localize;
mod upower;
mod upower_device;
mod upower_kbdbacklight;

View file

@ -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<Vec<zbus::zvariant::OwnedObjectPath>>;
/// GetCriticalAction method
fn get_critical_action(&self) -> zbus::Result<String>;
/// GetDisplayDevice method
fn get_display_device(&self) -> zbus::Result<zbus::zvariant::OwnedObjectPath>;
/// 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>;
}

View file

@ -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<Vec<(u32, f64, u32)>>;
/// GetStatistics method
fn get_statistics(&self, type_: &str) -> zbus::Result<Vec<(f64, f64)>>;
/// Refresh method
fn refresh(&self) -> zbus::Result<()>;
/// BatteryLevel property
#[zbus(property)]
fn battery_level(&self) -> zbus::Result<u32>;
/// Capacity property
#[zbus(property)]
fn capacity(&self) -> zbus::Result<f64>;
/// ChargeCycles property
#[zbus(property)]
fn charge_cycles(&self) -> zbus::Result<i32>;
/// Energy property
#[zbus(property)]
fn energy(&self) -> zbus::Result<f64>;
/// EnergyEmpty property
#[zbus(property)]
fn energy_empty(&self) -> zbus::Result<f64>;
/// EnergyFull property
#[zbus(property)]
fn energy_full(&self) -> zbus::Result<f64>;
/// EnergyFullDesign property
#[zbus(property)]
fn energy_full_design(&self) -> zbus::Result<f64>;
/// EnergyRate property
#[zbus(property)]
fn energy_rate(&self) -> zbus::Result<f64>;
/// HasHistory property
#[zbus(property)]
fn has_history(&self) -> zbus::Result<bool>;
/// HasStatistics property
#[zbus(property)]
fn has_statistics(&self) -> zbus::Result<bool>;
/// IconName property
#[zbus(property)]
fn icon_name(&self) -> zbus::Result<String>;
/// IsPresent property
#[zbus(property)]
fn is_present(&self) -> zbus::Result<bool>;
/// IsRechargeable property
#[zbus(property)]
fn is_rechargeable(&self) -> zbus::Result<bool>;
/// Luminosity property
#[zbus(property)]
fn luminosity(&self) -> zbus::Result<f64>;
/// Model property
#[zbus(property)]
fn model(&self) -> zbus::Result<String>;
/// NativePath property
#[zbus(property)]
fn native_path(&self) -> zbus::Result<String>;
/// Online property
#[zbus(property)]
fn online(&self) -> zbus::Result<bool>;
/// Percentage property
#[zbus(property)]
fn percentage(&self) -> zbus::Result<f64>;
/// PowerSupply property
#[zbus(property)]
fn power_supply(&self) -> zbus::Result<bool>;
/// Serial property
#[zbus(property)]
fn serial(&self) -> zbus::Result<String>;
/// State property
#[zbus(property)]
fn state(&self) -> zbus::Result<u32>;
/// Technology property
#[zbus(property)]
fn technology(&self) -> zbus::Result<u32>;
/// Temperature property
#[zbus(property)]
fn temperature(&self) -> zbus::Result<f64>;
/// TimeToEmpty property
#[zbus(property)]
fn time_to_empty(&self) -> zbus::Result<i64>;
/// TimeToFull property
#[zbus(property)]
fn time_to_full(&self) -> zbus::Result<i64>;
/// Type property
#[zbus(property)]
fn type_(&self) -> zbus::Result<u32>;
/// UpdateTime property
#[zbus(property)]
fn update_time(&self) -> zbus::Result<u64>;
/// Vendor property
#[zbus(property)]
fn vendor(&self) -> zbus::Result<String>;
/// Voltage property
#[zbus(property)]
fn voltage(&self) -> zbus::Result<f64>;
/// WarningLevel property
#[zbus(property)]
fn warning_level(&self) -> zbus::Result<u32>;
}
use upower_dbus::{BatteryType, DeviceProxy, UPowerProxy};
pub fn device_subscription<I: 'static + Hash + Copy + Send + Sync + Debug>(
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;

View file

@ -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<i32>;
/// GetMaxBrightness method
fn get_max_brightness(&self) -> zbus::Result<i32>;
/// 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<I: 'static + Hash + Copy + Send + Sync + Debug>(
id: I,