From 164d32c0794cb09cab250d02d20ea1d75ece70f2 Mon Sep 17 00:00:00 2001 From: Andrzej Rybczak Date: Mon, 26 Oct 2009 18:14:06 +0100 Subject: [PATCH] use one global timer instead of several local ones --- src/clock.cpp | 10 +++++----- src/global.h | 2 ++ src/ncmpcpp.cpp | 9 +++------ src/status.cpp | 16 +++++++++------- 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/clock.cpp b/src/clock.cpp index 528ebdd0..7268350e 100644 --- a/src/clock.cpp +++ b/src/clock.cpp @@ -25,6 +25,7 @@ #ifdef ENABLE_CLOCK #include +#include #include "global.h" #include "playlist.h" @@ -105,13 +106,12 @@ void Clock::Update() if (Width > size_t(COLS) || Height > MainHeight) myPlaylist->SwitchTo(); - static time_t now = 0, past; - time(&past); - if (past <= now) + static timeval past = { 0, 0 }; + if (Timer.tv_sec <= past.tv_sec) return; - time(&now); + gettimeofday(&past, 0); - tm *time = localtime(&now); + tm *time = localtime(&past.tv_sec); mask = 0; Set(time->tm_sec % 10, 0); diff --git a/src/global.h b/src/global.h index aca7f9b9..b31415fa 100644 --- a/src/global.h +++ b/src/global.h @@ -49,6 +49,8 @@ namespace Global extern bool RedrawStatusbar; extern std::string VolumeState; + + extern timeval Timer; } #endif diff --git a/src/ncmpcpp.cpp b/src/ncmpcpp.cpp index f31de1c7..a64fb4c0 100644 --- a/src/ncmpcpp.cpp +++ b/src/ncmpcpp.cpp @@ -277,7 +277,7 @@ int main(int argc, char *argv[]) std::string screen_title; - timeval now, past; + timeval past = { 0, 0 }; // local variables end # ifndef WIN32 @@ -285,8 +285,6 @@ int main(int argc, char *argv[]) signal(SIGWINCH, sighandler); # endif // !WIN32 - gettimeofday(&now, 0); - MEVENT mouse_event; mouseinterval(0); if (Config.mouse_support) @@ -328,13 +326,12 @@ int main(int argc, char *argv[]) MessagesAllowed = 1; // header stuff - gettimeofday(&past, 0); - if (((past.tv_sec == now.tv_sec && past.tv_usec >= now.tv_usec+500000) || past.tv_sec > now.tv_sec) + if (((Timer.tv_sec == past.tv_sec && Timer.tv_usec >= past.tv_usec+500000) || Timer.tv_sec > past.tv_sec) && (myScreen == myPlaylist || myScreen == myBrowser || myScreen == myLyrics) ) { RedrawHeader = 1; - gettimeofday(&now, 0); + gettimeofday(&past, 0); } if (Config.header_visibility && RedrawHeader) { diff --git a/src/status.cpp b/src/status.cpp index dfbd7861..3948a364 100644 --- a/src/status.cpp +++ b/src/status.cpp @@ -40,11 +40,13 @@ using namespace Global; using namespace MPD; -std::string Global::VolumeState; - bool Global::UpdateStatusImmediately = 0; bool Global::RedrawStatusbar = 0; +std::string Global::VolumeState; + +timeval Global::Timer; + namespace { time_t time_of_statusbar_lock; @@ -122,10 +124,10 @@ void UnlockStatusbar() void TraceMpdStatus() { - static timeval past, now; + static timeval past = { 0, 0 }; - gettimeofday(&now, 0); - if (Mpd.Connected() && (Mpd.SupportsIdle() || now.tv_sec > past.tv_sec || UpdateStatusImmediately)) + gettimeofday(&Global::Timer, 0); + if (Mpd.Connected() && (Mpd.SupportsIdle() || Timer.tv_sec > past.tv_sec || UpdateStatusImmediately)) { Mpd.UpdateStatus(); BlockItemListUpdate = 0; @@ -135,7 +137,7 @@ void TraceMpdStatus() } if (myScreen->ActiveWindow() == myPlaylist->Items - && now.tv_sec == myPlaylist->Timer()+Config.playlist_disable_highlight_delay + && Timer.tv_sec == myPlaylist->Timer()+Config.playlist_disable_highlight_delay && myPlaylist->Items->isHighlighted() && Config.playlist_disable_highlight_delay) { @@ -145,7 +147,7 @@ void TraceMpdStatus() if (lock_statusbar_delay > 0) { - if (now.tv_sec >= time_of_statusbar_lock+lock_statusbar_delay) + if (Timer.tv_sec >= time_of_statusbar_lock+lock_statusbar_delay) { lock_statusbar_delay = -1;