don't actually use a method from libardourcp inside libardour; use a PBD::Signal...
[ardour.git] / libs / surfaces / control_protocol / control_protocol.cc
index dd9adc206e87092dedbba97bc91efcce60879354..e094e9b6cc4a723c3410d4586962cb54bd812771 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2006 Paul Davis 
+    Copyright (C) 2006 Paul Davis
 
     This program is free software; you can redistribute it
     and/or modify it under the terms of the GNU Lesser
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id$
 */
 
-#include <ardour/session.h>
-#include <ardour/route.h>
-#include <ardour/audio_track.h>
+#include "pbd/convert.h"
+#include "pbd/error.h"
 
-#include <control_protocol/control_protocol.h>
+#include "ardour/control_protocol_manager.h"
+#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"
+#include "ardour/amp.h"
+#include "control_protocol/control_protocol.h"
 
 using namespace ARDOUR;
 using namespace std;
-
-sigc::signal<void> ControlProtocol::ZoomToSession;
-sigc::signal<void> ControlProtocol::ZoomOut;
-sigc::signal<void> ControlProtocol::ZoomIn;
-sigc::signal<void> ControlProtocol::Enter;
-sigc::signal<void,float> ControlProtocol::ScrollTimeline;
+using namespace PBD;
+
+Signal0<void>       ControlProtocol::ZoomToSession;
+Signal0<void>       ControlProtocol::ZoomOut;
+Signal0<void>       ControlProtocol::ZoomIn;
+Signal0<void>       ControlProtocol::Enter;
+Signal0<void>       ControlProtocol::Undo;
+Signal0<void>       ControlProtocol::Redo;
+Signal1<void,float> ControlProtocol::ScrollTimeline;
+Signal1<void,uint32_t> ControlProtocol::GotoView;
+Signal0<void> ControlProtocol::CloseDialog;
+PBD::Signal0<void> ControlProtocol::VerticalZoomInAll;
+PBD::Signal0<void> ControlProtocol::VerticalZoomOutAll;
+PBD::Signal0<void> ControlProtocol::VerticalZoomInSelected;
+PBD::Signal0<void> ControlProtocol::VerticalZoomOutSelected;
+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;
+
+Glib::Threads::Mutex ControlProtocol::special_stripable_mutex;
+boost::weak_ptr<Stripable> ControlProtocol::_first_selected_stripable;
+boost::weak_ptr<Stripable> ControlProtocol::_leftmost_mixer_stripable;
+StripableNotificationList ControlProtocol::_last_selected;
+PBD::ScopedConnection ControlProtocol::selection_connection;
+bool ControlProtocol::selection_connected = false;
+
+const std::string ControlProtocol::state_node_name ("Protocol");
 
 ControlProtocol::ControlProtocol (Session& s, string str)
-       : BasicUI (s),
-         _name (str)
+       : BasicUI (s)
+       , _name (str)
+       , _active (false)
 {
-       _active = false;
+       if (!selection_connected) {
+               /* this is all static, connect it only once (and early), for all ControlProtocols */
+               ControlProtocolManager::StripableSelectionChanged.connect_same_thread (selection_connection, boost::bind (&ControlProtocol::notify_stripable_selection_changed, _1));
+               selection_connected = true;
+       }
 }
 
 ControlProtocol::~ControlProtocol ()
 {
 }
 
+int
+ControlProtocol::set_active (bool yn)
+{
+       _active = yn;
+       return 0;
+}
+
 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() - 1;
-       boost::shared_ptr<Route> cr = route_table[0];
-       uint32_t id;
-
-       if (cr) {
-               id = cr->remote_control_id ();
-       } else {
-               id = 0;
-       }
-
-       if (id == 0) {
-               id = session->nroutes() - 1;
-       } else {
-               id--;
-       }
-
-       while (id >= 0) {
-               if ((cr = session->route_by_remote_id (id)) != 0) {
-                       break;
-               }
-               id--;
-       }
-
-       if (id < 0) {
-               id = limit;
-               while (id > initial_id) {
-                       if ((cr = session->route_by_remote_id (id)) != 0) {
-                               break;
-                       }
-                       id--;
-               }
+       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)
 {
@@ -138,7 +118,7 @@ ControlProtocol::set_route_table (uint32_t table_index, boost::shared_ptr<ARDOUR
        if (table_index >= route_table.size()) {
                return;
        }
-       
+
        route_table[table_index] = r;
 
        // XXX SHAREDPTR need to handle r->GoingAway
@@ -147,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) {
@@ -154,7 +135,7 @@ ControlProtocol::set_route_table (uint32_t table_index, uint32_t remote_control_
        }
 
        set_route_table (table_index, r);
-
+#endif
        return true;
 }
 
@@ -170,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_enable (yn, this);
+               at->rec_enable_control()->set_value (1.0, Controllable::UseGroup);
        }
 }
 
@@ -186,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;
@@ -206,7 +187,7 @@ ControlProtocol::route_get_gain (uint32_t table_index)
                return 0.0f;
        }
 
-       return r->gain ();
+       return r->gain_control()->get_value();
 }
 
 void
@@ -217,9 +198,9 @@ 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, this);
+               r->gain_control()->set_value (gain, Controllable::UseGroup);
        }
 }
 
@@ -236,7 +217,7 @@ ControlProtocol::route_get_effective_gain (uint32_t table_index)
                return 0.0f;
        }
 
-       return r->effective_gain ();
+       return r->amp()->gain_control()->get_value();
 }
 
 
@@ -253,10 +234,9 @@ ControlProtocol::route_get_peak_input_power (uint32_t table_index, uint32_t whic
                return 0.0f;
        }
 
-       return r->peak_input_power (which_input);
+       return r->peak_meter()->meter_level (which_input, MeterPeak);
 }
 
-
 bool
 ControlProtocol::route_get_muted (uint32_t table_index)
 {
@@ -270,7 +250,7 @@ ControlProtocol::route_get_muted (uint32_t table_index)
                return false;
        }
 
-       return r->muted ();
+       return r->mute_control()->muted ();
 }
 
 void
@@ -283,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, this);
+               r->mute_control()->set_value (yn ? 1.0 : 0.0, Controllable::UseGroup);
        }
 }
 
@@ -314,7 +294,8 @@ 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, this);
+               r->solo_control()->set_value (yn ? 1.0 : 0.0, Controllable::UseGroup); // XXX does not propagate
+               //_session->set_control (r->solo_control(), yn ? 1.0 : 0.0, Controllable::UseGroup); // << correct way, needs a session ptr
        }
 }
 
@@ -334,3 +315,78 @@ ControlProtocol:: route_get_name (uint32_t table_index)
        return r->name();
 }
 
+list<boost::shared_ptr<Bundle> >
+ControlProtocol::bundles ()
+{
+       return list<boost::shared_ptr<Bundle> > ();
+}
+
+XMLNode&
+ControlProtocol::get_state ()
+{
+       XMLNode* node = new XMLNode (state_node_name);
+
+       node->set_property ("name", _name);
+       node->set_property ("feedback", get_feedback());
+
+       return *node;
+}
+
+int
+ControlProtocol::set_state (XMLNode const & node, int /* version */)
+{
+       bool feedback;
+       if (node.get_property ("feedback", feedback)) {
+               set_feedback (feedback);
+       }
+
+       return 0;
+}
+
+boost::shared_ptr<Stripable>
+ControlProtocol::first_selected_stripable ()
+{
+       Glib::Threads::Mutex::Lock lm (special_stripable_mutex);
+       return _first_selected_stripable.lock();
+}
+
+boost::shared_ptr<Stripable>
+ControlProtocol::leftmost_mixer_stripable ()
+{
+       Glib::Threads::Mutex::Lock lm (special_stripable_mutex);
+       return _leftmost_mixer_stripable.lock();
+}
+
+void
+ControlProtocol::set_leftmost_mixer_stripable (boost::shared_ptr<Stripable> s)
+{
+       Glib::Threads::Mutex::Lock lm (special_stripable_mutex);
+       _leftmost_mixer_stripable = s;
+}
+
+void
+ControlProtocol::set_first_selected_stripable (boost::shared_ptr<Stripable> s)
+{
+       Glib::Threads::Mutex::Lock lm (special_stripable_mutex);
+       _first_selected_stripable = s;
+}
+
+void
+ControlProtocol::notify_stripable_selection_changed (StripableNotificationListPtr sp)
+{
+       bool had_selection = !_last_selected.empty();
+
+       _last_selected = *sp;
+
+       {
+               Glib::Threads::Mutex::Lock lm (special_stripable_mutex);
+
+               if (!_last_selected.empty()) {
+                       if (!had_selection) {
+                               _first_selected_stripable = _last_selected.front().lock();
+                       }
+               } else {
+                       _first_selected_stripable = boost::weak_ptr<Stripable>();
+               }
+       }
+}