major design changes: use glib event loop for MIDI thread/UI; rework design of BaseUI...
[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/error.h"
29 #include "pbd/compose.h"
30 #include "pbd/failed_constructor.h"
31
32 #include "i18n.h"
33
34 using namespace std;
35 using namespace PBD;
36 using namespace Glib;
37         
38 uint64_t BaseUI::rt_bit = 1;
39 BaseUI::RequestType BaseUI::CallSlot = BaseUI::new_request_type();
40
41 BaseUI::BaseUI (const string& str)
42         : run_loop_thread (0)
43         , _name (str)
44 {
45         base_ui_instance = this;
46
47         request_channel.ios()->connect (sigc::mem_fun (*this, &BaseUI::request_handler));
48
49         /* derived class must set _ok */
50 }
51
52 BaseUI::~BaseUI()
53 {
54 }
55
56 BaseUI::RequestType
57 BaseUI::new_request_type ()
58 {
59         RequestType rt;
60
61         /* XXX catch out-of-range */
62
63         rt = RequestType (rt_bit);
64         rt_bit <<= 1;
65
66         return rt;
67 }
68
69 void
70 BaseUI::main_thread ()
71 {
72         thread_init ();
73         _main_loop->run ();
74 }
75
76 void
77 BaseUI::run ()
78 {
79         /* to be called by UI's that need/want their own distinct, self-created event loop thread.
80            Derived classes should have set up a handler for IO on request_channel.ios()
81         */
82
83         _main_loop = MainLoop::create (MainContext::create());
84         request_channel.ios()->attach (_main_loop->get_context());
85         run_loop_thread = Thread::create (mem_fun (*this, &BaseUI::main_thread), true);
86 }
87
88 void
89 BaseUI::quit ()
90 {
91         _main_loop->quit ();
92         run_loop_thread->join ();
93 }
94
95 bool
96 BaseUI::request_handler (Glib::IOCondition ioc)
97 {
98         /* check the transport request pipe */
99
100         if (ioc & ~IO_IN) {
101                 _main_loop->quit ();
102         }
103
104         if (ioc & IO_IN) {
105                 request_channel.drain ();
106                 
107                 /* there may been an error. we'd rather handle requests first,
108                    and then get IO_HUP or IO_ERR on the next loop.
109                 */
110
111                 /* handle requests */
112
113                 handle_ui_requests ();
114         }
115
116         return true;
117 }
118