status: follow lyrics of proper song if consume mode is on

http://bugs.musicpd.org/view.php?id=4082
This commit is contained in:
Andrzej Rybczak
2014-10-18 14:01:43 +02:00
parent d9cf50b946
commit 683efb5a43
6 changed files with 53 additions and 28 deletions

View File

@@ -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)
{

View File

@@ -48,6 +48,7 @@ struct Lyrics: Screen<NC::Scrollpad>, 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<NC::Scrollpad>, Tabbable
static void DownloadInBackground(const MPD::Song &s);
# endif // HAVE_CURL_CURL_H
bool ReloadNP;
bool Reload;
protected:
virtual bool isLockable() OVERRIDE { return false; }

View File

@@ -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();

View File

@@ -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);

View File

@@ -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);
}

View File

@@ -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();