2 Copyright (C) 2002-2009 Paul Davis
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.
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.
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.
20 #include "port_group.h"
21 #include "port_matrix.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>
35 /** Add a bundle to a group.
39 PortGroup::add_bundle (boost::shared_ptr<ARDOUR::Bundle> b)
42 _bundles.push_back (b);
45 /** Add a port to a group.
49 PortGroup::add_port (std::string const &p)
62 PortGroup::has_port (std::string const& p) const
64 for (ARDOUR::BundleList::const_iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
65 if ((*i)->offers_port_alone (p)) {
70 for (vector<std::string>::const_iterator i = ports.begin(); i != ports.end(); ++i) {
79 boost::shared_ptr<ARDOUR::Bundle>
80 PortGroup::only_bundle ()
82 assert (_bundles.size() == 1);
83 return _bundles.front();
87 /** PortGroupUI constructor.
88 * @param m PortMatrix to work for.
89 * @Param g PortGroup to represent.
92 PortGroupUI::PortGroupUI (PortMatrix* m, PortGroup* g)
95 , _visibility_checkbutton (g->name)
97 _port_group->set_visible (true);
98 setup_visibility_checkbutton ();
100 _visibility_checkbutton.signal_toggled().connect (sigc::mem_fun (*this, &PortGroupUI::visibility_checkbutton_toggled));
103 /** The visibility of a PortGroupUI has been toggled */
105 PortGroupUI::visibility_checkbutton_toggled ()
107 _port_group->set_visible (_visibility_checkbutton.get_active ());
108 setup_visibility_checkbutton ();
109 _port_matrix->setup ();
112 /** Set up the visibility checkbutton according to PortGroup::visible */
114 PortGroupUI::setup_visibility_checkbutton ()
116 if (_visibility_checkbutton.get_active () != _port_group->visible()) {
117 _visibility_checkbutton.set_active (_port_group->visible());
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.
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)
136 /** Gather bundles from around the system and put them in this PortGroupList */
138 PortGroupList::gather (ARDOUR::Session& session)
142 /* Find the bundles for routes. We take their bundles, copy them,
143 and add ports from the route's processors */
145 boost::shared_ptr<ARDOUR::RouteList> routes = session.get_routes ();
147 for (ARDOUR::RouteList::const_iterator i = routes->begin(); i != routes->end(); ++i) {
148 /* Copy the appropriate bundle from the route */
149 boost::shared_ptr<ARDOUR::Bundle> bundle (
151 _offer_inputs ? (*i)->bundle_for_inputs() : (*i)->bundle_for_outputs ()
155 /* Add ports from the route's processors */
158 boost::shared_ptr<ARDOUR::Processor> p = (*i)->nth_processor (n);
163 boost::shared_ptr<ARDOUR::IOProcessor> iop = boost::dynamic_pointer_cast<ARDOUR::IOProcessor> (p);
166 boost::shared_ptr<ARDOUR::Bundle> pb = _offer_inputs ?
167 iop->io()->bundle_for_inputs() : iop->io()->bundle_for_outputs();
168 bundle->add_channels_from_bundle (pb);
174 /* Work out which group to put this bundle in */
176 if (_type == ARDOUR::DataType::AUDIO) {
178 if (boost::dynamic_pointer_cast<ARDOUR::AudioTrack> (*i)) {
180 } else if (!boost::dynamic_pointer_cast<ARDOUR::MidiTrack>(*i)) {
185 } else if (_type == ARDOUR::DataType::MIDI) {
187 if (boost::dynamic_pointer_cast<ARDOUR::MidiTrack> (*i)) {
191 /* No MIDI busses yet */
195 g->add_bundle (bundle);
199 /* Bundles created by the session */
201 boost::shared_ptr<ARDOUR::BundleList> b = session.bundles ();
202 for (ARDOUR::BundleList::iterator i = b->begin(); i != b->end(); ++i) {
203 if ((*i)->ports_are_inputs() == _offer_inputs && (*i)->type() == _type) {
204 _system.add_bundle (*i);
208 /* Now find all other ports that we haven't thought of yet */
210 const char **ports = session.engine().get_ports ("", _type.to_jack_type(), _offer_inputs ?
211 JackPortIsInput : JackPortIsOutput);
215 string client_matching_string;
217 client_matching_string = session.engine().client_name();
218 client_matching_string += ':';
222 std::string const p = ports[n];
224 if (!_system.has_port(p) && !_buss.has_port(p) && !_track.has_port(p) && !_other.has_port(p)) {
226 if (port_has_prefix (p, "system:") ||
227 port_has_prefix (p, "alsa_pcm") ||
228 port_has_prefix (p, "ardour:")) {
229 _system.add_port (p);
241 push_back (&_system);
246 for (iterator i = begin(); i != end(); ++i) {
247 _visibility_connections.push_back (
248 (*i)->VisibilityChanged.connect (sigc::mem_fun (*this, &PortGroupList::visibility_changed))
252 _bundles_dirty = true;
256 PortGroupList::port_has_prefix (const std::string& n, const std::string& p) const
258 return n.substr (0, p.length()) == p;
263 PortGroupList::set_type (ARDOUR::DataType t)
266 _bundles_dirty = true;
270 PortGroupList::set_offer_inputs (bool i)
273 _bundles_dirty = true;
277 PortGroupList::update_bundles () const
281 for (const_iterator i = begin (); i != end (); ++i) {
282 if ((*i)->visible()) {
284 std::copy ((*i)->bundles().begin(), (*i)->bundles().end(), std::back_inserter (_bundles));
286 /* make a bundle for the ports, if there are any */
287 if (!(*i)->ports.empty()) {
289 boost::shared_ptr<ARDOUR::Bundle> b (new ARDOUR::Bundle ("", _type, !_offer_inputs));
291 std::string const pre = common_prefix ((*i)->ports);
293 b->set_name (pre.substr (0, pre.length() - 1));
296 for (uint32_t j = 0; j < (*i)->ports.size(); ++j) {
297 std::string const p = (*i)->ports[j];
298 b->add_channel (p.substr (pre.length()));
302 _bundles.push_back (b);
307 _bundles_dirty = false;
311 PortGroupList::common_prefix (std::vector<std::string> const & p) const
313 /* common prefix before '/' ? */
314 if (p[0].find_first_of ("/") != std::string::npos) {
315 std::string const fp = p[0].substr (0, (p[0].find_first_of ("/") + 1));
317 while (j < p.size()) {
318 if (p[j].substr (0, fp.length()) != fp) {
329 /* or before ':' ? */
330 if (p[0].find_first_of (":") != std::string::npos) {
331 std::string const fp = p[0].substr (0, (p[0].find_first_of (":") + 1));
333 while (j < p.size()) {
334 if (p[j].substr (0, fp.length()) != fp) {
349 PortGroupList::visibility_changed ()
351 VisibilityChanged ();
355 PortGroupList::take_visibility_from (PortGroupList const & o)
357 iterator i = begin ();
358 const_iterator j = o.begin ();
360 while (i != end() && j != o.end()) {
361 (*i)->set_visible ((*j)->visible());
368 PortGroupList::clear_list ()
377 for (std::vector<sigc::connection>::iterator i = _visibility_connections.begin(); i != _visibility_connections.end(); ++i) {
381 _visibility_connections.clear ();
382 _bundles_dirty = true;
385 ARDOUR::BundleList const &
386 PortGroupList::bundles () const
388 if (_bundles_dirty) {