reset DiskReader "no disk output" flag in a couple of exceptional cases
[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);
119 const PresentationInfo::Flag PresentationInfo::AllStripables = PresentationInfo::Flag (PresentationInfo::AllRoutes|PresentationInfo::VCA);
120
121 void
122 PresentationInfo::make_property_quarks ()
123 {
124         Properties::selected.property_id = g_quark_from_static_string (X_("selected"));
125         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for selected = %1\n",    Properties::selected.property_id));
126         Properties::color.property_id = g_quark_from_static_string (X_("color"));
127         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for color = %1\n",       Properties::color.property_id));
128         Properties::order.property_id = g_quark_from_static_string (X_("order"));
129         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for order = %1\n",       Properties::order.property_id));
130 }
131
132 PresentationInfo::PresentationInfo (Flag f)
133         : _order (0)
134         , _flags (Flag (f & ~OrderSet))
135         , _color (0)
136 {
137         /* OrderSet is not set */
138 }
139
140 PresentationInfo::PresentationInfo (order_t o, Flag f)
141         : _order (o)
142         , _flags (Flag (f | OrderSet))
143         , _color (0)
144 {
145         /* OrderSet is set */
146 }
147 PresentationInfo::PresentationInfo (PresentationInfo const& other)
148         : PBD::Stateful ()
149         , _order (other.order())
150         , _flags (other.flags())
151         , _color (other.color())
152 {
153 }
154
155 XMLNode&
156 PresentationInfo::get_state ()
157 {
158         XMLNode* node = new XMLNode (state_node_name);
159         node->set_property ("order", _order);
160         node->set_property ("flags", _flags);
161         node->set_property ("color", _color);
162
163         return *node;
164 }
165
166 int
167 PresentationInfo::set_state (XMLNode const& node, int /* version */)
168 {
169         if (node.name() != state_node_name) {
170                 return -1;
171         }
172
173         PropertyChange pc;
174
175         order_t o;
176         if (node.get_property (X_("order"), o)) {
177                 if (o != _order) {
178                         pc.add (Properties::order);
179                         _order = o;
180                 }
181                 _order = o; // huh?
182         }
183
184         Flag f;
185         if (node.get_property (X_("flags"), f)) {
186                 if ((f&Hidden) != (_flags&Hidden)) {
187                         pc.add (Properties::hidden);
188                 }
189                 _flags = f;
190         }
191
192         color_t c;
193         if (node.get_property (X_("color"), c)) {
194                 if (c != _color) {
195                         pc.add (Properties::color);
196                         _color = c;
197                 }
198         }
199
200         send_change (PropertyChange (pc));
201
202         return 0;
203
204 }
205
206 PresentationInfo::Flag
207 PresentationInfo::get_flags (XMLNode const& node)
208 {
209         XMLNodeList nlist = node.children ();
210
211         for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter){
212                 XMLNode* child = *niter;
213
214                 if (child->name() == PresentationInfo::state_node_name) {
215                         Flag f;
216                         if (child->get_property (X_("flags"), f)) {
217                                 return f;
218                         }
219                 }
220         }
221         return Flag (0);
222 }
223
224 void
225 PresentationInfo::set_color (PresentationInfo::color_t c)
226 {
227         if (c != _color) {
228                 _color = c;
229                 send_change (PropertyChange (Properties::color));
230                 send_static_change (PropertyChange (Properties::color));
231         }
232 }
233
234 bool
235 PresentationInfo::color_set () const
236 {
237         /* all RGBA values zero? not set.
238          *
239          * this is heuristic, but it is fairly realistic. who will ever set
240          * a color to completely transparent black? only the constructor ..
241          */
242         return _color != 0;
243 }
244
245 void
246 PresentationInfo::set_hidden (bool yn)
247 {
248         if (yn != hidden()) {
249
250                 if (yn) {
251                         _flags = Flag (_flags | Hidden);
252                 } else {
253                         _flags = Flag (_flags & ~Hidden);
254                 }
255
256                 send_change (PropertyChange (Properties::hidden));
257                 send_static_change (PropertyChange (Properties::hidden));
258         }
259 }
260
261 void
262 PresentationInfo::set_order (order_t order)
263 {
264         _flags = Flag (_flags|OrderSet);
265
266         if (order != _order) {
267                 _order = order;
268                 send_change (PropertyChange (Properties::order));
269                 send_static_change (PropertyChange (Properties::order));
270         }
271 }
272
273 PresentationInfo&
274 PresentationInfo::operator= (PresentationInfo const& other)
275 {
276         if (this != &other) {
277                 _order = other.order();
278                 _flags = other.flags();
279                 _color = other.color();
280         }
281
282         return *this;
283 }
284
285 std::ostream&
286 operator<<(std::ostream& o, ARDOUR::PresentationInfo const& pi)
287 {
288         return o << pi.order() << '/' << enum_2_string (pi.flags()) << '/' << pi.color();
289 }