mackie: semi-working Sends subview mode
[ardour.git] / libs / surfaces / mackie / mackie_control_protocol.cc
index 93b54e7d6af76b3f87271756771e0cce8501723f..8ac54e64460dc1fa02ce9860cd9e1418085a7727 100644 (file)
@@ -31,6 +31,7 @@
 #include <errno.h>
 
 #include <boost/shared_array.hpp>
+#include <glibmm/miscutils.h>
 
 #include "midi++/types.h"
 #include "midi++/port.h"
 #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/profile.h"
 #include "ardour/route.h"
+#include "ardour/route_group.h"
 #include "ardour/session.h"
 #include "ardour/tempo.h"
 #include "ardour/track.h"
@@ -100,13 +105,15 @@ bool MackieControlProtocol::probe()
 
 MackieControlProtocol::MackieControlProtocol (Session& session)
        : ControlProtocol (session, X_("Mackie"))
-       , AbstractUI<MackieControlUIRequest> ("mackie")
+       , AbstractUI<MackieControlUIRequest> (name())
        , _current_initial_bank (0)
+       , _frame_last (0)
        , _timecode_type (ARDOUR::AnyTime::BBT)
        , _gui (0)
        , _scrub_mode (false)
        , _flip_mode (Normal)
        , _view_mode (Mixer)
+       , _subview_mode (None)
        , _pot_mode (Pan)
        , _current_selected_track (-1)
        , _modifier_state (0)
@@ -122,6 +129,12 @@ MackieControlProtocol::MackieControlProtocol (Session& session)
        DeviceInfo::reload_device_info ();
        DeviceProfile::reload_device_profiles ();
 
+       for (int i = 0; i < 9; i++) {
+               _last_bank[i] = 0;
+       }
+
+       _last_bank[Mixer] = _current_selected_track;
+
        TrackSelectionChanged.connect (gui_connections, MISSING_INVALIDATOR, boost::bind (&MackieControlProtocol::gui_track_selection_changed, this, _1, true), this);
 
        _instance = this;
@@ -170,10 +183,10 @@ MackieControlProtocol::thread_init ()
 {
        struct sched_param rtparam;
 
-       pthread_set_name (X_("MackieControl"));
+       pthread_set_name (event_loop_name().c_str());
 
-       PBD::notify_gui_about_thread_creation (X_("gui"), pthread_self(), X_("MackieControl"), 2048);
-       ARDOUR::SessionEvent::create_per_thread_pool (X_("MackieControl"), 128);
+       PBD::notify_event_loops_about_thread_creation (pthread_self(), event_loop_name(), 2048);
+       ARDOUR::SessionEvent::create_per_thread_pool (event_loop_name(), 128);
 
        memset (&rtparam, 0, sizeof (rtparam));
        rtparam.sched_priority = 9; /* XXX should be relative to audio (JACK) thread */
@@ -282,21 +295,61 @@ MackieControlProtocol::get_sorted_routes()
 
                switch (_view_mode) {
                case Mixer:
+                       if (! is_hidden(route)) {
+                               sorted.push_back (route);
+                               remote_ids.insert (route->remote_control_id());
+                       }
                        break;
                case AudioTracks:
+                       if (is_audio_track(route) && !is_hidden(route)) {
+                               sorted.push_back (route);
+                               remote_ids.insert (route->remote_control_id());
+                       }
                        break;
                case Busses:
+                       if (Profile->get_mixbus()) {
+#ifdef MIXBUS
+                               if (route->mixbus()) {
+                                       sorted.push_back (route);
+                                       remote_ids.insert (route->remote_control_id());
+                               }
+#endif
+                       } else {
+                               if (!is_track(route) && !is_hidden(route)) {
+                                       sorted.push_back (route);
+                                       remote_ids.insert (route->remote_control_id());
+                               }
+                       }
                        break;
                case MidiTracks:
+                       if (is_midi_track(route) && !is_hidden(route)) {
+                               sorted.push_back (route);
+                               remote_ids.insert (route->remote_control_id());
+                       }
                        break;
                case Plugins:
                        break;
-               case Auxes:
+               case Auxes: // in ardour, for now aux and buss are same. for mixbus, see "Busses" case above
+                       if (!is_track(route) && !is_hidden(route)) {
+                               sorted.push_back (route);
+                               remote_ids.insert (route->remote_control_id());
+                       }
+                       break;
+               case Hidden: // Show all the tracks we have hidden
+                       if (is_hidden(route)) {
+                               // maybe separate groups
+                               sorted.push_back (route);
+                               remote_ids.insert (route->remote_control_id());
+                       }
+                       break;
+               case Selected: // For example: a group (this is USER)
+                       if (selected(route) && !is_hidden(route)) {
+                               sorted.push_back (route);
+                               remote_ids.insert (route->remote_control_id());
+                       }
                        break;
                }
 
-               sorted.push_back (*it);
-               remote_ids.insert (route->remote_control_id());
        }
 
        sort (sorted.begin(), sorted.end(), RouteByRemoteId());
@@ -340,7 +393,6 @@ MackieControlProtocol::switch_banks (uint32_t initial, bool force)
                 */
                return;
        }
-       set_flip_mode (Normal);
        _current_initial_bank = initial;
        _current_selected_track = -1;
 
@@ -372,12 +424,9 @@ MackieControlProtocol::switch_banks (uint32_t initial, bool force)
                }
        }
 
-       /* reset this to get the right display of view mode after the switch */
-       set_view_mode (_view_mode);
-
        /* make sure selection is correct */
 
-       _gui_track_selection_changed (&_last_selected_routes, false);
+       _gui_track_selection_changed (&_last_selected_routes, false, false);
 
        /* current bank has not been saved */
        session->set_dirty();
@@ -590,6 +639,7 @@ MackieControlProtocol::device_ready ()
 
        DEBUG_TRACE (DEBUG::MackieControl, string_compose ("device ready init (active=%1)\n", active()));
        update_surfaces ();
+       set_pot_mode (_pot_mode);
 }
 
 // send messages to surface to set controls to correct values
@@ -642,6 +692,7 @@ MackieControlProtocol::connect_session_signals()
 {
        // receive routes added
        session->RouteAdded.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&MackieControlProtocol::notify_route_added, this, _1), this);
+       session->RouteAddedOrRemoved.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&MackieControlProtocol::notify_route_added_or_removed, this), this);
        // receive record state toggled
        session->RecordStateChanged.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&MackieControlProtocol::notify_record_state_changed, this), this);
        // receive transport state changed
@@ -665,14 +716,10 @@ MackieControlProtocol::connect_session_signals()
 void
 MackieControlProtocol::set_profile (const string& profile_name)
 {
-       if (profile_name == "default") {
-               /* reset to default */
-               _device_profile = DeviceProfile (profile_name);
-       }
-
        map<string,DeviceProfile>::iterator d = DeviceProfile::device_profiles.find (profile_name);
 
        if (d == DeviceProfile::device_profiles.end()) {
+               _device_profile = DeviceProfile (profile_name);
                return;
        }
 
@@ -999,7 +1046,17 @@ MackieControlProtocol::set_state (const XMLNode & node, int version)
        }
 
        if ((prop = node.property (X_("device-profile"))) != 0) {
-               set_profile (prop->value());
+               if (prop->value().empty()) {
+                       string default_profile_name;
+
+                       default_profile_name = Glib::get_user_name();
+                       default_profile_name += ' ';
+                       default_profile_name += _device_info.name();
+
+                       set_profile (default_profile_name);
+               } else {
+                       set_profile (prop->value());
+               }
        }
 
        XMLNode* dnode = node.child (X_("Configurations"));
@@ -1084,6 +1141,13 @@ MackieControlProtocol::update_timecode_display()
        // do assignment here so current_frame is fixed
        framepos_t current_frame = session->transport_frame();
        string timecode;
+       // For large jumps in play head possition do full reset
+       int moved = (current_frame - _frame_last) / session->frame_rate ();
+       if (moved) {
+               DEBUG_TRACE (DEBUG::MackieControl, "Timecode reset\n");
+               _timecode_last = string (10, ' ');
+       }
+       _frame_last = current_frame;
 
        switch (_timecode_type) {
        case ARDOUR::AnyTime::BBT:
@@ -1123,6 +1187,15 @@ void MackieControlProtocol::notify_parameter_changed (std::string const & p)
        }
 }
 
+void
+MackieControlProtocol::notify_route_added_or_removed ()
+{
+       Glib::Threads::Mutex::Lock lm (surfaces_lock);
+       for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
+               (*s)->master_monitor_may_have_changed ();
+       }
+}
+
 // RouteList is the set of routes that have just been added
 void
 MackieControlProtocol::notify_route_added (ARDOUR::RouteList & rl)
@@ -1135,6 +1208,15 @@ MackieControlProtocol::notify_route_added (ARDOUR::RouteList & rl)
                }
        }
 
+       /* special case: single route, and it is the monitor or master out */
+
+       if (rl.size() == 1 && (rl.front()->is_monitor() || rl.front()->is_master())) {
+               Glib::Threads::Mutex::Lock lm (surfaces_lock);
+               for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
+                       (*s)->master_monitor_may_have_changed ();
+               }
+       }
+
        // currently assigned banks are less than the full set of
        // strips, so activate the new strip now.
 
@@ -1193,7 +1275,11 @@ MackieControlProtocol::notify_remote_id_changed()
 
        if (sorted.size() - _current_initial_bank < sz) {
                // but don't shift backwards past the zeroth channel
-               switch_banks (max((Sorted::size_type) 0, sorted.size() - sz));
+               if (sorted.size() < sz) {  // avoid unsigned math mistake below
+                       switch_banks(0, true);
+               } else {
+                       switch_banks (max((Sorted::size_type) 0, sorted.size() - sz), true);
+               }
        } else {
                // Otherwise just refresh the current bank
                refresh_current_bank();
@@ -1224,6 +1310,9 @@ MackieControlProtocol::notify_transport_state_changed()
        update_global_button (Button::Rewind, session->transport_speed() < 0.0);
        update_global_button (Button::Ffwd, session->transport_speed() > 1.0);
 
+       // sometimes a return to start leaves time code at old time
+       _timecode_last = string (10, ' ');
+
        notify_metering_state_changed ();
 }
 
@@ -1298,6 +1387,7 @@ MackieControlProtocol::bundles ()
 void
 MackieControlProtocol::do_request (MackieControlUIRequest* req)
 {
+       DEBUG_TRACE (DEBUG::MackieControl, string_compose ("doing request type %1\n", req->type));
        if (req->type == CallSlot) {
 
                call_slot (MISSING_INVALIDATOR, req->the_slot);
@@ -1397,7 +1487,6 @@ MackieControlProtocol::build_button_map ()
        DEFINE_BUTTON_HANDLER (Button::UserA, &MackieControlProtocol::user_a_press, &MackieControlProtocol::user_a_release);
        DEFINE_BUTTON_HANDLER (Button::UserB, &MackieControlProtocol::user_b_press, &MackieControlProtocol::user_b_release);
        DEFINE_BUTTON_HANDLER (Button::MasterFaderTouch, &MackieControlProtocol::master_fader_touch_press, &MackieControlProtocol::master_fader_touch_release);
-
 }
 
 void
@@ -1527,17 +1616,113 @@ MackieControlProtocol::clear_ports ()
        }
 }
 
+void
+MackieControlProtocol::notify_subview_route_deleted ()
+{
+       /* return to global/mixer view */
+       _subview_route.reset ();
+       set_view_mode (Mixer);
+}
+
+void
+MackieControlProtocol::set_subview_mode (SubViewMode sm, boost::shared_ptr<Route> r)
+{
+       SubViewMode old_mode = _subview_mode;
+       boost::shared_ptr<Route> old_route = _subview_route;
+
+       _subview_mode = sm;
+
+       if (r) {
+               /* retain _subview_route even if it is reset to null implicitly */
+               _subview_route = r;
+       }
+
+       if ((_subview_mode != old_mode) || (_subview_route != old_route)) {
+
+               if (r != old_route) {
+                       subview_route_connections.drop_connections ();
+                       if (_subview_route) {
+                               _subview_route->DropReferences.connect (subview_route_connections, MISSING_INVALIDATOR, boost::bind (&MackieControlProtocol::notify_subview_route_deleted, this), this);
+                       }
+               }
+
+               /* subview mode did actually change */
+
+               {
+                       Surfaces copy; /* can't hold surfaces lock while calling Strip::subview_mode_changed */
+
+                       {
+                               Glib::Threads::Mutex::Lock lm (surfaces_lock);
+                               copy = surfaces;
+                       }
+
+                       for (Surfaces::iterator s = copy.begin(); s != copy.end(); ++s) {
+                               (*s)->subview_mode_changed ();
+                       }
+               }
+
+               if (_subview_mode != old_mode) {
+
+                       /* turn buttons related to vpot mode on or off as required */
+
+                       switch (_subview_mode) {
+                       case MackieControlProtocol::None:
+                               pot_mode_globals ();
+                               break;
+                       case MackieControlProtocol::EQ:
+                               update_global_button (Button::Send, off);
+                               update_global_button (Button::Eq, on);
+                               update_global_button (Button::Dyn, off);
+                               update_global_button (Button::AudioInstruments, off); /* faking up Dyn */
+                               update_global_button (Button::Trim, off);
+                               update_global_button (Button::Send, off);
+                               update_global_button (Button::Pan, off);
+                               break;
+                       case MackieControlProtocol::Dynamics:
+                               update_global_button (Button::Send, off);
+                               update_global_button (Button::Eq, off);
+                               update_global_button (Button::Dyn, on);
+                               update_global_button (Button::AudioInstruments, on); /* faking up Dyn */
+                               update_global_button (Button::Trim, off);
+                               update_global_button (Button::Send, off);
+                               update_global_button (Button::Pan, off);
+                               break;
+                       case MackieControlProtocol::Sends:
+                               update_global_button (Button::Send, on);
+                               update_global_button (Button::Eq, off);
+                               update_global_button (Button::Dyn, off);
+                               update_global_button (Button::AudioInstruments, on); /* faking up Dyn */
+                               update_global_button (Button::Trim, off);
+                               update_global_button (Button::Send, off);
+                               update_global_button (Button::Pan, off);
+                               break;
+                       }
+               }
+       }
+}
+
 void
 MackieControlProtocol::set_view_mode (ViewMode m)
 {
-       Glib::Threads::Mutex::Lock lm (surfaces_lock);
+       _last_bank[_view_mode] = _current_initial_bank;
 
        _view_mode = m;
+       set_subview_mode (None, boost::shared_ptr<Route>());
 
-       for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
-               (*s)->update_view_mode_display ();
-       }
+       switch_banks(_last_bank[_view_mode], true);
+       display_view_mode ();
+}
+
+void
+MackieControlProtocol::display_view_mode ()
+{
+       {
+               Glib::Threads::Mutex::Lock lm (surfaces_lock);
 
+               for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
+                       (*s)->update_view_mode_display ();
+               }
+       }
 }
 
 void
@@ -1563,6 +1748,11 @@ MackieControlProtocol::set_flip_mode (FlipMode fm)
 void
 MackieControlProtocol::set_pot_mode (PotMode m)
 {
+       // maybe not in flip mode.
+       if (flip_mode()) {
+               return;
+       }
+
        _pot_mode = m;
 
        {
@@ -1574,17 +1764,22 @@ MackieControlProtocol::set_pot_mode (PotMode m)
                }
        }
 
+       pot_mode_globals ();
+}
+
+void
+MackieControlProtocol::pot_mode_globals ()
+{
+       update_global_button (Button::Eq, off);
+       update_global_button (Button::Dyn, off);
+       update_global_button (Button::AudioInstruments, off);
+
        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);
@@ -1627,11 +1822,11 @@ MackieControlProtocol::force_special_route_to_strip (boost::shared_ptr<Route> r,
 void
 MackieControlProtocol::gui_track_selection_changed (ARDOUR::RouteNotificationListPtr rl, bool save_list)
 {
-       _gui_track_selection_changed (rl.get(), save_list);
+       _gui_track_selection_changed (rl.get(), save_list, true);
 }
 
 void
-MackieControlProtocol::_gui_track_selection_changed (ARDOUR::RouteNotificationList* rl, bool save_list)
+MackieControlProtocol::_gui_track_selection_changed (ARDOUR::RouteNotificationList* rl, bool save_list, bool gui_selection_did_change)
 {
        /* We need to keep a list of the most recently selected routes around,
           but we are not allowed to keep shared_ptr<Route> unless we want to
@@ -1662,6 +1857,11 @@ MackieControlProtocol::_gui_track_selection_changed (ARDOUR::RouteNotificationLi
        if (save_list) {
                _last_selected_routes = *rl;
        }
+
+       if (gui_selection_did_change) {
+               /* actual GUI selection changed */
+               set_subview_mode (_subview_mode, first_selected_route());
+       }
 }
 
 framepos_t
@@ -1984,3 +2184,89 @@ 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;
+}
+
+bool
+MackieControlProtocol::selected (boost::shared_ptr<Route> r) const
+{
+       const RouteNotificationList* rl = &_last_selected_routes;
+
+       for (ARDOUR::RouteNotificationList::const_iterator i = rl->begin(); i != rl->end(); ++i) {
+               boost::shared_ptr<ARDOUR::Route> rt = (*i).lock();
+               if (rt == r) {
+                       return true;
+               }
+       }
+       return false;
+}
+
+bool
+MackieControlProtocol::is_hidden (boost::shared_ptr<Route> r) const
+{
+       if (!r) {
+               return false;
+       }
+       return (((r->remote_control_id()) >>31) != 0);
+}
+
+boost::shared_ptr<Route>
+MackieControlProtocol::first_selected_route () const
+{
+       if (_last_selected_routes.empty()) {
+               return boost::shared_ptr<Route>();
+       }
+
+       boost::shared_ptr<Route> r = _last_selected_routes.front().lock();
+
+       return r; /* may be null */
+}
+
+boost::shared_ptr<Route>
+MackieControlProtocol::subview_route () const
+{
+       return _subview_route;
+}
+
+uint32_t
+MackieControlProtocol::global_index (Strip& strip)
+{
+       Glib::Threads::Mutex::Lock lm (surfaces_lock);
+       uint32_t global = 0;
+
+       for (Surfaces::const_iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
+               if ((*s).get() == strip.surface()) {
+                       return global + strip.index();
+               }
+               global += (*s)->n_strips ();
+       }
+
+       return global;
+}
+
+void*
+MackieControlProtocol::request_factory (uint32_t num_requests)
+{
+       /* AbstractUI<T>::request_buffer_factory() is a template method only
+          instantiated in this source module. To provide something visible for
+          use in the interface/descriptor, we have this static method that is
+          template-free.
+       */
+       return request_buffer_factory (num_requests);
+}