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