]> Dogcows Code - chaz/openbox/blob - otk/timer.hh
blef
[chaz/openbox] / otk / timer.hh
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 #ifndef __timer_hh
3 #define __timer_hh
4
5 /*! @file timer.hh
6 @brief Contains the Timer class, used for timed callbacks.
7 */
8
9 extern "C" {
10 #ifdef TIME_WITH_SYS_TIME
11 # include <sys/time.h>
12 # include <time.h>
13 #else // !TIME_WITH_SYS_TIME
14 # ifdef HAVE_SYS_TIME_H
15 # include <sys/time.h>
16 # else // !HAVE_SYS_TIME_H
17 # include <time.h>
18 # endif // HAVE_SYS_TIME_H
19 #endif // TIME_WITH_SYS_TIME
20 }
21
22 #include <queue>
23 #include <vector>
24
25 namespace otk {
26
27 //! The Timer class implements timed callbacks.
28 /*!
29 The Timer class can be used to have a callback fire after a given time
30 interval. A created Timer will fire repetitively until it is destroyed.
31 */
32 class Timer {
33 public:
34 //! Data type of Timer callback
35 typedef void (*TimeoutHandler)(void *data);
36
37 private:
38 //! Compares two timeval structs
39 struct TimerCompare {
40 //! Compares two timeval structs
41 inline bool operator()(const Timer *a, const Timer *b) const {
42 return timercmp(&a->_timeout, &b->_timeout, >);
43 }
44 };
45
46 typedef
47 std::priority_queue<Timer*, std::vector<Timer*>, TimerCompare> TimerQ;
48
49 //! Milliseconds between timer firings
50 long _delay;
51 //! Callback for timer expiry
52 TimeoutHandler _action;
53 //! Data sent to callback
54 void *_data;
55 //! We overload the delete operator to just set this to true
56 bool _del_me;
57 //! The time the last fire should've been at
58 struct timeval _last;
59 //! When this timer will next trigger
60 struct timeval _timeout;
61
62 //! Queue of pending timers
63 static TimerQ _q;
64 //! Time next timer will expire
65 static timeval _nearest_timeout;
66 //! Time at start of current processing loop
67 static timeval _now;
68
69 //! Really delete something (not just flag for later)
70 /*!
71 @param self Timer to be deleted.
72 */
73 static void realDelete(Timer *self);
74
75 //! Adds a millisecond delay to a timeval structure
76 /*!
77 @param a Amount of time to increment.
78 @param msec Number of milliseconds to increment by.
79 */
80 static void timevalAdd(timeval &a, long msec);
81
82 public:
83 //! Constructs a new running timer and queues it
84 /*!
85 @param delay Time in milliseconds between firings
86 @param cb The function to be called on fire.
87 @param data Data to be passed to the callback on fire.
88 */
89 Timer(long delay, TimeoutHandler cb, void *data);
90
91 //! Overloaded delete so we can leave deleted objects in queue for later reap
92 /*!
93 @param self Pointer to current instance of Timer.
94 */
95 void operator delete(void *self);
96
97 //! Dispatches all elligible timers, then optionally waits for X events
98 /*!
99 @param wait Whether to wait for X events after processing timers.
100 */
101 static void dispatchTimers(bool wait = true);
102
103 //! Returns a relative timeval (to pass select) of the next timer
104 /*!
105 @param tm Changed to hold the time until next timer.
106 @return true if there are any timers queued, and the timeout is being
107 returned in 'tm'. false if there are no timers queued.
108 */
109 static bool nearestTimeout(struct timeval &tm);
110
111 //! Initializes internal data before use
112 static void initialize(void);
113
114 //! Deletes all waiting timers
115 static void destroy(void);
116 };
117
118 }
119
120 #endif // __timer.hh
This page took 0.038654 seconds and 4 git commands to generate.