add -P flag blocking port connections while loading session
[ardour.git] / libs / ardour / port.cc
1 /*
2     Copyright (C) 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 #ifdef WAF_BUILD
21 #include "libardour-config.h"
22 #endif
23
24 #include "ardour/port.h"
25 #include "ardour/audioengine.h"
26 #include "pbd/failed_constructor.h"
27 #include "pbd/error.h"
28 #include "pbd/compose.h"
29 #include <stdexcept>
30
31 #include "i18n.h"
32
33 using namespace std;
34 using namespace ARDOUR;
35
36 AudioEngine* Port::_engine = 0;
37 nframes_t Port::_port_offset = 0;
38 nframes_t Port::_buffer_size = 0;
39 bool Port::_connecting_blocked = false;
40
41 /** @param n Port short name */
42 Port::Port (std::string const & n, DataType t, Flags f)
43         : _last_monitor (false)
44         , _name (n)
45         , _flags (f)
46 {
47
48         /* Unfortunately we have to pass the DataType into this constructor so that we can
49            create the right kind of JACK port; aside from this we'll use the virtual function type ()
50            to establish type.
51         */
52
53         assert (_name.find_first_of (':') == std::string::npos);
54
55         if ((_jack_port = jack_port_register (_engine->jack (), _name.c_str (), t.to_jack_type (), _flags, 0)) == 0) {
56                 throw failed_constructor ();
57         }
58 }
59
60 /** Port destructor */
61 Port::~Port ()
62 {
63         jack_port_unregister (_engine->jack (), _jack_port);
64 }
65
66 /** @return true if this port is connected to anything */
67 bool
68 Port::connected () const
69 {
70         return (jack_port_connected (_jack_port) != 0);
71 }
72
73 int
74 Port::disconnect_all ()
75 {
76         jack_port_disconnect (_engine->jack(), _jack_port);
77         _connections.clear ();
78
79         return 0;
80 }
81
82 /** @param o Port name
83  * @return true if this port is connected to o, otherwise false.
84  */
85 bool
86 Port::connected_to (std::string const & o) const
87 {
88         return jack_port_connected_to (_jack_port, _engine->make_port_name_non_relative(o).c_str ());
89 }
90
91 /** @param o Filled in with port full names of ports that we are connected to */
92 int
93 Port::get_connections (std::vector<std::string> & c) const
94 {
95         int n = 0;
96
97         const char** jc = jack_port_get_connections (_jack_port);
98         if (jc) {
99                 for (int i = 0; jc[i]; ++i) {
100                         c.push_back (jc[i]);
101                         ++n;
102                 }
103
104                 jack_free (jc);
105         }
106
107         return n;
108 }
109
110 int
111 Port::connect (std::string const & other)
112 {
113         /* caller must hold process lock */
114
115         std::string const other_shrt = _engine->make_port_name_non_relative (other);
116         std::string const this_shrt = _engine->make_port_name_non_relative (_name);
117
118         int r = 0;
119
120         if (_connecting_blocked)
121                 return r;
122
123         if (sends_output ()) {
124                 r = jack_connect (_engine->jack (), this_shrt.c_str (), other_shrt.c_str ());
125         } else {
126                 r = jack_connect (_engine->jack (), other_shrt.c_str (), this_shrt.c_str());
127         }
128
129         if (r == 0) {
130                 _connections.insert (other);
131         }
132
133         return r;
134 }
135
136 int
137 Port::disconnect (std::string const & other)
138 {
139         /* caller must hold process lock */
140
141         std::string const other_shrt = _engine->make_port_name_non_relative (other);
142         std::string const this_shrt = _engine->make_port_name_non_relative (_name);
143
144         int r = 0;
145
146         if (sends_output ()) {
147                 r = jack_disconnect (_engine->jack (), this_shrt.c_str (), other_shrt.c_str ());
148         } else {
149                 r = jack_disconnect (_engine->jack (), other_shrt.c_str (), this_shrt.c_str ());
150         }
151
152         if (r == 0) {
153                 _connections.erase (other);
154         }
155
156         return r;
157 }
158
159
160 bool
161 Port::connected_to (Port* o) const
162 {
163         return connected_to (o->name ());
164 }
165
166 int
167 Port::connect (Port* o)
168 {
169         return connect (o->name ());
170 }
171
172 int
173 Port::disconnect (Port* o)
174 {
175         return disconnect (o->name ());
176 }
177
178 void
179 Port::set_engine (AudioEngine* e)
180 {
181         _engine = e;
182 }
183
184 void
185 Port::ensure_monitor_input (bool yn)
186 {
187         jack_port_ensure_monitor (_jack_port, yn);
188 }
189
190 bool
191 Port::monitoring_input () const
192 {
193         return jack_port_monitoring_input (_jack_port);
194 }
195
196 void
197 Port::reset ()
198 {
199         _last_monitor = false;
200
201         // XXX
202         // _metering = 0;
203         // reset_meters ();
204 }
205
206 void
207 Port::recompute_total_latency () const
208 {
209 #ifdef HAVE_JACK_RECOMPUTE_LATENCY
210         jack_client_t* jack = _engine->jack();
211
212         if (!jack) {
213                 return;
214         }
215
216         jack_recompute_total_latency (jack, _jack_port);
217 #endif
218 }
219
220 nframes_t
221 Port::total_latency () const
222 {
223         jack_client_t* jack = _engine->jack();
224
225         if (!jack) {
226                 return 0;
227         }
228
229         return jack_port_get_total_latency (jack, _jack_port);
230 }
231
232 int
233 Port::reestablish ()
234 {
235         jack_client_t* jack = _engine->jack();
236
237         if (!jack) {
238                 return -1;
239         }
240
241         cerr << "RE-REGISTER: " << _name.c_str() << endl;
242         _jack_port = jack_port_register (jack, _name.c_str(), type().to_jack_type(), _flags, 0);
243
244         if (_jack_port == 0) {
245                 PBD::error << string_compose (_("could not reregister %1"), _name) << endmsg;
246                 return -1;
247         }
248
249         reset ();
250
251         return 0;
252 }
253
254
255 int
256 Port::reconnect ()
257 {
258         /* caller must hold process lock; intended to be used only after reestablish() */
259
260         for (std::set<string>::iterator i = _connections.begin(); i != _connections.end(); ++i) {
261                 if (connect (*i)) {
262                         return -1;
263                 }
264         }
265
266         return 0;
267 }
268
269 /** @param n Short port name (no JACK client name) */
270 int
271 Port::set_name (std::string const & n)
272 {
273         if (n == _name) {
274                 return 0;
275         }
276
277         int const r = jack_port_set_name (_jack_port, n.c_str());
278
279         if (r == 0) {
280                 _name = n;
281         }
282
283         return r;
284 }
285
286 void
287 Port::request_monitor_input (bool yn)
288 {
289         jack_port_request_monitor (_jack_port, yn);
290 }
291
292 void
293 Port::set_latency (nframes_t n)
294 {
295         jack_port_set_latency (_jack_port, n);
296 }
297
298 bool
299 Port::physically_connected () const
300 {
301         const char** jc = jack_port_get_connections (_jack_port);
302
303         if (jc) {
304                 for (int i = 0; jc[i]; ++i) {
305
306                         jack_port_t* port = jack_port_by_name (_engine->jack(), jc[i]);
307                         
308                         if (port && (jack_port_flags (port) & JackPortIsPhysical)) {
309                                 jack_free (jc);
310                                 return true;
311                         }
312                 }
313                 
314                 jack_free (jc);
315         }
316
317         return false;
318 }