set up MIDI port treeview columns *just once*
[ardour.git] / gtk2_ardour / engine_dialog.cc
index 6abae66dda264a20622e931ff4f8c83afc4588fd..145b6812ec935c98021e2ffc0733ef018f4cc2c1 100644 (file)
@@ -53,7 +53,7 @@
 #include "engine_dialog.h"
 #include "gui_thread.h"
 #include "utils.h"
-#include "i18n.h"
+#include "pbd/i18n.h"
 
 using namespace std;
 using namespace Gtk;
@@ -87,6 +87,7 @@ EngineControl::EngineControl ()
        , midi_devices_button (_("Midi Device Setup"))
        , start_stop_button (_("Stop"))
        , update_devices_button (_("Refresh Devices"))
+       , use_buffered_io_button (_("Use Buffered I/O"), ArdourButton::led_default_elements)
        , lm_measure_label (_("Measure"))
        , lm_use_button (_("Use results"))
        , lm_back_button (_("Back to settings ... (ignore results)"))
@@ -238,6 +239,21 @@ EngineControl::EngineControl ()
 
        midi_back_button.signal_clicked().connect (sigc::bind (sigc::mem_fun (notebook, &Gtk::Notebook::set_current_page), 0));
 
+       pretty_name_column = midi_input_view.append_column (_("Pretty Name"), midi_port_columns.pretty_name) - 1;
+       use_column = midi_input_view.append_column (_("Use"), midi_port_columns.in_use) - 1;
+       music_column = midi_input_view.append_column (_("Use for Music"), midi_port_columns.music_data) - 1;
+       control_column = midi_input_view.append_column (_("Use for Control"), midi_port_columns.control_data) - 1;
+       selection_column = midi_input_view.append_column (_("Follow Selection"), midi_port_columns.control_data) - 1;
+
+       midi_output_view.append_column (_("Pretty Name"), midi_port_columns.pretty_name);
+       midi_output_view.append_column (_("Use"), midi_port_columns.in_use);
+       midi_output_view.append_column (_("Use for Music"), midi_port_columns.music_data);
+       midi_output_view.append_column (_("Use for Control"), midi_port_columns.control_data);
+       midi_output_view.append_column (_("Follow Selection"), midi_port_columns.control_data);
+
+       midi_input_view.get_selection()->set_mode (SELECTION_NONE);
+       midi_output_view.get_selection()->set_mode (SELECTION_NONE);
+
        /* pack it all up */
 
        notebook.pages().push_back (TabElem (basic_vbox, _("Audio")));
@@ -245,7 +261,7 @@ EngineControl::EngineControl ()
        notebook.pages().push_back (TabElem (midi_vbox, _("MIDI")));
        notebook.set_border_width (12);
 
-       notebook.set_show_tabs (false);
+       //notebook.set_show_tabs (false);
        notebook.show_all ();
 
        notebook.set_name ("SettingsNotebook");
@@ -263,7 +279,6 @@ EngineControl::EngineControl ()
        output_channels.signal_output().connect (sigc::bind (sigc::ptr_fun (&EngineControl::print_channel_count), &output_channels));
 
        midi_devices_button.signal_clicked.connect (mem_fun (*this, &EngineControl::configure_midi_devices));
-       midi_devices_button.set_sensitive (false);
        midi_devices_button.set_name ("generic button");
        midi_devices_button.set_can_focus(true);
 
@@ -282,6 +297,11 @@ EngineControl::EngineControl ()
        update_devices_button.set_name ("generic button");
        update_devices_button.set_can_focus(true);
 
+       use_buffered_io_button.signal_clicked.connect (mem_fun (*this, &EngineControl::use_buffered_io_button_clicked));
+       use_buffered_io_button.set_sensitive (false);
+       use_buffered_io_button.set_name ("generic button");
+       use_buffered_io_button.set_can_focus(true);
+
        cancel_button = add_button (Gtk::Stock::CLOSE, Gtk::RESPONSE_CANCEL);
        ok_button = add_button (Gtk::Stock::OK, Gtk::RESPONSE_OK);
 
@@ -302,13 +322,18 @@ EngineControl::EngineControl ()
                set_default_state ();
        }
 
+       update_sensitivity ();
        connect_changed_signals ();
 
        notebook.signal_switch_page().connect (sigc::mem_fun (*this, &EngineControl::on_switch_page));
 
        connect_disconnect_button.signal_clicked().connect (sigc::mem_fun (*this, &EngineControl::connect_disconnect_click));
-       connect_disconnect_button.set_no_show_all();
 
+       connect_disconnect_button.set_no_show_all();
+       use_buffered_io_button.set_no_show_all();
+       update_devices_button.set_no_show_all();
+       start_stop_button.set_no_show_all();
+       midi_devices_button.set_no_show_all();
 }
 
 void
@@ -413,6 +438,18 @@ EngineControl::on_show ()
        ok_button->grab_focus();
 }
 
+bool
+EngineControl::try_autostart ()
+{
+       if (!start_stop_button.get_sensitive()) {
+               return false;
+       }
+       if (ARDOUR::AudioEngine::instance()->running()) {
+               return true;
+       }
+       return start_engine ();
+}
+
 bool
 EngineControl::start_engine ()
 {
@@ -504,6 +541,7 @@ EngineControl::build_notebook ()
 
        basic_packer.attach (start_stop_button, 3, 4, 0, 1, xopt, xopt);
        basic_packer.attach (update_devices_button, 3, 4, 1, 2, xopt, xopt);
+       basic_packer.attach (use_buffered_io_button, 3, 4, 2, 3, xopt, xopt);
 
        lm_button_audio.signal_clicked.connect (sigc::mem_fun (*this, &EngineControl::calibrate_audio_latency));
        lm_button_audio.set_name ("generic button");
@@ -650,13 +688,7 @@ 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++;
 }
 
@@ -760,22 +792,146 @@ EngineControl::enable_latency_tab ()
 void
 EngineControl::setup_midi_tab_for_backend ()
 {
-       string backend = backend_combo.get_active_text ();
-
        Gtkmm2ext::container_clear (midi_vbox);
 
        midi_vbox.set_border_width (12);
        midi_device_table.set_border_width (12);
 
-       if (backend == "JACK") {
-               setup_midi_tab_for_jack ();
-       }
-
        midi_vbox.pack_start (midi_device_table, true, true);
+       midi_vbox.pack_start (midi_input_view);
+       midi_vbox.pack_start (midi_output_view);
        midi_vbox.pack_start (midi_back_button, false, false);
+
        midi_vbox.show_all ();
 }
 
+void
+EngineControl::refill_midi_ports (bool for_input)
+{
+       using namespace ARDOUR;
+
+       std::vector<string> ports;
+
+       AudioEngine::instance()->get_ports (string(), DataType::MIDI, for_input ? IsInput : IsOutput, ports);
+
+       Glib::RefPtr<ListStore> model = Gtk::ListStore::create (midi_port_columns);
+
+       for (vector<string>::const_iterator s = ports.begin(); s != ports.end(); ++s) {
+
+               if (AudioEngine::instance()->port_is_mine (*s)) {
+                       continue;
+               }
+
+               TreeModel::Row row = *(model->append());
+
+               string pretty = AudioEngine::instance()->get_pretty_name_by_name (*s);
+               row[midi_port_columns.name] = *s;
+               row[midi_port_columns.pretty_name] = (pretty.empty() ? *s : pretty);
+               row[midi_port_columns.in_use] = true;
+               row[midi_port_columns.music_data] = true;
+               row[midi_port_columns.control_data] = true;
+       }
+
+       Gtk::TreeView& view (for_input ? midi_input_view : midi_output_view);
+
+       view.set_model (model);
+
+       CellRendererText* pretty_name_cell = dynamic_cast<CellRendererText*> (view.get_column_cell_renderer (pretty_name_column));
+       pretty_name_cell->property_editable() = true;
+       pretty_name_cell->signal_edited().connect (sigc::bind (sigc::mem_fun (*this, &EngineControl::pretty_name_edit), &view));
+
+       CellRendererToggle* toggle_cell;
+
+       toggle_cell = dynamic_cast<CellRendererToggle*> (view.get_column_cell_renderer (use_column));
+       toggle_cell->signal_toggled().connect (sigc::bind (sigc::mem_fun (*this, &EngineControl::midi_use_column_toggled), &view));
+
+       toggle_cell = dynamic_cast<CellRendererToggle*> (view.get_column_cell_renderer (music_column));
+       toggle_cell->signal_toggled().connect (sigc::bind (sigc::mem_fun (*this, &EngineControl::midi_music_column_toggled), &view));
+
+       toggle_cell = dynamic_cast<CellRendererToggle*> (view.get_column_cell_renderer (control_column));
+       toggle_cell->signal_toggled().connect (sigc::bind (sigc::mem_fun (*this, &EngineControl::midi_control_column_toggled), &view));
+
+       toggle_cell = dynamic_cast<CellRendererToggle*> (view.get_column_cell_renderer (selection_column));
+       toggle_cell->signal_toggled().connect (sigc::bind (sigc::mem_fun (*this, &EngineControl::midi_selection_column_toggled), &view));
+}
+
+void
+EngineControl::midi_use_column_toggled (string const & path, TreeView* view)
+{
+       TreeIter iter = view->get_model()->get_iter (path);
+
+       if (!iter) {
+               return;
+       }
+
+       bool value ((*iter)[midi_port_columns.in_use]);
+       (*iter)[midi_port_columns.in_use] = !value;
+
+}
+
+void
+EngineControl::midi_music_column_toggled (string const & path, TreeView* view)
+{
+       TreeIter iter = view->get_model()->get_iter (path);
+
+       if (!iter) {
+               return;
+       }
+
+       bool value ((*iter)[midi_port_columns.music_data]);
+       (*iter)[midi_port_columns.music_data] = !value;
+}
+
+void
+EngineControl::midi_control_column_toggled (string const & path, TreeView* view)
+{
+       TreeIter iter = view->get_model()->get_iter (path);
+
+       if (!iter) {
+               return;
+       }
+
+       bool value ((*iter)[midi_port_columns.control_data]);
+       (*iter)[midi_port_columns.control_data] = !value;
+
+       if (!value) {
+               // ARDOUR::AudioEngine::instance()->remove_port_purpose (PortFlags (ControlData));
+       } else {
+               // ARDOUR::AudioEngine::instance()->add_port_purpose (PortFlags (ControlData));
+       }
+}
+
+void
+EngineControl::midi_selection_column_toggled (string const & path, TreeView* view)
+{
+       TreeIter iter = view->get_model()->get_iter (path);
+
+       if (!iter) {
+               return;
+       }
+       bool value ((*iter)[midi_port_columns.selection]);
+       (*iter)[midi_port_columns.selection] = !value;
+}
+
+void
+EngineControl::pretty_name_edit (std::string const & path, string const & new_text, Gtk::TreeView* view)
+{
+       TreeIter iter = view->get_model()->get_iter (path);
+
+       if (!iter) {
+               return;
+       }
+
+       boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
+       if (backend) {
+               ARDOUR::PortEngine::PortHandle ph = backend->get_port_by_name ((*iter)[midi_port_columns.name]);
+               if (ph) {
+                       backend->set_port_property (ph, "http://jackaudio.org/metadata/pretty-name", new_text, "");
+                       (*iter)[midi_port_columns.pretty_name] = new_text;
+               }
+       }
+}
+
 void
 EngineControl::update_sensitivity ()
 {
@@ -835,11 +991,17 @@ EngineControl::update_sensitivity ()
        }
 
        if (get_popdown_string_count (sample_rate_combo) > 0) {
+               bool allow_to_set_rate = false;
                if (!ARDOUR::AudioEngine::instance()->running()) {
-                       sample_rate_combo.set_sensitive (true);
-               } else {
-                       sample_rate_combo.set_sensitive (false);
+                       if (!ARDOUR_UI::instance()->session_loaded) {
+                               // engine is not running, no session loaded -> anything goes.
+                               allow_to_set_rate = true;
+                       } else if (_desired_sample_rate > 0 && get_rate () != _desired_sample_rate) {
+                               // only allow to change if the current setting is not the native session rate.
+                               allow_to_set_rate = true;
+                       }
                }
+               sample_rate_combo.set_sensitive (allow_to_set_rate);
        } else {
                sample_rate_combo.set_sensitive (false);
                valid = false;
@@ -861,18 +1023,27 @@ EngineControl::update_sensitivity ()
                if (ARDOUR::AudioEngine::instance()->running()) {
                        start_stop_button.set_text("Stop");
                        update_devices_button.set_sensitive(false);
+                       use_buffered_io_button.set_sensitive(false);
                } else {
                        if (backend->can_request_update_devices()) {
                                update_devices_button.show();
                        } else {
                                update_devices_button.hide();
                        }
+                       if (backend->can_use_buffered_io()) {
+                               use_buffered_io_button.show();
+                       } else {
+                               use_buffered_io_button.hide();
+                       }
                        start_stop_button.set_text("Start");
                        update_devices_button.set_sensitive(true);
+                       use_buffered_io_button.set_sensitive(true);
                }
        } else {
                update_devices_button.set_sensitive(false);
                update_devices_button.hide();
+               use_buffered_io_button.set_sensitive(false);
+               use_buffered_io_button.hide();
                start_stop_button.set_sensitive(false);
                start_stop_button.hide();
        }
@@ -900,11 +1071,6 @@ EngineControl::update_sensitivity ()
        }
 }
 
-void
-EngineControl::setup_midi_tab_for_jack ()
-{
-}
-
 void
 EngineControl::midi_latency_adjustment_changed (Gtk::Adjustment *a, MidiDeviceSettings device, bool for_input) {
        if (for_input) {
@@ -1366,7 +1532,10 @@ EngineControl::set_samplerate_popdown_strings ()
        set_popdown_strings (sample_rate_combo, s);
 
        if (!s.empty()) {
-               if (desired.empty ()) {
+               if (ARDOUR::AudioEngine::instance()->running()) {
+                       sample_rate_combo.set_active_text (rate_as_string (backend->sample_rate()));
+               }
+               else if (desired.empty ()) {
                        float new_active_sr = backend->default_sample_rate ();
 
                        if (std::find (sr.begin (), sr.end (), new_active_sr) == sr.end ()) {
@@ -1485,7 +1654,7 @@ EngineControl::set_nperiods_popdown_strings ()
        set_popdown_strings (nperiods_combo, s);
 
        if (!s.empty()) {
-               set_active_text_if_present (nperiods_combo, nperiods_as_string (backend->period_size())); // XXX 
+               set_active_text_if_present (nperiods_combo, nperiods_as_string (backend->period_size())); // XXX
        }
 
        update_sensitivity ();
@@ -1570,12 +1739,7 @@ EngineControl::output_device_changed ()
 string
 EngineControl::bufsize_as_string (uint32_t sz)
 {
-       /* Translators: "samples" is always plural here, so no
-          need for plural+singular forms.
-        */
-       char buf[64];
-       snprintf (buf, sizeof (buf), "%u %s", sz, P_("sample", "samples", sz));
-       return buf;
+       return string_compose (P_("%1 sample", "%1 samples", sz), sz);
 }
 
 string
@@ -1676,9 +1840,9 @@ EngineControl::midi_option_changed ()
        _midi_devices = new_devices;
 
        if (_midi_devices.empty()) {
-               midi_devices_button.set_sensitive (false);
+               midi_devices_button.hide ();
        } else {
-               midi_devices_button.set_sensitive (true);
+               midi_devices_button.show ();
        }
 }
 
@@ -1830,6 +1994,7 @@ EngineControl::store_state (State state)
        state->output_channels = get_output_channels ();
        state->midi_option = get_midi_option ();
        state->midi_devices = _midi_devices;
+       state->use_buffered_io = get_use_buffered_io ();
        state->lru = time (NULL) ;
 }
 
@@ -1859,6 +2024,8 @@ EngineControl::maybe_display_saved_state ()
                input_latency.set_value (state->input_latency);
                output_latency.set_value (state->output_latency);
 
+               use_buffered_io_button.set_active (state->use_buffered_io);
+
                if (!state->midi_option.empty()) {
                        midi_option_combo.set_active_text (state->midi_option);
                        _midi_devices = state->midi_devices;
@@ -1871,7 +2038,7 @@ EngineControl::maybe_display_saved_state ()
 XMLNode&
 EngineControl::get_state ()
 {
-       LocaleGuard lg (X_("C"));
+       LocaleGuard lg;
 
        XMLNode* root = new XMLNode ("AudioMIDISetup");
        std::string path;
@@ -1896,6 +2063,7 @@ EngineControl::get_state ()
                        node->add_property ("input-channels", (*i)->input_channels);
                        node->add_property ("output-channels", (*i)->output_channels);
                        node->add_property ("active", (*i)->active ? "yes" : "no");
+                       node->add_property ("use-buffered-io", (*i)->use_buffered_io ? "yes" : "no");
                        node->add_property ("midi-option", (*i)->midi_option);
                        node->add_property ("lru", (*i)->active ? time (NULL) : (*i)->lru);
 
@@ -1940,11 +2108,9 @@ EngineControl::set_state (const XMLNode& root)
 {
        XMLNodeList          clist, cclist;
        XMLNodeConstIterator citer, cciter;
-       XMLNode* child;
-       XMLNode* grandchild;
-       XMLProperty* prop = NULL;
-
-       fprintf (stderr, "EngineControl::set_state\n");
+       XMLNode const * child;
+       XMLNode const * grandchild;
+       XMLProperty const * prop = NULL;
 
        if (root.name() != "AudioMIDISetup") {
                return false;
@@ -2040,6 +2206,11 @@ EngineControl::set_state (const XMLNode& root)
                        }
                        state->active = string_is_affirmative (prop->value ());
 
+                       if ((prop = grandchild->property ("use-buffered-io")) == 0) {
+                               continue;
+                       }
+                       state->use_buffered_io = string_is_affirmative (prop->value ());
+
                        if ((prop = grandchild->property ("midi-option")) == 0) {
                                continue;
                        }
@@ -2207,12 +2378,15 @@ EngineControl::set_current_state (const State& state)
        device_combo.set_active_text (state->device);
        input_device_combo.set_active_text (state->input_device);
        output_device_combo.set_active_text (state->output_device);
-       sample_rate_combo.set_active_text (rate_as_string (state->sample_rate));
+       if (!_desired_sample_rate) {
+               sample_rate_combo.set_active_text (rate_as_string (state->sample_rate));
+       }
        set_active_text_if_present (buffer_size_combo, bufsize_as_string (state->buffer_size));
        set_active_text_if_present (nperiods_combo, nperiods_as_string (state->n_periods));
        input_latency.set_value (state->input_latency);
        output_latency.set_value (state->output_latency);
        midi_option_combo.set_active_text (state->midi_option);
+       use_buffered_io_button.set_active (state->use_buffered_io);
        return true;
 }
 
@@ -2239,6 +2413,7 @@ EngineControl::push_state_to_backend (bool start)
        bool change_latency = false;
        bool change_channels = false;
        bool change_midi = false;
+       bool change_buffered_io = false;
 
        uint32_t ochan = get_output_channels ();
        uint32_t ichan = get_input_channels ();
@@ -2289,6 +2464,12 @@ EngineControl::push_state_to_backend (bool start)
                                change_midi = true;
                        }
 
+                       if (backend->can_use_buffered_io()) {
+                               if (get_use_buffered_io() != backend->get_use_buffered_io()) {
+                                       change_buffered_io = true;
+                               }
+                       }
+
                        /* zero-requested channels means "all available" */
 
                        if (ichan == 0) {
@@ -2324,6 +2505,8 @@ EngineControl::push_state_to_backend (bool start)
                        change_channels = true;
                        change_latency = true;
                        change_midi = true;
+                       change_buffered_io = backend->can_use_buffered_io();
+                       change_channels = true;
                        change_nperiods = backend->can_set_period_size() && get_popdown_string_count (nperiods_combo) > 0;
                }
 
@@ -2385,7 +2568,7 @@ EngineControl::push_state_to_backend (bool start)
        if (change_driver || change_device || change_channels || change_nperiods ||
                        (change_latency && !backend->can_change_systemic_latency_when_running ()) ||
                        (change_rate && !backend->can_change_sample_rate_when_running()) ||
-                       change_midi ||
+                       change_midi || change_buffered_io ||
                        (change_bufsize && !backend->can_change_buffer_size_when_running())) {
                restart_required = true;
        } else {
@@ -2458,6 +2641,10 @@ EngineControl::push_state_to_backend (bool start)
                backend->set_midi_option (get_midi_option());
        }
 
+       if (change_buffered_io) {
+               backend->set_use_buffered_io (use_buffered_io_button.get_active());
+       }
+
        if (1 /* TODO */) {
                for (vector<MidiDeviceSettings>::const_iterator p = _midi_devices.begin(); p != _midi_devices.end(); ++p) {
                        if (_measure_midi) {
@@ -2572,6 +2759,12 @@ EngineControl::get_midi_option () const
        return midi_option_combo.get_active_text();
 }
 
+bool
+EngineControl::get_use_buffered_io () const
+{
+       return use_buffered_io_button.get_active();
+}
+
 uint32_t
 EngineControl::get_input_channels() const
 {
@@ -2682,6 +2875,20 @@ EngineControl::update_devices_button_clicked ()
        }
 }
 
+void
+EngineControl::use_buffered_io_button_clicked ()
+{
+       boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
+
+       if (!backend) {
+               return;
+       }
+
+       bool set_buffered_io = !use_buffered_io_button.get_active();
+       use_buffered_io_button.set_active (set_buffered_io);
+       backend->set_use_buffered_io (set_buffered_io);
+}
+
 void
 EngineControl::manage_control_app_sensitivity ()
 {
@@ -2704,6 +2911,10 @@ void
 EngineControl::set_desired_sample_rate (uint32_t sr)
 {
        _desired_sample_rate = sr;
+       if (ARDOUR::AudioEngine::instance ()->running ()
+                       && ARDOUR::AudioEngine::instance ()->sample_rate () != sr) {
+               stop_engine ();
+       }
        device_changed ();
 }
 
@@ -3020,6 +3231,9 @@ EngineControl::engine_running ()
                engine_status.set_markup(string_compose ("<span foreground=\"green\">%1</span>", _("Connected")));
        }
        update_sensitivity();
+
+       refill_midi_ports (true);
+       refill_midi_ports (false);
 }
 
 void