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 */
144 boost::shared_ptr<ARDOUR::Session::RouteList> routes = session.get_routes ();
146 for (ARDOUR::Session::RouteList::const_iterator i = routes->begin(); i != routes->end(); ++i) {
151 if (_type == ARDOUR::DataType::AUDIO) {
153 if (boost::dynamic_pointer_cast<ARDOUR::AudioTrack> (*i)) {
155 } else if (!boost::dynamic_pointer_cast<ARDOUR::MidiTrack>(*i)) {
160 } else if (_type == ARDOUR::DataType::MIDI) {
162 if (boost::dynamic_pointer_cast<ARDOUR::MidiTrack> (*i)) {
166 /* No MIDI busses yet */
170 g->add_bundle (_offer_inputs ? (*i)->bundle_for_inputs() : (*i)->bundle_for_outputs ());
173 /* Ports from this route's processors */
177 boost::shared_ptr<ARDOUR::Processor> p = (*i)->nth_processor (n);
182 boost::shared_ptr<ARDOUR::IOProcessor> iop = boost::dynamic_pointer_cast<ARDOUR::IOProcessor> (p);
184 g->add_bundle (_offer_inputs ? iop->io()->bundle_for_inputs() : iop->io()->bundle_for_outputs());
191 /* Bundles created by the session */
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);
200 /* Now find all other ports that we haven't thought of yet */
202 const char **ports = session.engine().get_ports ("", _type.to_jack_type(), _offer_inputs ?
203 JackPortIsInput : JackPortIsOutput);
207 string client_matching_string;
209 client_matching_string = session.engine().client_name();
210 client_matching_string += ':';
214 std::string const p = ports[n];
216 if (!_system.has_port(p) && !_buss.has_port(p) && !_track.has_port(p) && !_other.has_port(p)) {
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);
233 push_back (&_system);
238 for (iterator i = begin(); i != end(); ++i) {
239 _visibility_connections.push_back (
240 (*i)->VisibilityChanged.connect (sigc::mem_fun (*this, &PortGroupList::visibility_changed))
244 _bundles_dirty = true;
248 PortGroupList::port_has_prefix (const std::string& n, const std::string& p) const
250 return n.substr (0, p.length()) == p;
255 PortGroupList::set_type (ARDOUR::DataType t)
258 _bundles_dirty = true;
262 PortGroupList::set_offer_inputs (bool i)
265 _bundles_dirty = true;
269 PortGroupList::update_bundles () const
273 for (const_iterator i = begin (); i != end (); ++i) {
274 if ((*i)->visible()) {
276 std::copy ((*i)->bundles().begin(), (*i)->bundles().end(), std::back_inserter (_bundles));
278 /* make a bundle for the ports, if there are any */
279 if (!(*i)->ports.empty()) {
281 boost::shared_ptr<ARDOUR::Bundle> b (new ARDOUR::Bundle ("", _type, !_offer_inputs));
283 std::string const pre = common_prefix ((*i)->ports);
285 b->set_name (pre.substr (0, pre.length() - 1));
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()));
294 _bundles.push_back (b);
299 _bundles_dirty = false;
303 PortGroupList::common_prefix (std::vector<std::string> const & p) const
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));
309 while (j < p.size()) {
310 if (p[j].substr (0, fp.length()) != fp) {
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));
325 while (j < p.size()) {
326 if (p[j].substr (0, fp.length()) != fp) {
341 PortGroupList::visibility_changed ()
343 VisibilityChanged ();
347 PortGroupList::take_visibility_from (PortGroupList const & o)
349 iterator i = begin ();
350 const_iterator j = o.begin ();
352 while (i != end() && j != o.end()) {
353 (*i)->set_visible ((*j)->visible());
360 PortGroupList::clear_list ()
369 for (std::vector<sigc::connection>::iterator i = _visibility_connections.begin(); i != _visibility_connections.end(); ++i) {
373 _visibility_connections.clear ();
374 _bundles_dirty = true;
377 ARDOUR::BundleList const &
378 PortGroupList::bundles () const
380 if (_bundles_dirty) {