Merge branch 'master' into windows
[ardour.git] / libs / backends / jack / jack_utils.cc
1 /*
2     Copyright (C) 2010 Paul Davis
3     Copyright (C) 2011 Tim Mayberry
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #ifdef HAVE_ALSA
22 #include <alsa/asoundlib.h>
23 #endif
24
25 #ifdef __APPLE__
26 #include <CoreAudio/CoreAudio.h>
27 #include <CoreFoundation/CFString.h>
28 #include <sys/param.h>
29 #include <mach-o/dyld.h>
30 #endif
31
32 #ifdef HAVE_PORTAUDIO
33 #include <portaudio.h>
34 #endif
35
36 #include <jack/jack.h>
37
38 #include <fstream>
39
40 #include <boost/scoped_ptr.hpp>
41
42 #include <glibmm/miscutils.h>
43
44 #include "pbd/epa.h"
45 #include "pbd/error.h"
46 #include "pbd/convert.h"
47 #include "pbd/file_utils.h"
48 #include "pbd/search_path.h"
49
50 #include "jack_utils.h"
51
52 #ifdef __APPLE
53 #include <CFBundle.h>
54 #endif
55
56 #include "i18n.h"
57
58 using namespace std;
59 using namespace PBD;
60
61 namespace ARDOUR {
62         // The pretty driver names
63         const char * const portaudio_driver_name = X_("Portaudio");
64         const char * const coreaudio_driver_name = X_("CoreAudio");
65         const char * const alsa_driver_name = X_("ALSA");
66         const char * const oss_driver_name = X_("OSS");
67         const char * const freebob_driver_name = X_("FreeBoB");
68         const char * const ffado_driver_name = X_("FFADO");
69         const char * const netjack_driver_name = X_("NetJACK");
70         const char * const dummy_driver_name = X_("Dummy");
71 }
72
73 namespace {
74
75         // The real driver names
76         const char * const portaudio_driver_command_line_name = X_("portaudio");
77         const char * const coreaudio_driver_command_line_name = X_("coreaudio");
78         const char * const alsa_driver_command_line_name = X_("alsa");
79         const char * const oss_driver_command_line_name = X_("oss");
80         const char * const freebob_driver_command_line_name = X_("freebob");
81         const char * const ffado_driver_command_line_name = X_("firewire");
82         const char * const netjack_driver_command_line_name = X_("netjack");
83         const char * const dummy_driver_command_line_name = X_("dummy");
84
85         // should we provide more "pretty" names like above?
86         const char * const alsaint_midi_driver_name = X_("alsa");
87         const char * const alsaseq_midi_driver_name = X_("seq");
88         const char * const alsaraw_midi_driver_name = X_("raw");
89         const char * const winmme_midi_driver_name = X_("winmme");
90         const char * const coremidi_midi_driver_name = X_("coremidi");
91
92         // this should probably be translated
93         const char * const default_device_name = X_("Default");
94 }
95
96 static ARDOUR::MidiOptions midi_options;
97
98 std::string
99 get_none_string ()
100 {
101         return _("None");
102 }
103
104 void
105 ARDOUR::get_jack_audio_driver_names (vector<string>& audio_driver_names)
106 {
107 #ifdef WIN32
108         audio_driver_names.push_back (portaudio_driver_name);
109 #elif __APPLE__
110         audio_driver_names.push_back (coreaudio_driver_name);
111 #else
112 #ifdef HAVE_ALSA
113         audio_driver_names.push_back (alsa_driver_name);
114 #endif
115         audio_driver_names.push_back (oss_driver_name);
116         audio_driver_names.push_back (freebob_driver_name);
117         audio_driver_names.push_back (ffado_driver_name);
118 #endif
119         audio_driver_names.push_back (netjack_driver_name);
120         audio_driver_names.push_back (dummy_driver_name);
121 }
122
123 void
124 ARDOUR::get_jack_default_audio_driver_name (string& audio_driver_name)
125 {
126         vector<string> drivers;
127         get_jack_audio_driver_names (drivers);
128         audio_driver_name = drivers.front ();
129 }
130
131 void
132 ARDOUR::get_jack_sample_rate_strings (vector<string>& samplerates)
133 {
134         // do these really need to be translated?
135         samplerates.push_back (_("8000Hz"));
136         samplerates.push_back (_("22050Hz"));
137         samplerates.push_back (_("44100Hz"));
138         samplerates.push_back (_("48000Hz"));
139         samplerates.push_back (_("88200Hz"));
140         samplerates.push_back (_("96000Hz"));
141         samplerates.push_back (_("192000Hz"));
142 }
143
144 string
145 ARDOUR::get_jack_default_sample_rate ()
146 {
147         return _("48000Hz");
148 }
149
150 void
151 ARDOUR::get_jack_period_size_strings (std::vector<std::string>& period_sizes)
152 {
153         period_sizes.push_back ("32");
154         period_sizes.push_back ("64");
155         period_sizes.push_back ("128");
156         period_sizes.push_back ("256");
157         period_sizes.push_back ("512");
158         period_sizes.push_back ("1024");
159         period_sizes.push_back ("2048");
160         period_sizes.push_back ("4096");
161         period_sizes.push_back ("8192");
162 }
163
164 string
165 ARDOUR::get_jack_default_period_size ()
166 {
167         return "1024";
168 }
169
170 void
171 ARDOUR::get_jack_dither_mode_strings (const string& driver, vector<string>& dither_modes)
172 {
173         dither_modes.push_back (get_none_string ());
174
175         if (driver == alsa_driver_name ) {
176                 dither_modes.push_back (_("Triangular"));
177                 dither_modes.push_back (_("Rectangular"));
178                 dither_modes.push_back (_("Shaped"));
179         }
180 }
181
182 string
183 ARDOUR::get_jack_default_dither_mode (const string& /*driver*/)
184 {
185         return get_none_string ();
186 }
187
188 string
189 ARDOUR::get_jack_latency_string (string samplerate, float periods, string period_size)
190 {
191         uint32_t rate = atoi (samplerate);
192         float psize = atof (period_size);
193
194         char buf[32];
195         snprintf (buf, sizeof(buf), "%.1fmsec", (periods * psize) / (rate/1000.0));
196
197         return buf;
198 }
199
200 bool
201 get_jack_command_line_audio_driver_name (const string& driver_name, string& command_line_name)
202 {
203         using namespace ARDOUR;
204         if (driver_name == portaudio_driver_name) {
205                 command_line_name = portaudio_driver_command_line_name;
206                 return true;
207         } else if (driver_name == coreaudio_driver_name) {
208                 command_line_name = coreaudio_driver_command_line_name;
209                 return true;
210         } else if (driver_name == alsa_driver_name) {
211                 command_line_name = alsa_driver_command_line_name;
212                 return true;
213         } else if (driver_name == oss_driver_name) {
214                 command_line_name = oss_driver_command_line_name;
215                 return true;
216         } else if (driver_name == freebob_driver_name) {
217                 command_line_name = freebob_driver_command_line_name;
218                 return true;
219         } else if (driver_name == ffado_driver_name) {
220                 command_line_name = ffado_driver_command_line_name;
221                 return true;
222         } else if (driver_name == netjack_driver_name) {
223                 command_line_name = netjack_driver_command_line_name;
224                 return true;
225         } else if (driver_name == dummy_driver_name) {
226                 command_line_name = dummy_driver_command_line_name;
227                 return true;
228         }
229         return false;
230 }
231
232 bool
233 get_jack_command_line_audio_device_name (const string& driver_name,
234                 const string& device_name, string& command_line_device_name)
235 {
236         using namespace ARDOUR;
237         device_map_t devices;
238
239         get_jack_device_names_for_audio_driver (driver_name, devices);
240
241         for (device_map_t::const_iterator i = devices.begin (); i != devices.end(); ++i) {
242                 if (i->first == device_name) {
243                         command_line_device_name = i->second;
244                         return true;
245                 }
246         }
247         return false;
248 }
249
250 bool
251 get_jack_command_line_dither_mode (const string& dither_mode, string& command_line_dither_mode)
252 {
253         using namespace ARDOUR;
254
255         if (dither_mode == _("Triangular")) {
256                 command_line_dither_mode = "triangular";
257                 return true;
258         } else if (dither_mode == _("Rectangular")) {
259                 command_line_dither_mode = "rectangular";
260                 return true;
261         } else if (dither_mode == _("Shaped")) {
262                 command_line_dither_mode = "shaped";
263                 return true;
264         }
265
266         return false;
267 }
268
269 void
270 ARDOUR::get_jack_alsa_device_names (device_map_t& devices)
271 {
272 #ifdef HAVE_ALSA
273         snd_ctl_t *handle;
274         snd_ctl_card_info_t *info;
275         snd_pcm_info_t *pcminfo;
276         snd_ctl_card_info_alloca(&info);
277         snd_pcm_info_alloca(&pcminfo);
278         string devname;
279         int cardnum = -1;
280         int device = -1;
281
282         while (snd_card_next (&cardnum) >= 0 && cardnum >= 0) {
283
284                 devname = "hw:";
285                 devname += PBD::to_string (cardnum, std::dec);
286
287                 if (snd_ctl_open (&handle, devname.c_str(), 0) >= 0 && snd_ctl_card_info (handle, info) >= 0) {
288
289                         if (snd_ctl_card_info (handle, info) < 0) {
290                                 continue;
291                         }
292                         
293                         string card_name = snd_ctl_card_info_get_name (info);
294
295                         /* change devname to use ID, not number */
296
297                         devname = "hw:";
298                         devname += snd_ctl_card_info_get_id (info);
299
300                         while (snd_ctl_pcm_next_device (handle, &device) >= 0 && device >= 0) {
301                                 
302                                 /* only detect duplex devices here. more
303                                  * complex arrangements are beyond our scope
304                                  */
305
306                                 snd_pcm_info_set_device (pcminfo, device);
307                                 snd_pcm_info_set_subdevice (pcminfo, 0);
308                                 snd_pcm_info_set_stream (pcminfo, SND_PCM_STREAM_CAPTURE);
309                                 
310                                 if (snd_ctl_pcm_info (handle, pcminfo) >= 0) {
311
312                                         snd_pcm_info_set_device (pcminfo, device);
313                                         snd_pcm_info_set_subdevice (pcminfo, 0);
314                                         snd_pcm_info_set_stream (pcminfo, SND_PCM_STREAM_PLAYBACK);
315
316                                         if (snd_ctl_pcm_info (handle, pcminfo) >= 0) {
317                                                 devname += ',';
318                                                 devname += PBD::to_string (device, std::dec);
319                                                 devices.insert (std::make_pair (card_name, devname));
320                                         }
321                                 }
322                         }
323
324                         snd_ctl_close(handle);
325                 }
326         }
327 #else
328         /* silence a compiler unused variable warning */
329         (void) devices;
330 #endif
331 }
332
333 #ifdef __APPLE__
334 static OSStatus
335 getDeviceUIDFromID( AudioDeviceID id, char *name, size_t nsize)
336 {
337         UInt32 size = sizeof(CFStringRef);
338         CFStringRef UI;
339         OSStatus res = AudioDeviceGetProperty(id, 0, false,
340                 kAudioDevicePropertyDeviceUID, &size, &UI);
341         if (res == noErr)
342                 CFStringGetCString(UI,name,nsize,CFStringGetSystemEncoding());
343         CFRelease(UI);
344         return res;
345 }
346 #endif
347
348 void
349 ARDOUR::get_jack_coreaudio_device_names (device_map_t& devices)
350 {
351 #ifdef __APPLE__
352         // Find out how many Core Audio devices are there, if any...
353         // (code snippet gently "borrowed" from St?hane Letz jackdmp;)
354         OSStatus err;
355         Boolean isWritable;
356         UInt32 outSize = sizeof(isWritable);
357
358         err = AudioHardwareGetPropertyInfo(kAudioHardwarePropertyDevices,
359                                            &outSize, &isWritable);
360         if (err == noErr) {
361                 // Calculate the number of device available...
362                 int numCoreDevices = outSize / sizeof(AudioDeviceID);
363                 // Make space for the devices we are about to get...
364                 AudioDeviceID *coreDeviceIDs = new AudioDeviceID [numCoreDevices];
365                 err = AudioHardwareGetProperty(kAudioHardwarePropertyDevices,
366                                                &outSize, (void *) coreDeviceIDs);
367                 if (err == noErr) {
368                         // Look for the CoreAudio device name...
369                         char coreDeviceName[256];
370                         UInt32 nameSize;
371
372                         for (int i = 0; i < numCoreDevices; i++) {
373
374                                 nameSize = sizeof (coreDeviceName);
375
376                                 /* enforce duplex devices only */
377
378                                 err = AudioDeviceGetPropertyInfo(coreDeviceIDs[i],
379                                                                  0, true, kAudioDevicePropertyStreams,
380                                                                  &outSize, &isWritable);
381
382                                 if (err != noErr || outSize == 0) {
383                                         continue;
384                                 }
385
386                                 err = AudioDeviceGetPropertyInfo(coreDeviceIDs[i],
387                                                                  0, false, kAudioDevicePropertyStreams,
388                                                                  &outSize, &isWritable);
389
390                                 if (err != noErr || outSize == 0) {
391                                         continue;
392                                 }
393
394                                 err = AudioDeviceGetPropertyInfo(coreDeviceIDs[i],
395                                                                  0, true, kAudioDevicePropertyDeviceName,
396                                                                  &outSize, &isWritable);
397                                 if (err == noErr) {
398                                         err = AudioDeviceGetProperty(coreDeviceIDs[i],
399                                                                      0, true, kAudioDevicePropertyDeviceName,
400                                                                      &nameSize, (void *) coreDeviceName);
401                                         if (err == noErr) {
402                                                 char drivername[128];
403
404                                                 // this returns the unique id for the device
405                                                 // that must be used on the commandline for jack
406
407                                                 if (getDeviceUIDFromID(coreDeviceIDs[i], drivername, sizeof (drivername)) == noErr) {
408                                                         devices.insert (make_pair (coreDeviceName, drivername));
409                                                 }
410                                         }
411                                 }
412                         }
413                 }
414                 delete [] coreDeviceIDs;
415         }
416 #else
417         /* silence a compiler unused variable warning */
418         (void) devices;
419 #endif
420 }
421
422 void
423 ARDOUR::get_jack_portaudio_device_names (device_map_t& devices)
424 {
425 #ifdef HAVE_PORTAUDIO
426         if (Pa_Initialize() != paNoError) {
427                 return;
428         }
429
430         for (PaDeviceIndex i = 0; i < Pa_GetDeviceCount (); ++i) {
431                 string api_name;
432                 string readable_name;
433                 string jack_device_name;
434                 const PaDeviceInfo* device_info = Pa_GetDeviceInfo(i);
435
436                 if (device_info != NULL) { // it should never be ?
437                         api_name = Pa_GetHostApiInfo (device_info->hostApi)->name;
438                         readable_name = api_name + " " + device_info->name;
439                         jack_device_name = api_name + "::" + device_info->name;
440                         devices.insert (make_pair (readable_name, jack_device_name));
441                 }
442         }
443         Pa_Terminate();
444 #else
445         /* silence a compiler unused variable warning */
446         (void) devices;
447 #endif
448 }
449
450 void
451 ARDOUR::get_jack_oss_device_names (device_map_t& devices)
452 {
453         devices.insert (make_pair (default_device_name, default_device_name));
454 }
455
456 void
457 ARDOUR::get_jack_freebob_device_names (device_map_t& devices)
458 {
459         devices.insert (make_pair (default_device_name, default_device_name));
460 }
461
462 void
463 ARDOUR::get_jack_ffado_device_names (device_map_t& devices)
464 {
465         devices.insert (make_pair (default_device_name, default_device_name));
466 }
467
468 void
469 ARDOUR::get_jack_netjack_device_names (device_map_t& devices)
470 {
471         devices.insert (make_pair (default_device_name, default_device_name));
472 }
473
474 void
475 ARDOUR::get_jack_dummy_device_names (device_map_t& devices)
476 {
477         devices.insert (make_pair (default_device_name, default_device_name));
478 }
479
480 bool
481 ARDOUR::get_jack_device_names_for_audio_driver (const string& driver_name, device_map_t& devices)
482 {
483         devices.clear();
484
485         if (driver_name == portaudio_driver_name) {
486                 get_jack_portaudio_device_names (devices);
487         } else if (driver_name == coreaudio_driver_name) {
488                 get_jack_coreaudio_device_names (devices);
489         } else if (driver_name == alsa_driver_name) {
490                 get_jack_alsa_device_names (devices);
491         } else if (driver_name == oss_driver_name) {
492                 get_jack_oss_device_names (devices);
493         } else if (driver_name == freebob_driver_name) {
494                 get_jack_freebob_device_names (devices);
495         } else if (driver_name == ffado_driver_name) {
496                 get_jack_ffado_device_names (devices);
497         } else if (driver_name == netjack_driver_name) {
498                 get_jack_netjack_device_names (devices);
499         } else if (driver_name == dummy_driver_name) {
500                 get_jack_dummy_device_names (devices);
501         }
502
503         return !devices.empty();
504 }
505
506
507 std::vector<std::string>
508 ARDOUR::get_jack_device_names_for_audio_driver (const string& driver_name)
509 {
510         std::vector<std::string> readable_names;
511         device_map_t devices;
512
513         get_jack_device_names_for_audio_driver (driver_name, devices);
514
515         for (device_map_t::const_iterator i = devices.begin (); i != devices.end(); ++i) {
516                 readable_names.push_back (i->first);
517         }
518
519         return readable_names;
520 }
521
522 bool
523 ARDOUR::get_jack_audio_driver_supports_two_devices (const string& driver)
524 {
525         return (driver == alsa_driver_name || driver == oss_driver_name);
526 }
527
528 bool
529 ARDOUR::get_jack_audio_driver_supports_latency_adjustment (const string& driver)
530 {
531         return (driver == alsa_driver_name || driver == coreaudio_driver_name ||
532                         driver == ffado_driver_name || driver == portaudio_driver_name);
533 }
534
535 bool
536 ARDOUR::get_jack_audio_driver_supports_setting_period_count (const string& driver)
537 {
538         return !(driver == dummy_driver_name || driver == coreaudio_driver_name ||
539                         driver == portaudio_driver_name);
540 }
541
542 bool
543 ARDOUR::get_jack_server_application_names (std::vector<std::string>& server_names)
544 {
545 #ifdef WIN32
546         server_names.push_back ("jackd.exe");
547 #else
548         server_names.push_back ("jackd");
549         server_names.push_back ("jackdmp");
550 #endif
551         return !server_names.empty();
552 }
553
554 void
555 ARDOUR::set_path_env_for_jack_autostart (const vector<std::string>& dirs)
556 {
557 #ifdef __APPLE__
558         // push it back into the environment so that auto-started JACK can find it.
559         // XXX why can't we just expect OS X users to have PATH set correctly? we can't ...
560         setenv ("PATH", Searchpath(dirs).to_string().c_str(), 1);
561 #else
562         /* silence a compiler unused variable warning */
563         (void) dirs;
564 #endif
565 }
566
567 bool
568 ARDOUR::get_jack_server_dir_paths (vector<std::string>& server_dir_paths)
569 {
570 #ifdef __APPLE__
571         /* this magic lets us finds the path to the OSX bundle, and then
572            we infer JACK's location from there
573         */
574
575         char execpath[MAXPATHLEN+1];
576         uint32_t pathsz = sizeof (execpath);
577
578         _NSGetExecutablePath (execpath, &pathsz);
579
580         server_dir_paths.push_back (Glib::path_get_dirname (execpath));
581 #endif
582
583         Searchpath sp(string(g_getenv("PATH")));
584
585 #ifdef WIN32
586         gchar *install_dir = g_win32_get_package_installation_directory_of_module (NULL);
587         if (install_dir) {
588                 sp.push_back (install_dir);
589                 g_free (install_dir);
590         }
591         // don't try and use a system wide JACK install yet.
592 #else
593         if (sp.empty()) {
594                 sp.push_back ("/usr/bin");
595                 sp.push_back ("/bin");
596                 sp.push_back ("/usr/local/bin");
597                 sp.push_back ("/opt/local/bin");
598         }
599 #endif
600
601         std::copy (sp.begin(), sp.end(), std::back_inserter(server_dir_paths));
602
603         return !server_dir_paths.empty();
604 }
605
606 bool
607 ARDOUR::get_jack_server_paths (const vector<std::string>& server_dir_paths,
608                 const vector<string>& server_names,
609                 vector<std::string>& server_paths)
610 {
611         for (vector<string>::const_iterator i = server_names.begin(); i != server_names.end(); ++i) {
612                 Glib::PatternSpec ps (*i);
613                 find_matching_files_in_directories (server_dir_paths, ps, server_paths);
614         }
615         return !server_paths.empty();
616 }
617
618 bool
619 ARDOUR::get_jack_server_paths (vector<std::string>& server_paths)
620 {
621         vector<std::string> server_dirs;
622
623         if (!get_jack_server_dir_paths (server_dirs)) {
624                 return false;
625         }
626
627         vector<string> server_names;
628
629         if (!get_jack_server_application_names (server_names)) {
630                 return false;
631         }
632
633         if (!get_jack_server_paths (server_dirs, server_names, server_paths)) {
634                 return false;
635         }
636
637         return !server_paths.empty();
638 }
639
640 bool
641 ARDOUR::get_jack_default_server_path (std::string& server_path)
642 {
643         vector<std::string> server_paths;
644
645         if (!get_jack_server_paths (server_paths)) {
646                 return false;
647         }
648
649         server_path = server_paths.front ();
650         return true;
651 }
652
653 string
654 quote_string (const string& str)
655 {
656         return "\"" + str + "\"";
657 }
658
659 ARDOUR::JackCommandLineOptions::JackCommandLineOptions ()
660         : server_path ()
661         , timeout(0)
662         , no_mlock(false)
663         , ports_max(128)
664         , realtime(true)
665         , priority(0)
666         , unlock_gui_libs(false)
667         , verbose(false)
668         , temporary(true)
669         , driver()
670         , input_device()
671         , output_device()
672         , num_periods(2)
673         , period_size(1024)
674         , samplerate(48000)
675         , input_latency(0)
676         , output_latency(0)
677         , hardware_metering(false)
678         , hardware_monitoring(false)
679         , dither_mode()
680         , force16_bit(false)
681         , soft_mode(false)
682         , midi_driver()
683 {
684
685 }
686
687 bool
688 ARDOUR::get_jack_command_line_string (JackCommandLineOptions& options, string& command_line, bool for_latency_measurement)
689 {
690         vector<string> args;
691
692         args.push_back (options.server_path);
693
694 #ifdef WIN32
695         // must use sync mode on windows
696         args.push_back ("-S");
697
698         // this needs to be added now on windows
699         if (!options.midi_driver.empty () && options.midi_driver != get_none_string ()) {
700                 args.push_back ("-X");
701                 args.push_back (options.midi_driver);
702         }
703 #endif
704
705         /* XXX hack to enforce qjackctl-like behaviour */
706         if (options.timeout == 0) {
707                 options.timeout = 200;
708         }
709
710         if (options.timeout) {
711                 args.push_back ("-t");
712                 args.push_back (to_string (options.timeout, std::dec));
713         }
714
715         if (options.no_mlock) {
716                 args.push_back ("-m");
717         }
718
719         args.push_back ("-p");
720         args.push_back (to_string(options.ports_max, std::dec));
721
722         if (options.realtime) {
723                 args.push_back ("-R");
724                 if (options.priority != 0) {
725                         args.push_back ("-P");
726                         args.push_back (to_string(options.priority, std::dec));
727                 }
728         } else {
729                 args.push_back ("-r");
730         }
731
732         if (options.unlock_gui_libs) {
733                 args.push_back ("-u");
734         }
735
736         if (options.verbose) {
737                 args.push_back ("-v");
738         }
739
740 #ifndef WIN32
741         if (options.temporary) {
742                 args.push_back ("-T");
743         }
744 #endif
745
746         if (options.driver == alsa_driver_name) {
747                 if (options.midi_driver == alsaint_midi_driver_name) {
748                         args.push_back ("-I");
749                         args.push_back ("alsa_midi");
750                 }
751         }
752
753         string command_line_driver_name;
754
755         if (!get_jack_command_line_audio_driver_name (options.driver, command_line_driver_name)) {
756                 return false;
757         }
758
759         args.push_back ("-d");
760         args.push_back (command_line_driver_name);
761
762         if (options.output_device.empty() && options.input_device.empty()) {
763                 return false;
764         }
765
766         string command_line_input_device_name;
767         string command_line_output_device_name;
768
769         if (!get_jack_command_line_audio_device_name (options.driver,
770                 options.input_device, command_line_input_device_name)) {
771                 return false;
772         }
773
774         if (!get_jack_command_line_audio_device_name (options.driver,
775                 options.output_device, command_line_output_device_name)) {
776                 return false;
777         }
778
779         if (options.input_device.empty()) {
780                 // playback only
781                 if (options.output_device.empty()) {
782                         return false;
783                 }
784                 args.push_back ("-P");
785         } else if (options.output_device.empty()) {
786                 // capture only
787                 if (options.input_device.empty()) {
788                         return false;
789                 }
790                 args.push_back ("-C");
791         } else if (options.input_device != options.output_device) {
792                 // capture and playback on two devices if supported
793                 if (get_jack_audio_driver_supports_two_devices (options.driver)) {
794                         args.push_back ("-C");
795                         args.push_back (command_line_input_device_name);
796                         args.push_back ("-P");
797                         args.push_back (command_line_output_device_name);
798                 } else {
799                         return false;
800                 }
801         }
802
803         if (options.input_channels) {
804                 args.push_back ("-i");
805                 args.push_back (to_string (options.input_channels, std::dec));
806         }
807
808         if (options.output_channels) {
809                 args.push_back ("-o");
810                 args.push_back (to_string (options.output_channels, std::dec));
811         }
812
813         if (get_jack_audio_driver_supports_setting_period_count (options.driver)) {
814                 args.push_back ("-n");
815                 args.push_back (to_string (options.num_periods, std::dec));
816         }
817
818         args.push_back ("-r");
819         args.push_back (to_string (options.samplerate, std::dec));
820
821         args.push_back ("-p");
822         args.push_back (to_string (options.period_size, std::dec));
823
824         if (!for_latency_measurement && get_jack_audio_driver_supports_latency_adjustment (options.driver)) {
825                 if (options.input_latency) {
826                         args.push_back ("-I");
827                         args.push_back (to_string (options.input_latency, std::dec));
828                 }
829                 if (options.output_latency) {
830                         args.push_back ("-O");
831                         args.push_back (to_string (options.output_latency, std::dec));
832                 }
833         }
834
835         if (options.input_device == options.output_device && options.input_device != default_device_name) {
836                 args.push_back ("-d");
837                 args.push_back (command_line_input_device_name);
838         }
839
840         if (options.driver == alsa_driver_name) {
841                 if (options.hardware_metering) {
842                         args.push_back ("-M");
843                 }
844                 if (options.hardware_monitoring) {
845                         args.push_back ("-H");
846                 }
847
848                 string command_line_dither_mode;
849                 if (get_jack_command_line_dither_mode (options.dither_mode, command_line_dither_mode)) {
850                         args.push_back ("-z");
851                         args.push_back (command_line_dither_mode);
852                 }
853                 if (options.force16_bit) {
854                         args.push_back ("-S");
855                 }
856                 if (options.soft_mode) {
857                         args.push_back ("-s");
858                 }
859         }
860
861         if (options.driver == alsa_driver_name || options.driver == coreaudio_driver_name) {
862
863                 if (options.midi_driver != alsaint_midi_driver_name) {
864                         if (!options.midi_driver.empty() && options.midi_driver != get_none_string ()) {
865                                 args.push_back ("-X");
866                                 args.push_back (options.midi_driver);
867                         }
868                 }
869         }
870
871         ostringstream oss;
872
873         for (vector<string>::const_iterator i = args.begin(); i != args.end();) {
874 #ifdef WIN32
875                 oss << quote_string (*i);
876 #else
877                 oss << *i;
878 #endif
879                 if (++i != args.end()) oss << ' ';
880         }
881
882         command_line = oss.str();
883         return true;
884 }
885
886 string
887 ARDOUR::get_jack_server_config_file_name ()
888 {
889         return ".jackdrc";
890 }
891
892 std::string
893 ARDOUR::get_jack_server_user_config_dir_path ()
894 {
895         return Glib::get_home_dir ();
896 }
897
898 std::string
899 ARDOUR::get_jack_server_user_config_file_path ()
900 {
901         return Glib::build_filename (get_jack_server_user_config_dir_path (), get_jack_server_config_file_name ());
902 }
903
904 bool
905 ARDOUR::write_jack_config_file (const std::string& config_file_path, const string& command_line)
906 {
907         ofstream jackdrc (config_file_path.c_str());
908
909         if (!jackdrc) {
910                 error << string_compose (_("cannot open JACK rc file %1 to store parameters"), config_file_path) << endmsg;
911                 return false;
912         }
913
914         jackdrc << command_line << endl;
915         jackdrc.close ();
916         return true;
917 }
918
919 vector<string>
920 ARDOUR::enumerate_midi_options () 
921 {
922         if (midi_options.empty()) {
923 #ifdef HAVE_ALSA
924                 midi_options.push_back (make_pair (_("ALSA"), alsaint_midi_driver_name));
925                 midi_options.push_back (make_pair (_("(legacy) ALSA raw devices"), alsaraw_midi_driver_name));
926                 midi_options.push_back (make_pair (_("(legacy) ALSA sequencer"), alsaseq_midi_driver_name));
927 #endif
928 #ifdef HAVE_PORTAUDIO
929                 /* Windows folks: what name makes sense here? Are there other
930                    choices as well ?
931                 */
932                 midi_options.push_back (make_pair (_("Multimedia Extension"), winmme_midi_driver_name));
933 #endif
934 #ifdef __APPLE__
935                 midi_options.push_back (make_pair (_("CoreMIDI"), coremidi_midi_driver_name));
936 #endif
937         }
938
939         vector<string> v;
940
941         v.push_back (get_none_string());
942
943         for (MidiOptions::const_iterator i = midi_options.begin(); i != midi_options.end(); ++i) {
944                 v.push_back (i->first);
945         }
946
947         return v;
948 }
949
950 int
951 ARDOUR::set_midi_option (ARDOUR::JackCommandLineOptions& options, const string& opt)
952 {
953         if (opt.empty() || opt == get_none_string()) {
954                 options.midi_driver = "";
955                 return 0;
956         }
957
958         for (MidiOptions::const_iterator i = midi_options.begin(); i != midi_options.end(); ++i) {
959                 if (i->first == opt) {
960                         options.midi_driver = i->second;
961                         return 0;
962                 }
963         }
964
965         return -1;
966 }
967