ardour
base_ui.cc
Go to the documentation of this file.
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 "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;
49 
50 BaseUI::BaseUI (const string& str)
51  : m_context(MainContext::get_default())
52  , run_loop_thread (0)
53  , _name (str)
54  , request_channel (true)
55 {
56  base_ui_instance = this;
58 
59  /* derived class must set _ok */
60 }
61 
63 {
64 }
65 
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
81 {
82  DEBUG_TRACE (DEBUG::EventLoop, string_compose ("%1: event loop running in thread %2\n", name(), pthread_name()));
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
91 {
93  _running.signal ();
94 
95  return false; // don't call it again
96 }
97 
98 void
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);
107 
109  run_loop_thread = Glib::Threads::Thread::create (mem_fun (*this, &BaseUI::main_thread));
110  _running.wait (_run_lock);
111 }
112 
113 void
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) {
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, "BaseUI::request_handler\n");
142  }
143 
144  return true;
145 }
146 
147 void
149 {
150  DEBUG_TRACE (DEBUG::EventLoop, "BaseUI::signal_new_request\n");
152 }
153 
157 void
159 {
160  DEBUG_TRACE (DEBUG::EventLoop, "BaseUI::attach_request_source\n");
162 }
static RequestType new_request_type()
Definition: base_ui.cc:67
static RequestType CallSlot
Definition: base_ui.h:62
LIBPBD_API const char * pthread_name()
Glib::Threads::Cond _running
Definition: base_ui.h:81
std::string name() const
Definition: base_ui.h:57
Glib::RefPtr< Glib::MainContext > m_context
Definition: base_ui.h:78
bool signal_running()
Definition: base_ui.cc:90
virtual void thread_init()
Definition: base_ui.h:94
static void set_event_loop_for_thread(EventLoop *ui)
Definition: event_loop.cc:36
void quit()
Definition: base_ui.cc:114
bool request_handler(Glib::IOCondition)
Definition: base_ui.cc:123
Definition: Beats.hpp:239
static uint64_t rt_bit
Definition: base_ui.h:115
BaseUI * base_ui_instance
Definition: base_ui.h:111
CrossThreadChannel request_channel
Definition: base_ui.h:113
Glib::Threads::Mutex _run_lock
Definition: base_ui.h:80
BaseUI(const std::string &name)
Definition: base_ui.cc:50
void run()
Definition: base_ui.cc:99
#define DEBUG_TRACE(bits, str)
Definition: debug.h:55
void signal_new_request()
Definition: base_ui.cc:148
virtual ~BaseUI()
Definition: base_ui.cc:62
void attach_request_source()
Definition: base_ui.cc:158
void main_thread()
Definition: base_ui.cc:80
static RequestType Quit
Definition: base_ui.h:63
Definition: debug.h:30
Glib::Threads::Thread * run_loop_thread
Definition: base_ui.h:79
virtual void handle_ui_requests()=0
LIBPBD_API uint64_t EventLoop
Definition: debug.cc:50
void attach(Glib::RefPtr< Glib::MainContext >)
Definition: crossthread.cc:59
void set_receive_handler(sigc::slot< bool, Glib::IOCondition > s)
Definition: crossthread.cc:53
std::string string_compose(const std::string &fmt, const T1 &o1)
Definition: compose.h:208
Glib::RefPtr< Glib::MainLoop > _main_loop
Definition: base_ui.h:77