Coding style.
[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
124         if (sends_output ()) {
125                 r = jack_connect (_engine->jack (), this_shrt.c_str (), other_shrt.c_str ());
126         } else {
127                 r = jack_connect (_engine->jack (), other_shrt.c_str (), this_shrt.c_str());
128         }
129
130         if (r == 0) {
131                 _connections.insert (other);
132         }
133
134         return r;
135 }
136
137 int
138 Port::disconnect (std::string const & other)
139 {
140         /* caller must hold process lock */
141
142         std::string const other_shrt = _engine->make_port_name_non_relative (other);
143         std::string const this_shrt = _engine->make_port_name_non_relative (_name);
144
145         int r = 0;
146
147         if (sends_output ()) {
148                 r = jack_disconnect (_engine->jack (), this_shrt.c_str (), other_shrt.c_str ());
149         } else {
150                 r = jack_disconnect (_engine->jack (), other_shrt.c_str (), this_shrt.c_str ());
151         }
152
153         if (r == 0) {
154                 _connections.erase (other);
155         }
156
157         return r;
158 }
159
160
161 bool
162 Port::connected_to (Port* o) const
163 {
164         return connected_to (o->name ());
165 }
166
167 int
168 Port::connect (Port* o)
169 {
170         return connect (o->name ());
171 }
172
173 int
174 Port::disconnect (Port* o)
175 {
176         return disconnect (o->name ());
177 }
178
179 void
180 Port::set_engine (AudioEngine* e)
181 {
182         _engine = e;
183 }
184
185 void
186 Port::ensure_monitor_input (bool yn)
187 {
188         jack_port_ensure_monitor (_jack_port, yn);
189 }
190
191 bool
192 Port::monitoring_input () const
193 {
194         return jack_port_monitoring_input (_jack_port);
195 }
196
197 void
198 Port::reset ()
199 {
200         _last_monitor = false;
201
202         // XXX
203         // _metering = 0;
204         // reset_meters ();
205 }
206
207 void
208 Port::recompute_total_latency () const
209 {
210 #ifdef HAVE_JACK_RECOMPUTE_LATENCY
211         jack_client_t* jack = _engine->jack();
212
213         if (!jack) {
214                 return;
215         }
216
217         jack_recompute_total_latency (jack, _jack_port);
218 #endif
219 }
220
221 nframes_t
222 Port::total_latency () const
223 {
224         jack_client_t* jack = _engine->jack();
225
226         if (!jack) {
227                 return 0;
228         }
229
230         return jack_port_get_total_latency (jack, _jack_port);
231 }
232
233 int
234 Port::reestablish ()
235 {
236         jack_client_t* jack = _engine->jack();
237
238         if (!jack) {
239                 return -1;
240         }
241
242         cerr << "RE-REGISTER: " << _name.c_str() << endl;
243         _jack_port = jack_port_register (jack, _name.c_str(), type().to_jack_type(), _flags, 0);
244
245         if (_jack_port == 0) {
246                 PBD::error << string_compose (_("could not reregister %1"), _name) << endmsg;
247                 return -1;
248         }
249
250         reset ();
251
252         return 0;
253 }
254
255
256 int
257 Port::reconnect ()
258 {
259         /* caller must hold process lock; intended to be used only after reestablish() */
260
261         for (std::set<string>::iterator i = _connections.begin(); i != _connections.end(); ++i) {
262                 if (connect (*i)) {
263                         return -1;
264                 }
265         }
266
267         return 0;
268 }
269
270 /** @param n Short port name (no JACK client name) */
271 int
272 Port::set_name (std::string const & n)
273 {
274         if (n == _name) {
275                 return 0;
276         }
277
278         int const r = jack_port_set_name (_jack_port, n.c_str());
279
280         if (r == 0) {
281                 _name = n;
282         }
283
284         return r;
285 }
286
287 void
288 Port::request_monitor_input (bool yn)
289 {
290         jack_port_request_monitor (_jack_port, yn);
291 }
292
293 void
294 Port::set_latency (nframes_t n)
295 {
296         jack_port_set_latency (_jack_port, n);
297 }
298
299 bool
300 Port::physically_connected () const
301 {
302         const char** jc = jack_port_get_connections (_jack_port);
303
304         if (jc) {
305                 for (int i = 0; jc[i]; ++i) {
306
307                         jack_port_t* port = jack_port_by_name (_engine->jack(), jc[i]);
308                         
309                         if (port && (jack_port_flags (port) & JackPortIsPhysical)) {
310                                 jack_free (jc);
311                                 return true;
312                         }
313                 }
314                 
315                 jack_free (jc);
316         }
317
318         return false;
319 }
320