104bb558c9443dee2a8b83b072aa92daf6475653
[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 #include "ardour/control_protocol_manager.h"
33 #include "control_protocol/control_protocol.h"
34
35 #include "gui_thread.h"
36 #include "port_group.h"
37 #include "port_matrix.h"
38 #include "time_axis_view.h"
39 #include "public_editor.h"
40
41 #include "i18n.h"
42
43 using namespace std;
44 using namespace Gtk;
45 using namespace ARDOUR;
46
47 /** PortGroup constructor.
48  * @param n Name.
49  */
50 PortGroup::PortGroup (std::string const & n)
51         : name (n)
52 {
53
54 }
55
56 PortGroup::~PortGroup()
57 {
58         for (BundleList::iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
59                 delete *i;
60         }
61         _bundles.clear ();
62 }
63
64 /** Add a bundle to a group.
65  *  @param b Bundle.
66  *  @param allow_dups true to allow the group to contain more than one bundle with the same port, otherwise false.
67  */
68 void
69 PortGroup::add_bundle (boost::shared_ptr<Bundle> b, bool allow_dups)
70 {
71         add_bundle_internal (b, boost::shared_ptr<IO> (), false, Gdk::Color (), allow_dups);
72 }
73
74 /** Add a bundle to a group.
75  *  @param b Bundle.
76  *  @param io IO whose ports are in the bundle.
77  */
78 void
79 PortGroup::add_bundle (boost::shared_ptr<Bundle> b, boost::shared_ptr<IO> io)
80 {
81         add_bundle_internal (b, io, false, Gdk::Color (), false);
82 }
83
84 /** Add a bundle to a group.
85  *  @param b Bundle.
86  *  @param c Colour to represent the bundle with.
87  */
88 void
89 PortGroup::add_bundle (boost::shared_ptr<Bundle> b, boost::shared_ptr<IO> io, Gdk::Color c)
90 {
91         add_bundle_internal (b, io, true, c, false);
92 }
93
94 PortGroup::BundleRecord::BundleRecord (boost::shared_ptr<ARDOUR::Bundle> b, boost::shared_ptr<ARDOUR::IO> iop, Gdk::Color c, bool has_c)
95         : bundle (b)
96         , io (iop)
97         , colour (c)
98         , has_colour (has_c)
99 {
100 }
101
102 void
103 PortGroup::add_bundle_internal (boost::shared_ptr<Bundle> b, boost::shared_ptr<IO> io, bool has_colour, Gdk::Color colour, bool allow_dups)
104 {
105         assert (b.get());
106
107         if (!allow_dups) {
108                 
109                 /* don't add this bundle if we already have one with the same ports */
110                 
111                 BundleList::iterator i = _bundles.begin ();
112                 while (i != _bundles.end() && b->has_same_ports ((*i)->bundle) == false) {
113                         ++i;
114                 }
115                 
116                 if (i != _bundles.end ()) {
117                         return;
118                 }
119         }
120
121         BundleRecord* br = new BundleRecord (b, io, colour, has_colour);
122         b->Changed.connect (br->changed_connection, invalidator (*this), ui_bind (&PortGroup::bundle_changed, this, _1), gui_context());
123         _bundles.push_back (br);
124
125         Changed ();     
126 }
127
128 void
129 PortGroup::remove_bundle (boost::shared_ptr<Bundle> b)
130 {
131         assert (b.get());
132
133         BundleList::iterator i = _bundles.begin ();
134         while (i != _bundles.end() && (*i)->bundle != b) {
135                 ++i;
136         }
137
138         if (i == _bundles.end()) {
139                 return;
140         }
141
142         delete *i;
143         _bundles.erase (i);
144
145         Changed ();
146 }
147
148 void
149 PortGroup::bundle_changed (Bundle::Change c)
150 {
151         BundleChanged (c);
152 }
153
154
155 void
156 PortGroup::clear ()
157 {
158         for (BundleList::iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
159                 delete *i;
160         }
161
162         _bundles.clear ();
163         Changed ();
164 }
165
166 bool
167 PortGroup::has_port (std::string const& p) const
168 {
169         for (BundleList::const_iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
170                 if ((*i)->bundle->offers_port_alone (p)) {
171                         return true;
172                 }
173         }
174
175         return false;
176 }
177
178 boost::shared_ptr<Bundle>
179 PortGroup::only_bundle ()
180 {
181         assert (_bundles.size() == 1);
182         return _bundles.front()->bundle;
183 }
184
185
186 ChanCount
187 PortGroup::total_channels () const
188 {
189         ChanCount n;
190         for (BundleList::const_iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
191                 n += (*i)->bundle->nchannels ();
192         }
193
194         return n;
195 }
196
197 boost::shared_ptr<IO>
198 PortGroup::io_from_bundle (boost::shared_ptr<ARDOUR::Bundle> b) const
199 {
200         BundleList::const_iterator i = _bundles.begin ();
201         while (i != _bundles.end() && (*i)->bundle != b) {
202                 ++i;
203         }
204
205         if (i == _bundles.end()) {
206                 return boost::shared_ptr<IO> ();
207         }
208
209         return (*i)->io;
210 }
211
212 /** Remove bundles whose channels are already represented by other, larger bundles */
213 void
214 PortGroup::remove_duplicates ()
215 {
216         BundleList::iterator i = _bundles.begin();
217         while (i != _bundles.end()) {
218
219                 BundleList::iterator tmp = i;
220                 ++tmp;
221
222                 bool remove = false;
223
224                 for (BundleList::iterator j = _bundles.begin(); j != _bundles.end(); ++j) {
225
226                         if ((*j)->bundle->nchannels() > (*i)->bundle->nchannels()) {
227                                 /* this bundle is larger */
228
229                                 uint32_t k = 0;
230                                 while (k < (*i)->bundle->nchannels().n_total()) {
231                                         /* see if this channel on *i has an equivalent on *j */
232                                         uint32_t l = 0;
233                                         while (l < (*j)->bundle->nchannels().n_total() && (*i)->bundle->channel_ports (k) != (*j)->bundle->channel_ports (l)) {
234                                                 ++l;
235                                         }
236
237                                         if (l == (*j)->bundle->nchannels().n_total()) {
238                                                 /* it does not */
239                                                 break;
240                                         }
241
242                                         ++k;
243                                 }
244
245                                 if (k == (*i)->bundle->nchannels().n_total()) {
246                                         /* all channels on *i are represented by the larger bundle *j, so remove *i */
247                                         remove = true;
248                                         break;
249                                 }
250                         }
251                 }
252                 
253                 if (remove) {
254                         _bundles.erase (i);
255                 }
256                 
257                 i = tmp;
258         }
259 }
260
261
262 /** PortGroupList constructor.
263  */
264 PortGroupList::PortGroupList ()
265         : _signals_suspended (false), _pending_change (false), _pending_bundle_change ((Bundle::Change) 0)
266 {
267
268 }
269
270 PortGroupList::~PortGroupList() 
271 {
272         /* XXX need to clean up bundles, but ownership shared with PortGroups */
273 }
274
275 void
276 PortGroupList::maybe_add_processor_to_list (
277         boost::weak_ptr<Processor> wp, list<boost::shared_ptr<IO> >* route_ios, bool inputs, set<boost::shared_ptr<IO> >& used_io
278         )
279 {
280         boost::shared_ptr<Processor> p (wp.lock());
281
282         if (!p) {
283                 return;
284         }
285
286         boost::shared_ptr<IOProcessor> iop = boost::dynamic_pointer_cast<IOProcessor> (p);
287
288         if (iop) {
289
290                 boost::shared_ptr<IO> io = inputs ? iop->input() : iop->output();
291
292                 if (io && used_io.find (io) == used_io.end()) {
293                         route_ios->push_back (io);
294                         used_io.insert (io);
295                 }
296         }
297 }
298
299 struct RouteIOs {
300         RouteIOs (boost::shared_ptr<Route> r, boost::shared_ptr<IO> i) {
301                 route = r;
302                 ios.push_back (i);
303         }
304         
305         boost::shared_ptr<Route> route;
306         std::list<boost::shared_ptr<IO> > ios;
307 };
308
309 class RouteIOsComparator {
310 public:
311         bool operator() (RouteIOs const & a, RouteIOs const & b) {
312                 return a.route->order_key (X_("editor")) < b.route->order_key (X_("editor"));
313         }
314 };
315
316 /** Gather ports from around the system and put them in this PortGroupList.
317  *  @param type Type of ports to collect, or NIL for all types.
318  */
319 void
320 PortGroupList::gather (ARDOUR::Session* session, ARDOUR::DataType type, bool inputs, bool allow_dups)
321 {
322         clear ();
323
324         if (session == 0) {
325                 return;
326         }
327
328         boost::shared_ptr<PortGroup> bus (new PortGroup (_("Bus")));
329         boost::shared_ptr<PortGroup> track (new PortGroup (_("Track")));
330         boost::shared_ptr<PortGroup> system (new PortGroup (_("System")));
331         boost::shared_ptr<PortGroup> ardour (new PortGroup (_("Ardour")));
332         boost::shared_ptr<PortGroup> other (new PortGroup (_("Other")));
333
334         /* Find the IOs which have bundles for routes and their processors.  We store
335            these IOs in a RouteIOs class so that we can then sort the results by route
336            order key.
337         */
338
339         boost::shared_ptr<RouteList> routes = session->get_routes ();
340         list<RouteIOs> route_ios;
341
342         for (RouteList::const_iterator i = routes->begin(); i != routes->end(); ++i) {
343
344                 /* keep track of IOs that we have taken bundles from,
345                    so that we can avoid taking the same IO from both
346                    Route::output() and the main_outs Delivery */
347
348                 set<boost::shared_ptr<IO> > used_io;
349                 boost::shared_ptr<IO> io = inputs ? (*i)->input() : (*i)->output();
350                 used_io.insert (io);
351
352                 RouteIOs rb (*i, io);
353                 (*i)->foreach_processor (boost::bind (&PortGroupList::maybe_add_processor_to_list, this, _1, &rb.ios, inputs, used_io));
354
355                 route_ios.push_back (rb);
356         }
357
358         /* Sort RouteIOs by the routes' editor order keys */
359         route_ios.sort (RouteIOsComparator ());
360
361         /* Now put the bundles that belong to these sorted RouteIOs into the PortGroup.
362            Note that if the RouteIO's bundles are multi-type, we may make new Bundles
363            with only the ports of one type.
364         */
365         
366         for (list<RouteIOs>::iterator i = route_ios.begin(); i != route_ios.end(); ++i) {
367                 TimeAxisView* tv = PublicEditor::instance().axis_view_from_route (i->route);
368
369                 /* Work out which group to put these IOs' bundles in */
370                 boost::shared_ptr<PortGroup> g;
371                 if (boost::dynamic_pointer_cast<Track> (i->route)) {
372                         g = track;
373                 } else {
374                         g = bus;
375                 }
376
377                 for (list<boost::shared_ptr<IO> >::iterator j = i->ios.begin(); j != i->ios.end(); ++j) {
378                         boost::shared_ptr<Bundle> b = bundle_for_type ((*j)->bundle(), type);
379                         if (b->nchannels() != ChanCount::ZERO) {
380                                 if (tv) {
381                                         g->add_bundle (b, *j, tv->color ());
382                                 } else {
383                                         g->add_bundle (b, *j);
384                                 }
385                         }
386                 }
387         }
388
389         /* Bundles owned by the session; add user bundles first, then normal ones, so
390            that UserBundles that offer the same ports as a normal bundle get priority
391         */
392
393         boost::shared_ptr<BundleList> b = session->bundles ();
394
395         for (BundleList::iterator i = b->begin(); i != b->end(); ++i) {
396                 if (boost::dynamic_pointer_cast<UserBundle> (*i) && (*i)->ports_are_inputs() == inputs) {
397                         boost::shared_ptr<Bundle> b = bundle_for_type (*i, type);
398                         if (b->nchannels() != ChanCount::ZERO) {
399                                 system->add_bundle (b, allow_dups);
400                         }
401                 }
402         }
403
404         for (BundleList::iterator i = b->begin(); i != b->end(); ++i) {
405                 if (boost::dynamic_pointer_cast<UserBundle> (*i) == 0 && (*i)->ports_are_inputs() == inputs) {
406                         boost::shared_ptr<Bundle> b = bundle_for_type (*i, type);
407                         if (b->nchannels() != ChanCount::ZERO) {
408                                 system->add_bundle (b, allow_dups);
409                         }
410                 }
411         }
412         
413         /* Ardour stuff */
414
415         if (!inputs) {
416                 boost::shared_ptr<Bundle> b = bundle_for_type (session->the_auditioner()->output()->bundle(), type);
417                 if (b->nchannels() != ChanCount::ZERO) {
418                         ardour->add_bundle (b);
419                 }
420
421                 b = bundle_for_type (session->click_io()->bundle(), type);
422                 if (b->nchannels() != ChanCount::ZERO) {
423                         ardour->add_bundle (b);
424                 }
425         }
426
427         /* Ardour's surfaces */
428
429         ControlProtocolManager& m = ControlProtocolManager::instance ();
430         for (list<ControlProtocolInfo*>::iterator i = m.control_protocol_info.begin(); i != m.control_protocol_info.end(); ++i) {
431                 if ((*i)->protocol) {
432                         list<boost::shared_ptr<Bundle> > b = (*i)->protocol->bundles ();
433                         for (list<boost::shared_ptr<Bundle> >::iterator j = b.begin(); j != b.end(); ++j) {
434                                 if ((*j)->ports_are_inputs() == inputs) {
435                                         ardour->add_bundle (*j);
436                                 }
437                         }
438                 }
439         }
440
441         /* Now find all other ports that we haven't thought of yet */
442
443         std::vector<std::string> extra_system[DataType::num_types];
444         std::vector<std::string> extra_other[DataType::num_types];
445
446         const char ** ports = 0;
447         if (type == DataType::NIL) {
448                 ports = session->engine().get_ports ("", "", inputs ? JackPortIsInput : JackPortIsOutput);
449         } else {
450                 ports = session->engine().get_ports ("", type.to_jack_type(), inputs ? JackPortIsInput : JackPortIsOutput);
451         }
452         
453         if (ports) {
454
455                 int n = 0;
456
457                 while (ports[n]) {
458
459                         std::string const p = ports[n];
460
461                         if (!system->has_port(p) &&
462                             !bus->has_port(p) &&
463                             !track->has_port(p) &&
464                             !ardour->has_port(p) &&
465                             !other->has_port(p)) {
466                                 
467                                 /* special hack: ignore MIDI ports labelled Midi-Through. these
468                                    are basically useless and mess things up for default
469                                    connections.
470                                 */
471
472                                 if (p.find ("Midi-Through") != string::npos) {
473                                         ++n;
474                                         continue;
475                                 }
476
477                                 /* can't use the audio engine for this as we are looking at non-Ardour ports */
478
479                                 jack_port_t* jp = jack_port_by_name (session->engine().jack(), p.c_str());
480                                 if (jp) {
481                                         DataType t (jack_port_type (jp));
482                                         if (t != DataType::NIL) {
483                                                 if (port_has_prefix (p, "system:") || port_has_prefix (p, "alsa_pcm") || port_has_prefix (p, "ardour:")) {
484                                                         extra_system[t].push_back (p);
485                                                 } else {
486                                                         extra_other[t].push_back (p);
487                                                 }
488                                         }
489                                 }
490                         }
491
492                         ++n;
493                 }
494
495                 free (ports);
496         }
497
498         for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
499                 if (!extra_system[*i].empty()) {
500                         boost::shared_ptr<Bundle> b = make_bundle_from_ports (extra_system[*i], *i, inputs);
501                         boost::shared_ptr<Bundle> bt = bundle_for_type (b, type);
502                         if (bt->nchannels() != ChanCount::ZERO) {
503                                 system->add_bundle (bt);
504                         }
505                 }
506         }
507
508         for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
509                 if (!extra_other[*i].empty()) {
510                         boost::shared_ptr<Bundle> b = make_bundle_from_ports (extra_other[*i], *i, inputs);
511                         boost::shared_ptr<Bundle> bt = bundle_for_type (b, type);
512                         if (bt->nchannels() != ChanCount::ZERO) {
513                                 other->add_bundle (bt);
514                         }
515                 }
516         }
517
518         if (!allow_dups) {
519                 system->remove_duplicates ();
520         }
521
522         add_group_if_not_empty (other);
523         add_group_if_not_empty (bus);
524         add_group_if_not_empty (track);
525         add_group_if_not_empty (ardour);
526         add_group_if_not_empty (system);
527
528         emit_changed ();
529 }
530
531 boost::shared_ptr<Bundle>
532 PortGroupList::make_bundle_from_ports (std::vector<std::string> const & p, ARDOUR::DataType type, bool inputs) const
533 {
534         boost::shared_ptr<Bundle> b (new Bundle ("", inputs));
535
536         std::string const pre = common_prefix (p);
537         if (!pre.empty()) {
538                 b->set_name (pre.substr (0, pre.length() - 1));
539         }
540
541         for (uint32_t j = 0; j < p.size(); ++j) {
542                 b->add_channel (p[j].substr (pre.length()), type);
543                 b->set_port (j, p[j]);
544         }
545
546         return b;
547 }
548
549 bool
550 PortGroupList::port_has_prefix (const std::string& n, const std::string& p) const
551 {
552         return n.substr (0, p.length()) == p;
553 }
554
555 std::string
556 PortGroupList::common_prefix_before (std::vector<std::string> const & p, std::string const & s) const
557 {
558         /* we must have some strings and the first must contain the separator string */
559         if (p.empty() || p[0].find_first_of (s) == std::string::npos) {
560                 return "";
561         }
562
563         /* prefix of the first string */
564         std::string const fp = p[0].substr (0, p[0].find_first_of (s) + 1);
565
566         /* see if the other strings also start with fp */
567         uint32_t j = 1;
568         while (j < p.size()) {
569                 if (p[j].substr (0, fp.length()) != fp) {
570                         break;
571                 }
572                 ++j;
573         }
574
575         if (j != p.size()) {
576                 return "";
577         }
578
579         return fp;
580 }
581
582
583 std::string
584 PortGroupList::common_prefix (std::vector<std::string> const & p) const
585 {
586         /* common prefix before '/' ? */
587         std::string cp = common_prefix_before (p, "/");
588         if (!cp.empty()) {
589                 return cp;
590         }
591
592         cp = common_prefix_before (p, ":");
593         if (!cp.empty()) {
594                 return cp;
595         }
596
597         return "";
598 }
599
600 void
601 PortGroupList::clear ()
602 {
603         _groups.clear ();
604         _bundle_changed_connections.drop_connections ();
605         emit_changed ();
606 }
607
608
609 PortGroup::BundleList const &
610 PortGroupList::bundles () const
611 {
612         _bundles.clear ();
613
614         for (PortGroupList::List::const_iterator i = begin (); i != end (); ++i) {
615                 std::copy ((*i)->bundles().begin(), (*i)->bundles().end(), std::back_inserter (_bundles));
616         }
617
618         return _bundles;
619 }
620
621 ChanCount
622 PortGroupList::total_channels () const
623 {
624         ChanCount n;
625
626         for (PortGroupList::List::const_iterator i = begin(); i != end(); ++i) {
627                 n += (*i)->total_channels ();
628         }
629
630         return n;
631 }
632
633 void
634 PortGroupList::add_group_if_not_empty (boost::shared_ptr<PortGroup> g)
635 {
636         if (!g->bundles().empty ()) {
637                 add_group (g);
638         }
639 }
640
641 void
642 PortGroupList::add_group (boost::shared_ptr<PortGroup> g)
643 {
644         _groups.push_back (g);
645
646         g->Changed.connect (_changed_connections, invalidator (*this), boost::bind (&PortGroupList::emit_changed, this), gui_context());
647         g->BundleChanged.connect (_bundle_changed_connections, invalidator (*this), ui_bind (&PortGroupList::emit_bundle_changed, this, _1), gui_context());
648
649         emit_changed ();
650 }
651
652 void
653 PortGroupList::remove_bundle (boost::shared_ptr<Bundle> b)
654 {
655         for (List::iterator i = _groups.begin(); i != _groups.end(); ++i) {
656                 (*i)->remove_bundle (b);
657         }
658
659         emit_changed ();
660 }
661
662 void
663 PortGroupList::emit_changed ()
664 {
665         if (_signals_suspended) {
666                 _pending_change = true;
667         } else {
668                 Changed ();
669         }
670 }
671
672 void
673 PortGroupList::emit_bundle_changed (Bundle::Change c)
674 {
675         if (_signals_suspended) {
676                 _pending_bundle_change = c;
677         } else {
678                 BundleChanged (c);
679         }
680 }
681 void
682 PortGroupList::suspend_signals ()
683 {
684         _signals_suspended = true;
685 }
686
687 void
688 PortGroupList::resume_signals ()
689 {
690         if (_pending_change) {
691                 Changed ();
692                 _pending_change = false;
693         }
694
695         if (_pending_bundle_change != 0) {
696                 BundleChanged (_pending_bundle_change);
697                 _pending_bundle_change = (ARDOUR::Bundle::Change) 0;
698         }
699
700         _signals_suspended = false;
701 }
702
703 boost::shared_ptr<IO>
704 PortGroupList::io_from_bundle (boost::shared_ptr<ARDOUR::Bundle> b) const
705 {
706         List::const_iterator i = _groups.begin ();
707         while (i != _groups.end()) {
708                 boost::shared_ptr<IO> io = (*i)->io_from_bundle (b);
709                 if (io) {
710                         return io;
711                 }
712                 ++i;
713         }
714
715         return boost::shared_ptr<IO> ();
716 }
717
718 bool
719 PortGroupList::empty () const
720 {
721         List::const_iterator i = _groups.begin ();
722         while (i != _groups.end() && (*i)->total_channels() == ChanCount::ZERO) {
723                 ++i;
724         }
725
726         return (i == _groups.end());
727 }
728
729 /** Take a bundle, and either return it, if it contains only ports of type \a t,
730  *  or return a new bundle with those ports from \a b which are of type \a t.
731  *  Note that t == NIL is taken to mean "all types".
732  */
733 boost::shared_ptr<Bundle>
734 PortGroupList::bundle_for_type (boost::shared_ptr<Bundle> b, DataType t) const
735 {
736         /* We are asked for a bundle with all types, so that's easy */
737         if (t == DataType::NIL) {
738                 return b;
739         }
740
741         if (b->nchannels().get(t) == b->nchannels().n_total()) {
742                 /* All channels on b are of the correct type, so just return b */
743                 return b;
744         }
745
746         /* We must build a new bundle */
747         boost::shared_ptr<Bundle> n (new ARDOUR::Bundle (b->name(), b->ports_are_inputs()));
748         for (uint32_t i = 0; i < b->nchannels().n_total(); ++i) {
749                 if (b->channel_type(i) == t) {
750                         n->add_channel (b->channel_name (i), t, b->channel_ports (i));
751                 }
752         }
753
754         return n;
755 }