Make global port matrix a Gtk::Window rather than an ArdourDialog.
[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 (ARDOUR::BundleList::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 type Type of bundles to offer (audio or MIDI)
115  *  @param offer_inputs true to offer output bundles, otherwise false.
116  */
117
118 PortGroupList::PortGroupList (ARDOUR::DataType type, bool offer_inputs)
119         : _type (type), _offer_inputs (offer_inputs),
120           _buss (_("Bus"), true),
121           _track (_("Track"), true),
122           _system (_("System"), true),
123           _other (_("Other"), true)
124 {
125         
126 }
127
128 /** Gather bundles from around the system and put them in this PortGroupList */
129 void
130 PortGroupList::gather (ARDOUR::Session& session)
131 {
132         clear_list ();
133
134         /* Find the bundles for routes */
135
136         boost::shared_ptr<ARDOUR::Session::RouteList> routes = session.get_routes ();
137
138         for (ARDOUR::Session::RouteList::const_iterator i = routes->begin(); i != routes->end(); ++i) {
139
140                 /* Route IO */
141                 PortGroup* g = 0;
142
143                 if (_type == ARDOUR::DataType::AUDIO) {
144
145                         if (boost::dynamic_pointer_cast<ARDOUR::AudioTrack> (*i)) {
146                                 g = &_track;
147                         } else if (!boost::dynamic_pointer_cast<ARDOUR::MidiTrack>(*i)) {
148                                 g = &_buss;
149                         } 
150
151
152                 } else if (_type == ARDOUR::DataType::MIDI) {
153
154                         if (boost::dynamic_pointer_cast<ARDOUR::MidiTrack> (*i)) {
155                                 g = &_track;
156                         }
157
158                         /* No MIDI busses yet */
159                 } 
160                         
161                 if (g) {
162                         g->add_bundle (_offer_inputs ? (*i)->bundle_for_inputs() : (*i)->bundle_for_outputs ());
163                 }
164
165                 /* Ports from this route's processors */
166
167                 uint32_t n = 0;
168                 while (1) {
169                         boost::shared_ptr<ARDOUR::Processor> p = (*i)->nth_processor (n);
170                         if (p == 0) {
171                                 break;
172                         }
173
174                         boost::shared_ptr<ARDOUR::IOProcessor> iop = boost::dynamic_pointer_cast<ARDOUR::IOProcessor> (p);
175                         if (iop) {
176                                 g->add_bundle (_offer_inputs ? iop->io()->bundle_for_inputs() : iop->io()->bundle_for_outputs());
177                         }
178
179                         ++n;
180                 }
181         }
182
183         /* Bundles created by the session */
184         
185         boost::shared_ptr<ARDOUR::BundleList> b = session.bundles ();
186         for (ARDOUR::BundleList::iterator i = b->begin(); i != b->end(); ++i) {
187                 if ((*i)->ports_are_inputs() == _offer_inputs && (*i)->type() == _type) {
188                         _system.bundles.push_back (*i);
189                 }
190         }
191
192         /* Now find all other ports that we haven't thought of yet */
193
194         const char **ports = session.engine().get_ports ("", _type.to_jack_type(), _offer_inputs ? 
195                                                           JackPortIsInput : JackPortIsOutput);
196         if (ports) {
197
198                 int n = 0;
199                 string client_matching_string;
200
201                 client_matching_string = session.engine().client_name();
202                 client_matching_string += ':';
203
204                 while (ports[n]) {
205                         
206                         std::string const p = ports[n];
207
208                         if (!_system.has_port(p) && !_buss.has_port(p) && !_track.has_port(p) && !_other.has_port(p)) {
209                                 
210                                 if (port_has_prefix (p, "system:") ||
211                                     port_has_prefix (p, "alsa_pcm") ||
212                                     port_has_prefix (p, "ardour:")) {
213                                         _system.add_port (p);
214                                 } else {
215                                         _other.add_port (p);
216                                 }
217                         }
218                         
219                         ++n;
220                 }
221
222                 free (ports);
223         }
224
225         push_back (&_system);
226         push_back (&_buss);
227         push_back (&_track);
228         push_back (&_other);
229
230         for (iterator i = begin(); i != end(); ++i) {
231                 _visibility_connections.push_back (
232                         (*i)->VisibilityChanged.connect (sigc::mem_fun (*this, &PortGroupList::visibility_changed))
233                         );
234         }
235 }
236
237 bool
238 PortGroupList::port_has_prefix (const std::string& n, const std::string& p) const
239 {
240         return n.substr (0, p.length()) == p;
241 }
242         
243
244 void
245 PortGroupList::set_type (ARDOUR::DataType t)
246 {
247         _type = t;
248 }
249
250 void
251 PortGroupList::set_offer_inputs (bool i)
252 {
253         _offer_inputs = i;
254 }
255
256 ARDOUR::BundleList
257 PortGroupList::bundles () const
258 {
259         ARDOUR::BundleList bundles;
260                 
261         for (const_iterator i = begin (); i != end (); ++i) {
262                 if ((*i)->visible()) {
263                         
264                         std::copy ((*i)->bundles.begin(), (*i)->bundles.end(), std::back_inserter (bundles));
265                         
266                         /* make a bundle for the ports, if there are any */
267                         if (!(*i)->ports.empty()) {
268
269                                 boost::shared_ptr<ARDOUR::Bundle> b (new ARDOUR::Bundle ("", _type, !_offer_inputs));
270                                 
271                                 std::string const pre = common_prefix ((*i)->ports);
272                                 if (!pre.empty()) {
273                                         b->set_name (pre.substr (0, pre.length() - 1));
274                                 }
275
276                                 for (uint32_t j = 0; j < (*i)->ports.size(); ++j) {
277                                         std::string const p = (*i)->ports[j];
278                                         b->add_channel (p.substr (pre.length()));
279                                         b->set_port (j, p);
280                                 }
281                                         
282                                 bundles.push_back (b);
283                         }
284                 }
285         }
286
287         return bundles;
288 }
289
290 std::string
291 PortGroupList::common_prefix (std::vector<std::string> const & p) const
292 {
293         /* common prefix before '/' ? */
294         if (p[0].find_first_of ("/") != std::string::npos) {
295                 std::string const fp = p[0].substr (0, (p[0].find_first_of ("/") + 1));
296                 uint32_t j = 1;
297                 while (j < p.size()) {
298                         if (p[j].substr (0, fp.length()) != fp) {
299                                 break;
300                         }
301                         ++j;
302                 }
303                 
304                 if (j == p.size()) {
305                         return fp;
306                 }
307         }
308         
309         /* or before ':' ? */
310         if (p[0].find_first_of (":") != std::string::npos) {
311                 std::string const fp = p[0].substr (0, (p[0].find_first_of (":") + 1));
312                 uint32_t j = 1;
313                 while (j < p.size()) {
314                         if (p[j].substr (0, fp.length()) != fp) {
315                                 break;
316                         }
317                         ++j;
318                 }
319                 
320                 if (j == p.size()) {
321                         return fp;
322                 }
323         }
324
325         return "";
326 }
327
328 void
329 PortGroupList::visibility_changed ()
330 {
331         VisibilityChanged ();
332 }
333
334 void
335 PortGroupList::take_visibility_from (PortGroupList const & o)
336 {
337         iterator i = begin ();
338         const_iterator j = o.begin ();
339
340         while (i != end() && j != o.end()) {
341                 (*i)->set_visible ((*j)->visible());
342                 ++i;
343                 ++j;
344         }
345 }
346
347 void
348 PortGroupList::clear_list ()
349 {
350         clear ();
351
352         _buss.clear ();
353         _track.clear ();
354         _system.clear ();
355         _other.clear ();
356
357         for (std::vector<sigc::connection>::iterator i = _visibility_connections.begin(); i != _visibility_connections.end(); ++i) {
358                 i->disconnect ();
359         }
360
361         _visibility_connections.clear ();
362 }