From ad1707a5417c331328a051009d2db64d7f2b175c Mon Sep 17 00:00:00 2001 From: Ian Douglas Scott Date: Sat, 25 Jun 2022 14:14:35 -0700 Subject: [PATCH] battery: system76 power dbus proxy --- .../cosmic-applet-power-battery/src/main.rs | 62 +++++++++++++++++- .../src/power_daemon.rs | 65 +++++++++++++++++++ 2 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 applets/cosmic-applet-power-battery/src/power_daemon.rs diff --git a/applets/cosmic-applet-power-battery/src/main.rs b/applets/cosmic-applet-power-battery/src/main.rs index bc35ccfd..4150254b 100644 --- a/applets/cosmic-applet-power-battery/src/main.rs +++ b/applets/cosmic-applet-power-battery/src/main.rs @@ -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 { + 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>, backlight: Option, kbd_backlight: Option>, + power_daemon: Option>, } 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() { diff --git a/applets/cosmic-applet-power-battery/src/power_daemon.rs b/applets/cosmic-applet-power-battery/src/power_daemon.rs new file mode 100644 index 00000000..3b095333 --- /dev/null +++ b/applets/cosmic-applet-power-battery/src/power_daemon.rs @@ -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>>; + + /// GetChargeThresholds method + fn get_charge_thresholds(&self) -> zbus::Result<(u8, u8)>; + + /// GetDefaultGraphics method + fn get_default_graphics(&self) -> zbus::Result; + + /// GetExternalDisplaysRequireDGPU method + fn get_external_displays_require_dgpu(&self) -> zbus::Result; + + /// GetGraphics method + fn get_graphics(&self) -> zbus::Result; + + /// GetGraphicsPower method + fn get_graphics_power(&self) -> zbus::Result; + + /// GetProfile method + fn get_profile(&self) -> zbus::Result; + + /// GetSwitchable method + fn get_switchable(&self) -> zbus::Result; + + /// 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<()>; +}