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