2 Copyright (C) 2000-2007 Paul Davis
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.
23 #include <io.h> // Microsoft's nearest equivalent to <unistd.h>
31 #include "pbd/base_ui.h"
32 #include "pbd/debug.h"
33 #include "pbd/pthread_utils.h"
34 #include "pbd/error.h"
35 #include "pbd/compose.h"
36 #include "pbd/failed_constructor.h"
40 #include "pbd/debug.h"
46 uint64_t BaseUI::rt_bit = 1;
47 BaseUI::RequestType BaseUI::CallSlot = BaseUI::new_request_type();
48 BaseUI::RequestType BaseUI::Quit = BaseUI::new_request_type();
50 BaseUI::BaseUI (const string& str)
51 : m_context(MainContext::get_default())
54 #ifndef PLATFORM_WINDOWS
55 , request_channel (true)
58 base_ui_instance = this;
60 #ifndef PLATFORM_WINDOWS
61 request_channel.ios()->connect (sigc::mem_fun (*this, &BaseUI::request_handler));
64 /* derived class must set _ok */
72 BaseUI::new_request_type ()
76 /* XXX catch out-of-range */
78 rt = RequestType (rt_bit);
85 BaseUI::main_thread ()
87 DEBUG_TRACE (DEBUG::EventLoop, string_compose ("%1: event loop running in thread %2\n", name(), pthread_name()));
88 set_event_loop_for_thread (this);
90 _main_loop->get_context()->signal_idle().connect (sigc::mem_fun (*this, &BaseUI::signal_running));
95 BaseUI::signal_running ()
97 Glib::Threads::Mutex::Lock lm (_run_lock);
100 return false; // don't call it again
106 /* to be called by UI's that need/want their own distinct, self-created event loop thread.
109 m_context = MainContext::create();
110 _main_loop = MainLoop::create (m_context);
111 attach_request_source ();
113 Glib::Threads::Mutex::Lock lm (_run_lock);
114 run_loop_thread = Glib::Threads::Thread::create (mem_fun (*this, &BaseUI::main_thread));
115 _running.wait (_run_lock);
121 if (_main_loop && _main_loop->is_running()) {
123 run_loop_thread->join ();
127 #ifdef PLATFORM_WINDOWS
129 BaseUI::_request_handler (gpointer data)
131 BaseUI* ui = static_cast<BaseUI*>(data);
132 return ui->request_handler ();
136 BaseUI::request_handler ()
138 DEBUG_TRACE (DEBUG::EventLoop, "BaseUI::request_handler\n");
139 handle_ui_requests ();
140 // keep calling indefinitely at the timeout interval
146 BaseUI::request_handler (Glib::IOCondition ioc)
148 /* check the request pipe */
155 request_channel.drain ();
157 /* there may been an error. we'd rather handle requests first,
158 and then get IO_HUP or IO_ERR on the next loop.
161 /* handle requests */
163 DEBUG_TRACE (DEBUG::EventLoop, "BaseUI::request_handler\n");
164 handle_ui_requests ();
172 BaseUI::signal_new_request ()
174 DEBUG_TRACE (DEBUG::EventLoop, "BaseUI::signal_new_request\n");
175 #ifdef PLATFORM_WINDOWS
176 // handled in timeout, how to signal...?
178 request_channel.wakeup ();
183 * This method relies on the caller having already set m_context
186 BaseUI::attach_request_source ()
188 DEBUG_TRACE (DEBUG::EventLoop, "BaseUI::attach_request_source\n");
189 #ifdef PLATFORM_WINDOWS
190 GSource* request_source = g_timeout_source_new(200);
191 g_source_set_callback (request_source, &BaseUI::_request_handler, this, NULL);
192 g_source_attach (request_source, m_context->gobj());
194 request_channel.ios()->attach (m_context);
195 /* glibmm hack - drop the refptr to the IOSource now before it can hurt */
196 request_channel.drop_ios ();