Add Audio Tracks and Busses too.
[ardour.git] / libs / surfaces / mackie / mackie_control_protocol.cc
index 0334431d819cb8372c9b541b81c3ed6d280da3fb..581d68b89e411b521070e5a9b3bc9fa19a6d9748 100644 (file)
 #include "pbd/memento_command.h"
 #include "pbd/convert.h"
 
+#include "ardour/audio_track.h"
 #include "ardour/automation_control.h"
 #include "ardour/async_midi_port.h"
 #include "ardour/dB.h"
 #include "ardour/debug.h"
 #include "ardour/location.h"
 #include "ardour/meter.h"
+#include "ardour/midi_track.h"
 #include "ardour/panner.h"
 #include "ardour/panner_shell.h"
 #include "ardour/route.h"
@@ -114,9 +116,8 @@ MackieControlProtocol::MackieControlProtocol (Session& session)
        , needs_ipmidi_restart (false)
        , _metering_active (true)
        , _initialized (false)
-       , _surfaces_state (0)
-       , _surfaces_version (0)
-       , _session_load (true)
+       , configuration_state (0)
+       , state_version (0)
 {
        DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::MackieControlProtocol\n");
 
@@ -144,7 +145,7 @@ MackieControlProtocol::~MackieControlProtocol()
        DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::~MackieControlProtocol tear_down_gui ()\n");
        tear_down_gui ();
 
-       delete _surfaces_state;
+       delete configuration_state;
 
        /* stop event loop */
        DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::~MackieControlProtocol BaseUI::quit ()\n");
@@ -280,25 +281,36 @@ MackieControlProtocol::get_sorted_routes()
                if (route_is_locked_to_strip(route)) {
                        continue;
                }
-               /* This next section which is not used yet, looks wrong to me
-                       The first four belong here but the bottom five are not a selection
-                       of routes and belong elsewhere as they are v-pot modes.
-               */
+
                switch (_view_mode) {
                case Mixer:
+                       sorted.push_back (*it);
+                       remote_ids.insert (route->remote_control_id());
                        break;
                case AudioTracks:
+                       if (is_audio_track(*it)) {
+                               sorted.push_back (*it);
+                               remote_ids.insert (route->remote_control_id());
+                       }
                        break;
                case Busses:
+                       if (!is_track(*it)) {
+                               sorted.push_back (*it);
+                               remote_ids.insert (route->remote_control_id());
+                       }
                        break;
                case MidiTracks:
+                       if (is_midi_track(*it)) {
+                               sorted.push_back (*it);
+                               remote_ids.insert (route->remote_control_id());
+                       }
+                       break;
+               case Plugins:
                        break;
-               case Loop:
+               case Auxes:
                        break;
                }
 
-               sorted.push_back (*it);
-               remote_ids.insert (route->remote_control_id());
        }
 
        sort (sorted.begin(), sorted.end(), RouteByRemoteId());
@@ -342,7 +354,6 @@ MackieControlProtocol::switch_banks (uint32_t initial, bool force)
                 */
                return;
        }
-
        _current_initial_bank = initial;
        _current_selected_track = -1;
 
@@ -532,17 +543,21 @@ MackieControlProtocol::update_timecode_beats_led()
 void
 MackieControlProtocol::update_global_button (int id, LedState ls)
 {
-       Glib::Threads::Mutex::Lock lm (surfaces_lock);
+       boost::shared_ptr<Surface> surface;
 
-       if (surfaces.empty()) {
-               return;
-       }
+       {
+               Glib::Threads::Mutex::Lock lm (surfaces_lock);
 
-       if (!_device_info.has_global_controls()) {
-               return;
+               if (surfaces.empty()) {
+                       return;
+               }
+
+               if (!_device_info.has_global_controls()) {
+                       return;
+               }
+               // surface needs to be master surface
+               surface = _master_surface;
        }
-       // surface needs to be master surface
-       boost::shared_ptr<Surface> surface = _master_surface;
 
        map<int,Control*>::iterator x = surface->controls_by_device_independent_id.find (id);
        if (x != surface->controls_by_device_independent_id.end()) {
@@ -696,15 +711,23 @@ MackieControlProtocol::set_device_info (const string& device_name)
 int
 MackieControlProtocol::set_device (const string& device_name, bool force)
 {
-       cerr << "Set Device\n\n\n\n\n\n";
-
        if (device_name == device_info().name() && !force) {
                /* already using that device, nothing to do */
                return 0;
        }
+       /* get state from the current setup, and make sure it is stored in
+          the configuration_states node so that if we switch back to this device,
+          we will have its state available.
+       */
+
+       {
+               Glib::Threads::Mutex::Lock lm (surfaces_lock);
+               if (!surfaces.empty()) {
+                       update_configuration_state ();
+               }
+       }
 
        if (set_device_info (device_name)) {
-               cerr << "Unknown device name\n";
                return -1;
        }
 
@@ -750,17 +773,36 @@ MackieControlProtocol::create_surfaces ()
 
        DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Create %1 surfaces for %2\n", 1 + _device_info.extenders(), _device_info.name()));
 
+       if (!_device_info.uses_ipmidi()) {
+               _input_bundle.reset (new ARDOUR::Bundle (_("Mackie Control In"), true));
+               _output_bundle.reset (new ARDOUR::Bundle (_("Mackie Control Out"), false));
+       } else {
+               _input_bundle.reset ();
+               _output_bundle.reset ();
+
+       }
        for (uint32_t n = 0; n < 1 + _device_info.extenders(); ++n) {
+               bool is_master = false;
+
+               if (n == _device_info.master_position()) {
+                       is_master = true;
+                       if (_device_info.extenders() == 0) {
+                               device_name = _device_info.name();
+                       } else {
+                               device_name = X_("mackie control");
+                       }
 
-               if (n == 0) {
-                       device_name = _device_info.name();
-               } else {
-                       device_name = string_compose ("%1 #%2", _device_info.name(), n+1);
                }
 
+               if (!is_master) {
+                       device_name = string_compose (X_("mackie control ext %1"), n+1);
+               }
+
+               DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Port Name for surface %1 is %2\n", n, device_name));
+
                boost::shared_ptr<Surface> surface;
 
-               if (n == _device_info.master_position()) {
+               if (is_master) {
                        stype = mcu;
                } else {
                        stype = ext;
@@ -771,13 +813,26 @@ MackieControlProtocol::create_surfaces ()
                        return -1;
                }
 
-               if (n == _device_info.master_position()) {
+               if (is_master) {
                        _master_surface = surface;
                }
 
-               if (_surfaces_state) {
-                       cerr << "Resetting surface state\n";
-                       surface->set_state (*_surfaces_state, _surfaces_version);
+               if (configuration_state) {
+                       XMLNode* this_device = 0;
+                       XMLNodeList const& devices = configuration_state->children();
+                       for (XMLNodeList::const_iterator d = devices.begin(); d != devices.end(); ++d) {
+                               XMLProperty* prop = (*d)->property (X_("name"));
+                               if (prop && prop->value() == _device_info.name()) {
+                                       this_device = *d;
+                                       break;
+                               }
+                       }
+                       if (this_device) {
+                               XMLNode* snode = this_device->child (X_("Surfaces"));
+                               if (snode) {
+                                       surface->set_state (*snode, state_version);
+                               }
+                       }
                }
 
                {
@@ -787,9 +842,6 @@ MackieControlProtocol::create_surfaces ()
 
                if (!_device_info.uses_ipmidi()) {
 
-                       _input_bundle.reset (new ARDOUR::Bundle (_("Mackie Control In"), true));
-                       _output_bundle.reset (new ARDOUR::Bundle (_("Mackie Control Out"), false));
-
                        _input_bundle->add_channel (
                                surface->port().input_port().name(),
                                ARDOUR::DataType::MIDI,
@@ -801,14 +853,6 @@ MackieControlProtocol::create_surfaces ()
                                ARDOUR::DataType::MIDI,
                                session->engine().make_port_name_non_relative (surface->port().output_port().name())
                                );
-
-                       session->BundleAddedOrRemoved ();
-
-               } else {
-                       _input_bundle.reset ((ARDOUR::Bundle*) 0);
-                       _output_bundle.reset ((ARDOUR::Bundle*) 0);
-
-                       session->BundleAddedOrRemoved ();
                }
 
                MIDI::Port& input_port (surface->port().input_port());
@@ -856,6 +900,17 @@ MackieControlProtocol::create_surfaces ()
                }
        }
 
+       if (!_device_info.uses_ipmidi()) {
+               Glib::Threads::Mutex::Lock lm (surfaces_lock);
+               for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
+                       (*s)->port().reconnect ();
+               }
+       }
+
+       session->BundleAddedOrRemoved ();
+
+       assert (_master_surface);
+
        return 0;
 }
 
@@ -870,6 +925,34 @@ MackieControlProtocol::close()
        clear_surfaces();
 }
 
+/** Ensure that the configuration_state XML node contains an up-to-date
+ *  copy of the state node the current device. If configuration_state already
+ *  contains a state node for the device, it will deleted and replaced.
+ */
+void
+MackieControlProtocol::update_configuration_state ()
+{
+       /* CALLER MUST HOLD SURFACES LOCK */
+
+       if (!configuration_state) {
+               configuration_state = new XMLNode (X_("Configurations"));
+       }
+
+       XMLNode* devnode = new XMLNode (X_("Configuration"));
+       devnode->add_property (X_("name"), _device_info.name());
+
+       configuration_state->remove_nodes_and_delete (X_("name"), _device_info.name());
+       configuration_state->add_child_nocopy (*devnode);
+
+       XMLNode* snode = new XMLNode (X_("Surfaces"));
+
+       for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
+               snode->add_child_nocopy ((*s)->get_state());
+       }
+
+       devnode->add_child_nocopy (*snode);
+}
+
 XMLNode&
 MackieControlProtocol::get_state()
 {
@@ -889,12 +972,13 @@ MackieControlProtocol::get_state()
        node.add_property (X_("device-profile"), _device_profile.name());
        node.add_property (X_("device-name"), _device_info.name());
 
-       XMLNode* snode = new XMLNode (X_("Surfaces"));
-       for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
-               snode->add_child_nocopy ((*s)->get_state());
+       {
+               Glib::Threads::Mutex::Lock lm (surfaces_lock);
+               update_configuration_state ();
        }
 
-       node.add_child_nocopy (*snode);
+       /* force a copy of the _surfaces_state node, because we want to retain ownership */
+       node.add_child_copy (*configuration_state);
 
        DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::get_state done\n");
 
@@ -931,14 +1015,14 @@ MackieControlProtocol::set_state (const XMLNode & node, int version)
                set_profile (prop->value());
        }
 
-       XMLNode* snode = node.child (X_("Surfaces"));
+       XMLNode* dnode = node.child (X_("Configurations"));
 
-       delete _surfaces_state;
-       _surfaces_state = 0;
+       delete configuration_state;
+       configuration_state = 0;
 
-       if (snode) {
-               _surfaces_state = new XMLNode (*snode);
-               _surfaces_version = version;
+       if (dnode) {
+               configuration_state = new XMLNode (*dnode);
+               state_version = version;
        }
 
        switch_banks (bank, true);
@@ -1472,27 +1556,57 @@ MackieControlProtocol::set_view_mode (ViewMode m)
 void
 MackieControlProtocol::set_flip_mode (FlipMode fm)
 {
-       Glib::Threads::Mutex::Lock lm (surfaces_lock);
+       if (_flip_mode != fm) {
+               if (fm == Normal) {
+                       update_global_button (Button::Flip, off);
+               } else {
+                       update_global_button (Button::Flip, on);
+               }
 
-       _flip_mode = fm;
+               Glib::Threads::Mutex::Lock lm (surfaces_lock);
 
-       for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
-               (*s)->update_flip_mode_display ();
+               _flip_mode = fm;
+
+               for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
+                       (*s)->update_flip_mode_display ();
+               }
        }
 }
 
 void
 MackieControlProtocol::set_pot_mode (PotMode m)
 {
-       Glib::Threads::Mutex::Lock lm (surfaces_lock);
-
+       // maybe not in flip mode.
+       if (flip_mode()) {
+               return;
+       }
        _pot_mode = m;
 
-       for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
-               (*s)->update_potmode ();
+       {
+               Glib::Threads::Mutex::Lock lm (surfaces_lock);
 
+               for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
+                       (*s)->update_potmode ();
+
+               }
        }
 
+       switch (_pot_mode) {
+       case Trim:
+               update_global_button (Button::Track, on);
+               update_global_button (Button::Send, off);
+               update_global_button (Button::Pan, off);
+               break;
+       case Send:
+               update_global_button (Button::Track, off);
+               update_global_button (Button::Send, on);
+               update_global_button (Button::Pan, off);
+               break;
+       case Pan:
+               update_global_button (Button::Track, off);
+               update_global_button (Button::Send, off);
+               update_global_button (Button::Pan, on);
+       };
 }
 
 void
@@ -1887,3 +2001,21 @@ MackieControlProtocol::connection_handler (boost::weak_ptr<ARDOUR::Port> wp1, st
                }
        }
 }
+
+bool
+MackieControlProtocol::is_track (boost::shared_ptr<Route> r) const
+{
+       return boost::dynamic_pointer_cast<Track>(r) != 0;
+}
+
+bool
+MackieControlProtocol::is_audio_track (boost::shared_ptr<Route> r) const
+{
+       return boost::dynamic_pointer_cast<AudioTrack>(r) != 0;
+}
+
+bool
+MackieControlProtocol::is_midi_track (boost::shared_ptr<Route> r) const
+{
+       return boost::dynamic_pointer_cast<MidiTrack>(r) != 0;
+}