Trim include dependency tree (particularly on evoral/Sequence.hpp).
[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 using namespace std;
29 using namespace ARDOUR;
30
31 AudioEngine* Port::_engine = 0;
32
33 /** @param n Port short name */
34 Port::Port (std::string const & n, DataType t, Flags f)
35         : _last_monitor (false)
36         , _name (n)
37         , _flags (f)
38 {
39
40         /* Unfortunately we have to pass the DataType into this constructor so that we can
41            create the right kind of JACK port; aside from this we'll use the virtual function type ()
42            to establish type. 
43         */
44
45         assert (_name.find_first_of (':') == std::string::npos);
46         
47         if ((_jack_port = jack_port_register (_engine->jack (), _name.c_str (), t.to_jack_type (), _flags, 0)) == 0) {
48                 throw failed_constructor ();
49         }
50 }
51
52 /** Port destructor */
53 Port::~Port ()
54 {
55         jack_port_unregister (_engine->jack (), _jack_port);
56 }
57
58 /** @return true if this port is connected to anything */
59 bool
60 Port::connected () const
61 {
62         return (jack_port_connected (_jack_port) != 0);
63 }
64
65 int
66 Port::disconnect_all ()
67 {
68         jack_port_disconnect (_engine->jack(), _jack_port);
69         _connections.clear ();
70
71         return 0;
72 }
73
74 /** @param o Port name
75  * @return true if this port is connected to o, otherwise false.
76  */
77 bool
78 Port::connected_to (std::string const & o) const
79 {
80         return jack_port_connected_to (_jack_port, _engine->make_port_name_non_relative(o).c_str ());
81 }
82
83 /** @param o Filled in with port full names of ports that we are connected to */
84 int
85 Port::get_connections (std::vector<std::string> & c) const
86 {
87         int n = 0;
88
89         const char** jc = jack_port_get_connections (_jack_port);
90         if (jc) {
91                 for (int i = 0; jc[i]; ++i) {
92                         c.push_back (jc[i]);
93                         ++n;
94                 }
95         }
96         
97         return n;
98 }
99
100 int
101 Port::connect (std::string const & other)
102 {
103         /* caller must hold process lock */
104
105         std::string const other_shrt = _engine->make_port_name_non_relative (other);
106         std::string const this_shrt = _engine->make_port_name_non_relative (_name);
107
108         int r = 0;
109                 
110         if (sends_output ()) {
111                 r = jack_connect (_engine->jack (), this_shrt.c_str (), other_shrt.c_str ());
112         } else {
113                 r = jack_connect (_engine->jack (), other_shrt.c_str (), this_shrt.c_str());
114         }
115         
116         if (r == 0) {
117                 _connections.insert (other);
118         }
119
120         return r;
121 }
122
123 int
124 Port::disconnect (std::string const & other)
125 {
126         /* caller must hold process lock */
127
128         std::string const other_shrt = _engine->make_port_name_non_relative (other);
129         std::string const this_shrt = _engine->make_port_name_non_relative (_name);
130
131         int r = 0;
132         
133         if (sends_output ()) {
134                 r = jack_disconnect (_engine->jack (), this_shrt.c_str (), other_shrt.c_str ());
135         } else {
136                 r = jack_disconnect (_engine->jack (), other_shrt.c_str (), this_shrt.c_str ());
137         }
138         
139         if (r == 0) {
140                 _connections.erase (other);
141         }
142         
143 return r;
144 }
145
146
147 bool
148 Port::connected_to (Port* o) const
149 {
150         return connected_to (o->name ());
151 }
152
153 int
154 Port::connect (Port* o)
155 {
156         return connect (o->name ());
157 }
158
159 int
160 Port::disconnect (Port* o)
161 {
162         return disconnect (o->name ());
163 }
164
165 void
166 Port::set_engine (AudioEngine* e)
167 {
168         _engine = e;
169 }
170
171 void
172 Port::ensure_monitor_input (bool yn)
173 {
174         jack_port_ensure_monitor (_jack_port, yn);
175 }
176
177 bool
178 Port::monitoring_input () const
179 {
180         return jack_port_monitoring_input (_jack_port);
181 }
182
183 void
184 Port::reset ()
185 {
186         _last_monitor = false;
187
188         // XXX
189         // _metering = 0;
190         // reset_meters ();
191 }
192
193 void
194 Port::recompute_total_latency () const
195 {
196 #ifdef HAVE_JACK_RECOMPUTE_LATENCY      
197         jack_recompute_total_latency (_engine->jack (), _jack_port);
198 #endif  
199 }
200
201 nframes_t
202 Port::total_latency () const
203 {
204         return jack_port_get_total_latency (_engine->jack (), _jack_port);
205 }
206
207 int
208 Port::reestablish ()
209 {
210         _jack_port = jack_port_register (_engine->jack(), _name.c_str(), type().to_jack_type(), _flags, 0);
211
212         if (_jack_port == 0) {
213                 PBD::error << string_compose (_("could not reregister %1"), _name) << endmsg;
214                 return -1;
215         }
216
217         reset ();
218
219         return 0;
220 }
221
222
223 int
224 Port::reconnect ()
225 {
226         /* caller must hold process lock; intended to be used only after reestablish() */
227
228         for (std::set<string>::iterator i = _connections.begin(); i != _connections.end(); ++i) {
229                 if (connect (*i)) {
230                         return -1;
231                 }
232         }
233
234         return 0;
235 }
236
237 /** @param n Short name */
238 int
239 Port::set_name (std::string const & n)
240 {
241         assert (_name.find_first_of (':') == std::string::npos);
242
243         int const r = jack_port_set_name (_jack_port, n.c_str());
244         if (r == 0) {
245                 _name = n;
246         }
247
248         return r;
249 }
250
251 void
252 Port::request_monitor_input (bool yn)
253 {
254         jack_port_request_monitor (_jack_port, yn);
255 }
256
257 void
258 Port::set_latency (nframes_t n)
259 {
260         jack_port_set_latency (_jack_port, n);
261 }