Install battery applet, and make it work in the panel
This commit is contained in:
parent
ad1707a541
commit
14101dc7db
13 changed files with 89 additions and 5 deletions
1540
applets/cosmic-applet-battery/Cargo.lock
generated
Normal file
1540
applets/cosmic-applet-battery/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
10
applets/cosmic-applet-battery/Cargo.toml
Normal file
10
applets/cosmic-applet-battery/Cargo.toml
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
[package]
|
||||
name = "cosmic-applet-battery"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
futures = "0.3"
|
||||
gtk4 = { git = "https://github.com/gtk-rs/gtk4-rs" }
|
||||
relm4 = { git = "https://github.com/relm4/relm4", branch = "next", features = ["macros"] }
|
||||
zbus = { version = "2", no-default-features = true }
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
[Desktop Entry]
|
||||
Name=Cosmic Applet Battery
|
||||
Comment=Write a GTK + Rust application
|
||||
Type=Application
|
||||
Exec=cosmic-applet-battery
|
||||
Terminal=false
|
||||
Categories=GNOME;GTK;
|
||||
Keywords=Gnome;GTK;
|
||||
# Translators: Do NOT translate or transliterate this text (this is an icon file name)!
|
||||
Icon=com.system76.CosmicAppletBattery.svg
|
||||
StartupNotify=true
|
||||
NoDisplay=true
|
||||
|
|
@ -0,0 +1,60 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="128px" height="128px" viewBox="0 0 128 128" version="1.1">
|
||||
<defs>
|
||||
<filter id="alpha" filterUnits="objectBoundingBox" x="0%" y="0%" width="100%" height="100%">
|
||||
<feColorMatrix type="matrix" in="SourceGraphic" values="0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 1 0"/>
|
||||
</filter>
|
||||
<mask id="mask0">
|
||||
<g filter="url(#alpha)">
|
||||
<rect x="0" y="0" width="128" height="128" style="fill:rgb(0%,0%,0%);fill-opacity:0.1;stroke:none;"/>
|
||||
</g>
|
||||
</mask>
|
||||
<clipPath id="clip1">
|
||||
<rect x="0" y="0" width="192" height="152"/>
|
||||
</clipPath>
|
||||
<g id="surface10632" clip-path="url(#clip1)">
|
||||
<path style="fill:none;stroke-width:0.99;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-dasharray:0.99,0.99;stroke-miterlimit:4;" d="M 123.503906 236 C 123.503906 268.863281 96.863281 295.503906 64 295.503906 C 31.136719 295.503906 4.496094 268.863281 4.496094 236 C 4.496094 203.136719 31.136719 176.496094 64 176.496094 C 96.863281 176.496094 123.503906 203.136719 123.503906 236 Z M 123.503906 236 " transform="matrix(1,0,0,1,8,-156)"/>
|
||||
</g>
|
||||
<mask id="mask1">
|
||||
<g filter="url(#alpha)">
|
||||
<rect x="0" y="0" width="128" height="128" style="fill:rgb(0%,0%,0%);fill-opacity:0.1;stroke:none;"/>
|
||||
</g>
|
||||
</mask>
|
||||
<clipPath id="clip2">
|
||||
<rect x="0" y="0" width="192" height="152"/>
|
||||
</clipPath>
|
||||
<g id="surface10635" clip-path="url(#clip2)">
|
||||
<path style="fill:none;stroke-width:0.99;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-dasharray:0.99,0.99;stroke-miterlimit:4;" d="M 29.195312 180.496094 L 98.804688 180.496094 C 103.609375 180.496094 107.503906 184.046875 107.503906 188.425781 L 107.503906 283.574219 C 107.503906 287.953125 103.609375 291.503906 98.804688 291.503906 L 29.195312 291.503906 C 24.390625 291.503906 20.496094 287.953125 20.496094 283.574219 L 20.496094 188.425781 C 20.496094 184.046875 24.390625 180.496094 29.195312 180.496094 Z M 29.195312 180.496094 " transform="matrix(1,0,0,1,8,-156)"/>
|
||||
</g>
|
||||
<mask id="mask2">
|
||||
<g filter="url(#alpha)">
|
||||
<rect x="0" y="0" width="128" height="128" style="fill:rgb(0%,0%,0%);fill-opacity:0.1;stroke:none;"/>
|
||||
</g>
|
||||
</mask>
|
||||
<clipPath id="clip3">
|
||||
<rect x="0" y="0" width="192" height="152"/>
|
||||
</clipPath>
|
||||
<g id="surface10638" clip-path="url(#clip3)">
|
||||
<path style="fill:none;stroke-width:0.99;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-dasharray:0.99,0.99;stroke-miterlimit:4;" d="M 20.417969 184.496094 L 107.582031 184.496094 C 111.957031 184.496094 115.503906 188.042969 115.503906 192.417969 L 115.503906 279.582031 C 115.503906 283.957031 111.957031 287.503906 107.582031 287.503906 L 20.417969 287.503906 C 16.042969 287.503906 12.496094 283.957031 12.496094 279.582031 L 12.496094 192.417969 C 12.496094 188.042969 16.042969 184.496094 20.417969 184.496094 Z M 20.417969 184.496094 " transform="matrix(1,0,0,1,8,-156)"/>
|
||||
</g>
|
||||
<mask id="mask3">
|
||||
<g filter="url(#alpha)">
|
||||
<rect x="0" y="0" width="128" height="128" style="fill:rgb(0%,0%,0%);fill-opacity:0.1;stroke:none;"/>
|
||||
</g>
|
||||
</mask>
|
||||
<clipPath id="clip4">
|
||||
<rect x="0" y="0" width="192" height="152"/>
|
||||
</clipPath>
|
||||
<g id="surface10641" clip-path="url(#clip4)">
|
||||
<path style="fill:none;stroke-width:0.99;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-dasharray:0.99,0.99;stroke-miterlimit:4;" d="M 16.425781 200.496094 L 111.574219 200.496094 C 115.953125 200.496094 119.503906 204.390625 119.503906 209.195312 L 119.503906 278.804688 C 119.503906 283.609375 115.953125 287.503906 111.574219 287.503906 L 16.425781 287.503906 C 12.046875 287.503906 8.496094 283.609375 8.496094 278.804688 L 8.496094 209.195312 C 8.496094 204.390625 12.046875 200.496094 16.425781 200.496094 Z M 16.425781 200.496094 " transform="matrix(1,0,0,1,8,-156)"/>
|
||||
</g>
|
||||
</defs>
|
||||
<g id="surface10578">
|
||||
<rect x="0" y="0" width="128" height="128" style="fill:rgb(94.117647%,94.117647%,94.117647%);fill-opacity:1;stroke:none;"/>
|
||||
<use xlink:href="#surface10632" transform="matrix(1,0,0,1,-8,-16)" mask="url(#mask0)"/>
|
||||
<use xlink:href="#surface10635" transform="matrix(1,0,0,1,-8,-16)" mask="url(#mask1)"/>
|
||||
<use xlink:href="#surface10638" transform="matrix(1,0,0,1,-8,-16)" mask="url(#mask2)"/>
|
||||
<use xlink:href="#surface10641" transform="matrix(1,0,0,1,-8,-16)" mask="url(#mask3)"/>
|
||||
<path style="fill:none;stroke-width:2;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(38.431373%,62.7451%,91.764706%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0 289 L 128 289 " transform="matrix(1,0,0,1,0,-172)"/>
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 4.5 KiB |
88
applets/cosmic-applet-battery/src/backlight.rs
Normal file
88
applets/cosmic-applet-battery/src/backlight.rs
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
// TODO: use udev to monitor for brightness changes?
|
||||
// How should key bindings be handled? Need something like gnome-settings-daemon?
|
||||
|
||||
use std::{
|
||||
fs::{self, File},
|
||||
io::{self, Read},
|
||||
os::unix::ffi::OsStrExt,
|
||||
path::Path,
|
||||
str::{self, FromStr},
|
||||
};
|
||||
|
||||
const BACKLIGHT_SYSDIR: &str = "/sys/class/backlight";
|
||||
|
||||
#[zbus::dbus_proxy(
|
||||
default_service = "org.freedesktop.login1",
|
||||
interface = "org.freedesktop.login1.Session",
|
||||
default_path = "/org/freedesktop/login1/session/auto"
|
||||
)]
|
||||
trait LogindSession {
|
||||
fn set_brightness(&self, subsystem: &str, name: &str, brightness: u32) -> zbus::Result<()>;
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Backlight(String);
|
||||
|
||||
impl Backlight {
|
||||
pub fn brightness(&self) -> Option<u32> {
|
||||
self.prop("brightness")
|
||||
}
|
||||
|
||||
// XXX cache value. Async?
|
||||
pub fn max_brightness(&self) -> Option<u32> {
|
||||
self.prop("max_brightness")
|
||||
}
|
||||
|
||||
pub async fn set_brightness(
|
||||
&self,
|
||||
session: &LogindSessionProxy<'_>,
|
||||
value: u32,
|
||||
) -> zbus::Result<()> {
|
||||
session.set_brightness("backlight", &self.0, value).await
|
||||
}
|
||||
|
||||
fn prop<T: FromStr>(&self, name: &str) -> Option<T> {
|
||||
let path = Path::new(BACKLIGHT_SYSDIR).join(&self.0).join(name);
|
||||
let mut file = File::open(path).ok()?;
|
||||
let mut s = String::new();
|
||||
file.read_to_string(&mut s).ok()?;
|
||||
s.trim().parse().ok()
|
||||
}
|
||||
}
|
||||
|
||||
// Choose backlight with most "precision". This is what `light` does.
|
||||
pub fn backlight() -> io::Result<Option<Backlight>> {
|
||||
let mut best_backlight = None;
|
||||
let mut best_max_brightness = 0;
|
||||
for i in fs::read_dir(BACKLIGHT_SYSDIR)? {
|
||||
if let Ok(filename) = str::from_utf8(i?.file_name().as_bytes()) {
|
||||
let backlight = Backlight(filename.to_string());
|
||||
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)
|
||||
}
|
||||
|
||||
/*
|
||||
// TODO: Cache device, max_brightness, etc.
|
||||
async fn set_display_brightness(brightness: f64) -> io::Result<()> {
|
||||
if let Some(backlight) = backlight()? {
|
||||
if let Some(max_brightness) = backlight.max_brightness() {
|
||||
let value = brightness.clamp(0., 1.) * (max_brightness as f64);
|
||||
let value = value.round() as u32;
|
||||
let connection = zbus::Connection::system().await?;
|
||||
if let Ok(session) = LogindSessionProxy::builder(&connection).build().await {
|
||||
backlight.set_brightness(&session, value).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
*/
|
||||
|
||||
// TODO: keyboard backlight
|
||||
432
applets/cosmic-applet-battery/src/main.rs
Normal file
432
applets/cosmic-applet-battery/src/main.rs
Normal file
|
|
@ -0,0 +1,432 @@
|
|||
// TODO: don't allow brightness 0?
|
||||
// TODO: handle dbus service start/stop?
|
||||
|
||||
use futures::prelude::*;
|
||||
use gtk4::{glib, prelude::*};
|
||||
use relm4::{ComponentParts, ComponentSender, RelmApp, SimpleComponent, WidgetPlus};
|
||||
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;
|
||||
use upower_device::DeviceProxy;
|
||||
mod upower_kbdbacklight;
|
||||
use upower_kbdbacklight::KbdBacklightProxy;
|
||||
|
||||
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)?
|
||||
.cache_properties(zbus::CacheProperties::Yes)
|
||||
.build()
|
||||
.await
|
||||
}
|
||||
|
||||
// XXX improve
|
||||
// TODO: time to empty varies? needs averaging?
|
||||
fn format_duration(duration: Duration) -> String {
|
||||
let secs = duration.as_secs();
|
||||
if secs > 60 {
|
||||
let min = secs / 60;
|
||||
if min > 60 {
|
||||
format!("{}:{:02}", min / 60, min % 60)
|
||||
} else {
|
||||
format!("{}m", min)
|
||||
}
|
||||
} else {
|
||||
format!("{}s", secs)
|
||||
}
|
||||
}
|
||||
|
||||
#[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,
|
||||
battery_percent: f64,
|
||||
time_remaining: Duration,
|
||||
display_brightness: f64,
|
||||
keyboard_brightness: f64,
|
||||
device: Option<DeviceProxy<'static>>,
|
||||
session: Option<LogindSessionProxy<'static>>,
|
||||
backlight: Option<Backlight>,
|
||||
kbd_backlight: Option<KbdBacklightProxy<'static>>,
|
||||
power_daemon: Option<PowerDaemonProxy<'static>>,
|
||||
}
|
||||
|
||||
enum AppMsg {
|
||||
SetDisplayBrightness(f64),
|
||||
SetKeyboardBrightness(f64),
|
||||
SetDevice(DeviceProxy<'static>),
|
||||
SetSession(LogindSessionProxy<'static>),
|
||||
SetKbdBacklight(KbdBacklightProxy<'static>),
|
||||
SetPowerDaemon(PowerDaemonProxy<'static>),
|
||||
UpdateProperties,
|
||||
UpdateKbdBrightness(f64),
|
||||
}
|
||||
|
||||
#[relm4::component]
|
||||
impl SimpleComponent for AppModel {
|
||||
type Widgets = AppWidgets;
|
||||
|
||||
type InitParams = ();
|
||||
|
||||
type Input = AppMsg;
|
||||
type Output = ();
|
||||
|
||||
view! {
|
||||
gtk4::Window {
|
||||
set_decorated: false,
|
||||
set_resizable: false,
|
||||
set_width_request: 1,
|
||||
set_height_request: 1,
|
||||
gtk4::MenuButton {
|
||||
set_has_frame: false,
|
||||
#[watch]
|
||||
set_icon_name: &model.icon_name,
|
||||
#[wrap(Some)]
|
||||
set_popover = >k4::Popover {
|
||||
#[wrap(Some)]
|
||||
set_child = >k4::Box {
|
||||
set_orientation: gtk4::Orientation::Vertical,
|
||||
|
||||
// Battery
|
||||
gtk4::Box {
|
||||
set_orientation: gtk4::Orientation::Horizontal,
|
||||
gtk4::Image {
|
||||
#[watch]
|
||||
set_icon_name: Some(&model.icon_name),
|
||||
},
|
||||
gtk4::Box {
|
||||
set_orientation: gtk4::Orientation::Vertical,
|
||||
gtk4::Label {
|
||||
set_halign: gtk4::Align::Start,
|
||||
set_label: "Battery",
|
||||
},
|
||||
gtk4::Label {
|
||||
set_halign: gtk4::Align::Start,
|
||||
// XXX time to full, fully changed, etc.
|
||||
#[watch]
|
||||
set_label: &format!("{} until empty ({:.0}%)", format_duration(model.time_remaining), model.battery_percent),
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
gtk4::Separator {
|
||||
},
|
||||
|
||||
// Profiles
|
||||
|
||||
gtk4::Separator {
|
||||
},
|
||||
|
||||
// Limit charging
|
||||
gtk4::Box {
|
||||
set_orientation: gtk4::Orientation::Horizontal,
|
||||
gtk4::Box {
|
||||
set_orientation: gtk4::Orientation::Vertical,
|
||||
gtk4::Label {
|
||||
set_halign: gtk4::Align::Start,
|
||||
set_label: "Limit Battery Charging",
|
||||
},
|
||||
gtk4::Label {
|
||||
set_halign: gtk4::Align::Start,
|
||||
set_label: "Increase the lifespan of your battery by setting a maximum charge value of 80%."
|
||||
},
|
||||
},
|
||||
gtk4::Switch {
|
||||
set_valign: gtk4::Align::Center,
|
||||
},
|
||||
},
|
||||
|
||||
gtk4::Separator {
|
||||
},
|
||||
|
||||
// Brightness
|
||||
gtk4::Box {
|
||||
#[watch]
|
||||
set_visible: model.backlight.is_some(),
|
||||
set_orientation: gtk4::Orientation::Horizontal,
|
||||
gtk4::Image {
|
||||
set_icon_name: Some("display-brightness-symbolic"),
|
||||
},
|
||||
gtk4::Scale {
|
||||
set_hexpand: true,
|
||||
set_adjustment: >k4::Adjustment::new(0., 0., 1., 1., 1., 0.),
|
||||
#[watch]
|
||||
set_value: model.display_brightness,
|
||||
connect_change_value[sender] => move |_, _, value| {
|
||||
sender.input(AppMsg::SetDisplayBrightness(value));
|
||||
gtk4::Inhibit(false)
|
||||
},
|
||||
},
|
||||
gtk4::Label {
|
||||
#[watch]
|
||||
set_label: &format!("{:.0}%", model.display_brightness * 100.),
|
||||
},
|
||||
},
|
||||
gtk4::Box {
|
||||
#[watch]
|
||||
set_visible: model.kbd_backlight.is_some(),
|
||||
set_orientation: gtk4::Orientation::Horizontal,
|
||||
gtk4::Image {
|
||||
set_icon_name: Some("keyboard-brightness-symbolic"),
|
||||
},
|
||||
gtk4::Scale {
|
||||
set_hexpand: true,
|
||||
set_adjustment: >k4::Adjustment::new(0., 0., 1., 1., 1., 0.),
|
||||
#[watch]
|
||||
set_value: model.keyboard_brightness,
|
||||
connect_change_value[sender] => move |_, _, value| {
|
||||
sender.input(AppMsg::SetKeyboardBrightness(value));
|
||||
gtk4::Inhibit(false)
|
||||
},
|
||||
},
|
||||
gtk4::Label {
|
||||
#[watch]
|
||||
set_label: &format!("{:.0}%", model.keyboard_brightness * 100.),
|
||||
},
|
||||
},
|
||||
|
||||
gtk4::Separator {
|
||||
},
|
||||
|
||||
gtk4::Button {
|
||||
set_label: "Power Settings...",
|
||||
connect_clicked => move |_| {
|
||||
// XXX open subpanel
|
||||
let _ = Command::new("cosmic-settings").spawn();
|
||||
// TODO hide
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn init(
|
||||
_params: Self::InitParams,
|
||||
root: &Self::Root,
|
||||
sender: &ComponentSender<Self>,
|
||||
) -> ComponentParts<Self> {
|
||||
let mut model = AppModel {
|
||||
icon_name: "battery-symbolic".to_string(),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let widgets = view_output!();
|
||||
|
||||
match backlight() {
|
||||
Ok(Some(backlight)) => {
|
||||
if let (Some(brightness), Some(max_brightness)) =
|
||||
(backlight.brightness(), backlight.max_brightness())
|
||||
{
|
||||
model.display_brightness = brightness as f64 / max_brightness as f64;
|
||||
}
|
||||
model.backlight = Some(backlight);
|
||||
}
|
||||
Ok(None) => {}
|
||||
Err(err) => eprintln!("Error finding backlight: {}", err),
|
||||
};
|
||||
|
||||
glib::MainContext::default().spawn(glib::clone!(@strong sender => async move {
|
||||
match display_device().await {
|
||||
Ok(device) => sender.input(AppMsg::SetDevice(device)),
|
||||
Err(err) => eprintln!("Failed to open UPower display device: {}", err),
|
||||
}
|
||||
}));
|
||||
|
||||
glib::MainContext::default().spawn(glib::clone!(@strong sender => async move {
|
||||
// XXX avoid multiple connections?
|
||||
let proxy = async {
|
||||
let connection = zbus::Connection::system().await?;
|
||||
LogindSessionProxy::builder(&connection).build().await
|
||||
}.await;
|
||||
match proxy {
|
||||
Ok(session) => sender.input(AppMsg::SetSession(session)),
|
||||
Err(err) => eprintln!("Failed to open logind session: {}", err),
|
||||
}
|
||||
}));
|
||||
|
||||
glib::MainContext::default().spawn(glib::clone!(@strong sender => async move {
|
||||
let proxy = async {
|
||||
let connection = zbus::Connection::system().await?;
|
||||
KbdBacklightProxy::builder(&connection).build().await
|
||||
}.await;
|
||||
match proxy {
|
||||
Ok(kbd_backlight) => sender.input(AppMsg::SetKbdBacklight(kbd_backlight)),
|
||||
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),
|
||||
}
|
||||
|
||||
}));
|
||||
|
||||
ComponentParts { model, widgets }
|
||||
}
|
||||
|
||||
fn update(&mut self, msg: Self::Input, sender: &ComponentSender<Self>) {
|
||||
match msg {
|
||||
AppMsg::SetDisplayBrightness(value) => {
|
||||
self.display_brightness = value;
|
||||
// XXX clone
|
||||
if let Some(backlight) = self.backlight.clone() {
|
||||
if let Some(session) = self.session.clone() {
|
||||
// XXX cache max brightness
|
||||
if let Some(max_brightness) = backlight.max_brightness() {
|
||||
let value = value.clamp(0., 1.) * (max_brightness as f64);
|
||||
let value = value.round() as u32;
|
||||
// XXX limit queueing?
|
||||
glib::MainContext::default().spawn(async move {
|
||||
if let Err(err) = backlight.set_brightness(&session, value).await {
|
||||
eprintln!("Failed to set backlight: {}", err);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
AppMsg::SetKeyboardBrightness(value) => {
|
||||
self.keyboard_brightness = value;
|
||||
|
||||
if let Some(kbd_backlight) = self.kbd_backlight.clone() {
|
||||
glib::MainContext::default().spawn(async move {
|
||||
let res = async {
|
||||
// XXX cache
|
||||
let max_brightness = kbd_backlight.get_max_brightness().await?;
|
||||
let value = value.clamp(0., 1.) * (max_brightness as f64);
|
||||
let value = value.round() as i32;
|
||||
kbd_backlight.set_brightness(value).await
|
||||
}
|
||||
.await;
|
||||
if let Err(err) = res {
|
||||
eprintln!("Failed to set keyboard backlight: {}", err);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
AppMsg::SetDevice(device) => {
|
||||
self.device = Some(device.clone());
|
||||
|
||||
let sender = sender.clone();
|
||||
glib::MainContext::default().spawn(async move {
|
||||
let mut stream = futures::stream_select!(
|
||||
device.receive_icon_name_changed().await.map(|_| ()),
|
||||
device.receive_percentage_changed().await.map(|_| ()),
|
||||
device.receive_time_to_empty_changed().await.map(|_| ()),
|
||||
);
|
||||
|
||||
sender.input(AppMsg::UpdateProperties);
|
||||
while let Some(()) = stream.next().await {
|
||||
sender.input(AppMsg::UpdateProperties);
|
||||
}
|
||||
});
|
||||
}
|
||||
AppMsg::SetSession(session) => {
|
||||
self.session = Some(session);
|
||||
}
|
||||
AppMsg::SetKbdBacklight(kbd_backlight) => {
|
||||
self.kbd_backlight = Some(kbd_backlight.clone());
|
||||
|
||||
glib::MainContext::default().spawn(glib::clone!(@strong sender => async move {
|
||||
let res = async {
|
||||
let stream = kbd_backlight.receive_brightness_changed().await?;
|
||||
let brightness = kbd_backlight.get_brightness().await?;
|
||||
let max_brightness = kbd_backlight.get_max_brightness().await?;
|
||||
zbus::Result::Ok((brightness, max_brightness, stream))
|
||||
}.await;
|
||||
match res {
|
||||
Ok((brightness, max_brightness, mut stream)) => {
|
||||
let value = (brightness as f64) / (max_brightness as f64);
|
||||
sender.input(AppMsg::UpdateKbdBrightness(value));
|
||||
while let Some(evt) = stream.next().await {
|
||||
// TODO
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
}
|
||||
}
|
||||
}));
|
||||
}
|
||||
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() {
|
||||
self.battery_percent = percentage;
|
||||
}
|
||||
if let Ok(Some(icon_name)) = device.cached_icon_name() {
|
||||
self.icon_name = icon_name;
|
||||
}
|
||||
if let Ok(Some(secs)) = device.cached_time_to_empty() {
|
||||
self.time_remaining = Duration::from_secs(secs as u64);
|
||||
}
|
||||
}
|
||||
}
|
||||
AppMsg::UpdateKbdBrightness(value) => {
|
||||
self.keyboard_brightness = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let app: RelmApp<AppModel> = RelmApp::new("com.system76.CosmicAppletBattery");
|
||||
app.run(());
|
||||
}
|
||||
65
applets/cosmic-applet-battery/src/power_daemon.rs
Normal file
65
applets/cosmic-applet-battery/src/power_daemon.rs
Normal 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<()>;
|
||||
}
|
||||
45
applets/cosmic-applet-battery/src/upower.rs
Normal file
45
applets/cosmic-applet-battery/src/upower.rs
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
//! # 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(
|
||||
default_service = "org.freedesktop.UPower",
|
||||
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>;
|
||||
}
|
||||
146
applets/cosmic-applet-battery/src/upower_device.rs
Normal file
146
applets/cosmic-applet-battery/src/upower_device.rs
Normal file
|
|
@ -0,0 +1,146 @@
|
|||
//! # 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(
|
||||
default_service = "org.freedesktop.UPower",
|
||||
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>;
|
||||
}
|
||||
30
applets/cosmic-applet-battery/src/upower_kbdbacklight.rs
Normal file
30
applets/cosmic-applet-battery/src/upower_kbdbacklight.rs
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
//! # DBus interface proxy for: `org.freedesktop.UPower.KbdBacklight`
|
||||
//!
|
||||
//! This code was generated by `zbus-xmlgen` `2.0.1` from DBus introspection data.
|
||||
//! Source: `Interface '/org/freedesktop/UPower/KbdBacklight' from service 'org.freedesktop.UPower' on system bus`.
|
||||
|
||||
use zbus::dbus_proxy;
|
||||
|
||||
#[dbus_proxy(
|
||||
default_service = "org.freedesktop.UPower",
|
||||
interface = "org.freedesktop.UPower.KbdBacklight",
|
||||
default_path = "/org/freedesktop/UPower/KbdBacklight"
|
||||
)]
|
||||
trait KbdBacklight {
|
||||
/// GetBrightness method
|
||||
fn get_brightness(&self) -> zbus::Result<i32>;
|
||||
|
||||
/// GetMaxBrightness method
|
||||
fn get_max_brightness(&self) -> zbus::Result<i32>;
|
||||
|
||||
/// SetBrightness method
|
||||
fn set_brightness(&self, value: i32) -> zbus::Result<()>;
|
||||
|
||||
/// BrightnessChanged signal
|
||||
#[dbus_proxy(signal)]
|
||||
fn brightness_changed(&self, value: i32) -> zbus::Result<()>;
|
||||
|
||||
/// BrightnessChangedWithSource signal
|
||||
#[dbus_proxy(signal)]
|
||||
fn brightness_changed_with_source(&self, value: i32, source: &str) -> zbus::Result<()>;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue