use idle notifications instead of polling mpd all the time
This commit is contained in:
561
src/mpdpp.cpp
561
src/mpdpp.cpp
File diff suppressed because it is too large
Load Diff
114
src/mpdpp.h
114
src/mpdpp.h
@@ -21,6 +21,7 @@
|
||||
#ifndef _MPDPP_H
|
||||
#define _MPDPP_H
|
||||
|
||||
#include <poll.h>
|
||||
#include <vector>
|
||||
|
||||
#include <mpd/client.h>
|
||||
@@ -92,12 +93,13 @@ namespace MPD
|
||||
int GetPort() { return itsPort; }
|
||||
|
||||
float Version() const;
|
||||
bool SupportsIdle() const { return supportsIdle; }
|
||||
|
||||
void SetHostname(const std::string &);
|
||||
void SetPort(int port) { itsPort = port; }
|
||||
void SetTimeout(int timeout) { itsTimeout = timeout; }
|
||||
void SetPassword(const std::string &password) { itsPassword = password; }
|
||||
bool SendPassword() const;
|
||||
bool SendPassword();
|
||||
|
||||
void SetStatusUpdater(StatusUpdater, void *);
|
||||
void SetErrorHandler(ErrorHandler, void *);
|
||||
@@ -105,19 +107,19 @@ namespace MPD
|
||||
void UpdateStats();
|
||||
bool UpdateDirectory(const std::string &);
|
||||
|
||||
void Play() const;
|
||||
void Play(int) const;
|
||||
void PlayID(int) const;
|
||||
void Pause(bool) const;
|
||||
void Toggle() const;
|
||||
void Stop() const;
|
||||
void Next() const;
|
||||
void Prev() const;
|
||||
void Move(unsigned, unsigned) const;
|
||||
void Swap(unsigned, unsigned) const;
|
||||
void Seek(unsigned) const;
|
||||
void Shuffle() const;
|
||||
void ClearPlaylist() const;
|
||||
void Play();
|
||||
void Play(int);
|
||||
void PlayID(int);
|
||||
void Pause(bool);
|
||||
void Toggle();
|
||||
void Stop();
|
||||
void Next();
|
||||
void Prev();
|
||||
void Move(unsigned, unsigned);
|
||||
void Swap(unsigned, unsigned);
|
||||
void Seek(unsigned);
|
||||
void Shuffle();
|
||||
void ClearPlaylist();
|
||||
|
||||
bool isPlaying() const { return GetState() > psStop; }
|
||||
|
||||
@@ -131,7 +133,7 @@ namespace MPD
|
||||
unsigned GetCrossfade() const { return itsCurrentStatus ? mpd_status_get_crossfade(itsCurrentStatus) : 0; }
|
||||
unsigned GetPlaylistID() const { return itsCurrentStatus ? mpd_status_get_queue_version(itsCurrentStatus) : 0; }
|
||||
unsigned GetOldPlaylistID() const { return itsOldStatus ? mpd_status_get_queue_version(itsOldStatus) : 0; }
|
||||
unsigned GetElapsedTime() const { return itsCurrentStatus ? mpd_status_get_elapsed_time(itsCurrentStatus) : 0; }
|
||||
unsigned GetElapsedTime() const { return itsCurrentStatus ? itsElapsed : 0; }
|
||||
int GetTotalTime() const { return itsCurrentStatus ? mpd_status_get_total_time(itsCurrentStatus) : 0; }
|
||||
unsigned GetBitrate() const { return itsCurrentStatus ? mpd_status_get_kbit_rate(itsCurrentStatus) : 0; }
|
||||
|
||||
@@ -145,66 +147,69 @@ namespace MPD
|
||||
|
||||
size_t GetMaxPlaylistLength() const { return itsMaxPlaylistLength; }
|
||||
size_t GetPlaylistLength() const { return itsCurrentStatus ? mpd_status_get_queue_length(itsCurrentStatus) : 0; }
|
||||
void GetPlaylistChanges(unsigned, SongList &) const;
|
||||
void GetPlaylistChanges(unsigned, SongList &);
|
||||
|
||||
const std::string & GetErrorMessage() const { return itsErrorMessage; }
|
||||
int GetErrorCode() const { return itsErrorCode; }
|
||||
|
||||
Song GetCurrentSong() const;
|
||||
Song GetCurrentSong();
|
||||
int GetCurrentSongPos() const;
|
||||
Song GetSong(const std::string &) const;
|
||||
void GetPlaylistContent(const std::string &, SongList &) const;
|
||||
Song GetSong(const std::string &);
|
||||
void GetPlaylistContent(const std::string &, SongList &);
|
||||
|
||||
void SetRepeat(bool) const;
|
||||
void SetRandom(bool) const;
|
||||
void SetSingle(bool) const;
|
||||
void SetConsume(bool) const;
|
||||
void SetCrossfade(unsigned) const;
|
||||
void SetRepeat(bool);
|
||||
void SetRandom(bool);
|
||||
void SetSingle(bool);
|
||||
void SetConsume(bool);
|
||||
void SetCrossfade(unsigned);
|
||||
void SetVolume(unsigned);
|
||||
|
||||
std::string GetReplayGainMode() const;
|
||||
void SetReplayGainMode(ReplayGainMode) const;
|
||||
std::string GetReplayGainMode();
|
||||
void SetReplayGainMode(ReplayGainMode);
|
||||
|
||||
int AddSong(const std::string &, int = -1); // returns id of added song
|
||||
int AddSong(const Song &, int = -1); // returns id of added song
|
||||
bool AddRandomSongs(size_t);
|
||||
void Add(const std::string &path) const;
|
||||
void Delete(unsigned) const;
|
||||
void DeleteID(unsigned) const;
|
||||
void Delete(const std::string &, unsigned) const;
|
||||
void Add(const std::string &path);
|
||||
void Delete(unsigned);
|
||||
void DeleteID(unsigned);
|
||||
void Delete(const std::string &, unsigned);
|
||||
void StartCommandsList();
|
||||
bool CommitCommandsList();
|
||||
|
||||
bool DeletePlaylist(const std::string &) const;
|
||||
bool SavePlaylist(const std::string &) const;
|
||||
void ClearPlaylist(const std::string &) const;
|
||||
void AddToPlaylist(const std::string &, const Song &) const;
|
||||
void AddToPlaylist(const std::string &, const std::string &) const;
|
||||
void Move(const std::string &, int, int) const;
|
||||
bool Rename(const std::string &, const std::string &) const;
|
||||
bool DeletePlaylist(const std::string &);
|
||||
bool SavePlaylist(const std::string &);
|
||||
void ClearPlaylist(const std::string &);
|
||||
void AddToPlaylist(const std::string &, const Song &);
|
||||
void AddToPlaylist(const std::string &, const std::string &);
|
||||
void Move(const std::string &, int, int);
|
||||
bool Rename(const std::string &, const std::string &);
|
||||
|
||||
void StartSearch(bool) const;
|
||||
void StartSearch(bool);
|
||||
void StartFieldSearch(mpd_tag_type);
|
||||
void AddSearch(mpd_tag_type, const std::string &) const;
|
||||
void CommitSearch(SongList &) const;
|
||||
void CommitSearch(TagList &) const;
|
||||
void AddSearch(mpd_tag_type, const std::string &);
|
||||
void CommitSearch(SongList &);
|
||||
void CommitSearch(TagList &);
|
||||
|
||||
void GetPlaylists(TagList &) const;
|
||||
void GetList(TagList &, mpd_tag_type) const;
|
||||
void GetAlbums(const std::string &, TagList &) const;
|
||||
void GetDirectory(const std::string &, ItemList &) const;
|
||||
void GetDirectoryRecursive(const std::string &, SongList &) const;
|
||||
void GetSongs(const std::string &, SongList &) const;
|
||||
void GetDirectories(const std::string &, TagList &) const;
|
||||
void GetPlaylists(TagList &);
|
||||
void GetList(TagList &, mpd_tag_type);
|
||||
void GetAlbums(const std::string &, TagList &);
|
||||
void GetDirectory(const std::string &, ItemList &);
|
||||
void GetDirectoryRecursive(const std::string &, SongList &);
|
||||
void GetSongs(const std::string &, SongList &);
|
||||
void GetDirectories(const std::string &, TagList &);
|
||||
|
||||
void GetOutputs(OutputList &) const;
|
||||
void GetOutputs(OutputList &);
|
||||
bool EnableOutput(int);
|
||||
bool DisableOutput(int);
|
||||
|
||||
void GetURLHandlers(TagList &v) const;
|
||||
void GetTagTypes(TagList &v) const;
|
||||
void GetURLHandlers(TagList &v);
|
||||
void GetTagTypes(TagList &v);
|
||||
|
||||
private:
|
||||
void GoIdle();
|
||||
mpd_idle GoBusy();
|
||||
|
||||
int CheckForErrors();
|
||||
|
||||
mpd_connection *itsConnection;
|
||||
@@ -214,6 +219,10 @@ namespace MPD
|
||||
int itsErrorCode;
|
||||
size_t itsMaxPlaylistLength;
|
||||
|
||||
pollfd itsPoll;
|
||||
bool isIdle;
|
||||
bool supportsIdle;
|
||||
|
||||
std::string itsHost;
|
||||
int itsPort;
|
||||
int itsTimeout;
|
||||
@@ -223,6 +232,9 @@ namespace MPD
|
||||
mpd_status *itsOldStatus;
|
||||
mpd_stats *itsStats;
|
||||
|
||||
unsigned itsElapsed;
|
||||
time_t itsElapsedTimer[2];
|
||||
|
||||
StatusChanges itsChanges;
|
||||
|
||||
StatusUpdater itsUpdater;
|
||||
|
||||
@@ -37,7 +37,7 @@ using namespace NCurses;
|
||||
|
||||
typedef std::pair<std::string, std::string> string_pair;
|
||||
|
||||
const int ncmpcpp_window_timeout = 250;
|
||||
const int ncmpcpp_window_timeout = 100;
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@@ -53,8 +53,6 @@ namespace
|
||||
bool block_statusbar_update = 0;
|
||||
bool block_progressbar_update = 0;
|
||||
bool allow_statusbar_unlock = 1;
|
||||
|
||||
int local_elapsed;
|
||||
}
|
||||
|
||||
#ifndef USE_PDCURSES
|
||||
@@ -122,7 +120,7 @@ void TraceMpdStatus()
|
||||
static timeval past, now;
|
||||
|
||||
gettimeofday(&now, 0);
|
||||
if ((Mpd.Connected() && now.tv_sec > past.tv_sec) || UpdateStatusImmediately)
|
||||
if (Mpd.Connected() && (Mpd.SupportsIdle() || now.tv_sec > past.tv_sec || UpdateStatusImmediately))
|
||||
{
|
||||
Mpd.UpdateStatus();
|
||||
BlockItemListUpdate = 0;
|
||||
@@ -154,7 +152,7 @@ void TraceMpdStatus()
|
||||
if (Mpd.GetState() != psPlay && !block_statusbar_update && !block_progressbar_update)
|
||||
{
|
||||
if (Config.new_design)
|
||||
DrawProgressbar(local_elapsed, Mpd.GetTotalTime());
|
||||
DrawProgressbar(Mpd.GetElapsedTime(), Mpd.GetTotalTime());
|
||||
else
|
||||
Statusbar() << wclrtoeol;
|
||||
wFooter->Refresh();
|
||||
@@ -167,12 +165,21 @@ void NcmpcppErrorCallback(Connection *, int errorid, const char *msg, void *)
|
||||
{
|
||||
if (errorid == MPD_SERVER_ERROR_PERMISSION)
|
||||
{
|
||||
wFooter->SetGetStringHelper(0);
|
||||
Statusbar() << "Password: ";
|
||||
Mpd.SetPassword(wFooter->GetString(-1, 0, 1));
|
||||
Mpd.SendPassword();
|
||||
Mpd.UpdateStatus();
|
||||
wFooter->SetGetStringHelper(StatusbarGetStringHelper);
|
||||
Statusbar() << msg << ", enter password ? [" << fmtBold << 'y' << fmtBoldEnd << "/" << fmtBold << 'n' << fmtBoldEnd << "]";
|
||||
wFooter->Refresh();
|
||||
int answer = 0;
|
||||
do
|
||||
wFooter->ReadKey(answer);
|
||||
while (answer != 'y' && answer != 'n');
|
||||
if (answer == 'y')
|
||||
{
|
||||
wFooter->SetGetStringHelper(0);
|
||||
Statusbar() << "Password: ";
|
||||
Mpd.SetPassword(wFooter->GetString(-1, 0, 1));
|
||||
if (Mpd.SendPassword())
|
||||
ShowMessage("Password accepted!");
|
||||
wFooter->SetGetStringHelper(StatusbarGetStringHelper);
|
||||
}
|
||||
}
|
||||
else
|
||||
ShowMessage("%s", msg);
|
||||
@@ -310,6 +317,7 @@ void NcmpcppStatusChanged(Connection *, StatusChanges changed, void *)
|
||||
case psPause:
|
||||
{
|
||||
player_state = Config.new_design ? "[paused] " : "[Paused] ";
|
||||
changed.ElapsedTime = 1;
|
||||
break;
|
||||
}
|
||||
case psStop:
|
||||
@@ -364,38 +372,26 @@ void NcmpcppStatusChanged(Connection *, StatusChanges changed, void *)
|
||||
Lyrics::Reload = 1;
|
||||
}
|
||||
Playlist::ReloadRemaining = 1;
|
||||
|
||||
playing_song_scroll_begin = 0;
|
||||
first_line_scroll_begin = 0;
|
||||
second_line_scroll_begin = 0;
|
||||
}
|
||||
static time_t now, past = 0;
|
||||
time(&now);
|
||||
if (((now > past || changed.SongID) && Mpd.isPlaying()) || RedrawStatusbar)
|
||||
if (changed.ElapsedTime || changed.SongID || RedrawStatusbar)
|
||||
{
|
||||
time(&past);
|
||||
if (np.Empty() && !(np = Mpd.GetCurrentSong()).Empty())
|
||||
WindowTitle(utf_to_locale_cpy(np.toString(Config.song_window_title_format)));
|
||||
if (!np.Empty() && Mpd.isPlaying())
|
||||
{
|
||||
changed.ElapsedTime = 1;
|
||||
|
||||
int mpd_elapsed = Mpd.GetElapsedTime();
|
||||
if (local_elapsed < mpd_elapsed-2 || local_elapsed+1 > mpd_elapsed)
|
||||
local_elapsed = mpd_elapsed;
|
||||
else if (Mpd.GetState() == psPlay && !RedrawStatusbar)
|
||||
++local_elapsed;
|
||||
|
||||
std::string tracklength;
|
||||
if (Config.new_design)
|
||||
{
|
||||
if (Config.display_remaining_time)
|
||||
{
|
||||
tracklength = "-";
|
||||
tracklength += Song::ShowTime(Mpd.GetTotalTime()-local_elapsed);
|
||||
tracklength += Song::ShowTime(Mpd.GetTotalTime()-Mpd.GetElapsedTime());
|
||||
}
|
||||
else
|
||||
tracklength = Song::ShowTime(local_elapsed);
|
||||
tracklength = Song::ShowTime(Mpd.GetElapsedTime());
|
||||
if (Mpd.GetTotalTime())
|
||||
{
|
||||
tracklength += "/";
|
||||
@@ -448,17 +444,17 @@ void NcmpcppStatusChanged(Connection *, StatusChanges changed, void *)
|
||||
if (Config.display_remaining_time)
|
||||
{
|
||||
tracklength += "-";
|
||||
tracklength += Song::ShowTime(Mpd.GetTotalTime()-local_elapsed);
|
||||
tracklength += Song::ShowTime(Mpd.GetTotalTime()-Mpd.GetElapsedTime());
|
||||
}
|
||||
else
|
||||
tracklength += Song::ShowTime(local_elapsed);
|
||||
tracklength += Song::ShowTime(Mpd.GetElapsedTime());
|
||||
tracklength += "/";
|
||||
tracklength += MPD::Song::ShowTime(Mpd.GetTotalTime());
|
||||
tracklength += "]";
|
||||
}
|
||||
else
|
||||
{
|
||||
tracklength += Song::ShowTime(local_elapsed);
|
||||
tracklength += Song::ShowTime(Mpd.GetElapsedTime());
|
||||
tracklength += "]";
|
||||
}
|
||||
basic_buffer<my_char_t> np_song;
|
||||
@@ -468,7 +464,7 @@ void NcmpcppStatusChanged(Connection *, StatusChanges changed, void *)
|
||||
*wFooter << fmtBold << XY(wFooter->GetWidth()-tracklength.length(), 1) << tracklength;
|
||||
}
|
||||
if (!block_progressbar_update)
|
||||
DrawProgressbar(local_elapsed, Mpd.GetTotalTime());
|
||||
DrawProgressbar(Mpd.GetElapsedTime(), Mpd.GetTotalTime());
|
||||
RedrawStatusbar = 0;
|
||||
}
|
||||
else
|
||||
|
||||
Reference in New Issue
Block a user