From 9c9c190742203c33dff1e35aaf063567dd8317e4 Mon Sep 17 00:00:00 2001 From: Andrzej Rybczak Date: Sun, 31 Aug 2014 10:09:47 +0200 Subject: [PATCH] change window timeout more transparently --- src/ncmpcpp.cpp | 19 +++++++++---------- src/screen.cpp | 8 ++++---- src/screen.h | 11 ++++++++++- src/status.cpp | 5 ----- src/visualizer.cpp | 13 +++++++++---- src/visualizer.h | 4 ++-- src/window.cpp | 7 +++++-- 7 files changed, 39 insertions(+), 28 deletions(-) diff --git a/src/ncmpcpp.cpp b/src/ncmpcpp.cpp index 4b4edf3f..3f49e09e 100644 --- a/src/ncmpcpp.cpp +++ b/src/ncmpcpp.cpp @@ -141,6 +141,7 @@ int main(int argc, char **argv) myPlaylist->switchTo(); // local variables + int nc_wtimeout; Key input(0, Key::Standard); auto past = boost::posix_time::from_time_t(0); @@ -243,18 +244,16 @@ int main(int argc, char **argv) { Statusbar::printf("Unexpected error: %1%", e.what()); } - + + // set appropriate window timeout + nc_wtimeout = std::numeric_limits::max(); + applyToVisibleWindows([&nc_wtimeout](BaseScreen *s) { + nc_wtimeout = std::min(nc_wtimeout, s->windowTimeout()); + }); + wFooter->setTimeout(nc_wtimeout); + if (myScreen == myPlaylist) myPlaylist->EnableHighlighting(); - -# ifdef ENABLE_VISUALIZER - // visualizer sets timeout to 40ms, but since only it needs such small - // value, we should restore default one after switching to another screen. - if (wFooter->getTimeout() < 500 - && !(myScreen == myVisualizer || myLockedScreen == myVisualizer || myInactiveScreen == myVisualizer) - ) - wFooter->setTimeout(500); -# endif // ENABLE_VISUALIZER } catch (MPD::ClientError &e) { diff --git a/src/screen.cpp b/src/screen.cpp index d193e56b..1150ed60 100644 --- a/src/screen.cpp +++ b/src/screen.cpp @@ -125,19 +125,19 @@ void BaseScreen::unlock() /***********************************************************************/ -void applyToVisibleWindows(void (BaseScreen::*f)()) +void applyToVisibleWindows(std::function f) { if (myLockedScreen && myScreen->isMergable()) { if (myScreen == myLockedScreen) { if (myInactiveScreen) - (myInactiveScreen->*f)(); + f(myInactiveScreen); } else - (myLockedScreen->*f)(); + f(myLockedScreen); } - (myScreen->*f)(); + f(myScreen); } void updateInactiveScreen(BaseScreen *screen_to_be_set) diff --git a/src/screen.h b/src/screen.h index f885606a..758ce6bc 100644 --- a/src/screen.h +++ b/src/screen.h @@ -57,6 +57,9 @@ struct BaseScreen /// Method that should resize screen /// if requested by hasToBeResized virtual void resize() = 0; + + /// @return ncurses timeout parameter for the screen + virtual int windowTimeout() = 0; /// @return title of the screen virtual std::wstring title() = 0; @@ -107,7 +110,7 @@ protected: void getWindowResizeParams(size_t &x_offset, size_t &width, bool adjust_locked_screen = true); }; -void applyToVisibleWindows(void (BaseScreen::*f)()); +void applyToVisibleWindows(std::function f); void updateInactiveScreen(BaseScreen *screen_to_be_set); bool isVisible(BaseScreen *screen); @@ -172,6 +175,12 @@ public: Accessor::apply(w).scroll(where); } + /// @return timeout parameter used for the screen (in ms) + /// @default 500 + virtual int windowTimeout() OVERRIDE { + return 500; + } + /// Invoked after there was one of mouse buttons pressed /// @param me struct that contains coords of where the click /// had its place and button actions diff --git a/src/status.cpp b/src/status.cpp index 799facdf..c3ab3912 100644 --- a/src/status.cpp +++ b/src/status.cpp @@ -352,11 +352,6 @@ void Status::Changes::playerState() break; } -# ifdef ENABLE_VISUALIZER - if (myScreen == myVisualizer) - wFooter->setTimeout(State::player() == MPD::psPlay ? Visualizer::WindowTimeout : 500); -# endif // ENABLE_VISUALIZER - std::string state = playerStateToString(State::player()); if (Config.design == Design::Alternative) { diff --git a/src/visualizer.cpp b/src/visualizer.cpp index d4a9cb79..2fa06ae0 100644 --- a/src/visualizer.cpp +++ b/src/visualizer.cpp @@ -35,14 +35,13 @@ #include "statusbar.h" #include "title.h" #include "screen_switcher.h" +#include "status.h" using Global::MainStartY; using Global::MainHeight; Visualizer *myVisualizer; -const int Visualizer::WindowTimeout = 1000/25; /* 25 fps */ - Visualizer::Visualizer() : Screen(NC::Window(0, MainStartY, COLS, MainHeight, "", Config.visualizer_color, NC::Border::None)) { @@ -62,8 +61,6 @@ void Visualizer::switchTo() SwitchTo::execute(this); w.clear(); SetFD(); - if (m_fifo >= 0) - Global::wFooter->setTimeout(WindowTimeout); drawHeader(); } @@ -127,6 +124,14 @@ void Visualizer::update() w.refresh(); } +int Visualizer::windowTimeout() +{ + if (m_fifo >= 0 && Status::State::player() == MPD::psPlay) + return 1000/25; // 25 fps + else + return Screen::windowTimeout(); +} + void Visualizer::spacePressed() { # ifdef HAVE_FFTW3_H diff --git a/src/visualizer.h b/src/visualizer.h index 3a9a8573..ed174eee 100644 --- a/src/visualizer.h +++ b/src/visualizer.h @@ -46,6 +46,8 @@ struct Visualizer: Screen, Tabbable virtual void update() OVERRIDE; virtual void scroll(NC::Scroll) OVERRIDE { } + + virtual int windowTimeout() OVERRIDE; virtual void enterPressed() OVERRIDE { } virtual void spacePressed() OVERRIDE; @@ -58,8 +60,6 @@ struct Visualizer: Screen, Tabbable void ResetFD(); void FindOutputID(); - static const int WindowTimeout; - protected: virtual bool isLockable() OVERRIDE { return true; } diff --git a/src/window.cpp b/src/window.cpp index 65e69a2d..15877c14 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -707,8 +707,11 @@ void Window::altCharset(bool altcharset_state) const void Window::setTimeout(int timeout) { - m_window_timeout = timeout; - wtimeout(m_window, timeout); + if (timeout != m_window_timeout) + { + m_window_timeout = timeout; + wtimeout(m_window, timeout); + } } void Window::addFDCallback(int fd, void (*callback)())