lyrics: pass wrapper around member function to new thread

+ some more code refactoring
This commit is contained in:
Andrzej Rybczak
2010-08-10 01:32:34 +02:00
parent 7cd6508496
commit 3bb3607399
3 changed files with 48 additions and 51 deletions

View File

@@ -50,12 +50,6 @@ using Global::myScreen;
using Global::myOldScreen; using Global::myOldScreen;
const std::string Lyrics::Folder = home_path + LYRICS_FOLDER; const std::string Lyrics::Folder = home_path + LYRICS_FOLDER;
bool Lyrics::ReloadNP = 0;
#ifdef HAVE_CURL_CURL_H
bool Lyrics::ReadyToTake = 0;
pthread_t *Lyrics::Downloader = 0;
#endif // HAVE_CURL_CURL_H
Lyrics *myLyrics = new Lyrics; Lyrics *myLyrics = new Lyrics;
@@ -100,32 +94,29 @@ void Lyrics::Update()
void Lyrics::SwitchTo() void Lyrics::SwitchTo()
{ {
if (myScreen == this) if (myScreen == this)
return myOldScreen->SwitchTo();
if (!isInitialized)
Init();
if (hasToBeResized)
Resize();
itsScrollBegin = 0;
myOldScreen = myScreen;
myScreen = this;
// for taking lyrics if they were downloaded
Update();
if (const MPD::Song *s = myOldScreen->CurrentSong())
{ {
myOldScreen->SwitchTo(); itsSong = *s;
Load();
} }
else
{
if (!isInitialized)
Init();
if (hasToBeResized) Global::RedrawHeader = 1;
Resize();
itsScrollBegin = 0;
myOldScreen = myScreen;
myScreen = this;
Update();
if (const MPD::Song *s = myOldScreen->CurrentSong())
{
itsSong = *s;
Load();
}
Global::RedrawHeader = 1;
}
} }
std::basic_string<my_char_t> Lyrics::Title() std::basic_string<my_char_t> Lyrics::Title()
@@ -142,35 +133,38 @@ void Lyrics::SpacePressed()
} }
#ifdef HAVE_CURL_CURL_H #ifdef HAVE_CURL_CURL_H
void *Lyrics::Get(void *screen_void_ptr) void *Lyrics::DownloadWrapper(void *this_ptr)
{ {
Lyrics *screen = static_cast<Lyrics *>(screen_void_ptr); return static_cast<Lyrics *>(this_ptr)->Download();
}
std::string artist = Curl::escape(locale_to_utf_cpy(screen->itsSong.GetArtist())); void *Lyrics::Download()
std::string title = Curl::escape(locale_to_utf_cpy(screen->itsSong.GetTitle())); {
std::string artist = Curl::escape(locale_to_utf_cpy(itsSong.GetArtist()));
std::string title = Curl::escape(locale_to_utf_cpy(itsSong.GetTitle()));
LyricsFetcher::Result result; LyricsFetcher::Result result;
for (LyricsFetcher **plugin = lyricsPlugins; *plugin != 0; ++plugin) for (LyricsFetcher **plugin = lyricsPlugins; *plugin != 0; ++plugin)
{ {
*screen->w << "Fetching lyrics from " << fmtBold << (*plugin)->name() << fmtBoldEnd << "... "; *w << "Fetching lyrics from " << fmtBold << (*plugin)->name() << fmtBoldEnd << "... ";
result = (*plugin)->fetch(artist, title); result = (*plugin)->fetch(artist, title);
if (result.first == false) if (result.first == false)
*screen->w << clRed << result.second << clEnd << "\n"; *w << clRed << result.second << clEnd << "\n";
else else
break; break;
} }
if (result.first == true) if (result.first == true)
{ {
screen->Save(result.second); Save(result.second);
utf_to_locale(result.second); utf_to_locale(result.second);
screen->w->Clear(); w->Clear();
*screen->w << result.second; *w << result.second;
} }
else else
*screen->w << "\nLyrics weren't found."; *w << "\nLyrics weren't found.";
ReadyToTake = 1; ReadyToTake = 1;
pthread_exit(0); pthread_exit(0);
@@ -221,7 +215,7 @@ void Lyrics::Load()
{ {
# ifdef HAVE_CURL_CURL_H # ifdef HAVE_CURL_CURL_H
Downloader = new pthread_t; Downloader = new pthread_t;
pthread_create(Downloader, 0, Get, this); pthread_create(Downloader, 0, DownloadWrapper, this);
# else # else
*w << "Local lyrics not found. As ncmpcpp has been compiled without curl support, you can put appropriate lyrics into " << Folder << " directory (file syntax is \"$ARTIST - $TITLE.txt\") or recompile ncmpcpp with curl support."; *w << "Local lyrics not found. As ncmpcpp has been compiled without curl support, you can put appropriate lyrics into " << Folder << " directory (file syntax is \"$ARTIST - $TITLE.txt\") or recompile ncmpcpp with curl support.";
w->Flush(); w->Flush();

View File

@@ -28,7 +28,12 @@
class Lyrics : public Screen<Scrollpad> class Lyrics : public Screen<Scrollpad>
{ {
public: public:
Lyrics() : itsScrollBegin(0) { } Lyrics() : ReloadNP(0),
# ifdef HAVE_CURL_CURL_H
ReadyToTake(0), Downloader(0),
# endif // HAVE_CURL_CURL_H
itsScrollBegin(0) { }
~Lyrics() { } ~Lyrics() { }
virtual void Resize(); virtual void Resize();
@@ -49,7 +54,7 @@ class Lyrics : public Screen<Scrollpad>
void Save(const std::string &lyrics); void Save(const std::string &lyrics);
void Refetch(); void Refetch();
static bool ReloadNP; bool ReloadNP;
protected: protected:
virtual void Init(); virtual void Init();
@@ -62,14 +67,12 @@ class Lyrics : public Screen<Scrollpad>
static const std::string Folder; static const std::string Folder;
# ifdef HAVE_CURL_CURL_H # ifdef HAVE_CURL_CURL_H
static void *Get(void *); void *Download();
static void *DownloadWrapper(void *);
void Take(); void Take();
bool ReadyToTake;
static bool ReadyToTake; pthread_t *Downloader;
static pthread_t *Downloader;
# endif // HAVE_CURL_CURL_H # endif // HAVE_CURL_CURL_H
size_t itsScrollBegin; size_t itsScrollBegin;

View File

@@ -412,7 +412,7 @@ void NcmpcppStatusChanged(MPD::Connection *, MPD::StatusChanges changed, void *)
myPlaylist->Items->Highlight(myPlaylist->NowPlaying); myPlaylist->Items->Highlight(myPlaylist->NowPlaying);
if (Config.now_playing_lyrics && myScreen == myLyrics && Global::myOldScreen == myPlaylist) if (Config.now_playing_lyrics && myScreen == myLyrics && Global::myOldScreen == myPlaylist)
Lyrics::ReloadNP = 1; myLyrics->ReloadNP = 1;
} }
Playlist::ReloadRemaining = 1; Playlist::ReloadRemaining = 1;
playing_song_scroll_begin = 0; playing_song_scroll_begin = 0;