Remove redundant beat arg from TempoMap::add_meter
[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         : _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->set_property ("order", _order);
159         node->set_property ("flags", _flags);
160         node->set_property ("color", _color);
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         PropertyChange pc;
173
174         order_t o;
175         if (node.get_property (X_("order"), o)) {
176                 if (o != _order) {
177                         pc.add (Properties::order);
178                         _order = o;
179                 }
180                 _order = o; // huh?
181         }
182
183         Flag f;
184         if (node.get_property (X_("flags"), f)) {
185                 if ((f&Hidden) != (_flags&Hidden)) {
186                         pc.add (Properties::hidden);
187                 }
188                 _flags = f;
189         }
190
191         color_t c;
192         if (node.get_property (X_("color"), c)) {
193                 if (c != _color) {
194                         pc.add (Properties::color);
195                         _color = c;
196                 }
197         }
198
199         send_change (PropertyChange (pc));
200
201         return 0;
202
203 }
204
205 PresentationInfo::Flag
206 PresentationInfo::get_flags (XMLNode const& node)
207 {
208         XMLNodeList nlist = node.children ();
209
210         for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter){
211                 XMLNode* child = *niter;
212
213                 if (child->name() == PresentationInfo::state_node_name) {
214                         Flag f;
215                         if (child->get_property (X_("flags"), f)) {
216                                 return f;
217                         }
218                 }
219         }
220         return Flag (0);
221 }
222
223 void
224 PresentationInfo::set_color (PresentationInfo::color_t c)
225 {
226         if (c != _color) {
227                 _color = c;
228                 send_change (PropertyChange (Properties::color));
229                 send_static_change (PropertyChange (Properties::color));
230         }
231 }
232
233 bool
234 PresentationInfo::color_set () const
235 {
236         /* all RGBA values zero? not set.
237          *
238          * this is heuristic, but it is fairly realistic. who will ever set
239          * a color to completely transparent black? only the constructor ..
240          */
241         return _color != 0;
242 }
243
244 void
245 PresentationInfo::set_hidden (bool yn)
246 {
247         if (yn != hidden()) {
248
249                 if (yn) {
250                         _flags = Flag (_flags | Hidden);
251                 } else {
252                         _flags = Flag (_flags & ~Hidden);
253                 }
254
255                 send_change (PropertyChange (Properties::hidden));
256                 send_static_change (PropertyChange (Properties::hidden));
257         }
258 }
259
260 void
261 PresentationInfo::set_order (order_t order)
262 {
263         _flags = Flag (_flags|OrderSet);
264
265         if (order != _order) {
266                 _order = order;
267                 send_change (PropertyChange (Properties::order));
268                 send_static_change (PropertyChange (Properties::order));
269         }
270 }
271
272 PresentationInfo&
273 PresentationInfo::operator= (PresentationInfo const& other)
274 {
275         if (this != &other) {
276                 _order = other.order();
277                 _flags = other.flags();
278                 _color = other.color();
279         }
280
281         return *this;
282 }
283
284 std::ostream&
285 operator<<(std::ostream& o, ARDOUR::PresentationInfo const& pi)
286 {
287         return o << pi.order() << '/' << enum_2_string (pi.flags()) << '/' << pi.color();
288 }