all repos — fluxbox @ ddabec794c5875fc33d06ec370eab0099eaac99b

custom fork of the fluxbox windowmanager

src/Timer.cc (raw)

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
// Timer.cc for Blackbox - An X11 Window Manager
// Copyright (c) 1997 - 2000 Brad Hughes (bhughes@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.

//use GNU extensions
#ifndef	 _GNU_SOURCE
#define _GNU_SOURCE
#endif // _GNU_SOURCE

#ifdef		HAVE_CONFIG_H
#include "../config.h"
#endif // HAVE_CONFIG_H

#include "Timer.hh"
#include <sys/types.h>
#include <unistd.h>
#include <cassert>

//static var
BTimer::TimerList BTimer::m_timerlist;

BTimer::BTimer(TimeoutHandler *h):
m_handler(h),
m_timing(false),
m_once(false) {
}


BTimer::~BTimer(void) {
	if (isTiming()) stop();
}


void BTimer::setTimeout(long 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 BTimer::setTimeout(timeval t) {
	m_timeout.tv_sec = t.tv_sec;
	m_timeout.tv_usec = t.tv_usec;
}


void BTimer::start(void) {
	gettimeofday(&m_start, 0);

	if (! m_timing) {
		m_timing = true;
		addTimer(this); //add us to the list
	}
}


void BTimer::stop(void) {
	m_timing = false;
	removeTimer(this); //remove us from the list
}


void BTimer::fireTimeout(void) {
	if (m_handler) m_handler->timeout();
}

void BTimer::updateTimers(int fd) {
	fd_set rfds;
	timeval now, tm, *timeout = 0;

	FD_ZERO(&rfds);
	FD_SET(fd, &rfds);

	if (m_timerlist.size() > 0) {
		gettimeofday(&now, 0);

		tm.tv_sec = tm.tv_usec = 0l;

		BTimer *timer = m_timerlist.front();

		tm.tv_sec = timer->getStartTime().tv_sec +
			timer->getTimeout().tv_sec - now.tv_sec;
		tm.tv_usec = timer->getStartTime().tv_usec +
			timer->getTimeout().tv_usec - now.tv_usec;

		while (tm.tv_usec >= 1000000) {
			tm.tv_sec++;
			tm.tv_usec -= 1000000;
		}

		while (tm.tv_usec < 0) {
			if (tm.tv_sec > 0) {
				tm.tv_sec--;
				tm.tv_usec += 1000000;
			} else {
				tm.tv_usec = 0;
				break;
			}
		}

		timeout = &tm;
	}

	select(fd + 1, &rfds, 0, 0, timeout);

	// check for timer timeout
	gettimeofday(&now, 0);

	TimerList::iterator it = m_timerlist.begin();
	//must check end ...the timer might remove
	//it self from the list (should be fixed in the future)
	for(; it != m_timerlist.end(); ++it) {
		tm.tv_sec = (*it)->getStartTime().tv_sec +
		(*it)->getTimeout().tv_sec;
		tm.tv_usec = (*it)->getStartTime().tv_usec +
		(*it)->getTimeout().tv_usec;

		if ((now.tv_sec < tm.tv_sec) ||
			(now.tv_sec == tm.tv_sec && now.tv_usec < tm.tv_usec))
			break;

		(*it)->fireTimeout();

		// restart the current timer so that the start time is updated
		if (! (*it)->doOnce())
			(*it)->start();
		else { 
			(*it)->stop();			
			it--;
		}					
	}
}

void BTimer::addTimer(BTimer *timer) {
	assert(timer);

	TimerList::iterator it = m_timerlist.begin();
	TimerList::iterator it_end = m_timerlist.end();
	int index = 0;
	for (; it != it_end; ++it, ++index) {
		if (((*it)->getTimeout().tv_sec > timer->getTimeout().tv_sec) ||
				(((*it)->getTimeout().tv_sec == timer->getTimeout().tv_sec) &&
				((*it)->getTimeout().tv_usec >= timer->getTimeout().tv_usec))) {
			break;
		}
	}
	m_timerlist.insert(it, timer); 

}

void BTimer::removeTimer(BTimer *timer) {
	assert(timer);
	m_timerlist.remove(timer);
}