Skip to content

Commit

Permalink
Fix mpris Album Info (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamdb authored Oct 31, 2023
1 parent 4d5b525 commit 5583718
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 28 additions & 29 deletions hifirs/src/mpris.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ pub async fn init(controls: Controls) -> Connection {
let mpris_player = MprisPlayer {
controls: controls.clone(),
status: GstState::Null,
current_track: None,
total_tracks: 0,
position: ClockTime::default(),
position_ts: chrono::offset::Local::now(),
Expand All @@ -28,6 +27,7 @@ pub async fn init(controls: Controls) -> Connection {
can_stop: true,
can_next: true,
can_previous: true,
track_list: TrackListValue::default(),
};
let mpris_tracklist = MprisTrackList {
controls,
Expand Down Expand Up @@ -148,41 +148,37 @@ pub async fn receive_notifications(conn: Connection) {
}
}
Notification::CurrentTrackList { list } => {
if let Some(current) = list.current_track() {
let player_ref = object_server
.interface::<_, MprisPlayer>("/org/mpris/MediaPlayer2")
.await
.expect("failed to get object server");

let mut player_iface = player_ref.get_mut().await;
let player_ref = object_server
.interface::<_, MprisPlayer>("/org/mpris/MediaPlayer2")
.await
.expect("failed to get object server");

player_iface.can_previous = current.position != 0;
let list_ref = object_server
.interface::<_, MprisTrackList>("/org/mpris/MediaPlayer2")
.await
.expect("failed to get object server");

player_iface.can_next = !(player_iface.total_tracks != 0
&& current.position == player_iface.total_tracks - 1);
let mut player_iface = player_ref.get_mut().await;
player_iface.track_list = list.clone();

if let Some(album) = &current.album {
player_iface.total_tracks = album.total_tracks;
}
let mut list_iface = list_ref.get_mut().await;
list_iface.track_list = list.clone();

player_iface.current_track = Some(current.clone());
if let Some(album) = list.get_album() {
player_iface.total_tracks = album.total_tracks;
}

player_iface
.metadata_changed(player_ref.signal_context())
.await
.expect("failed to signal metadata change");
if let Some(current) = list.current_track() {
player_iface.can_previous = current.position != 0;

let list_ref = object_server
.interface::<_, MprisTrackList>("/org/mpris/MediaPlayer2")
.await
.expect("failed to get object server");
player_iface.can_next = !(player_iface.total_tracks != 0
&& current.position == player_iface.total_tracks - 1);

let tracks = list
.cursive_list()
.iter()
.map(|t| t.0.clone())
.collect::<Vec<String>>();
let mut list_iface = list_ref.get_mut().await;

MprisTrackList::track_list_replaced(
list_ref.signal_context(),
Expand All @@ -191,9 +187,12 @@ pub async fn receive_notifications(conn: Connection) {
)
.await
.expect("failed to send track list replaced signal");

list_iface.track_list = list;
}

player_iface
.metadata_changed(player_ref.signal_context())
.await
.expect("failed to signal metadata change");
}
Notification::Error { error: _ } => {}
Notification::AudioQuality {
Expand Down Expand Up @@ -244,12 +243,12 @@ impl Mpris {

#[derive(Debug)]
pub struct MprisPlayer {
track_list: TrackListValue,
controls: Controls,
status: GstState,
position: ClockTime,
position_ts: DateTime<Local>,
total_tracks: u32,
current_track: Option<Track>,
can_play: bool,
can_pause: bool,
can_stop: bool,
Expand Down Expand Up @@ -305,8 +304,8 @@ impl MprisPlayer {
#[dbus_interface(property, name = "Metadata")]
async fn metadata(&self) -> HashMap<&'static str, zvariant::Value> {
debug!("signal metadata refresh");
if let Some(current_track) = &self.current_track {
track_to_meta(current_track, current_track.album.as_ref())
if let Some(current_track) = &self.track_list.current_track() {
track_to_meta(current_track, self.track_list.get_album())
} else {
HashMap::default()
}
Expand Down
10 changes: 9 additions & 1 deletion hifirs/src/player/queue/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,15 @@ impl TrackListValue {

#[instrument(skip(self))]
pub fn get_album(&self) -> Option<&Album> {
self.album.as_ref()
if let Some(c) = self.current_track() {
if let Some(album) = &c.album {
Some(album)
} else {
self.album.as_ref()
}
} else {
self.album.as_ref()
}
}

#[instrument(skip(self))]
Expand Down

0 comments on commit 5583718

Please sign in to comment.