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