1 /*
2  * Copyright (C) 2000-2016 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2008-2009 David Robillard <d@drobilla.net>
4  * Copyright (C) 2011-2012 Carl Hetherington <carl@carlh.net>
5  * Copyright (C) 2013 John Emmas <john@creativepost.co.uk>
6  * Copyright (C) 2015-2017 Robin Gareus <robin@gareus.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22 
23 #include <cstring>
24 #include <stdint.h>
25 #ifdef COMPILER_MSVC
26 #include <io.h>      // Microsoft's nearest equivalent to <unistd.h>
27 #else
28 #include <unistd.h>
29 #endif
30 #include <fcntl.h>
31 #include <cerrno>
32 #include <cstring>
33 
34 #include <pthread.h>
35 #include <sched.h>
36 
37 #include "pbd/base_ui.h"
38 #include "pbd/debug.h"
39 #include "pbd/pthread_utils.h"
40 #include "pbd/error.h"
41 #include "pbd/compose.h"
42 #include "pbd/failed_constructor.h"
43 
44 #include "pbd/i18n.h"
45 
46 #include "pbd/debug.h"
47 
48 using namespace std;
49 using namespace PBD;
50 using namespace Glib;
51 
52 uint64_t BaseUI::rt_bit = 1;
53 int BaseUI::_thread_priority = PBD_RT_PRI_PROC - 1;
54 
55 BaseUI::RequestType BaseUI::CallSlot = BaseUI::new_request_type();
56 BaseUI::RequestType BaseUI::Quit = BaseUI::new_request_type();
57 
BaseUI(const string & loop_name)58 BaseUI::BaseUI (const string& loop_name)
59 	: EventLoop (loop_name)
60 	, m_context(MainContext::get_default())
61 	, run_loop_thread (0)
62 	, request_channel (true)
63 {
64 	base_ui_instance = this;
65 	request_channel.set_receive_handler (sigc::mem_fun (*this, &BaseUI::request_handler));
66 
67 	/* derived class must set _ok */
68 }
69 
~BaseUI()70 BaseUI::~BaseUI()
71 {
72 }
73 
74 BaseUI::RequestType
new_request_type()75 BaseUI::new_request_type ()
76 {
77 	RequestType rt;
78 
79 	/* XXX catch out-of-range */
80 
81 	rt = RequestType (rt_bit);
82 	rt_bit <<= 1;
83 
84 	return rt;
85 }
86 
87 int
set_thread_priority() const88 BaseUI::set_thread_priority () const
89 {
90 	return pbd_set_thread_priority (pthread_self(), PBD_SCHED_FIFO, _thread_priority);
91 }
92 
93 void
main_thread()94 BaseUI::main_thread ()
95 {
96 	pthread_set_name (string_compose ("UI:%1", event_loop_name ()).c_str ());
97 	DEBUG_TRACE (DEBUG::EventLoop, string_compose ("%1: event loop running in thread %2\n", event_loop_name(), pthread_name()));
98 	set_event_loop_for_thread (this);
99 	thread_init ();
100 	_main_loop->get_context()->signal_idle().connect (sigc::mem_fun (*this, &BaseUI::signal_running));
101 	_main_loop->run ();
102 }
103 
104 bool
signal_running()105 BaseUI::signal_running ()
106 {
107 	Glib::Threads::Mutex::Lock lm (_run_lock);
108 	_running.signal ();
109 
110 	return false; // don't call it again
111 }
112 
113 void
run()114 BaseUI::run ()
115 {
116 	/* to be called by UI's that need/want their own distinct, self-created event loop thread.
117 	*/
118 
119 	m_context = MainContext::create();
120 	_main_loop = MainLoop::create (m_context);
121 	attach_request_source ();
122 
123 	Glib::Threads::Mutex::Lock lm (_run_lock);
124 	run_loop_thread = Glib::Threads::Thread::create (mem_fun (*this, &BaseUI::main_thread));
125 	_running.wait (_run_lock);
126 }
127 
128 void
quit()129 BaseUI::quit ()
130 {
131 	if (_main_loop && _main_loop->is_running()) {
132 		_main_loop->quit ();
133 		run_loop_thread->join ();
134 	}
135 }
136 
137 bool
request_handler(Glib::IOCondition ioc)138 BaseUI::request_handler (Glib::IOCondition ioc)
139 {
140 	/* check the request pipe */
141 
142 	if (ioc & IO_IN) {
143 		request_channel.drain ();
144 
145 		/* there may been an error. we'd rather handle requests first,
146 		   and then get IO_HUP or IO_ERR on the next loop.
147 		*/
148 
149 		/* handle requests */
150 
151 		DEBUG_TRACE (DEBUG::EventLoop, string_compose ("%1: request handler\n", event_loop_name()));
152 		handle_ui_requests ();
153 	}
154 
155 	if (ioc & ~(IO_IN|IO_PRI)) {
156 		_main_loop->quit ();
157 	}
158 
159 	return true;
160 }
161 
162 void
signal_new_request()163 BaseUI::signal_new_request ()
164 {
165 	DEBUG_TRACE (DEBUG::EventLoop, string_compose ("%1: signal_new_request\n", event_loop_name()));
166 	request_channel.wakeup ();
167 }
168 
169 /**
170  * This method relies on the caller having already set m_context
171  */
172 void
attach_request_source()173 BaseUI::attach_request_source ()
174 {
175 	DEBUG_TRACE (DEBUG::EventLoop, string_compose ("%1: attach request source\n", event_loop_name()));
176 	request_channel.attach (m_context);
177 }
178