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