battery: system76 power dbus proxy

This commit is contained in:
Ian Douglas Scott 2022-06-25 14:14:35 -07:00
parent 6874bdf486
commit ad1707a541
2 changed files with 126 additions and 1 deletions

View file

@ -1,4 +1,5 @@
// TODO: don't allow brightness 0?
// TODO: handle dbus service start/stop?
use futures::prelude::*;
use gtk4::{glib, prelude::*};
@ -7,6 +8,8 @@ use std::{process::Command, time::Duration};
mod backlight;
use backlight::{backlight, Backlight, LogindSessionProxy};
mod power_daemon;
use power_daemon::PowerDaemonProxy;
mod upower;
use upower::UPowerProxy;
mod upower_device;
@ -41,6 +44,35 @@ fn format_duration(duration: Duration) -> String {
}
}
#[derive(Copy, Clone)]
enum Graphics {
Compute,
Hybrid,
Integrated,
Nvidia,
}
impl Graphics {
fn from_str(s: &str) -> Option<Self> {
match s {
"compute" => Some(Self::Compute),
"hybrid" => Some(Self::Hybrid),
"integrated" => Some(Self::Integrated),
"nvidia" => Some(Self::Nvidia),
_ => None,
}
}
fn to_str(self) -> &'static str {
match self {
Self::Compute => "compute",
Self::Hybrid => "hybrid",
Self::Integrated => "integrated",
Self::Nvidia => "nvidia",
}
}
}
#[derive(Default)]
struct AppModel {
icon_name: String,
@ -52,6 +84,7 @@ struct AppModel {
session: Option<LogindSessionProxy<'static>>,
backlight: Option<Backlight>,
kbd_backlight: Option<KbdBacklightProxy<'static>>,
power_daemon: Option<PowerDaemonProxy<'static>>,
}
enum AppMsg {
@ -60,6 +93,7 @@ enum AppMsg {
SetDevice(DeviceProxy<'static>),
SetSession(LogindSessionProxy<'static>),
SetKbdBacklight(KbdBacklightProxy<'static>),
SetPowerDaemon(PowerDaemonProxy<'static>),
UpdateProperties,
UpdateKbdBrightness(f64),
}
@ -251,7 +285,18 @@ impl SimpleComponent for AppModel {
}.await;
match proxy {
Ok(kbd_backlight) => sender.input(AppMsg::SetKbdBacklight(kbd_backlight)),
Err(err) => eprintln!("Failed to open kbdbacklight: {}", err),
Err(err) => eprintln!("Failed to open kbd_backlight: {}", err),
}
}));
glib::MainContext::default().spawn(glib::clone!(@strong sender => async move {
let proxy = async {
let connection = zbus::Connection::system().await?;
PowerDaemonProxy::builder(&connection).build().await
}.await;
match proxy {
Ok(power_daemon) => sender.input(AppMsg::SetPowerDaemon(power_daemon)),
Err(err) => eprintln!("Failed to open power daemon: {}", err),
}
}));
@ -342,6 +387,21 @@ impl SimpleComponent for AppModel {
}
}));
}
AppMsg::SetPowerDaemon(power_daemon) => {
self.power_daemon = Some(power_daemon.clone());
// XXX detect change?
glib::MainContext::default().spawn(glib::clone!(@strong sender => async move {
async {
zbus::Result::Ok(if power_daemon.get_switchable().await? {
Some(power_daemon.get_graphics().await?)
} else {
None
})
};
}));
// XXX
}
AppMsg::UpdateProperties => {
if let Some(device) = self.device.as_ref() {
if let Ok(Some(percentage)) = device.cached_percentage() {

View file

@ -0,0 +1,65 @@
//! # DBus interface proxy for: `com.system76.PowerDaemon`
//!
//! This code was generated by `zbus-xmlgen` `2.0.1` from DBus introspection data.
//! Source: `Interface '/com/system76/PowerDaemon' from service 'com.system76.PowerDaemon' on system bus`.
use zbus::dbus_proxy;
#[dbus_proxy(
default_service = "com.system76.PowerDaemon",
interface = "com.system76.PowerDaemon",
default_path = "/com/system76/PowerDaemon"
)]
trait PowerDaemon {
/// Balanced method
fn balanced(&self) -> zbus::Result<()>;
/// Battery method
fn battery(&self) -> zbus::Result<()>;
/// GetChargeProfiles method
fn get_charge_profiles(
&self,
) -> zbus::Result<Vec<std::collections::HashMap<String, zbus::zvariant::OwnedValue>>>;
/// GetChargeThresholds method
fn get_charge_thresholds(&self) -> zbus::Result<(u8, u8)>;
/// GetDefaultGraphics method
fn get_default_graphics(&self) -> zbus::Result<String>;
/// GetExternalDisplaysRequireDGPU method
fn get_external_displays_require_dgpu(&self) -> zbus::Result<bool>;
/// GetGraphics method
fn get_graphics(&self) -> zbus::Result<String>;
/// GetGraphicsPower method
fn get_graphics_power(&self) -> zbus::Result<bool>;
/// GetProfile method
fn get_profile(&self) -> zbus::Result<String>;
/// GetSwitchable method
fn get_switchable(&self) -> zbus::Result<bool>;
/// Performance method
fn performance(&self) -> zbus::Result<()>;
/// SetChargeThresholds method
fn set_charge_thresholds(&self, thresholds: &(u8, u8)) -> zbus::Result<()>;
/// SetGraphics method
fn set_graphics(&self, vendor: &str) -> zbus::Result<()>;
/// SetGraphicsPower method
fn set_graphics_power(&self, power: bool) -> zbus::Result<()>;
/// HotPlugDetect signal
#[dbus_proxy(signal)]
fn hot_plug_detect(&self, port: u64) -> zbus::Result<()>;
/// PowerProfileSwitch signal
#[dbus_proxy(signal)]
fn power_profile_switch(&self, profile: &str) -> zbus::Result<()>;
}