Skip to content

Commit

Permalink
Emit signal for changed properties on track change
Browse files Browse the repository at this point in the history
  • Loading branch information
hrkfdn committed Apr 1, 2023
1 parent 4d60b2b commit baa4dc7
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 27 deletions.
7 changes: 5 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,12 @@ fn main() -> Result<(), String> {
));

#[cfg(feature = "mpris")]
ASYNC_RUNTIME.spawn(mpris::serve(
let mpris_manager = mpris::MprisManager::new(
event_manager.clone(),
queue.clone(),
library.clone(),
spotify.clone(),
));
);

let mut cmd_manager = CommandManager::new(
spotify.clone(),
Expand Down Expand Up @@ -364,6 +364,9 @@ fn main() -> Result<(), String> {
trace!("event received: {:?}", state);
spotify.update_status(state.clone());

#[cfg(feature = "mpris")]
mpris_manager.update()?;

#[cfg(unix)]
ipc.publish(&state, queue.get_current());

Expand Down
89 changes: 64 additions & 25 deletions src/mpris.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use std::collections::HashMap;
use std::{error::Error, future::pending, sync::Arc};
use std::error::Error;
use std::sync::Arc;
use tokio::sync::mpsc;
use tokio_stream::wrappers::UnboundedReceiverStream;
use tokio_stream::StreamExt;
use zbus::zvariant::{ObjectPath, Value};
use zbus::{dbus_interface, ConnectionBuilder};

Expand All @@ -14,6 +18,7 @@ use crate::queue::RepeatSetting;
use crate::spotify::UriType;
use crate::spotify_url::SpotifyUrl;
use crate::traits::ListItem;
use crate::ASYNC_RUNTIME;
use crate::{
events::EventManager,
queue::Queue,
Expand Down Expand Up @@ -453,28 +458,62 @@ impl MprisPlayer {
}
}

pub async fn serve(
event: EventManager,
queue: Arc<Queue>,
library: Arc<Library>,
spotify: Spotify,
) -> Result<(), Box<dyn Error + Sync + Send>> {
let root = MprisRoot {};
let player = MprisPlayer {
event,
queue,
library,
spotify,
};

let _conn = ConnectionBuilder::session()?
.name("org.mpris.MediaPlayer2.ncspot")?
.serve_at("/org/mpris/MediaPlayer2", root)?
.serve_at("/org/mpris/MediaPlayer2", player)?
.build()
.await?;

pending::<()>().await;

Ok(())
pub struct MprisManager {
tx: mpsc::UnboundedSender<()>,
}

impl MprisManager {
pub fn new(
event: EventManager,
queue: Arc<Queue>,
library: Arc<Library>,
spotify: Spotify,
) -> Self {
let root = MprisRoot {};
let player = MprisPlayer {
event,
queue,
library,
spotify,
};

let (tx, rx) = mpsc::unbounded_channel::<()>();

ASYNC_RUNTIME.spawn(Self::serve(UnboundedReceiverStream::new(rx), root, player));

MprisManager { tx }
}

async fn serve(
mut rx: UnboundedReceiverStream<()>,
root: MprisRoot,
player: MprisPlayer,
) -> Result<(), Box<dyn Error + Sync + Send>> {
let conn = ConnectionBuilder::session()?
.name("org.mpris.MediaPlayer2.ncspot")?
.serve_at("/org/mpris/MediaPlayer2", root)?
.serve_at("/org/mpris/MediaPlayer2", player)?
.build()
.await?;

let object_server = conn.object_server();
let player_iface_ref = object_server
.interface::<_, MprisPlayer>("/org/mpris/MediaPlayer2")
.await?;
let player_iface = player_iface_ref.get().await;

loop {
tokio::select! {
Some(()) = rx.next() => {
let ctx = player_iface_ref.signal_context();
player_iface.playback_status_changed(ctx).await?;
player_iface.metadata_changed(ctx).await?;
}
}
}
}

pub fn update(&self) -> Result<(), String> {
self.tx.send(()).map_err(|e| e.to_string())
}
}

0 comments on commit baa4dc7

Please sign in to comment.