2 Copyright (C) 2014 Tim Mayberry
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.
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.
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.
20 #include "pbd/timer.h"
24 Timer::Timer (unsigned int interval,
25 const Glib::RefPtr<Glib::MainContext>& main_context)
26 : m_timeout_source(NULL)
27 , m_timeout_interval(interval)
28 , m_main_context(main_context)
35 Timer::_timeout_handler (void *data)
37 Timer *const timer = static_cast<Timer*>(data);
38 return timer->timeout_handler();
42 Timer::get_interval () const
44 return m_timeout_interval;
48 Timer::set_interval (unsigned int new_interval)
50 if (new_interval == m_timeout_interval) return;
53 m_timeout_interval = new_interval;
58 * We don't use Glibmm::TimeoutSource::create() here as contrary
59 * to the documentation, SignalTimeout::connect and manually
60 * adding a TimeoutSource to a GMainContext are not equivalent.
62 * SignalTimeout::connect is the equivalent of g_timeout_add in
63 * terms off callback timing but TimeoutSource tries to adjust
64 * the timeout based on the time elapsed since the last timeout.
66 * On Windows with a high frequency timeout(40ms) this causes a
67 * small but noticable increase in CPU Usage.
72 if (m_timeout_source) return;
74 m_timeout_source = g_timeout_source_new (m_timeout_interval);
76 #if 0 // support priorites?
77 if(priority != G_PRIORITY_DEFAULT)
78 g_source_set_priority(source, priority);
81 g_source_set_callback (m_timeout_source, &Timer::_timeout_handler, this, NULL);
83 g_source_attach (m_timeout_source, m_main_context->gobj());
84 // GMainContext also holds a reference
90 if (m_timeout_source) {
91 g_source_destroy (m_timeout_source);
92 g_source_unref (m_timeout_source);
93 m_timeout_source = NULL;
98 Timer::timeout_handler()
103 StandardTimer::StandardTimer(unsigned int interval,
104 const Glib::RefPtr<Glib::MainContext>& main_context)
105 : Timer(interval, main_context)
109 StandardTimer::connect(const sigc::slot<void>& slot)
111 if(m_signal.size() == 0) { start(); }
113 return m_signal.connect(slot);
117 StandardTimer::on_elapsed()
119 if(m_signal.size() == 0)
131 BlinkTimer::BlinkTimer(unsigned int interval,
132 const Glib::RefPtr<Glib::MainContext>& main_context)
133 : Timer(interval, main_context)
137 BlinkTimer::connect(const sigc::slot<void, bool>& slot)
139 if(m_blink_signal.size() == 0) { start(); }
141 return m_blink_signal.connect(slot);
145 BlinkTimer::on_elapsed()
147 static bool blink_on = false;
149 if(m_blink_signal.size() == 0)
156 m_blink_signal(blink_on = !blink_on);