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