use new shared cancel_all_solo() in Mackie code
[ardour.git] / libs / surfaces / control_protocol / control_protocol.cc
index 5470232ad795474703572a6702b7c4adb4673a79..8c2a1217790063cd4650d6debddda2a54149f41e 100644 (file)
@@ -22,6 +22,7 @@
 
 #include "ardour/gain_control.h"
 #include "ardour/session.h"
+#include "ardour/record_enable_control.h"
 #include "ardour/route.h"
 #include "ardour/audio_track.h"
 #include "ardour/meter.h"
@@ -45,15 +46,23 @@ PBD::Signal0<void> ControlProtocol::VerticalZoomInAll;
 PBD::Signal0<void> ControlProtocol::VerticalZoomOutAll;
 PBD::Signal0<void> ControlProtocol::VerticalZoomInSelected;
 PBD::Signal0<void> ControlProtocol::VerticalZoomOutSelected;
-PBD::Signal1<void,RouteNotificationListPtr> ControlProtocol::TrackSelectionChanged;
-PBD::Signal1<void,uint32_t> ControlProtocol::AddRouteToSelection;
-PBD::Signal1<void,uint32_t> ControlProtocol::SetRouteSelection;
-PBD::Signal1<void,uint32_t> ControlProtocol::ToggleRouteSelection;
-PBD::Signal1<void,uint32_t> ControlProtocol::RemoveRouteFromSelection;
-PBD::Signal0<void>          ControlProtocol::ClearRouteSelection;
 PBD::Signal0<void>          ControlProtocol::StepTracksDown;
 PBD::Signal0<void>          ControlProtocol::StepTracksUp;
 
+PBD::Signal1<void,boost::shared_ptr<ARDOUR::Stripable> > ControlProtocol::AddStripableToSelection;
+PBD::Signal1<void,boost::shared_ptr<ARDOUR::Stripable> > ControlProtocol::SetStripableSelection;
+PBD::Signal1<void,boost::shared_ptr<ARDOUR::Stripable> > ControlProtocol::ToggleStripableSelection;
+PBD::Signal1<void,boost::shared_ptr<ARDOUR::Stripable> > ControlProtocol::RemoveStripableFromSelection;
+PBD::Signal0<void>          ControlProtocol::ClearStripableSelection;
+
+PBD::Signal1<void,StripableNotificationListPtr> ControlProtocol::StripableSelectionChanged;
+
+Glib::Threads::Mutex ControlProtocol::first_selected_mutex;
+boost::weak_ptr<Stripable> ControlProtocol::_first_selected_stripable;
+StripableNotificationList ControlProtocol::_last_selected;
+bool ControlProtocol::selection_connected = false;
+PBD::ScopedConnection ControlProtocol::selection_connection;
+
 const std::string ControlProtocol::state_node_name ("Protocol");
 
 ControlProtocol::ControlProtocol (Session& s, string str)
@@ -61,6 +70,12 @@ ControlProtocol::ControlProtocol (Session& s, string str)
        , _name (str)
        , _active (false)
 {
+       if (!selection_connected) {
+               /* this is all static, connect it only once (and early), for all ControlProtocols */
+
+               StripableSelectionChanged.connect_same_thread (selection_connection, boost::bind (&ControlProtocol::stripable_selection_changed, _1));
+               selection_connected = true;
+       }
 }
 
 ControlProtocol::~ControlProtocol ()
@@ -77,82 +92,18 @@ ControlProtocol::set_active (bool yn)
 void
 ControlProtocol::next_track (uint32_t initial_id)
 {
-       uint32_t limit = session->nroutes();
-       boost::shared_ptr<Route> cr = route_table[0];
-       uint32_t id;
-
-       if (cr) {
-               id = cr->remote_control_id ();
-       } else {
-               id = 0;
-       }
-
-       if (id == limit) {
-               id = 0;
-       } else {
-               id++;
-       }
-
-       while (id <= limit) {
-               if ((cr = session->route_by_remote_id (id)) != 0) {
-                       break;
-               }
-               id++;
-       }
-
-       if (id >= limit) {
-               id = 0;
-               while (id != initial_id) {
-                       if ((cr = session->route_by_remote_id (id)) != 0) {
-                               break;
-                       }
-                       id++;
-               }
-       }
-
-       route_table[0] = cr;
+       // STRIPABLE route_table[0] = _session->get_nth_stripable (++initial_id, RemoteControlID::Route);
 }
 
 void
 ControlProtocol::prev_track (uint32_t initial_id)
 {
-       uint32_t limit = session->nroutes();
-       boost::shared_ptr<Route> cr = route_table[0];
-       int32_t id;
-
-       if (cr) {
-               id = cr->remote_control_id ();
-       } else {
-               id = 0;
-       }
-
-       if (id == 0) {
-               id = limit;
-       } else {
-               id--;
-       }
-
-       while (id >= 0) {
-               if ((cr = session->route_by_remote_id (id)) != 0) {
-                       break;
-               }
-               id--;
-       }
-
-       if (id < 0) {
-               uint32_t i = limit;
-               while (i > initial_id) {
-                       if ((cr = session->route_by_remote_id (i)) != 0) {
-                               break;
-                       }
-                       i--;
-               }
+       if (!initial_id) {
+               return;
        }
-
-       route_table[0] = cr;
+       // STRIPABLE route_table[0] = _session->get_nth_stripable (--initial_id, RemoteControlID::Route);
 }
 
-
 void
 ControlProtocol::set_route_table_size (uint32_t size)
 {
@@ -176,6 +127,7 @@ ControlProtocol::set_route_table (uint32_t table_index, boost::shared_ptr<ARDOUR
 bool
 ControlProtocol::set_route_table (uint32_t table_index, uint32_t remote_control_id)
 {
+#if 0 // STRIPABLE
        boost::shared_ptr<Route> r = session->route_by_remote_id (remote_control_id);
 
        if (!r) {
@@ -183,7 +135,7 @@ ControlProtocol::set_route_table (uint32_t table_index, uint32_t remote_control_
        }
 
        set_route_table (table_index, r);
-
+#endif
        return true;
 }
 
@@ -199,7 +151,7 @@ ControlProtocol::route_set_rec_enable (uint32_t table_index, bool yn)
        boost::shared_ptr<AudioTrack> at = boost::dynamic_pointer_cast<AudioTrack>(r);
 
        if (at) {
-               at->set_record_enabled (yn, Controllable::NoGroup);
+               at->rec_enable_control()->set_value (1.0, Controllable::UseGroup);
        }
 }
 
@@ -215,7 +167,7 @@ ControlProtocol::route_get_rec_enable (uint32_t table_index)
        boost::shared_ptr<AudioTrack> at = boost::dynamic_pointer_cast<AudioTrack>(r);
 
        if (at) {
-               return at->record_enabled ();
+               return at->rec_enable_control()->get_value();
        }
 
        return false;
@@ -248,7 +200,7 @@ ControlProtocol::route_set_gain (uint32_t table_index, float gain)
        boost::shared_ptr<Route> r = route_table[table_index];
 
        if (r != 0) {
-               r->set_gain (gain, Controllable::UseGroup);
+               r->gain_control()->set_value (gain, Controllable::UseGroup);
        }
 }
 
@@ -298,7 +250,7 @@ ControlProtocol::route_get_muted (uint32_t table_index)
                return false;
        }
 
-       return r->muted ();
+       return r->mute_control()->muted ();
 }
 
 void
@@ -311,7 +263,7 @@ ControlProtocol::route_set_muted (uint32_t table_index, bool yn)
        boost::shared_ptr<Route> r = route_table[table_index];
 
        if (r != 0) {
-               r->set_mute (yn, Controllable::UseGroup);
+               r->mute_control()->set_value (yn ? 1.0 : 0.0, Controllable::UseGroup);
        }
 }
 
@@ -342,7 +294,7 @@ ControlProtocol::route_set_soloed (uint32_t table_index, bool yn)
        boost::shared_ptr<Route> r = route_table[table_index];
 
        if (r != 0) {
-               r->set_solo (yn, Controllable::UseGroup);
+               r->solo_control()->set_value (yn ? 1.0 : 0.0, Controllable::UseGroup);
        }
 }
 
@@ -390,3 +342,37 @@ ControlProtocol::set_state (XMLNode const & node, int /* version */)
 
        return 0;
 }
+
+boost::shared_ptr<Stripable>
+ControlProtocol::first_selected_stripable ()
+{
+       Glib::Threads::Mutex::Lock lm (first_selected_mutex);
+       return _first_selected_stripable.lock();
+}
+
+void
+ControlProtocol::set_first_selected_stripable (boost::shared_ptr<Stripable> s)
+{
+       Glib::Threads::Mutex::Lock lm (first_selected_mutex);
+       _first_selected_stripable = s;
+}
+
+void
+ControlProtocol::stripable_selection_changed (StripableNotificationListPtr sp)
+{
+       bool had_selection = !_last_selected.empty();
+
+       _last_selected = *sp;
+
+       {
+               Glib::Threads::Mutex::Lock lm (first_selected_mutex);
+
+               if (!_last_selected.empty()) {
+                       if (!had_selection) {
+                               _first_selected_stripable = _last_selected.front().lock();
+                       }
+               } else {
+                       _first_selected_stripable = boost::weak_ptr<Stripable>();
+               }
+       }
+}