From d873d03973f448298051588a2f990e935a6b1e29 Mon Sep 17 00:00:00 2001 From: Erica Z Date: Fri, 1 Nov 2024 15:17:37 +0100 Subject: [PATCH] can now set volume again thru mpris --- src/mpris/player.rs | 53 ++++++++++++++++++++++++++++++++------------- src/playbin.vala | 2 +- 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/src/mpris/player.rs b/src/mpris/player.rs index 9be58b9..0b2f94d 100644 --- a/src/mpris/player.rs +++ b/src/mpris/player.rs @@ -12,24 +12,24 @@ struct MetadataMap { } impl MetadataMap { - fn as_hash_map(&self) -> HashMap<&'static str, Value> { - HashMap::from([("mpris:trackid", Value::new(self.trackid.as_ref()))]) - } -} - -impl Default for MetadataMap { - fn default() -> Self { + fn from_playbin(_playbin: &crate::Playbin) -> Self { + // TODO Self { trackid: "/org/mpris/MediaPlayer2/TrackList/NoTrack" .try_into() .unwrap(), } } + + fn as_hash_map(&self) -> HashMap<&'static str, Value> { + HashMap::from([("mpris:trackid", Value::new(self.trackid.as_ref()))]) + } } pub struct Player { playbin: SendWeakRef, metadata: MetadataMap, + volume: f64, } impl Player { @@ -41,12 +41,13 @@ impl Player { let player = Self { playbin: playbin.downgrade().into(), - metadata: Default::default(), + metadata: MetadataMap::from_playbin(playbin), + volume: (playbin.volume() as f64) / 100.0, }; object_server.at("/org/mpris/MediaPlayer2", player).await?; - let player = object_server + let player_ref = object_server .interface::<_, Self>("/org/mpris/MediaPlayer2") .await?; @@ -54,15 +55,35 @@ impl Player { "new-track", false, glib::closure_local!( - #[strong] - player, + //#[strong] + //player, move |_playbin: &crate::Playbin| { - let _player = player.get_mut(); // TODO } ), ); + playbin.connect_notify_local( + Some("volume"), + glib::clone!( + #[strong] + player_ref, + move |playbin: &crate::Playbin, _| { + let playbin_volume = playbin.volume(); + + let player_ref = player_ref.clone(); + glib::spawn_future_local(async move { + let mut player = player_ref.get_mut().await; + player.volume = (playbin_volume as f64) / 100.0; + player + .volume_changed(player_ref.signal_emitter()) + .await + .unwrap(); + }); + } + ), + ); + Ok(()) } } @@ -158,8 +179,10 @@ impl Player { } #[zbus(property)] - fn set_volume(&self, _volume: f64) { - todo!() + fn set_volume(&mut self, volume: f64) { + let playbin = self.playbin.upgrade().unwrap(); + // FIXME: check if this is set by the notify callback: self.volume = volume; + playbin.set_volume((volume * 100.0) as i32); } #[zbus(property(emits_changed_signal = "false"))] @@ -204,6 +227,6 @@ impl Player { #[zbus(property(emits_changed_signal = "const"))] fn can_control(&self) -> bool { - false // TODO + true } } diff --git a/src/playbin.vala b/src/playbin.vala index 11630a3..a247efe 100644 --- a/src/playbin.vala +++ b/src/playbin.vala @@ -77,7 +77,7 @@ public class Audrey.Playbin : GLib.Object { if (ret >= 0) { _volume = value; } else { - warning ("failed to set volume: %s", ret.to_string ()); + warning ("failed to set volume to %d: %s", value, ret.to_string ()); } } }