OSC: Further fixes so multiple controls can touch at once.
[ardour.git] / libs / surfaces / osc / osc.cc
index 2553499ad15c9c55efc8486895cd943f450a11c4..70b02a7501c324e83cd3f053283c434bfd0591ef 100644 (file)
@@ -862,6 +862,10 @@ OSC::catchall (const char *path, const char* types, lo_arg **argv, int argc, lo_
        if (strstr (path, "/automation")) {
                ret = set_automation (path, argv, argc, msg);
 
+       } else
+       if (strstr (path, "/touch")) {
+               ret = touch_detect (path, argv, argc, msg);
+
        } else
        if (len >= 17 && !strcmp (&path[len-15], "/#current_value")) {
                current_value_query (path, len, argv, argc, msg);
@@ -2207,24 +2211,22 @@ OSC::set_automation (const char *path, lo_arg **argv, int argc, lo_message msg)
        uint32_t ctr = 0;
        uint32_t aut = 0;
 
+       if (argc) {
+               if (argv[argc - 1]->f) {
+                       aut = (int)argv[argc - 1]->f;
+               } else {
+                       aut = argv[argc - 1]->i;
+               }
+       }
+
        //parse path first to find stripable
        if (!strncmp (path, "/strip/", 7)) {
                // find ssid and stripable
                if (argc > 1) {
                        strp = get_strip (argv[0]->i, get_address (msg));
-                       if (argv[1]->f) {
-                               aut = (int)argv[1]->f;
-                       } else {
-                               aut = argv[1]->i;
-                       }
                } else {
                        uint32_t ssid = atoi (&(strrchr (path, '/' ))[1]);
                        strp = get_strip (ssid, get_address (msg));
-                       if (argv[0]->f) {
-                               aut = (int)argv[0]->f;
-                       } else {
-                               aut = argv[0]->i;
-                       }
                }
                ctr = 7;
        } else if (!strncmp (path, "/select/", 8)) {
@@ -2233,11 +2235,6 @@ OSC::set_automation (const char *path, lo_arg **argv, int argc, lo_message msg)
                } else {
                        strp = ControlProtocol::first_selected_stripable();
                }
-               if (argv[0]->f) {
-                       aut = (int)argv[0]->f;
-               } else {
-                       aut = argv[0]->i;
-               }
                ctr = 8;
        } else {
                return ret;
@@ -2283,6 +2280,93 @@ OSC::set_automation (const char *path, lo_arg **argv, int argc, lo_message msg)
        return ret;
 }
 
+int
+OSC::touch_detect (const char *path, lo_arg **argv, int argc, lo_message msg)
+{
+       if (!session) return -1;
+
+       int ret = 1;
+       OSCSurface *sur = get_surface(get_address (msg));
+       boost::shared_ptr<Stripable> strp = boost::shared_ptr<Stripable>();
+       uint32_t ctr = 0;
+       uint32_t touch = 0;
+
+       if (argc) {
+               if (argv[argc - 1]->f) {
+                       touch = (int)argv[argc - 1]->f;
+               } else {
+                       touch = argv[argc - 1]->i;
+               }
+       }
+
+       //parse path first to find stripable
+       if (!strncmp (path, "/strip/", 7)) {
+               // find ssid and stripable
+               if (argc > 1) {
+                       strp = get_strip (argv[0]->i, get_address (msg));
+               } else {
+                       uint32_t ssid = atoi (&(strrchr (path, '/' ))[1]);
+                       strp = get_strip (ssid, get_address (msg));
+               }
+               ctr = 7;
+       } else if (!strncmp (path, "/select/", 8)) {
+               if (sur->expand_enable && sur->expand) {
+                       strp = get_strip (sur->expand, get_address (msg));
+               } else {
+                       strp = ControlProtocol::first_selected_stripable();
+               }
+               ctr = 8;
+       } else {
+               return ret;
+       }
+       if (strp) {
+               boost::shared_ptr<AutomationControl> control = boost::shared_ptr<AutomationControl>();
+               // other automatable controls can be added by repeating the next 6.5 lines
+               if ((!strncmp (&path[ctr], "fader", 5)) || (!strncmp (&path[ctr], "gain", 4))) {
+                       if (strp->gain_control ()) {
+                               control = strp->gain_control ();
+                       } else {
+                               PBD::warning << "No fader for this strip" << endmsg;
+                       }
+               } else {
+                       PBD::warning << "Automation not available for " << path << endmsg;
+               }
+
+               if (control) {
+                       if (touch) {
+                               //start touch
+                               if (control->automation_state() == Touch && !control->touching ()) {
+                                       control->start_touch (control->session().transport_frame());
+                               }
+                       } else {
+                               // end touch
+                               control->stop_touch (true, control->session().transport_frame());
+                       }
+                       // just in case some crazy surface starts sending control values before touch
+                       FakeTouchMap::iterator x = _touch_timeout.find(control);
+                       if (x != _touch_timeout.end()) {
+                               _touch_timeout.erase (x);
+                       }
+               }
+       }
+
+       return ret;
+}
+
+int
+OSC::fake_touch (boost::shared_ptr<ARDOUR::AutomationControl> ctrl)
+{
+       if (ctrl) {
+               //start touch
+               if (ctrl->automation_state() == Touch && !ctrl->touching ()) {
+               ctrl->start_touch (ctrl->session().transport_frame());
+               _touch_timeout[ctrl] = 10;
+               }
+       }
+
+       return 0;
+}
+
 int
 OSC::route_mute (int ssid, int yn, lo_message msg)
 {
@@ -2768,6 +2852,7 @@ OSC::route_set_gain_abs (int ssid, float level, lo_message msg)
 
        if (s) {
                if (s->gain_control()) {
+                       fake_touch (s->gain_control());
                        s->gain_control()->set_value (level, PBD::Controllable::NoGroup);
                } else {
                        return 1;
@@ -2816,6 +2901,7 @@ OSC::sel_gain (float val, lo_message msg)
                        abs = dB_to_coefficient (val);
                }
                if (s->gain_control()) {
+                       fake_touch (s->gain_control());
                        s->gain_control()->set_value (abs, PBD::Controllable::NoGroup);
                        return 0;
                }
@@ -2852,6 +2938,7 @@ OSC::sel_fader (float val, lo_message msg)
                float abs;
                abs = slider_position_to_gain_with_max (val, 2.0);
                if (s->gain_control()) {
+                       fake_touch (s->gain_control());
                        s->gain_control()->set_value (abs, PBD::Controllable::NoGroup);
                        return 0;
                }
@@ -3886,6 +3973,17 @@ OSC::periodic (void)
                        co->tick();
                }
        }
+       for (FakeTouchMap::iterator x = _touch_timeout.begin(); x != _touch_timeout.end();) {
+               _touch_timeout[(*x).first] = (*x).second - 1;
+               if (!(*x).second) {
+                       boost::shared_ptr<ARDOUR::AutomationControl> ctrl = (*x).first;
+                       // turn touch off
+                       ctrl->stop_touch (true, ctrl->session().transport_frame());
+                       _touch_timeout.erase (x++);
+               } else {
+                       x++;
+               }
+       }
        return true;
 }