i3status_rs/blocks/music/
zbus_mpris.rs

1//! # DBus interface proxies for: `org.mpris.MediaPlayer2.Player`
2//!
3//! This code was generated by `zbus-xmlgen` `1.0.0` from DBus introspection data.
4//! Source: `11`.
5//!
6//! You may prefer to adapt it, instead of using it verbatim.
7//!
8//! More information can be found in the
9//! [Writing a client proxy](https://zeenix.pages.freedesktop.org/zbus/client.html)
10//! section of the zbus documentation.
11//!
12//! This DBus object implements
13//! [standard DBus interfaces](https://dbus.freedesktop.org/doc/dbus-specification.html),
14//! (`org.freedesktop.DBus.*`) for which the following zbus proxies can be used:
15//!
16//! * [`zbus::fdo::PropertiesProxy`]
17//! * [`zbus::fdo::IntrospectableProxy`]
18//! * [`zbus::fdo::PeerProxy`]
19//!
20//! …consequently `zbus-xmlgen` did not generate code for the above interfaces.
21
22use 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    /// Next method
64    fn next(&self) -> zbus::Result<()>;
65
66    /// OpenUri method
67    fn open_uri(&self, uri: &str) -> zbus::Result<()>;
68
69    /// Pause method
70    fn pause(&self) -> zbus::Result<()>;
71
72    /// Play method
73    fn play(&self) -> zbus::Result<()>;
74
75    /// PlayPause method
76    fn play_pause(&self) -> zbus::Result<()>;
77
78    /// Previous method
79    fn previous(&self) -> zbus::Result<()>;
80
81    /// Seek method
82    fn seek(&self, offset: i64) -> zbus::Result<()>;
83
84    /// SetPosition method
85    fn set_position(&self, track_id: &ObjectPath<'_>, position: i64) -> zbus::Result<()>;
86
87    /// Stop method
88    fn stop(&self) -> zbus::Result<()>;
89
90    /// Seeked signal
91    #[zbus(signal)]
92    fn seeked(&self, position: i64) -> zbus::Result<()>;
93
94    /// CanControl property
95    #[zbus(property)]
96    fn can_control(&self) -> zbus::Result<bool>;
97
98    /// CanGoNext property
99    #[zbus(property)]
100    fn can_go_next(&self) -> zbus::Result<bool>;
101
102    /// CanGoPrevious property
103    #[zbus(property)]
104    fn can_go_previous(&self) -> zbus::Result<bool>;
105
106    /// CanPause property
107    #[zbus(property)]
108    fn can_pause(&self) -> zbus::Result<bool>;
109
110    /// CanPlay property
111    #[zbus(property)]
112    fn can_play(&self) -> zbus::Result<bool>;
113
114    /// CanSeek property
115    #[zbus(property)]
116    fn can_seek(&self) -> zbus::Result<bool>;
117
118    /// MaximumRate property
119    #[zbus(property)]
120    fn maximum_rate(&self) -> zbus::Result<f64>;
121
122    /// Metadata property
123    #[zbus(property)]
124    fn metadata(&self) -> zbus::Result<PlayerMetadata>;
125
126    /// MinimumRate property
127    #[zbus(property)]
128    fn minimum_rate(&self) -> zbus::Result<f64>;
129
130    /// PlaybackStatus property
131    #[zbus(property)]
132    fn playback_status(&self) -> zbus::Result<String>;
133
134    /// Position property
135    #[zbus(property)]
136    fn position(&self) -> zbus::Result<i64>;
137
138    /// Rate property
139    #[zbus(property)]
140    fn rate(&self) -> zbus::Result<f64>;
141    #[zbus(property)]
142    fn set_rate(&self, value: f64) -> zbus::Result<()>;
143
144    /// Volume property
145    #[zbus(property)]
146    fn volume(&self) -> zbus::Result<f64>;
147    #[zbus(property)]
148    fn set_volume(&self, value: f64) -> zbus::Result<()>;
149}