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