Add dbus support

This commit is contained in:
Alexandr Bogomyakov
2019-11-29 18:17:57 +03:00
parent b070d50bc7
commit a9216ff630

View File

@@ -1,9 +1,7 @@
extern crate dbus; extern crate dbus;
use crate::dbus::blocking::stdintf::org_freedesktop_dbus::Properties;
use dbus::blocking::Connection; use dbus::{arg, blocking::Connection};
use std::env; use std::{collections::HashMap, env, fs, time::Duration};
use std::fs;
use std::time::Duration;
use sys_info; use sys_info;
const LOW: &str = "#[fg=colour186]"; const LOW: &str = "#[fg=colour186]";
@@ -60,29 +58,43 @@ fn cpu_load_bar(bar_len: i32) {
print!("{:.2} LA1", one); print!("{:.2} LA1", one);
} }
fn player_info() -> Result<(), Box<dyn std::error::Error>> { fn print_refarg(value: &dyn arg::RefArg) {
// First open up a connection to the session bus. // We don't know what type the value is. We'll try a few and fall back to
let conn = Connection::new_session()?; // debug printing if the value is more complex than that.
if let Some(s) = value.as_str() {
println!("{}", s);
} else if let Some(i) = value.as_i64() {
println!("{}", i);
// } else if let Some(mut c) = value.as_iter() {
// while let Some(key) = c.next() {
// // Printing the key is easy, since we know it's a String.
// print!(" {}: ", key.as_str().unwrap());
// let val = c.next().unwrap();
// print_refarg(&val);
// }
} else {
println!("{:?}", value);
}
}
// Second, create a wrapper struct around the connection that makes it easy fn player_info() -> Result<(), Box<dyn std::error::Error>> {
// to send method calls to a specific destination and path. let conn = Connection::new_session()?;
let proxy = conn.with_proxy( let proxy = conn.with_proxy(
"org.mpris.MediaPlayer2.cmus", "org.mpris.MediaPlayer2.cmus",
"/org/mpris/MediaPlayer2", "/org/mpris/MediaPlayer2",
Duration::from_millis(5000), Duration::from_millis(5000),
); );
let metadata: Box<dyn arg::RefArg> =
proxy.get("org.mpris.MediaPlayer2.Player", "Metadata")?;
let mut iter = metadata.as_iter().unwrap();
// Now make the method call. The ListNames method call takes zero input parameters and println!("Option 2:");
// one output parameter which is an array of strings. while let Some(key) = iter.next() {
// Therefore the input is a zero tuple "()", and the output is a single tuple "(names,)". // Printing the key is easy, since we know it's a String.
let (names,): (Vec<String>,) = print!(" {}: ", key.as_str().unwrap());
proxy.method_call("org.mpris.MediaPlayer2.cmus", "Metadata", ())?; let value = iter.next().unwrap();
print_refarg(&value);
// Let's print all the names to stdout.
for name in names {
println!("{}", name);
} }
Ok(()) Ok(())
} }
@@ -98,6 +110,7 @@ fn main() {
"-mb" => mem_load_bar(15), "-mb" => mem_load_bar(15),
"-p" => { "-p" => {
let x = player_info(); let x = player_info();
println!("{:?}", x);
} }
_ => panic!(help_text), _ => panic!(help_text),
}, },