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