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.
27 #include "pbd/base_ui.h"
28 #include "pbd/debug.h"
29 #include "pbd/pthread_utils.h"
30 #include "pbd/error.h"
31 #include "pbd/compose.h"
32 #include "pbd/failed_constructor.h"
40 uint64_t BaseUI::rt_bit = 1;
41 BaseUI::RequestType BaseUI::CallSlot = BaseUI::new_request_type();
42 BaseUI::RequestType BaseUI::Quit = BaseUI::new_request_type();
44 BaseUI::BaseUI (const string& str)
45 : request_channel (true)
49 base_ui_instance = this;
51 request_channel.ios()->connect (sigc::mem_fun (*this, &BaseUI::request_handler));
53 /* derived class must set _ok */
61 BaseUI::new_request_type ()
65 /* XXX catch out-of-range */
67 rt = RequestType (rt_bit);
74 BaseUI::main_thread ()
76 DEBUG_TRACE (DEBUG::EventLoop, string_compose ("%1: event loop running in thread %2\n", name(), pthread_self()));
77 set_event_loop_for_thread (this);
79 _main_loop->get_context()->signal_idle().connect (sigc::mem_fun (*this, &BaseUI::signal_running));
84 BaseUI::signal_running ()
86 Glib::Threads::Mutex::Lock lm (_run_lock);
89 return false; // don't call it again
95 /* to be called by UI's that need/want their own distinct, self-created event loop thread.
98 _main_loop = MainLoop::create (MainContext::create());
99 request_channel.ios()->attach (_main_loop->get_context());
101 /* glibmm hack - drop the refptr to the IOSource now before it can hurt */
102 request_channel.drop_ios ();
104 Glib::Threads::Mutex::Lock lm (_run_lock);
105 run_loop_thread = Glib::Threads::Thread::create (mem_fun (*this, &BaseUI::main_thread));
106 _running.wait (_run_lock);
112 if (_main_loop && _main_loop->is_running()) {
114 run_loop_thread->join ();
119 BaseUI::request_handler (Glib::IOCondition ioc)
121 /* check the request pipe */
128 request_channel.drain ();
130 /* there may been an error. we'd rather handle requests first,
131 and then get IO_HUP or IO_ERR on the next loop.
134 /* handle requests */
136 handle_ui_requests ();