Add Audio Tracks and Busses too.
[ardour.git] / libs / surfaces / mackie / mackie_control_protocol.cc
index c195916df98efa1c1aeb58d37fba831cee3cfdb0..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;
 
@@ -400,11 +411,11 @@ MackieControlProtocol::set_active (bool yn)
 
                BaseUI::run ();
 
-               if (create_surfaces ()) {
-                       return -1;
-               }
                connect_session_signals ();
-               update_surfaces ();
+
+               if (!_device_info.name().empty()) {
+                       set_device (_device_info.name(), true);
+               }
 
                /* set up periodic task for metering and automation
                 */
@@ -463,16 +474,6 @@ MackieControlProtocol::periodic ()
 
        ARDOUR::microseconds_t now_usecs = ARDOUR::get_microseconds ();
 
-       static int cnt = 0;
-
-       cnt++;
-       if ((cnt != 1) && (cnt % 25 == 0)) {
-               if (_master_surface) {
-                       cerr << string_compose ("Cnt now %1 ", cnt) << endl;
-                       _master_surface->display_message_for (string_compose ("Cnt now %1\n12Hey Paul", cnt), 1000);
-               }
-       }
-
        {
                Glib::Threads::Mutex::Lock lm (surfaces_lock);
 
@@ -542,13 +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 (!_device_info.has_global_controls()) {
-               return;
+       {
+               Glib::Threads::Mutex::Lock lm (surfaces_lock);
+
+               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()) {
@@ -564,6 +573,10 @@ MackieControlProtocol::update_global_led (int id, LedState ls)
 {
        Glib::Threads::Mutex::Lock lm (surfaces_lock);
 
+       if (surfaces.empty()) {
+               return;
+       }
+
        if (!_device_info.has_global_controls()) {
                return;
        }
@@ -696,13 +709,30 @@ MackieControlProtocol::set_device_info (const string& device_name)
 }
 
 int
-MackieControlProtocol::set_device (const string& device_name)
+MackieControlProtocol::set_device (const string& device_name, bool force)
 {
+       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)) {
                return -1;
        }
 
        clear_surfaces ();
+       port_connection.disconnect ();
        hui_connection.disconnect ();
 
        if (_device_info.device_type() == DeviceInfo::HUI) {
@@ -711,11 +741,19 @@ MackieControlProtocol::set_device (const string& device_name)
                hui_timeout->attach (main_loop()->get_context());
        }
 
+       if (!_device_info.uses_ipmidi()) {
+               /* notice that the handler for this will execute in our event
+                  loop, not in the thread where the
+                  PortConnectedOrDisconnected signal is emitted.
+               */
+               ARDOUR::AudioEngine::instance()->PortConnectedOrDisconnected.connect (port_connection, MISSING_INVALIDATOR, boost::bind (&MackieControlProtocol::connection_handler, this, _1, _2, _3, _4, _5), this);
+       }
+
        if (create_surfaces ()) {
                return -1;
        }
 
-       switch_banks (0, true);
+       DeviceChanged ();
 
        return 0;
 }
@@ -732,21 +770,39 @@ MackieControlProtocol::create_surfaces ()
 {
        string device_name;
        surface_type_t stype = mcu; // type not yet determined
-       char buf[128];
 
-       if (_device_info.extenders() == 0) {
-               device_name = X_("mackie control");
+       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 {
-               device_name = X_("mackie control #1");
+               _input_bundle.reset ();
+               _output_bundle.reset ();
+
        }
+       for (uint32_t n = 0; n < 1 + _device_info.extenders(); ++n) {
+               bool is_master = false;
 
-       DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Create %1 surfaces\n", 1 + _device_info.extenders()));
+               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");
+                       }
 
-       for (uint32_t n = 0; n < 1 + _device_info.extenders(); ++n) {
+               }
+
+               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;
@@ -757,12 +813,26 @@ MackieControlProtocol::create_surfaces ()
                        return -1;
                }
 
-               if (n == _device_info.master_position()) {
+               if (is_master) {
                        _master_surface = surface;
                }
 
-               if (_surfaces_state) {
-                       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);
+                               }
+                       }
                }
 
                {
@@ -770,18 +840,8 @@ MackieControlProtocol::create_surfaces ()
                        surfaces.push_back (surface);
                }
 
-               if (_device_info.extenders() < 2) {
-                       device_name = X_("mackie control #2");
-               } else {
-                       snprintf (buf, sizeof (buf), X_("mackie control #%d"), n+2);
-                       device_name = buf;
-               }
-
                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,
@@ -793,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());
@@ -822,7 +874,12 @@ MackieControlProtocol::create_surfaces ()
                        if ((fd = input_port.selectable ()) >= 0) {
 
                                GIOChannel* ioc = g_io_channel_unix_new (fd);
-                               GSource* gsrc = g_io_create_watch (ioc, GIOCondition (G_IO_IN|G_IO_HUP|G_IO_ERR));
+                               surface->input_source = g_io_create_watch (ioc, GIOCondition (G_IO_IN|G_IO_HUP|G_IO_ERR));
+
+                               /* make surface's input source now hold the
+                                * only reference on the IO channel
+                                */
+                               g_io_channel_unref (ioc);
 
                                /* hack up an object so that in the callback from the event loop
                                   we have both the MackieControlProtocol and the input port.
@@ -837,18 +894,30 @@ MackieControlProtocol::create_surfaces ()
                                ipm->mcp = this;
                                ipm->port = &input_port;
 
-                               g_source_set_callback (gsrc, (GSourceFunc) ipmidi_input_handler, ipm, NULL);
-                               g_source_attach (gsrc, main_loop()->get_context()->gobj());
+                               g_source_set_callback (surface->input_source, (GSourceFunc) ipmidi_input_handler, ipm, NULL);
+                               g_source_attach (surface->input_source, main_loop()->get_context()->gobj());
                        }
                }
        }
 
+       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;
 }
 
 void
 MackieControlProtocol::close()
 {
+       port_connection.disconnect ();
        session_connections.drop_connections ();
        route_connections.drop_connections ();
        periodic_connection.disconnect ();
@@ -856,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()
 {
@@ -875,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");
 
@@ -917,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);
@@ -1042,6 +1140,14 @@ void MackieControlProtocol::notify_parameter_changed (std::string const & p)
 void
 MackieControlProtocol::notify_route_added (ARDOUR::RouteList & rl)
 {
+       {
+               Glib::Threads::Mutex::Lock lm (surfaces_lock);
+
+               if (surfaces.empty()) {
+                       return;
+               }
+       }
+
        // currently assigned banks are less than the full set of
        // strips, so activate the new strip now.
 
@@ -1064,6 +1170,11 @@ MackieControlProtocol::notify_solo_active_changed (bool active)
 
        {
                Glib::Threads::Mutex::Lock lm (surfaces_lock);
+
+               if (surfaces.empty()) {
+                       return;
+               }
+
                surface = _master_surface;
        }
 
@@ -1079,6 +1190,14 @@ MackieControlProtocol::notify_solo_active_changed (bool active)
 void
 MackieControlProtocol::notify_remote_id_changed()
 {
+       {
+               Glib::Threads::Mutex::Lock lm (surfaces_lock);
+
+               if (surfaces.empty()) {
+                       return;
+               }
+       }
+
        Sorted sorted = get_sorted_routes();
        uint32_t sz = n_strips();
 
@@ -1142,6 +1261,9 @@ MackieControlProtocol::notify_record_state_changed ()
 
        {
                Glib::Threads::Mutex::Lock lm (surfaces_lock);
+               if (surfaces.empty()) {
+                       return;
+               }
                surface = _master_surface;
        }
 
@@ -1377,14 +1499,15 @@ MackieControlProtocol::handle_button_event (Surface& surface, Button& button, Bu
 bool
 MackieControlProtocol::midi_input_handler (IOCondition ioc, MIDI::Port* port)
 {
-       DEBUG_TRACE (DEBUG::MackieControl, string_compose ("something happend on  %1\n", port->name()));
-
        if (ioc & ~IO_IN) {
+               DEBUG_TRACE (DEBUG::MackieControl, "MIDI port closed\n");
                return false;
        }
 
        if (ioc & IO_IN) {
 
+               DEBUG_TRACE (DEBUG::MackieControl, string_compose ("something happend on  %1\n", port->name()));
+
                /* Devices using regular JACK MIDI ports will need to have
                   the x-thread FIFO drained to avoid burning endless CPU.
 
@@ -1433,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
@@ -1763,9 +1916,12 @@ void
 MackieControlProtocol::clear_surfaces ()
 {
        clear_ports ();
-       Glib::Threads::Mutex::Lock lm (surfaces_lock);
-       _master_surface.reset ();
-       surfaces.clear ();
+
+       {
+               Glib::Threads::Mutex::Lock lm (surfaces_lock);
+               _master_surface.reset ();
+               surfaces.clear ();
+       }
 }
 
 void
@@ -1800,3 +1956,66 @@ MackieControlProtocol::toggle_backlight ()
                (*s)->toggle_backlight ();
        }
 }
+
+boost::shared_ptr<Surface>
+MackieControlProtocol::get_surface_by_raw_pointer (void* ptr) const
+{
+       Glib::Threads::Mutex::Lock lm (surfaces_lock);
+
+       for (Surfaces::const_iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
+               if ((*s).get() == (Surface*) ptr) {
+                       return *s;
+               }
+       }
+
+       return boost::shared_ptr<Surface> ();
+}
+
+boost::shared_ptr<Surface>
+MackieControlProtocol::nth_surface (uint32_t n) const
+{
+       Glib::Threads::Mutex::Lock lm (surfaces_lock);
+
+       for (Surfaces::const_iterator s = surfaces.begin(); s != surfaces.end(); ++s, --n) {
+               if (n == 0) {
+                       return *s;
+               }
+       }
+
+       return boost::shared_ptr<Surface> ();
+}
+
+void
+MackieControlProtocol::connection_handler (boost::weak_ptr<ARDOUR::Port> wp1, std::string name1, boost::weak_ptr<ARDOUR::Port> wp2, std::string name2, bool yn)
+{
+       Surfaces scopy;
+       {
+               Glib::Threads::Mutex::Lock lm (surfaces_lock);
+               scopy = surfaces;
+       }
+
+       for (Surfaces::const_iterator s = scopy.begin(); s != scopy.end(); ++s) {
+               if ((*s)->connection_handler (wp1, name1, wp2, name2, yn)) {
+                       ConnectionChange (*s);
+                       break;
+               }
+       }
+}
+
+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;
+}