diff --git a/src/lyrics.cpp b/src/lyrics.cpp index a343dbdc..f4514a03 100644 --- a/src/lyrics.cpp +++ b/src/lyrics.cpp @@ -52,7 +52,7 @@ Lyrics *myLyrics; Lyrics::Lyrics() : Screen(NC::Scrollpad(0, MainStartY, COLS, MainHeight, "", Config.main_color, NC::Border::None)) -, ReloadNP(0), +, Reload(0), #ifdef HAVE_CURL_CURL_H isReadyToTake(0), isDownloadInProgress(0), #endif // HAVE_CURL_CURL_H @@ -80,17 +80,12 @@ void Lyrics::update() w.refresh(); } # endif // HAVE_CURL_CURL_H - if (ReloadNP) + if (Reload) { - const MPD::Song s = myPlaylist->nowPlayingSong(); - if (!s.empty() && !s.getArtist().empty() && !s.getTitle().empty()) - { - drawHeader(); - itsScrollBegin = 0; - itsSong = s; - Load(); - } - ReloadNP = 0; + drawHeader(); + itsScrollBegin = 0; + Load(); + Reload = 0; } } @@ -111,15 +106,11 @@ void Lyrics::switchTo() } # endif // HAVE_CURL_CURL_H - const MPD::Song *s = currentSong(myScreen); - if (!s) - return; - - if (!s->getArtist().empty() && !s->getTitle().empty()) + auto s = currentSong(myScreen); + if (SetSong(*s)) { SwitchTo::execute(this); itsScrollBegin = 0; - itsSong = *s; Load(); drawHeader(); } @@ -330,7 +321,7 @@ void Lyrics::Load() first = 0; } w.flush(); - if (ReloadNP) + if (Reload) w.refresh(); } else @@ -372,6 +363,17 @@ void Lyrics::Edit() res = system(("nohup " + Config.external_editor + " \"" + itsFilename + "\" > /dev/null 2>&1 &").c_str()); } +bool Lyrics::SetSong(const MPD::Song &s) +{ + if (!s.getArtist().empty() && !s.getTitle().empty()) + { + itsSong = s; + return true; + } + else + return false; +} + #ifdef HAVE_CURL_CURL_H void Lyrics::Save(const std::string &filename, const std::string &lyrics) { diff --git a/src/lyrics.h b/src/lyrics.h index 02d8f84f..bc5d5c5a 100644 --- a/src/lyrics.h +++ b/src/lyrics.h @@ -48,6 +48,7 @@ struct Lyrics: Screen, Tabbable virtual bool isMergable() OVERRIDE { return true; } // private members + bool SetSong(const MPD::Song &s); void Edit(); # ifdef HAVE_CURL_CURL_H @@ -57,7 +58,7 @@ struct Lyrics: Screen, Tabbable static void DownloadInBackground(const MPD::Song &s); # endif // HAVE_CURL_CURL_H - bool ReloadNP; + bool Reload; protected: virtual bool isLockable() OVERRIDE { return false; } diff --git a/src/mpdpp.cpp b/src/mpdpp.cpp index dfaa765a..4bcd5aac 100644 --- a/src/mpdpp.cpp +++ b/src/mpdpp.cpp @@ -308,6 +308,16 @@ void Connection::GetPlaylistChanges(unsigned version, SongConsumer f) checkErrors(); } +Song Connection::GetCurrentSong() +{ + prechecksNoCommandsList(); + mpd_send_current_song(m_connection); + mpd_song *s = mpd_recv_song(m_connection); + mpd_response_finish(m_connection); + checkErrors(); + return Song(s); +} + Song Connection::GetSong(const std::string &path) { prechecksNoCommandsList(); diff --git a/src/mpdpp.h b/src/mpdpp.h index c970d4ce..d067a191 100644 --- a/src/mpdpp.h +++ b/src/mpdpp.h @@ -195,7 +195,7 @@ public: void GetPlaylistChanges(unsigned, SongConsumer f); - Song GetCurrentlyPlayingSong(); + Song GetCurrentSong(); Song GetSong(const std::string &); void GetPlaylistContent(const std::string &name, SongConsumer f); void GetPlaylistContentNoInfo(const std::string &name, SongConsumer f); diff --git a/src/status.cpp b/src/status.cpp index 5ce4001b..d6219810 100644 --- a/src/status.cpp +++ b/src/status.cpp @@ -246,7 +246,7 @@ void Status::update(int event) Changes::playerState(); if (m_current_song_id != st.currentSongID()) { - Changes::songID(); + Changes::songID(st.currentSongID()); m_current_song_id = st.currentSongID(); } } @@ -507,7 +507,7 @@ void Status::Changes::playerState() elapsedTime(false); } -void Status::Changes::songID() +void Status::Changes::songID(int song_id) { // update information about current song myPlaylist->reloadRemaining(); @@ -516,22 +516,34 @@ void Status::Changes::songID() second_line_scroll_begin = 0; if (m_player_state != MPD::psStop) { + auto &pl = myPlaylist->main(); + + // try to find the song with new id in the playlist + auto it = std::find_if(pl.beginV(), pl.endV(), [song_id](const MPD::Song &s) { + return s.getID() == unsigned(song_id); + }); + // if it's not there (playlist may be outdated), fetch it + const auto &s = it != pl.endV() ? *it : Mpd.GetCurrentSong(); + GNUC_UNUSED int res; if (!Config.execute_on_song_change.empty()) res = system(Config.execute_on_song_change.c_str()); # ifdef HAVE_CURL_CURL_H if (Config.fetch_lyrics_in_background) - Lyrics::DownloadInBackground(myPlaylist->nowPlayingSong()); + Lyrics::DownloadInBackground(s); # endif // HAVE_CURL_CURL_H - drawTitle(myPlaylist->nowPlayingSong()); + drawTitle(s); - if (Config.autocenter_mode && !myPlaylist->main().isFiltered()) - myPlaylist->main().highlight(Status::State::currentSongPosition()); + if (Config.autocenter_mode && !pl.isFiltered()) + pl.highlight(Status::State::currentSongPosition()); if (Config.now_playing_lyrics && isVisible(myLyrics) && myLyrics->previousScreen() == myPlaylist) - myLyrics->ReloadNP = 1; + { + if (myLyrics->SetSong(s)) + myLyrics->Reload = 1; + } } elapsedTime(false); } diff --git a/src/status.h b/src/status.h index d2d739a8..b771b277 100644 --- a/src/status.h +++ b/src/status.h @@ -59,7 +59,7 @@ void playlist(unsigned previous_version); void storedPlaylists(); void database(); void playerState(); -void songID(); +void songID(int song_id); void elapsedTime(bool update_elapsed); void flags(); void mixer();