+void
+AutomationControl::set_automation_state (AutoState as)
+{
+ if (flags() & NotAutomatable) {
+ return;
+ }
+ if (alist() && as != alist()->automation_state()) {
+
+ const double val = get_value ();
+
+ alist()->set_automation_state (as);
+ if (_desc.toggled) {
+ Changed (false, Controllable::NoGroup); // notify slaves, update boolean masters
+ return; // No watch for boolean automation
+ }
+
+ if (as == Write) {
+ AutomationWatch::instance().add_automation_watch (boost::dynamic_pointer_cast<AutomationControl>(shared_from_this()));
+ } else if (as & (Touch | Latch)) {
+ if (alist()->empty()) {
+ Control::set_double (val, _session.current_start_sample (), true);
+ Control::set_double (val, _session.current_end_sample (), true);
+ Changed (true, Controllable::NoGroup);
+ }
+ if (!touching()) {
+ AutomationWatch::instance().remove_automation_watch (boost::dynamic_pointer_cast<AutomationControl>(shared_from_this()));
+ } else {
+ /* this seems unlikely, but the combination of
+ * a control surface and the mouse could make
+ * it possible to put the control into Touch
+ * mode *while* touching it.
+ */
+ AutomationWatch::instance().add_automation_watch (boost::dynamic_pointer_cast<AutomationControl>(shared_from_this()));
+ }
+ } else {
+ AutomationWatch::instance().remove_automation_watch (boost::dynamic_pointer_cast<AutomationControl>(shared_from_this()));
+ Changed (false, Controllable::NoGroup);
+ }
+ }
+}
+
+void
+AutomationControl::start_touch (double when)
+{
+ if (!_list || touching ()) {
+ return;
+ }
+
+ if (alist()->automation_state() & (Touch | Latch)) {
+ /* subtle. aligns the user value with the playback and
+ * use take actual value (incl masters).
+ *
+ * Touch + hold writes inverse curve of master-automation
+ * using AutomationWatch::timer ()
+ */
+ AutomationControl::actually_set_value (get_value (), Controllable::NoGroup);
+ alist()->start_touch (when);
+ if (!_desc.toggled) {
+ AutomationWatch::instance().add_automation_watch (boost::dynamic_pointer_cast<AutomationControl>(shared_from_this()));
+ }
+ set_touching (true);
+ }
+}
+
+void
+AutomationControl::stop_touch (double when)
+{
+ if (!_list || !touching ()) {
+ return;
+ }
+
+ if (alist()->automation_state() == Latch && _session.transport_rolling ()) {
+ return;
+ }
+
+ set_touching (false);
+
+ if (alist()->automation_state() & (Touch | Latch)) {
+ alist()->stop_touch (when);
+ if (!_desc.toggled) {
+ AutomationWatch::instance().remove_automation_watch (boost::dynamic_pointer_cast<AutomationControl>(shared_from_this()));
+ }
+ }
+}
+
+void
+AutomationControl::commit_transaction (bool did_write)
+{
+ if (did_write) {
+ XMLNode* before = alist ()->before ();
+ if (before) {
+ _session.begin_reversible_command (string_compose (_("record %1 automation"), name ()));
+ _session.commit_reversible_command (alist ()->memento_command (before, &alist ()->get_state ()));
+ }
+ } else {
+ alist ()->clear_history ();
+ }
+}
+
+/* take control-value and return UI range [0..1] */
+double
+AutomationControl::internal_to_interface (double val) const
+{
+ // XXX maybe optimize. _desc.from_interface() has
+ // a switch-statement depending on AutomationType.
+ return _desc.to_interface (val);
+}
+
+/* map GUI range [0..1] to control-value */
+double
+AutomationControl::interface_to_internal (double val) const
+{
+ if (!isfinite_local (val)) {
+ assert (0);
+ val = 0;
+ }
+ // XXX maybe optimize. see above.
+ return _desc.from_interface (val);
+}
+
+std::string
+AutomationControl::get_user_string () const
+{
+ return ARDOUR::value_as_string (_desc, get_value());
+}
+
+void
+AutomationControl::set_group (boost::shared_ptr<ControlGroup> cg)
+{
+ /* this method can only be called by a ControlGroup. We do not need
+ to ensure consistency by calling ControlGroup::remove_control(),
+ since we are guaranteed that the ControlGroup will take care of that
+ for us.
+ */
+
+ _group = cg;
+}
+
+bool
+AutomationControl::check_rt (double val, Controllable::GroupControlDisposition gcd)
+{
+ if (!_session.loading() && (flags() & Controllable::RealTime) && !AudioEngine::instance()->in_process_thread()) {
+ /* queue change in RT context */
+ _session.set_control (boost::dynamic_pointer_cast<AutomationControl>(shared_from_this()), val, gcd);
+ return true;
+ }
+
+ return false;
+}