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