From cf06e14545fecdffb8d7dd78ccf5789648eacf58 Mon Sep 17 00:00:00 2001 From: Piotr <114903054+elevenhsoft@users.noreply.github.com> Date: Fri, 6 Sep 2024 17:56:11 +0200 Subject: [PATCH] feat(battery): support charging limits with system76-power --- cosmic-applet-battery/src/app.rs | 26 ++++++++++++--- cosmic-applet-battery/src/backend/mod.rs | 40 ++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 4 deletions(-) diff --git a/cosmic-applet-battery/src/app.rs b/cosmic-applet-battery/src/app.rs index 7d25f1f0..40194b7b 100644 --- a/cosmic-applet-battery/src/app.rs +++ b/cosmic-applet-battery/src/app.rs @@ -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>, ) { + 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); diff --git a/cosmic-applet-battery/src/backend/mod.rs b/cosmic-applet-battery/src/backend/mod.rs index e5495500..52c56714 100644 --- a/cosmic-applet-battery/src/backend/mod.rs +++ b/cosmic-applet-battery/src/backend/mod.rs @@ -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(()) +}