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) {
if (showSeconds(fmt_string)) { // microseconds till next full second
return FbTk::FbTime::remainingNext(FbTk::FbTime::IN_SECONDS);
uint64_t now = FbTk::FbTime::system();
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(60L * FbTk::FbTime::IN_SECONDS);
return FbTk::FbTime::remainingNext(now, unit);
}

View file

@ -21,6 +21,8 @@
#include "FbTime.hh"
#include <cstdlib>
#include <sys/time.h>
#ifdef HAVE_CLOCK_GETTIME // linux|*bsd|solaris
@ -28,16 +30,16 @@
namespace {
uint64_t _now() {
uint64_t _mono() {
uint64_t n = 0L;
uint64_t t = 0L;
timespec ts;
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 {
uint64_t _now() {
uint64_t _mono() {
// mach_absolute_time() * info.numer / info.denom yields
// nanoseconds.
@ -85,13 +87,15 @@ uint64_t _now() {
uint64_t FbTk::FbTime::now() {
return ::_now();
uint64_t FbTk::FbTime::mono() {
return ::_mono();
}
uint64_t FbTk::FbTime::remainingNext(uint64_t unit) {
return (unit - (::_now() % unit) - 1);
uint64_t FbTk::FbTime::system() {
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 {
// time in micro-seconds, monotonic increasing
// time in micro-seconds
//
// interesting links:
//
@ -44,12 +44,17 @@ namespace FbTk {
namespace FbTime {
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'
uint64_t remainingNext(uint64_t unit);
// calculates the remaining microseconds from 'now' up to the
// next full 'unit'
inline uint64_t remainingNext(uint64_t now, uint64_t unit) {
return (unit - (now % unit) - 1);
}
} // namespace FbTime

View file

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

View file

@ -550,7 +550,7 @@ void FluxboxWindow::init() {
m_workspacesig.emit(*this);
m_creation_time = FbTk::FbTime::now();
m_creation_time = FbTk::FbTime::mono();
frame().frameExtentSig().emit();
@ -2206,7 +2206,7 @@ void FluxboxWindow::configureRequestEvent(XConfigureRequestEvent &cr) {
// just after creation if the user has a saved position/size
if (m_creation_time) {
uint64_t now = FbTk::FbTime::now();
uint64_t now = FbTk::FbTime::mono();
Remember& rinst = Remember::instance();
@ -2310,12 +2310,12 @@ void FluxboxWindow::keyPressEvent(XKeyEvent &ke) {
}
// 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 {
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());
}