2019-08-02 12:56:26 +02:00
|
|
|
use crate::buffer::Buffer;
|
|
|
|
|
use crate::color::Color;
|
2019-08-06 14:04:34 +02:00
|
|
|
use crate::draw::{draw_bar, draw_box, Font, ROBOTO_REGULAR};
|
2019-08-11 00:52:47 +02:00
|
|
|
use crate::modules::module::{Input, ModuleImpl};
|
2019-08-27 15:07:39 +02:00
|
|
|
use crate::cmd::Cmd;
|
2019-08-02 12:56:26 +02:00
|
|
|
|
|
|
|
|
use std::sync::mpsc::Sender;
|
2019-08-04 14:23:49 +02:00
|
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
|
use std::thread;
|
2019-08-02 12:56:26 +02:00
|
|
|
|
|
|
|
|
use chrono::{DateTime, Local};
|
2019-08-04 14:23:49 +02:00
|
|
|
use dbus;
|
2019-08-02 12:56:26 +02:00
|
|
|
|
2019-08-04 14:23:49 +02:00
|
|
|
fn get_upower_property(
|
|
|
|
|
con: &dbus::Connection,
|
|
|
|
|
device_path: &str,
|
|
|
|
|
property: &str,
|
|
|
|
|
) -> Result<dbus::Message, ::std::io::Error> {
|
2019-08-02 12:56:26 +02:00
|
|
|
let msg = dbus::Message::new_method_call(
|
|
|
|
|
"org.freedesktop.UPower",
|
|
|
|
|
device_path,
|
|
|
|
|
"org.freedesktop.DBus.Properties",
|
|
|
|
|
"Get",
|
2019-08-04 14:23:49 +02:00
|
|
|
)
|
|
|
|
|
.map_err(|_| {
|
|
|
|
|
::std::io::Error::new(
|
|
|
|
|
::std::io::ErrorKind::Other,
|
|
|
|
|
"could not send make dbus method call",
|
|
|
|
|
)
|
|
|
|
|
})?
|
|
|
|
|
.append2(
|
|
|
|
|
dbus::MessageItem::Str("org.freedesktop.UPower.Device".to_string()),
|
|
|
|
|
dbus::MessageItem::Str(property.to_string()),
|
|
|
|
|
);;
|
2019-08-02 12:56:26 +02:00
|
|
|
|
2019-08-04 14:23:49 +02:00
|
|
|
con.send_with_reply_and_block(msg, 1000).map_err(|_| {
|
|
|
|
|
::std::io::Error::new(::std::io::ErrorKind::Other, "could not send dbus message")
|
|
|
|
|
})
|
2019-08-02 12:56:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct UpowerBattery {
|
|
|
|
|
device_path: String,
|
2019-08-06 16:39:17 +02:00
|
|
|
font: Font,
|
2019-08-04 14:23:49 +02:00
|
|
|
inner: Arc<Mutex<UpowerBatteryInner>>,
|
2019-08-02 12:56:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct UpowerBatteryInner {
|
|
|
|
|
state: UpowerBatteryState,
|
|
|
|
|
capacity: f64,
|
|
|
|
|
dirty: bool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum UpowerBatteryState {
|
|
|
|
|
Charging,
|
|
|
|
|
Discharging,
|
|
|
|
|
Empty,
|
|
|
|
|
Full,
|
|
|
|
|
NotCharging,
|
|
|
|
|
Unknown,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl UpowerBattery {
|
|
|
|
|
pub fn from_device(device: &str) -> Result<Self, ::std::io::Error> {
|
|
|
|
|
let device_path = format!("/org/freedesktop/UPower/devices/battery_{}", device);
|
2019-08-04 14:23:49 +02:00
|
|
|
let con = dbus::Connection::get_private(dbus::BusType::System).map_err(|_| {
|
|
|
|
|
::std::io::Error::new(::std::io::ErrorKind::Other, "could not get dbus connection")
|
|
|
|
|
})?;
|
2019-08-02 12:56:26 +02:00
|
|
|
|
2019-08-04 14:23:49 +02:00
|
|
|
let upower_type: dbus::arg::Variant<u32> =
|
|
|
|
|
match get_upower_property(&con, &device_path, "Type")?.get1() {
|
|
|
|
|
Some(v) => v,
|
|
|
|
|
None => {
|
|
|
|
|
return Err(::std::io::Error::new(
|
|
|
|
|
::std::io::ErrorKind::Other,
|
|
|
|
|
"no such upower device",
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
};
|
2019-08-02 12:56:26 +02:00
|
|
|
|
|
|
|
|
// https://upower.freedesktop.org/docs/Device.html#Device:Type
|
|
|
|
|
if upower_type.0 != 2 {
|
|
|
|
|
return Err(::std::io::Error::new(
|
|
|
|
|
::std::io::ErrorKind::Other,
|
|
|
|
|
"UPower device is not a battery.",
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-04 14:23:49 +02:00
|
|
|
let capacity: f64 = match get_upower_property(&con, &device_path, "Percentage")?
|
|
|
|
|
.get1::<dbus::arg::Variant<f64>>()
|
|
|
|
|
{
|
2019-08-02 12:56:26 +02:00
|
|
|
Some(v) => v.0,
|
|
|
|
|
None => {
|
2019-08-04 14:23:49 +02:00
|
|
|
return Err(::std::io::Error::new(
|
|
|
|
|
::std::io::ErrorKind::Other,
|
|
|
|
|
"no such upower device",
|
|
|
|
|
));
|
2019-08-02 12:56:26 +02:00
|
|
|
}
|
|
|
|
|
};
|
2019-08-04 14:23:49 +02:00
|
|
|
let state: UpowerBatteryState = match get_upower_property(&con, &device_path, "State")?
|
|
|
|
|
.get1::<dbus::arg::Variant<u32>>()
|
|
|
|
|
{
|
2019-08-02 12:56:26 +02:00
|
|
|
Some(v) => match v.0 {
|
|
|
|
|
1 => UpowerBatteryState::Charging,
|
|
|
|
|
2 => UpowerBatteryState::Discharging,
|
|
|
|
|
3 => UpowerBatteryState::Empty,
|
|
|
|
|
4 => UpowerBatteryState::Full,
|
|
|
|
|
5 => UpowerBatteryState::NotCharging,
|
|
|
|
|
6 => UpowerBatteryState::Discharging,
|
|
|
|
|
_ => UpowerBatteryState::Unknown,
|
|
|
|
|
},
|
|
|
|
|
None => {
|
2019-08-04 14:23:49 +02:00
|
|
|
return Err(::std::io::Error::new(
|
|
|
|
|
::std::io::ErrorKind::Other,
|
|
|
|
|
"no such upower device",
|
|
|
|
|
));
|
2019-08-02 12:56:26 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-08-06 16:39:17 +02:00
|
|
|
let mut font = Font::new(&ROBOTO_REGULAR, 24.0);
|
|
|
|
|
font.add_str_to_cache("battery");
|
|
|
|
|
|
2019-08-02 12:56:26 +02:00
|
|
|
Ok(UpowerBattery {
|
|
|
|
|
device_path,
|
2019-08-06 16:39:17 +02:00
|
|
|
font: font,
|
2019-08-04 14:23:49 +02:00
|
|
|
inner: Arc::new(Mutex::new(UpowerBatteryInner {
|
2019-08-02 12:56:26 +02:00
|
|
|
capacity: capacity,
|
|
|
|
|
state: state,
|
2019-08-04 14:23:49 +02:00
|
|
|
dirty: true,
|
2019-08-02 12:56:26 +02:00
|
|
|
})),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-27 15:07:39 +02:00
|
|
|
pub fn new(listener: Sender<Cmd>) -> Result<Self, ::std::io::Error> {
|
2019-08-02 12:56:26 +02:00
|
|
|
let d = UpowerBattery::from_device("BAT0")?;
|
|
|
|
|
let path = d.device_path.clone();
|
|
|
|
|
let inner = d.inner.clone();
|
|
|
|
|
thread::spawn(move || {
|
|
|
|
|
let con = dbus::Connection::get_private(dbus::BusType::System)
|
|
|
|
|
.expect("Failed to establish D-Bus connection.");
|
|
|
|
|
let rule = format!(
|
|
|
|
|
"type='signal',\
|
|
|
|
|
path='{}',\
|
|
|
|
|
interface='org.freedesktop.DBus.Properties',\
|
|
|
|
|
member='PropertiesChanged'",
|
|
|
|
|
path
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// First we're going to get an (irrelevant) NameAcquired event.
|
|
|
|
|
con.incoming(10_000).next();
|
|
|
|
|
|
|
|
|
|
con.add_match(&rule)
|
|
|
|
|
.expect("Failed to add D-Bus match rule.");
|
|
|
|
|
|
|
|
|
|
loop {
|
|
|
|
|
if con.incoming(10_000).next().is_some() {
|
2019-08-04 14:23:49 +02:00
|
|
|
let capacity = get_upower_property(&con, &path, "Percentage")
|
|
|
|
|
.unwrap()
|
|
|
|
|
.get1::<dbus::arg::Variant<f64>>()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.0;
|
|
|
|
|
let state = match get_upower_property(&con, &path, "State")
|
|
|
|
|
.unwrap()
|
|
|
|
|
.get1::<dbus::arg::Variant<u32>>()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.0
|
|
|
|
|
{
|
2019-08-02 12:56:26 +02:00
|
|
|
1 => UpowerBatteryState::Charging,
|
|
|
|
|
2 => UpowerBatteryState::Discharging,
|
|
|
|
|
3 => UpowerBatteryState::Empty,
|
|
|
|
|
4 => UpowerBatteryState::Full,
|
|
|
|
|
5 => UpowerBatteryState::NotCharging,
|
|
|
|
|
6 => UpowerBatteryState::Discharging,
|
|
|
|
|
_ => UpowerBatteryState::Unknown,
|
|
|
|
|
};
|
|
|
|
|
let mut inner = inner.lock().unwrap();
|
|
|
|
|
inner.state = state;
|
|
|
|
|
inner.capacity = capacity;
|
|
|
|
|
inner.dirty = true;
|
2019-08-27 15:07:39 +02:00
|
|
|
listener.send(Cmd::Draw).unwrap();
|
2019-08-02 12:56:26 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Ok(d)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ModuleImpl for UpowerBattery {
|
|
|
|
|
fn draw(
|
|
|
|
|
&self,
|
|
|
|
|
buf: &mut Buffer,
|
|
|
|
|
bg: &Color,
|
|
|
|
|
_time: &DateTime<Local>,
|
|
|
|
|
) -> Result<Vec<(i32, i32, i32, i32)>, ::std::io::Error> {
|
|
|
|
|
buf.memset(bg);
|
2019-08-12 01:48:00 +02:00
|
|
|
let inner = self.inner.lock().unwrap();
|
2019-08-04 21:52:13 +02:00
|
|
|
let text_color = Color::new(1.0, 1.0, 1.0, 1.0);
|
2019-08-12 01:48:00 +02:00
|
|
|
let bar_color = match inner.state {
|
2019-08-06 14:21:10 +02:00
|
|
|
UpowerBatteryState::Discharging | UpowerBatteryState::Unknown => {
|
2019-08-12 01:48:00 +02:00
|
|
|
if inner.capacity > 10.0 {
|
|
|
|
|
Color::new(1.0, 1.0, 1.0, 1.0)
|
|
|
|
|
} else {
|
|
|
|
|
Color::new(1.0, 0.5, 0.0, 1.0)
|
|
|
|
|
}
|
2019-08-06 14:21:10 +02:00
|
|
|
}
|
|
|
|
|
UpowerBatteryState::Charging | UpowerBatteryState::Full => {
|
|
|
|
|
Color::new(0.5, 1.0, 0.5, 1.0)
|
|
|
|
|
}
|
|
|
|
|
UpowerBatteryState::NotCharging | UpowerBatteryState::Empty => {
|
|
|
|
|
Color::new(1.0, 0.5, 0.5, 1.0)
|
|
|
|
|
}
|
2019-08-04 21:52:13 +02:00
|
|
|
};
|
|
|
|
|
|
2019-08-06 16:39:17 +02:00
|
|
|
self.font.draw_text(
|
2019-08-02 15:59:10 +02:00
|
|
|
&mut buf.subdimensions((0, 0, 128, 24))?,
|
2019-08-02 12:56:26 +02:00
|
|
|
bg,
|
2019-08-04 21:52:13 +02:00
|
|
|
&text_color,
|
2019-08-02 12:56:26 +02:00
|
|
|
"battery",
|
|
|
|
|
)?;
|
|
|
|
|
draw_bar(
|
2019-08-07 00:01:07 +02:00
|
|
|
&mut buf.subdimensions((128, 0, 464, 24))?,
|
2019-08-04 21:52:13 +02:00
|
|
|
&bar_color,
|
2019-08-07 00:01:07 +02:00
|
|
|
464,
|
2019-08-02 12:56:26 +02:00
|
|
|
24,
|
|
|
|
|
(inner.capacity as f32) / 100.0,
|
|
|
|
|
)?;
|
|
|
|
|
|
2019-08-06 14:21:10 +02:00
|
|
|
draw_box(
|
2019-08-07 00:01:07 +02:00
|
|
|
&mut buf.subdimensions((128, 0, 464, 24))?,
|
2019-08-06 14:21:10 +02:00
|
|
|
&bar_color,
|
2019-08-07 00:01:07 +02:00
|
|
|
(464, 24),
|
2019-08-06 14:21:10 +02:00
|
|
|
)?;
|
2019-08-02 12:56:26 +02:00
|
|
|
Ok(vec![buf.get_signed_bounds()])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn update(&mut self, _time: &DateTime<Local>, force: bool) -> Result<bool, ::std::io::Error> {
|
|
|
|
|
let mut inner = self.inner.lock().unwrap();
|
|
|
|
|
if inner.dirty || force {
|
|
|
|
|
inner.dirty = false;
|
|
|
|
|
Ok(true)
|
|
|
|
|
} else {
|
|
|
|
|
Ok(false)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn input(&mut self, _input: Input) {}
|
|
|
|
|
}
|