enough with umpteen "i18n.h" files. Consolidate on pbd/i18n.h
[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 "pbd/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& loop_name)
51         : EventLoop (loop_name)
52         , m_context(MainContext::get_default())
53         , run_loop_thread (0)
54         , request_channel (true)
55 {
56         base_ui_instance = this;
57         request_channel.set_receive_handler (sigc::mem_fun (*this, &BaseUI::request_handler));
58
59         /* derived class must set _ok */
60 }
61
62 BaseUI::~BaseUI()
63 {
64 }
65
66 BaseUI::RequestType
67 BaseUI::new_request_type ()
68 {
69         RequestType rt;
70
71         /* XXX catch out-of-range */
72
73         rt = RequestType (rt_bit);
74         rt_bit <<= 1;
75
76         return rt;
77 }
78
79 void
80 BaseUI::main_thread ()
81 {
82         DEBUG_TRACE (DEBUG::EventLoop, string_compose ("%1: event loop running in thread %2\n", event_loop_name(), pthread_name()));
83         set_event_loop_for_thread (this);
84         thread_init ();
85         _main_loop->get_context()->signal_idle().connect (sigc::mem_fun (*this, &BaseUI::signal_running));
86         _main_loop->run ();
87 }
88
89 bool
90 BaseUI::signal_running ()
91 {
92         Glib::Threads::Mutex::Lock lm (_run_lock);
93         _running.signal ();
94
95         return false; // don't call it again
96 }
97
98 void
99 BaseUI::run ()
100 {
101         /* to be called by UI's that need/want their own distinct, self-created event loop thread.
102         */
103
104         m_context = MainContext::create();
105         _main_loop = MainLoop::create (m_context);
106         attach_request_source ();
107
108         Glib::Threads::Mutex::Lock lm (_run_lock);
109         run_loop_thread = Glib::Threads::Thread::create (mem_fun (*this, &BaseUI::main_thread));
110         _running.wait (_run_lock);
111 }
112
113 void
114 BaseUI::quit ()
115 {
116         if (_main_loop && _main_loop->is_running()) {
117                 _main_loop->quit ();
118                 run_loop_thread->join ();
119         }
120 }
121
122 bool
123 BaseUI::request_handler (Glib::IOCondition ioc)
124 {
125         /* check the request pipe */
126
127         if (ioc & ~IO_IN) {
128                 _main_loop->quit ();
129         }
130
131         if (ioc & IO_IN) {
132                 request_channel.drain ();
133
134                 /* there may been an error. we'd rather handle requests first,
135                    and then get IO_HUP or IO_ERR on the next loop.
136                 */
137
138                 /* handle requests */
139
140                 DEBUG_TRACE (DEBUG::EventLoop, string_compose ("%1: request handler\n", event_loop_name()));
141                 handle_ui_requests ();
142         }
143
144         return true;
145 }
146
147 void
148 BaseUI::signal_new_request ()
149 {
150         DEBUG_TRACE (DEBUG::EventLoop, string_compose ("%1: signal_new_request\n", event_loop_name()));
151         request_channel.wakeup ();
152 }
153
154 /**
155  * This method relies on the caller having already set m_context
156  */
157 void
158 BaseUI::attach_request_source ()
159 {
160         DEBUG_TRACE (DEBUG::EventLoop, string_compose ("%1: attach request source\n", event_loop_name()));
161         request_channel.attach (m_context);
162 }