MCP:maybe functioning button binding stuff, plus save-to-disk-on-change (still needs...
[ardour.git] / libs / surfaces / mackie / mackie_control_protocol.cc
index c0ad5fde1f6428398e9ea83bcca572a7e98c8625..e75d1824e2f2afb8551340f1d90bc1ec2a10294b 100644 (file)
@@ -57,6 +57,7 @@
 
 #include "midi_byte_array.h"
 #include "mackie_control_exception.h"
+#include "device_profile.h"
 #include "surface_port.h"
 #include "surface.h"
 #include "strip.h"
@@ -80,8 +81,8 @@ using namespace Glib;
 
 const int MackieControlProtocol::MODIFIER_OPTION = 0x1;
 const int MackieControlProtocol::MODIFIER_CONTROL = 0x2;
-const int MackieControlProtocol::MODIFIER_SHIFT = 0x3;
-const int MackieControlProtocol::MODIFIER_CMDALT = 0x4;
+const int MackieControlProtocol::MODIFIER_SHIFT = 0x4;
+const int MackieControlProtocol::MODIFIER_CMDALT = 0x8;
 
 MackieControlProtocol* MackieControlProtocol::_instance = 0;
 
@@ -108,7 +109,10 @@ MackieControlProtocol::MackieControlProtocol (Session& session)
        DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::MackieControlProtocol\n");
 
        DeviceInfo::reload_device_info ();
+       DeviceProfile::reload_device_profiles ();
+
        set_device (Config->get_mackie_device_name());
+       set_profile (Config->get_mackie_device_profile());
 
        AudioEngine::instance()->PortConnectedOrDisconnected.connect (
                audio_engine_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::port_connected_or_disconnected, this, _2, _4, _5),
@@ -506,6 +510,23 @@ 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()) {
+               return;
+       }
+       
+       _device_profile = d->second;
+}      
+
 void
 MackieControlProtocol::set_device (const string& device_name)
 {
@@ -615,12 +636,6 @@ MackieControlProtocol::get_state()
        os << _current_initial_bank;
        node->add_property (X_("bank"), os.str());
 
-       for (uint32_t n = 0; n < 16; ++n) {
-               ostringstream s;
-               s << string_compose ("f%1-action", n+1);
-               node->add_property (s.str().c_str(), f_action (n));
-       }
-
        return *node;
 }
 
@@ -642,23 +657,6 @@ MackieControlProtocol::set_state (const XMLNode & node, int /*version*/)
                }
        }
 
-       _f_actions.clear ();
-       _f_actions.resize (16);
-
-       for (uint32_t n = 0; n < 16; ++n) {
-               string action;
-               if ((prop = node.property (string_compose ("f%1-action", n+1))) != 0) {
-                       action = prop->value();
-               }
-
-               if (action.empty()) {
-                       /* default action if nothing is specified */
-                       action = string_compose ("Editor/goto-visual-state-%1", n+1);
-               }
-
-               _f_actions[n] = action;
-       }
-
        return retval;
 }
 
@@ -923,47 +921,6 @@ MackieControlProtocol::stop ()
        return 0;
 }
 
-/** Add a timeout so that a control's in_use flag will be reset some time in the future.
- *  @param in_use_control the control whose in_use flag to reset.
- *  @param touch_control a touch control to emit an event for, or 0.
- */
-void
-MackieControlProtocol::add_in_use_timeout (Surface& surface, Control& in_use_control, boost::weak_ptr<AutomationControl> touched)
-{
-       Glib::RefPtr<Glib::TimeoutSource> timeout (Glib::TimeoutSource::create (250)); // milliseconds
-
-       in_use_control.in_use_connection.disconnect ();
-       in_use_control.in_use_connection = timeout->connect (
-               sigc::bind (sigc::mem_fun (*this, &MackieControlProtocol::control_in_use_timeout), &surface, &in_use_control, touched));
-       
-       timeout->attach (main_loop()->get_context());
-
-       DEBUG_TRACE (DEBUG::MackieControl, string_compose ("timeout queued for surface %1, control %2\n",
-                                                          surface.number(), &in_use_control));}
-
-/** Handle timeouts to reset in_use for controls that can't
- *  do this by themselves (e.g. pots, and faders without touch support).
- *  @param in_use_control the control whose in_use flag to reset.
- *  @param touch_control a touch control to emit an event for, or 0.
- */
-bool
-MackieControlProtocol::control_in_use_timeout (Surface* surface, Control* in_use_control, boost::weak_ptr<AutomationControl> wtouched)
-{
-       boost::shared_ptr<AutomationControl> touched (wtouched.lock());
-
-       DEBUG_TRACE (DEBUG::MackieControl, string_compose ("timeout elapsed for surface %1, control %2\n", surface->number(), in_use_control));
-
-       in_use_control->set_in_use (false);
-
-       if (touched) {
-               /* end the touch, and mark the end */
-               touched->stop_touch (session->transport_frame(), true);
-       }
-       
-       // only call this method once from the timer
-       return false;
-}
-
 void 
 MackieControlProtocol::update_led (Surface& surface, Button& button, Mackie::LedState ls)
 {
@@ -1080,6 +1037,22 @@ MackieControlProtocol::handle_button_event (Surface& surface, Button& button, Bu
        
        DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Handling %1 for button %2\n", (bs == press ? "press" : "release"), button.id()));
 
+       /* check profile first */
+       
+       string action = _device_profile.get_button_action (button.bid(), _modifier_state);
+       
+       if (!action.empty()) {
+               /* if there is a bound action for this button, and this is a press event,
+                  carry out the action. If its a release event, do nothing since we 
+                  don't bind to them at all but don't want any other handling to 
+                  occur either.
+               */
+               if (bs == press) {
+                       access_action (action);
+               }
+               return;
+       }
+
        /* lookup using the device-INDEPENDENT button ID */
 
        ButtonMap::iterator b = button_map.find (button.bid());
@@ -1140,25 +1113,6 @@ MackieControlProtocol::clear_ports ()
        port_sources.clear ();
 }
 
-string
-MackieControlProtocol::f_action (uint32_t fn, uint32_t /* modifier */)
-{
-       if (fn >= _f_actions.size()) {
-               return string();
-       }
-
-       return _f_actions[fn];
-}
-
-void
-MackieControlProtocol::f_press (uint32_t fn)
-{
-       string action = f_action (0);
-       if (!action.empty()) {
-               access_action (action);
-       }
-}
-
 void
 MackieControlProtocol::set_view_mode (ViewMode m)
 {