aboutsummaryrefslogtreecommitdiff
path: root/src/FbTk/Timer.cc
blob: 61875f7f8e6ac3c2aed7e7b1071ee69738c43958 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Timer.cc for FbTk - Fluxbox Toolkit
// 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)
//
// 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 "Timer.hh"

#include "CommandParser.hh"
#include "StringUtil.hh"

#ifdef HAVE_CASSERT
  #include <cassert>
#else
  #include <assert.h>
#endif

// sys/select.h on solaris wants to use memset()
#ifdef HAVE_CSTRING
#  include <cstring>
#else
#  include <string.h>
#endif

#ifdef HAVE_SYS_SELECT_H
#  include <sys/select.h>
#elif defined(_WIN32)
#  include <winsock.h>
#endif

#include <cstdio>
#include <vector>
#include <set>

namespace {

struct TimerCompare {
    // stable sort order and allows multiple timers to have
    // the same end-time
    bool operator() (const FbTk::Timer* a, const FbTk::Timer* b) const {
        uint64_t ae = a->getEndTime();
        uint64_t be = b->getEndTime();
        return (ae < be) || (ae == be && a < b);
    }
};
typedef std::set<FbTk::Timer*, TimerCompare> TimerList;
TimerList s_timerlist;

}


namespace FbTk {

Timer::Timer() :
    m_once(false),
    m_interval(0),
    m_start(0),
    m_timeout(0) {

}

Timer::Timer(const RefCount<Slot<void> > &handler):
    m_handler(handler),
    m_once(false),
    m_interval(0),
    m_start(0),
    m_timeout(0) {
}


Timer::~Timer() {
    stop();
}


void Timer::setTimeout(uint64_t timeout, bool force_start) {

    bool was_timing = isTiming();
    if (was_timing) {
        stop();
    }
    m_timeout = timeout;

    if (force_start || was_timing) {
        start();
    }
}

void Timer::setCommand(const RefCount<Slot<void> > &cmd) {
    m_handler = cmd;
}

void Timer::start() {

    // only add Timers that actually DO something
    if ( ( ! isTiming() || m_interval > 0 ) && m_handler) {

        // in case start() gets triggered on a started 
        // timer with 'm_interval != 0' we have to remove
        // it from s_timerlist before restarting it
        stop();

        m_start = FbTk::FbTime::mono();

        // interval timers have their timeout change every 
        // time they are started!
        if (m_interval != 0) {
            m_timeout = m_interval * FbTk::FbTime::IN_SECONDS;
        }
        s_timerlist.insert(this);
    }
}


void Timer::stop() {
    s_timerlist.erase(this);
}

uint64_t Timer::getEndTime() const {
    return m_start + m_timeout;
}

int Timer::isTiming() const {
    return s_timerlist.find(const_cast<FbTk::Timer*>(this)) != s_timerlist.end();
}

void Timer::fireTimeout() {
    if (m_handler)
        (*m_handler)();
}


void Timer::updateTimers(int fd) {

    fd_set              rfds;
    timeval*            tout;
    timeval             tm;
    TimerList::iterator t;
    bool                overdue = false;
    uint64_t            now;


    FD_ZERO(&rfds);
    FD_SET(fd, &rfds);
    tout = NULL;

    // search for overdue timers
    if (!s_timerlist.empty()) {

        Timer*      timer = *s_timerlist.begin();
        uint64_t    end_time = timer->getEndTime();

        now = FbTime::mono();
        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;
            tout = &tm;
        }
    }

    // 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, tout) != 0) {
        // didn't time out! x events are pending
        return;
    }

    // stoping / restarting the timers modifies the list in an upredictable
    // way. to avoid problems (infinite loops etc) we copy the current overdue
    // timers from the gloabl (and ordered) list of timers and work on it.

    static std::vector<FbTk::Timer*> timeouts;

    now = FbTime::mono();
    for (t = s_timerlist.begin(); t != s_timerlist.end(); ++t ) {
        if (now < (*t)->getEndTime()) {
            break;
        }
        timeouts.push_back(*t);
    }

    size_t i;
    const size_t ts = timeouts.size();
    for (i = 0; i < ts; ++i) {

        FbTk::Timer& timer = *timeouts[i];

        // first we stop the timer to remove it
        // from s_timerlist
        timer.stop();

        // then we call the handler which might (re)start 't'
        // on it's own
        timer.fireTimeout();

        // restart 't' if needed
        if (!timer.doOnce() && !timer.isTiming()) {
            timer.start();
        }
    }

    timeouts.clear();
}


Command<void> *DelayedCmd::parse(const std::string &command,
                           const std::string &args, bool trusted) {

    std::string cmd_str;
    int err = StringUtil::getStringBetween(cmd_str, args.c_str(), '{', '}',
                                           " \t\n", true);
    if (err == 0)
        return 0;

    RefCount<Command<void> > cmd(CommandParser<void>::instance().parse(cmd_str, trusted));
    if (cmd == 0)
        return 0;

    uint64_t delay = 200;
    StringUtil::fromString<uint64_t>(args.c_str() + err, delay);

    return new DelayedCmd(cmd, delay);
}

REGISTER_COMMAND_PARSER(delay, DelayedCmd::parse, void);

DelayedCmd::DelayedCmd(const RefCount<Slot<void> > &cmd, uint64_t timeout) {
    initTimer(timeout);
    m_timer.setCommand(cmd);
}

void DelayedCmd::initTimer(uint64_t timeout) {
    m_timer.setTimeout(timeout);
    m_timer.fireOnce(true);
}

void DelayedCmd::execute() {
    if (m_timer.isTiming())
        m_timer.stop();
    m_timer.start();
}

} // end namespace FbTk