Only show user-presets in favorite sidebar
[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_audiobackend.h"
26 #include "jack_connection.h"
27
28 #include "ardour/port_manager.h"
29
30 #include "pbd/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 void
89 JACKAudioBackend::when_connected_to_jack ()
90 {
91         /* register callbacks for stuff that is our responsibility */
92
93         jack_client_t* client = _jack_connection->jack();
94
95         if (!client) {
96                 /* how could this happen? it could ... */
97                 error << _("Already disconnected from JACK before PortEngine could register callbacks") << endmsg;
98                 return;
99         }
100
101         jack_set_port_registration_callback (client, _registration_callback, this);
102         jack_set_port_connect_callback (client, _connect_callback, this);
103         jack_set_graph_order_callback (client, _graph_order_callback, this);
104 }
105
106 int
107 JACKAudioBackend::set_port_name (PortHandle port, const std::string& name)
108 {
109 #if HAVE_JACK_PORT_RENAME
110         jack_client_t* client = _jack_connection->jack();
111         if (client) {
112                 return jack_port_rename (client, (jack_port_t*) port, name.c_str());
113         } else {
114                 return -1;
115         }
116 #else
117         return jack_port_set_name ((jack_port_t*) port, name.c_str());
118 #endif
119 }
120
121 string
122 JACKAudioBackend::get_port_name (PortHandle port) const
123 {
124         return jack_port_name ((jack_port_t*) port);
125 }
126
127 PortFlags
128 JACKAudioBackend::get_port_flags (PortHandle port) const
129 {
130         return PortFlags (jack_port_flags ((jack_port_t*) port));
131 }
132
133 int
134 JACKAudioBackend::get_port_property (PortHandle port, const std::string& key, std::string& value, std::string& type) const
135 {
136 #ifdef HAVE_JACK_METADATA // really everyone ought to have this by now.
137         int rv = -1;
138         char *cvalue = NULL;
139         char *ctype = NULL;
140
141         jack_uuid_t uuid = jack_port_uuid((jack_port_t*) port);
142         rv = jack_get_property(uuid, key.c_str(), &cvalue, &ctype);
143
144         if (0 == rv && cvalue) {
145                 value = cvalue;
146                 if (ctype) {
147                         type = ctype;
148                 }
149         } else {
150                 rv = -1;
151         }
152
153         jack_free(cvalue);
154         jack_free(ctype);
155         return rv;
156 #else
157         return -1;
158 #endif
159 }
160
161 int
162 JACKAudioBackend::set_port_property (PortHandle port, const std::string& key, const std::string& value, const std::string& type)
163 {
164 #ifdef HAVE_JACK_METADATA // really everyone ought to have this by now.
165         int rv = -1;
166         jack_client_t* client = _jack_connection->jack();
167         jack_uuid_t uuid = jack_port_uuid((jack_port_t*) port);
168         return jack_set_property(client, uuid, key.c_str(), value.c_str(), type.c_str());
169         return rv;
170 #else
171         return -1;
172 #endif
173 }
174
175 PortEngine::PortHandle
176 JACKAudioBackend:: get_port_by_name (const std::string& name) const
177 {
178         GET_PRIVATE_JACK_POINTER_RET (_priv_jack, 0);
179         return (PortHandle) jack_port_by_name (_priv_jack, name.c_str());
180 }
181
182 void
183 JACKAudioBackend::_registration_callback (jack_port_id_t /*id*/, int /*reg*/, void* arg)
184 {
185         static_cast<JACKAudioBackend*> (arg)->manager.registration_callback ();
186 }
187
188 int
189 JACKAudioBackend::_graph_order_callback (void *arg)
190 {
191         return static_cast<JACKAudioBackend*> (arg)->manager.graph_order_callback ();
192 }
193
194 void
195 JACKAudioBackend::_connect_callback (jack_port_id_t id_a, jack_port_id_t id_b, int conn, void* arg)
196 {
197         static_cast<JACKAudioBackend*> (arg)->connect_callback (id_a, id_b, conn);
198 }
199
200 void
201 JACKAudioBackend::connect_callback (jack_port_id_t id_a, jack_port_id_t id_b, int conn)
202 {
203         if (manager.port_remove_in_progress()) {
204                 return;
205         }
206
207         GET_PRIVATE_JACK_POINTER (_priv_jack);
208
209         jack_port_t* a = jack_port_by_id (_priv_jack, id_a);
210         jack_port_t* b = jack_port_by_id (_priv_jack, id_b);
211
212         manager.connect_callback (jack_port_name (a), jack_port_name (b), conn == 0 ? false : true);
213 }
214
215 bool
216 JACKAudioBackend::connected (PortHandle port, bool process_callback_safe)
217 {
218         bool ret = false;
219
220         const char** ports;
221
222         if (process_callback_safe) {
223                 ports = jack_port_get_connections ((jack_port_t*)port);
224         } else {
225                 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, false);
226                 ports = jack_port_get_all_connections (_priv_jack, (jack_port_t*)port);
227         }
228
229         if (ports) {
230                 ret = true;
231         }
232
233         jack_free (ports);
234
235         return ret;
236 }
237
238 bool
239 JACKAudioBackend::connected_to (PortHandle port, const std::string& other, bool process_callback_safe)
240 {
241         bool ret = false;
242         const char** ports;
243
244         if (process_callback_safe) {
245                 ports = jack_port_get_connections ((jack_port_t*)port);
246         } else {
247                 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, false);
248                 ports = jack_port_get_all_connections (_priv_jack, (jack_port_t*)port);
249         }
250
251         if (ports) {
252                 for (int i = 0; ports[i]; ++i) {
253                         if (other == ports[i]) {
254                                 ret = true;
255                         }
256                 }
257                 jack_free (ports);
258         }
259
260         return ret;
261 }
262
263 bool
264 JACKAudioBackend::physically_connected (PortHandle p, bool process_callback_safe)
265 {
266         GET_PRIVATE_JACK_POINTER_RET (_priv_jack, false);
267         jack_port_t* port = (jack_port_t*) p;
268
269         const char** ports;
270
271         if (process_callback_safe) {
272                 ports = jack_port_get_connections ((jack_port_t*)port);
273         } else {
274                 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, false);
275                 ports = jack_port_get_all_connections (_priv_jack, (jack_port_t*)port);
276         }
277
278         if (ports) {
279                 for (int i = 0; ports[i]; ++i) {
280
281                         jack_port_t* other = jack_port_by_name (_priv_jack, ports[i]);
282
283                         if (other && (jack_port_flags (other) & JackPortIsPhysical)) {
284                                 jack_free (ports);
285                                 return true;
286                         }
287                 }
288                 jack_free (ports);
289         }
290
291         return false;
292 }
293
294 bool
295 JACKAudioBackend::externally_connected (PortHandle p, bool process_callback_safe)
296 {
297         GET_PRIVATE_JACK_POINTER_RET (_priv_jack, false);
298         jack_port_t* port = (jack_port_t*) p;
299
300         const char** ports;
301
302         if (process_callback_safe) {
303                 ports = jack_port_get_connections ((jack_port_t*)port);
304         } else {
305                 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, false);
306                 ports = jack_port_get_all_connections (_priv_jack, (jack_port_t*)port);
307         }
308
309         if (ports) {
310                 for (int i = 0; ports[i]; ++i) {
311                         jack_port_t* other = jack_port_by_name (_priv_jack, ports[i]);
312
313                         if (other && (jack_port_flags (other) & JackPortIsPhysical)) {
314                                 jack_free (ports);
315                                 return true;
316                         }
317                         if (other && !jack_port_is_mine (_priv_jack, other)) {
318                                 jack_free (ports);
319                                 return true;
320                         }
321                 }
322                 jack_free (ports);
323         }
324         return false;
325 }
326
327 int
328 JACKAudioBackend::get_connections (PortHandle port, vector<string>& s, bool process_callback_safe)
329 {
330         const char** ports;
331
332         if (process_callback_safe) {
333                 ports = jack_port_get_connections ((jack_port_t*)port);
334         } else {
335                 GET_PRIVATE_JACK_POINTER_RET (_priv_jack, 0);
336                 ports = jack_port_get_all_connections (_priv_jack, (jack_port_t*)port);
337         }
338
339         if (ports) {
340                 for (int i = 0; ports[i]; ++i) {
341                         s.push_back (ports[i]);
342                 }
343                 jack_free (ports);
344         }
345
346         return s.size();
347 }
348
349 DataType
350 JACKAudioBackend::port_data_type (PortHandle p) const
351 {
352         return jack_port_type_to_ardour_data_type (jack_port_type ((jack_port_t*) p));
353 }
354
355 const string&
356 JACKAudioBackend::my_name() const
357 {
358         return _jack_connection->client_name();
359 }
360
361 bool
362 JACKAudioBackend::port_is_physical (PortHandle ph) const
363 {
364         if (!ph) {
365                 return false;
366         }
367
368         return jack_port_flags ((jack_port_t*) ph) & JackPortIsPhysical;
369 }
370
371 int
372 JACKAudioBackend::get_ports (const string& port_name_pattern, DataType type, PortFlags flags, vector<string>& s) const
373 {
374
375         GET_PRIVATE_JACK_POINTER_RET (_priv_jack,0);
376
377         const char** ports =  jack_get_ports (_priv_jack, port_name_pattern.c_str(),
378                                               ardour_data_type_to_jack_port_type (type),
379                                               ardour_port_flags_to_jack_flags (flags));
380
381         if (ports == 0) {
382                 return 0;
383         }
384
385         for (uint32_t i = 0; ports[i]; ++i) {
386                 s.push_back (ports[i]);
387         }
388
389         jack_free (ports);
390
391         return s.size();
392 }
393
394 ChanCount
395 JACKAudioBackend::n_physical_inputs () const
396 {
397         return n_physical (JackPortIsInput);
398 }
399
400 ChanCount
401 JACKAudioBackend::n_physical_outputs () const
402 {
403         return n_physical (JackPortIsOutput);
404 }
405
406 void
407 JACKAudioBackend::get_physical (DataType type, unsigned long flags, vector<string>& phy) const
408 {
409         GET_PRIVATE_JACK_POINTER (_priv_jack);
410         const char ** ports;
411
412         if ((ports = jack_get_ports (_priv_jack, NULL, ardour_data_type_to_jack_port_type (type), JackPortIsPhysical | flags)) == 0) {
413                 return;
414         }
415
416         if (ports) {
417                 for (uint32_t i = 0; ports[i]; ++i) {
418                         if (strstr (ports[i], "Midi-Through")) {
419                                 continue;
420                         }
421                         phy.push_back (ports[i]);
422                 }
423                 jack_free (ports);
424         }
425 }
426
427 /** Get physical ports for which JackPortIsOutput is set; ie those that correspond to
428  *  a physical input connector.
429  */
430 void
431 JACKAudioBackend::get_physical_inputs (DataType type, vector<string>& ins)
432 {
433         get_physical (type, JackPortIsOutput, ins);
434 }
435
436 /** Get physical ports for which JackPortIsInput is set; ie those that correspond to
437  *  a physical output connector.
438  */
439 void
440 JACKAudioBackend::get_physical_outputs (DataType type, vector<string>& outs)
441 {
442         get_physical (type, JackPortIsInput, outs);
443 }
444
445
446 bool
447 JACKAudioBackend::can_monitor_input () const
448 {
449         GET_PRIVATE_JACK_POINTER_RET (_priv_jack,false);
450         const char ** ports;
451
452         if ((ports = jack_get_ports (_priv_jack, NULL, JACK_DEFAULT_AUDIO_TYPE, JackPortCanMonitor)) == 0) {
453                 return false;
454         }
455
456         jack_free (ports);
457
458         return true;
459 }
460
461 int
462 JACKAudioBackend::request_input_monitoring (PortHandle port, bool yn)
463 {
464         return jack_port_request_monitor ((jack_port_t*) port, yn);
465 }
466 int
467 JACKAudioBackend::ensure_input_monitoring (PortHandle port, bool yn)
468 {
469         return jack_port_ensure_monitor ((jack_port_t*) port, yn);
470 }
471 bool
472 JACKAudioBackend::monitoring_input (PortHandle port)
473 {
474         return jack_port_monitoring_input ((jack_port_t*) port);
475 }
476
477 PortEngine::PortHandle
478 JACKAudioBackend::register_port (const std::string& shortname, ARDOUR::DataType type, ARDOUR::PortFlags flags)
479 {
480         GET_PRIVATE_JACK_POINTER_RET (_priv_jack, 0);
481         return jack_port_register (_priv_jack, shortname.c_str(),
482                                    ardour_data_type_to_jack_port_type (type),
483                                    ardour_port_flags_to_jack_flags (flags),
484                                    0);
485 }
486
487 void
488 JACKAudioBackend::unregister_port (PortHandle port)
489 {
490         GET_PRIVATE_JACK_POINTER (_priv_jack);
491         (void) jack_port_unregister (_priv_jack, (jack_port_t*) port);
492 }
493
494 int
495 JACKAudioBackend::connect (PortHandle port, const std::string& other)
496 {
497         GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
498         return jack_connect (_priv_jack, jack_port_name ((jack_port_t*) port), other.c_str());
499 }
500 int
501 JACKAudioBackend::connect (const std::string& src, const std::string& dst)
502 {
503         GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
504
505         int r = jack_connect (_priv_jack, src.c_str(), dst.c_str());
506         return r;
507 }
508
509 int
510 JACKAudioBackend::disconnect (PortHandle port, const std::string& other)
511 {
512         GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
513         return jack_disconnect (_priv_jack, jack_port_name ((jack_port_t*) port), other.c_str());
514 }
515
516 int
517 JACKAudioBackend::disconnect (const std::string& src, const std::string& dst)
518 {
519         GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
520         return jack_disconnect (_priv_jack, src.c_str(), dst.c_str());
521 }
522
523 int
524 JACKAudioBackend::disconnect_all (PortHandle port)
525 {
526         GET_PRIVATE_JACK_POINTER_RET (_priv_jack, -1);
527         return jack_port_disconnect (_priv_jack, (jack_port_t*) port);
528 }
529
530 int
531 JACKAudioBackend::midi_event_get (pframes_t& timestamp, size_t& size, uint8_t const** buf, void* port_buffer, uint32_t event_index)
532 {
533         jack_midi_event_t ev;
534         int ret;
535
536         if ((ret = jack_midi_event_get (&ev, port_buffer, event_index)) == 0) {
537                 timestamp = ev.time;
538                 size = ev.size;
539                 *buf = ev.buffer;
540         }
541
542         return ret;
543 }
544
545 int
546 JACKAudioBackend::midi_event_put (void* port_buffer, pframes_t timestamp, const uint8_t* buffer, size_t size)
547 {
548         return jack_midi_event_write (port_buffer, timestamp, buffer, size);
549 }
550
551 uint32_t
552 JACKAudioBackend::get_midi_event_count (void* port_buffer)
553 {
554         return jack_midi_get_event_count (port_buffer);
555 }
556
557 void
558 JACKAudioBackend::midi_clear (void* port_buffer)
559 {
560         jack_midi_clear_buffer (port_buffer);
561 }
562
563 void
564 JACKAudioBackend::set_latency_range (PortHandle port, bool for_playback, LatencyRange r)
565 {
566         jack_latency_range_t range;
567
568         range.min = r.min;
569         range.max = r.max;
570
571         jack_port_set_latency_range ((jack_port_t*) port, for_playback ? JackPlaybackLatency : JackCaptureLatency, &range);
572 }
573
574 LatencyRange
575 JACKAudioBackend::get_latency_range (PortHandle port, bool for_playback)
576 {
577         jack_latency_range_t range;
578         LatencyRange ret;
579
580         jack_port_get_latency_range ((jack_port_t*) port, for_playback ? JackPlaybackLatency : JackCaptureLatency, &range);
581
582         ret.min = range.min;
583         ret.max = range.max;
584
585         return ret;
586 }
587
588 void*
589 JACKAudioBackend::get_buffer (PortHandle port, pframes_t nframes)
590 {
591         return jack_port_get_buffer ((jack_port_t*) port, nframes);
592 }
593
594 uint32_t
595 JACKAudioBackend::port_name_size() const
596 {
597         return jack_port_name_size ();
598 }