diff options
author | Mathias Gumz <akira at fluxbox dot org> | 2010-01-19 19:19:45 (GMT) |
---|---|---|
committer | Mathias Gumz <akira at fluxbox dot org> | 2010-01-19 19:19:45 (GMT) |
commit | a2f673ccaff7d6562c58cf2453ffd6afb86031fe (patch) | |
tree | 5699bebb1c1c6aff013f023b07740c5ed7d0e8a9 | |
parent | e102544c2d05b3bce65d3f6b76cf4a4450b1899a (diff) | |
download | fluxbox_pavel-a2f673ccaff7d6562c58cf2453ffd6afb86031fe.zip fluxbox_pavel-a2f673ccaff7d6562c58cf2453ffd6afb86031fe.tar.bz2 |
optimization of how often fluxbox checks for the system time to display it in the clocktool
the idea (as a first patch) for this change was provided by
Thomas Habets (thomas at habets pp se). instead of having a
fixed interval each second, we now calculate the next point in
time based upon the format string used by the clocktool to render
the time. as long as no seconds are shown fluxbox now wakes up
once every minute.
-rw-r--r-- | src/ClockTool.cc | 50 |
1 files changed, 46 insertions, 4 deletions
diff --git a/src/ClockTool.cc b/src/ClockTool.cc index 1dd07cd..5892abe 100644 --- a/src/ClockTool.cc +++ b/src/ClockTool.cc | |||
@@ -47,6 +47,48 @@ | |||
47 | #include <sys/time.h> | 47 | #include <sys/time.h> |
48 | #include <typeinfo> | 48 | #include <typeinfo> |
49 | 49 | ||
50 | |||
51 | namespace { | ||
52 | |||
53 | /** | ||
54 | * return true if clock shows seconds. If clock doesn't show seconds then | ||
55 | * there is no need to wake up every second to redraw the clock. | ||
56 | */ | ||
57 | |||
58 | int showSeconds(const std::string& fmt_string) { | ||
59 | |||
60 | return fmt_string.find("%c") != -1 | ||
61 | || fmt_string.find("%r") != -1 | ||
62 | || fmt_string.find("%s") != -1 | ||
63 | || fmt_string.find("%S") != -1 | ||
64 | || fmt_string.find("%T") != -1 | ||
65 | || fmt_string.find("%X") != -1 | ||
66 | || fmt_string.find("%+") != -1; | ||
67 | } | ||
68 | |||
69 | timeval calcNextTimeout(const std::string& fmt_string) { | ||
70 | timeval now; | ||
71 | timeval next; | ||
72 | gettimeofday(&now, 0); | ||
73 | next.tv_sec = 60 - (now.tv_sec % 60) - 1; | ||
74 | next.tv_usec = 1000000 - now.tv_usec; | ||
75 | if (next.tv_usec >= 1000000) { | ||
76 | next.tv_sec++; | ||
77 | next.tv_usec -= 1000000; | ||
78 | } | ||
79 | |||
80 | // wake up at next second-change | ||
81 | if (showSeconds(fmt_string)) { | ||
82 | next.tv_sec = 0; | ||
83 | } | ||
84 | |||
85 | return next; | ||
86 | } | ||
87 | |||
88 | |||
89 | } // end of anonymous namespace | ||
90 | |||
91 | |||
50 | class ClockMenuItem: public FbTk::MenuItem { | 92 | class ClockMenuItem: public FbTk::MenuItem { |
51 | public: | 93 | public: |
52 | explicit ClockMenuItem(ClockTool &tool): | 94 | explicit ClockMenuItem(ClockTool &tool): |
@@ -157,10 +199,8 @@ ClockTool::ClockTool(const FbTk::FbWindow &parent, | |||
157 | 199 | ||
158 | _FB_USES_NLS; | 200 | _FB_USES_NLS; |
159 | 201 | ||
160 | // setup timer to check the clock every 0.01 second | 202 | m_timer.setTimeout(calcNextTimeout(*m_timeformat)); |
161 | // if nothing has changed, it wont update the graphics | 203 | |
162 | m_timer.setInterval(1); | ||
163 | // m_timer.setTimeout(delay); // don't need to set timeout on interval timer | ||
164 | FbTk::RefCount<FbTk::Command<void> > update_graphic(new FbTk::SimpleCommand<ClockTool>(*this, | 204 | FbTk::RefCount<FbTk::Command<void> > update_graphic(new FbTk::SimpleCommand<ClockTool>(*this, |
165 | &ClockTool::updateTime)); | 205 | &ClockTool::updateTime)); |
166 | m_timer.setCommand(update_graphic); | 206 | m_timer.setCommand(update_graphic); |
@@ -262,6 +302,8 @@ void ClockTool::updateTime() { | |||
262 | gettimeofday(&now, 0); | 302 | gettimeofday(&now, 0); |
263 | time_t the_time = now.tv_sec; | 303 | time_t the_time = now.tv_sec; |
264 | 304 | ||
305 | m_timer.setTimeout(calcNextTimeout(*m_timeformat)); | ||
306 | |||
265 | if (the_time != -1) { | 307 | if (the_time != -1) { |
266 | char time_string[255]; | 308 | char time_string[255]; |
267 | int time_string_len; | 309 | int time_string_len; |