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 | ||
ddaab88f | 8 | fn update_map(arc_map: Arc<Mutex<HashMap<String, String>>>, key: &str, val: &str) { |
1e52c28e JC |
9 | let clone_arc = arc_map.clone(); |
10 | let mut map = clone_arc.lock().unwrap(); | |
ddaab88f | 11 | map.insert(String::from(key), String::from(val)); |
1e52c28e JC |
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 | ||
16683530 | 27 | let spotify_match_map = arc_locked_xset_map.clone(); |
baedbc7e | 28 | let _ = proxy.match_signal( |
1e52c28e | 29 | move |pc: PropertiesPropertiesChanged, _: &Connection, _: &Message| { |
79b46cd5 JC |
30 | pc.changed_properties["Metadata"] |
31 | .0 | |
32 | .as_iter() | |
33 | .and_then(|mut iter| { | |
34 | // Iterating over a RefArg goes key, value, key, value... Insane honestly. | |
35 | while let Some(key) = iter.next() { | |
36 | let key_str = key.as_str()?; | |
37 | let value = iter.next(); | |
38 | ||
39 | match key_str { | |
40 | "xesam:artist" => { | |
41 | // Variant holding a variant that should just be a Vec<&str> I | |
42 | // believe. This is _the recommended_ way to do this by the author. | |
43 | let inner_value = value?.as_iter()?.next()?.as_iter()?.next()?; | |
ddaab88f | 44 | let artist = inner_value.as_str()?; |
16683530 | 45 | update_map(spotify_match_map.clone(), "artist", artist); |
79b46cd5 | 46 | } |
b710f9dc | 47 | "xesam:title" => { |
1e52c28e | 48 | let title = value?.as_iter()?.next()?.as_str()?; |
16683530 | 49 | update_map(spotify_match_map.clone(), "title", title); |
b710f9dc | 50 | } |
79b46cd5 JC |
51 | _ => (), |
52 | } | |
53 | } | |
54 | Some(()) | |
55 | }); | |
baedbc7e JC |
56 | true |
57 | }, | |
58 | ); | |
59 | ||
60 | loop { | |
61 | conn.process(Duration::from_millis(1000))?; | |
62 | } | |
63 | } |