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