Merge branch 'master' into windows
[ardour.git] / gtk2_ardour / engine_dialog.cc
index e1c8b679865a33899895133b78338eead2023eea..05cd9a661ed8bae19245759ae54fef19380a4431 100644 (file)
 #include <CoreFoundation/CFString.h>
 #include <sys/param.h>
 #include <mach-o/dyld.h>
-#else
+#elif !defined(__FreeBSD__)
 #include <alsa/asoundlib.h>
 #endif
 
-#include "ardour/profile.h"
 #include <jack/jack.h>
 
 #include <gtkmm/stock.h>
 #include <gtkmm2ext/utils.h>
 
+#include "ardour/rc_configuration.h"
+
 #include "pbd/convert.h"
 #include "pbd/error.h"
 #include "pbd/pathscanner.h"
@@ -65,8 +66,6 @@ using namespace Glib;
 EngineControl::EngineControl ()
        : periods_adjustment (2, 2, 16, 1, 2),
          periods_spinner (periods_adjustment),
-         priority_adjustment (60, 10, 90, 1, 10),
-         priority_spinner (priority_adjustment),
          ports_adjustment (128, 8, 1024, 1, 16),
          ports_spinner (ports_adjustment),
          input_latency_adjustment (0, 0, 99999, 1),
@@ -84,7 +83,7 @@ EngineControl::EngineControl ()
          verbose_output_button (_("Verbose output")),
          start_button (_("Start")),
          stop_button (_("Stop")),
-#ifdef __APPLE__
+#ifdef __APPLE___
          basic_packer (5, 2),
          options_packer (4, 2),
          device_packer (4, 2)
@@ -140,7 +139,9 @@ EngineControl::EngineControl ()
 #ifdef __APPLE__
        strings.push_back (X_("CoreAudio"));
 #else
+#ifndef __FreeBSD__
        strings.push_back (X_("ALSA"));
+#endif
        strings.push_back (X_("OSS"));
        strings.push_back (X_("FreeBoB"));
        strings.push_back (X_("FFADO"));
@@ -166,48 +167,46 @@ EngineControl::EngineControl ()
 
        strings.clear ();
        strings.push_back (_("None"));
+#ifdef __APPLE__
+       strings.push_back (_("coremidi"));
+#else
        strings.push_back (_("seq"));
        strings.push_back (_("raw"));
+#endif
        set_popdown_strings (midi_driver_combo, strings);
        midi_driver_combo.set_active_text (strings.front ());
 
        row = 0;
 
-       label = manage (new Label (_("Driver:")));
-       label->set_alignment (0, 0.5);
+       label = manage (left_aligned_label (_("Driver:")));
        basic_packer.attach (*label, 0, 1, row, row + 1, FILL|EXPAND, (AttachOptions) 0);
        basic_packer.attach (driver_combo, 1, 2, row, row + 1, FILL|EXPAND, (AttachOptions) 0);
        row++;
 
-       label = manage (new Label (_("Audio Interface:")));
-       label->set_alignment (0, 0.5);
+       label = manage (left_aligned_label (_("Audio Interface:")));
        basic_packer.attach (*label, 0, 1, row, row + 1, FILL|EXPAND, (AttachOptions) 0);
        basic_packer.attach (interface_combo, 1, 2, row, row + 1, FILL|EXPAND, (AttachOptions) 0);
        row++;
 
-       label = manage (new Label (_("Sample rate:")));
-       label->set_alignment (0, 0.5);
+       label = manage (left_aligned_label (_("Sample rate:")));
        basic_packer.attach (*label, 0, 1, row, row + 1, FILL|EXPAND, (AttachOptions) 0);
        basic_packer.attach (sample_rate_combo, 1, 2, row, row + 1, FILL|EXPAND, (AttachOptions) 0);
        row++;
 
-       label = manage (new Label (_("Buffer size:")));
-       label->set_alignment (0, 0.5);
+       label = manage (left_aligned_label (_("Buffer size:")));
        basic_packer.attach (*label, 0, 1, row, row + 1, FILL|EXPAND, (AttachOptions) 0);
        basic_packer.attach (period_size_combo, 1, 2, row, row + 1, FILL|EXPAND, (AttachOptions) 0);
        row++;
 
-#ifndef __APPLE__
-       label = manage (new Label (_("Number of buffers:")));
-       label->set_alignment (0, 0.5);
+#if !defined(__APPLE__) && !defined(__FreeBSD__)
+       label = manage (left_aligned_label (_("Number of buffers:")));
        basic_packer.attach (*label, 0, 1, row, row + 1, FILL|EXPAND, (AttachOptions) 0);
        basic_packer.attach (periods_spinner, 1, 2, row, row + 1, FILL|EXPAND, (AttachOptions) 0);
        periods_spinner.set_value (2);
        row++;
 #endif
 
-       label = manage (new Label (_("Approximate latency:")));
-       label->set_alignment (0, 0.5);
+       label = manage (left_aligned_label (_("Approximate latency:")));
        basic_packer.attach (*label, 0, 1, row, row + 1, FILL|EXPAND, (AttachOptions) 0);
        basic_packer.attach (latency_label, 1, 2, row, row + 1, FILL|EXPAND, (AttachOptions) 0);
        row++;
@@ -219,9 +218,8 @@ EngineControl::EngineControl ()
        row++;
        /* no audio mode with CoreAudio, its duplex or nuthin' */
 
-#ifndef __APPLE__
-       label = manage (new Label (_("Audio mode:")));
-       label->set_alignment (0, 0.5);
+#if !defined(__APPLE__) && !defined(__FreeBSD__)
+       label = manage (left_aligned_label (_("Audio mode:")));
        basic_packer.attach (*label, 0, 1, row, row + 1, FILL|EXPAND, (AttachOptions) 0);
        basic_packer.attach (audio_mode_combo, 1, 2, row, row + 1, FILL|EXPAND, (AttachOptions) 0);
        row++;
@@ -257,19 +255,10 @@ EngineControl::EngineControl ()
        ++row;
 
        realtime_button.set_active (true);
-       realtime_button.signal_toggled().connect (sigc::mem_fun (*this, &EngineControl::realtime_changed));
-       realtime_changed ();
 
 #if PROVIDE_TOO_MANY_OPTIONS
 
-#ifndef __APPLE__
-       label = manage (new Label (_("Realtime Priority")));
-       label->set_alignment (1.0, 0.5);
-       options_packer.attach (*label, 0, 1, row, row + 1, FILL|EXPAND, (AttachOptions) 0);
-       options_packer.attach (priority_spinner, 1, 2, row, row + 1, FILL|EXPAND, (AttachOptions) 0);
-       ++row;
-       priority_spinner.set_value (60);
-
+#if !defined(__APPLE__) && !defined(__FreeBSD__)
        options_packer.attach (no_memory_lock_button, 1, 2, row, row + 1, FILL|EXPAND, (AttachOptions) 0);
        ++row;
        options_packer.attach (unlock_memory_button, 1, 2, row, row + 1, FILL|EXPAND, (AttachOptions) 0);
@@ -307,21 +296,18 @@ EngineControl::EngineControl ()
        ++row;
 
 #endif /* PROVIDE_TOO_MANY_OPTIONS */
-       label = manage (new Label (_("Number of ports:")));
-       label->set_alignment (0, 0.5);
+       label = manage (left_aligned_label (_("Number of ports:")));
        options_packer.attach (ports_spinner, 1, 2, row, row + 1, FILL|EXPAND, AttachOptions(0));
        options_packer.attach (*label, 0, 1, row, row + 1, FILL|EXPAND, (AttachOptions) 0);
        ++row;
 
-       label = manage (new Label (_("MIDI driver:")));
-       label->set_alignment (0, 0.5);
+       label = manage (left_aligned_label (_("MIDI driver:")));
        options_packer.attach (midi_driver_combo, 1, 2, row, row + 1, FILL|EXPAND, AttachOptions(0));
        options_packer.attach (*label, 0, 1, row, row + 1, FILL|EXPAND, (AttachOptions) 0);
        ++row;
 
-#ifndef __APPLE__
-       label = manage (new Label (_("Dither:")));
-       label->set_alignment (0, 0.5);
+#if !defined(__APPLE__) && !defined(__FreeBSD__)
+       label = manage (left_aligned_label (_("Dither:")));
        options_packer.attach (dither_mode_combo, 1, 2, row, row + 1, FILL|EXPAND, AttachOptions(0));
        options_packer.attach (*label, 0, 1, row, row + 1, FILL|EXPAND, (AttachOptions) 0);
        ++row;
@@ -338,9 +324,8 @@ EngineControl::EngineControl ()
        serverpath_combo.set_active_text (server_strings.front());
 
        if (server_strings.size() > 1) {
-               label = manage (new Label (_("Server:")));
+               label = manage (left_aligned_label (_("Server:")));
                options_packer.attach (*label, 0, 1, row, row + 1, FILL|EXPAND, (AttachOptions) 0);
-               label->set_alignment (0.0, 0.5);
                options_packer.attach (serverpath_combo, 1, 2, row, row + 1, FILL|EXPAND, (AttachOptions) 0);
                ++row;
        }
@@ -350,32 +335,26 @@ EngineControl::EngineControl ()
        device_packer.set_spacings (6);
        row = 0;
 
-#ifndef __APPLE__
-       label = manage (new Label (_("Input device:")));
-       label->set_alignment (0, 0.5);
+#if !defined(__APPLE__) && !defined(__FreeBSD__)
+       label = manage (left_aligned_label (_("Input device:")));
        device_packer.attach (*label, 0, 1, row, row+1, FILL|EXPAND, (AttachOptions) 0);
        device_packer.attach (input_device_combo, 1, 2, row, row+1, FILL|EXPAND, (AttachOptions) 0);
        ++row;
-       label = manage (new Label (_("Output device:")));
-       label->set_alignment (0, 0.5);
+       label = manage (left_aligned_label (_("Output device:")));
        device_packer.attach (*label, 0, 1, row, row+1, FILL|EXPAND, (AttachOptions) 0);
        device_packer.attach (output_device_combo, 1, 2, row, row+1, FILL|EXPAND, (AttachOptions) 0);
        ++row;
 #endif
-       label = manage (new Label (_("Hardware input latency:")));
-       label->set_alignment (0, 0.5);
+       label = manage (left_aligned_label (_("Hardware input latency:")));
        device_packer.attach (*label, 0, 1, row, row+1, FILL|EXPAND, (AttachOptions) 0);
        device_packer.attach (input_latency, 1, 2, row, row+1, FILL|EXPAND, (AttachOptions) 0);
-       label = manage (new Label (_("samples")));
-       label->set_alignment (0, 0.5);
+       label = manage (left_aligned_label (_("samples")));
        device_packer.attach (*label, 2, 3, row, row+1, FILL|EXPAND, (AttachOptions) 0);
        ++row;
-       label = manage (new Label (_("Hardware output latency:")));
-       label->set_alignment (0, 0.5);
+       label = manage (left_aligned_label (_("Hardware output latency:")));
        device_packer.attach (*label, 0, 1, row, row+1, FILL|EXPAND, (AttachOptions) 0);
        device_packer.attach (output_latency, 1, 2, row, row+1, FILL|EXPAND, (AttachOptions) 0);
-       label = manage (new Label (_("samples")));
-       label->set_alignment (0, 0.5);
+       label = manage (left_aligned_label (_("samples")));
        device_packer.attach (*label, 2, 3, row, row+1, FILL|EXPAND, (AttachOptions) 0);
        ++row;
 
@@ -393,6 +372,14 @@ EngineControl::EngineControl ()
 
        set_border_width (12);
        pack_start (notebook);
+
+       /* Pick up any existing audio setup configuration, if appropriate */
+
+       XMLNode* audio_setup = ARDOUR::Config->extra_xml ("AudioSetup");
+       
+       if (audio_setup) {
+               set_state (*audio_setup);
+       }
 }
 
 EngineControl::~EngineControl ()
@@ -440,8 +427,6 @@ EngineControl::build_command_line (vector<string>& cmd)
 
        if (realtime_button.get_active()) {
                cmd.push_back ("-R");
-               cmd.push_back ("-P");
-               cmd.push_back (to_string ((uint32_t) floor (priority_spinner.get_value()), std::dec));
        } else {
                cmd.push_back ("-r"); /* override jackd's default --realtime */
        }
@@ -458,6 +443,12 @@ EngineControl::build_command_line (vector<string>& cmd)
 
        cmd.push_back ("-T"); // temporary */
 
+       /* setup coremidi before the driver, otherwise jack won't start */
+
+       if (midi_driver_combo.get_active_text() == _("coremidi")) {
+               cmd.push_back ("-X coremidi");
+       }
+
        /* next the driver */
 
        cmd.push_back ("-d");
@@ -489,11 +480,11 @@ EngineControl::build_command_line (vector<string>& cmd)
        if (!using_coreaudio) {
                str = audio_mode_combo.get_active_text();
 
-               if (str == _("Playback/Recording on 1 Device")) {
+               if (str == _("Playback/recording on 1 device")) {
 
                        /* relax */
 
-               } else if (str == _("Playback/Recording on 2 Devices")) {
+               } else if (str == _("Playback/recording on 2 devices")) {
 
                        string input_device = get_device_name (driver, input_device_combo.get_active_text());
                        string output_device = get_device_name (driver, output_device_combo.get_active_text());
@@ -546,7 +537,7 @@ EngineControl::build_command_line (vector<string>& cmd)
 
        if (using_alsa) {
 
-               if (audio_mode_combo.get_active_text() != _("Playback/Recording on 2 Devices")) {
+               if (audio_mode_combo.get_active_text() != _("Playback/recording on 2 devices")) {
 
                        string device = get_device_name (driver, interface_combo.get_active_text());
                        if (device.empty()) {
@@ -610,13 +601,21 @@ EngineControl::build_command_line (vector<string>& cmd)
        }
 }
 
+bool
+EngineControl::need_setup ()
+{
+       return !engine_running();
+}
+
 bool
 EngineControl::engine_running ()
 {
         EnvironmentalProtectionAgency* global_epa = EnvironmentalProtectionAgency::get_global_epa ();
         boost::scoped_ptr<EnvironmentalProtectionAgency> current_epa;
 
-        /* revert all environment settings back to whatever they were when ardour started
+        /* revert all environment settings back to whatever they were when
+        * ardour started, because ardour's startup script may have reset
+        * something in ways that interfere with finding/starting JACK.
          */
 
         if (global_epa) {
@@ -654,12 +653,11 @@ EngineControl::setup_engine ()
                error << string_compose (_("cannot open JACK rc file %1 to store parameters"), jackdrc_path) << endmsg;
                return -1;
        }
-       cerr << "JACK COMMAND: ";
+
        for (vector<string>::iterator i = args.begin(); i != args.end(); ++i) {
-               cerr << (*i) << ' ';
                jackdrc << (*i) << ' ';
        }
-       cerr << endl;
+
        jackdrc << endl;
        jackdrc.close ();
 
@@ -668,14 +666,6 @@ EngineControl::setup_engine ()
        return 0;
 }
 
-void
-EngineControl::realtime_changed ()
-{
-#ifndef __APPLE__
-       priority_spinner.set_sensitive (realtime_button.get_active());
-#endif
-}
-
 void
 EngineControl::enumerate_devices (const string& driver)
 {
@@ -686,7 +676,7 @@ EngineControl::enumerate_devices (const string& driver)
                devices[driver] = enumerate_coreaudio_devices ();
 #endif
 
-#ifndef __APPLE__
+#if !defined(__APPLE__) && !defined(__FreeBSD__)
        } else if (driver == "ALSA") {
                devices[driver] = enumerate_alsa_devices ();
        } else if (driver == "FreeBOB") {
@@ -794,7 +784,7 @@ EngineControl::enumerate_coreaudio_devices ()
 
 
        if (devs.size() == 0) {
-               MessageDialog msg (_("\
+               MessageDialog msg (string_compose (_("\
 You do not have any audio devices capable of\n\
 simultaneous playback and recording.\n\n\
 Please use Applications -> Utilities -> Audio MIDI Setup\n\
@@ -804,8 +794,8 @@ Please send email to Apple and ask them why new Macs\n\
 have no duplex audio device.\n\n\
 Alternatively, if you really want just playback\n\
 or recording but not both, start JACK before running\n\
-Ardour and choose the relevant device then."
-                                          ),
+%1 and choose the relevant device then."
+                                                          ), PROGRAM_NAME),
                                   true, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK);
                msg.set_title (_("No suitable audio devices"));
                msg.set_position (Gtk::WIN_POS_MOUSE);
@@ -817,6 +807,8 @@ Ardour and choose the relevant device then."
        return devs;
 }
 #else
+
+#if !defined(__FreeBSD__)
 vector<string>
 EngineControl::enumerate_alsa_devices ()
 {
@@ -860,6 +852,7 @@ EngineControl::enumerate_alsa_devices ()
 
        return devs;
 }
+#endif
 
 vector<string>
 EngineControl::enumerate_ffado_devices ()
@@ -959,7 +952,7 @@ void
 EngineControl::redisplay_latency ()
 {
        uint32_t rate = get_rate();
-#ifdef __APPLE_
+#if defined(__APPLE__) || defined(__FreeBSD__)
        float periods = 2;
 #else
        float periods = periods_adjustment.get_value();
@@ -978,10 +971,10 @@ EngineControl::audio_mode_changed ()
 {
        std::string str = audio_mode_combo.get_active_text();
 
-       if (str == _("Playback/Recording on 1 Device")) {
+       if (str == _("Playback/recording on 1 device")) {
                input_device_combo.set_sensitive (false);
                output_device_combo.set_sensitive (false);
-       } else if (str == _("Playback/Recording on 2 Devices")) {
+       } else if (str == _("Playback/recording on 2 devices")) {
                input_device_combo.set_sensitive (true);
                output_device_combo.set_sensitive (true);
        } else if (str == _("Playback only")) {
@@ -1065,6 +1058,9 @@ EngineControl::find_jack_servers (vector<string>& strings)
 #endif
 
        jack_servers = scanner (path, jack_server_filter, 0, false, true);
+       if (!jack_servers) {
+               return;
+       }
 
        vector<string *>::iterator iter;
 
@@ -1089,6 +1085,7 @@ EngineControl::get_device_name (const string& driver, const string& human_readab
                   another computer system in it
                */
                MessageDialog msg (_("You need to choose an audio device first."));
+                msg.set_position (WIN_POS_MOUSE);
                msg.run ();
                return string();
        }
@@ -1121,10 +1118,6 @@ EngineControl::get_state ()
        child->add_property ("val", to_string (periods_adjustment.get_value(), std::dec));
        root->add_child_nocopy (*child);
 
-       child = new XMLNode ("priority");
-       child->add_property ("val", to_string (priority_adjustment.get_value(), std::dec));
-       root->add_child_nocopy (*child);
-
        child = new XMLNode ("ports");
        child->add_property ("val", to_string (ports_adjustment.get_value(), std::dec));
        root->add_child_nocopy (*child);
@@ -1271,9 +1264,6 @@ EngineControl::set_state (const XMLNode& root)
                if (child->name() == "periods") {
                        val = atoi (strval);
                        periods_adjustment.set_value(val);
-               } else if (child->name() == "priority") {
-                       val = atoi (strval);
-                       priority_adjustment.set_value(val);
                } else if (child->name() == "ports") {
                        val = atoi (strval);
                        ports_adjustment.set_value(val);