1 /*
2  * Copyright (C) 2016-2017 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2016-2019 Robin Gareus <robin@gareus.org>
4  * Copyright (C) 2018 Len Ovens <len@ovenwerks.net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20 
21 #include <sstream>
22 #include <typeinfo>
23 
24 #include <cassert>
25 
26 #include "pbd/debug.h"
27 #include "pbd/enum_convert.h"
28 #include "pbd/enumwriter.h"
29 #include "pbd/error.h"
30 #include "pbd/failed_constructor.h"
31 #include "pbd/xml++.h"
32 
33 #include "ardour/presentation_info.h"
34 #include "ardour/selection.h"
35 
36 #include "pbd/i18n.h"
37 
38 namespace PBD {
39 	DEFINE_ENUM_CONVERT(ARDOUR::PresentationInfo::Flag);
40 }
41 
42 using namespace ARDOUR;
43 using namespace PBD;
44 using std::string;
45 
46 string PresentationInfo::state_node_name = X_("PresentationInfo");
47 
48 PBD::Signal1<void,PropertyChange const &> PresentationInfo::Change;
49 Glib::Threads::Mutex PresentationInfo::static_signal_lock;
50 GATOMIC_QUAL gint PresentationInfo::_change_signal_suspended = 0;
51 PBD::PropertyChange PresentationInfo::_pending_static_changes;
52 int PresentationInfo::selection_counter= 0;
53 
54 namespace ARDOUR {
55 	namespace Properties {
56 		PBD::PropertyDescriptor<bool>     selected;
57 		PBD::PropertyDescriptor<uint32_t> order;
58 		PBD::PropertyDescriptor<uint32_t> color;
59 	}
60 }
61 
62 void
suspend_change_signal()63 PresentationInfo::suspend_change_signal ()
64 {
65 	g_atomic_int_add (&_change_signal_suspended, 1);
66 }
67 
68 void
unsuspend_change_signal()69 PresentationInfo::unsuspend_change_signal ()
70 {
71 	Glib::Threads::Mutex::Lock lm (static_signal_lock);
72 
73 	if (g_atomic_int_get (&_change_signal_suspended) == 1) {
74 
75 		/* atomically grab currently pending flags */
76 
77 		PropertyChange pc = _pending_static_changes;
78 		_pending_static_changes.clear ();
79 
80 		if (!pc.empty()) {
81 
82 			/* emit the signal with further emissions still blocked
83 			 * by _change_signal_suspended, but not by the lock.
84 			 *
85 			 * This means that if the handlers modify other PI
86 			 * states, the signal for that won't be sent while they
87 			 * are handling the current signal.
88 			 */
89 			lm.release ();
90 			Change (pc); /* EMIT SIGNAL */
91 			lm.acquire ();
92 		}
93 	}
94 
95 	g_atomic_int_add (&_change_signal_suspended, -1);
96 }
97 
98 void
send_static_change(const PropertyChange & what_changed)99 PresentationInfo::send_static_change (const PropertyChange& what_changed)
100 {
101 	if (what_changed.empty()) {
102 		return;
103 	}
104 
105 
106 	if (g_atomic_int_get (&_change_signal_suspended)) {
107 		Glib::Threads::Mutex::Lock lm (static_signal_lock);
108 		_pending_static_changes.add (what_changed);
109 		return;
110 	}
111 
112 	Change (what_changed);
113 }
114 
115 const PresentationInfo::order_t PresentationInfo::max_order = UINT32_MAX;
116 const PresentationInfo::Flag PresentationInfo::Bus = PresentationInfo::Flag (PresentationInfo::AudioBus|PresentationInfo::MidiBus);
117 const PresentationInfo::Flag PresentationInfo::Track = PresentationInfo::Flag (PresentationInfo::AudioTrack|PresentationInfo::MidiTrack);
118 const PresentationInfo::Flag PresentationInfo::Route = PresentationInfo::Flag (PresentationInfo::Bus|PresentationInfo::Track);
119 const PresentationInfo::Flag PresentationInfo::AllRoutes = PresentationInfo::Flag (PresentationInfo::Route|PresentationInfo::MasterOut|PresentationInfo::MonitorOut|PresentationInfo::FoldbackBus);
120 const PresentationInfo::Flag PresentationInfo::MixerRoutes = PresentationInfo::Flag (PresentationInfo::Route|PresentationInfo::MasterOut|PresentationInfo::MonitorOut);
121 const PresentationInfo::Flag PresentationInfo::AllStripables = PresentationInfo::Flag (PresentationInfo::AllRoutes|PresentationInfo::VCA);
122 const PresentationInfo::Flag PresentationInfo::MixerStripables = PresentationInfo::Flag (PresentationInfo::MixerRoutes|PresentationInfo::VCA);
123 
124 void
make_property_quarks()125 PresentationInfo::make_property_quarks ()
126 {
127 	Properties::selected.property_id = g_quark_from_static_string (X_("selected"));
128 	DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for selected = %1\n", Properties::selected.property_id));
129 	Properties::color.property_id = g_quark_from_static_string (X_("color"));
130 	DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for color = %1\n", Properties::color.property_id));
131 	Properties::order.property_id = g_quark_from_static_string (X_("order"));
132 	DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for order = %1\n", Properties::order.property_id));
133 }
134 
PresentationInfo(Flag f)135 PresentationInfo::PresentationInfo (Flag f)
136 	: _order (0)
137 	, _flags (Flag (f & ~OrderSet))
138 	, _color (0)
139 {
140 	/* OrderSet is not set */
141 }
142 
PresentationInfo(order_t o,Flag f)143 PresentationInfo::PresentationInfo (order_t o, Flag f)
144 	: _order (o)
145 	, _flags (Flag (f | OrderSet))
146 	, _color (0)
147 {
148 	/* OrderSet is set */
149 }
PresentationInfo(PresentationInfo const & other)150 PresentationInfo::PresentationInfo (PresentationInfo const& other)
151 	: PBD::Stateful ()
152 	, _order (other.order())
153 	, _flags (other.flags())
154 	, _color (other.color())
155 {
156 }
157 
158 XMLNode&
get_state()159 PresentationInfo::get_state ()
160 {
161 	XMLNode* node = new XMLNode (state_node_name);
162 	node->set_property ("order", _order);
163 	node->set_property ("flags", _flags);
164 	node->set_property ("color", _color);
165 
166 	return *node;
167 }
168 
169 int
set_state(XMLNode const & node,int)170 PresentationInfo::set_state (XMLNode const& node, int /* version */)
171 {
172 	if (node.name() != state_node_name) {
173 		return -1;
174 	}
175 
176 	PropertyChange pc;
177 
178 	order_t o;
179 	if (node.get_property (X_("order"), o)) {
180 		if (o != _order) {
181 			pc.add (Properties::order);
182 			_order = o;
183 		}
184 		_order = o; // huh?
185 	}
186 
187 	Flag f;
188 	if (node.get_property (X_("flags"), f)) {
189 		if ((f&Hidden) != (_flags&Hidden)) {
190 			pc.add (Properties::hidden);
191 		}
192 		_flags = f;
193 	}
194 
195 	color_t c;
196 	if (node.get_property (X_("color"), c)) {
197 		if (c != _color) {
198 			pc.add (Properties::color);
199 			_color = c;
200 		}
201 	}
202 
203 	send_change (PropertyChange (pc));
204 
205 	return 0;
206 
207 }
208 
209 PresentationInfo::Flag
get_flags(XMLNode const & node)210 PresentationInfo::get_flags (XMLNode const& node)
211 {
212 	XMLNodeList nlist = node.children ();
213 
214 	for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter){
215 		XMLNode* child = *niter;
216 
217 		if (child->name() == PresentationInfo::state_node_name) {
218 			Flag f;
219 			if (child->get_property (X_("flags"), f)) {
220 				return f;
221 			}
222 		}
223 	}
224 	return Flag (0);
225 }
226 
227 PresentationInfo::Flag
get_flags2X3X(XMLNode const & node)228 PresentationInfo::get_flags2X3X (XMLNode const& node)
229 {
230 	/* Ardour 2.x and session-format 300x used <Route flags="MasterOut" .. /> */
231 	Flag f;
232 	if (node.get_property (X_("flags"), f)) {
233 		return f;
234 	}
235 	return get_flags (node);
236 }
237 
238 void
set_color(PresentationInfo::color_t c)239 PresentationInfo::set_color (PresentationInfo::color_t c)
240 {
241 	if (c != _color) {
242 		_color = c;
243 		send_change (PropertyChange (Properties::color));
244 		send_static_change (PropertyChange (Properties::color));
245 	}
246 }
247 
248 bool
color_set() const249 PresentationInfo::color_set () const
250 {
251 	/* all RGBA values zero? not set.
252 	 *
253 	 * this is heuristic, but it is fairly realistic. who will ever set
254 	 * a color to completely transparent black? only the constructor ..
255 	 */
256 	return _color != 0;
257 }
258 
259 void
set_hidden(bool yn)260 PresentationInfo::set_hidden (bool yn)
261 {
262 	if (yn != hidden()) {
263 
264 		if (yn) {
265 			_flags = Flag (_flags | Hidden);
266 		} else {
267 			_flags = Flag (_flags & ~Hidden);
268 		}
269 
270 		send_change (PropertyChange (Properties::hidden));
271 		send_static_change (PropertyChange (Properties::hidden));
272 	}
273 }
274 
275 void
set_order(order_t order)276 PresentationInfo::set_order (order_t order)
277 {
278 	_flags = Flag (_flags|OrderSet);
279 
280 	if (order != _order) {
281 		_order = order;
282 		send_change (PropertyChange (Properties::order));
283 		send_static_change (PropertyChange (Properties::order));
284 	}
285 }
286 
287 PresentationInfo&
operator =(PresentationInfo const & other)288 PresentationInfo::operator= (PresentationInfo const& other)
289 {
290 	if (this != &other) {
291 		_order = other.order();
292 		_flags = other.flags();
293 		_color = other.color();
294 	}
295 
296 	return *this;
297 }
298 
299 std::ostream&
operator <<(std::ostream & o,ARDOUR::PresentationInfo const & pi)300 operator<<(std::ostream& o, ARDOUR::PresentationInfo const& pi)
301 {
302 	return o << pi.order() << '/' << enum_2_string (pi.flags()) << '/' << pi.color();
303 }
304