i3status_rs/blocks/music/
zbus_mpris.rs1use std::collections::HashMap;
23use zbus::zvariant::{self, ObjectPath, OwnedValue, Value};
24
25#[derive(Debug, Clone)]
26pub struct PlayerMetadata {
27 pub title: Option<String>,
28 pub artist: Option<String>,
29 pub url: Option<String>,
30}
31
32impl TryFrom<OwnedValue> for PlayerMetadata {
33 type Error = <HashMap<String, OwnedValue> as TryFrom<OwnedValue>>::Error;
34
35 fn try_from(value: OwnedValue) -> Result<Self, Self::Error> {
36 let map = HashMap::<String, OwnedValue>::try_from(value)?;
37
38 let val_to_string = |val: &Value| {
39 val.downcast_ref::<&str>()
40 .ok()
41 .and_then(|val| (!val.is_empty()).then(|| val.to_string()))
42 };
43
44 let title = map.get("xesam:title").and_then(|val| val_to_string(val));
45
46 let artists = map
47 .get("xesam:artist")
48 .and_then(|val| val.downcast_ref::<&zvariant::Array>().ok())
49 .map(|val| val.inner());
50 let artist = artists.and_then(|val| val.first()).and_then(val_to_string);
51
52 let url = map.get("xesam:url").and_then(|val| val_to_string(val));
53
54 Ok(Self { title, artist, url })
55 }
56}
57
58#[zbus::proxy(
59 interface = "org.mpris.MediaPlayer2.Player",
60 default_path = "/org/mpris/MediaPlayer2"
61)]
62pub(super) trait Player {
63 fn next(&self) -> zbus::Result<()>;
65
66 fn open_uri(&self, uri: &str) -> zbus::Result<()>;
68
69 fn pause(&self) -> zbus::Result<()>;
71
72 fn play(&self) -> zbus::Result<()>;
74
75 fn play_pause(&self) -> zbus::Result<()>;
77
78 fn previous(&self) -> zbus::Result<()>;
80
81 fn seek(&self, offset: i64) -> zbus::Result<()>;
83
84 fn set_position(&self, track_id: &ObjectPath<'_>, position: i64) -> zbus::Result<()>;
86
87 fn stop(&self) -> zbus::Result<()>;
89
90 #[zbus(signal)]
92 fn seeked(&self, position: i64) -> zbus::Result<()>;
93
94 #[zbus(property)]
96 fn can_control(&self) -> zbus::Result<bool>;
97
98 #[zbus(property)]
100 fn can_go_next(&self) -> zbus::Result<bool>;
101
102 #[zbus(property)]
104 fn can_go_previous(&self) -> zbus::Result<bool>;
105
106 #[zbus(property)]
108 fn can_pause(&self) -> zbus::Result<bool>;
109
110 #[zbus(property)]
112 fn can_play(&self) -> zbus::Result<bool>;
113
114 #[zbus(property)]
116 fn can_seek(&self) -> zbus::Result<bool>;
117
118 #[zbus(property)]
120 fn maximum_rate(&self) -> zbus::Result<f64>;
121
122 #[zbus(property)]
124 fn metadata(&self) -> zbus::Result<PlayerMetadata>;
125
126 #[zbus(property)]
128 fn minimum_rate(&self) -> zbus::Result<f64>;
129
130 #[zbus(property)]
132 fn playback_status(&self) -> zbus::Result<String>;
133
134 #[zbus(property)]
136 fn position(&self) -> zbus::Result<i64>;
137
138 #[zbus(property)]
140 fn rate(&self) -> zbus::Result<f64>;
141 #[zbus(property)]
142 fn set_rate(&self, value: f64) -> zbus::Result<()>;
143
144 #[zbus(property)]
146 fn volume(&self) -> zbus::Result<f64>;
147 #[zbus(property)]
148 fn set_volume(&self, value: f64) -> zbus::Result<()>;
149}