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