c246a50656701ecb2022050396a801d680f322c9
[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 #include <unistd.h>
23 #include <fcntl.h>
24 #include <cerrno>
25 #include <cstring>
26
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"
33
34 #include "i18n.h"
35
36 using namespace std;
37 using namespace PBD;
38 using namespace Glib;
39         
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();
43
44 BaseUI::BaseUI (const string& str)
45         : request_channel (true)
46         , run_loop_thread (0)
47         , _name (str)
48 {
49         base_ui_instance = this;
50
51         request_channel.ios()->connect (sigc::mem_fun (*this, &BaseUI::request_handler));
52
53         /* derived class must set _ok */
54 }
55
56 BaseUI::~BaseUI()
57 {
58 }
59
60 BaseUI::RequestType
61 BaseUI::new_request_type ()
62 {
63         RequestType rt;
64
65         /* XXX catch out-of-range */
66
67         rt = RequestType (rt_bit);
68         rt_bit <<= 1;
69
70         return rt;
71 }
72
73 void
74 BaseUI::main_thread ()
75 {
76         DEBUG_TRACE (DEBUG::EventLoop, string_compose ("%1: event loop running in thread %2\n", name(), pthread_self()));
77         std::cerr << string_compose ("%1: event loop running in thread %2\n", name(), pthread_self());
78         set_event_loop_for_thread (this);
79         thread_init ();
80         _main_loop->get_context()->signal_idle().connect (sigc::mem_fun (*this, &BaseUI::signal_running));
81         _main_loop->run ();
82 }
83
84 bool
85 BaseUI::signal_running ()
86 {
87         Glib::Mutex::Lock lm (_run_lock);
88         _running.signal ();
89         
90         return false; // don't call it again
91 }
92
93 void
94 BaseUI::run ()
95 {
96         /* to be called by UI's that need/want their own distinct, self-created event loop thread.
97         */
98
99         _main_loop = MainLoop::create (MainContext::create());
100         request_channel.ios()->attach (_main_loop->get_context());
101
102         /* glibmm hack - drop the refptr to the IOSource now before it can hurt */
103         request_channel.drop_ios ();
104
105         Glib::Mutex::Lock lm (_run_lock);
106         run_loop_thread = Thread::create (mem_fun (*this, &BaseUI::main_thread), true);
107         std::cerr << "wait for " << name() << " thread to start\n";
108         _running.wait (_run_lock);
109         std::cerr << "\tthread now running\n";
110 }
111
112 void
113 BaseUI::quit ()
114 {
115         if (_main_loop && _main_loop->is_running()) {
116                 _main_loop->quit ();
117                 run_loop_thread->join ();
118         }
119 }
120
121 bool
122 BaseUI::request_handler (Glib::IOCondition ioc)
123 {
124         /* check the request pipe */
125
126         if (ioc & ~IO_IN) {
127                 _main_loop->quit ();
128         }
129
130         if (ioc & IO_IN) {
131                 request_channel.drain ();
132                 
133                 /* there may been an error. we'd rather handle requests first,
134                    and then get IO_HUP or IO_ERR on the next loop.
135                 */
136
137                 /* handle requests */
138
139                 handle_ui_requests ();
140         }
141
142         return true;
143 }
144