+
+ update_midi_options ();
+
+ connect_disconnect_button.hide();
+
+ midi_option_changed();
+
+ started_at_least_once = false;
+
+ if (!ignore_changes) {
+ maybe_display_saved_state ();
+ }
+}
+
+void
+EngineControl::update_midi_options ()
+{
+ boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
+ vector<string> midi_options = backend->enumerate_midi_options();
+
+ if (midi_options.size() == 1) {
+ /* only contains the "none" option */
+ midi_option_combo.set_sensitive (false);
+ } else {
+ if (_have_control) {
+ set_popdown_strings (midi_option_combo, midi_options);
+ midi_option_combo.set_active_text (midi_options.front());
+ midi_option_combo.set_sensitive (true);
+ } else {
+ midi_option_combo.set_sensitive (false);
+ }
+ }
+}
+
+bool
+EngineControl::print_channel_count (Gtk::SpinButton* sb)
+{
+ if (ARDOUR::Profile->get_mixbus()) {
+ return true;
+ }
+
+ uint32_t cnt = (uint32_t) sb->get_value();
+ if (cnt == 0) {
+ sb->set_text (_("all available channels"));
+ } else {
+ char buf[32];
+ snprintf (buf, sizeof (buf), "%d", cnt);
+ sb->set_text (buf);
+ }
+ return true;
+}
+
+bool
+EngineControl::set_driver_popdown_strings ()
+{
+ DEBUG_ECONTROL ("set_driver_popdown_strings");
+ string backend_name = backend_combo.get_active_text();
+ boost::shared_ptr<ARDOUR::AudioBackend> backend;
+
+ if (!(backend = ARDOUR::AudioEngine::instance()->set_backend (backend_name, "ardour", ""))) {
+ /* eh? setting the backend failed... how ? */
+ /* A: stale config contains a backend that does not exist in current build */
+ return false;
+ }
+
+ vector<string> drivers = backend->enumerate_drivers();
+ set_popdown_strings (driver_combo, drivers);
+ return true;
+}
+
+// @return true if there are devices available
+bool
+EngineControl::set_device_popdown_strings ()
+{
+ DEBUG_ECONTROL ("set_device_popdown_strings");
+ boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
+ vector<ARDOUR::AudioBackend::DeviceStatus> all_devices = backend->enumerate_devices ();
+
+ /* NOTE: Ardour currently does not display the "available" field of the
+ * returned devices.
+ *
+ * Doing so would require a different GUI widget than the combo
+ * box/popdown that we currently use, since it has no way to list
+ * items that are not selectable. Something more like a popup menu,
+ * which could have unselectable items, would be appropriate.
+ */
+
+ vector<string> available_devices;
+
+ for (vector<ARDOUR::AudioBackend::DeviceStatus>::const_iterator i = all_devices.begin(); i != all_devices.end(); ++i) {
+ available_devices.push_back (i->name);
+ }
+
+
+ if (!available_devices.empty()) {
+
+ {
+ string current_device, found_device;
+ current_device = device_combo.get_active_text ();
+ if (current_device == "") {
+ current_device = backend->device_name ();
+ }
+
+ // Make sure that the active text is still relevant for this
+ // device (it might only be relevant to the previous device!!)
+ for (vector<string>::const_iterator i = available_devices.begin(); i != available_devices.end(); ++i) {
+ if (*i == current_device)
+ found_device = current_device;
+ }
+ if (found_device == "")
+ // device has never been set (or was not relevant
+ // for this backend) Let's make sure it's not blank
+ current_device = available_devices.front ();
+
+ PBD::Unwinder<uint32_t> protect_ignore_changes (ignore_changes, ignore_changes + 1);
+ set_popdown_strings (device_combo, available_devices);
+ DEBUG_ECONTROL (string_compose ("set device_combo active text: %1", current_device));
+
+ device_combo.set_active_text (current_device);
+ }
+ return true;
+ }
+ return false;
+}
+
+// @return true if there are input devices available
+bool
+EngineControl::set_input_device_popdown_strings ()
+{
+ DEBUG_ECONTROL ("set_input_device_popdown_strings");
+ boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
+ vector<ARDOUR::AudioBackend::DeviceStatus> all_devices = backend->enumerate_input_devices ();
+
+ vector<string> available_devices;
+
+ for (vector<ARDOUR::AudioBackend::DeviceStatus>::const_iterator i = all_devices.begin(); i != all_devices.end(); ++i) {
+ available_devices.push_back (i->name);
+ }
+
+ if (!available_devices.empty()) {
+
+ {
+ string current_device, found_device;
+ current_device = input_device_combo.get_active_text ();
+ if (current_device == "") {
+ current_device = backend->input_device_name ();
+ }
+
+ // Make sure that the active text is still relevant for this
+ // device (it might only be relevant to the previous device!!)
+ for (vector<string>::const_iterator i = available_devices.begin(); i != available_devices.end(); ++i) {
+ if (*i == current_device)
+ found_device = current_device;
+ }
+ if (found_device == "")
+ // device has never been set (or was not relevant
+ // for this backend) Let's make sure it's not blank
+ current_device = available_devices.front ();
+
+ PBD::Unwinder<uint32_t> protect_ignore_changes (ignore_changes, ignore_changes + 1);
+ set_popdown_strings (input_device_combo, available_devices);
+
+ DEBUG_ECONTROL (string_compose ("set input_device_combo active text: %1", current_device));
+ input_device_combo.set_active_text (current_device);
+ }
+ return true;
+ }
+
+ return false;
+}
+
+// @return true if there are output devices available
+bool
+EngineControl::set_output_device_popdown_strings ()
+{
+ DEBUG_ECONTROL ("set_output_device_popdown_strings");
+ boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
+ vector<ARDOUR::AudioBackend::DeviceStatus> all_devices = backend->enumerate_output_devices ();
+
+ vector<string> available_devices;
+
+ for (vector<ARDOUR::AudioBackend::DeviceStatus>::const_iterator i = all_devices.begin(); i != all_devices.end(); ++i) {
+ available_devices.push_back (i->name);
+ }
+
+ if (!available_devices.empty()) {
+
+ {
+ string current_device, found_device;
+ current_device = output_device_combo.get_active_text ();
+ if (current_device == "") {
+ current_device = backend->output_device_name ();
+ }
+
+ // Make sure that the active text is still relevant for this
+ // device (it might only be relevant to the previous device!!)
+ for (vector<string>::const_iterator i = available_devices.begin(); i != available_devices.end(); ++i) {
+ if (*i == current_device)
+ found_device = current_device;
+ }
+ if (found_device == "")
+ // device has never been set (or was not relevant
+ // for this backend) Let's make sure it's not blank
+ current_device = available_devices.front ();
+
+ PBD::Unwinder<uint32_t> protect_ignore_changes (ignore_changes, ignore_changes + 1);
+ set_popdown_strings (output_device_combo, available_devices);
+
+ DEBUG_ECONTROL (string_compose ("set input_device_combo active text: %1", current_device));
+ output_device_combo.set_active_text (current_device);
+ }
+ return true;
+ }
+
+ return false;
+}
+
+void
+EngineControl::list_devices ()
+{
+ DEBUG_ECONTROL ("list_devices");
+ boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
+ assert (backend);
+
+ /* now fill out devices, mark sample rates, buffer sizes insensitive */
+
+ bool devices_available = false;
+
+ if (backend->use_separate_input_and_output_devices ()) {
+ bool input_devices_available = set_input_device_popdown_strings ();
+ bool output_devices_available = set_output_device_popdown_strings ();
+ devices_available = input_devices_available || output_devices_available;
+ } else {
+ devices_available = set_device_popdown_strings ();
+ }
+
+ if (devices_available) {
+ device_changed ();
+
+ input_latency.set_sensitive (true);
+ output_latency.set_sensitive (true);
+ input_channels.set_sensitive (true);
+ output_channels.set_sensitive (true);
+
+ ok_button->set_sensitive (true);
+ apply_button->set_sensitive (true);
+
+ } else {
+ device_combo.clear();
+ input_device_combo.clear();
+ output_device_combo.clear();
+ sample_rate_combo.set_sensitive (false);
+ buffer_size_combo.set_sensitive (false);
+ input_latency.set_sensitive (false);
+ output_latency.set_sensitive (false);
+ input_channels.set_sensitive (false);
+ output_channels.set_sensitive (false);
+ if (_have_control) {
+ ok_button->set_sensitive (false);
+ apply_button->set_sensitive (false);
+ } else {
+ ok_button->set_sensitive (true);
+ apply_button->set_sensitive (true);
+ if (backend->can_change_sample_rate_when_running() && sample_rate_combo.get_children().size() > 0) {
+ sample_rate_combo.set_sensitive (true);
+ }
+ if (backend->can_change_buffer_size_when_running() && buffer_size_combo.get_children().size() > 0) {
+ buffer_size_combo.set_sensitive (true);
+ }
+
+ }
+ }
+}
+
+void
+EngineControl::driver_changed ()
+{
+ DEBUG_ECONTROL ("driver_changed");
+ boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
+ assert (backend);
+
+ backend->set_driver (driver_combo.get_active_text());
+ list_devices ();
+
+ if (!ignore_changes) {
+ maybe_display_saved_state ();
+ }
+}
+
+void
+EngineControl::set_samplerate_popdown_strings (const std::string& device_name)
+{
+ DEBUG_ECONTROL ("set_samplerate_popdown_strings");
+ boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
+ string desired;
+ vector<float> sr;
+ vector<string> s;
+
+ if (_have_control) {
+ sr = backend->available_sample_rates (device_name);
+ } else {
+
+ sr.push_back (8000.0f);
+ sr.push_back (16000.0f);
+ sr.push_back (32000.0f);
+ sr.push_back (44100.0f);
+ sr.push_back (48000.0f);
+ sr.push_back (88200.0f);
+ sr.push_back (96000.0f);
+ sr.push_back (192000.0f);
+ sr.push_back (384000.0f);
+ }
+
+ for (vector<float>::const_iterator x = sr.begin(); x != sr.end(); ++x) {
+ s.push_back (rate_as_string (*x));
+ if (*x == _desired_sample_rate) {
+ desired = s.back();
+ }
+ }
+
+ if (!s.empty()) {
+ sample_rate_combo.set_sensitive (true);
+ set_popdown_strings (sample_rate_combo, s);
+
+ if (desired.empty()) {
+ sample_rate_combo.set_active_text (rate_as_string (backend->default_sample_rate()));
+ } else {
+ sample_rate_combo.set_active_text (desired);
+ }
+
+ } else {
+ sample_rate_combo.set_sensitive (false);
+ }
+}
+
+void
+EngineControl::set_buffersize_popdown_strings (const std::string& device_name)
+{
+ DEBUG_ECONTROL ("set_buffersize_popdown_strings");
+ boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
+ vector<uint32_t> bs;
+ vector<string> s;
+
+ if (_have_control) {
+ bs = backend->available_buffer_sizes (device_name);
+ } else if (backend->can_change_buffer_size_when_running()) {
+ bs.push_back (8);
+ bs.push_back (16);
+ bs.push_back (32);
+ bs.push_back (64);
+ bs.push_back (128);
+ bs.push_back (256);
+ bs.push_back (512);
+ bs.push_back (1024);
+ bs.push_back (2048);
+ bs.push_back (4096);
+ bs.push_back (8192);
+ }
+ s.clear ();
+ for (vector<uint32_t>::const_iterator x = bs.begin(); x != bs.end(); ++x) {
+ s.push_back (bufsize_as_string (*x));
+ }
+
+ if (!s.empty()) {
+ buffer_size_combo.set_sensitive (true);
+ set_popdown_strings (buffer_size_combo, s);
+ buffer_size_combo.set_active_text (s.front());
+
+ uint32_t period = backend->buffer_size();
+ if (0 == period) {
+ period = backend->default_buffer_size(device_name);
+ }
+ set_active_text_if_present (buffer_size_combo, bufsize_as_string (period));
+ show_buffer_duration ();
+ } else {
+ buffer_size_combo.set_sensitive (false);
+ }
+}
+
+void
+EngineControl::device_changed ()
+{
+ DEBUG_ECONTROL ("device_changed");
+ boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
+ assert (backend);
+
+ string device_name_in;
+ string device_name_out; // only used if backend support separate I/O devices
+
+ if (backend->use_separate_input_and_output_devices()) {
+ device_name_in = get_input_device_name ();
+ device_name_out = get_output_device_name ();
+ } else {
+ device_name_in = get_device_name ();
+ }
+
+ /* we set the backend-device to query various device related intormation.
+ * This has the side effect that backend->device_name() will match
+ * the device_name and 'change_device' will never be true.
+ * so work around this by setting...
+ */
+ if (backend->use_separate_input_and_output_devices()) {
+ if (device_name_in != backend->input_device_name() || device_name_out != backend->output_device_name ()) {
+ queue_device_changed = true;
+ }
+ } else {
+ if (device_name_in != backend->device_name()) {
+ queue_device_changed = true;
+ }
+ }
+
+ //the device name must be set FIRST so ASIO can populate buffersizes and the control panel button
+ if (backend->use_separate_input_and_output_devices()) {
+ backend->set_input_device_name (device_name_in);
+ backend->set_output_device_name (device_name_out);
+ } else {
+ backend->set_device_name(device_name_in);
+ }
+
+ {
+ /* don't allow programmatic change to combos to cause a
+ recursive call to this method.
+ */
+ PBD::Unwinder<uint32_t> protect_ignore_changes (ignore_changes, ignore_changes + 1);
+
+ /* backends that support separate devices, need to ignore
+ * the device-name - and use the devies set above
+ */
+ set_samplerate_popdown_strings (device_name_in);
+ set_buffersize_popdown_strings (device_name_in);
+ /* XXX theoretically need to set min + max channel counts here
+ */
+
+ manage_control_app_sensitivity ();
+ }
+
+ /* pick up any saved state for this device */
+
+ if (!ignore_changes) {
+ maybe_display_saved_state ();
+ }
+}
+
+void
+EngineControl::input_device_changed ()
+{
+ DEBUG_ECONTROL ("input_device_changed");
+ device_changed ();
+}
+
+void
+EngineControl::output_device_changed ()
+{
+ DEBUG_ECONTROL ("output_device_changed");
+ 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;
+}
+
+void
+EngineControl::sample_rate_changed ()
+{
+ DEBUG_ECONTROL ("sample_rate_changed");
+ /* reset the strings for buffer size to show the correct msec value
+ (reflecting the new sample rate).
+ */
+
+ show_buffer_duration ();
+
+}
+
+void
+EngineControl::buffer_size_changed ()
+{
+ DEBUG_ECONTROL ("buffer_size_changed");
+ show_buffer_duration ();
+}
+
+void
+EngineControl::show_buffer_duration ()
+{
+ DEBUG_ECONTROL ("show_buffer_duration");
+ /* buffer sizes - convert from just samples to samples + msecs for
+ * the displayed string
+ */
+
+ string bs_text = buffer_size_combo.get_active_text ();
+ uint32_t samples = atoi (bs_text); /* will ignore trailing text */
+ uint32_t rate = get_rate();
+
+ /* Developers: note the hard-coding of a double buffered model
+ in the (2 * samples) computation of latency. we always start
+ the audiobackend in this configuration.
+ */
+ /* note to jack1 developers: ardour also always starts the engine
+ * in async mode (no jack2 --sync option) which adds an extra cycle
+ * of latency with jack2 (and *3 would be correct)
+ * The value can also be wrong if jackd is started externally..
+ *
+ * At the time of writing the ALSA backend always uses double-buffering *2,
+ * The Dummy backend *1, and who knows what ASIO really does :)
+ *
+ * So just display the period size, that's also what
+ * ARDOUR_UI::update_sample_rate() does for the status bar.
+ * (the statusbar calls AudioEngine::instance()->usecs_per_cycle()
+ * but still, that's the buffer period, not [round-trip] latency)
+ */
+ char buf[32];
+ snprintf (buf, sizeof (buf), _("(%.1f ms)"), (samples / (rate/1000.0f)));
+ buffer_size_duration_label.set_text (buf);
+}
+
+void
+EngineControl::midi_option_changed ()
+{
+ DEBUG_ECONTROL ("midi_option_changed");
+ boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
+ assert (backend);
+
+ backend->set_midi_option (get_midi_option());
+
+ vector<ARDOUR::AudioBackend::DeviceStatus> midi_devices = backend->enumerate_midi_devices();
+
+ //_midi_devices.clear(); // TODO merge with state-saved settings..
+ _can_set_midi_latencies = backend->can_set_systemic_midi_latencies();
+ std::vector<MidiDeviceSettings> new_devices;
+
+ for (vector<ARDOUR::AudioBackend::DeviceStatus>::const_iterator i = midi_devices.begin(); i != midi_devices.end(); ++i) {
+ MidiDeviceSettings mds = find_midi_device (i->name);
+ if (i->available && !mds) {
+ uint32_t input_latency = 0;
+ uint32_t output_latency = 0;
+ if (_can_set_midi_latencies) {
+ input_latency = backend->systemic_midi_input_latency (i->name);
+ output_latency = backend->systemic_midi_output_latency (i->name);
+ }
+ bool enabled = backend->midi_device_enabled (i->name);
+ MidiDeviceSettings ptr (new MidiDeviceSetting (i->name, enabled, input_latency, output_latency));
+ new_devices.push_back (ptr);
+ } else if (i->available) {
+ new_devices.push_back (mds);
+ }
+ }
+ _midi_devices = new_devices;
+
+ if (_midi_devices.empty()) {
+ midi_devices_button.set_sensitive (false);
+ } else {
+ midi_devices_button.set_sensitive (true);
+ }
+}
+
+void
+EngineControl::parameter_changed ()
+{
+}
+
+EngineControl::State
+EngineControl::get_matching_state (
+ const string& backend,
+ const string& driver,
+ const string& device)
+{
+ for (StateList::iterator i = states.begin(); i != states.end(); ++i) {
+ if ((*i)->backend == backend &&
+ (!_have_control || ((*i)->driver == driver && (*i)->device == device)))
+ {
+ return (*i);
+ }
+ }
+ return State();
+}
+
+EngineControl::State
+EngineControl::get_matching_state (
+ const string& backend,
+ const string& driver,
+ const string& input_device,
+ const string& output_device)
+{
+ for (StateList::iterator i = states.begin(); i != states.end(); ++i) {
+ if ((*i)->backend == backend &&
+ (!_have_control || ((*i)->driver == driver && ((*i)->input_device == input_device) && (*i)->output_device == output_device)))
+ {
+ return (*i);
+ }
+ }
+ return State();
+}
+
+EngineControl::State
+EngineControl::get_saved_state_for_currently_displayed_backend_and_device ()
+{
+ boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
+
+ if (backend) {
+ if (backend->use_separate_input_and_output_devices ()) {
+ return get_matching_state (backend_combo.get_active_text(),
+ (backend->requires_driver_selection() ? (std::string) driver_combo.get_active_text() : string()),
+ input_device_combo.get_active_text(),
+ output_device_combo.get_active_text());
+ } else {
+ return get_matching_state (backend_combo.get_active_text(),
+ (backend->requires_driver_selection() ? (std::string) driver_combo.get_active_text() : string()),
+ device_combo.get_active_text());
+ }
+ }
+
+ return get_matching_state (backend_combo.get_active_text(),
+ string(),
+ device_combo.get_active_text());
+}
+
+bool EngineControl::equivalent_states (const EngineControl::State& state1,
+ const EngineControl::State& state2)
+{
+ if (state1->backend == state2->backend &&
+ state1->driver == state2->driver &&
+ state1->device == state2->device &&
+ state1->input_device == state2->input_device &&
+ state1->output_device == state2->output_device) {
+ return true;
+ }
+ return false;
+}
+
+EngineControl::State
+EngineControl::save_state ()
+{
+ State state;
+
+ if (!_have_control) {
+ state = get_matching_state (backend_combo.get_active_text(), string(), string());
+ if (state) {
+ return state;
+ }
+ state.reset(new StateStruct);
+ state->backend = get_backend ();
+ } else {
+ state.reset(new StateStruct);
+ store_state (state);
+ }
+
+ for (StateList::iterator i = states.begin(); i != states.end();) {
+ if (equivalent_states (*i, state)) {
+ i = states.erase(i);
+ } else {
+ ++i;
+ }
+ }
+
+ states.push_back (state);
+
+ return state;
+}
+
+void
+EngineControl::store_state (State state)
+{
+ state->backend = get_backend ();
+ state->driver = get_driver ();
+ state->device = get_device_name ();
+ state->input_device = get_input_device_name ();
+ state->output_device = get_output_device_name ();
+ state->sample_rate = get_rate ();
+ state->buffer_size = get_buffer_size ();
+ state->input_latency = get_input_latency ();
+ state->output_latency = get_output_latency ();
+ state->input_channels = get_input_channels ();
+ state->output_channels = get_output_channels ();
+ state->midi_option = get_midi_option ();
+ state->midi_devices = _midi_devices;
+}
+
+void
+EngineControl::maybe_display_saved_state ()
+{
+ if (!_have_control) {
+ return;
+ }
+
+ State state = get_saved_state_for_currently_displayed_backend_and_device ();
+
+ if (state) {
+ PBD::Unwinder<uint32_t> protect_ignore_changes (ignore_changes, ignore_changes + 1);
+
+ 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));
+ /* call this explicitly because we're ignoring changes to
+ the controls at this point.
+ */
+ show_buffer_duration ();
+ input_latency.set_value (state->input_latency);
+ output_latency.set_value (state->output_latency);
+
+ if (!state->midi_option.empty()) {
+ midi_option_combo.set_active_text (state->midi_option);
+ _midi_devices = state->midi_devices;
+ }
+ }
+}
+
+XMLNode&
+EngineControl::get_state ()
+{
+ LocaleGuard lg (X_("C"));
+
+ XMLNode* root = new XMLNode ("AudioMIDISetup");
+ std::string path;
+
+ if (!states.empty()) {
+ XMLNode* state_nodes = new XMLNode ("EngineStates");
+
+ for (StateList::const_iterator i = states.begin(); i != states.end(); ++i) {
+
+ XMLNode* node = new XMLNode ("State");
+
+ node->add_property ("backend", (*i)->backend);
+ node->add_property ("driver", (*i)->driver);
+ node->add_property ("device", (*i)->device);
+ node->add_property ("input-device", (*i)->input_device);
+ node->add_property ("output-device", (*i)->output_device);
+ node->add_property ("sample-rate", (*i)->sample_rate);
+ node->add_property ("buffer-size", (*i)->buffer_size);
+ node->add_property ("input-latency", (*i)->input_latency);
+ node->add_property ("output-latency", (*i)->output_latency);
+ 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 ("midi-option", (*i)->midi_option);
+
+ XMLNode* midi_devices = new XMLNode ("MIDIDevices");
+ for (std::vector<MidiDeviceSettings>::const_iterator p = (*i)->midi_devices.begin(); p != (*i)->midi_devices.end(); ++p) {
+ XMLNode* midi_device_stuff = new XMLNode ("MIDIDevice");
+ midi_device_stuff->add_property (X_("name"), (*p)->name);
+ midi_device_stuff->add_property (X_("enabled"), (*p)->enabled);
+ midi_device_stuff->add_property (X_("input-latency"), (*p)->input_latency);
+ midi_device_stuff->add_property (X_("output-latency"), (*p)->output_latency);
+ midi_devices->add_child_nocopy (*midi_device_stuff);
+ }
+ node->add_child_nocopy (*midi_devices);
+
+ state_nodes->add_child_nocopy (*node);
+ }
+
+ root->add_child_nocopy (*state_nodes);
+ }
+
+ return *root;
+}
+
+void
+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");
+
+ if (root.name() != "AudioMIDISetup") {
+ return;
+ }
+
+ clist = root.children();
+
+ states.clear ();
+
+ for (citer = clist.begin(); citer != clist.end(); ++citer) {
+
+ child = *citer;
+
+ if (child->name() != "EngineStates") {
+ continue;
+ }
+
+ cclist = child->children();
+
+ for (cciter = cclist.begin(); cciter != cclist.end(); ++cciter) {
+ State state (new StateStruct);
+
+ grandchild = *cciter;
+
+ if (grandchild->name() != "State") {
+ continue;
+ }
+
+ if ((prop = grandchild->property ("backend")) == 0) {
+ continue;
+ }
+ state->backend = prop->value ();
+
+ if ((prop = grandchild->property ("driver")) == 0) {
+ continue;
+ }
+ state->driver = prop->value ();
+
+ if ((prop = grandchild->property ("device")) == 0) {
+ continue;
+ }
+ state->device = prop->value ();
+
+ if ((prop = grandchild->property ("input-device")) == 0) {
+ continue;
+ }
+ state->input_device = prop->value ();
+
+ if ((prop = grandchild->property ("output-device")) == 0) {
+ continue;
+ }
+ state->output_device = prop->value ();
+
+ if ((prop = grandchild->property ("sample-rate")) == 0) {
+ continue;
+ }
+ state->sample_rate = atof (prop->value ());
+
+ if ((prop = grandchild->property ("buffer-size")) == 0) {
+ continue;
+ }
+ state->buffer_size = atoi (prop->value ());
+
+ if ((prop = grandchild->property ("input-latency")) == 0) {
+ continue;
+ }
+ state->input_latency = atoi (prop->value ());
+
+ if ((prop = grandchild->property ("output-latency")) == 0) {
+ continue;
+ }
+ state->output_latency = atoi (prop->value ());
+
+ if ((prop = grandchild->property ("input-channels")) == 0) {
+ continue;
+ }
+ state->input_channels = atoi (prop->value ());
+
+ if ((prop = grandchild->property ("output-channels")) == 0) {
+ continue;
+ }
+ state->output_channels = atoi (prop->value ());
+
+ if ((prop = grandchild->property ("active")) == 0) {
+ continue;
+ }
+ state->active = string_is_affirmative (prop->value ());
+
+ if ((prop = grandchild->property ("midi-option")) == 0) {
+ continue;
+ }
+ state->midi_option = prop->value ();
+
+ state->midi_devices.clear();
+ XMLNode* midinode;
+ if ((midinode = ARDOUR::find_named_node (*grandchild, "MIDIDevices")) != 0) {
+ const XMLNodeList mnc = midinode->children();
+ for (XMLNodeList::const_iterator n = mnc.begin(); n != mnc.end(); ++n) {
+ if ((*n)->property (X_("name")) == 0
+ || (*n)->property (X_("enabled")) == 0
+ || (*n)->property (X_("input-latency")) == 0
+ || (*n)->property (X_("output-latency")) == 0
+ ) {
+ continue;
+ }
+
+ MidiDeviceSettings ptr (new MidiDeviceSetting(
+ (*n)->property (X_("name"))->value (),
+ string_is_affirmative ((*n)->property (X_("enabled"))->value ()),
+ atoi ((*n)->property (X_("input-latency"))->value ()),
+ atoi ((*n)->property (X_("output-latency"))->value ())
+ ));
+ state->midi_devices.push_back (ptr);
+ }
+ }
+
+#if 1
+ /* remove accumulated duplicates (due to bug in ealier version)
+ * this can be removed again before release
+ */
+ for (StateList::iterator i = states.begin(); i != states.end();) {
+ if ((*i)->backend == state->backend &&
+ (*i)->driver == state->driver &&
+ (*i)->device == state->device) {
+ i = states.erase(i);
+ } else {
+ ++i;
+ }
+ }