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