WIP: UPower and backlight handling in battery applet
This commit is contained in:
parent
198fe3dd71
commit
89f82fd829
6 changed files with 792 additions and 3 deletions
41
applets/cosmic-applet-power-battery/src/backlight.rs
Normal file
41
applets/cosmic-applet-power-battery/src/backlight.rs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
use std::{
|
||||
fs::{self, File},
|
||||
io::{self, Read},
|
||||
path::PathBuf,
|
||||
str::FromStr,
|
||||
};
|
||||
|
||||
struct Backlight(PathBuf);
|
||||
|
||||
impl Backlight {
|
||||
fn brightness(&self) -> Option<i64> {
|
||||
self.prop("brightness")
|
||||
}
|
||||
|
||||
fn max_brightness(&self) -> Option<i64> {
|
||||
self.prop("max_brightness")
|
||||
}
|
||||
|
||||
fn prop<T: FromStr>(&self, name: &str) -> Option<T> {
|
||||
let mut file = File::open(&self.0.join(name)).ok()?;
|
||||
let mut s = String::new();
|
||||
file.read_to_string(&mut s).ok()?;
|
||||
s.parse().ok()
|
||||
}
|
||||
}
|
||||
|
||||
// Choose backlight with most "precision". This is what `light` does.
|
||||
fn backlight() -> io::Result<Option<Backlight>> {
|
||||
let mut best_backlight = None;
|
||||
let mut best_max_brightness = 0;
|
||||
for i in fs::read_dir("/sys/class/backlight")? {
|
||||
let backlight = Backlight(i?.path());
|
||||
if let Some(max_brightness) = backlight.max_brightness() {
|
||||
if max_brightness > best_max_brightness {
|
||||
best_backlight = Some(backlight);
|
||||
best_max_brightness = max_brightness;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(best_backlight)
|
||||
}
|
||||
|
|
@ -1,7 +1,35 @@
|
|||
use gtk4::prelude::*;
|
||||
use futures::prelude::*;
|
||||
use gtk4::{glib, prelude::*};
|
||||
use relm4::{ComponentParts, ComponentSender, RelmApp, SimpleComponent, WidgetPlus};
|
||||
use std::time::Duration;
|
||||
|
||||
mod backlight;
|
||||
mod upower;
|
||||
use upower::UPowerProxy;
|
||||
mod upower_device;
|
||||
use upower_device::DeviceProxy;
|
||||
|
||||
async fn display_device() -> zbus::Result<DeviceProxy<'static>> {
|
||||
let connection = zbus::Connection::system().await?;
|
||||
let upower = UPowerProxy::new(&connection).await?;
|
||||
let device_path = upower.get_display_device().await?;
|
||||
DeviceProxy::builder(&connection)
|
||||
.path(device_path)?
|
||||
.build()
|
||||
.await
|
||||
}
|
||||
|
||||
async fn foo(device: &DeviceProxy<'static>) {
|
||||
let mut icon_name_stream = device.receive_icon_name_changed().await;
|
||||
let mut battery_level_stream = device.receive_battery_level_changed().await;
|
||||
|
||||
glib::MainContext::default()
|
||||
.spawn(async move { while let Some(evt) = icon_name_stream.next().await {} });
|
||||
|
||||
glib::MainContext::default()
|
||||
.spawn(async move { while let Some(evt) = battery_level_stream.next().await {} });
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct AppModel {
|
||||
icon_name: String,
|
||||
|
|
@ -171,6 +199,6 @@ impl SimpleComponent for AppModel {
|
|||
}
|
||||
|
||||
fn main() {
|
||||
let app: RelmApp<AppModel> = RelmApp::new("relm4.test.simple");
|
||||
let app: RelmApp<AppModel> = RelmApp::new("com.system76.CosmicAppletBattery");
|
||||
app.run(());
|
||||
}
|
||||
|
|
|
|||
42
applets/cosmic-applet-power-battery/src/upower.rs
Normal file
42
applets/cosmic-applet-power-battery/src/upower.rs
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
//! # 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::dbus_proxy;
|
||||
|
||||
#[dbus_proxy(interface = "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
|
||||
#[dbus_proxy(signal)]
|
||||
fn device_added(&self, device: zbus::zvariant::ObjectPath<'_>) -> zbus::Result<()>;
|
||||
|
||||
/// DeviceRemoved signal
|
||||
#[dbus_proxy(signal)]
|
||||
fn device_removed(&self, device: zbus::zvariant::ObjectPath<'_>) -> zbus::Result<()>;
|
||||
|
||||
/// DaemonVersion property
|
||||
#[dbus_proxy(property)]
|
||||
fn daemon_version(&self) -> zbus::Result<String>;
|
||||
|
||||
/// LidIsClosed property
|
||||
#[dbus_proxy(property)]
|
||||
fn lid_is_closed(&self) -> zbus::Result<bool>;
|
||||
|
||||
/// LidIsPresent property
|
||||
#[dbus_proxy(property)]
|
||||
fn lid_is_present(&self) -> zbus::Result<bool>;
|
||||
|
||||
/// OnBattery property
|
||||
#[dbus_proxy(property)]
|
||||
fn on_battery(&self) -> zbus::Result<bool>;
|
||||
}
|
||||
143
applets/cosmic-applet-power-battery/src/upower_device.rs
Normal file
143
applets/cosmic-applet-power-battery/src/upower_device.rs
Normal file
|
|
@ -0,0 +1,143 @@
|
|||
//! # DBus interface proxy for: `org.freedesktop.UPower.Device`
|
||||
//!
|
||||
//! This code was generated by `zbus-xmlgen` `2.0.1` from DBus introspection data.
|
||||
//! Source: `Interface '/org/freedesktop/UPower/devices/DisplayDevice' from service 'org.freedesktop.UPower' on system bus`.
|
||||
|
||||
use zbus::dbus_proxy;
|
||||
|
||||
#[dbus_proxy(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
|
||||
#[dbus_proxy(property)]
|
||||
fn battery_level(&self) -> zbus::Result<u32>;
|
||||
|
||||
/// Capacity property
|
||||
#[dbus_proxy(property)]
|
||||
fn capacity(&self) -> zbus::Result<f64>;
|
||||
|
||||
/// ChargeCycles property
|
||||
#[dbus_proxy(property)]
|
||||
fn charge_cycles(&self) -> zbus::Result<i32>;
|
||||
|
||||
/// Energy property
|
||||
#[dbus_proxy(property)]
|
||||
fn energy(&self) -> zbus::Result<f64>;
|
||||
|
||||
/// EnergyEmpty property
|
||||
#[dbus_proxy(property)]
|
||||
fn energy_empty(&self) -> zbus::Result<f64>;
|
||||
|
||||
/// EnergyFull property
|
||||
#[dbus_proxy(property)]
|
||||
fn energy_full(&self) -> zbus::Result<f64>;
|
||||
|
||||
/// EnergyFullDesign property
|
||||
#[dbus_proxy(property)]
|
||||
fn energy_full_design(&self) -> zbus::Result<f64>;
|
||||
|
||||
/// EnergyRate property
|
||||
#[dbus_proxy(property)]
|
||||
fn energy_rate(&self) -> zbus::Result<f64>;
|
||||
|
||||
/// HasHistory property
|
||||
#[dbus_proxy(property)]
|
||||
fn has_history(&self) -> zbus::Result<bool>;
|
||||
|
||||
/// HasStatistics property
|
||||
#[dbus_proxy(property)]
|
||||
fn has_statistics(&self) -> zbus::Result<bool>;
|
||||
|
||||
/// IconName property
|
||||
#[dbus_proxy(property)]
|
||||
fn icon_name(&self) -> zbus::Result<String>;
|
||||
|
||||
/// IsPresent property
|
||||
#[dbus_proxy(property)]
|
||||
fn is_present(&self) -> zbus::Result<bool>;
|
||||
|
||||
/// IsRechargeable property
|
||||
#[dbus_proxy(property)]
|
||||
fn is_rechargeable(&self) -> zbus::Result<bool>;
|
||||
|
||||
/// Luminosity property
|
||||
#[dbus_proxy(property)]
|
||||
fn luminosity(&self) -> zbus::Result<f64>;
|
||||
|
||||
/// Model property
|
||||
#[dbus_proxy(property)]
|
||||
fn model(&self) -> zbus::Result<String>;
|
||||
|
||||
/// NativePath property
|
||||
#[dbus_proxy(property)]
|
||||
fn native_path(&self) -> zbus::Result<String>;
|
||||
|
||||
/// Online property
|
||||
#[dbus_proxy(property)]
|
||||
fn online(&self) -> zbus::Result<bool>;
|
||||
|
||||
/// Percentage property
|
||||
#[dbus_proxy(property)]
|
||||
fn percentage(&self) -> zbus::Result<f64>;
|
||||
|
||||
/// PowerSupply property
|
||||
#[dbus_proxy(property)]
|
||||
fn power_supply(&self) -> zbus::Result<bool>;
|
||||
|
||||
/// Serial property
|
||||
#[dbus_proxy(property)]
|
||||
fn serial(&self) -> zbus::Result<String>;
|
||||
|
||||
/// State property
|
||||
#[dbus_proxy(property)]
|
||||
fn state(&self) -> zbus::Result<u32>;
|
||||
|
||||
/// Technology property
|
||||
#[dbus_proxy(property)]
|
||||
fn technology(&self) -> zbus::Result<u32>;
|
||||
|
||||
/// Temperature property
|
||||
#[dbus_proxy(property)]
|
||||
fn temperature(&self) -> zbus::Result<f64>;
|
||||
|
||||
/// TimeToEmpty property
|
||||
#[dbus_proxy(property)]
|
||||
fn time_to_empty(&self) -> zbus::Result<i64>;
|
||||
|
||||
/// TimeToFull property
|
||||
#[dbus_proxy(property)]
|
||||
fn time_to_full(&self) -> zbus::Result<i64>;
|
||||
|
||||
/// Type property
|
||||
#[dbus_proxy(property)]
|
||||
fn type_(&self) -> zbus::Result<u32>;
|
||||
|
||||
/// UpdateTime property
|
||||
#[dbus_proxy(property)]
|
||||
fn update_time(&self) -> zbus::Result<u64>;
|
||||
|
||||
/// Vendor property
|
||||
#[dbus_proxy(property)]
|
||||
fn vendor(&self) -> zbus::Result<String>;
|
||||
|
||||
/// Voltage property
|
||||
#[dbus_proxy(property)]
|
||||
fn voltage(&self) -> zbus::Result<f64>;
|
||||
|
||||
/// WarningLevel property
|
||||
#[dbus_proxy(property)]
|
||||
fn warning_level(&self) -> zbus::Result<u32>;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue