X-Git-Url: https://git.jacobcasper.com/?p=xsetrootd.git;a=blobdiff_plain;f=src%2Fmain.rs;fp=src%2Fmain.rs;h=fd92244c828fa0d01c6c6475cf0b3bc3920225dd;hp=938002793e0085f86411d092fe6303ada76bcac5;hb=1e52c28e97aa8e39c001f9e4c6a65ff81c3ea474;hpb=b710f9dca5681fe1b2fcddab6e1d78a3a0d86668 diff --git a/src/main.rs b/src/main.rs index 9380027..fd92244 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,21 @@ use dbus::blocking::stdintf::org_freedesktop_dbus::PropertiesPropertiesChanged; use dbus::blocking::Connection; use dbus::message::Message; +use std::collections::HashMap; +use std::sync::{Arc, Mutex}; use std::time::Duration; +fn update_map(arc_map: Arc>>, key: String, val: String) { + let clone_arc = arc_map.clone(); + let mut map = clone_arc.lock().unwrap(); + map.insert(key, val.clone()); + for (k, v) in &*map { + println! {"{}: {}", k.clone(), v.clone()}; + } +} + fn main() -> Result<(), Box> { + let arc_locked_xset_map = Arc::new(Mutex::new(HashMap::new())); let conn = Connection::new_session()?; let proxy = conn.with_proxy( @@ -13,7 +25,7 @@ fn main() -> Result<(), Box> { ); let _ = proxy.match_signal( - |pc: PropertiesPropertiesChanged, _: &Connection, _: &Message| { + move |pc: PropertiesPropertiesChanged, _: &Connection, _: &Message| { pc.changed_properties["Metadata"] .0 .as_iter() @@ -23,16 +35,27 @@ fn main() -> Result<(), Box> { let key_str = key.as_str()?; let value = iter.next(); + let arc_locked_xset_map = Arc::clone(&arc_locked_xset_map); + match key_str { "xesam:artist" => { // Variant holding a variant that should just be a Vec<&str> I // believe. This is _the recommended_ way to do this by the author. let inner_value = value?.as_iter()?.next()?.as_iter()?.next()?; let artist = String::from(inner_value.as_str()?); - println!("artist: {}", artist); + update_map( + arc_locked_xset_map, + String::from("artist"), + String::from(artist), + ); } "xesam:title" => { - println!("title: {}", value?.as_iter()?.next()?.as_str()?); + let title = value?.as_iter()?.next()?.as_str()?; + update_map( + arc_locked_xset_map, + String::from("title"), + String::from(title), + ); } _ => (), }