52 lines
1.4 KiB
Rust
52 lines
1.4 KiB
Rust
// Copyright (c) 2022 Marceline Cramer
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use zbus::fdo::DBusProxy;
|
|
use zbus::zvariant::Value;
|
|
use zbus::{dbus_proxy, Connection, Result};
|
|
|
|
pub type MetadataMap<'a> = HashMap<String, Value<'a>>;
|
|
|
|
#[dbus_proxy(
|
|
interface = "org.mpris.MediaPlayer2.Player",
|
|
default_path = "/org/mpris/MediaPlayer2"
|
|
)]
|
|
trait Player {
|
|
fn next(&self) -> Result<()>;
|
|
fn previous(&self) -> Result<()>;
|
|
fn pause(&self) -> Result<()>;
|
|
fn play_pause(&self) -> Result<()>;
|
|
fn stop(&self) -> Result<()>;
|
|
fn play(&self) -> Result<()>;
|
|
fn seek(&self, offset: i64) -> Result<()>;
|
|
|
|
#[dbus_proxy(property)]
|
|
fn playback_status(&self) -> Result<String>;
|
|
|
|
#[dbus_proxy(property)]
|
|
fn position(&self) -> Result<i64>;
|
|
|
|
#[dbus_proxy(property)]
|
|
fn metadata(&self) -> Result<MetadataMap>;
|
|
}
|
|
|
|
pub async fn find_player(connection: &Connection) -> Result<Option<PlayerProxy>> {
|
|
let dbus = DBusProxy::new(connection).await?;
|
|
let names = dbus.list_names().await?;
|
|
|
|
for name in names {
|
|
let name = name.as_str().to_string();
|
|
if name.starts_with("org.mpris.MediaPlayer2") {
|
|
let player = PlayerProxy::builder(connection)
|
|
.destination(name)?
|
|
.build()
|
|
.await?;
|
|
return Ok(Some(player));
|
|
}
|
|
}
|
|
|
|
Ok(None)
|
|
}
|