all repos — fluxbox @ 541c8c407b7ba8dd10f85bb48bcb5900270b3f84

custom fork of the fluxbox windowmanager

changed timing functions to use a monotonic increasing clock

gettimeofday() is subject to be changed on daylight-saving or to ntp-related
(think leap-seconds). even worse, it is subject to be changed BACK in time. this
is hard to fix correctly (see commit 45726d3016e and bug #3560509). it is
irrelevant for timers to know the nano-seconds since the epoch anyways.
Mathias Gumz akira at fluxbox dot org
commit

541c8c407b7ba8dd10f85bb48bcb5900270b3f84

parent

60a53113e05db443af4d520883ec3145680642a8

M configure.inconfigure.in

@@ -35,7 +35,7 @@ AC_HEADER_STDC

AC_HEADER_STDBOOL AC_CHECK_HEADERS(errno.h ctype.h dirent.h fcntl.h libgen.h \ locale.h math.h nl_types.h process.h signal.h stdarg.h \ - stdio.h time.h unistd.h \ + stdint.h stdio.h time.h unistd.h \ sys/param.h sys/select.h sys/signal.h sys/stat.h \ sys/time.h sys/types.h sys/wait.h \ langinfo.h iconv.h)

@@ -48,7 +48,7 @@ [AC_MSG_ERROR([Your libstdc++ doesn't have the sstream or strstream classes])]

)] ) -AC_CHECK_HEADERS(cassert cctype cerrno cmath cstdarg cstdio cstdlib cstring ctime) +AC_CHECK_HEADERS(cassert cctype cerrno cmath cstdarg cstdint cstdio cstdlib cstring ctime) dnl Check for existance of basename(), setlocale() and strftime()

@@ -61,9 +61,10 @@ AC_FUNC_SELECT_ARGTYPES

AC_FUNC_STAT AC_CHECK_FUNCS(basename, , AC_CHECK_LIB(gen, basename, LIBS="-lgen $LIBS")) -AC_CHECK_FUNCS(catclose catgets catopen getpid gettimeofday memset mkdir \ +AC_CHECK_FUNCS(catclose catgets catopen getpid memset mkdir \ nl_langinfo putenv regcomp select setenv setlocale sigaction snprintf \ sqrt strcasecmp strcasestr strchr strstr strtol strtoul sync vsnprintf) + dnl Windows requires the mingw-catgets library for the catgets function. AC_SEARCH_LIBS([catgets], [catgets], [], [])

@@ -92,6 +93,48 @@ ],

[AC_MSG_RESULT(no)]) +AC_MSG_CHECKING(for clock_gettime) +AC_COMPILE_IFELSE( + [AC_LANG_PROGRAM( + [#include <time.h>], + [[ + clock_gettime(CLOCK_MONOTONIC, 0); + return 0; + ]] + )], + [ + AC_DEFINE(HAVE_CLOCK_GETTIME, 1, [Define to 1 if you have the 'clock_gettime' function.]) + AC_MSG_RESULT(yes) + # *bsd has clock_gettime() in libc + AC_CHECK_LIB(rt, clock_gettime, LIBS="-lrt $LIBS") + ], + [ + AC_MSG_RESULT(no) + ] +) + +AC_MSG_CHECKING(for mach_absolute_time) +AC_COMPILE_IFELSE( + [AC_LANG_PROGRAM( + [#include <mach/mach_time.h>], + [[ + mach_absolute_time(); + return 0; + ]] + )], + [ + AC_DEFINE(HAVE_MACH_ABSOLUTE_TIME, 1, [Define to 1 if you have the 'mach_absolute_time' function.]) + AC_MSG_RESULT(yes) + #AC_CHECK_LIB(, clock_gettime, LIBS="-lrt $LIBS") + ], + [ + AC_MSG_RESULT(no) + ] +) + + + + AC_STRUCT_TM dnl ---------------

@@ -130,18 +173,8 @@ [ ])

if test "x$ac_found_iconv" = xyes; then AC_DEFINE(HAVE_ICONV, 1, [Define if you have the iconv() function.]) - AC_CHECK_LIB([iconv], - [iconv_open], - [ - LIBS="-liconv $LIBS" - ], - []) - AC_CHECK_LIB([iconv], - [libiconv_open], - [ - LIBS="-liconv $LIBS" - ], - []) + AC_CHECK_LIB(iconv, iconv_open, LIBS="-liconv $LIBS") + AC_CHECK_LIB(iconv, libiconv_open, LIBS="-liconv $LIBS") dnl Check if iconv uses const in prototype declaration AC_CACHE_CHECK(for iconv declaration,

@@ -185,10 +218,7 @@ LDFLAGS="$LDFLAGS $LIBS $X_PRE_LIBS"

dnl Check for required functions in -lX11 -AC_CHECK_LIB(X11, XOpenDisplay, - LIBS="-lX11 $LIBS", - AC_MSG_ERROR([Could not find XOpenDisplay in -lX11.]) -) +AC_CHECK_LIB(X11, XOpenDisplay, LIBS="-lX11 $LIBS", AC_MSG_ERROR([Could not find XOpenDisplay in -lX11.])) LIBS="$X_EXTRA_LIBS $LIBS" AC_CHECK_LIB(xpg4, setlocale, LIBS="-lxpg4 $LIBS")
M src/AttentionNoticeHandler.ccsrc/AttentionNoticeHandler.cc

@@ -81,7 +81,7 @@ // setup timer

RefCount<Command<void> > cmd(new ToggleFrameFocusCmd(client)); Timer *timer = new Timer(); timer->setCommand(cmd); - timer->setTimeout(**timeout_res); + timer->setTimeout(**timeout_res * FbTk::FbTime::IN_MILLISECONDS); timer->fireOnce(false); // will repeat until window has focus timer->start();
M src/ClockTool.ccsrc/ClockTool.cc

@@ -35,6 +35,7 @@ #include "FbTk/TextUtils.hh"

#include "FbTk/Menu.hh" #include "FbTk/MenuItem.hh" #include "FbTk/I18n.hh" +#include "FbTk/FbTime.hh" #ifdef HAVE_CONFIG_H #include "config.h"

@@ -45,16 +46,15 @@ #include <ctime>

#else #include <time.h> #endif -#include <sys/time.h> #include <typeinfo> - +#include <cstdio> namespace { -static const char SWITCHES_SECONDS[] = "crsSTX+"; -static const char SWITCHES_12_24H[] = "lIrkHT"; -static const char SWITCHES_24_12H[] = "kHTlIr"; -static const char SWITCH_AM_PM[] = "pP"; +const char SWITCHES_SECONDS[] = "crsSTX+"; +const char SWITCHES_12_24H[] = "lIrkHT"; +const char SWITCHES_24_12H[] = "kHTlIr"; +const char SWITCH_AM_PM[] = "pP"; /** * return true if clock shows seconds. If clock doesn't show seconds then

@@ -68,23 +68,13 @@ fmt_string, '%', SWITCHES_SECONDS, sizeof(SWITCHES_SECONDS), 0) != std::string::npos;

} -timeval calcNextTimeout(const std::string& fmt_string) { - timeval now; - timeval next; - gettimeofday(&now, 0); - next.tv_sec = 60 - (now.tv_sec % 60) - 1; - next.tv_usec = 1000000 - now.tv_usec; - if (next.tv_usec >= 1000000) { - next.tv_sec++; - next.tv_usec -= 1000000; - } +uint64_t calcNextTimeout(const std::string& fmt_string) { - // wake up at next second-change - if (showSeconds(fmt_string)) { - next.tv_sec = 0; + if (showSeconds(fmt_string)) { // microseconds till next full second + return FbTk::FbTime::remainingNext(FbTk::FbTime::IN_SECONDS); + } else { // microseconds until next full minute + return FbTk::FbTime::remainingNext(60L * FbTk::FbTime::IN_SECONDS); } - - return next; }

@@ -278,13 +268,10 @@ return m_button.height();

} void ClockTool::updateTime() { - // update clock - timeval now; - gettimeofday(&now, 0); - time_t the_time = now.tv_sec; m_timer.setTimeout(calcNextTimeout(*m_timeformat)); + time_t the_time = time(NULL); if (the_time != -1) { char time_string[255]; int time_string_len;
A src/FbTk/FbTime.cc

@@ -0,0 +1,97 @@

+// FbTime.cc for FbTk - Fluxbox Toolkit +// Copyright (c) 2012 Mathias Gumz (akira at fluxbox dot org) +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +#include "FbTime.hh" + + + +#ifdef HAVE_CLOCK_GETTIME // linux|*bsd|solaris +#include <time.h> + +namespace { + +uint64_t _now() { + + uint64_t n = 0L; + timespec ts; + + if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) { + n = (ts.tv_sec * FbTk::FbTime::IN_SECONDS) + (ts.tv_nsec / 1000L); + } + + return n; +} + +} + +#endif // HAVE_CLOCK_GETTIME + + + + + +#ifdef HAVE_MACH_ABSOLUTE_TIME // macosx + +// http://stackoverflow.com/questions/5167269/clock-gettime-alternative-in-mac-os-x +// https://github.com/ThomasHabets/monotonic_clock/blob/master/src/monotonic_mach.c +// http://shiftedbits.org/2008/10/01/mach_absolute_time-on-the-iphone/ + + +#include <mach/mach_time.h> + +namespace { + +uint64_t _now() { + + // mach_absolute_time() * info.numer / info.denom yields + // nanoseconds. + + static double micro_scale = 0.001; // 1000ms == 1ns + static bool initial = true; + + if (initial) { + initial = false; + mach_timebase_info_data_t info; + if (mach_timebase_info(&info) == 0) { + micro_scale *= static_cast<double>info.numer / static_cast<double>(info.denom); + } + } + + return static_cast<uint64_t>(mach_absolute_time() * micro_scale); +} + +} + +#endif // HAVE_MACH_ABSOLUTE_TIME + + + + + +uint64_t FbTk::FbTime::now() { + return ::_now(); +} + + +uint64_t FbTk::FbTime::remainingNext(uint64_t unit) { + return (unit - (::_now() % unit) - 1); +} +
A src/FbTk/FbTime.hh

@@ -0,0 +1,58 @@

+// FbTime.hh for FbTk - Fluxbox Toolkit +// Copyright (c) 2012 Mathias Gumz (akira at fluxbox dot org) +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +#ifndef FBTK_FBTIME_HH +#define FBTK_FBTIME_HH + + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif // HAVE_CONFIG_H + + +#ifdef HAVE_INTTYPES_H +#include <inttypes.h> +#endif // HAVE_INTTYPES_H + +namespace FbTk { + +// time in micro-seconds, monotonic increasing +// +// interesting links: +// +// http://www.python.org/dev/peps/pep-0418/#operating-system-time-functions +// http://en.cppreference.com/w/cpp/chrono + +namespace FbTime { + + const uint64_t IN_MILLISECONDS = 1000L; + const uint64_t IN_SECONDS = 1000L * 1000L; + + uint64_t now(); + + // calculates the remaining microseconds up to the next full 'unit' + uint64_t remainingNext(uint64_t unit); + +} // namespace FbTime + +} // namespace FbTk + +#endif // FBTK_TIME_HH
M src/FbTk/ImageControl.ccsrc/FbTk/ImageControl.cc

@@ -188,7 +188,7 @@

cache_max = cmax; if (cache_timeout && s_timed_cache) { - m_timer.setTimeout(cache_timeout); + m_timer.setTimeout(cache_timeout * FbTk::FbTime::IN_MILLISECONDS); RefCount<Command<void> > clean_cache(new SimpleCommand<ImageControl>(*this, &ImageControl::cleanCache)); m_timer.setCommand(clean_cache); m_timer.start();
M src/FbTk/Makefile.amsrc/FbTk/Makefile.am

@@ -36,6 +36,7 @@ TextUtils.hh TextUtils.cc Orientation.hh \

Texture.cc Texture.hh TextureRender.hh TextureRender.cc \ Shape.hh Shape.cc \ Theme.hh Theme.cc ThemeItems.cc Timer.hh Timer.cc \ + FbTime.cc FbTime.hh \ XFontImp.cc XFontImp.hh \ Button.hh Button.cc \ TextButton.hh TextButton.cc \
M src/FbTk/Menu.ccsrc/FbTk/Menu.cc

@@ -1006,7 +1006,7 @@

} if (itmp->submenu()) { // start submenu open delay - m_submenu_timer.setTimeout(theme()->getDelay()); + m_submenu_timer.setTimeout(theme()->getDelay() * FbTk::FbTime::IN_MILLISECONDS); m_submenu_timer.start(); } else if (isItemSelectable(w)){ // else normal menu item

@@ -1225,7 +1225,7 @@ internal_hide();

} void Menu::startHide() { - m_hide_timer.setTimeout(theme()->getDelay()); + m_hide_timer.setTimeout(theme()->getDelay() * FbTk::FbTime::IN_MILLISECONDS); m_hide_timer.start(); }
M src/FbTk/Timer.ccsrc/FbTk/Timer.cc

@@ -1,5 +1,5 @@

// Timer.cc for FbTk - Fluxbox Toolkit -// Copyright (c) 2003 - 2006 Henrik Kinnunen (fluxgen at fluxbox dot org) +// Copyright (c) 2003 - 2012 Henrik Kinnunen (fluxgen at fluxbox dot org) // // Timer.cc for Blackbox - An X11 Window Manager // Copyright (c) 1997 - 2000 Brad Hughes (bhughes at tcac.net)

@@ -32,10 +32,6 @@ #ifndef _GNU_SOURCE

#define _GNU_SOURCE #endif // _GNU_SOURCE -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif // HAVE_CONFIG_H - #ifdef HAVE_CASSERT #include <cassert> #else

@@ -55,9 +51,48 @@ #elif defined(_WIN32)

# include <winsock.h> #endif -namespace FbTk { +#include <cstdio> +#include <set> + + +namespace { + +struct TimerCompare { + bool operator() (const FbTk::Timer* a, const FbTk::Timer* b) { + return a->getEndTime() < b->getEndTime(); + } +}; +typedef std::set<FbTk::Timer*, TimerCompare> TimerList; + +TimerList s_timerlist; -Timer::TimerList Timer::m_timerlist; + +/// add a timer to the static list +void addTimer(FbTk::Timer *timer) { + + assert(timer); + int interval = timer->getInterval(); + + // interval timers have their timeout change every time they are started! + if (interval != 0) { + timer->setTimeout(interval * FbTk::FbTime::IN_SECONDS); + } + + s_timerlist.insert(timer); +} + +/// remove a timer from the static list +void removeTimer(FbTk::Timer *timer) { + + assert(timer); + s_timerlist.erase(timer); +} + + +} + + +namespace FbTk { Timer::Timer():m_timing(false), m_once(false), m_interval(0) {

@@ -76,22 +111,8 @@ if (isTiming()) stop();

} -void Timer::setTimeout(time_t t) { - m_timeout.tv_sec = t / 1000; - m_timeout.tv_usec = t; - m_timeout.tv_usec -= (m_timeout.tv_sec * 1000); - m_timeout.tv_usec *= 1000; -} - - -void Timer::setTimeout(const timeval &t) { - m_timeout.tv_sec = t.tv_sec; - m_timeout.tv_usec = t.tv_usec; -} - -void Timer::setTimeout(unsigned int secs, unsigned int usecs) { - m_timeout.tv_sec = secs; - m_timeout.tv_usec = usecs; +void Timer::setTimeout(uint64_t timeout) { + m_timeout = timeout; } void Timer::setCommand(const RefCount<Slot<void> > &cmd) {

@@ -99,28 +120,24 @@ m_handler = cmd;

} void Timer::start() { - gettimeofday(&m_start, 0); + + m_start = FbTk::FbTime::now(); // only add Timers that actually DO something if ((! m_timing || m_interval != 0) && m_handler) { m_timing = true; - addTimer(this); //add us to the list + ::addTimer(this); } } void Timer::stop() { m_timing = false; - removeTimer(this); //remove us from the list + ::removeTimer(this); } -void Timer::makeEndTime(timeval &tm) const { - tm.tv_sec = m_start.tv_sec + m_timeout.tv_sec; - tm.tv_usec = m_start.tv_usec + m_timeout.tv_usec; - if (tm.tv_usec >= 1000000) { - tm.tv_usec -= 1000000; - tm.tv_sec++; - } +uint64_t Timer::getEndTime() const { + return m_start + m_timeout; }

@@ -129,42 +146,34 @@ if (m_handler)

(*m_handler)(); } + void Timer::updateTimers(int fd) { + fd_set rfds; - timeval now, tm, *timeout = 0; + timeval tm; + timeval* timeout = 0; + TimerList::iterator it; FD_ZERO(&rfds); FD_SET(fd, &rfds); bool overdue = false; + uint64_t now = FbTime::now(); + uint64_t end_time; // see, if the first timer in the // list is overdue - if (!m_timerlist.empty()) { - gettimeofday(&now, 0); + if (!s_timerlist.empty()) { - Timer *timer = m_timerlist.front(); - - timer->makeEndTime(tm); - - tm.tv_sec -= now.tv_sec; - tm.tv_usec -= now.tv_usec; + Timer* timer = *s_timerlist.begin(); + end_time = timer->getEndTime(); - while (tm.tv_usec < 0) { - if (tm.tv_sec > 0) { - tm.tv_sec--; - tm.tv_usec += 1000000; - } else { - overdue = true; - tm.tv_usec = 0; - break; - } - } - - if (tm.tv_sec < 0) { // usec zero-ed above if negative - tm.tv_sec = 0; - tm.tv_usec = 0; + if (end_time < now) { overdue = true; + } else { + uint64_t diff = (end_time - now); + tm.tv_sec = diff / FbTime::IN_SECONDS; + tm.tv_usec = diff % FbTime::IN_SECONDS; } timeout = &tm;

@@ -173,99 +182,40 @@

// if not overdue, wait for the next xevent via the blocking // select(), so OS sends fluxbox to sleep. the select() will // time out when the next timer has to be handled - if (!overdue && select(fd + 1, &rfds, 0, 0, timeout) != 0) + if (!overdue && select(fd + 1, &rfds, 0, 0, timeout) != 0) { // didn't time out! x events are pending return; - - TimerList::iterator it; - - // check for timer timeout - gettimeofday(&now, 0); - - // someone set the date of the machine BACK - // so we have to adjust the start_time - static time_t last_time = 0; - if (now.tv_sec < last_time) { - - time_t delta = last_time - now.tv_sec; - - for (it = m_timerlist.begin(); it != m_timerlist.end(); ++it) { - (*it)->m_start.tv_sec -= delta; - } } - last_time = now.tv_sec; - - //must check end ...the timer might remove - //it self from the list (should be fixed in the future) - for(it = m_timerlist.begin(); it != m_timerlist.end(); ) { - //This is to make sure we don't get an invalid iterator - //when we do fireTimeout - Timer &t = *(*it); + now = FbTime::now(); + for (it = s_timerlist.begin(); it != s_timerlist.end(); ) { - t.makeEndTime(tm); - - if (((now.tv_sec < tm.tv_sec) || - (now.tv_sec == tm.tv_sec && now.tv_usec < tm.tv_usec))) + // t->fireTimeout() might add timers to the list + // this invalidates 'it'. thus we store the current + // item here + Timer* t = *it; + if (now < t->getEndTime()) { break; - - t.fireTimeout(); - // restart the current timer so that the start time is updated - if (! t.doOnce()) { - // must erase so that it's put into the right place in the list - it = m_timerlist.erase(it); - t.m_timing = false; - t.start(); - } else { - // Since the default stop behaviour results in the timer - // being removed, we must remove it here, so that the iterator - // lives well. Another option would be to add it to another - // list, and then just go through that list and stop them all. - it = m_timerlist.erase(it); - t.stop(); } - } -} + t->fireTimeout(); -void Timer::addTimer(Timer *timer) { - assert(timer); - int interval = timer->getInterval(); - // interval timers have their timeout change every time they are started! - timeval tm; - if (interval != 0) { - tm.tv_sec = timer->getStartTime().tv_sec; - tm.tv_usec = timer->getStartTime().tv_usec; - - // now convert to interval - tm.tv_sec = interval - (tm.tv_sec % interval) - 1; - tm.tv_usec = 1000000 - tm.tv_usec; - if (tm.tv_usec == 1000000) { - tm.tv_usec = 0; - tm.tv_sec += 1; - } - timer->setTimeout(tm); - } - - // set timeval to the time-of-trigger - timer->makeEndTime(tm); - - // timer list is sorted by trigger time (i.e. start plus timeout) - TimerList::iterator it = m_timerlist.begin(); - TimerList::iterator it_end = m_timerlist.end(); - for (; it != it_end; ++it) { - timeval trig; - (*it)->makeEndTime(trig); + // find the iterator to the timer again + // and continue working on the list + it = s_timerlist.find(t); + it++; + s_timerlist.erase(t); - if ((trig.tv_sec > tm.tv_sec) || - (trig.tv_sec == tm.tv_sec && - trig.tv_usec >= tm.tv_usec)) { - break; + if (! t->doOnce()) { // restart the current timer + t->m_timing = false; + t->start(); + } else { + t->stop(); } } - m_timerlist.insert(it, timer); } + Command<void> *DelayedCmd::parse(const std::string &command, const std::string &args, bool trusted) {

@@ -280,7 +230,7 @@ RefCount<Command<void> > cmd(CommandParser<void>::instance().parse(cmd_str, trusted));

if (cmd == 0) return 0; - int delay = 200000; + int delay = 200; StringUtil::fromString<int>(args.c_str() + err, delay); return new DelayedCmd(cmd, delay);

@@ -294,10 +244,7 @@ m_timer.setCommand(cmd);

} void DelayedCmd::initTimer(unsigned int timeout) { - timeval to; - to.tv_sec = timeout/1000000; - to.tv_usec = timeout % 1000000; - m_timer.setTimeout(to); + m_timer.setTimeout(timeout * FbTime::IN_MILLISECONDS); m_timer.fireOnce(true); }

@@ -307,9 +254,4 @@ m_timer.stop();

m_timer.start(); } -void Timer::removeTimer(Timer *timer) { - assert(timer); - m_timerlist.remove(timer); -} - } // end namespace FbTk
M src/FbTk/Timer.hhsrc/FbTk/Timer.hh

@@ -27,26 +27,13 @@ #define FBTK_TIMER_HH

#include "RefCount.hh" #include "Command.hh" - -#ifdef HAVE_CTIME - #include <ctime> -#else - #include <time.h> -#endif -#include <list> -#include <string> +#include "FbTime.hh" #ifdef HAVE_CONFIG_H #include "config.h" #endif //HAVE_CONFIG_H -#ifdef HAVE_INTTYPES_H -#include <inttypes.h> -#endif // HAVE_INTTYPES_H - -#include <sys/types.h> -#include <sys/time.h> -#include <unistd.h> +#include <string> namespace FbTk {

@@ -57,24 +44,21 @@ class Timer {

public: Timer(); explicit Timer(const RefCount<Slot<void> > &handler); - virtual ~Timer(); + ~Timer(); void fireOnce(bool once) { m_once = once; } - /// set timeout - void setTimeout(time_t val); - /// set timeout - void setTimeout(const timeval &val); - void setTimeout(unsigned int secs, unsigned int usecs); + void setTimeout(uint64_t timeout); void setCommand(const RefCount<Slot<void> > &cmd); + template<typename Functor> - void setFunctor(const Functor &functor) - { setCommand(RefCount<Slot<void> >(new SlotImpl<Functor, void>(functor))); } - void setInterval(int val) { m_interval = val; } - /// start timing + void setFunctor(const Functor &functor) { + setCommand(RefCount<Slot<void> >(new SlotImpl<Functor, void>(functor))); + } + + void setInterval(int seconds) { m_interval = seconds; } void start(); - /// stop timing void stop(); - /// update all timers + static void updateTimers(int file_descriptor); int isTiming() const { return m_timing; }

@@ -82,38 +66,29 @@ int getInterval() const { return m_interval; }

int doOnce() const { return m_once; } - const timeval &getTimeout() const { return m_timeout; } - const timeval &getStartTime() const { return m_start; } - void makeEndTime(timeval &tm) const; + uint64_t getTimeout() const { return m_timeout; } + uint64_t getStartTime() const { return m_start; } + uint64_t getEndTime() const; protected: /// force a timeout void fireTimeout(); private: - /// add a timer to the static list - static void addTimer(Timer *timer); - /// remove a timer from the static list - static void removeTimer(Timer *timer); - - typedef std::list<Timer *> TimerList; - static TimerList m_timerlist; ///< list of all timers, sorted by next trigger time (start + timeout) - RefCount<Slot<void> > m_handler; ///< what to do on a timeout bool m_timing; ///< clock running? bool m_once; ///< do timeout only once? - int m_interval; ///< Is an interval-only timer (e.g. clock) - // note that intervals only take note of the seconds, not microseconds + int m_interval; ///< Is an interval-only timer (e.g. clock), in seconds - timeval m_start; ///< start time - timeval m_timeout; ///< time length + uint64_t m_start; ///< start time in microseconds + uint64_t m_timeout; ///< time length in microseconds }; /// executes a command after a specified timeout class DelayedCmd: public Command<void> { public: - DelayedCmd(const RefCount<Slot<void> > &cmd, unsigned int timeout = 200000); + DelayedCmd(const RefCount<Slot<void> > &cmd, unsigned int timeout = 200); // this constructor has inverted order of parameters to avoid ambiguity with the previous // constructor
M src/Slit.ccsrc/Slit.cc

@@ -279,7 +279,7 @@ frame.pixmap = None;

// move the frame out of sight for a moment frame.window.move(-frame.window.width(), -frame.window.height()); // setup timer - m_timer.setTimeout(200); // default timeout + m_timer.setTimeout(200L * FbTk::FbTime::IN_MILLISECONDS); // default timeout m_timer.fireOnce(true); FbTk::RefCount<FbTk::Command<void> > toggle_hidden(new FbTk::SimpleCommand<Slit>(*this, &Slit::toggleHidden)); m_timer.setCommand(toggle_hidden);
M src/Toolbar.ccsrc/Toolbar.cc

@@ -258,7 +258,7 @@ frame.bevel_w = 1;

frame.grab_x = frame.grab_y = 0; // setup hide timer - m_hide_timer.setTimeout(Fluxbox::instance()->getAutoRaiseDelay()); + m_hide_timer.setTimeout(Fluxbox::instance()->getAutoRaiseDelay() * FbTk::FbTime::IN_MILLISECONDS); FbTk::RefCount<FbTk::Command<void> > toggle_hidden(new FbTk::SimpleCommand<Toolbar>(*this, &Toolbar::toggleHidden)); m_hide_timer.setCommand(toggle_hidden); m_hide_timer.fireOnce(true);
M src/TooltipWindow.ccsrc/TooltipWindow.cc

@@ -55,8 +55,11 @@ return;

resize(m_lastText); reconfigTheme(); - int h = theme()->iconbarTheme().text().font().height() + theme()->bevelWidth() * 2; - int w = theme()->iconbarTheme().text().font().textWidth(m_lastText) + theme()->bevelWidth() * 2; + + FbTk::Font& font = theme()->iconbarTheme().text().font(); + + int h = font.height() + theme()->bevelWidth() * 2; + int w = font.textWidth(m_lastText) + theme()->bevelWidth() * 2; Window root_ret; // not used Window window_ret; // not used

@@ -91,11 +94,11 @@

show(); clear(); // TODO: make this use a TextButton like TextDialog does - theme()->iconbarTheme().text().font().drawText(*this, screen().screenNumber(), - theme()->iconbarTheme().text().textGC(), - m_lastText, - theme()->bevelWidth(), - theme()->bevelWidth() + theme()->iconbarTheme().text().font().ascent()); + font.drawText(*this, screen().screenNumber(), + theme()->iconbarTheme().text().textGC(), + m_lastText, + theme()->bevelWidth(), + theme()->bevelWidth() + font.ascent()); } void TooltipWindow::updateText(const FbTk::BiDiString& text) {
M src/TooltipWindow.hhsrc/TooltipWindow.hh

@@ -47,7 +47,7 @@

/// Sets the delay before the window pops up void setDelay(int delay) { m_delay = delay; - m_timer.setTimeout(delay); + m_timer.setTimeout(delay * FbTk::FbTime::IN_MILLISECONDS); } void hide();
M src/Window.ccsrc/Window.cc

@@ -416,7 +416,7 @@ = functions.close = functions.tabable = true;

updateMWMHintsFromClient(*m_client); - m_timer.setTimeout(fluxbox.getAutoRaiseDelay()); + m_timer.setTimeout(fluxbox.getAutoRaiseDelay() * FbTk::FbTime::IN_MILLISECONDS); FbTk::RefCount<FbTk::Command<void> > raise_cmd(new FbTk::SimpleCommand<FluxboxWindow>(*this, &FluxboxWindow::raise)); m_timer.setCommand(raise_cmd);

@@ -538,9 +538,7 @@ }

m_workspacesig.emit(*this); - struct timeval now; - gettimeofday(&now, NULL); - m_creation_time = now.tv_sec; + m_creation_time = FbTk::FbTime::now(); frame().frameExtentSig().emit();

@@ -1049,15 +1047,10 @@

void FluxboxWindow::reconfigure() { applyDecorations(); - setFocusFlag(m_focused); - moveResize(frame().x(), frame().y(), frame().width(), frame().height()); - - m_timer.setTimeout(Fluxbox::instance()->getAutoRaiseDelay()); - + m_timer.setTimeout(Fluxbox::instance()->getAutoRaiseDelay() * FbTk::FbTime::IN_MILLISECONDS); updateButtons(); - frame().reconfigure(); menu().reconfigure();

@@ -2200,14 +2193,14 @@ #ifdef REMEMBER

// don't let misbehaving clients (e.g. MPlayer) move/resize their windows // just after creation if the user has a saved position/size if (m_creation_time) { - struct timeval now; - gettimeofday(&now, NULL); + + uint64_t now = FbTk::FbTime::now(); Remember& rinst = Remember::instance(); - if (now.tv_sec > m_creation_time + 1) + if (now > (m_creation_time + FbTk::FbTime::IN_SECONDS)) { m_creation_time = 0; - else if (rinst.isRemembered(*client, Remember::REM_MAXIMIZEDSTATE) || + } else if (rinst.isRemembered(*client, Remember::REM_MAXIMIZEDSTATE) || rinst.isRemembered(*client, Remember::REM_FULLSCREENSTATE)) { cr.value_mask = cr.value_mask & ~(CWWidth | CWHeight); cr.value_mask = cr.value_mask & ~(CWX | CWY);

@@ -2300,23 +2293,18 @@ // if the key was return/enter, the user probably expects the window

// e.g., typed the command in a terminal if (ks == XK_KP_Enter || ks == XK_Return) { // we'll actually reset the time for this one - m_last_keypress_time.tv_sec = 0; + m_last_keypress_time = 0; return; } // otherwise, make a note that the user is typing - gettimeofday(&m_last_keypress_time, 0); + m_last_keypress_time = FbTk::FbTime::now(); } bool FluxboxWindow::isTyping() const { - timeval now; - if (gettimeofday(&now, NULL) == -1) - return false; - unsigned int diff = 1000*(now.tv_sec - m_last_keypress_time.tv_sec); - diff += (now.tv_usec - m_last_keypress_time.tv_usec)/1000; - - return (diff < screen().noFocusWhileTypingDelay()); + uint64_t diff = FbTk::FbTime::now() - m_last_keypress_time; + return ((diff / 1000) < screen().noFocusWhileTypingDelay()); } void FluxboxWindow::buttonPressEvent(XButtonEvent &be) {
M src/Window.hhsrc/Window.hh

@@ -33,11 +33,11 @@ #include "WinButton.hh"

#include "FbTk/DefaultValue.hh" #include "FbTk/Timer.hh" +#include "FbTk/FbTime.hh" #include "FbTk/EventHandler.hh" #include "FbTk/LayerItem.hh" #include "FbTk/Signal.hh" -#include <sys/time.h> #include <vector> #include <string> #include <memory>

@@ -529,14 +529,15 @@

// state and hint signals FbTk::Signal<FluxboxWindow &> m_workspacesig, m_statesig, m_layersig, m_hintsig; - time_t m_creation_time; + uint64_t m_creation_time; + uint64_t m_last_keypress_time; + FbTk::Timer m_timer; // Window states bool moving, resizing, m_initialized; WinClient *m_attaching_tab; - FbTk::Timer m_timer; Display *display; /// display connection int m_button_grab_x, m_button_grab_y; // handles last button press event for move

@@ -544,8 +545,6 @@ int m_last_resize_x, m_last_resize_y; // handles last button press event for resize

int m_last_move_x, m_last_move_y; // handles last pos for non opaque moving int m_last_resize_h, m_last_resize_w; // handles height/width for resize "window" int m_last_pressed_button; - - timeval m_last_keypress_time; unsigned int m_workspace_number; unsigned long m_current_state; // NormalState | IconicState | Withdrawn
M src/fluxbox.ccsrc/fluxbox.cc

@@ -306,7 +306,7 @@ // Because when the command is executed we shouldn't do reconfig directly

// because it could affect ongoing menu stuff so we need to reconfig in // the next event "round". FbTk::RefCount<FbTk::Command<void> > reconfig_cmd(new FbTk::SimpleCommand<Fluxbox>(*this, &Fluxbox::timed_reconfigure)); - m_reconfig_timer.setTimeout(0, 1); + m_reconfig_timer.setTimeout(1); m_reconfig_timer.setCommand(reconfig_cmd); m_reconfig_timer.fireOnce(true);

@@ -363,7 +363,6 @@ for (i = 0; i < ScreenCount(disp); i++)

screens.push_back(i); // find out, on what "screens" fluxbox should run - // FIXME(php-coder): maybe it worths moving this code to main.cc, where command line is parsed? for (i = 1; i < m_argc; i++) { if (! strcmp(m_argv[i], "-screen")) { if ((++i) >= m_argc) {

@@ -507,8 +506,11 @@ }

void Fluxbox::eventLoop() { + Display *disp = display(); + while (!m_shutdown) { + if (XPending(disp)) { XEvent e; XNextEvent(disp, &e);

@@ -524,8 +526,9 @@ last_bad_window = None;

handleEvent(&e); } } else { - FbTk::Timer::updateTimers(ConnectionNumber(disp)); //handle all timers + FbTk::Timer::updateTimers(ConnectionNumber(disp)); } + } }

@@ -553,8 +556,10 @@ m_server_grabs = 0;

} void Fluxbox::handleEvent(XEvent * const e) { + _FB_USES_NLS; m_last_event = *e; + // it is possible (e.g. during moving) for a window // to mask all events to go to it
M src/tests/testDemandAttention.ccsrc/tests/testDemandAttention.cc

@@ -61,10 +61,7 @@ False);

FbTk::RefCount<FbTk::Command<void> > cmd(new FbTk::SimpleCommand<App> (*this, &App::demandAttention)); - timeval t; - t.tv_sec = 5; - t.tv_usec = 0; - m_timer.setTimeout(t); + m_timer.setTimeout(5 * FbTk::FbTime::IN_SECONDS); m_timer.setCommand(cmd); m_timer.fireOnce(false); m_timer.start();
M src/tests/titletest.ccsrc/tests/titletest.cc

@@ -52,10 +52,7 @@ FbTk::EventManager::instance()->add(*this, m_win);

FbTk::RefCount<FbTk::Command> cmd(new FbTk::SimpleCommand<App> (*this, &App::updateTitle)); - timeval t; - t.tv_sec = 0; - t.tv_usec = 150000; - m_timer.setTimeout(t); + m_timer.setTimeout(150 * FbTk::FbTime::IN_MILLISECONDS); m_timer.setCommand(cmd); m_timer.fireOnce(false); m_timer.start();