restore rec-enable, solo & mute functionality; remove all Global*Command cruft; reimp...
[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 BaseUI::RequestType BaseUI::Quit = BaseUI::new_request_type();
41
42 BaseUI::BaseUI (const string& str)
43         : run_loop_thread (0)
44         , _name (str)
45 {
46         base_ui_instance = this;
47
48         request_channel.ios()->connect (sigc::mem_fun (*this, &BaseUI::request_handler));
49
50         /* derived class must set _ok */
51 }
52
53 BaseUI::~BaseUI()
54 {
55 }
56
57 BaseUI::RequestType
58 BaseUI::new_request_type ()
59 {
60         RequestType rt;
61
62         /* XXX catch out-of-range */
63
64         rt = RequestType (rt_bit);
65         rt_bit <<= 1;
66
67         return rt;
68 }
69
70 void
71 BaseUI::main_thread ()
72 {
73         set_ui_for_thread (this);
74         thread_init ();
75         _main_loop->run ();
76 }
77
78 void
79 BaseUI::run ()
80 {
81         /* to be called by UI's that need/want their own distinct, self-created event loop thread.
82         */
83
84         _main_loop = MainLoop::create (MainContext::create());
85         request_channel.ios()->attach (_main_loop->get_context());
86
87         /* glibmm hack - drop the refptr to the IOSource now before it can hurt */
88         request_channel.drop_ios ();
89
90         run_loop_thread = Thread::create (mem_fun (*this, &BaseUI::main_thread), true);
91 }
92
93 void
94 BaseUI::quit ()
95 {
96         if (_main_loop->is_running()) {
97                 _main_loop->quit ();
98                 run_loop_thread->join ();
99         }
100 }
101
102 bool
103 BaseUI::request_handler (Glib::IOCondition ioc)
104 {
105         /* check the transport request pipe */
106
107         if (ioc & ~IO_IN) {
108                 _main_loop->quit ();
109         }
110
111         if (ioc & IO_IN) {
112                 request_channel.drain ();
113                 
114                 /* there may been an error. we'd rather handle requests first,
115                    and then get IO_HUP or IO_ERR on the next loop.
116                 */
117
118                 /* handle requests */
119
120                 handle_ui_requests ();
121         }
122
123         return true;
124 }
125