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