Better fix for non-rearrangement after open for the port matrix.
[ardour.git] / gtk2_ardour / port_group.cc
1 /*
2     Copyright (C) 2002-2009 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
20 #include <cstring>
21 #include <boost/shared_ptr.hpp>
22
23 #include "ardour/audio_track.h"
24 #include "ardour/audioengine.h"
25 #include "ardour/bundle.h"
26 #include "ardour/user_bundle.h"
27 #include "ardour/io_processor.h"
28 #include "ardour/midi_track.h"
29 #include "ardour/port.h"
30 #include "ardour/session.h"
31
32 #include "port_group.h"
33 #include "port_matrix.h"
34 #include "time_axis_view.h"
35 #include "public_editor.h"
36
37 #include "i18n.h"
38
39 using namespace std;
40 using namespace Gtk;
41 using namespace ARDOUR;
42
43 /** PortGroup constructor.
44  * @param n Name.
45  */
46 PortGroup::PortGroup (std::string const & n)
47         : name (n), _visible (true)
48 {
49         
50 }
51
52 /** Add a bundle to a group.
53  *  @param b Bundle.
54  */
55 void
56 PortGroup::add_bundle (boost::shared_ptr<Bundle> b)
57 {
58         assert (b.get());
59
60         BundleRecord r;
61         r.bundle = b;
62         r.has_colour = false;
63         r.changed_connection = b->Changed.connect (sigc::mem_fun (*this, &PortGroup::bundle_changed));
64
65         _bundles.push_back (r);
66
67         Changed ();
68 }
69
70 /** Add a bundle to a group.
71  *  @param b Bundle.
72  *  @param c Colour to represent the group with.
73  */
74 void
75 PortGroup::add_bundle (boost::shared_ptr<Bundle> b, Gdk::Color c)
76 {
77         assert (b.get());
78
79         BundleRecord r;
80         r.bundle = b;
81         r.colour = c;
82         r.has_colour = true;
83         r.changed_connection = b->Changed.connect (sigc::mem_fun (*this, &PortGroup::bundle_changed));
84
85         _bundles.push_back (r);
86
87         Changed ();
88 }
89
90 void
91 PortGroup::remove_bundle (boost::shared_ptr<Bundle> b)
92 {
93         assert (b.get());
94
95         BundleList::iterator i = _bundles.begin ();
96         while (i != _bundles.end() && i->bundle != b) {
97                 ++i;
98         }
99
100         if (i == _bundles.end()) {
101                 return;
102         }
103
104         i->changed_connection.disconnect ();
105         _bundles.erase (i);
106         
107         Changed ();
108 }
109
110 void
111 PortGroup::bundle_changed (Bundle::Change c)
112 {
113         BundleChanged (c);
114 }
115
116
117 void
118 PortGroup::clear ()
119 {
120         for (BundleList::iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
121                 i->changed_connection.disconnect ();
122         }
123
124         _bundles.clear ();
125         Changed ();
126 }
127
128 bool
129 PortGroup::has_port (std::string const& p) const
130 {
131         for (BundleList::const_iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
132                 if (i->bundle->offers_port_alone (p)) {
133                         return true;
134                 }
135         }
136
137         return false;
138 }
139
140 boost::shared_ptr<Bundle>
141 PortGroup::only_bundle ()
142 {
143         assert (_bundles.size() == 1);
144         return _bundles.front().bundle;
145 }
146
147
148 uint32_t
149 PortGroup::total_channels () const
150 {
151         uint32_t n = 0;
152         for (BundleList::const_iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
153                 n += i->bundle->nchannels ();
154         }
155
156         return n;
157 }
158
159 /** PortGroupList constructor.
160  */
161 PortGroupList::PortGroupList ()
162         : _type (DataType::AUDIO), _signals_suspended (false), _pending_change (false)
163 {
164         
165 }
166
167 void
168 PortGroupList::set_type (DataType t)
169 {
170         _type = t;
171         clear ();
172 }
173
174 void
175 PortGroupList::maybe_add_processor_to_bundle (boost::weak_ptr<Processor> wp, boost::shared_ptr<RouteBundle> rb, bool inputs, set<boost::shared_ptr<IO> >& used_io)
176 {
177         boost::shared_ptr<Processor> p (wp.lock());
178
179         if (!p) {
180                 return;
181         }
182
183         boost::shared_ptr<IOProcessor> iop = boost::dynamic_pointer_cast<IOProcessor> (p);
184         
185         if (iop) {
186
187                 boost::shared_ptr<IO> io = inputs ? iop->input() : iop->output();
188                 
189                 if (io && used_io.find (io) == used_io.end()) {
190                         rb->add_processor_bundle (io->bundle ());
191                         used_io.insert (io);
192                 }
193         }
194 }
195
196
197 /** Gather bundles from around the system and put them in this PortGroupList */
198 void
199 PortGroupList::gather (ARDOUR::Session& session, bool inputs)
200 {
201         clear ();
202
203         boost::shared_ptr<PortGroup> bus (new PortGroup (_("Bus")));
204         boost::shared_ptr<PortGroup> track (new PortGroup (_("Track")));
205         boost::shared_ptr<PortGroup> system (new PortGroup (_("System")));
206         boost::shared_ptr<PortGroup> other (new PortGroup (_("Other")));
207
208         /* Find the bundles for routes.  We use the RouteBundle class to join
209            the route's input/output and processor bundles together so that they
210            are presented as one bundle in the matrix. */
211
212         boost::shared_ptr<RouteList> routes = session.get_routes ();
213
214         for (RouteList::const_iterator i = routes->begin(); i != routes->end(); ++i) {
215
216                 /* keep track of IOs that we have taken bundles from, so that maybe_add_processor... below
217                    can avoid taking the same IO from both Route::output() and the main_outs Delivery */
218                    
219                 set<boost::shared_ptr<IO> > used_io;
220                 boost::shared_ptr<IO> io = inputs ? (*i)->input() : (*i)->output();
221                 used_io.insert (io);
222                 
223                 boost::shared_ptr<RouteBundle> rb (new RouteBundle (io->bundle()));
224
225                 (*i)->foreach_processor (bind (mem_fun (*this, &PortGroupList::maybe_add_processor_to_bundle), rb, inputs, used_io));
226
227                 /* Work out which group to put this bundle in */
228                 boost::shared_ptr<PortGroup> g;
229                 if (_type == DataType::AUDIO) {
230
231                         if (boost::dynamic_pointer_cast<AudioTrack> (*i)) {
232                                 g = track;
233                         } else if (!boost::dynamic_pointer_cast<MidiTrack>(*i)) {
234                                 g = bus;
235                         } 
236
237
238                 } else if (_type == DataType::MIDI) {
239
240                         if (boost::dynamic_pointer_cast<MidiTrack> (*i)) {
241                                 g = track;
242                         }
243
244                         /* No MIDI busses yet */
245                 } 
246                         
247                 if (g) {
248
249                         TimeAxisView* tv = PublicEditor::instance().axis_view_from_route (i->get());
250                         if (tv) {
251                                 g->add_bundle (rb, tv->color ());
252                         } else {
253                                 g->add_bundle (rb);
254                         }
255                 }
256         }
257
258         /* Bundles owned by the session.  We only add the mono ones and the User ones
259            otherwise there is duplication of the same ports within the matrix */
260         
261         boost::shared_ptr<BundleList> b = session.bundles ();
262         for (BundleList::iterator i = b->begin(); i != b->end(); ++i) {
263                 if ((*i)->ports_are_inputs() == inputs && (*i)->type() == _type) {
264
265                         if ((*i)->nchannels() == 1 || boost::dynamic_pointer_cast<UserBundle> (*i)) {
266                                 system->add_bundle (*i);
267                         }
268                 
269                 }
270         }
271
272         /* Now find all other ports that we haven't thought of yet */
273
274         std::vector<std::string> extra_system;
275         std::vector<std::string> extra_other;
276
277         const char **ports = session.engine().get_ports ("", _type.to_jack_type(), inputs ? 
278                                                          JackPortIsInput : JackPortIsOutput);
279         if (ports) {
280
281                 int n = 0;
282                 string client_matching_string;
283
284                 client_matching_string = session.engine().client_name();
285                 client_matching_string += ':';
286
287                 while (ports[n]) {
288                         
289                         std::string const p = ports[n];
290
291                         if (!system->has_port(p) && !bus->has_port(p) && !track->has_port(p) && !other->has_port(p)) {
292                                 
293                                 if (port_has_prefix (p, "system:") ||
294                                     port_has_prefix (p, "alsa_pcm") ||
295                                     port_has_prefix (p, "ardour:")) {
296                                         extra_system.push_back (p);
297                                 } else {
298                                         extra_other.push_back (p);
299                                 }
300                         }
301                         
302                         ++n;
303                 }
304
305                 free (ports);
306         }
307
308         if (!extra_system.empty()) {
309                 system->add_bundle (make_bundle_from_ports (extra_system, inputs));
310         }
311
312         if (!extra_other.empty()) {
313                 other->add_bundle (make_bundle_from_ports (extra_other, inputs));
314         }
315
316         add_group (system);
317         add_group (bus);
318         add_group (track);
319         add_group (other);
320
321         emit_changed ();
322 }
323
324 boost::shared_ptr<Bundle>
325 PortGroupList::make_bundle_from_ports (std::vector<std::string> const & p, bool inputs) const
326 {
327         boost::shared_ptr<Bundle> b (new Bundle ("", _type, inputs));
328
329         std::string const pre = common_prefix (p);
330         if (!pre.empty()) {
331                 b->set_name (pre.substr (0, pre.length() - 1));
332         }
333
334         for (uint32_t j = 0; j < p.size(); ++j) {
335                 b->add_channel (p[j].substr (pre.length()));
336                 b->set_port (j, p[j]);
337         }
338
339         return b;
340 }
341
342 bool
343 PortGroupList::port_has_prefix (const std::string& n, const std::string& p) const
344 {
345         return n.substr (0, p.length()) == p;
346 }
347
348 std::string
349 PortGroupList::common_prefix_before (std::vector<std::string> const & p, std::string const & s) const
350 {
351         /* we must have some strings and the first must contain the separator string */
352         if (p.empty() || p[0].find_first_of (s) == std::string::npos) {
353                 return "";
354         }
355
356         /* prefix of the first string */
357         std::string const fp = p[0].substr (0, p[0].find_first_of (s) + 1);
358
359         /* see if the other strings also start with fp */
360         uint32_t j = 1;
361         while (j < p.size()) {
362                 if (p[j].substr (0, fp.length()) != fp) {
363                         break;
364                 }
365                 ++j;
366         }
367
368         if (j != p.size()) {
369                 return "";
370         }
371
372         return fp;
373 }
374         
375
376 std::string
377 PortGroupList::common_prefix (std::vector<std::string> const & p) const
378 {
379         /* common prefix before '/' ? */
380         std::string cp = common_prefix_before (p, "/");
381         if (!cp.empty()) {
382                 return cp;
383         }
384
385         cp = common_prefix_before (p, ":");
386         if (!cp.empty()) {
387                 return cp;
388         }
389
390         return "";
391 }
392
393 void
394 PortGroupList::clear ()
395 {
396         _groups.clear ();
397
398         for (std::vector<sigc::connection>::iterator i = _bundle_changed_connections.begin(); i != _bundle_changed_connections.end(); ++i) {
399                 i->disconnect ();
400         }
401
402         _bundle_changed_connections.clear ();
403
404         emit_changed ();
405 }
406
407
408 PortGroup::BundleList const &
409 PortGroupList::bundles () const
410 {
411         _bundles.clear ();
412         
413         for (PortGroupList::List::const_iterator i = begin (); i != end (); ++i) {
414                 if ((*i)->visible()) {
415                         std::copy ((*i)->bundles().begin(), (*i)->bundles().end(), std::back_inserter (_bundles));
416                 }
417         }
418         
419         return _bundles;
420 }
421
422 uint32_t
423 PortGroupList::total_visible_channels () const
424 {
425         uint32_t n = 0;
426         
427         for (PortGroupList::List::const_iterator i = begin(); i != end(); ++i) {
428                 if ((*i)->visible()) {
429                         n += (*i)->total_channels ();
430                 }
431         }
432
433         return n;
434 }
435
436
437 void
438 PortGroupList::add_group (boost::shared_ptr<PortGroup> g)
439 {
440         _groups.push_back (g);
441         
442         g->Changed.connect (sigc::mem_fun (*this, &PortGroupList::emit_changed));
443         
444         _bundle_changed_connections.push_back (
445                 g->BundleChanged.connect (sigc::hide (sigc::mem_fun (*this, &PortGroupList::emit_changed)))
446                 );
447
448         emit_changed ();
449 }
450
451 void
452 PortGroupList::remove_bundle (boost::shared_ptr<Bundle> b)
453 {
454         for (List::iterator i = _groups.begin(); i != _groups.end(); ++i) {
455                 (*i)->remove_bundle (b);
456         }
457
458         emit_changed ();
459 }
460
461 void
462 PortGroupList::emit_changed ()
463 {
464         if (_signals_suspended) {
465                 _pending_change = true;
466         } else {
467                 Changed ();
468         }
469 }
470                 
471 void
472 PortGroupList::suspend_signals ()
473 {
474         _signals_suspended = true;
475 }
476
477 void
478 PortGroupList::resume_signals ()
479 {
480         if (_pending_change) {
481                 Changed ();
482                 _pending_change = false;
483         }
484
485         _signals_suspended = false;
486 }
487
488 RouteBundle::RouteBundle (boost::shared_ptr<Bundle> r)
489         : _route (r)
490 {
491         _route->Changed.connect (sigc::hide (sigc::mem_fun (*this, &RouteBundle::reread_component_bundles)));
492         reread_component_bundles ();
493 }
494
495 void
496 RouteBundle::reread_component_bundles ()
497 {
498         suspend_signals ();
499         
500         remove_channels ();
501
502         set_name (_route->name());
503
504         for (uint32_t i = 0; i < _route->nchannels(); ++i) {
505                 add_channel (_route->channel_name (i));
506                 PortList const & pl = _route->channel_ports (i);
507                 for (uint32_t j = 0; j < pl.size(); ++j) {
508                         add_port_to_channel (i, pl[j]);
509                 }
510         }
511                 
512         for (std::vector<boost::shared_ptr<Bundle> >::iterator i = _processor.begin(); i != _processor.end(); ++i) {
513                 add_channels_from_bundle (*i);
514         }
515
516         resume_signals ();
517 }
518
519 void
520 RouteBundle::add_processor_bundle (boost::shared_ptr<Bundle> p)
521 {
522         p->Changed.connect (sigc::hide (sigc::mem_fun (*this, &RouteBundle::reread_component_bundles)));
523         _processor.push_back (p);
524         
525         reread_component_bundles ();
526 }
527