]> Dogcows Code - chaz/openbox/blob - src/timer.cc
c785b3456759e3132b10321efcd3cfa101dad584
[chaz/openbox] / src / timer.cc
1 // -*- mode: C++; indent-tabs-mode: nil; -*-
2
3 #ifdef HAVE_CONFIG_H
4 # include "../config.h"
5 #endif // HAVE_CONFIG_H
6
7 #include "basedisplay.hh"
8 #include "timer.hh"
9 #include "util.hh"
10
11 BTimer::BTimer(TimerQueueManager *m, TimeoutHandler *h) {
12 manager = m;
13 handler = h;
14
15 recur = timing = False;
16 }
17
18
19 BTimer::~BTimer(void) {
20 if (timing) stop();
21 }
22
23
24 void BTimer::setTimeout(long t) {
25 _timeout.tv_sec = t / 1000;
26 _timeout.tv_usec = t % 1000;
27 _timeout.tv_usec *= 1000;
28 }
29
30
31 void BTimer::setTimeout(const timeval &t) {
32 _timeout.tv_sec = t.tv_sec;
33 _timeout.tv_usec = t.tv_usec;
34 }
35
36
37 void BTimer::start(void) {
38 gettimeofday(&_start, 0);
39
40 if (! timing) {
41 timing = True;
42 manager->addTimer(this);
43 }
44 }
45
46
47 void BTimer::stop(void) {
48 timing = False;
49
50 manager->removeTimer(this);
51 }
52
53
54 void BTimer::halt(void) {
55 timing = False;
56 }
57
58
59 void BTimer::fireTimeout(void) {
60 if (handler)
61 handler->timeout();
62 }
63
64
65 timeval BTimer::timeRemaining(const timeval &tm) const {
66 timeval ret = endpoint();
67
68 ret.tv_sec -= tm.tv_sec;
69 ret.tv_usec -= tm.tv_usec;
70
71 return normalizeTimeval(ret);
72 }
73
74
75 timeval BTimer::endpoint(void) const {
76 timeval ret;
77
78 ret.tv_sec = _start.tv_sec + _timeout.tv_sec;
79 ret.tv_usec = _start.tv_usec + _timeout.tv_usec;
80
81 return normalizeTimeval(ret);
82 }
83
84
85 bool BTimer::shouldFire(const timeval &tm) const {
86 timeval end = endpoint();
87
88 return ! ((tm.tv_sec < end.tv_sec) ||
89 (tm.tv_sec == end.tv_sec && tm.tv_usec < end.tv_usec));
90 }
This page took 0.038026 seconds and 4 git commands to generate.