File missing from previous commit.
[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 "i18n.h"
22 #include "ardour/session.h"
23 #include "ardour/audio_track.h"
24 #include "ardour/midi_track.h"
25 #include "ardour/audioengine.h"
26 #include <boost/shared_ptr.hpp>
27 #include <cstring>
28
29 using namespace std;
30 using namespace Gtk;
31
32 /** Add a port to a group.
33  *  @param p Port name, with or without prefix.
34  */
35 void
36 PortGroup::add (std::string const & p)
37 {
38         if (prefix.empty() == false && p.substr (0, prefix.length()) == prefix) {
39                 ports.push_back (p.substr (prefix.length()));
40         } else {
41                 ports.push_back (p);
42         }
43 }
44
45 /** PortGroupUI constructor.
46  *  @param m PortMatrix to work for.
47  *  @Param g PortGroup to represent.
48  */
49
50 PortGroupUI::PortGroupUI (PortMatrix& m, PortGroup& g)
51         : _port_matrix (m)
52         , _port_group (g)
53         , _ignore_check_button_toggle (false)
54         , _visibility_checkbutton (g.name)
55 {
56         _port_group.visible = true;
57         _ignore_check_button_toggle = false;
58         _visibility_checkbutton.signal_toggled().connect (sigc::mem_fun (*this, &PortGroupUI::visibility_checkbutton_toggled));
59 }
60
61 /** The visibility of a PortGroupUI has been toggled */
62 void
63 PortGroupUI::visibility_checkbutton_toggled ()
64 {
65         _port_group.visible = _visibility_checkbutton.get_active ();
66 }
67
68 /** @return Checkbutton used to toggle visibility */
69 Widget&
70 PortGroupUI::get_visibility_checkbutton ()
71 {
72         return _visibility_checkbutton;
73 }
74
75
76 /** Handle a toggle of a port check button */
77 void
78 PortGroupUI::port_checkbutton_toggled (CheckButton* b, int r, int c)
79 {
80         if (_ignore_check_button_toggle == false) {
81                 // _port_matrix.hide_group (_port_group);
82         }
83 }
84
85 /** Set up visibility of the port group according to PortGroup::visible */
86 void
87 PortGroupUI::setup_visibility ()
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 ports from.
96  *  @param type Type of ports to offer (audio or MIDI)
97  *  @param offer_inputs true to offer output ports, 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"), "ardour:", mask & BUSS),
104           _track (_("Track"), "ardour:", mask & TRACK),
105           _system (_("System"), "system:", mask & SYSTEM),
106           _other (_("Other"), "", mask & OTHER)
107 {
108         refresh ();
109 }
110
111 /** Find or re-find all our ports and set up our lists */
112 void
113 PortGroupList::refresh ()
114 {
115         clear ();
116         
117         _buss.ports.clear ();
118         _track.ports.clear ();
119         _system.ports.clear ();
120         _other.ports.clear ();
121
122         /* Find the ports provided by ardour; we can't derive their type just from their
123            names, so we'll have to be more devious. 
124         */
125
126         boost::shared_ptr<ARDOUR::Session::RouteList> routes = _session.get_routes ();
127
128         for (ARDOUR::Session::RouteList::const_iterator i = routes->begin(); i != routes->end(); ++i) {
129
130                 PortGroup* g = 0;
131
132                 if (_type == ARDOUR::DataType::AUDIO) {
133
134                         if (boost::dynamic_pointer_cast<ARDOUR::AudioTrack> (*i)) {
135                                 g = &_track;
136                         } else if (!boost::dynamic_pointer_cast<ARDOUR::MidiTrack>(*i)) {
137                                 g = &_buss;
138                         } 
139
140
141                 } else if (_type == ARDOUR::DataType::MIDI) {
142
143                         if (boost::dynamic_pointer_cast<ARDOUR::MidiTrack> (*i)) {
144                                 g = &_track;
145                         }
146
147                         /* No MIDI busses yet */
148                 } 
149                         
150                 if (g) {
151                         ARDOUR::PortSet const & p = _offer_inputs ? ((*i)->inputs()) : ((*i)->outputs());
152                         for (uint32_t j = 0; j < p.num_ports(); ++j) {
153                                 g->add (p.port(j)->name ());
154                         }
155
156                         std::sort (g->ports.begin(), g->ports.end());
157                 }
158         }
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:") {
180                                 /* system: prefix */
181                                 _system.add (p);
182                         } else {
183                                 if (p.substr(0, client_matching_string.length()) != client_matching_string) {
184                                         /* other (non-ardour) prefix */
185                                         _other.add (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 int
202 PortGroupList::n_visible_ports () const
203 {
204         int n = 0;
205         
206         for (const_iterator i = begin(); i != end(); ++i) {
207                 if ((*i)->visible) {
208                         n += (*i)->ports.size();
209                 }
210         }
211
212         return n;
213 }
214
215 std::string
216 PortGroupList::get_port_by_index (int n, bool with_prefix) const
217 {
218         /* XXX: slightly inefficient algorithm */
219
220         for (const_iterator i = begin(); i != end(); ++i) {
221                 for (std::vector<std::string>::const_iterator j = (*i)->ports.begin(); j != (*i)->ports.end(); ++j) {
222                         if (n == 0) {
223                                 if (with_prefix) {
224                                         return (*i)->prefix + *j;
225                                 } else {
226                                         return *j;
227                                 }
228                         }
229                         --n;
230                 }
231         }
232
233         return "";
234 }
235
236 void
237 PortGroupList::set_type (ARDOUR::DataType t)
238 {
239         _type = t;
240 }
241
242 void
243 PortGroupList::set_offer_inputs (bool i)
244 {
245         _offer_inputs = i;
246 }
247