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