Calculates timeouts of ClockTool based upon System Clock

Users expect time switches to happen upon system clock times. Calculating the
timeout for the next refresh of the shown time via the monotonic clock is
wrong: The monotonic clock yields values based upon some arbitrary point in
time which might be off a little bit to the system clock, a 'full' minute of
the monotonic clock might be in the midst of a system clock minute.
This commit is contained in:
Mathias Gumz 2013-01-26 09:21:47 +01:00
parent e7bfc63932
commit 716532dd47
5 changed files with 36 additions and 26 deletions

View file

@ -70,12 +70,13 @@ int showSeconds(const std::string& fmt_string) {
uint64_t calcNextTimeout(const std::string& fmt_string) { uint64_t calcNextTimeout(const std::string& fmt_string) {
if (showSeconds(fmt_string)) { // microseconds till next full second uint64_t now = FbTk::FbTime::system();
return FbTk::FbTime::remainingNext(FbTk::FbTime::IN_SECONDS); uint64_t unit = FbTk::FbTime::IN_SECONDS;
if (!showSeconds(fmt_string)) { // microseconds till next full minute
unit *= 60L;
} }
// microseconds until next full minute return FbTk::FbTime::remainingNext(now, unit);
return FbTk::FbTime::remainingNext(60L * FbTk::FbTime::IN_SECONDS);
} }

View file

@ -21,6 +21,8 @@
#include "FbTime.hh" #include "FbTime.hh"
#include <cstdlib>
#include <sys/time.h>
#ifdef HAVE_CLOCK_GETTIME // linux|*bsd|solaris #ifdef HAVE_CLOCK_GETTIME // linux|*bsd|solaris
@ -28,16 +30,16 @@
namespace { namespace {
uint64_t _now() { uint64_t _mono() {
uint64_t n = 0L; uint64_t t = 0L;
timespec ts; timespec ts;
if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) { if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
n = (ts.tv_sec * FbTk::FbTime::IN_SECONDS) + (ts.tv_nsec / 1000L); t = (ts.tv_sec * FbTk::FbTime::IN_SECONDS) + (ts.tv_nsec / 1000L);
} }
return n; return t;
} }
} }
@ -59,7 +61,7 @@ uint64_t _now() {
namespace { namespace {
uint64_t _now() { uint64_t _mono() {
// mach_absolute_time() * info.numer / info.denom yields // mach_absolute_time() * info.numer / info.denom yields
// nanoseconds. // nanoseconds.
@ -85,13 +87,15 @@ uint64_t _now() {
uint64_t FbTk::FbTime::mono() {
uint64_t FbTk::FbTime::now() { return ::_mono();
return ::_now();
} }
uint64_t FbTk::FbTime::remainingNext(uint64_t unit) { uint64_t FbTk::FbTime::system() {
return (unit - (::_now() % unit) - 1); static timeval v;
gettimeofday(&v, NULL);
return (v.tv_sec * FbTk::FbTime::IN_SECONDS) + v.tv_usec;
} }

View file

@ -34,7 +34,7 @@
namespace FbTk { namespace FbTk {
// time in micro-seconds, monotonic increasing // time in micro-seconds
// //
// interesting links: // interesting links:
// //
@ -44,12 +44,17 @@ namespace FbTk {
namespace FbTime { namespace FbTime {
const uint64_t IN_MILLISECONDS = 1000L; const uint64_t IN_MILLISECONDS = 1000L;
const uint64_t IN_SECONDS = 1000L * 1000L; const uint64_t IN_SECONDS = 1000L * IN_MILLISECONDS;
const uint64_t IN_MINUTES = 60 * IN_SECONDS;
uint64_t now(); uint64_t mono(); // point in time, always monotonic
uint64_t system(); // system time, might jump (DST, leap seconds)
// calculates the remaining microseconds up to the next full 'unit' // calculates the remaining microseconds from 'now' up to the
uint64_t remainingNext(uint64_t unit); // next full 'unit'
inline uint64_t remainingNext(uint64_t now, uint64_t unit) {
return (unit - (now % unit) - 1);
}
} // namespace FbTime } // namespace FbTime

View file

@ -117,7 +117,7 @@ void Timer::start() {
// it from s_timerlist before restarting it // it from s_timerlist before restarting it
stop(); stop();
m_start = FbTk::FbTime::now(); m_start = FbTk::FbTime::mono();
// interval timers have their timeout change every // interval timers have their timeout change every
// time they are started! // time they are started!
@ -158,7 +158,7 @@ void Timer::updateTimers(int fd) {
FD_SET(fd, &rfds); FD_SET(fd, &rfds);
bool overdue = false; bool overdue = false;
uint64_t now = FbTime::now(); uint64_t now = FbTime::mono();
uint64_t end_time; uint64_t end_time;
// search for overdue timers // search for overdue timers
@ -192,7 +192,7 @@ void Timer::updateTimers(int fd) {
static std::vector<FbTk::Timer*> timeouts; static std::vector<FbTk::Timer*> timeouts;
now = FbTime::now(); now = FbTime::mono();
for (it = s_timerlist.begin(); it != s_timerlist.end(); ++it ) { for (it = s_timerlist.begin(); it != s_timerlist.end(); ++it ) {
if (now < (*it)->getEndTime()) { if (now < (*it)->getEndTime()) {
break; break;

View file

@ -550,7 +550,7 @@ void FluxboxWindow::init() {
m_workspacesig.emit(*this); m_workspacesig.emit(*this);
m_creation_time = FbTk::FbTime::now(); m_creation_time = FbTk::FbTime::mono();
frame().frameExtentSig().emit(); frame().frameExtentSig().emit();
@ -2206,7 +2206,7 @@ void FluxboxWindow::configureRequestEvent(XConfigureRequestEvent &cr) {
// just after creation if the user has a saved position/size // just after creation if the user has a saved position/size
if (m_creation_time) { if (m_creation_time) {
uint64_t now = FbTk::FbTime::now(); uint64_t now = FbTk::FbTime::mono();
Remember& rinst = Remember::instance(); Remember& rinst = Remember::instance();
@ -2310,12 +2310,12 @@ void FluxboxWindow::keyPressEvent(XKeyEvent &ke) {
} }
// otherwise, make a note that the user is typing // otherwise, make a note that the user is typing
m_last_keypress_time = FbTk::FbTime::now(); m_last_keypress_time = FbTk::FbTime::mono();
} }
bool FluxboxWindow::isTyping() const { bool FluxboxWindow::isTyping() const {
uint64_t diff = FbTk::FbTime::now() - m_last_keypress_time; uint64_t diff = FbTk::FbTime::mono() - m_last_keypress_time;
return ((diff / 1000) < screen().noFocusWhileTypingDelay()); return ((diff / 1000) < screen().noFocusWhileTypingDelay());
} }