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