Gather all ardour: ports so that a few more things are found to put in the port matrix.
[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         bundles.push_back (b);
42 }
43
44 /** Add a port to a group.
45  *  @param p Port.
46  */
47 void
48 PortGroup::add_port (std::string const &p)
49 {
50         ports.push_back (p);
51 }
52
53 void
54 PortGroup::clear ()
55 {
56         bundles.clear ();
57         ports.clear ();
58 }
59
60 bool
61 PortGroup::has_port (std::string const& p) const
62 {
63         for (vector<boost::shared_ptr<ARDOUR::Bundle> >::const_iterator i = bundles.begin(); i != bundles.end(); ++i) {
64                 if ((*i)->offers_port_alone (p)) {
65                         return true;
66                 }
67         }
68
69         for (vector<std::string>::const_iterator i = ports.begin(); i != ports.end(); ++i) {
70                 if (*i == p) {
71                         return true;
72                 }
73         }
74
75         return false;
76 }
77         
78
79 /** PortGroupUI constructor.
80  *  @param m PortMatrix to work for.
81  *  @Param g PortGroup to represent.
82  */
83
84 PortGroupUI::PortGroupUI (PortMatrix* m, PortGroup* g)
85         : _port_matrix (m)
86         , _port_group (g)
87         , _visibility_checkbutton (g->name)
88 {
89         _port_group->set_visible (true);
90         setup_visibility_checkbutton ();
91         
92         _visibility_checkbutton.signal_toggled().connect (sigc::mem_fun (*this, &PortGroupUI::visibility_checkbutton_toggled));
93 }
94
95 /** The visibility of a PortGroupUI has been toggled */
96 void
97 PortGroupUI::visibility_checkbutton_toggled ()
98 {
99         _port_group->set_visible (_visibility_checkbutton.get_active ());
100         setup_visibility_checkbutton ();
101         _port_matrix->setup ();
102 }
103
104 /** Set up the visibility checkbutton according to PortGroup::visible */
105 void
106 PortGroupUI::setup_visibility_checkbutton ()
107 {
108         if (_visibility_checkbutton.get_active () != _port_group->visible()) {
109                 _visibility_checkbutton.set_active (_port_group->visible());
110         }
111 }
112
113 /** PortGroupList constructor.
114  *  @param session Session to get bundles from.
115  *  @param type Type of bundles to offer (audio or MIDI)
116  *  @param offer_inputs true to offer output bundles, otherwise false.
117  *  @param mask Mask of groups to make visible by default.
118  */
119
120 PortGroupList::PortGroupList (ARDOUR::Session & session, ARDOUR::DataType type, bool offer_inputs, Mask mask)
121         : _session (session), _type (type), _offer_inputs (offer_inputs),
122           _buss (_("Bus"), mask & BUSS),
123           _track (_("Track"), mask & TRACK),
124           _system (_("System"), mask & SYSTEM),
125           _other (_("Other"), mask & OTHER)
126 {
127         refresh ();
128
129         for (iterator i = begin(); i != end(); ++i) {
130                 (*i)->VisibilityChanged.connect (sigc::mem_fun (*this, &PortGroupList::visibility_changed));
131         }
132 }
133
134 /** Find or re-find all our bundles and set up our lists */
135 void
136 PortGroupList::refresh ()
137 {
138         clear ();
139
140         _buss.clear ();
141         _track.clear ();
142         _system.clear ();
143         _other.clear ();
144
145         /* Find the bundles for routes */
146
147         boost::shared_ptr<ARDOUR::Session::RouteList> routes = _session.get_routes ();
148
149         for (ARDOUR::Session::RouteList::const_iterator i = routes->begin(); i != routes->end(); ++i) {
150
151                 PortGroup* g = 0;
152
153                 if (_type == ARDOUR::DataType::AUDIO) {
154
155                         if (boost::dynamic_pointer_cast<ARDOUR::AudioTrack> (*i)) {
156                                 g = &_track;
157                         } else if (!boost::dynamic_pointer_cast<ARDOUR::MidiTrack>(*i)) {
158                                 g = &_buss;
159                         } 
160
161
162                 } else if (_type == ARDOUR::DataType::MIDI) {
163
164                         if (boost::dynamic_pointer_cast<ARDOUR::MidiTrack> (*i)) {
165                                 g = &_track;
166                         }
167
168                         /* No MIDI busses yet */
169                 } 
170                         
171                 if (g) {
172                         g->add_bundle (_offer_inputs ? (*i)->bundle_for_inputs() : (*i)->bundle_for_outputs ());
173                 }
174         }
175
176         /* Bundles created by the session */
177         _session.foreach_bundle (sigc::mem_fun (*this, &PortGroupList::maybe_add_session_bundle));
178         
179         /* XXX: inserts, sends, plugin inserts? */
180         
181         /* Now find all other ports that we haven't thought of yet */
182
183         const char **ports = _session.engine().get_ports ("", _type.to_jack_type(), _offer_inputs ? 
184                                                           JackPortIsInput : JackPortIsOutput);
185         if (ports) {
186
187                 int n = 0;
188                 string client_matching_string;
189
190                 client_matching_string = _session.engine().client_name();
191                 client_matching_string += ':';
192
193                 while (ports[n]) {
194                         
195                         std::string const p = ports[n];
196
197                         if (!_system.has_port(p) && !_buss.has_port(p) && !_track.has_port(p) && !_other.has_port(p)) {
198                                 
199                                 if (port_has_prefix (p, "system:") ||
200                                     port_has_prefix (p, "alsa_pcm") ||
201                                     port_has_prefix (p, "ardour:")) {
202                                         _system.add_port (p);
203                                 } else {
204                                         _other.add_port (p);
205                                 }
206                         }
207                         
208                         ++n;
209                 }
210
211                 free (ports);
212         }
213
214         push_back (&_system);
215         push_back (&_buss);
216         push_back (&_track);
217         push_back (&_other);
218 }
219
220 bool
221 PortGroupList::port_has_prefix (const std::string& n, const std::string& p) const
222 {
223         return n.substr (0, p.length()) == p;
224 }
225         
226
227 void
228 PortGroupList::set_type (ARDOUR::DataType t)
229 {
230         _type = t;
231 }
232
233 void
234 PortGroupList::set_offer_inputs (bool i)
235 {
236         _offer_inputs = i;
237 }
238
239 void
240 PortGroupList::maybe_add_session_bundle (boost::shared_ptr<ARDOUR::Bundle> b)
241 {
242         if (b->ports_are_inputs () == _offer_inputs && b->type () == _type) {
243                 _system.bundles.push_back (b);
244         }
245 }
246
247 std::vector<boost::shared_ptr<ARDOUR::Bundle> >
248 PortGroupList::bundles ()
249 {
250         std::vector<boost::shared_ptr<ARDOUR::Bundle> > bundles;
251                 
252         for (iterator i = begin (); i != end (); ++i) {
253                 if ((*i)->visible()) {
254                         
255                         std::copy ((*i)->bundles.begin(), (*i)->bundles.end(), std::back_inserter (bundles));
256                         
257                         /* make a bundle for the ports, if there are any */
258                         if (!(*i)->ports.empty()) {
259
260                                 boost::shared_ptr<ARDOUR::Bundle> b (new ARDOUR::Bundle ("", _type, !_offer_inputs));
261                                 
262                                 std::string const pre = common_prefix ((*i)->ports);
263                                 if (!pre.empty()) {
264                                         b->set_name (pre.substr (0, pre.length() - 1));
265                                 }
266
267                                 for (uint32_t j = 0; j < (*i)->ports.size(); ++j) {
268                                         std::string const p = (*i)->ports[j];
269                                         b->add_channel (p.substr (pre.length()));
270                                         b->set_port (j, p);
271                                 }
272                                         
273                                 bundles.push_back (b);
274                         }
275                 }
276         }
277
278         return bundles;
279 }
280
281 std::string
282 PortGroupList::common_prefix (std::vector<std::string> const & p) const
283 {
284         /* common prefix before '/' ? */
285         if (p[0].find_first_of ("/") != std::string::npos) {
286                 std::string const fp = p[0].substr (0, (p[0].find_first_of ("/") + 1));
287                 uint32_t j = 1;
288                 while (j < p.size()) {
289                         if (p[j].substr (0, fp.length()) != fp) {
290                                 break;
291                         }
292                         ++j;
293                 }
294                 
295                 if (j == p.size()) {
296                         return fp;
297                 }
298         }
299         
300         /* or before ':' ? */
301         if (p[0].find_first_of (":") != std::string::npos) {
302                 std::string const fp = p[0].substr (0, (p[0].find_first_of (":") + 1));
303                 uint32_t j = 1;
304                 while (j < p.size()) {
305                         if (p[j].substr (0, fp.length()) != fp) {
306                                 break;
307                         }
308                         ++j;
309                 }
310                 
311                 if (j == p.size()) {
312                         return fp;
313                 }
314         }
315
316         return "";
317 }
318
319 void
320 PortGroupList::visibility_changed ()
321 {
322         VisibilityChanged ();
323 }
324
325 void
326 PortGroupList::take_visibility_from (PortGroupList const & o)
327 {
328         iterator i = begin ();
329         const_iterator j = o.begin ();
330
331         while (i != end() && j != o.end()) {
332                 (*i)->set_visible ((*j)->visible());
333                 ++i;
334                 ++j;
335         }
336 }