From 44302aee47348c36a853e6f29ad5f80cfd3e333b Mon Sep 17 00:00:00 2001 From: Andrzej Rybczak Date: Sun, 31 Aug 2014 11:08:53 +0200 Subject: [PATCH] add posibility of dynamic window timeout change --- src/ncmpcpp.cpp | 35 ++++++++++++++++++----------------- src/status.cpp | 14 +++++++++++--- src/status.h | 3 ++- 3 files changed, 31 insertions(+), 21 deletions(-) diff --git a/src/ncmpcpp.cpp b/src/ncmpcpp.cpp index 3f49e09e..cfd4ed5c 100644 --- a/src/ncmpcpp.cpp +++ b/src/ncmpcpp.cpp @@ -88,8 +88,6 @@ namespace int main(int argc, char **argv) { using Global::myScreen; - using Global::myLockedScreen; - using Global::myInactiveScreen; using Global::wHeader; using Global::wFooter; @@ -97,7 +95,7 @@ int main(int argc, char **argv) using Global::VolumeState; using Global::Timer; - std::srand(std::time(0)); + srand(time(nullptr)); std::setlocale(LC_ALL, ""); std::locale::global(Charset::internalLocale()); @@ -131,7 +129,6 @@ int main(int argc, char **argv) wHeader->display(); wFooter = new NC::Window(0, Actions::FooterStartY, COLS, Actions::FooterHeight, "", Config.statusbar_color, NC::Border::None); - wFooter->setTimeout(500); wFooter->setGetStringHelper(Statusbar::Helpers::getString); // initialize global timer @@ -141,8 +138,8 @@ int main(int argc, char **argv) myPlaylist->switchTo(); // local variables - int nc_wtimeout; - Key input(0, Key::Standard); + bool key_pressed = false; + Key input = Key::noOp; auto past = boost::posix_time::from_time_t(0); /// enable mouse @@ -183,6 +180,7 @@ int main(int argc, char **argv) // go to startup screen if (Config.startup_screen_type != myScreen->type()) toScreen(Config.startup_screen_type)->switchTo(); + myScreen->refresh(); myBrowser->fetchSupportedExtensions(); # ifdef ENABLE_OUTPUTS @@ -203,7 +201,8 @@ int main(int argc, char **argv) } } - Status::trace(); + // update timer, status if necessary etc. + Status::trace(!key_pressed); if (run_resize_screen) { @@ -220,13 +219,22 @@ int main(int argc, char **argv) past = Timer; } - if (input != Key::noOp) + if (key_pressed) myScreen->refreshWindow(); input = Key::read(*wFooter); + key_pressed = input != Key::noOp; - if (input == Key::noOp) + if (!key_pressed) continue; - + + // The reason we want to update timer here is that if the timer is updated + // in Status::trace, then Key::read usually blocks for 500ms and if key is + // pressed 400ms after Key::read was called, we end up with Timer that is + // ~400ms inaccurate. On the other hand, if keys are being pressed, we don't + // want to update timer in both Status::trace and here. Therefore we update + // timer in Status::trace only if there was no recent input. + Timer = boost::posix_time::microsec_clock::local_time(); + try { auto k = Bindings.get(input); @@ -245,13 +253,6 @@ 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(); } diff --git a/src/status.cpp b/src/status.cpp index c3ab3912..6114b6dd 100644 --- a/src/status.cpp +++ b/src/status.cpp @@ -131,9 +131,10 @@ void Status::handleServerError(MPD::ServerError &e) /*************************************************************************/ -void Status::trace() +void Status::trace(bool update_timer) { - Timer = boost::posix_time::microsec_clock::local_time(); + if (update_timer) + Timer = boost::posix_time::microsec_clock::local_time(); if (Mpd.Connected()) { if (State::player() == MPD::psPlay @@ -144,9 +145,16 @@ void Status::trace() wFooter->refresh(); past = Timer; } - + applyToVisibleWindows(&BaseScreen::update); Statusbar::tryRedraw(); + + // set appropriate window timeout + int nc_wtimeout = std::numeric_limits::max(); + applyToVisibleWindows([&nc_wtimeout](BaseScreen *s) { + nc_wtimeout = std::min(nc_wtimeout, s->windowTimeout()); + }); + wFooter->setTimeout(nc_wtimeout); Mpd.idle(); } diff --git a/src/status.h b/src/status.h index 7a8c8088..a4ec3178 100644 --- a/src/status.h +++ b/src/status.h @@ -29,7 +29,8 @@ namespace Status {// void handleClientError(MPD::ClientError &e); void handleServerError(MPD::ServerError &e); -void trace(); +void trace(bool update_timer); +inline void trace() { trace(true); } void update(int event); namespace State {//