fix merge conflict with master
[ardour.git] / libs / pbd / base_ui.cc
1 /*
2     Copyright (C) 2000-2007 Paul Davis 
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 #include <cstring>
21 #include <stdint.h>
22 #ifdef COMPILER_MSVC
23 #include <io.h>      // Microsoft's nearest equivalent to <unistd.h>
24 #else
25 #include <unistd.h>
26 #endif
27 #include <fcntl.h>
28 #include <cerrno>
29 #include <cstring>
30
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"
37
38 #include "i18n.h"
39
40 #include "pbd/debug.h"
41
42 using namespace std;
43 using namespace PBD;
44 using namespace Glib;
45         
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();
49
50 BaseUI::BaseUI (const string& str)
51         : m_context(MainContext::get_default())
52         , run_loop_thread (0)
53         , _name (str)
54 #ifndef PLATFORM_WINDOWS
55         , request_channel (true)
56 #endif
57 {
58         base_ui_instance = this;
59
60 #ifndef PLATFORM_WINDOWS
61         request_channel.ios()->connect (sigc::mem_fun (*this, &BaseUI::request_handler));
62 #endif
63
64         /* derived class must set _ok */
65 }
66
67 BaseUI::~BaseUI()
68 {
69 }
70
71 BaseUI::RequestType
72 BaseUI::new_request_type ()
73 {
74         RequestType rt;
75
76         /* XXX catch out-of-range */
77
78         rt = RequestType (rt_bit);
79         rt_bit <<= 1;
80
81         return rt;
82 }
83
84 void
85 BaseUI::main_thread ()
86 {
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);
89         thread_init ();
90         _main_loop->get_context()->signal_idle().connect (sigc::mem_fun (*this, &BaseUI::signal_running));
91         _main_loop->run ();
92 }
93
94 bool
95 BaseUI::signal_running ()
96 {
97         Glib::Threads::Mutex::Lock lm (_run_lock);
98         _running.signal ();
99         
100         return false; // don't call it again
101 }
102
103 void
104 BaseUI::run ()
105 {
106         /* to be called by UI's that need/want their own distinct, self-created event loop thread.
107         */
108
109         m_context = MainContext::create();
110         _main_loop = MainLoop::create (m_context);
111         attach_request_source ();
112
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);
116 }
117
118 void
119 BaseUI::quit ()
120 {
121         if (_main_loop && _main_loop->is_running()) {
122                 _main_loop->quit ();
123                 run_loop_thread->join ();
124         }
125 }
126
127 #ifdef PLATFORM_WINDOWS
128 gboolean
129 BaseUI::_request_handler (gpointer data)
130 {
131         BaseUI* ui = static_cast<BaseUI*>(data);
132         return ui->request_handler ();
133 }
134
135 bool
136 BaseUI::request_handler ()
137 {
138         DEBUG_TRACE (DEBUG::EventLoop, "BaseUI::request_handler\n");
139         handle_ui_requests ();
140         // keep calling indefinitely at the timeout interval
141         return true;
142 }
143
144 #else
145 bool
146 BaseUI::request_handler (Glib::IOCondition ioc)
147 {
148         /* check the request pipe */
149
150         if (ioc & ~IO_IN) {
151                 _main_loop->quit ();
152         }
153
154         if (ioc & IO_IN) {
155                 request_channel.drain ();
156                 
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.
159                 */
160
161                 /* handle requests */
162
163                 DEBUG_TRACE (DEBUG::EventLoop, "BaseUI::request_handler\n");
164                 handle_ui_requests ();
165         }
166
167         return true;
168 }
169 #endif
170
171 void
172 BaseUI::signal_new_request ()
173 {
174         DEBUG_TRACE (DEBUG::EventLoop, "BaseUI::signal_new_request\n");
175 #ifdef PLATFORM_WINDOWS
176         // handled in timeout, how to signal...?
177 #else
178         request_channel.wakeup ();
179 #endif
180 }
181
182 /**
183  * This method relies on the caller having already set m_context
184  */
185 void
186 BaseUI::attach_request_source ()
187 {
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());
193 #else
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 ();
197 #endif
198 }