Trim include dependency graph, especially for io.h and session.h.
[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> buss (new PortGroup (_("Buss")));
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 = buss;
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 */
196         
197         boost::shared_ptr<ARDOUR::BundleList> b = session.bundles ();
198         for (ARDOUR::BundleList::iterator i = b->begin(); i != b->end(); ++i) {
199                 if ((*i)->ports_are_inputs() == inputs && (*i)->type() == _type) {
200                         system->add_bundle (*i);
201                 }
202         }
203
204         /* Now find all other ports that we haven't thought of yet */
205
206         const char **ports = session.engine().get_ports ("", _type.to_jack_type(), inputs ? 
207                                                           JackPortIsInput : JackPortIsOutput);
208         if (ports) {
209
210                 int n = 0;
211                 string client_matching_string;
212
213                 client_matching_string = session.engine().client_name();
214                 client_matching_string += ':';
215
216                 while (ports[n]) {
217                         
218                         std::string const p = ports[n];
219
220                         if (!system->has_port(p) && !buss->has_port(p) && !track->has_port(p) && !other->has_port(p)) {
221                                 
222                                 if (port_has_prefix (p, "system:") ||
223                                     port_has_prefix (p, "alsa_pcm") ||
224                                     port_has_prefix (p, "ardour:")) {
225                                         system->add_port (p);
226                                 } else {
227                                         other->add_port (p);
228                                 }
229                         }
230                         
231                         ++n;
232                 }
233
234                 free (ports);
235         }
236
237         add_group (system);
238         add_group (buss);
239         add_group (track);
240         add_group (other);
241
242         _bundles_dirty = true;
243 }
244
245 bool
246 PortGroupList::port_has_prefix (const std::string& n, const std::string& p) const
247 {
248         return n.substr (0, p.length()) == p;
249 }
250         
251
252 void
253 PortGroupList::update_bundles () const
254 {
255         _bundles.clear ();
256                 
257         for (PortGroupList::List::const_iterator i = begin (); i != end (); ++i) {
258                 if ((*i)->visible()) {
259                         
260                         std::copy ((*i)->bundles().begin(), (*i)->bundles().end(), std::back_inserter (_bundles));
261
262                         /* make a bundle for the ports, if there are any */
263                         if (!(*i)->ports.empty()) {
264
265                                 boost::shared_ptr<ARDOUR::Bundle> b (new ARDOUR::Bundle ("", _type, !_offer_inputs));
266                                 
267                                 std::string const pre = common_prefix ((*i)->ports);
268                                 if (!pre.empty()) {
269                                         b->set_name (pre.substr (0, pre.length() - 1));
270                                 }
271
272                                 for (uint32_t j = 0; j < (*i)->ports.size(); ++j) {
273                                         std::string const p = (*i)->ports[j];
274                                         b->add_channel (p.substr (pre.length()));
275                                         b->set_port (j, p);
276                                 }
277                                         
278                                 _bundles.push_back (b);
279                         }
280                 }
281         }
282
283         _bundles_dirty = false;
284 }
285
286 std::string
287 PortGroupList::common_prefix (std::vector<std::string> const & p) const
288 {
289         /* common prefix before '/' ? */
290         if (p[0].find_first_of ("/") != std::string::npos) {
291                 std::string const fp = p[0].substr (0, (p[0].find_first_of ("/") + 1));
292                 uint32_t j = 1;
293                 while (j < p.size()) {
294                         if (p[j].substr (0, fp.length()) != fp) {
295                                 break;
296                         }
297                         ++j;
298                 }
299                 
300                 if (j == p.size()) {
301                         return fp;
302                 }
303         }
304         
305         /* or before ':' ? */
306         if (p[0].find_first_of (":") != std::string::npos) {
307                 std::string const fp = p[0].substr (0, (p[0].find_first_of (":") + 1));
308                 uint32_t j = 1;
309                 while (j < p.size()) {
310                         if (p[j].substr (0, fp.length()) != fp) {
311                                 break;
312                         }
313                         ++j;
314                 }
315                 
316                 if (j == p.size()) {
317                         return fp;
318                 }
319         }
320
321         return "";
322 }
323
324 void
325 PortGroupList::clear ()
326 {
327         _groups.clear ();
328         _bundles_dirty = true;
329 }
330
331 ARDOUR::BundleList const &
332 PortGroupList::bundles () const
333 {
334         if (_bundles_dirty) {
335                 update_bundles ();
336         }
337
338         return _bundles;
339 }
340
341 uint32_t
342 PortGroupList::total_visible_ports () const
343 {
344         uint32_t n = 0;
345         
346         for (PortGroupList::List::const_iterator i = begin(); i != end(); ++i) {
347                 if ((*i)->visible()) {
348                         n += (*i)->total_ports ();
349                 }
350         }
351
352         return n;
353 }
354
355 void
356 PortGroupList::group_modified ()
357 {
358         _bundles_dirty = true;
359 }
360
361 void
362 PortGroupList::add_group (boost::shared_ptr<PortGroup> g)
363 {
364         _groups.push_back (g);
365         g->Modified.connect (sigc::mem_fun (*this, &PortGroupList::group_modified));
366         _bundles_dirty = true;
367 }