change MidiPlaylist::dump() into ::render(); change type of initial argument
[ardour.git] / libs / ardour / presentation_info.cc
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/stacktrace.h"
32 #include "pbd/xml++.h"
33
34 #include "ardour/presentation_info.h"
35 #include "ardour/selection.h"
36
37 #include "pbd/i18n.h"
38
39 namespace PBD {
40         DEFINE_ENUM_CONVERT(ARDOUR::PresentationInfo::Flag);
41 }
42
43 using namespace ARDOUR;
44 using namespace PBD;
45 using std::string;
46
47 string PresentationInfo::state_node_name = X_("PresentationInfo");
48
49 PBD::Signal1<void,PropertyChange const &> PresentationInfo::Change;
50 Glib::Threads::Mutex PresentationInfo::static_signal_lock;
51 int PresentationInfo::_change_signal_suspended = 0;
52 PBD::PropertyChange PresentationInfo::_pending_static_changes;
53 int PresentationInfo::selection_counter= 0;
54
55 namespace ARDOUR {
56         namespace Properties {
57                 PBD::PropertyDescriptor<bool>     selected;
58                 PBD::PropertyDescriptor<uint32_t> order;
59                 PBD::PropertyDescriptor<uint32_t> color;
60         }
61 }
62
63 void
64 PresentationInfo::suspend_change_signal ()
65 {
66         g_atomic_int_add (&_change_signal_suspended, 1);
67 }
68
69 void
70 PresentationInfo::unsuspend_change_signal ()
71 {
72         Glib::Threads::Mutex::Lock lm (static_signal_lock);
73
74         if (g_atomic_int_get (const_cast<gint*> (&_change_signal_suspended)) == 1) {
75
76                 /* atomically grab currently pending flags */
77
78                 PropertyChange pc = _pending_static_changes;
79                 _pending_static_changes.clear ();
80
81                 if (!pc.empty()) {
82
83                         /* emit the signal with further emissions still blocked
84                          * by _change_signal_suspended, but not by the lock.
85                          *
86                          * This means that if the handlers modify other PI
87                          * states, the signal for that won't be sent while they
88                          * are handling the current signal.
89                          */
90                         lm.release ();
91                         Change (pc); /* EMIT SIGNAL */
92                         lm.acquire ();
93                 }
94         }
95
96         g_atomic_int_add (const_cast<gint*>(&_change_signal_suspended), -1);
97 }
98
99 void
100 PresentationInfo::send_static_change (const PropertyChange& what_changed)
101 {
102         if (what_changed.empty()) {
103                 return;
104         }
105
106
107         if (g_atomic_int_get (&_change_signal_suspended)) {
108                 Glib::Threads::Mutex::Lock lm (static_signal_lock);
109                 _pending_static_changes.add (what_changed);
110                 return;
111         }
112
113         Change (what_changed);
114 }
115
116 const PresentationInfo::order_t PresentationInfo::max_order = UINT32_MAX;
117 const PresentationInfo::Flag PresentationInfo::Bus = PresentationInfo::Flag (PresentationInfo::AudioBus|PresentationInfo::MidiBus);
118 const PresentationInfo::Flag PresentationInfo::Track = PresentationInfo::Flag (PresentationInfo::AudioTrack|PresentationInfo::MidiTrack);
119 const PresentationInfo::Flag PresentationInfo::Route = PresentationInfo::Flag (PresentationInfo::Bus|PresentationInfo::Track);
120 const PresentationInfo::Flag PresentationInfo::AllRoutes = PresentationInfo::Flag (PresentationInfo::Route|PresentationInfo::MasterOut|PresentationInfo::MonitorOut|PresentationInfo::FoldbackBus);
121 const PresentationInfo::Flag PresentationInfo::MixerRoutes = PresentationInfo::Flag (PresentationInfo::Route|PresentationInfo::MasterOut|PresentationInfo::MonitorOut);
122 const PresentationInfo::Flag PresentationInfo::AllStripables = PresentationInfo::Flag (PresentationInfo::AllRoutes|PresentationInfo::VCA);
123 const PresentationInfo::Flag PresentationInfo::MixerStripables = PresentationInfo::Flag (PresentationInfo::MixerRoutes|PresentationInfo::VCA);
124
125 void
126 PresentationInfo::make_property_quarks ()
127 {
128         Properties::selected.property_id = g_quark_from_static_string (X_("selected"));
129         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for selected = %1\n", Properties::selected.property_id));
130         Properties::color.property_id = g_quark_from_static_string (X_("color"));
131         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for color = %1\n", Properties::color.property_id));
132         Properties::order.property_id = g_quark_from_static_string (X_("order"));
133         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for order = %1\n", Properties::order.property_id));
134 }
135
136 PresentationInfo::PresentationInfo (Flag f)
137         : _order (0)
138         , _flags (Flag (f & ~OrderSet))
139         , _color (0)
140 {
141         /* OrderSet is not set */
142 }
143
144 PresentationInfo::PresentationInfo (order_t o, Flag f)
145         : _order (o)
146         , _flags (Flag (f | OrderSet))
147         , _color (0)
148 {
149         /* OrderSet is set */
150 }
151 PresentationInfo::PresentationInfo (PresentationInfo const& other)
152         : PBD::Stateful ()
153         , _order (other.order())
154         , _flags (other.flags())
155         , _color (other.color())
156 {
157 }
158
159 XMLNode&
160 PresentationInfo::get_state ()
161 {
162         XMLNode* node = new XMLNode (state_node_name);
163         node->set_property ("order", _order);
164         node->set_property ("flags", _flags);
165         node->set_property ("color", _color);
166
167         return *node;
168 }
169
170 int
171 PresentationInfo::set_state (XMLNode const& node, int /* version */)
172 {
173         if (node.name() != state_node_name) {
174                 return -1;
175         }
176
177         PropertyChange pc;
178
179         order_t o;
180         if (node.get_property (X_("order"), o)) {
181                 if (o != _order) {
182                         pc.add (Properties::order);
183                         _order = o;
184                 }
185                 _order = o; // huh?
186         }
187
188         Flag f;
189         if (node.get_property (X_("flags"), f)) {
190                 if ((f&Hidden) != (_flags&Hidden)) {
191                         pc.add (Properties::hidden);
192                 }
193                 _flags = f;
194         }
195
196         color_t c;
197         if (node.get_property (X_("color"), c)) {
198                 if (c != _color) {
199                         pc.add (Properties::color);
200                         _color = c;
201                 }
202         }
203
204         send_change (PropertyChange (pc));
205
206         return 0;
207
208 }
209
210 PresentationInfo::Flag
211 PresentationInfo::get_flags (XMLNode const& node)
212 {
213         XMLNodeList nlist = node.children ();
214
215         for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter){
216                 XMLNode* child = *niter;
217
218                 if (child->name() == PresentationInfo::state_node_name) {
219                         Flag f;
220                         if (child->get_property (X_("flags"), f)) {
221                                 return f;
222                         }
223                 }
224         }
225         return Flag (0);
226 }
227
228 void
229 PresentationInfo::set_color (PresentationInfo::color_t c)
230 {
231         if (c != _color) {
232                 _color = c;
233                 send_change (PropertyChange (Properties::color));
234                 send_static_change (PropertyChange (Properties::color));
235         }
236 }
237
238 bool
239 PresentationInfo::color_set () const
240 {
241         /* all RGBA values zero? not set.
242          *
243          * this is heuristic, but it is fairly realistic. who will ever set
244          * a color to completely transparent black? only the constructor ..
245          */
246         return _color != 0;
247 }
248
249 void
250 PresentationInfo::set_hidden (bool yn)
251 {
252         if (yn != hidden()) {
253
254                 if (yn) {
255                         _flags = Flag (_flags | Hidden);
256                 } else {
257                         _flags = Flag (_flags & ~Hidden);
258                 }
259
260                 send_change (PropertyChange (Properties::hidden));
261                 send_static_change (PropertyChange (Properties::hidden));
262         }
263 }
264
265 void
266 PresentationInfo::set_order (order_t order)
267 {
268         _flags = Flag (_flags|OrderSet);
269
270         if (order != _order) {
271                 _order = order;
272                 send_change (PropertyChange (Properties::order));
273                 send_static_change (PropertyChange (Properties::order));
274         }
275 }
276
277 PresentationInfo&
278 PresentationInfo::operator= (PresentationInfo const& other)
279 {
280         if (this != &other) {
281                 _order = other.order();
282                 _flags = other.flags();
283                 _color = other.color();
284         }
285
286         return *this;
287 }
288
289 std::ostream&
290 operator<<(std::ostream& o, ARDOUR::PresentationInfo const& pi)
291 {
292         return o << pi.order() << '/' << enum_2_string (pi.flags()) << '/' << pi.color();
293 }