feat(battery): support charging limits with system76-power

This commit is contained in:
Piotr 2024-09-06 17:56:11 +02:00 committed by GitHub
parent 58a61b7eee
commit cf06e14545
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 62 additions and 4 deletions

View file

@ -2,7 +2,10 @@
// SPDX-License-Identifier: GPL-3.0-only
use crate::{
backend::{power_profile_subscription, Power, PowerProfileRequest, PowerProfileUpdate},
backend::{
get_charging_limit, power_profile_subscription, set_charging_limit, Power,
PowerProfileRequest, PowerProfileUpdate,
},
config,
dgpu::{dgpu_subscription, Entry, GpuUpdate},
fl,
@ -163,6 +166,7 @@ enum Message {
CloseRequested(window::Id),
SetKbdBrightness(i32),
SetScreenBrightness(i32),
InitChargingLimit(bool),
SetChargingLimit(chain::Toggler, bool),
KeyboardBacklight(KeyboardBacklightUpdate),
UpowerDevice(DeviceDbusEvent),
@ -193,6 +197,13 @@ impl cosmic::Application for CosmicBatteryApplet {
Self,
cosmic::iced::Command<cosmic::app::Message<Self::Message>>,
) {
let zbus_session_cmd = cosmic::iced::Command::perform(zbus::Connection::session(), |res| {
cosmic::app::Message::App(Message::ZbusConnection(res))
});
let init_charging_limit_cmd =
cosmic::iced::Command::perform(get_charging_limit(), |limit| {
cosmic::app::Message::App(Message::InitChargingLimit(limit))
});
(
Self {
core,
@ -202,9 +213,7 @@ impl cosmic::Application for CosmicBatteryApplet {
..Default::default()
},
cosmic::iced::Command::perform(zbus::Connection::session(), |res| {
cosmic::app::Message::App(Message::ZbusConnection(res))
}),
Command::batch(vec![zbus_session_cmd, init_charging_limit_cmd]),
)
}
@ -235,9 +244,18 @@ impl cosmic::Application for CosmicBatteryApplet {
let _ = tx.send(settings_daemon::Request::SetDisplayBrightness(brightness));
}
}
Message::InitChargingLimit(enable) => {
self.set_charging_limit(enable);
}
Message::SetChargingLimit(chain, enable) => {
self.timeline.set_chain(chain).start();
self.set_charging_limit(enable);
if enable {
return cosmic::iced::Command::perform(set_charging_limit(), |_| {
cosmic::app::Message::None
});
}
}
Message::Errored(why) => {
tracing::error!("{}", why);

View file

@ -207,3 +207,43 @@ pub enum PowerProfileUpdate {
Update { profile: Power },
Error(String),
}
// check if battery charging thresholds is set
pub async fn get_charging_limit() -> bool {
if let Ok(conn) = Connection::system().await {
if let Ok(backend) = get_power_backend(&conn, &BackendType::S76PowerDaemon).await {
match backend {
Backend::S76PowerDaemon(proxy) => {
if let Ok((start, end)) = proxy.get_charge_thresholds().await {
return start > 0 || end > 0;
}
}
Backend::PowerProfilesDaemon(_) => {
tracing::info!("Power Profiles Daemon is not supported.");
return false;
}
};
}
}
false
}
// set battery charging thresholds via s76 power_daemon
pub async fn set_charging_limit() -> Result<()> {
if let Ok(conn) = Connection::system().await {
if let Ok(backend) = get_power_backend(&conn, &BackendType::S76PowerDaemon).await {
match backend {
Backend::S76PowerDaemon(proxy) => {
let _ = proxy.set_charge_thresholds(&(70, 80)).await;
}
Backend::PowerProfilesDaemon(_) => {
tracing::info!(
"Setting charging limit via Power Profiles Daemon is not supported."
);
}
};
}
}
Ok(())
}