change window timeout more transparently

This commit is contained in:
Andrzej Rybczak
2014-08-31 10:09:47 +02:00
parent 966f3ef927
commit 9c9c190742
7 changed files with 39 additions and 28 deletions

View File

@@ -141,6 +141,7 @@ int main(int argc, char **argv)
myPlaylist->switchTo(); myPlaylist->switchTo();
// local variables // local variables
int nc_wtimeout;
Key input(0, Key::Standard); Key input(0, Key::Standard);
auto past = boost::posix_time::from_time_t(0); 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()); Statusbar::printf("Unexpected error: %1%", e.what());
} }
// set appropriate window timeout
nc_wtimeout = std::numeric_limits<int>::max();
applyToVisibleWindows([&nc_wtimeout](BaseScreen *s) {
nc_wtimeout = std::min(nc_wtimeout, s->windowTimeout());
});
wFooter->setTimeout(nc_wtimeout);
if (myScreen == myPlaylist) if (myScreen == myPlaylist)
myPlaylist->EnableHighlighting(); 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) catch (MPD::ClientError &e)
{ {

View File

@@ -125,19 +125,19 @@ void BaseScreen::unlock()
/***********************************************************************/ /***********************************************************************/
void applyToVisibleWindows(void (BaseScreen::*f)()) void applyToVisibleWindows(std::function<void(BaseScreen *)> f)
{ {
if (myLockedScreen && myScreen->isMergable()) if (myLockedScreen && myScreen->isMergable())
{ {
if (myScreen == myLockedScreen) if (myScreen == myLockedScreen)
{ {
if (myInactiveScreen) if (myInactiveScreen)
(myInactiveScreen->*f)(); f(myInactiveScreen);
} }
else else
(myLockedScreen->*f)(); f(myLockedScreen);
} }
(myScreen->*f)(); f(myScreen);
} }
void updateInactiveScreen(BaseScreen *screen_to_be_set) void updateInactiveScreen(BaseScreen *screen_to_be_set)

View File

@@ -57,6 +57,9 @@ struct BaseScreen
/// Method that should resize screen /// Method that should resize screen
/// if requested by hasToBeResized /// if requested by hasToBeResized
virtual void resize() = 0; virtual void resize() = 0;
/// @return ncurses timeout parameter for the screen
virtual int windowTimeout() = 0;
/// @return title of the screen /// @return title of the screen
virtual std::wstring title() = 0; 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 getWindowResizeParams(size_t &x_offset, size_t &width, bool adjust_locked_screen = true);
}; };
void applyToVisibleWindows(void (BaseScreen::*f)()); void applyToVisibleWindows(std::function<void(BaseScreen *)> f);
void updateInactiveScreen(BaseScreen *screen_to_be_set); void updateInactiveScreen(BaseScreen *screen_to_be_set);
bool isVisible(BaseScreen *screen); bool isVisible(BaseScreen *screen);
@@ -172,6 +175,12 @@ public:
Accessor::apply(w).scroll(where); 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 /// Invoked after there was one of mouse buttons pressed
/// @param me struct that contains coords of where the click /// @param me struct that contains coords of where the click
/// had its place and button actions /// had its place and button actions

View File

@@ -352,11 +352,6 @@ void Status::Changes::playerState()
break; 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()); std::string state = playerStateToString(State::player());
if (Config.design == Design::Alternative) if (Config.design == Design::Alternative)
{ {

View File

@@ -35,14 +35,13 @@
#include "statusbar.h" #include "statusbar.h"
#include "title.h" #include "title.h"
#include "screen_switcher.h" #include "screen_switcher.h"
#include "status.h"
using Global::MainStartY; using Global::MainStartY;
using Global::MainHeight; using Global::MainHeight;
Visualizer *myVisualizer; Visualizer *myVisualizer;
const int Visualizer::WindowTimeout = 1000/25; /* 25 fps */
Visualizer::Visualizer() Visualizer::Visualizer()
: Screen(NC::Window(0, MainStartY, COLS, MainHeight, "", Config.visualizer_color, NC::Border::None)) : Screen(NC::Window(0, MainStartY, COLS, MainHeight, "", Config.visualizer_color, NC::Border::None))
{ {
@@ -62,8 +61,6 @@ void Visualizer::switchTo()
SwitchTo::execute(this); SwitchTo::execute(this);
w.clear(); w.clear();
SetFD(); SetFD();
if (m_fifo >= 0)
Global::wFooter->setTimeout(WindowTimeout);
drawHeader(); drawHeader();
} }
@@ -127,6 +124,14 @@ void Visualizer::update()
w.refresh(); w.refresh();
} }
int Visualizer::windowTimeout()
{
if (m_fifo >= 0 && Status::State::player() == MPD::psPlay)
return 1000/25; // 25 fps
else
return Screen<WindowType>::windowTimeout();
}
void Visualizer::spacePressed() void Visualizer::spacePressed()
{ {
# ifdef HAVE_FFTW3_H # ifdef HAVE_FFTW3_H

View File

@@ -46,6 +46,8 @@ struct Visualizer: Screen<NC::Window>, Tabbable
virtual void update() OVERRIDE; virtual void update() OVERRIDE;
virtual void scroll(NC::Scroll) OVERRIDE { } virtual void scroll(NC::Scroll) OVERRIDE { }
virtual int windowTimeout() OVERRIDE;
virtual void enterPressed() OVERRIDE { } virtual void enterPressed() OVERRIDE { }
virtual void spacePressed() OVERRIDE; virtual void spacePressed() OVERRIDE;
@@ -58,8 +60,6 @@ struct Visualizer: Screen<NC::Window>, Tabbable
void ResetFD(); void ResetFD();
void FindOutputID(); void FindOutputID();
static const int WindowTimeout;
protected: protected:
virtual bool isLockable() OVERRIDE { return true; } virtual bool isLockable() OVERRIDE { return true; }

View File

@@ -707,8 +707,11 @@ void Window::altCharset(bool altcharset_state) const
void Window::setTimeout(int timeout) void Window::setTimeout(int timeout)
{ {
m_window_timeout = timeout; if (timeout != m_window_timeout)
wtimeout(m_window, timeout); {
m_window_timeout = timeout;
wtimeout(m_window, timeout);
}
} }
void Window::addFDCallback(int fd, void (*callback)()) void Window::addFDCallback(int fd, void (*callback)())