Find active player and listen for playback status changes
This commit is contained in:
parent
55859ab5c0
commit
a2432f77d2
|
@ -8,6 +8,7 @@ use canary_music_player::*;
|
||||||
pub mod mpris;
|
pub mod mpris;
|
||||||
|
|
||||||
use mpris::*;
|
use mpris::*;
|
||||||
|
use smol::stream::StreamExt;
|
||||||
|
|
||||||
pub struct Metadata {
|
pub struct Metadata {
|
||||||
pub album: AlbumInfo,
|
pub album: AlbumInfo,
|
||||||
|
@ -93,7 +94,14 @@ fn main() {
|
||||||
|
|
||||||
let result: Result<(), Box<dyn std::error::Error>> = smol::block_on(async {
|
let result: Result<(), Box<dyn std::error::Error>> = smol::block_on(async {
|
||||||
let dbus = zbus::Connection::session().await?;
|
let dbus = zbus::Connection::session().await?;
|
||||||
let player = PlayerProxy::new(&dbus).await?;
|
let player = find_player(&dbus).await?.unwrap();
|
||||||
|
|
||||||
|
let mut playback_status = player.receive_playback_status_changed().await;
|
||||||
|
while let Some(status) = playback_status.next().await {
|
||||||
|
println!("Status: {}", status.get().await?);
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("Done looping.");
|
||||||
|
|
||||||
let protocol = "tebibyte-media.desktop.music-player-controller".to_string();
|
let protocol = "tebibyte-media.desktop.music-player-controller".to_string();
|
||||||
let script = std::path::PathBuf::from(&module_path);
|
let script = std::path::PathBuf::from(&module_path);
|
||||||
|
|
|
@ -1,11 +1,15 @@
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use zbus::fdo::DBusProxy;
|
||||||
use zbus::zvariant::Value;
|
use zbus::zvariant::Value;
|
||||||
use zbus::{dbus_proxy, Result};
|
use zbus::{dbus_proxy, Connection, Result};
|
||||||
|
|
||||||
pub type MetadataMap<'a> = HashMap<String, Value<'a>>;
|
pub type MetadataMap<'a> = HashMap<String, Value<'a>>;
|
||||||
|
|
||||||
#[dbus_proxy(interface = "org.mpris.MediaPlayer2.Player")]
|
#[dbus_proxy(
|
||||||
|
interface = "org.mpris.MediaPlayer2.Player",
|
||||||
|
default_path = "/org/mpris/MediaPlayer2"
|
||||||
|
)]
|
||||||
trait Player {
|
trait Player {
|
||||||
fn next(&self) -> Result<()>;
|
fn next(&self) -> Result<()>;
|
||||||
fn previous(&self) -> Result<()>;
|
fn previous(&self) -> Result<()>;
|
||||||
|
@ -23,3 +27,21 @@ trait Player {
|
||||||
#[dbus_proxy(property)]
|
#[dbus_proxy(property)]
|
||||||
fn metadata(&self) -> Result<MetadataMap>;
|
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)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue