fix merge issues with master
[ardour.git] / libs / ardour / jack_portengine.cc
1 /*
2     Copyright (C) 2013 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 <string.h>
21 #include <stdint.h>
22
23 #include "pbd/error.h"
24
25 #include "ardour/jack_portengine.h"
26 #include "ardour/jack_connection.h"
27 #include "ardour/port_manager.h"
28
29 #include "i18n.h"
30
31 using namespace ARDOUR;
32 using namespace PBD;
33 using std::string;
34 using std::vector;
35
36 #define GET_PRIVATE_JACK_POINTER(localvar)  jack_client_t* localvar = _jack_connection->jack(); if (!(localvar)) { return; }
37 #define GET_PRIVATE_JACK_POINTER_RET(localvar,r) jack_client_t* localvar = _jack_connection->jack(); if (!(localvar)) { return r; }
38
39 static uint32_t
40 ardour_port_flags_to_jack_flags (PortFlags flags)
41 {
42         uint32_t jack_flags = 0;
43         
44         if (flags & IsInput) {
45                 jack_flags |= JackPortIsInput;
46         }
47         if (flags & IsOutput) {
48                 jack_flags |= JackPortIsOutput;
49         }
50         if (flags & IsTerminal) {
51                 jack_flags |= JackPortIsTerminal;
52         }
53         if (flags & IsPhysical) {
54                 jack_flags |= JackPortIsPhysical;
55         }
56         if (flags & CanMonitor) {
57                 jack_flags |= JackPortCanMonitor;
58         }
59
60         return jack_flags;
61 }
62
63 static DataType
64 jack_port_type_to_ardour_data_type (const char* jack_type)
65 {
66         if (strcmp (jack_type, JACK_DEFAULT_AUDIO_TYPE) == 0) {
67                 return DataType::AUDIO;
68         } else if (strcmp (jack_type, JACK_DEFAULT_MIDI_TYPE) == 0) {
69                 return DataType::MIDI;
70         }
71         return DataType::NIL;
72 }
73
74 static const char*
75 ardour_data_type_to_jack_port_type (DataType d)
76 {
77         switch (d) {
78         case DataType::AUDIO:
79                 return JACK_DEFAULT_AUDIO_TYPE;
80         case DataType::MIDI:
81                 return JACK_DEFAULT_MIDI_TYPE;
82         }
83
84         return "";
85 }
86
87 JACKPortEngine::JACKPortEngine (PortManager& pm, boost::shared_ptr<JackConnection> jc)
88         : PortEngine (pm)
89         , _jack_connection (jc)
90 {
91         _jack_connection->Connected.connect_same_thread (jack_connection_connection, boost::bind (&JACKPortEngine::connected_to_jack, this));
92 }
93
94 JACKPortEngine::~JACKPortEngine ()
95 {
96         /* a default destructor would do this, and so would this one,
97            but we'll make it explicit in case we ever need to debug
98            the lifetime of the JACKConnection
99         */
100         _jack_connection.reset ();
101 }
102
103 void
104 JACKPortEngine::connected_to_jack ()
105 {
106         /* register callbacks for stuff that is our responsibility */
107
108         jack_client_t* client = _jack_connection->jack();
109
110         if (!client) {
111                 /* how could this happen? it could ... */
112                 error << _("Already disconnected from JACK before PortEngine could register callbacks") << endmsg;
113                 return;
114         }
115
116         jack_set_port_registration_callback (client, _registration_callback, this);
117         jack_set_port_connect_callback (client, _connect_callback, this);
118         jack_set_graph_order_callback (client, _graph_order_callback, this);
119 }
120
121 void*
122 JACKPortEngine::private_handle() const
123 {
124         return _jack_connection->jack();
125 }
126
127 bool
128 JACKPortEngine::connected() const
129 {
130         return _jack_connection->connected();
131 }
132
133 int
134 JACKPortEngine::set_port_name (PortHandle port, const std::string& name)
135 {
136         return jack_port_set_name ((jack_port_t*) port, name.c_str());
137 }
138
139 string
140 JACKPortEngine::get_port_name (PortHandle port) const
141 {
142         return jack_port_name ((jack_port_t*) port);
143 }
144
145 PortEngine::PortHandle*
146 JACKPortEngine:: get_port_by_name (const std::string& name) const
147 {
148         GET_PRIVATE_JACK_POINTER_RET (_priv_jack, 0);
149         return (PortHandle*) jack_port_by_name (_priv_jack, name.c_str());
150 }
151
152 void
153 JACKPortEngine::_registration_callback (jack_port_id_t /*id*/, int /*reg*/, void* arg)
154 {
155         static_cast<JACKPortEngine*> (arg)->manager.registration_callback ();
156 }
157
158 int
159 JACKPortEngine::_graph_order_callback (void *arg)
160 {
161         return static_cast<JACKPortEngine*> (arg)->manager.graph_order_callback ();
162 }
163
164 void
165 JACKPortEngine::_connect_callback (jack_port_id_t id_a, jack_port_id_t id_b, int conn, void* arg)
166 {
167         static_cast<JACKPortEngine*> (arg)->connect_callback (id_a, id_b, conn);
168 }
169
170 void
171 JACKPortEngine::connect_callback (jack_port_id_t id_a, jack_port_id_t id_b, int conn)
172 {
173         if (manager.port_remove_in_progress()) {
174                 return;
175         }
176
177         GET_PRIVATE_JACK_POINTER (_priv_jack);
178
179         jack_port_t* a = jack_port_by_id (_priv_jack, id_a);
180         jack_port_t* b = jack_port_by_id (_priv_jack, id_b);
181
182         manager.connect_callback (jack_port_name (a), jack_port_name (b), conn == 0 ? false : true);
183 }
184
185 bool
186 JACKPortEngine::connected (PortHandle port)
187 {
188         bool ret = false;
189
190         const char** ports = jack_port_get_connections ((jack_port_t*) port);
191
192         if (ports) {
193                 ret = true;
194         }
195
196         jack_free (ports);
197
198         return ret;
199 }
200
201 bool
202 JACKPortEngine::connected_to (PortHandle port, const std::string& other)
203 {
204         bool ret = false;
205         const char** ports = jack_port_get_connections ((jack_port_t*) port);
206
207         if (ports) {
208                 for (int i = 0; ports[i]; ++i) {
209                         if (other == ports[i]) {
210                                 ret = true;
211                         }
212                 }
213                 jack_free (ports);
214         }
215
216         return ret;
217 }
218
219 bool
220 JACKPortEngine::physically_connected (PortHandle p)
221 {
222         GET_PRIVATE_JACK_POINTER_RET (_priv_jack, false);
223         jack_port_t* port = (jack_port_t*) p;
224
225         const char** ports = jack_port_get_connections (port);
226
227         if (ports) {
228                 for (int i = 0; ports[i]; ++i) {
229
230                         jack_port_t* other = jack_port_by_name (_priv_jack, ports[i]);
231
232                         if (other && (jack_port_flags (other) & JackPortIsPhysical)) {
233                                 return true;
234                         }
235                 }
236                 jack_free (ports);
237         }
238
239         return false;
240 }
241
242 int
243 JACKPortEngine::get_connections (PortHandle port, vector<string>& s)
244 {
245         const char** ports = jack_port_get_connections ((jack_port_t*) port);
246
247         if (ports) {
248                 for (int i = 0; ports[i]; ++i) {
249                         s.push_back (ports[i]);
250                 }
251                 jack_free (ports);
252         }
253
254         return s.size();
255 }
256
257 DataType
258 JACKPortEngine::port_data_type (PortHandle p) const
259 {
260         return jack_port_type_to_ardour_data_type (jack_port_type ((jack_port_t*) p));
261 }
262
263 const string&
264 JACKPortEngine::my_name() const
265 {
266         return _jack_connection->client_name();
267 }
268
269 bool
270 JACKPortEngine::port_is_physical (PortHandle ph) const
271 {
272         if (!ph) {
273                 return false;
274         }
275
276         return jack_port_flags ((jack_port_t*) ph) & JackPortIsPhysical;
277 }
278
279 int
280 JACKPortEngine::get_ports (const string& port_name_pattern, DataType type, PortFlags flags, vector<string>& s) const
281 {
282
283         GET_PRIVATE_JACK_POINTER_RET (_priv_jack,0);
284
285         const char** ports =  jack_get_ports (_priv_jack, port_name_pattern.c_str(), 
286                                               ardour_data_type_to_jack_port_type (type), 
287                                               ardour_port_flags_to_jack_flags (flags));
288
289         if (ports == 0) {
290                 return 0;
291         }
292
293         for (uint32_t i = 0; ports[i]; ++i) {
294                 s.push_back (ports[i]);
295         }
296
297         jack_free (ports);
298         
299         return s.size();
300 }
301
302 ChanCount
303 JACKPortEngine::n_physical (unsigned long flags) const
304 {
305         ChanCount c;
306
307         GET_PRIVATE_JACK_POINTER_RET (_priv_jack, c);
308
309         const char ** ports = jack_get_ports (_priv_jack, NULL, NULL, JackPortIsPhysical | flags);
310
311         if (ports) {
312                 for (uint32_t i = 0; ports[i]; ++i) {
313                         if (!strstr (ports[i], "Midi-Through")) {
314                                 DataType t = port_data_type (jack_port_by_name (_priv_jack, ports[i]));
315                                 if (t != DataType::NIL) {
316                                         c.set (t, c.get (t) + 1);
317                                 }
318                         }
319                 }
320                 
321                 jack_free (ports);
322         }
323
324         return c;
325 }
326
327 ChanCount
328 JACKPortEngine::n_physical_inputs () const
329 {
330         return n_physical (JackPortIsInput);
331 }
332
333 ChanCount
334 JACKPortEngine::n_physical_outputs () const
335 {
336         return n_physical (JackPortIsOutput);
337 }
338
339 void
340 JACKPortEngine::get_physical (DataType type, unsigned long flags, vector<string>& phy) const
341 {
342         GET_PRIVATE_JACK_POINTER (_priv_jack);
343         const char ** ports;
344
345         if ((ports = jack_get_ports (_priv_jack, NULL, ardour_data_type_to_jack_port_type (type), JackPortIsPhysical | flags)) == 0) {
346                 return;
347         }
348
349         if (ports) {
350                 for (uint32_t i = 0; ports[i]; ++i) {
351                         if (strstr (ports[i], "Midi-Through")) {
352                                 continue;
353                         }
354                         phy.push_back (ports[i]);
355                 }
356                 jack_free (ports);
357         }
358 }
359
360 /** Get physical ports for which JackPortIsOutput is set; ie those that correspond to
361  *  a physical input connector.
362  */
363 void
364 JACKPortEngine::get_physical_inputs (DataType type, vector<string>& ins)
365 {
366         get_physical (type, JackPortIsOutput, ins);
367 }
368
369 /** Get physical ports for which JackPortIsInput is set; ie those that correspond to
370  *  a physical output connector.
371  */
372 void
373 JACKPortEngine::get_physical_outputs (DataType type, vector<string>& outs)
374 {
375         get_physical (type, JackPortIsInput, outs);
376 }
377
378
379 bool
380 JACKPortEngine::can_monitor_input () const
381 {
382         GET_PRIVATE_JACK_POINTER_RET (_priv_jack,false);
383         const char ** ports;
384
385         if ((ports = jack_get_ports (_priv_jack, NULL, JACK_DEFAULT_AUDIO_TYPE, JackPortCanMonitor)) == 0) {
386                 return false;
387         }
388
389         jack_free (ports);
390
391         return true;
392 }
393
394 int
395 JACKPortEngine::request_input_monitoring (PortHandle port, bool yn)
396 {
397         return jack_port_request_monitor ((jack_port_t*) port, yn);
398 }
399 int
400 JACKPortEngine::ensure_input_monitoring (PortHandle port, bool yn)
401 {
402         return jack_port_ensure_monitor ((jack_port_t*) port, yn);
403 }
404 bool
405 JACKPortEngine::monitoring_input (PortHandle port)
406 {
407         return jack_port_monitoring_input ((jack_port_t*) port);
408 }
409
410
411 pframes_t
412 JACKPortEngine::sample_time_at_cycle_start ()
413 {
414         GET_PRIVATE_JACK_POINTER_RET (_priv_jack, 0);
415         return jack_last_frame_time (_priv_jack);
416 }
417
418
419 PortEngine::PortHandle
420 JACKPortEngine::register_port (const std::string& shortname, ARDOUR::DataType type, ARDOUR::PortFlags flags)
421 {
422         GET_PRIVATE_JACK_POINTER_RET (_priv_jack, 0);
423         return jack_port_register (_priv_jack, shortname.c_str(), 
424                                    ardour_data_type_to_jack_port_type (type),
425                                    ardour_port_flags_to_jack_flags (flags),
426                                    0);
427 }
428
429 void
430 JACKPortEngine::unregister_port (PortHandle port)
431 {
432         GET_PRIVATE_JACK_POINTER (_priv_jack);
433         (void) jack_port_unregister (_priv_jack, (jack_port_t*) port);
434 }
435
436 int
437 JACKPortEngine::connect (PortHandle port, const std::string& other)
438 {
439         GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
440         return jack_connect (_priv_jack, jack_port_name ((jack_port_t*) port), other.c_str());
441 }
442 int
443 JACKPortEngine::connect (const std::string& src, const std::string& dst)
444 {
445         GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
446         return jack_connect (_priv_jack, src.c_str(), dst.c_str());
447 }
448
449 int
450 JACKPortEngine::disconnect (PortHandle port, const std::string& other)
451 {
452         GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
453         return jack_disconnect (_priv_jack, jack_port_name ((jack_port_t*) port), other.c_str());
454 }
455
456 int
457 JACKPortEngine::disconnect (const std::string& src, const std::string& dst)
458 {
459         GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
460         return jack_disconnect (_priv_jack, src.c_str(), dst.c_str());
461 }
462
463 int
464 JACKPortEngine::disconnect_all (PortHandle port)
465 {
466         GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
467         return jack_port_disconnect (_priv_jack, (jack_port_t*) port);
468 }
469
470 int
471 JACKPortEngine::midi_event_get (pframes_t& timestamp, size_t& size, uint8_t** buf, void* port_buffer, uint32_t event_index)
472 {
473         jack_midi_event_t ev;
474         int ret;
475
476         if ((ret = jack_midi_event_get (&ev, port_buffer, event_index)) == 0) {
477                 timestamp = ev.time;
478                 size = ev.size;
479                 *buf = ev.buffer;
480         }
481
482         return ret;
483 }
484
485 int
486 JACKPortEngine::midi_event_put (void* port_buffer, pframes_t timestamp, const uint8_t* buffer, size_t size)
487 {
488         return jack_midi_event_write (port_buffer, timestamp, buffer, size);
489 }
490
491 uint32_t
492 JACKPortEngine::get_midi_event_count (void* port_buffer)
493 {
494         return jack_midi_get_event_count (port_buffer);
495 }
496
497 void
498 JACKPortEngine::midi_clear (void* port_buffer)
499 {
500         jack_midi_clear_buffer (port_buffer);
501 }
502
503 void
504 JACKPortEngine::set_latency_range (PortHandle port, bool for_playback, LatencyRange r)
505 {
506         jack_latency_range_t range;
507         
508         range.min = r.min;
509         range.max = r.max;
510
511         jack_port_set_latency_range ((jack_port_t*) port, for_playback ? JackPlaybackLatency : JackCaptureLatency, &range);
512 }
513
514 LatencyRange
515 JACKPortEngine::get_latency_range (PortHandle port, bool for_playback)
516 {
517         jack_latency_range_t range;
518         LatencyRange ret;
519         
520         jack_port_get_latency_range ((jack_port_t*) port, for_playback ? JackPlaybackLatency : JackCaptureLatency, &range);
521
522         ret.min = range.min;
523         ret.max = range.max;
524
525         return ret;
526 }
527
528 void*
529 JACKPortEngine::get_buffer (PortHandle port, pframes_t nframes)
530 {
531         return jack_port_get_buffer ((jack_port_t*) port, nframes);
532 }
533
534 uint32_t
535 JACKPortEngine::port_name_size() const
536 {
537         return jack_port_name_size ();
538 }