rustfmt
This commit is contained in:
+65
-35
@@ -1,33 +1,45 @@
|
||||
use crate::buffer::Buffer;
|
||||
use crate::color::Color;
|
||||
use crate::draw::{draw_bar, draw_text, draw_box, ROBOTO_REGULAR};
|
||||
use crate::draw::{draw_bar, draw_box, draw_text, ROBOTO_REGULAR};
|
||||
use crate::module::{Input, ModuleImpl};
|
||||
|
||||
use std::thread;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::sync::mpsc::Sender;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::thread;
|
||||
|
||||
use dbus;
|
||||
use chrono::{DateTime, Local};
|
||||
use dbus;
|
||||
|
||||
fn get_upower_property(con: &dbus::Connection, device_path: &str, property: &str) -> Result<dbus::Message, ::std::io::Error> {
|
||||
fn get_upower_property(
|
||||
con: &dbus::Connection,
|
||||
device_path: &str,
|
||||
property: &str,
|
||||
) -> Result<dbus::Message, ::std::io::Error> {
|
||||
let msg = dbus::Message::new_method_call(
|
||||
"org.freedesktop.UPower",
|
||||
device_path,
|
||||
"org.freedesktop.DBus.Properties",
|
||||
"Get",
|
||||
).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()),
|
||||
);;
|
||||
)
|
||||
.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()),
|
||||
);;
|
||||
|
||||
con.send_with_reply_and_block(msg, 1000).map_err(|_| {::std::io::Error::new(::std::io::ErrorKind::Other, "could not send dbus message")})
|
||||
con.send_with_reply_and_block(msg, 1000).map_err(|_| {
|
||||
::std::io::Error::new(::std::io::ErrorKind::Other, "could not send dbus message")
|
||||
})
|
||||
}
|
||||
|
||||
pub struct UpowerBattery {
|
||||
device_path: String,
|
||||
inner: Arc<Mutex<UpowerBatteryInner>>
|
||||
inner: Arc<Mutex<UpowerBatteryInner>>,
|
||||
}
|
||||
|
||||
pub struct UpowerBatteryInner {
|
||||
@@ -48,15 +60,20 @@ enum UpowerBatteryState {
|
||||
impl UpowerBattery {
|
||||
pub fn from_device(device: &str) -> Result<Self, ::std::io::Error> {
|
||||
let device_path = format!("/org/freedesktop/UPower/devices/battery_{}", device);
|
||||
let con = dbus::Connection::get_private(dbus::BusType::System)
|
||||
.map_err(|_| ::std::io::Error::new(::std::io::ErrorKind::Other, "could not get dbus connection"))?;
|
||||
let con = dbus::Connection::get_private(dbus::BusType::System).map_err(|_| {
|
||||
::std::io::Error::new(::std::io::ErrorKind::Other, "could not get dbus connection")
|
||||
})?;
|
||||
|
||||
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"));
|
||||
}
|
||||
};
|
||||
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",
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
// https://upower.freedesktop.org/docs/Device.html#Device:Type
|
||||
if upower_type.0 != 2 {
|
||||
@@ -66,13 +83,20 @@ impl UpowerBattery {
|
||||
));
|
||||
}
|
||||
|
||||
let capacity: f64 = match get_upower_property(&con, &device_path, "Percentage")?.get1::<dbus::arg::Variant<f64>>() {
|
||||
let capacity: f64 = match get_upower_property(&con, &device_path, "Percentage")?
|
||||
.get1::<dbus::arg::Variant<f64>>()
|
||||
{
|
||||
Some(v) => v.0,
|
||||
None => {
|
||||
return Err(::std::io::Error::new(::std::io::ErrorKind::Other, "no such upower device"));
|
||||
return Err(::std::io::Error::new(
|
||||
::std::io::ErrorKind::Other,
|
||||
"no such upower device",
|
||||
));
|
||||
}
|
||||
};
|
||||
let state: UpowerBatteryState = match get_upower_property(&con, &device_path, "State")?.get1::<dbus::arg::Variant<u32>>() {
|
||||
let state: UpowerBatteryState = match get_upower_property(&con, &device_path, "State")?
|
||||
.get1::<dbus::arg::Variant<u32>>()
|
||||
{
|
||||
Some(v) => match v.0 {
|
||||
1 => UpowerBatteryState::Charging,
|
||||
2 => UpowerBatteryState::Discharging,
|
||||
@@ -83,16 +107,19 @@ impl UpowerBattery {
|
||||
_ => UpowerBatteryState::Unknown,
|
||||
},
|
||||
None => {
|
||||
return Err(::std::io::Error::new(::std::io::ErrorKind::Other, "no such upower device"));
|
||||
return Err(::std::io::Error::new(
|
||||
::std::io::ErrorKind::Other,
|
||||
"no such upower device",
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
Ok(UpowerBattery {
|
||||
device_path,
|
||||
inner: Arc::new(Mutex::new(UpowerBatteryInner{
|
||||
inner: Arc::new(Mutex::new(UpowerBatteryInner {
|
||||
capacity: capacity,
|
||||
state: state,
|
||||
dirty: true
|
||||
dirty: true,
|
||||
})),
|
||||
})
|
||||
}
|
||||
@@ -120,8 +147,17 @@ impl UpowerBattery {
|
||||
|
||||
loop {
|
||||
if con.incoming(10_000).next().is_some() {
|
||||
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 {
|
||||
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
|
||||
{
|
||||
1 => UpowerBatteryState::Charging,
|
||||
2 => UpowerBatteryState::Discharging,
|
||||
3 => UpowerBatteryState::Empty,
|
||||
@@ -140,9 +176,7 @@ impl UpowerBattery {
|
||||
});
|
||||
|
||||
Ok(d)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl ModuleImpl for UpowerBattery {
|
||||
@@ -171,11 +205,7 @@ impl ModuleImpl for UpowerBattery {
|
||||
(inner.capacity as f32) / 100.0,
|
||||
)?;
|
||||
|
||||
draw_box(
|
||||
&mut buf.subdimensions((128, 0, 432, 24))?,
|
||||
&c,
|
||||
(432, 24),
|
||||
)?;
|
||||
draw_box(&mut buf.subdimensions((128, 0, 432, 24))?, &c, (432, 24))?;
|
||||
Ok(vec![buf.get_signed_bounds()])
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user