notify mpris on playback status change

This commit is contained in:
Erica Z 2024-11-19 20:58:23 +01:00
parent 4c90d0f10e
commit eab11095c8

View file

@ -407,6 +407,26 @@ mod imp {
self.setup.present(Some(self.obj().as_ref())); self.setup.present(Some(self.obj().as_ref()));
} }
fn mpris_player(&self) -> Option<InterfaceRef<mpris::Player>> {
let borrowed = self.mpris_player.borrow();
borrowed.as_ref().cloned()
}
fn mpris_player_playback_status_changed(&self) {
if let Some(iface_ref) = self.mpris_player() {
glib::spawn_future_local(async move {
let iface = iface_ref.get().await;
match iface.playback_status_changed(iface_ref.signal_emitter()).await {
Ok(()) => {}
Err(err) => event!(
Level::ERROR,
"could not notify new playback status through mpris interface: {err}"
),
}
});
}
}
fn volume(&self) -> i64 { fn volume(&self) -> i64 {
self.mpv.get_property("volume").unwrap() self.mpv.get_property("volume").unwrap()
} }
@ -414,21 +434,18 @@ mod imp {
fn set_volume(&self, volume: i64) { fn set_volume(&self, volume: i64) {
self.mpv.set_property("volume", volume).unwrap(); self.mpv.set_property("volume", volume).unwrap();
let iface_borrowed = self.mpris_player.borrow(); if let Some(iface_ref) = self.mpris_player() {
let iface_ref = match iface_borrowed.as_ref() { glib::spawn_future_local(async move {
None => return, // zbus not set up yet let iface = iface_ref.get().await;
Some(iface_ref) => iface_ref.clone(), match iface.volume_changed(iface_ref.signal_emitter()).await {
}; Ok(()) => {}
glib::spawn_future_local(async move { Err(err) => event!(
let iface = iface_ref.get().await; Level::ERROR,
match iface.volume_changed(iface_ref.signal_emitter()).await { "could not notify new volume through mpris interface: {err}"
Ok(()) => {} ),
Err(err) => event!( }
Level::ERROR, });
"could not notify new volume through mpris interface: {err}" }
),
}
});
} }
fn mute(&self) -> bool { fn mute(&self) -> bool {
@ -445,6 +462,10 @@ mod imp {
fn set_pause(&self, pause: bool) { fn set_pause(&self, pause: bool) {
self.mpv.set_property("pause", pause).unwrap(); self.mpv.set_property("pause", pause).unwrap();
if !self.idle_active() {
self.mpris_player_playback_status_changed();
}
} }
fn time_pos(&self) -> f64 { fn time_pos(&self) -> f64 {
@ -641,6 +662,8 @@ mod imp {
self.obj().set_playing_cover_art(None::<gdk::Texture>); self.obj().set_playing_cover_art(None::<gdk::Texture>);
self.obj().set_background(None::<gdk::Texture>); self.obj().set_background(None::<gdk::Texture>);
self.mpris_player_playback_status_changed();
} }
fn on_start_file(&self) { fn on_start_file(&self) {
@ -668,6 +691,8 @@ mod imp {
event!(target: "audrey::playback", Level::DEBUG, "duration is now {duration} (from subsonic)"); event!(target: "audrey::playback", Level::DEBUG, "duration is now {duration} (from subsonic)");
self.obj().notify("duration"); self.obj().notify("duration");
self.mpris_player_playback_status_changed();
let window = self.obj().clone(); let window = self.obj().clone();
let song_id = window.song().unwrap().id(); let song_id = window.song().unwrap().id();
if let Some(handle) = self if let Some(handle) = self