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)
41 bundles.push_back (b);
44 /** Add a port to a group.
48 PortGroup::add_port (std::string const &p)
61 PortGroup::has_port (std::string const& p) const
63 for (vector<boost::shared_ptr<ARDOUR::Bundle> >::const_iterator i = bundles.begin(); i != bundles.end(); ++i) {
64 if ((*i)->offers_port_alone (p)) {
69 for (vector<std::string>::const_iterator i = ports.begin(); i != ports.end(); ++i) {
79 /** PortGroupUI constructor.
80 * @param m PortMatrix to work for.
81 * @Param g PortGroup to represent.
84 PortGroupUI::PortGroupUI (PortMatrix* m, PortGroup* g)
87 , _visibility_checkbutton (g->name)
89 _port_group->set_visible (true);
90 setup_visibility_checkbutton ();
92 _visibility_checkbutton.signal_toggled().connect (sigc::mem_fun (*this, &PortGroupUI::visibility_checkbutton_toggled));
95 /** The visibility of a PortGroupUI has been toggled */
97 PortGroupUI::visibility_checkbutton_toggled ()
99 _port_group->set_visible (_visibility_checkbutton.get_active ());
100 setup_visibility_checkbutton ();
101 _port_matrix->setup ();
104 /** Set up the visibility checkbutton according to PortGroup::visible */
106 PortGroupUI::setup_visibility_checkbutton ()
108 if (_visibility_checkbutton.get_active () != _port_group->visible()) {
109 _visibility_checkbutton.set_active (_port_group->visible());
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.
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)
129 for (iterator i = begin(); i != end(); ++i) {
130 (*i)->VisibilityChanged.connect (sigc::mem_fun (*this, &PortGroupList::visibility_changed));
134 /** Find or re-find all our bundles and set up our lists */
136 PortGroupList::refresh ()
145 /* Find the bundles for routes */
147 boost::shared_ptr<ARDOUR::Session::RouteList> routes = _session.get_routes ();
149 for (ARDOUR::Session::RouteList::const_iterator i = routes->begin(); i != routes->end(); ++i) {
153 if (_type == ARDOUR::DataType::AUDIO) {
155 if (boost::dynamic_pointer_cast<ARDOUR::AudioTrack> (*i)) {
157 } else if (!boost::dynamic_pointer_cast<ARDOUR::MidiTrack>(*i)) {
162 } else if (_type == ARDOUR::DataType::MIDI) {
164 if (boost::dynamic_pointer_cast<ARDOUR::MidiTrack> (*i)) {
168 /* No MIDI busses yet */
172 g->add_bundle (_offer_inputs ? (*i)->bundle_for_inputs() : (*i)->bundle_for_outputs ());
176 /* Bundles created by the session */
177 _session.foreach_bundle (sigc::mem_fun (*this, &PortGroupList::maybe_add_session_bundle));
179 /* XXX: inserts, sends, plugin inserts? */
181 /* Now find all other ports that we haven't thought of yet */
183 const char **ports = _session.engine().get_ports ("", _type.to_jack_type(), _offer_inputs ?
184 JackPortIsInput : JackPortIsOutput);
188 string client_matching_string;
190 client_matching_string = _session.engine().client_name();
191 client_matching_string += ':';
195 std::string const p = ports[n];
197 if (!_system.has_port(p) && !_buss.has_port(p) && !_track.has_port(p) && !_other.has_port(p)) {
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);
214 push_back (&_system);
221 PortGroupList::port_has_prefix (const std::string& n, const std::string& p) const
223 return n.substr (0, p.length()) == p;
228 PortGroupList::set_type (ARDOUR::DataType t)
234 PortGroupList::set_offer_inputs (bool i)
240 PortGroupList::maybe_add_session_bundle (boost::shared_ptr<ARDOUR::Bundle> b)
242 if (b->ports_are_inputs () == _offer_inputs && b->type () == _type) {
243 _system.bundles.push_back (b);
247 std::vector<boost::shared_ptr<ARDOUR::Bundle> >
248 PortGroupList::bundles ()
250 std::vector<boost::shared_ptr<ARDOUR::Bundle> > bundles;
252 for (iterator i = begin (); i != end (); ++i) {
253 if ((*i)->visible()) {
255 std::copy ((*i)->bundles.begin(), (*i)->bundles.end(), std::back_inserter (bundles));
257 /* make a bundle for the ports, if there are any */
258 if (!(*i)->ports.empty()) {
260 boost::shared_ptr<ARDOUR::Bundle> b (new ARDOUR::Bundle ("", _type, !_offer_inputs));
262 std::string const pre = common_prefix ((*i)->ports);
264 b->set_name (pre.substr (0, pre.length() - 1));
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()));
273 bundles.push_back (b);
282 PortGroupList::common_prefix (std::vector<std::string> const & p) const
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));
288 while (j < p.size()) {
289 if (p[j].substr (0, fp.length()) != fp) {
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));
304 while (j < p.size()) {
305 if (p[j].substr (0, fp.length()) != fp) {
320 PortGroupList::visibility_changed ()
322 VisibilityChanged ();
326 PortGroupList::take_visibility_from (PortGroupList const & o)
328 iterator i = begin ();
329 const_iterator j = o.begin ();
331 while (i != end() && j != o.end()) {
332 (*i)->set_visible ((*j)->visible());