Move MIDI control port ownership into the MIDI Manager, since control port state...
[ardour.git] / libs / surfaces / control_protocol / control_protocol.cc
index 10295bc698d522d970d045db1b5d3c8c170bc11f..62214e6429031da68b35d1ca7b6357911a49bbda 100644 (file)
     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/error.h"
 
-#include <control_protocol/control_protocol.h>
+#include "ardour/session.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;
+using namespace PBD;
 
-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;
+Signal0<void>       ControlProtocol::ZoomToSession;
+Signal0<void>       ControlProtocol::ZoomOut;
+Signal0<void>       ControlProtocol::ZoomIn;
+Signal0<void>       ControlProtocol::Enter;
+Signal1<void,float> ControlProtocol::ScrollTimeline;
 
-ControlProtocol::ControlProtocol (Session& s, string str)
+ControlProtocol::ControlProtocol (Session& s, string str, EventLoop* evloop)
        : BasicUI (s),
          _name (str)
 {
+       if (evloop) {
+               _own_event_loop = false;
+               _event_loop = evloop;
+       } else {
+               _own_event_loop = true;
+               fatal << "programming error: cannot create control protocols without an existing event loop (yet)" << endmsg;
+               /*NOTREACHED*/
+       }
+
        _active = false;
+       
+       session->RouteAdded.connect (*this, MISSING_INVALIDATOR, boost::protect (boost::bind (&ControlProtocol::add_strip, this, _1)), _event_loop);
 }
 
 ControlProtocol::~ControlProtocol ()
 {
 }
 
+void
+ControlProtocol::add_strip (ARDOUR::RouteList&)
+{
+       route_list_changed();
+}
+       
 void
 ControlProtocol::next_track (uint32_t initial_id)
 {
        uint32_t limit = session->nroutes();
-       Route* cr = route_table[0];
+       boost::shared_ptr<Route> cr = route_table[0];
        uint32_t id;
 
        if (cr) {
@@ -64,14 +84,14 @@ ControlProtocol::next_track (uint32_t initial_id)
                id++;
        }
 
-       while (id < limit) {
+       while (id <= limit) {
                if ((cr = session->route_by_remote_id (id)) != 0) {
                        break;
                }
                id++;
        }
 
-       if (id == limit) {
+       if (id >= limit) {
                id = 0;
                while (id != initial_id) {
                        if ((cr = session->route_by_remote_id (id)) != 0) {
@@ -87,9 +107,9 @@ ControlProtocol::next_track (uint32_t initial_id)
 void
 ControlProtocol::prev_track (uint32_t initial_id)
 {
-       uint32_t limit = session->nroutes() - 1;
-       Route* cr = route_table[0];
-       uint32_t id;
+       uint32_t limit = session->nroutes();
+       boost::shared_ptr<Route> cr = route_table[0];
+       int32_t id;
 
        if (cr) {
                id = cr->remote_control_id ();
@@ -98,7 +118,7 @@ ControlProtocol::prev_track (uint32_t initial_id)
        }
 
        if (id == 0) {
-               id = session->nroutes() - 1;
+               id = limit;
        } else {
                id--;
        }
@@ -111,12 +131,12 @@ ControlProtocol::prev_track (uint32_t initial_id)
        }
 
        if (id < 0) {
-               id = limit;
-               while (id > initial_id) {
-                       if ((cr = session->route_by_remote_id (id)) != 0) {
+               uint32_t i = limit;
+               while (i > initial_id) {
+                       if ((cr = session->route_by_remote_id (i)) != 0) {
                                break;
                        }
-                       id--;
+                       i--;
                }
        }
 
@@ -128,29 +148,32 @@ void
 ControlProtocol::set_route_table_size (uint32_t size)
 {
        while (route_table.size() < size) {
-               route_table.push_back (0);
+               route_table.push_back (boost::shared_ptr<Route> ((Route*) 0));
        }
 }
 
 void
-ControlProtocol::set_route_table (uint32_t table_index, ARDOUR::Route*)
+ControlProtocol::set_route_table (uint32_t table_index, boost::shared_ptr<ARDOUR::Route> r)
 {
+       if (table_index >= route_table.size()) {
+               return;
+       }
+       
+       route_table[table_index] = r;
+
+       // XXX SHAREDPTR need to handle r->GoingAway
 }
 
 bool
 ControlProtocol::set_route_table (uint32_t table_index, uint32_t remote_control_id)
 {
-       if (table_index >= route_table.size()) {
-               return false;
-       }
-               
-       Route* r = session->route_by_remote_id (remote_control_id);
+       boost::shared_ptr<Route> r = session->route_by_remote_id (remote_control_id);
 
        if (!r) {
                return false;
        }
-       
-       route_table[table_index] = r;
+
+       set_route_table (table_index, r);
 
        return true;
 }
@@ -162,9 +185,9 @@ ControlProtocol::route_set_rec_enable (uint32_t table_index, bool yn)
                return;
        }
 
-       Route* r = route_table[table_index];
+       boost::shared_ptr<Route> r = route_table[table_index];
 
-       AudioTrack* at = dynamic_cast<AudioTrack*>(r);
+       boost::shared_ptr<AudioTrack> at = boost::dynamic_pointer_cast<AudioTrack>(r);
 
        if (at) {
                at->set_record_enable (yn, this);
@@ -178,9 +201,9 @@ ControlProtocol::route_get_rec_enable (uint32_t table_index)
                return false;
        }
 
-       Route* r = route_table[table_index];
+       boost::shared_ptr<Route> r = route_table[table_index];
 
-       AudioTrack* at = dynamic_cast<AudioTrack*>(r);
+       boost::shared_ptr<AudioTrack> at = boost::dynamic_pointer_cast<AudioTrack>(r);
 
        if (at) {
                return at->record_enabled ();
@@ -197,13 +220,13 @@ ControlProtocol::route_get_gain (uint32_t table_index)
                return 0.0f;
        }
 
-       Route* r = route_table[table_index];
+       boost::shared_ptr<Route> r = route_table[table_index];
 
        if (r == 0) {
                return 0.0f;
        }
 
-       return r->gain ();
+       return r->amp()->gain ();
 }
 
 void
@@ -213,7 +236,7 @@ ControlProtocol::route_set_gain (uint32_t table_index, float gain)
                return;
        }
 
-       Route* r = route_table[table_index];
+       boost::shared_ptr<Route> r = route_table[table_index];
        
        if (r != 0) {
                r->set_gain (gain, this);
@@ -227,13 +250,13 @@ ControlProtocol::route_get_effective_gain (uint32_t table_index)
                return 0.0f;
        }
 
-       Route* r = route_table[table_index];
+       boost::shared_ptr<Route> r = route_table[table_index];
 
        if (r == 0) {
                return 0.0f;
        }
 
-       return r->effective_gain ();
+       return r->amp()->gain_control()->get_value();
 }
 
 
@@ -244,13 +267,13 @@ ControlProtocol::route_get_peak_input_power (uint32_t table_index, uint32_t whic
                return 0.0f;
        }
 
-       Route* r = route_table[table_index];
+       boost::shared_ptr<Route> r = route_table[table_index];
 
        if (r == 0) {
                return 0.0f;
        }
 
-       return r->peak_input_power (which_input);
+       return r->peak_meter().peak_power (which_input);
 }
 
 
@@ -261,7 +284,7 @@ ControlProtocol::route_get_muted (uint32_t table_index)
                return false;
        }
 
-       Route* r = route_table[table_index];
+       boost::shared_ptr<Route> r = route_table[table_index];
 
        if (r == 0) {
                return false;
@@ -277,7 +300,7 @@ ControlProtocol::route_set_muted (uint32_t table_index, bool yn)
                return;
        }
 
-       Route* r = route_table[table_index];
+       boost::shared_ptr<Route> r = route_table[table_index];
 
        if (r != 0) {
                r->set_mute (yn, this);
@@ -292,7 +315,7 @@ ControlProtocol::route_get_soloed (uint32_t table_index)
                return false;
        }
 
-       Route* r = route_table[table_index];
+       boost::shared_ptr<Route> r = route_table[table_index];
 
        if (r == 0) {
                return false;
@@ -308,7 +331,7 @@ ControlProtocol::route_set_soloed (uint32_t table_index, bool yn)
                return;
        }
 
-       Route* r = route_table[table_index];
+       boost::shared_ptr<Route> r = route_table[table_index];
 
        if (r != 0) {
                r->set_solo (yn, this);
@@ -322,7 +345,7 @@ ControlProtocol:: route_get_name (uint32_t table_index)
                return "";
        }
 
-       Route* r = route_table[table_index];
+       boost::shared_ptr<Route> r = route_table[table_index];
 
        if (r == 0) {
                return "";