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