can now set volume again thru mpris

This commit is contained in:
Erica Z 2024-11-01 15:17:37 +01:00
parent 6272c2ce07
commit d873d03973
2 changed files with 39 additions and 16 deletions

View file

@ -12,24 +12,24 @@ struct MetadataMap {
} }
impl MetadataMap { impl MetadataMap {
fn as_hash_map(&self) -> HashMap<&'static str, Value> { fn from_playbin(_playbin: &crate::Playbin) -> Self {
HashMap::from([("mpris:trackid", Value::new(self.trackid.as_ref()))]) // TODO
}
}
impl Default for MetadataMap {
fn default() -> Self {
Self { Self {
trackid: "/org/mpris/MediaPlayer2/TrackList/NoTrack" trackid: "/org/mpris/MediaPlayer2/TrackList/NoTrack"
.try_into() .try_into()
.unwrap(), .unwrap(),
} }
} }
fn as_hash_map(&self) -> HashMap<&'static str, Value> {
HashMap::from([("mpris:trackid", Value::new(self.trackid.as_ref()))])
}
} }
pub struct Player { pub struct Player {
playbin: SendWeakRef<crate::Playbin>, playbin: SendWeakRef<crate::Playbin>,
metadata: MetadataMap, metadata: MetadataMap,
volume: f64,
} }
impl Player { impl Player {
@ -41,12 +41,13 @@ impl Player {
let player = Self { let player = Self {
playbin: playbin.downgrade().into(), 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?; object_server.at("/org/mpris/MediaPlayer2", player).await?;
let player = object_server let player_ref = object_server
.interface::<_, Self>("/org/mpris/MediaPlayer2") .interface::<_, Self>("/org/mpris/MediaPlayer2")
.await?; .await?;
@ -54,15 +55,35 @@ impl Player {
"new-track", "new-track",
false, false,
glib::closure_local!( glib::closure_local!(
#[strong] //#[strong]
player, //player,
move |_playbin: &crate::Playbin| { move |_playbin: &crate::Playbin| {
let _player = player.get_mut();
// TODO // 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(()) Ok(())
} }
} }
@ -158,8 +179,10 @@ impl Player {
} }
#[zbus(property)] #[zbus(property)]
fn set_volume(&self, _volume: f64) { fn set_volume(&mut self, volume: f64) {
todo!() 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"))] #[zbus(property(emits_changed_signal = "false"))]
@ -204,6 +227,6 @@ impl Player {
#[zbus(property(emits_changed_signal = "const"))] #[zbus(property(emits_changed_signal = "const"))]
fn can_control(&self) -> bool { fn can_control(&self) -> bool {
false // TODO true
} }
} }

View file

@ -77,7 +77,7 @@ public class Audrey.Playbin : GLib.Object {
if (ret >= 0) { if (ret >= 0) {
_volume = value; _volume = value;
} else { } else {
warning ("failed to set volume: %s", ret.to_string ()); warning ("failed to set volume to %d: %s", value, ret.to_string ());
} }
} }
} }