~ubuntu-branches/ubuntu/trusty/fluxbox/trusty-proposed

« back to all changes in this revision

Viewing changes to src/FbTk/Timer.cc

  • Committer: Bazaar Package Importer
  • Author(s): Dmitry E. Oboukhov
  • Date: 2008-07-01 10:38:14 UTC
  • mfrom: (2.1.12 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080701103814-khx2b6il152x9p93
Tags: 1.0.0+deb1-8
* x-dev has been removed from build-depends (out-of-date package).
* Standards-Version bumped to 3.8.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Timer.cc for FbTk - Fluxbox Toolkit
2
 
// Copyright (c) 2003 - 2005 Henrik Kinnunen (fluxgen at fluxbox dot org)
3
 
//
4
 
// Timer.cc for Blackbox - An X11 Window Manager
5
 
// Copyright (c) 1997 - 2000 Brad Hughes (bhughes at tcac.net)
6
 
//
7
 
// Permission is hereby granted, free of charge, to any person obtaining a
8
 
// copy of this software and associated documentation files (the "Software"),
9
 
// to deal in the Software without restriction, including without limitation
10
 
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
11
 
// and/or sell copies of the Software, and to permit persons to whom the
12
 
// Software is furnished to do so, subject to the following conditions:
13
 
//
14
 
// The above copyright notice and this permission notice shall be included in
15
 
// all copies or substantial portions of the Software.
16
 
//
17
 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20
 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
 
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23
 
// DEALINGS IN THE SOFTWARE.
24
 
 
25
 
#include "Timer.hh"
26
 
 
27
 
#include "Command.hh"
28
 
 
29
 
//use GNU extensions
30
 
#ifndef _GNU_SOURCE
31
 
#define _GNU_SOURCE
32
 
#endif // _GNU_SOURCE
33
 
 
34
 
#ifdef  HAVE_CONFIG_H
35
 
#include "config.h"
36
 
#endif // HAVE_CONFIG_H
37
 
 
38
 
#include <sys/time.h>
39
 
#include <sys/types.h>
40
 
#include <unistd.h>
41
 
#ifdef HAVE_CASSERT
42
 
  #include <cassert>
43
 
#else
44
 
  #include <assert.h>
45
 
#endif
46
 
 
47
 
namespace FbTk {
48
 
 
49
 
Timer::TimerList Timer::m_timerlist;
50
 
 
51
 
Timer::Timer():m_timing(false), m_once(false) {
52
 
 
53
 
}
54
 
 
55
 
Timer::Timer(RefCount<Command> &handler):
56
 
    m_handler(handler),
57
 
    m_timing(false),
58
 
    m_once(false) {
59
 
}
60
 
 
61
 
 
62
 
Timer::~Timer() {
63
 
    if (isTiming()) stop();
64
 
}
65
 
 
66
 
 
67
 
void Timer::setTimeout(time_t t) {
68
 
    m_timeout.tv_sec = t / 1000;
69
 
    m_timeout.tv_usec = t;
70
 
    m_timeout.tv_usec -= (m_timeout.tv_sec * 1000);
71
 
    m_timeout.tv_usec *= 1000;
72
 
}
73
 
 
74
 
 
75
 
void Timer::setTimeout(timeval t) {
76
 
    m_timeout.tv_sec = t.tv_sec;
77
 
    m_timeout.tv_usec = t.tv_usec;
78
 
}
79
 
 
80
 
void Timer::setCommand(RefCount<Command> &cmd) {
81
 
    m_handler = cmd;
82
 
}
83
 
 
84
 
void Timer::start() {
85
 
    gettimeofday(&m_start, 0);
86
 
 
87
 
    // only add Timers that actually DO something
88
 
    if (! m_timing && *m_handler) {
89
 
        m_timing = true;
90
 
        addTimer(this); //add us to the list
91
 
    }
92
 
}
93
 
 
94
 
 
95
 
void Timer::stop() {
96
 
    m_timing = false;
97
 
    removeTimer(this); //remove us from the list
98
 
}
99
 
 
100
 
 
101
 
void Timer::fireTimeout() {
102
 
    if (*m_handler)
103
 
        m_handler->execute();
104
 
}
105
 
 
106
 
void Timer::updateTimers(int fd) {
107
 
    fd_set rfds;
108
 
    timeval now, tm, *timeout = 0;
109
 
 
110
 
    FD_ZERO(&rfds);
111
 
    FD_SET(fd, &rfds);
112
 
 
113
 
  
114
 
    if (m_timerlist.size() > 0) {
115
 
        gettimeofday(&now, 0);
116
 
 
117
 
        tm.tv_sec = tm.tv_usec = 0l;
118
 
 
119
 
        Timer *timer = m_timerlist.front();
120
 
 
121
 
        tm.tv_sec = timer->getStartTime().tv_sec +
122
 
            timer->getTimeout().tv_sec - now.tv_sec;
123
 
        tm.tv_usec = timer->getStartTime().tv_usec +
124
 
            timer->getTimeout().tv_usec - now.tv_usec;
125
 
 
126
 
        while (tm.tv_usec >= 1000000) {
127
 
            tm.tv_sec++;
128
 
            tm.tv_usec -= 1000000;
129
 
        }
130
 
 
131
 
        while (tm.tv_usec < 0) {
132
 
            if (tm.tv_sec > 0) {
133
 
                tm.tv_sec--;
134
 
                tm.tv_usec += 1000000;
135
 
            } else {
136
 
                tm.tv_usec = 0;
137
 
                break;
138
 
            }
139
 
        }
140
 
 
141
 
        timeout = &tm;
142
 
    }
143
 
 
144
 
    select(fd + 1, &rfds, 0, 0, timeout);
145
 
 
146
 
    TimerList::iterator it;
147
 
 
148
 
    // someone set the date of the machine BACK
149
 
    // so we have to adjust the start_time
150
 
    static time_t last_time = time(0);
151
 
    if (time(0) < last_time) {
152
 
    
153
 
        time_t delta = time(0) - last_time;
154
 
 
155
 
        for (it = m_timerlist.begin(); it != m_timerlist.end(); it++) {
156
 
            (*it)->m_start.tv_sec += delta;
157
 
        }
158
 
    }
159
 
 
160
 
 
161
 
    // check for timer timeout
162
 
    gettimeofday(&now, 0);
163
 
 
164
 
    //must check end ...the timer might remove
165
 
    //it self from the list (should be fixed in the future)
166
 
    for(it = m_timerlist.begin(); it != m_timerlist.end(); ) {
167
 
        //This is to make sure we don't get an invalid iterator
168
 
        //when we do fireTimeout
169
 
        Timer &t = *(*it);
170
 
        tm.tv_sec = t.getStartTime().tv_sec +
171
 
            t.getTimeout().tv_sec;
172
 
        tm.tv_usec = t.getStartTime().tv_usec +
173
 
            t.getTimeout().tv_usec;
174
 
 
175
 
        if (((now.tv_sec < tm.tv_sec) ||
176
 
             (now.tv_sec == tm.tv_sec && now.tv_usec < tm.tv_usec)))
177
 
            break;
178
 
 
179
 
        t.fireTimeout();
180
 
        // restart the current timer so that the start time is updated
181
 
        if (! t.doOnce()) {
182
 
            t.start();
183
 
 
184
 
            // Note that this mustn't be done if we're deleting the
185
 
            // entry from the list, so therefore it's not in the update
186
 
            // section of the for loop
187
 
            it++;
188
 
        } else {
189
 
            // Since the default stop behaviour results in the timer
190
 
            // being removed, we must remove it here, so that the iterator
191
 
            // lives well. Another option would be to add it to another
192
 
            // list, and then just go through that list and stop them all.
193
 
            it = m_timerlist.erase(it);
194
 
            t.stop();
195
 
        }
196
 
    }
197
 
 
198
 
    last_time = time(0);
199
 
}
200
 
 
201
 
void Timer::addTimer(Timer *timer) {
202
 
    assert(timer);
203
 
 
204
 
    TimerList::iterator it = m_timerlist.begin();
205
 
    TimerList::iterator it_end = m_timerlist.end();
206
 
    int index = 0;
207
 
    for (; it != it_end; ++it, ++index) {
208
 
        if (((*it)->getTimeout().tv_sec > timer->getTimeout().tv_sec) ||
209
 
            (((*it)->getTimeout().tv_sec == timer->getTimeout().tv_sec) &&
210
 
             ((*it)->getTimeout().tv_usec >= timer->getTimeout().tv_usec))) {
211
 
            break;
212
 
        }
213
 
    }
214
 
    m_timerlist.insert(it, timer); 
215
 
 
216
 
}
217
 
 
218
 
void Timer::removeTimer(Timer *timer) {
219
 
    assert(timer);
220
 
    m_timerlist.remove(timer);
221
 
}
222
 
        
223
 
}; // end namespace FbTk