31f595a8c2fc9fdfabb0150049885b9a5ff844c3
[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/convert.h"
25 #include "pbd/debug.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
34 #include "pbd/i18n.h"
35
36 using namespace ARDOUR;
37 using namespace PBD;
38 using std::string;
39
40 string PresentationInfo::state_node_name = X_("PresentationInfo");
41
42 PBD::Signal1<void,PropertyChange const &> PresentationInfo::Change;
43 Glib::Threads::Mutex PresentationInfo::static_signal_lock;
44 int PresentationInfo::_change_signal_suspended = 0;
45 PBD::PropertyChange PresentationInfo::_pending_static_changes;
46
47 namespace ARDOUR {
48         namespace Properties {
49                 PBD::PropertyDescriptor<bool>     selected;
50                 PBD::PropertyDescriptor<uint32_t> order;
51                 PBD::PropertyDescriptor<uint32_t> color;
52         }
53 }
54
55 void
56 PresentationInfo::suspend_change_signal ()
57 {
58         g_atomic_int_add (&_change_signal_suspended, 1);
59 }
60
61 void
62 PresentationInfo::unsuspend_change_signal ()
63 {
64         Glib::Threads::Mutex::Lock lm (static_signal_lock);
65
66         if (g_atomic_int_get (const_cast<gint*> (&_change_signal_suspended)) == 1) {
67
68                 if (!_pending_static_changes.empty()) {
69
70                         std::cerr << "PI change (unsuspended): ";
71                         for (PropertyChange::const_iterator x = _pending_static_changes.begin(); x != _pending_static_changes.end(); ++x) {
72                                 std::cerr << g_quark_to_string (*x) << ',';
73                         }
74                         std::cerr << '\n';
75
76                         /* emit the signal with further emissions still
77                          * blocked, so that if the handlers modify other PI
78                          * states, the signal for that won't be sent while
79                          * they are handling this one.
80                          */
81
82                         Change (_pending_static_changes); /* EMIT SIGNAL */
83
84                         _pending_static_changes.clear ();
85                 }
86
87                 g_atomic_int_add (const_cast<gint*>(&_change_signal_suspended), -1);
88         }
89 }
90
91 void
92 PresentationInfo::send_static_change (const PropertyChange& what_changed)
93 {
94         if (what_changed.empty()) {
95                 return;
96         }
97
98         Glib::Threads::Mutex::Lock lm (static_signal_lock);
99
100         if (g_atomic_int_get (&_change_signal_suspended)) {
101                 _pending_static_changes.add (what_changed);
102                 return;
103         }
104
105         std::cerr << "PI change (direct): ";
106         for (PropertyChange::const_iterator x = what_changed.begin(); x != what_changed.end(); ++x) {
107                 std::cerr << g_quark_to_string (*x) << ',';
108         }
109         std::cerr << '\n';
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         : _order (other.order())
149         , _flags (other.flags())
150         , _color (other.color())
151 {
152 }
153
154 XMLNode&
155 PresentationInfo::get_state ()
156 {
157         XMLNode* node = new XMLNode (state_node_name);
158         node->add_property ("order", PBD::to_string (_order, std::dec));
159         node->add_property ("flags", enum_2_string (_flags));
160         node->add_property ("color", PBD::to_string (_color, std::dec));
161
162         return *node;
163 }
164
165 int
166 PresentationInfo::set_state (XMLNode const& node, int /* version */)
167 {
168         if (node.name() != state_node_name) {
169                 return -1;
170         }
171
172         XMLProperty const* prop;
173         PropertyChange pc;
174
175         if ((prop = node.property (X_("order"))) != 0) {
176                 order_t o = atoi (prop->value());
177                 if (o != _order) {
178                         pc.add (Properties::order);
179                         _order = o;
180                 }
181                 _order = atoi (prop->value());
182         }
183
184         if ((prop = node.property (X_("flags"))) != 0) {
185                 Flag f = Flag (string_2_enum (prop->value(), f));
186                 if ((f&Hidden) != (_flags&Hidden)) {
187                         pc.add (Properties::hidden);
188                 }
189                 _flags = f;
190         }
191
192         if ((prop = node.property (X_("color"))) != 0) {
193                 color_t c = atoi (prop->value());
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                         XMLProperty const* prop = child->property (X_("flags"));
216                         if (prop) {
217                                 Flag f = (Flag) string_2_enum (prop->value(), f);
218                                 return f;
219                         }
220                 }
221         }
222         return Flag (0);
223 }
224
225 void
226 PresentationInfo::set_color (PresentationInfo::color_t c)
227 {
228         if (c != _color) {
229                 _color = c;
230                 send_change (PropertyChange (Properties::color));
231                 send_static_change (PropertyChange (Properties::color));
232         }
233 }
234
235 bool
236 PresentationInfo::color_set () const
237 {
238         /* all RGBA values zero? not set.
239          *
240          * this is heuristic, but it is fairly realistic. who will ever set
241          * a color to completely transparent black? only the constructor ..
242          */
243         return _color != 0;
244 }
245
246 void
247 PresentationInfo::set_selected (bool yn)
248 {
249         if (yn != selected()) {
250                 if (yn) {
251                         _flags = Flag (_flags | Selected);
252                 } else {
253                         _flags = Flag (_flags & ~Selected);
254                 }
255                 send_change (PropertyChange (Properties::selected));
256                 send_static_change (PropertyChange (Properties::selected));
257         }
258 }
259
260 void
261 PresentationInfo::set_hidden (bool yn)
262 {
263         if (yn != hidden()) {
264
265                 if (yn) {
266                         _flags = Flag (_flags | Hidden);
267                 } else {
268                         _flags = Flag (_flags & ~Hidden);
269                 }
270
271                 send_change (PropertyChange (Properties::hidden));
272                 send_static_change (PropertyChange (Properties::hidden));
273         }
274 }
275
276 void
277 PresentationInfo::set_order (order_t order)
278 {
279         _flags = Flag (_flags|OrderSet);
280
281         if (order != _order) {
282                 _order = order;
283                 send_change (PropertyChange (Properties::order));
284                 send_static_change (PropertyChange (Properties::order));
285         }
286 }
287
288 PresentationInfo&
289 PresentationInfo::operator= (PresentationInfo const& other)
290 {
291         if (this != &other) {
292                 _order = other.order();
293                 _flags = other.flags();
294                 _color = other.color();
295         }
296
297         return *this;
298 }
299
300 std::ostream&
301 operator<<(std::ostream& o, ARDOUR::PresentationInfo const& pi)
302 {
303         return o << pi.order() << '/' << enum_2_string (pi.flags()) << '/' << pi.color();
304 }