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