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