]> icculus.org git repositories - dana/openbox.git/blob - otk/timer.hh
split the OBTimerQueueManager and TimerQueue into their own files
[dana/openbox.git] / otk / timer.hh
1 // -*- mode: C++; indent-tabs-mode: nil; -*-
2 #ifndef   __timer_hh
3 #define   __timer_hh
4
5 extern "C" {
6 #ifdef    TIME_WITH_SYS_TIME
7 #  include <sys/time.h>
8 #  include <time.h>
9 #else // !TIME_WITH_SYS_TIME
10 #  ifdef    HAVE_SYS_TIME_H
11 #    include <sys/time.h>
12 #  else // !HAVE_SYS_TIME_H
13 #    include <time.h>
14 #  endif // HAVE_SYS_TIME_H
15 #endif // TIME_WITH_SYS_TIME
16 }
17
18 namespace otk {
19
20 class OBTimerQueueManager;
21
22 //! The data passed to the OBTimeoutHandler function.
23 /*!
24   Note: this is a very useful place to put an object instance, and set the
25   event handler to a static function in the same class.
26 */
27 typedef void *OBTimeoutData;
28 //! The type of function which can be set as the callback for an OBTimer firing
29 typedef void (*OBTimeoutHandler)(OBTimeoutData);
30
31 class OBTimer {
32 private:
33   OBTimerQueueManager *manager;
34   OBTimeoutHandler handler;
35   OBTimeoutData data;
36   bool timing, recur;
37
38   timeval _start, _timeout;
39
40   OBTimer(const OBTimer&);
41   OBTimer& operator=(const OBTimer&);
42
43 public:
44   OBTimer(OBTimerQueueManager *m, OBTimeoutHandler h, OBTimeoutData d);
45   virtual ~OBTimer();
46
47   void fireTimeout();
48
49   inline bool isTiming() const { return timing; }
50   inline bool isRecurring() const { return recur; }
51
52   inline const timeval &getTimeout() const { return _timeout; }
53   inline const timeval &getStartTime() const { return _start; }
54
55   timeval timeRemaining(const timeval &tm) const;
56   bool shouldFire(const timeval &tm) const;
57   timeval endpoint() const;
58
59   inline void recurring(bool b) { recur = b; }
60
61   void setTimeout(long t);
62   void setTimeout(const timeval &t);
63
64   void start();  // manager acquires timer
65   void stop();   // manager releases timer
66   void halt();   // halts the timer
67
68   bool operator<(const OBTimer& other) const
69   { return shouldFire(other.endpoint()); }
70 };
71
72 }
73
74 #endif // __timer_hh