Commit | Line | Data |
---|---|---|
baedbc7e JC |
1 | use dbus::blocking::stdintf::org_freedesktop_dbus::PropertiesPropertiesChanged; |
2 | use dbus::blocking::Connection; | |
3 | use dbus::message::Message; | |
4 | use std::time::Duration; | |
5 | ||
6 | fn main() -> Result<(), Box<dyn std::error::Error>> { | |
7 | let conn = Connection::new_session()?; | |
8 | ||
9 | let proxy = conn.with_proxy( | |
10 | "org.mpris.MediaPlayer2.spotify", | |
11 | "/org/mpris/MediaPlayer2", | |
12 | Duration::from_millis(5000), | |
13 | ); | |
14 | ||
15 | let _ = proxy.match_signal( | |
16 | |pc: PropertiesPropertiesChanged, _: &Connection, _: &Message| { | |
79b46cd5 JC |
17 | pc.changed_properties["Metadata"] |
18 | .0 | |
19 | .as_iter() | |
20 | .and_then(|mut iter| { | |
21 | // Iterating over a RefArg goes key, value, key, value... Insane honestly. | |
22 | while let Some(key) = iter.next() { | |
23 | let key_str = key.as_str()?; | |
24 | let value = iter.next(); | |
25 | ||
26 | match key_str { | |
27 | "xesam:artist" => { | |
28 | // Variant holding a variant that should just be a Vec<&str> I | |
29 | // believe. This is _the recommended_ way to do this by the author. | |
30 | let inner_value = value?.as_iter()?.next()?.as_iter()?.next()?; | |
31 | let artist = String::from(inner_value.as_str()?); | |
32 | println!("artist: {}", artist); | |
33 | } | |
b710f9dc JC |
34 | "xesam:title" => { |
35 | println!("title: {}", value?.as_iter()?.next()?.as_str()?); | |
36 | } | |
79b46cd5 JC |
37 | _ => (), |
38 | } | |
39 | } | |
40 | Some(()) | |
41 | }); | |
baedbc7e JC |
42 | true |
43 | }, | |
44 | ); | |
45 | ||
46 | loop { | |
47 | conn.process(Duration::from_millis(1000))?; | |
48 | } | |
49 | } |