Commit | Line | Data |
---|---|---|
baedbc7e JC |
1 | use dbus::blocking::stdintf::org_freedesktop_dbus::PropertiesPropertiesChanged; |
2 | use dbus::blocking::Connection; | |
3 | use dbus::message::Message; | |
1e52c28e JC |
4 | use std::collections::HashMap; |
5 | use std::sync::{Arc, Mutex}; | |
baedbc7e JC |
6 | use std::time::Duration; |
7 | ||
1e52c28e JC |
8 | fn update_map(arc_map: Arc<Mutex<HashMap<String, String>>>, key: String, val: String) { |
9 | let clone_arc = arc_map.clone(); | |
10 | let mut map = clone_arc.lock().unwrap(); | |
11 | map.insert(key, val.clone()); | |
12 | for (k, v) in &*map { | |
13 | println! {"{}: {}", k.clone(), v.clone()}; | |
14 | } | |
15 | } | |
16 | ||
baedbc7e | 17 | fn main() -> Result<(), Box<dyn std::error::Error>> { |
1e52c28e | 18 | let arc_locked_xset_map = Arc::new(Mutex::new(HashMap::new())); |
baedbc7e JC |
19 | let conn = Connection::new_session()?; |
20 | ||
21 | let proxy = conn.with_proxy( | |
22 | "org.mpris.MediaPlayer2.spotify", | |
23 | "/org/mpris/MediaPlayer2", | |
24 | Duration::from_millis(5000), | |
25 | ); | |
26 | ||
27 | let _ = proxy.match_signal( | |
1e52c28e | 28 | move |pc: PropertiesPropertiesChanged, _: &Connection, _: &Message| { |
79b46cd5 JC |
29 | pc.changed_properties["Metadata"] |
30 | .0 | |
31 | .as_iter() | |
32 | .and_then(|mut iter| { | |
33 | // Iterating over a RefArg goes key, value, key, value... Insane honestly. | |
34 | while let Some(key) = iter.next() { | |
35 | let key_str = key.as_str()?; | |
36 | let value = iter.next(); | |
37 | ||
1e52c28e JC |
38 | let arc_locked_xset_map = Arc::clone(&arc_locked_xset_map); |
39 | ||
79b46cd5 JC |
40 | match key_str { |
41 | "xesam:artist" => { | |
42 | // Variant holding a variant that should just be a Vec<&str> I | |
43 | // believe. This is _the recommended_ way to do this by the author. | |
44 | let inner_value = value?.as_iter()?.next()?.as_iter()?.next()?; | |
45 | let artist = String::from(inner_value.as_str()?); | |
1e52c28e JC |
46 | update_map( |
47 | arc_locked_xset_map, | |
48 | String::from("artist"), | |
49 | String::from(artist), | |
50 | ); | |
79b46cd5 | 51 | } |
b710f9dc | 52 | "xesam:title" => { |
1e52c28e JC |
53 | let title = value?.as_iter()?.next()?.as_str()?; |
54 | update_map( | |
55 | arc_locked_xset_map, | |
56 | String::from("title"), | |
57 | String::from(title), | |
58 | ); | |
b710f9dc | 59 | } |
79b46cd5 JC |
60 | _ => (), |
61 | } | |
62 | } | |
63 | Some(()) | |
64 | }); | |
baedbc7e JC |
65 | true |
66 | }, | |
67 | ); | |
68 | ||
69 | loop { | |
70 | conn.process(Duration::from_millis(1000))?; | |
71 | } | |
72 | } |