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;
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;
@@ -100,32 +94,29 @@ void Lyrics::Update()
void Lyrics::SwitchTo()
{
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();
}
else
{
if (!isInitialized)
Init();
if (hasToBeResized)
Resize();
itsScrollBegin = 0;
myOldScreen = myScreen;
myScreen = this;
Update();
if (const MPD::Song *s = myOldScreen->CurrentSong())
{
itsSong = *s;
Load();
}
Global::RedrawHeader = 1;
itsSong = *s;
Load();
}
Global::RedrawHeader = 1;
}
std::basic_string<my_char_t> Lyrics::Title()
@@ -142,35 +133,38 @@ void Lyrics::SpacePressed()
}
#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);
std::string artist = Curl::escape(locale_to_utf_cpy(screen->itsSong.GetArtist()));
std::string title = Curl::escape(locale_to_utf_cpy(screen->itsSong.GetTitle()));
return static_cast<Lyrics *>(this_ptr)->Download();
}
void *Lyrics::Download()
{
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;
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);
if (result.first == false)
*screen->w << clRed << result.second << clEnd << "\n";
*w << clRed << result.second << clEnd << "\n";
else
break;
}
if (result.first == true)
{
screen->Save(result.second);
Save(result.second);
utf_to_locale(result.second);
screen->w->Clear();
*screen->w << result.second;
w->Clear();
*w << result.second;
}
else
*screen->w << "\nLyrics weren't found.";
*w << "\nLyrics weren't found.";
ReadyToTake = 1;
pthread_exit(0);
@@ -221,7 +215,7 @@ void Lyrics::Load()
{
# ifdef HAVE_CURL_CURL_H
Downloader = new pthread_t;
pthread_create(Downloader, 0, Get, this);
pthread_create(Downloader, 0, DownloadWrapper, this);
# 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->Flush();

View File

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

View File

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