a more reliable/robust/less complex version of previous commit
[ardour.git] / libs / surfaces / generic_midi / generic_midi_control_protocol.h
index c9d11c3e096754b23c736ceb42ade1d22bbf6889..d3709c5374d8a1af12525e871da5c27ddbcc72f8 100644 (file)
 #include <glibmm/threads.h>
 
 #include "ardour/types.h"
+#include "ardour/port.h"
 
 #include "control_protocol/control_protocol.h"
 
 namespace PBD {
        class Controllable;
-       class ControllableDescriptor;
-}      
+}
 
 namespace ARDOUR {
-       class Session;
+       class AsyncMIDIPort;
+       class ControllableDescriptor;
        class MidiPort;
+       class Session;
 }
 
 namespace MIDI {
@@ -53,14 +55,15 @@ class GenericMidiControlProtocol : public ARDOUR::ControlProtocol {
        int set_active (bool yn);
        static bool probe() { return true; }
 
-        MIDI::Port* input_port () const { return _input_port; }
-        MIDI::Port* output_port () const { return _output_port; }
+       boost::shared_ptr<ARDOUR::Port> input_port () const;
+       boost::shared_ptr<ARDOUR::Port> output_port () const;
+
        void set_feedback_interval (ARDOUR::microseconds_t);
 
        int set_feedback (bool yn);
        bool get_feedback () const;
 
-        boost::shared_ptr<PBD::Controllable> lookup_controllable (const PBD::ControllableDescriptor&) const;
+        boost::shared_ptr<PBD::Controllable> lookup_controllable (const ARDOUR::ControllableDescriptor&) const;
 
        XMLNode& get_state ();
        int set_state (const XMLNode&, int version);
@@ -73,7 +76,7 @@ class GenericMidiControlProtocol : public ARDOUR::ControlProtocol {
        void drop_bindings ();
 
        void check_used_event (int, int);
-       
+
        std::string current_binding() const { return _current_binding; }
 
        struct MapInfo {
@@ -89,7 +92,7 @@ class GenericMidiControlProtocol : public ARDOUR::ControlProtocol {
        void prev_bank ();
 
        void set_motorised (bool);
-       
+
        bool motorised () const {
                return _motorised;
        }
@@ -100,9 +103,12 @@ class GenericMidiControlProtocol : public ARDOUR::ControlProtocol {
                return _threshold;
        }
 
+       PBD::Signal0<void> ConnectionChange;
+
   private:
-        MIDI::Port* _input_port;
-        MIDI::Port* _output_port;
+       boost::shared_ptr<ARDOUR::AsyncMIDIPort> _input_port;
+       boost::shared_ptr<ARDOUR::AsyncMIDIPort> _output_port;
+
        ARDOUR::microseconds_t _feedback_interval;
        ARDOUR::microseconds_t last_feedback_time;
 
@@ -119,7 +125,16 @@ class GenericMidiControlProtocol : public ARDOUR::ControlProtocol {
        typedef std::list<MIDIAction*> MIDIActions;
        MIDIActions actions;
 
-       typedef std::pair<MIDIControllable*,PBD::ScopedConnection> MIDIPendingControllable;
+       struct MIDIPendingControllable {
+               MIDIControllable* mc;
+               bool own_mc;
+               PBD::ScopedConnection connection;
+
+               MIDIPendingControllable (MIDIControllable* c, bool omc)
+                       : mc (c)
+                       , own_mc (omc)
+               {}
+       };
        typedef std::list<MIDIPendingControllable* > MIDIPendingControllables;
        MIDIPendingControllables pending_controllables;
         Glib::Threads::Mutex controllables_lock;
@@ -140,6 +155,16 @@ class GenericMidiControlProtocol : public ARDOUR::ControlProtocol {
        void reset_controllables ();
        void drop_all ();
 
+       enum ConnectionState {
+               InputConnected = 0x1,
+               OutputConnected = 0x2
+       };
+
+       int connection_state;
+       bool connection_handler (boost::weak_ptr<ARDOUR::Port>, std::string name1, boost::weak_ptr<ARDOUR::Port>, std::string name2, bool yn);
+       PBD::ScopedConnection port_connection;
+       void connected();
+
        std::string _current_binding;
        uint32_t _bank_size;
        uint32_t _current_bank;
@@ -153,6 +178,8 @@ class GenericMidiControlProtocol : public ARDOUR::ControlProtocol {
 
        mutable void *gui;
        void build_gui ();
+
+
 };
 
 #endif /* ardour_generic_midi_control_protocol_h */