05bd3153b775ebfc7b1563a7b6ff4ecaf8936c01
[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/io_processor.h"
27 #include "ardour/midi_track.h"
28 #include "ardour/port.h"
29 #include "ardour/session.h"
30
31 #include "port_group.h"
32 #include "port_matrix.h"
33
34 #include "i18n.h"
35
36 using namespace std;
37 using namespace Gtk;
38
39 /** Add a bundle to a group.
40  *  @param b Bundle.
41  */
42 void
43 PortGroup::add_bundle (boost::shared_ptr<ARDOUR::Bundle> b)
44 {
45         assert (b.get());
46         _bundles.push_back (b);
47
48         Modified ();
49 }
50
51 /** Add a port to a group.
52  *  @param p Port.
53  */
54 void
55 PortGroup::add_port (std::string const &p)
56 {
57         ports.push_back (p);
58
59         Modified ();
60 }
61
62 void
63 PortGroup::clear ()
64 {
65         _bundles.clear ();
66         ports.clear ();
67
68         Modified ();
69 }
70
71 bool
72 PortGroup::has_port (std::string const& p) const
73 {
74         for (ARDOUR::BundleList::const_iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
75                 if ((*i)->offers_port_alone (p)) {
76                         return true;
77                 }
78         }
79
80         for (vector<std::string>::const_iterator i = ports.begin(); i != ports.end(); ++i) {
81                 if (*i == p) {
82                         return true;
83                 }
84         }
85
86         return false;
87 }
88
89 boost::shared_ptr<ARDOUR::Bundle>
90 PortGroup::only_bundle ()
91 {
92         assert (_bundles.size() == 1);
93         return _bundles.front();
94 }
95
96
97 uint32_t
98 PortGroup::total_ports () const
99 {
100         uint32_t n = 0;
101         for (ARDOUR::BundleList::const_iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
102                 n += (*i)->nchannels ();
103         }
104
105         n += ports.size();
106
107         return n;
108 }
109
110         
111 /** PortGroupList constructor.
112  */
113
114 PortGroupList::PortGroupList ()
115         : _type (ARDOUR::DataType::AUDIO), _bundles_dirty (true)
116 {
117         
118 }
119
120 void
121 PortGroupList::set_type (ARDOUR::DataType t)
122 {
123         _type = t;
124         clear ();
125 }
126
127 /** Gather bundles from around the system and put them in this PortGroupList */
128 void
129 PortGroupList::gather (ARDOUR::Session& session, bool inputs)
130 {
131         clear ();
132
133         boost::shared_ptr<PortGroup> bus (new PortGroup (_("Bus")));
134         boost::shared_ptr<PortGroup> track (new PortGroup (_("Track")));
135         boost::shared_ptr<PortGroup> system (new PortGroup (_("System")));
136         boost::shared_ptr<PortGroup> other (new PortGroup (_("Other")));
137
138         /* Find the bundles for routes.  We take their bundles, copy them,
139            and add ports from the route's processors */
140
141         boost::shared_ptr<ARDOUR::RouteList> routes = session.get_routes ();
142
143         for (ARDOUR::RouteList::const_iterator i = routes->begin(); i != routes->end(); ++i) {
144                 /* Copy the appropriate bundle from the route */
145                 boost::shared_ptr<ARDOUR::Bundle> bundle (
146                         new ARDOUR::Bundle (
147                                 inputs ? (*i)->bundle_for_inputs() : (*i)->bundle_for_outputs ()
148                                 )
149                         );
150
151                 /* Add ports from the route's processors */
152                 uint32_t n = 0;
153                 while (1) {
154                         boost::shared_ptr<ARDOUR::Processor> p = (*i)->nth_processor (n);
155                         if (p == 0) {
156                                 break;
157                         }
158
159                         boost::shared_ptr<ARDOUR::IOProcessor> iop = boost::dynamic_pointer_cast<ARDOUR::IOProcessor> (p);
160
161                         if (iop) {
162                                 boost::shared_ptr<ARDOUR::Bundle> pb = inputs ?
163                                         iop->io()->bundle_for_inputs() : iop->io()->bundle_for_outputs();
164                                 bundle->add_channels_from_bundle (pb);
165                         }
166
167                         ++n;
168                 }
169                         
170                 /* Work out which group to put this bundle in */
171                 boost::shared_ptr<PortGroup> g;
172                 if (_type == ARDOUR::DataType::AUDIO) {
173
174                         if (boost::dynamic_pointer_cast<ARDOUR::AudioTrack> (*i)) {
175                                 g = track;
176                         } else if (!boost::dynamic_pointer_cast<ARDOUR::MidiTrack>(*i)) {
177                                 g = bus;
178                         } 
179
180
181                 } else if (_type == ARDOUR::DataType::MIDI) {
182
183                         if (boost::dynamic_pointer_cast<ARDOUR::MidiTrack> (*i)) {
184                                 g = track;
185                         }
186
187                         /* No MIDI busses yet */
188                 } 
189                         
190                 if (g) {
191                         g->add_bundle (bundle);
192                 }
193         }
194
195         /* Bundles created by the session.  We only add the mono ones,
196            otherwise there is duplication of the same ports within the matrix */
197         
198         boost::shared_ptr<ARDOUR::BundleList> b = session.bundles ();
199         for (ARDOUR::BundleList::iterator i = b->begin(); i != b->end(); ++i) {
200                 if ((*i)->nchannels() == 1 && (*i)->ports_are_inputs() == inputs && (*i)->type() == _type) {
201                         system->add_bundle (*i);
202                 }
203         }
204
205         /* Now find all other ports that we haven't thought of yet */
206
207         const char **ports = session.engine().get_ports ("", _type.to_jack_type(), inputs ? 
208                                                           JackPortIsInput : JackPortIsOutput);
209         if (ports) {
210
211                 int n = 0;
212                 string client_matching_string;
213
214                 client_matching_string = session.engine().client_name();
215                 client_matching_string += ':';
216
217                 while (ports[n]) {
218                         
219                         std::string const p = ports[n];
220
221                         if (!system->has_port(p) && !bus->has_port(p) && !track->has_port(p) && !other->has_port(p)) {
222                                 
223                                 if (port_has_prefix (p, "system:") ||
224                                     port_has_prefix (p, "alsa_pcm") ||
225                                     port_has_prefix (p, "ardour:")) {
226                                         system->add_port (p);
227                                 } else {
228                                         other->add_port (p);
229                                 }
230                         }
231                         
232                         ++n;
233                 }
234
235                 free (ports);
236         }
237
238         add_group (system);
239         add_group (bus);
240         add_group (track);
241         add_group (other);
242
243         _bundles_dirty = true;
244 }
245
246 bool
247 PortGroupList::port_has_prefix (const std::string& n, const std::string& p) const
248 {
249         return n.substr (0, p.length()) == p;
250 }
251         
252
253 void
254 PortGroupList::update_bundles () const
255 {
256         _bundles.clear ();
257                 
258         for (PortGroupList::List::const_iterator i = begin (); i != end (); ++i) {
259                 if ((*i)->visible()) {
260                         
261                         std::copy ((*i)->bundles().begin(), (*i)->bundles().end(), std::back_inserter (_bundles));
262
263                         /* make a bundle for the ports, if there are any */
264                         if (!(*i)->ports.empty()) {
265
266                                 boost::shared_ptr<ARDOUR::Bundle> b (new ARDOUR::Bundle ("", _type, !_offer_inputs));
267                                 
268                                 std::string const pre = common_prefix ((*i)->ports);
269                                 if (!pre.empty()) {
270                                         b->set_name (pre.substr (0, pre.length() - 1));
271                                 }
272
273                                 for (uint32_t j = 0; j < (*i)->ports.size(); ++j) {
274                                         std::string const p = (*i)->ports[j];
275                                         b->add_channel (p.substr (pre.length()));
276                                         b->set_port (j, p);
277                                 }
278                                         
279                                 _bundles.push_back (b);
280                         }
281                 }
282         }
283
284         _bundles_dirty = false;
285 }
286
287 std::string
288 PortGroupList::common_prefix (std::vector<std::string> const & p) const
289 {
290         /* common prefix before '/' ? */
291         if (p[0].find_first_of ("/") != std::string::npos) {
292                 std::string const fp = p[0].substr (0, (p[0].find_first_of ("/") + 1));
293                 uint32_t j = 1;
294                 while (j < p.size()) {
295                         if (p[j].substr (0, fp.length()) != fp) {
296                                 break;
297                         }
298                         ++j;
299                 }
300                 
301                 if (j == p.size()) {
302                         return fp;
303                 }
304         }
305         
306         /* or before ':' ? */
307         if (p[0].find_first_of (":") != std::string::npos) {
308                 std::string const fp = p[0].substr (0, (p[0].find_first_of (":") + 1));
309                 uint32_t j = 1;
310                 while (j < p.size()) {
311                         if (p[j].substr (0, fp.length()) != fp) {
312                                 break;
313                         }
314                         ++j;
315                 }
316                 
317                 if (j == p.size()) {
318                         return fp;
319                 }
320         }
321
322         return "";
323 }
324
325 void
326 PortGroupList::clear ()
327 {
328         _groups.clear ();
329         _bundles_dirty = true;
330 }
331
332 ARDOUR::BundleList const &
333 PortGroupList::bundles () const
334 {
335         if (_bundles_dirty) {
336                 update_bundles ();
337         }
338
339         return _bundles;
340 }
341
342 uint32_t
343 PortGroupList::total_visible_ports () const
344 {
345         uint32_t n = 0;
346         
347         for (PortGroupList::List::const_iterator i = begin(); i != end(); ++i) {
348                 if ((*i)->visible()) {
349                         n += (*i)->total_ports ();
350                 }
351         }
352
353         return n;
354 }
355
356 void
357 PortGroupList::group_modified ()
358 {
359         _bundles_dirty = true;
360 }
361
362 void
363 PortGroupList::add_group (boost::shared_ptr<PortGroup> g)
364 {
365         _groups.push_back (g);
366         g->Modified.connect (sigc::mem_fun (*this, &PortGroupList::group_modified));
367         _bundles_dirty = true;
368 }