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)
34 Timer::_timeout_handler (void *data)
36 Timer *const timer = static_cast<Timer*>(data);
37 return timer->timeout_handler();
41 Timer::get_interval () const
43 return m_timeout_interval;
47 Timer::set_interval (unsigned int new_interval)
49 if (new_interval == m_timeout_interval) return;
52 m_timeout_interval = new_interval;
57 * We don't use Glibmm::TimeoutSource::create() here as contrary
58 * to the documentation, SignalTimeout::connect and manually
59 * adding a TimeoutSource to a GMainContext are not equivalent.
61 * SignalTimeout::connect is the equivalent of g_timeout_add in
62 * terms off callback timing but TimeoutSource tries to adjust
63 * the timeout based on the time elapsed since the last timeout.
65 * On Windows with a high frequency timeout(40ms) this causes a
66 * small but noticable increase in CPU Usage.
71 if (m_timeout_source) return;
73 m_timeout_source = g_timeout_source_new (m_timeout_interval);
75 #if 0 // support priorites?
76 if(priority != G_PRIORITY_DEFAULT)
77 g_source_set_priority(source, priority);
80 g_source_set_callback (m_timeout_source, &Timer::_timeout_handler, this, NULL);
82 g_source_attach (m_timeout_source, m_main_context->gobj());
83 // GMainContext also holds a reference
89 if (m_timeout_source) {
90 g_source_destroy (m_timeout_source);
91 g_source_unref (m_timeout_source);
92 m_timeout_source = NULL;
97 Timer::timeout_handler()
102 StandardTimer::StandardTimer(unsigned int interval,
103 const Glib::RefPtr<Glib::MainContext>& main_context)
104 : Timer(interval, main_context)
108 StandardTimer::connect(const sigc::slot<void>& slot)
110 if(m_signal.size() == 0) { start(); }
112 return m_signal.connect(slot);
116 StandardTimer::on_elapsed()
118 if(m_signal.size() == 0)
128 BlinkTimer::BlinkTimer(unsigned int interval,
129 const Glib::RefPtr<Glib::MainContext>& main_context)
130 : Timer(interval, main_context)
134 BlinkTimer::connect(const sigc::slot<void, bool>& slot)
136 if(m_blink_signal.size() == 0) { start(); }
138 return m_blink_signal.connect(slot);
142 BlinkTimer::on_elapsed()
144 static bool blink_on = false;
146 if(m_blink_signal.size() == 0)
152 m_blink_signal(blink_on = !blink_on);