Some more full / short name confusions.
[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 #include "ardour/port.h"
21 #include "ardour/audioengine.h"
22 #include "ardour/i18n.h"
23 #include "pbd/failed_constructor.h"
24 #include "pbd/error.h"
25 #include "pbd/compose.h"
26 #include <stdexcept>
27
28 ARDOUR::AudioEngine* ARDOUR::Port::_engine = 0;
29
30 /** @param n Port short name */
31 ARDOUR::Port::Port (std::string const & n, DataType t, Flags f, bool e) : _jack_port (0), _last_monitor (false), _latency (0), _name (n), _flags (f)
32 {
33         /* Unfortunately we have to pass the DataType into this constructor so that we can
34            create the right kind of JACK port; aside from this we'll use the virtual function type ()
35            to establish type. */
36
37         assert (_name.find_first_of (':') == std::string::npos);
38         
39         if (e) {
40                 try {
41                         do_make_external (t);
42                 }
43                 catch (...) {
44                         throw failed_constructor ();
45                 }
46         }
47 }
48
49 /** Port destructor */
50 ARDOUR::Port::~Port ()
51 {
52         if (_jack_port) {
53                 jack_port_unregister (_engine->jack (), _jack_port);
54         }
55 }
56
57 /** Make this port externally visible by setting it up to use a JACK port.
58  * @param t Data type, so that we can call this method from the constructor.
59  */
60 void
61 ARDOUR::Port::do_make_external (DataType t)
62 {
63         if (_jack_port) {
64                 /* already external */
65                 return;
66         }
67         
68         _jack_port = jack_port_register (_engine->jack (), _name.c_str (), t.to_jack_type (), _flags, 0);
69         if (_jack_port == 0) {
70                 throw std::runtime_error ("Could not register JACK port");
71         }
72 }
73
74 void
75 ARDOUR::Port::make_external ()
76 {
77         do_make_external (type ());
78 }
79
80 /** @return true if this port is connected to anything */
81 bool
82 ARDOUR::Port::connected () const
83 {
84         if (!_connections.empty ()) {
85                 /* connected to a Port* */
86                 return true;
87         }
88
89         if (_jack_port == 0) {
90                 /* not using a JACK port, so can't be connected to anything else */
91                 return false;
92         }
93         
94         return (jack_port_connected (_jack_port) != 0);
95 }
96
97 int
98 ARDOUR::Port::disconnect_all ()
99 {
100         /* Disconnect from Port* connections */
101         for (std::set<Port*>::iterator i = _connections.begin (); i != _connections.end (); ++i) {
102                 (*i)->_connections.erase (this);
103         }
104
105         _connections.clear ();
106
107         /* And JACK connections */
108         jack_port_disconnect (_engine->jack(), _jack_port);
109         _named_connections.clear ();
110
111         return 0;
112 }
113
114 /** @param o Port name
115  * @return true if this port is connected to o, otherwise false.
116  */
117 bool
118 ARDOUR::Port::connected_to (std::string const & o) const
119 {
120         std::string const full = _engine->make_port_name_non_relative (o);
121         std::string const shrt = _engine->make_port_name_non_relative (o);
122         
123         if (_jack_port && jack_port_connected_to (_jack_port, full.c_str ())) {
124                 /* connected via JACK */
125                 return true;
126         }
127
128         for (std::set<Port*>::iterator i = _connections.begin (); i != _connections.end (); ++i) {
129                 if ((*i)->name () == shrt) {
130                         /* connected internally */
131                         return true;
132                 }
133         }
134
135         return false;
136 }
137
138 /** @param o Filled in with port full names of ports that we are connected to */
139 int
140 ARDOUR::Port::get_connections (std::vector<std::string> & c) const
141 {
142         int n = 0;
143
144         /* JACK connections */
145         if (_jack_port) {
146                 const char** jc = jack_port_get_connections (_jack_port);
147                 if (jc) {
148                         for (int i = 0; jc[i]; ++i) {
149                                 c.push_back (jc[i]);
150                                 ++n;
151                         }
152                 }
153         }
154
155         /* Internal connections */
156         for (std::set<Port*>::iterator i = _connections.begin (); i != _connections.end (); ++i) {
157                 std::string const full = _engine->make_port_name_non_relative ((*i)->name());
158                 c.push_back (full);
159                 ++n;
160         }
161
162         return n;
163 }
164
165 int
166 ARDOUR::Port::connect (std::string const & other)
167 {
168         /* caller must hold process lock */
169
170         std::string const other_shrt = _engine->make_port_name_non_relative (other);
171         
172         Port* p = _engine->get_port_by_name_locked (other_shrt);
173         
174         int r;
175         
176         if (p && !p->external ()) {
177                 /* non-external Ardour port; connect using Port* */
178                 r = connect (p);
179         } else {
180                 /* connect using name */
181
182                 /* for this to work, we must be an external port */
183                 if (!external ()) {
184                         make_external ();
185                 }
186
187                 std::string const this_shrt = _engine->make_port_name_non_relative (_name);
188
189                 if (sends_output ()) {
190                         r = jack_connect (_engine->jack (), this_shrt.c_str (), other_shrt.c_str ());
191                 } else {
192                         r = jack_connect (_engine->jack (), other_shrt.c_str (), this_shrt.c_str());
193                 }
194
195                 if (r == 0) {
196                         _named_connections.insert (other);
197                 }
198         }
199
200         return r;
201 }
202
203 int
204 ARDOUR::Port::disconnect (std::string const & other)
205 {
206         /* caller must hold process lock */
207
208         std::string const other_shrt = _engine->make_port_name_non_relative (other);
209         
210         Port* p = _engine->get_port_by_name_locked (other_shrt);
211         int r;
212
213         if (p && !p->external ()) {
214                 /* non-external Ardour port; disconnect using Port* */
215                 r = disconnect (p);
216         } else {
217                 /* disconnect using name */
218
219                 std::string const this_shrt = _engine->make_port_name_non_relative (_name);
220
221                 if (sends_output ()) {
222                         r = jack_disconnect (_engine->jack (), this_shrt.c_str (), other_shrt.c_str ());
223                 } else {
224                         r = jack_disconnect (_engine->jack (), other_shrt.c_str (), this_shrt.c_str ());
225                 }
226
227                 if (r == 0) {
228                         _named_connections.erase (other);
229                 }
230         }
231
232         return r;
233 }
234
235
236 bool
237 ARDOUR::Port::connected_to (Port* o) const
238 {
239         return connected_to (o->name ());
240 }
241
242 int
243 ARDOUR::Port::connect (Port* o)
244 {
245         /* caller must hold process lock */
246
247         if (external () && o->external ()) {
248                 /* we're both external; connect using name */
249                 return connect (o->name ());
250         }
251
252         /* otherwise connect by Port* */
253         _connections.insert (o);
254         o->_connections.insert (this);
255
256         return 0;
257 }
258
259 int
260 ARDOUR::Port::disconnect (Port* o)
261 {
262         if (external () && o->external ()) {
263                 /* we're both external; try disconnecting using name */
264                 int const r = disconnect (o->name ());
265                 if (r == 0) {
266                         return 0;
267                 }
268         }
269         
270         _connections.erase (o);
271         o->_connections.erase (this);
272
273         return 0;
274 }
275
276 void
277 ARDOUR::Port::set_engine (AudioEngine* e)
278 {
279         _engine = e;
280 }
281
282 void
283 ARDOUR::Port::ensure_monitor_input (bool yn)
284 {
285         if (_jack_port) {
286                 jack_port_ensure_monitor (_jack_port, yn);
287         }
288 }
289
290 bool
291 ARDOUR::Port::monitoring_input () const
292 {
293         if (_jack_port) {
294                 return jack_port_monitoring_input (_jack_port);
295         } else {
296                 return false;
297         }
298 }
299
300 void
301 ARDOUR::Port::reset ()
302 {
303         _last_monitor = false;
304
305         // XXX
306         // _metering = 0;
307         // reset_meters ();
308 }
309
310 void
311 ARDOUR::Port::recompute_total_latency () const
312 {
313 #ifdef HAVE_JACK_RECOMPUTE_LATENCY      
314         if (_jack_port) {
315                 jack_recompute_total_latency (_engine->jack (), _jack_port);
316         }
317 #endif  
318 }
319
320 nframes_t
321 ARDOUR::Port::total_latency () const
322 {
323         if (_jack_port) {
324                 return jack_port_get_total_latency (_engine->jack (), _jack_port);
325         } else {
326                 return _latency;
327         }
328 }
329
330 int
331 ARDOUR::Port::reestablish ()
332 {
333         if (!_jack_port) {
334                 return 0;
335         }
336
337         _jack_port = jack_port_register (_engine->jack(), _name.c_str(), type().to_jack_type(), _flags, 0);
338
339         if (_jack_port == 0) {
340                 PBD::error << string_compose (_("could not reregister %1"), _name) << endmsg;
341                 return -1;
342         }
343
344         reset ();
345
346         return 0;
347 }
348
349
350 int
351 ARDOUR::Port::reconnect ()
352 {
353         /* caller must hold process lock; intended to be used only after reestablish() */
354
355         if (!_jack_port) {
356                 return 0;
357         }
358         
359         for (std::set<string>::iterator i = _named_connections.begin(); i != _named_connections.end(); ++i) {
360                 if (connect (*i)) {
361                         return -1;
362                 }
363         }
364
365         return 0;
366 }
367
368 /** @param n Short name */
369 int
370 ARDOUR::Port::set_name (std::string const & n)
371 {
372         assert (_name.find_first_of (':') == std::string::npos);
373
374         int r = 0;
375         
376         if (_jack_port) {
377                 r = jack_port_set_name (_jack_port, n.c_str());
378                 if (r) {
379                         _name = n;
380                 }
381         } else {
382                 _name = n;
383         }
384
385         return r;
386 }
387
388 void
389 ARDOUR::Port::set_latency (nframes_t n)
390 {
391         _latency = n;
392 }
393
394 void
395 ARDOUR::Port::request_monitor_input (bool yn)
396 {
397         if (_jack_port) {
398                 jack_port_request_monitor (_jack_port, yn);
399         }
400 }