Don't hide Audio Setup dialog when clicking 'OK' if engine fails to start
[ardour.git] / gtk2_ardour / engine_dialog.cc
index cd84e58ae357963de9b487f60eb6e202b1f92731..8799d6aa8ff2f411a68c9936d73b1efd35e70276 100644 (file)
@@ -86,7 +86,8 @@ EngineControl::EngineControl ()
        , ports_spinner (ports_adjustment)
        , control_app_button (_("Device Control Panel"))
        , midi_devices_button (_("Midi Device Setup"))
-       , stop_engine_button (_("Stop (Reconfigure)"))
+       , start_stop_button (_("Stop"))
+       , update_devices_button (_("Refresh Devices"))
        , lm_measure_label (_("Measure"))
        , lm_use_button (_("Use results"))
        , lm_back_button (_("Back to settings ... (ignore results)"))
@@ -99,6 +100,7 @@ EngineControl::EngineControl ()
        , _desired_sample_rate (0)
        , started_at_least_once (false)
        , queue_device_changed (false)
+       , _have_control (true)
        , block_signals(0)
 {
        using namespace Notebook_Helpers;
@@ -269,13 +271,17 @@ EngineControl::EngineControl ()
        control_app_button.set_can_focus(true);
        manage_control_app_sensitivity ();
 
-       stop_engine_button.signal_clicked.connect (mem_fun (*this, &EngineControl::stop_engine_button_clicked));
-       stop_engine_button.set_sensitive (false);
-       stop_engine_button.set_name ("generic button");
-       stop_engine_button.set_can_focus(true);
+       start_stop_button.signal_clicked.connect (mem_fun (*this, &EngineControl::start_stop_button_clicked));
+       start_stop_button.set_sensitive (false);
+       start_stop_button.set_name ("generic button");
+       start_stop_button.set_can_focus(true);
+
+       update_devices_button.signal_clicked.connect (mem_fun (*this, &EngineControl::update_devices_button_clicked));
+       update_devices_button.set_sensitive (false);
+       update_devices_button.set_name ("generic button");
+       update_devices_button.set_can_focus(true);
 
        cancel_button = add_button (Gtk::Stock::CLOSE, Gtk::RESPONSE_CANCEL);
-       apply_button = add_button (Gtk::Stock::APPLY, Gtk::RESPONSE_APPLY);
        ok_button = add_button (Gtk::Stock::OK, Gtk::RESPONSE_OK);
 
        /* Pick up any existing audio setup configuration, if appropriate */
@@ -408,14 +414,13 @@ EngineControl::on_response (int response_id)
        ArdourDialog::on_response (response_id);
 
        switch (response_id) {
-               case RESPONSE_APPLY:
-                       push_state_to_backend (true);
-                       break;
                case RESPONSE_OK:
+                       if (push_state_to_backend (true) != 0) {
+                               return;
+                       } else {
+                               hide ();
+                       }
 #ifdef PLATFORM_WINDOWS
-                       // For some reason we don't understand, 'hide()'
-                       // needs to get called first in Windows
-                       hide ();
 
                        // But if there's no session open, this can produce
                        // a long gap when nothing appears to be happening.
@@ -427,13 +432,8 @@ EngineControl::on_response (int response_id)
                                        }
                                }
                        }
-                       push_state_to_backend (true);
-                       break;
-#else
-                       push_state_to_backend (true);
-                       hide ();
-                       break;
 #endif
+                       break;
                case RESPONSE_DELETE_EVENT:
                        {
                                GdkEventButton ev;
@@ -469,7 +469,8 @@ EngineControl::build_notebook ()
        basic_packer.attach (engine_status, 2, 3, 0, 1, xopt, (AttachOptions) 0);
        engine_status.show();
 
-       basic_packer.attach (stop_engine_button, 3, 4, 0, 1, xopt, xopt);
+       basic_packer.attach (start_stop_button, 3, 4, 0, 1, xopt, xopt);
+       basic_packer.attach (update_devices_button, 3, 4, 1, 2, xopt, xopt);
 
        lm_button_audio.signal_clicked.connect (sigc::mem_fun (*this, &EngineControl::calibrate_audio_latency));
        lm_button_audio.set_name ("generic button");
@@ -607,7 +608,13 @@ EngineControl::build_full_control_notebook ()
        label = manage (left_aligned_label (_("MIDI System:")));
        basic_packer.attach (*label, 0, 1, row, row + 1, xopt, (AttachOptions) 0);
        basic_packer.attach (midi_option_combo, 1, 2, row, row + 1, SHRINK, (AttachOptions) 0);
+#if ! defined __APPLE__  && ! defined PLATFORM_WINDOWS // => linux, YAY
+       /* Currently the only backend with dedicated Midi setup is ALSA.
+        * lot of people complain that this is greyed out
+        * "I can't use MIDI, the setup is greyed out"
+        */
        basic_packer.attach (midi_devices_button, 3, 4, row, row+1, xopt, xopt);
+#endif
        row++;
 }
 
@@ -732,8 +739,7 @@ EngineControl::update_sensitivity ()
        boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
        if (!backend) {
                ok_button->set_sensitive (false);
-               apply_button->set_sensitive (false);
-               stop_engine_button.set_sensitive (false);
+               start_stop_button.set_sensitive (false);
                return;
        }
 
@@ -796,26 +802,48 @@ EngineControl::update_sensitivity ()
                valid = false;
        }
 
+       if (_have_control) {
+               start_stop_button.set_sensitive(true);
+               start_stop_button.show();
+               if (ARDOUR::AudioEngine::instance()->running()) {
+                       start_stop_button.set_text("Stop");
+                       update_devices_button.set_sensitive(false);
+               } else {
+                       if (backend->can_request_update_devices()) {
+                               update_devices_button.show();
+                       } else {
+                               update_devices_button.hide();
+                       }
+                       start_stop_button.set_text("Start");
+                       update_devices_button.set_sensitive(true);
+               }
+       } else {
+               update_devices_button.set_sensitive(false);
+               update_devices_button.hide();
+               start_stop_button.set_sensitive(false);
+               start_stop_button.hide();
+       }
+
        if (ARDOUR::AudioEngine::instance()->running() && _have_control) {
                input_device_combo.set_sensitive (false);
                output_device_combo.set_sensitive (false);
                device_combo.set_sensitive (false);
-               stop_engine_button.set_sensitive (true);
-               stop_engine_button.show ();
+               driver_combo.set_sensitive (false);
        } else {
                input_device_combo.set_sensitive (true);
                output_device_combo.set_sensitive (true);
                device_combo.set_sensitive (true);
-               stop_engine_button.set_sensitive (false);
-               stop_engine_button.hide ();
+               if (backend->requires_driver_selection() && get_popdown_string_count(driver_combo) > 0) {
+                       driver_combo.set_sensitive (true);
+               } else {
+                       driver_combo.set_sensitive (false);
+               }
        }
 
        if (valid || !_have_control) {
                ok_button->set_sensitive (true);
-               apply_button->set_sensitive (true);
        } else {
                ok_button->set_sensitive (false);
-               apply_button->set_sensitive (false);
        }
 }
 
@@ -920,7 +948,7 @@ EngineControl::backend_changed ()
        string backend_name = backend_combo.get_active_text();
        boost::shared_ptr<ARDOUR::AudioBackend> backend;
 
-       if (!(backend = ARDOUR::AudioEngine::instance()->set_backend (backend_name, "ardour", ""))) {
+       if (!(backend = ARDOUR::AudioEngine::instance()->set_backend (backend_name, downcase (std::string(PROGRAM_NAME)), ""))) {
                /* eh? setting the backend failed... how ? */
                /* A: stale config contains a backend that does not exist in current build */
                return;
@@ -936,12 +964,9 @@ EngineControl::backend_changed ()
 
        if (backend->requires_driver_selection()) {
                if (set_driver_popdown_strings ()) {
-                       driver_combo.set_sensitive (true);
                        driver_changed ();
                }
-
        } else {
-               driver_combo.set_sensitive (false);
                /* this will change the device text which will cause a call to
                 * device changed which will set up parameters
                 */
@@ -1035,6 +1060,46 @@ EngineControl::set_driver_popdown_strings ()
        return true;
 }
 
+std::string
+EngineControl::get_default_device(const string& current_device_name,
+                                  const vector<string>& available_devices)
+{
+       // If the current device is available, use it as default
+       if (std::find (available_devices.begin (),
+                      available_devices.end (),
+                      current_device_name) != available_devices.end ()) {
+
+               return current_device_name;
+       }
+
+       using namespace ARDOUR;
+
+       string default_device_name =
+           AudioBackend::get_standard_device_name(AudioBackend::DeviceDefault);
+
+       vector<string>::const_iterator i;
+
+       // If there is a "Default" device available, use it
+       for (i = available_devices.begin(); i != available_devices.end(); ++i) {
+               if (*i == default_device_name) {
+                       return *i;
+               }
+       }
+
+       string none_device_name =
+           AudioBackend::get_standard_device_name(AudioBackend::DeviceNone);
+
+       // Use the first device that isn't "None"
+       for (i = available_devices.begin(); i != available_devices.end(); ++i) {
+               if (*i != none_device_name) {
+                       return *i;
+               }
+       }
+
+       // Use "None" if there are no other available
+       return available_devices.front();
+}
+
 // @return true if there are devices available
 bool
 EngineControl::set_device_popdown_strings ()
@@ -1062,23 +1127,15 @@ EngineControl::set_device_popdown_strings ()
                return false;
        }
 
-       string current_device = backend->device_name ();
-
-       // Make sure that backend->device_name () is a valid
-       // device, the backend may not return a valid device if it hasn't
-       // been set yet.
-       if (std::find (available_devices.begin (),
-                      available_devices.end (),
-                      current_device) == available_devices.end ()) {
+       set_popdown_strings (device_combo, available_devices);
 
-               current_device = available_devices.front ();
-       }
+       std::string default_device =
+           get_default_device(backend->device_name(), available_devices);
 
-       set_popdown_strings (device_combo, available_devices);
        DEBUG_ECONTROL (
-           string_compose ("set device_combo active text: %1", current_device));
+           string_compose ("set device_combo active text: %1", default_device));
 
-       device_combo.set_active_text (current_device);
+       device_combo.set_active_text(default_device);
        return true;
 }
 
@@ -1100,23 +1157,14 @@ EngineControl::set_input_device_popdown_strings ()
                return false;
        }
 
-       string current_device = backend->input_device_name ();
-
-       // Make sure that backend->input_device_name () is a valid
-       // device, the backend may not return a valid device if it hasn't
-       // been set yet.
-       if (std::find (available_devices.begin (),
-                      available_devices.end (),
-                      current_device) == available_devices.end ()) {
-
-               current_device = available_devices.front ();
-       }
-
        set_popdown_strings (input_device_combo, available_devices);
 
+       std::string default_device =
+           get_default_device(backend->input_device_name(), available_devices);
+
        DEBUG_ECONTROL (
-           string_compose ("set input_device_combo active text: %1", current_device));
-       input_device_combo.set_active_text (current_device);
+           string_compose ("set input_device_combo active text: %1", default_device));
+       input_device_combo.set_active_text(default_device);
        return true;
 }
 
@@ -1138,23 +1186,14 @@ EngineControl::set_output_device_popdown_strings ()
                return false;
        }
 
-       string current_device = backend->output_device_name ();
-
-       // Make sure that backend->output_device_name () is a valid
-       // device, the backend may not return a valid device if it hasn't
-       // been set yet.
-       if (std::find (available_devices.begin (),
-                      available_devices.end (),
-                      current_device) == available_devices.end ()) {
-
-               current_device = available_devices.front ();
-       }
-
        set_popdown_strings (output_device_combo, available_devices);
 
+       std::string default_device =
+           get_default_device(backend->output_device_name(), available_devices);
+
        DEBUG_ECONTROL (
-           string_compose ("set output_device_combo active text: %1", current_device));
-       output_device_combo.set_active_text (current_device);
+           string_compose ("set output_device_combo active text: %1", default_device));
+       output_device_combo.set_active_text(default_device);
        return true;
 }
 
@@ -1210,7 +1249,7 @@ EngineControl::get_sample_rates_for_all_devices ()
        vector<float> all_rates;
 
        if (backend->use_separate_input_and_output_devices ()) {
-               all_rates = backend->available_sample_rates (get_input_device_name (), get_output_device_name ());
+               all_rates = backend->available_sample_rates2 (get_input_device_name (), get_output_device_name ());
        } else {
                all_rates = backend->available_sample_rates (get_device_name ());
        }
@@ -1282,7 +1321,7 @@ EngineControl::get_buffer_sizes_for_all_devices ()
        vector<uint32_t> all_sizes;
 
        if (backend->use_separate_input_and_output_devices ()) {
-               all_sizes = backend->available_buffer_sizes (get_input_device_name (), get_output_device_name ());
+               all_sizes = backend->available_buffer_sizes2 (get_input_device_name (), get_output_device_name ());
        } else {
                all_sizes = backend->available_buffer_sizes (get_device_name ());
        }
@@ -1325,23 +1364,34 @@ EngineControl::set_buffersize_popdown_strings ()
                s.push_back (bufsize_as_string (*x));
        }
 
+       uint32_t previous_size = 0;
+       if (!buffer_size_combo.get_active_text().empty()) {
+               previous_size = get_buffer_size ();
+       }
+
        set_popdown_strings (buffer_size_combo, s);
 
        if (!s.empty()) {
-               buffer_size_combo.set_active_text (s.front());
 
-               uint32_t period = backend->buffer_size();
-               if (0 == period && backend->use_separate_input_and_output_devices ()) {
-                       period = backend->default_buffer_size (get_input_device_name ());
-               }
-               if (0 == period && backend->use_separate_input_and_output_devices ()) {
-                       period = backend->default_buffer_size (get_output_device_name ());
-               }
-               if (0 == period && !backend->use_separate_input_and_output_devices ()) {
-                       period = backend->default_buffer_size (get_device_name ());
-               }
+               if (std::find(bs.begin(), bs.end(), previous_size) != bs.end()) {
+                       buffer_size_combo.set_active_text(bufsize_as_string(previous_size));
+               } else {
+
+                       buffer_size_combo.set_active_text(s.front());
 
-               set_active_text_if_present (buffer_size_combo, bufsize_as_string (period));
+                       uint32_t period = backend->buffer_size();
+                       if (0 == period && backend->use_separate_input_and_output_devices()) {
+                               period = backend->default_buffer_size(get_input_device_name());
+                       }
+                       if (0 == period && backend->use_separate_input_and_output_devices()) {
+                               period = backend->default_buffer_size(get_output_device_name());
+                       }
+                       if (0 == period && !backend->use_separate_input_and_output_devices()) {
+                               period = backend->default_buffer_size(get_device_name());
+                       }
+
+                       set_active_text_if_present(buffer_size_combo, bufsize_as_string(period));
+               }
                show_buffer_duration ();
        }
        update_sensitivity ();
@@ -1658,6 +1708,7 @@ EngineControl::maybe_display_saved_state ()
        State state = get_saved_state_for_currently_displayed_backend_and_device ();
 
        if (state) {
+               DEBUG_ECONTROL ("Restoring saved state");
                PBD::Unwinder<uint32_t> protect_ignore_changes (ignore_changes, ignore_changes + 1);
 
                if (!_desired_sample_rate) {
@@ -1675,6 +1726,8 @@ EngineControl::maybe_display_saved_state ()
                        midi_option_combo.set_active_text (state->midi_option);
                        _midi_devices = state->midi_devices;
                }
+       } else {
+               DEBUG_ECONTROL ("Unable to find saved state for backend and devices");
        }
 }
 
@@ -1922,7 +1975,7 @@ EngineControl::set_current_state (const State& state)
        boost::shared_ptr<ARDOUR::AudioBackend> backend;
 
        if (!(backend = ARDOUR::AudioEngine::instance ()->set_backend (
-                 state->backend, "ardour", ""))) {
+                 state->backend, downcase (std::string(PROGRAM_NAME)), ""))) {
                DEBUG_ECONTROL (string_compose ("Unable to set backend to %1", state->backend));
                // this shouldn't happen as the invalid backend names should have been
                // removed from the list of states.
@@ -2396,7 +2449,7 @@ EngineControl::get_backend () const
 string
 EngineControl::get_driver () const
 {
-       if (driver_combo.get_sensitive() && driver_combo.get_parent()) {
+       if (driver_combo.get_parent()) {
                return driver_combo.get_active_text ();
        } else {
                return "";
@@ -2434,9 +2487,33 @@ EngineControl::control_app_button_clicked ()
 }
 
 void
-EngineControl::stop_engine_button_clicked ()
+EngineControl::start_stop_button_clicked ()
 {
-       ARDOUR::AudioEngine::instance()->stop ();
+       boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
+
+       if (!backend) {
+               return;
+       }
+
+       if (ARDOUR::AudioEngine::instance()->running()) {
+               ARDOUR::AudioEngine::instance()->stop ();
+       } else {
+               push_state_to_backend (true);
+       }
+}
+
+void
+EngineControl::update_devices_button_clicked ()
+{
+       boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
+
+       if (!backend) {
+               return;
+       }
+
+       if (backend->update_devices()) {
+               device_list_changed ();
+       }
 }
 
 void
@@ -2474,7 +2551,6 @@ EngineControl::on_switch_page (GtkNotebookPage*, guint page_num)
        } else {
                cancel_button->set_sensitive (false);
                ok_button->set_sensitive (false);
-               apply_button->set_sensitive (false);
        }
 
        if (page_num == midi_tab) {
@@ -2756,7 +2832,11 @@ EngineControl::engine_running ()
        connect_disconnect_button.show();
 
        started_at_least_once = true;
-       engine_status.set_markup(string_compose ("<span foreground=\"green\">%1</span>", _("Running")));
+       if (_have_control) {
+               engine_status.set_markup(string_compose ("<span foreground=\"green\">%1</span>", _("Running")));
+       } else {
+               engine_status.set_markup(string_compose ("<span foreground=\"green\">%1</span>", _("Connected")));
+       }
        update_sensitivity();
 }
 
@@ -2769,7 +2849,12 @@ EngineControl::engine_stopped ()
        connect_disconnect_button.set_label (string_compose (_("Connect to %1"), backend->name()));
        connect_disconnect_button.show();
 
-       engine_status.set_markup(X_(""));
+       if (_have_control) {
+               engine_status.set_markup(string_compose ("<span foreground=\"red\">%1</span>", _("Stopped")));
+       } else {
+               engine_status.set_markup(X_(""));
+       }
+
        update_sensitivity();
 }