4d96eedf7fccb735b295fb2bf993fb1350738da3
[ardour.git] / libs / pbd / pbd / timer.h
1 /*
2     Copyright (C) 2014 Tim Mayberry
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #ifndef __libpbd_timer_h__
21 #define __libpbd_timer_h__
22
23 #include <sigc++/signal.h>
24
25 #include <glibmm/main.h>
26
27 #include "pbd/libpbd_visibility.h"
28
29 namespace PBD {
30
31 /**
32  * The Timer class is a wrapper around Glib TimeoutSources
33  * The Timer will start automatically when the first connection
34  * is made and stop when the last callback is disconnected.
35  */
36 class LIBPBD_API Timer
37 {
38 public:
39
40         Timer (unsigned int interval,
41                const Glib::RefPtr<Glib::MainContext>& main_context);
42
43         unsigned int get_interval () const;
44
45         void set_interval (unsigned int new_interval);
46
47         virtual unsigned int connection_count () const = 0;
48
49         void suspend () { m_suspended = true; }
50         void resume  () { m_suspended = false; }
51
52 protected:
53
54         virtual ~Timer() { }
55
56         void start ();
57
58         void stop ();
59
60         virtual bool on_elapsed () = 0;
61
62         bool suspended  () const { return m_suspended; }
63
64 private:
65
66         Timer(const Timer&);
67         Timer& operator= (const Timer&);
68
69 private:
70
71         static gboolean _timeout_handler (void *data);
72
73         bool timeout_handler ();
74
75         GSource*                               m_timeout_source;
76
77         unsigned int                           m_timeout_interval;
78
79         const Glib::RefPtr<Glib::MainContext>  m_main_context;
80
81         bool                                   m_suspended;
82
83 };
84
85 class LIBPBD_API StandardTimer : public Timer
86 {
87 public:
88
89         StandardTimer (unsigned int interval,
90                        const Glib::RefPtr<Glib::MainContext>& main_context = Glib::MainContext::get_default());
91
92         sigc::connection connect (const sigc::slot<void>& slot);
93
94         virtual unsigned int connection_count () const
95         { return m_signal.size (); }
96
97 protected:
98
99         virtual bool on_elapsed ();
100
101         sigc::signal<void>                  m_signal;
102
103 };
104
105 class LIBPBD_API BlinkTimer : public Timer
106 {
107 public:
108
109         BlinkTimer (unsigned int interval,
110                     const Glib::RefPtr<Glib::MainContext>& main_context = Glib::MainContext::get_default());
111
112
113         sigc::connection connect (const sigc::slot<void, bool>& slot);
114
115         virtual unsigned int connection_count () const
116         { return m_blink_signal.size (); }
117
118 protected:
119
120         virtual bool on_elapsed ();
121
122         sigc::signal<void, bool> m_blink_signal;
123
124 };
125
126 } // namespace PBD
127
128 #endif // __libpbd_timer_h__