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