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