i3status_rs/blocks/music/
zbus_mpris.rsuse std::collections::HashMap;
use zbus::zvariant::{self, ObjectPath, OwnedValue, Value};
#[derive(Debug, Clone)]
pub struct PlayerMetadata {
pub title: Option<String>,
pub artist: Option<String>,
pub url: Option<String>,
}
impl TryFrom<OwnedValue> for PlayerMetadata {
type Error = <HashMap<String, OwnedValue> as TryFrom<OwnedValue>>::Error;
fn try_from(value: OwnedValue) -> Result<Self, Self::Error> {
let map = HashMap::<String, OwnedValue>::try_from(value)?;
let val_to_string = |val: &Value| {
val.downcast_ref::<&str>()
.ok()
.and_then(|val| (!val.is_empty()).then(|| val.to_string()))
};
let title = map.get("xesam:title").and_then(|val| val_to_string(val));
let artists = map
.get("xesam:artist")
.and_then(|val| val.downcast_ref::<&zvariant::Array>().ok())
.map(|val| val.inner());
let artist = artists.and_then(|val| val.first()).and_then(val_to_string);
let url = map.get("xesam:url").and_then(|val| val_to_string(val));
Ok(Self { title, artist, url })
}
}
#[zbus::proxy(
interface = "org.mpris.MediaPlayer2.Player",
default_path = "/org/mpris/MediaPlayer2"
)]
pub(super) trait Player {
fn next(&self) -> zbus::Result<()>;
fn open_uri(&self, uri: &str) -> zbus::Result<()>;
fn pause(&self) -> zbus::Result<()>;
fn play(&self) -> zbus::Result<()>;
fn play_pause(&self) -> zbus::Result<()>;
fn previous(&self) -> zbus::Result<()>;
fn seek(&self, offset: i64) -> zbus::Result<()>;
fn set_position(&self, track_id: &ObjectPath<'_>, position: i64) -> zbus::Result<()>;
fn stop(&self) -> zbus::Result<()>;
#[zbus(signal)]
fn seeked(&self, position: i64) -> zbus::Result<()>;
#[zbus(property)]
fn can_control(&self) -> zbus::Result<bool>;
#[zbus(property)]
fn can_go_next(&self) -> zbus::Result<bool>;
#[zbus(property)]
fn can_go_previous(&self) -> zbus::Result<bool>;
#[zbus(property)]
fn can_pause(&self) -> zbus::Result<bool>;
#[zbus(property)]
fn can_play(&self) -> zbus::Result<bool>;
#[zbus(property)]
fn can_seek(&self) -> zbus::Result<bool>;
#[zbus(property)]
fn maximum_rate(&self) -> zbus::Result<f64>;
#[zbus(property)]
fn metadata(&self) -> zbus::Result<PlayerMetadata>;
#[zbus(property)]
fn minimum_rate(&self) -> zbus::Result<f64>;
#[zbus(property)]
fn playback_status(&self) -> zbus::Result<String>;
#[zbus(property)]
fn position(&self) -> zbus::Result<i64>;
#[zbus(property)]
fn rate(&self) -> zbus::Result<f64>;
#[zbus(property)]
fn set_rate(&self, value: f64) -> zbus::Result<()>;
#[zbus(property)]
fn volume(&self) -> zbus::Result<f64>;
#[zbus(property)]
fn set_volume(&self, value: f64) -> zbus::Result<()>;
}