diff options
-rw-r--r-- | src/actions.cpp | 60 | ||||
-rw-r--r-- | src/display.cpp | 4 | ||||
-rw-r--r-- | src/ncmpcpp.cpp | 43 | ||||
-rw-r--r-- | src/playlist.cpp | 6 | ||||
-rw-r--r-- | src/sel_items_adder.cpp | 8 | ||||
-rw-r--r-- | src/status.cpp | 323 | ||||
-rw-r--r-- | src/status.h | 25 | ||||
-rw-r--r-- | src/statusbar.cpp | 8 | ||||
-rw-r--r-- | src/visualizer.cpp | 2 |
9 files changed, 275 insertions, 204 deletions
diff --git a/src/actions.cpp b/src/actions.cpp index 42b4d55b..82f89fd4 100644 --- a/src/actions.cpp +++ b/src/actions.cpp @@ -331,14 +331,14 @@ void MouseEvent::run() && m_mouse_event.y == LINES-(Config.statusbar_visibility ? 2 : 1) ) // progressbar { - if (Status::get().playerState() == MPD::psStop) + if (Status::State::player() == MPD::psStop) return; - Mpd.Seek(Status::get().currentSongPosition(), - Status::get().totalTime()*m_mouse_event.x/double(COLS)); + Mpd.Seek(Status::State::currentSongPosition(), + Status::State::totalTime()*m_mouse_event.x/double(COLS)); } else if (m_mouse_event.bstate & BUTTON1_PRESSED && (Config.statusbar_visibility || Config.design == Design::Alternative) - && Status::get().playerState() != MPD::psStop + && Status::State::player() != MPD::psStop && m_mouse_event.y == (Config.design == Design::Alternative ? 1 : LINES-1) && m_mouse_event.x < 9 ) // playing/paused @@ -616,13 +616,13 @@ void SlaveScreen::run() void VolumeUp::run() { - int volume = std::min(Status::get().volume()+Config.volume_change_step, 100u); + int volume = std::min(Status::State::volume()+Config.volume_change_step, 100u); Mpd.SetVolume(volume); } void VolumeDown::run() { - int volume = std::max(int(Status::get().volume()-Config.volume_change_step), 0); + int volume = std::max(int(Status::State::volume()-Config.volume_change_step), 0); Mpd.SetVolume(volume); } @@ -744,8 +744,8 @@ void DeleteStoredPlaylist::run() void ReplaySong::run() { - if (Status::get().playerState() != MPD::psStop) - Mpd.Seek(Status::get().currentSongPosition(), 0); + if (Status::State::player() != MPD::psStop) + Mpd.Seek(Status::State::currentSongPosition(), 0); } void PreviousSong::run() @@ -979,7 +979,7 @@ void Add::run() bool SeekForward::canBeRun() const { - return Status::get().playerState() != MPD::psStop && Status::get().totalTime() > 0; + return Status::State::player() != MPD::psStop && Status::State::totalTime() > 0; } void SeekForward::run() @@ -989,7 +989,7 @@ void SeekForward::run() bool SeekBackward::canBeRun() const { - return Status::get().playerState() != MPD::psStop && Status::get().totalTime() > 0; + return Status::State::player() != MPD::psStop && Status::State::totalTime() > 0; } void SeekBackward::run() @@ -1140,9 +1140,9 @@ void TogglePlayingSongCentering::run() Config.autocenter_mode ? "on" : "off" ); if (Config.autocenter_mode - && Status::get().playerState() != MPD::psStop + && Status::State::player() != MPD::psStop && !myPlaylist->main().isFiltered()) - myPlaylist->main().highlight(Status::get().currentSongPosition()); + myPlaylist->main().highlight(Status::State::currentSongPosition()); } void UpdateDatabase::run() @@ -1162,13 +1162,13 @@ bool JumpToPlayingSong::canBeRun() const return ((myScreen == myPlaylist && !myPlaylist->isFiltered()) || myScreen == myBrowser || myScreen == myLibrary) - && Status::get().playerState() != MPD::psStop; + && Status::State::player() != MPD::psStop; } void JumpToPlayingSong::run() { if (myScreen == myPlaylist) - myPlaylist->main().highlight(Status::get().currentSongPosition()); + myPlaylist->main().highlight(Status::State::currentSongPosition()); else if (myScreen == myBrowser) { myBrowser->LocateSong(myPlaylist->nowPlayingSong()); @@ -1182,7 +1182,7 @@ void JumpToPlayingSong::run() void ToggleRepeat::run() { - Mpd.SetRepeat(!Status::get().repeat()); + Mpd.SetRepeat(!Status::State::repeat()); } void Shuffle::run() @@ -1192,7 +1192,7 @@ void Shuffle::run() void ToggleRandom::run() { - Mpd.SetRandom(!Status::get().random()); + Mpd.SetRandom(!Status::State::random()); } bool StartSearching::canBeRun() const @@ -1237,17 +1237,17 @@ void SaveTagChanges::run() void ToggleSingle::run() { - Mpd.SetSingle(!Status::get().single()); + Mpd.SetSingle(!Status::State::single()); } void ToggleConsume::run() { - Mpd.SetConsume(!Status::get().consume()); + Mpd.SetConsume(!Status::State::consume()); } void ToggleCrossfade::run() { - Mpd.SetCrossfade(Status::get().crossfade() ? 0 : Config.crossfade_time); + Mpd.SetCrossfade(Status::State::crossfade() ? 0 : Config.crossfade_time); } void SetCrossfade::run() @@ -1612,7 +1612,7 @@ void JumpToTagEditor::run() bool JumpToPositionInSong::canBeRun() const { - return Status::get().playerState() != MPD::psStop && Status::get().totalTime() > 0; + return Status::State::player() != MPD::psStop && Status::State::totalTime() > 0; } void JumpToPositionInSong::run() @@ -2680,7 +2680,7 @@ void seek() using Global::Timer; using Global::SeekingInProgress; - if (!Status::get().totalTime()) + if (!Status::State::totalTime()) { Statusbar::print("Unknown item length"); return; @@ -2689,7 +2689,7 @@ void seek() Progressbar::lock(); Statusbar::lock(); - unsigned songpos = Status::elapsedTime(); + unsigned songpos = Status::State::elapsedTime(); auto t = Timer; int old_timeout = wFooter->getTimeout(); @@ -2714,8 +2714,8 @@ void seek() auto a = k.first->action(); if (a == seekForward) { - if (songpos < Status::get().totalTime()) - songpos = std::min(songpos + howmuch, Status::get().totalTime()); + if (songpos < Status::State::totalTime()) + songpos = std::min(songpos + howmuch, Status::State::totalTime()); } else if (a == seekBackward) { @@ -2739,12 +2739,12 @@ void seek() if (Config.display_remaining_time) { tracklength += "-"; - tracklength += MPD::Song::ShowTime(Status::get().totalTime()-songpos); + tracklength += MPD::Song::ShowTime(Status::State::totalTime()-songpos); } else tracklength += MPD::Song::ShowTime(songpos); tracklength += "/"; - tracklength += MPD::Song::ShowTime(Status::get().totalTime()); + tracklength += MPD::Song::ShowTime(Status::State::totalTime()); tracklength += "]"; *wFooter << NC::XY(wFooter->getWidth()-tracklength.length(), 1) << tracklength; break; @@ -2752,22 +2752,22 @@ void seek() if (Config.display_remaining_time) { tracklength = "-"; - tracklength += MPD::Song::ShowTime(Status::get().totalTime()-songpos); + tracklength += MPD::Song::ShowTime(Status::State::totalTime()-songpos); } else tracklength = MPD::Song::ShowTime(songpos); tracklength += "/"; - tracklength += MPD::Song::ShowTime(Status::get().totalTime()); + tracklength += MPD::Song::ShowTime(Status::State::totalTime()); *wHeader << NC::XY(0, 0) << tracklength << " "; wHeader->refresh(); break; } *wFooter << NC::Format::NoBold; - Progressbar::draw(songpos, Status::get().totalTime()); + Progressbar::draw(songpos, Status::State::totalTime()); wFooter->refresh(); } SeekingInProgress = false; - Mpd.Seek(Status::get().currentSongPosition(), songpos); + Mpd.Seek(Status::State::currentSongPosition(), songpos); wFooter->setTimeout(old_timeout); diff --git a/src/display.cpp b/src/display.cpp index 64b10ef6..45d892bf 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -97,8 +97,8 @@ void setProperties(NC::Menu<T> &menu, const MPD::Song &s, const ProxySongList &p discard_colors = Config.discard_colors_if_item_is_selected && is_selected; int song_pos = menu.isFiltered() ? s.getPosition() : drawn_pos; - is_now_playing = Status::get().playerState() != MPD::psStop && myPlaylist->isActiveWindow(menu) - && song_pos == Status::get().currentSongPosition(); + is_now_playing = Status::State::player() != MPD::psStop && myPlaylist->isActiveWindow(menu) + && song_pos == Status::State::currentSongPosition(); if (is_now_playing) menu << Config.now_playing_prefix; } diff --git a/src/ncmpcpp.cpp b/src/ncmpcpp.cpp index 789a13fa..e3da219b 100644 --- a/src/ncmpcpp.cpp +++ b/src/ncmpcpp.cpp @@ -22,8 +22,6 @@ #include <clocale> #include <csignal> #include <cstring> -#include <netinet/tcp.h> -#include <netinet/in.h> #include <boost/date_time/posix_time/posix_time.hpp> #include <boost/locale.hpp> @@ -160,49 +158,18 @@ int main(int argc, char **argv) { if (!Mpd.Connected()) { + // reset local status info + Status::clear(); + // clear mpd callback wFooter->clearFDCallbacksList(); try { Mpd.Connect(); if (Mpd.Version() < 16) { - Mpd.Disconnect(); - throw MPD::ClientError(MPD_ERROR_STATE, "MPD < 0.16.0 is not supported", false); + Mpd.Disconnect(); + throw MPD::ClientError(MPD_ERROR_STATE, "MPD < 0.16.0 is not supported", false); } - wFooter->addFDCallback(Mpd.GetFD(), Statusbar::Helpers::mpd); - Status::clear(); // reset local status info - Status::update(-1); // we need info about new connection - - if (Config.jump_to_now_playing_song_at_start) - { - int curr_pos = Status::get().currentSongPosition(); - if (curr_pos >= 0) - myPlaylist->main().highlight(curr_pos); - } - - // Set TCP_NODELAY on the tcp socket as we are using write-write-read pattern - // a lot (idle - write, noidle - write, then read the result of noidle), which - // kills the performance. - int flag = 1; - setsockopt(Mpd.GetFD(), IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(flag)); - - // go to startup screen - if (Config.startup_screen_type != myScreen->type()) - toScreen(Config.startup_screen_type)->switchTo(); - myScreen->refresh(); - - myBrowser->fetchSupportedExtensions(); -# ifdef ENABLE_OUTPUTS - myOutputs->FetchList(); -# endif // ENABLE_OUTPUTS -# ifdef ENABLE_VISUALIZER - myVisualizer->ResetFD(); - if (myScreen == myVisualizer) - myVisualizer->SetFD(); - myVisualizer->FindOutputID(); -# endif // ENABLE_VISUALIZER - - Statusbar::printf("Connected to %1%", Mpd.GetHostname()); } catch (MPD::ClientError &e) { diff --git a/src/playlist.cpp b/src/playlist.cpp index f6828ce4..602a8f43 100644 --- a/src/playlist.cpp +++ b/src/playlist.cpp @@ -249,9 +249,9 @@ MPD::SongList Playlist::getSelectedSongs() MPD::Song Playlist::nowPlayingSong() { MPD::Song s; - if (Status::get().playerState() != MPD::psStop) + if (Status::State::player() != MPD::psStop) withUnfilteredMenu(w, [this, &s]() { - s = w.at(Status::get().currentSongPosition()).value(); + s = w.at(Status::State::currentSongPosition()).value(); }); return s; } @@ -298,7 +298,7 @@ std::string Playlist::getTotalLength() if (Config.playlist_show_remaining_time && m_reload_remaining && !w.isFiltered()) { m_remaining_time = 0; - for (size_t i = Status::get().currentSongPosition(); i < w.size(); ++i) + for (size_t i = Status::State::currentSongPosition(); i < w.size(); ++i) m_remaining_time += w[i].value().getDuration(); m_reload_remaining = false; } diff --git a/src/sel_items_adder.cpp b/src/sel_items_adder.cpp index 97d44a69..68fad684 100644 --- a/src/sel_items_adder.cpp +++ b/src/sel_items_adder.cpp @@ -255,9 +255,9 @@ void SelectedItemsAdder::addAtTheBeginningOfPlaylist() const void SelectedItemsAdder::addAfterCurrentSong() const { - if (Status::get().playerState() == MPD::psStop) + if (Status::State::player() == MPD::psStop) return; - size_t pos = Status::get().currentSongPosition(); + size_t pos = Status::State::currentSongPosition(); ++pos; bool success = addSongsToPlaylist(m_selected_items.begin(), m_selected_items.end(), false, pos); exitSuccessfully(success); @@ -265,10 +265,10 @@ void SelectedItemsAdder::addAfterCurrentSong() const void SelectedItemsAdder::addAfterCurrentAlbum() const { - if (Status::get().playerState() == MPD::psStop) + if (Status::State::player() == MPD::psStop) return; auto &pl = myPlaylist->main(); - size_t pos = Status::get().currentSongPosition(); + size_t pos = Status::State::currentSongPosition(); withUnfilteredMenu(pl, [&pos, &pl]() { std::string album = pl[pos].value().getAlbum(); while (pos < pl.size() && pl[pos].value().getAlbum() == album) diff --git a/src/status.cpp b/src/status.cpp index d2ec1048..3d3d14b3 100644 --- a/src/status.cpp +++ b/src/status.cpp @@ -19,6 +19,8 @@ ***************************************************************************/ #include <boost/date_time/posix_time/posix_time.hpp> +#include <netinet/tcp.h> +#include <netinet/in.h> #include "browser.h" #include "charset.h" @@ -47,7 +49,7 @@ using Global::wHeader; using Global::Timer; using Global::VolumeState; -namespace {// +namespace { boost::posix_time::ptime past = boost::posix_time::from_time_t(0); @@ -55,19 +57,24 @@ size_t playing_song_scroll_begin = 0; size_t first_line_scroll_begin = 0; size_t second_line_scroll_begin = 0; -MPD::Status m_status; -unsigned m_elapsed_time = 0; - -// local copies of these are needed to be independent -// of the order of idle events incoming from MPD. -char m_repeat = 0; -char m_random = 0; -char m_single = 0; -char m_consume = 0; -char m_crossfade = 0; -char m_db_updating = 0; -int m_current_song_id = 0; -unsigned m_playlist_version = 0; +bool m_status_initialized; + +char m_consume; +char m_crossfade; +char m_db_updating; +char m_repeat; +char m_random; +char m_single; + +int m_current_song_id; +int m_current_song_pos; +unsigned m_elapsed_time; +unsigned m_kbps; +MPD::PlayerState m_player_state; +unsigned m_playlist_version; +unsigned m_playlist_length; +unsigned m_total_time; +int m_volume; void drawTitle(const MPD::Song &np) { @@ -105,8 +112,49 @@ std::string playerStateToString(MPD::PlayerState ps) return result; } +void initialize_status() +{ + // get full info about new connection + Status::update(-1); + + if (Config.jump_to_now_playing_song_at_start) + { + int curr_pos = Status::State::currentSongPosition(); + if (curr_pos >= 0) + myPlaylist->main().highlight(curr_pos); + } + + // Set TCP_NODELAY on the tcp socket as we are using write-write-read pattern + // a lot (noidle - write, command - write, then read the result of command), + // which kills the performance. + int flag = 1; + setsockopt(Mpd.GetFD(), IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(flag)); + + // go to startup screen + if (Config.startup_screen_type != myScreen->type()) + toScreen(Config.startup_screen_type)->switchTo(); + myScreen->refresh(); + + myBrowser->fetchSupportedExtensions(); +# ifdef ENABLE_OUTPUTS + myOutputs->FetchList(); +# endif // ENABLE_OUTPUTS +# ifdef ENABLE_VISUALIZER + myVisualizer->ResetFD(); + if (myScreen == myVisualizer) + myVisualizer->SetFD(); + myVisualizer->FindOutputID(); +# endif // ENABLE_VISUALIZER + + m_status_initialized = true; + wFooter->addFDCallback(Mpd.GetFD(), Statusbar::Helpers::mpd); + Statusbar::printf("Connected to %1%", Mpd.GetHostname()); +} + } +/*************************************************************************/ + void Status::handleClientError(MPD::ClientError &e) { if (!e.clearable()) @@ -116,13 +164,18 @@ void Status::handleClientError(MPD::ClientError &e) void Status::handleServerError(MPD::ServerError &e) { + Statusbar::printf("MPD: %1%", e.what()); if (e.code() == MPD_SERVER_ERROR_PERMISSION) { wFooter->setGetStringHelper(nullptr); Statusbar::put() << "Password: "; Mpd.SetPassword(wFooter->getString(0, true)); - Mpd.SendPassword(); - Statusbar::print("Password accepted"); + try { + Mpd.SendPassword(); + Statusbar::print("Password accepted"); + } catch (MPD::ServerError &e_prim) { + handleServerError(e_prim); + } wFooter->setGetStringHelper(Statusbar::Helpers::getString); } else if (e.code() == MPD_SERVER_ERROR_NO_EXIST && myScreen == myBrowser) @@ -130,7 +183,6 @@ void Status::handleServerError(MPD::ServerError &e) myBrowser->GetDirectory(getParentDirectory(myBrowser->CurrentDir())); myBrowser->refresh(); } - Statusbar::printf("MPD: %1%", e.what()); } /*************************************************************************/ @@ -141,7 +193,10 @@ void Status::trace(bool update_timer, bool update_window_timeout) Timer = boost::posix_time::microsec_clock::local_time(); if (Mpd.Connected()) { - if (m_status.playerState() == MPD::psPlay + if (!m_status_initialized) + initialize_status(); + + if (m_player_state == MPD::psPlay && Global::Timer - past > boost::posix_time::seconds(1)) { // update elapsed time/bitrate of the current song @@ -169,9 +224,14 @@ void Status::trace(bool update_timer, bool update_window_timeout) void Status::update(int event) { - MPD::Status old_status = m_status; - m_status = Mpd.getStatus(); - m_elapsed_time = m_status.elapsedTime(); + auto st = Mpd.getStatus(); + m_current_song_pos = st.currentSongPosition(); + m_elapsed_time = st.elapsedTime(); + m_kbps = st.kbps(); + m_player_state = st.playerState(); + m_playlist_length = st.playlistLength(); + m_total_time = st.totalTime(); + m_volume = st.volume(); if (event & MPD_IDLE_DATABASE) Changes::database(); @@ -180,15 +240,15 @@ void Status::update(int event) if (event & MPD_IDLE_PLAYLIST) { Changes::playlist(m_playlist_version); - m_playlist_version = m_status.playlistVersion(); + m_playlist_version = st.playlistVersion(); } if (event & MPD_IDLE_PLAYER) { Changes::playerState(); - if (m_current_song_id != m_status.currentSongID()) + if (m_current_song_id != st.currentSongID()) { Changes::songID(); - m_current_song_id = m_status.currentSongID(); + m_current_song_id = st.currentSongID(); } } if (event & MPD_IDLE_MIXER) @@ -197,28 +257,53 @@ void Status::update(int event) Changes::outputs(); if (event & (MPD_IDLE_UPDATE | MPD_IDLE_OPTIONS)) { - bool show_msg = !old_status.empty(); if (event & MPD_IDLE_UPDATE) - Changes::dbUpdateState(show_msg); + { + m_db_updating = st.updateID() ? 'U' : 0; + if (m_status_initialized) + Statusbar::printf("Database update %1%", m_db_updating ? "started" : "finished"); + } if (event & MPD_IDLE_OPTIONS) { - if (('r' == m_repeat) != m_status.repeat()) - Changes::repeat(show_msg); - if (('z' == m_random) != m_status.random()) - Changes::random(show_msg); - if (('s' == m_single) != m_status.single()) - Changes::single(show_msg); - if (('c' == m_consume) != m_status.consume()) - Changes::consume(show_msg); - if (('x' == m_crossfade) != m_status.crossfade()) - Changes::crossfade(show_msg); + if (('r' == m_repeat) != st.repeat()) + { + m_repeat = st.repeat() ? 'r' : 0; + if (m_status_initialized) + Statusbar::printf("Repeat mode is %1%", !m_repeat ? "off" : "on"); + } + if (('z' == m_random) != st.random()) + { + m_random = st.random() ? 'z' : 0; + if (m_status_initialized) + Statusbar::printf("Random mode is %1%", !m_random ? "off" : "on"); + } + if (('s' == m_single) != st.single()) + { + m_single = st.single() ? 's' : 0; + if (m_status_initialized) + Statusbar::printf("Single mode is %1%", !m_single ? "off" : "on"); + } + if (('c' == m_consume) != st.consume()) + { + m_consume = st.consume() ? 'c' : 0; + if (m_status_initialized) + Statusbar::printf("Consume mode is %1%", !m_consume ? "off" : "on"); + } + if (('x' == m_crossfade) != st.crossfade()) + { + int crossfade = st.crossfade(); + m_crossfade = crossfade ? 'x' : 0; + if (m_status_initialized) + Statusbar::printf("Crossfade set to %1% seconds", crossfade); + } } Changes::flags(); } - + m_status_initialized = true; + if (event & MPD_IDLE_PLAYER) wFooter->refresh(); - + if (event & (MPD_IDLE_PLAYLIST | MPD_IDLE_DATABASE | MPD_IDLE_PLAYER)) applyToVisibleWindows(&BaseScreen::refreshWindow); } @@ -226,41 +311,93 @@ void Status::update(int event) void Status::clear() { // reset local variables - m_status.clear(); + m_status_initialized = false; m_repeat = 0; m_random = 0; m_single = 0; m_consume = 0; m_crossfade = 0; m_db_updating = 0; - m_current_song_id = 0; + m_current_song_id = -1; + m_current_song_pos = -1; + m_kbps = 0; + m_player_state = MPD::psUnknown; + m_playlist_length = 0; m_playlist_version = 0; + m_total_time = 0; + m_volume = -1; +} + +/*************************************************************************/ + +bool Status::State::consume() +{ + return m_consume != 0; +} + +bool Status::State::crossfade() +{ + return m_crossfade != 0; } -const MPD::Status &Status::get() +bool Status::State::repeat() { - return m_status; + return m_repeat != 0; } -unsigned Status::elapsedTime() +bool Status::State::random() +{ + return m_random != 0; +} + +bool Status::State::single() +{ + return m_single != 0; +} + +int Status::State::currentSongID() +{ + return m_current_song_id; +} + +int Status::State::currentSongPosition() +{ + return m_current_song_pos; +} + +unsigned Status::State::elapsedTime() { return m_elapsed_time; } +MPD::PlayerState Status::State::player() +{ + return m_player_state; +} + +unsigned Status::State::totalTime() +{ + return m_total_time; +} + +int Status::State::volume() +{ + return m_volume; +} + /*************************************************************************/ void Status::Changes::playlist(unsigned previous_version) { myPlaylist->main().clearSearchResults(); withUnfilteredMenuReapplyFilter(myPlaylist->main(), [previous_version]() { - size_t playlist_length = m_status.playlistLength(); - if (playlist_length < myPlaylist->main().size()) + if (m_playlist_length < myPlaylist->main().size()) { - auto it = myPlaylist->main().begin()+playlist_length; + auto it = myPlaylist->main().begin()+m_playlist_length; auto end = myPlaylist->main().end(); for (; it != end; ++it) myPlaylist->unregisterSong(it->value()); - myPlaylist->main().resizeList(playlist_length); + myPlaylist->main().resizeList(m_playlist_length); } Mpd.GetPlaylistChanges(previous_version, [](MPD::Song s) { @@ -278,7 +415,7 @@ void Status::Changes::playlist(unsigned previous_version) }); }); - if (m_status.playerState() != MPD::psStop) + if (m_player_state == MPD::psStop) drawTitle(myPlaylist->nowPlayingSong()); myPlaylist->reloadTotalLength(); @@ -328,7 +465,7 @@ void Status::Changes::database() void Status::Changes::playerState() { - switch (m_status.playerState()) + switch (m_player_state) { case MPD::psPlay: drawTitle(myPlaylist->nowPlayingSong()); @@ -354,7 +491,7 @@ void Status::Changes::playerState() break; } - std::string state = playerStateToString(m_status.playerState()); + std::string state = playerStateToString(m_player_state); if (Config.design == Design::Alternative) { *wHeader << NC::XY(0, 1) << NC::Format::Bold << state << NC::Format::NoBold; @@ -381,7 +518,7 @@ void Status::Changes::songID() playing_song_scroll_begin = 0; first_line_scroll_begin = 0; second_line_scroll_begin = 0; - if (m_status.playerState() != MPD::psStop) + if (m_player_state != MPD::psStop) { GNUC_UNUSED int res; if (!Config.execute_on_song_change.empty()) @@ -395,7 +532,7 @@ void Status::Changes::songID() drawTitle(myPlaylist->nowPlayingSong()); if (Config.autocenter_mode && !myPlaylist->main().isFiltered()) - myPlaylist->main().highlight(Status::get().currentSongPosition()); + myPlaylist->main().highlight(Status::State::currentSongPosition()); if (Config.now_playing_lyrics && isVisible(myLyrics) && myLyrics->previousScreen() == myPlaylist) myLyrics->ReloadNP = 1; @@ -406,17 +543,20 @@ void Status::Changes::songID() void Status::Changes::elapsedTime(bool update_elapsed) { if (update_elapsed) - m_elapsed_time = Mpd.getStatus().elapsedTime(); - const auto &st = m_status; - - if (st.playerState() == MPD::psStop) + { + auto st = Mpd.getStatus(); + m_elapsed_time = st.elapsedTime(); + m_kbps = st.kbps(); + } + + if (m_player_state == MPD::psStop) { if (Statusbar::isUnlocked() && Config.statusbar_visibility) *wFooter << NC::XY(0, 1) << wclrtoeol; return; } - std::string ps = playerStateToString(st.playerState()); + std::string ps = playerStateToString(m_player_state); MPD::Song np = myPlaylist->nowPlayingSong(); drawTitle(np); @@ -426,24 +566,24 @@ void Status::Changes::elapsedTime(bool update_elapsed) case Design::Classic: if (Statusbar::isUnlocked() && Config.statusbar_visibility) { - if (Config.display_bitrate && st.kbps()) + if (Config.display_bitrate && m_kbps) { tracklength += " ["; - tracklength += boost::lexical_cast<std::string>(st.kbps()); + tracklength += boost::lexical_cast<std::string>(m_kbps); tracklength += " kbps]"; } tracklength += " ["; - if (st.totalTime()) + if (m_total_time) { if (Config.display_remaining_time) { tracklength += "-"; - tracklength += MPD::Song::ShowTime(st.totalTime()-m_elapsed_time); + tracklength += MPD::Song::ShowTime(m_total_time-m_elapsed_time); } else tracklength += MPD::Song::ShowTime(m_elapsed_time); tracklength += "/"; - tracklength += MPD::Song::ShowTime(st.totalTime()); + tracklength += MPD::Song::ShowTime(m_total_time); tracklength += "]"; } else @@ -462,20 +602,20 @@ void Status::Changes::elapsedTime(bool update_elapsed) if (Config.display_remaining_time) { tracklength = "-"; - tracklength += MPD::Song::ShowTime(st.totalTime()-m_elapsed_time); + tracklength += MPD::Song::ShowTime(m_total_time-m_elapsed_time); } else tracklength = MPD::Song::ShowTime(m_elapsed_time); - if (st.totalTime()) + if (m_total_time) { tracklength += "/"; - tracklength += MPD::Song::ShowTime(st.totalTime()); + tracklength += MPD::Song::ShowTime(m_total_time); } // bitrate here doesn't look good, but it can be moved somewhere else later - if (Config.display_bitrate && st.kbps()) + if (Config.display_bitrate && m_kbps) { tracklength += " "; - tracklength += boost::lexical_cast<std::string>(st.kbps()); + tracklength += boost::lexical_cast<std::string>(m_kbps); tracklength += " kbps"; } @@ -505,50 +645,7 @@ void Status::Changes::elapsedTime(bool update_elapsed) flags(); } if (Progressbar::isUnlocked()) - Progressbar::draw(m_elapsed_time, st.totalTime()); -} - -void Status::Changes::repeat(bool show_msg) -{ - m_repeat = m_status.repeat() ? 'r' : 0; - if (show_msg) - Statusbar::printf("Repeat mode is %1%", !m_repeat ? "off" : "on"); -} - -void Status::Changes::random(bool show_msg) -{ - m_random = m_status.random() ? 'z' : 0; - if (show_msg) - Statusbar::printf("Random mode is %1%", !m_random ? "off" : "on"); -} - -void Status::Changes::single(bool show_msg) -{ - m_single = m_status.single() ? 's' : 0; - if (show_msg) - Statusbar::printf("Single mode is %1%", !m_single ? "off" : "on"); -} - -void Status::Changes::consume(bool show_msg) -{ - m_consume = m_status.consume() ? 'c' : 0; - if (show_msg) - Statusbar::printf("Consume mode is %1%", !m_consume ? "off" : "on"); -} - -void Status::Changes::crossfade(bool show_msg) -{ - int crossfade = m_status.crossfade(); - m_crossfade = crossfade ? 'x' : 0; - if (show_msg) - Statusbar::printf("Crossfade set to %1% seconds", crossfade); -} - -void Status::Changes::dbUpdateState(bool show_msg) -{ - m_db_updating = m_status.updateID() ? 'U' : 0; - if (show_msg) - Statusbar::printf("Database update %1%", m_status.updateID() ? "started" : "finished"); + Progressbar::draw(m_elapsed_time, m_total_time); } void Status::Changes::flags() @@ -625,11 +722,11 @@ void Status::Changes::mixer() VolumeState = " " "Vol" ": "; break; } - if (m_status.volume() < 0) + if (m_volume < 0) VolumeState += "n/a"; else { - VolumeState += boost::lexical_cast<std::string>(m_status.volume()); + VolumeState += boost::lexical_cast<std::string>(m_volume); VolumeState += "%"; } *wHeader << Config.volume_color; diff --git a/src/status.h b/src/status.h index 61fef8f9..d2d739a8 100644 --- a/src/status.h +++ b/src/status.h @@ -34,11 +34,24 @@ inline void trace() { trace(true, false); } void update(int event); void clear(); -const MPD::Status &get(); +namespace State { -// get current elapsed time (the one from -// the status is outdated most of the time). +// flags +bool consume(); +bool crossfade(); +bool repeat(); +bool random(); +bool single(); + +// misc +int currentSongID(); +int currentSongPosition(); unsigned elapsedTime(); +MPD::PlayerState player(); +unsigned totalTime(); +int volume(); + +} namespace Changes {// @@ -48,12 +61,6 @@ void database(); void playerState(); void songID(); void elapsedTime(bool update_elapsed); -void repeat(bool show_msg); -void random(bool show_msg); -void single(bool show_msg); -void consume(bool show_msg); -void crossfade(bool show_msg); -void dbUpdateState(bool show_msg); void flags(); void mixer(); void outputs(); diff --git a/src/statusbar.cpp b/src/statusbar.cpp index a7c99c83..4b45482e 100644 --- a/src/statusbar.cpp +++ b/src/statusbar.cpp @@ -104,7 +104,7 @@ void Statusbar::unlock() else progressbarBlockUpdate = false; } - if (Status::get().playerState() == MPD::psStop) + if (Status::State::player() == MPD::psStop) { switch (Config.design) { @@ -112,7 +112,7 @@ void Statusbar::unlock() put() << wclrtoeol; break; case Design::Alternative: - Progressbar::draw(Status::elapsedTime(), Status::get().totalTime()); + Progressbar::draw(Status::State::elapsedTime(), Status::State::totalTime()); break; } wFooter->refresh(); @@ -137,7 +137,7 @@ void Statusbar::tryRedraw() else progressbarBlockUpdate = !statusbarAllowUnlock; - if (Status::get().playerState() != MPD::psStop && !statusbarBlockUpdate && !progressbarBlockUpdate) + if (Status::State::player() != MPD::psStop && !statusbarBlockUpdate && !progressbarBlockUpdate) { switch (Config.design) { @@ -145,7 +145,7 @@ void Statusbar::tryRedraw() Status::Changes::elapsedTime(false); break; case Design::Alternative: - Progressbar::draw(Status::elapsedTime(), Status::get().totalTime()); + Progressbar::draw(Status::State::elapsedTime(), Status::State::totalTime()); break; } wFooter->refresh(); diff --git a/src/visualizer.cpp b/src/visualizer.cpp index 1c403f96..1621ddca 100644 --- a/src/visualizer.cpp +++ b/src/visualizer.cpp @@ -146,7 +146,7 @@ void Visualizer::update() int Visualizer::windowTimeout() { - if (m_fifo >= 0 && Status::get().playerState() == MPD::psPlay) + if (m_fifo >= 0 && Status::State::player() == MPD::psPlay) return 1000/fps; else return Screen<WindowType>::windowTimeout(); |