Call PBD::EnumWriter::destroy in ARDOUR::cleanup (from
[ardour.git] / libs / surfaces / mackie / mackie_control_protocol.cc
index 93202f607b198d6bb60b0482f838c17acee7c9db..b13314fdc4472bd96c273deb9c2527f0cf99d7c9 100644 (file)
@@ -107,13 +107,14 @@ MackieControlProtocol::MackieControlProtocol (Session& session)
        , _modifier_state (0)
        , _ipmidi_base (MIDI::IPMIDIPort::lowest_ipmidi_port_default)
        , needs_ipmidi_restart (false)
+       , _metering_active (true)
 {
        DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::MackieControlProtocol\n");
 
        DeviceInfo::reload_device_info ();
        DeviceProfile::reload_device_profiles ();
 
-       TrackSelectionChanged.connect (gui_connections, MISSING_INVALIDATOR, boost::bind (&MackieControlProtocol::gui_track_selection_changed, this, _1), this);
+       TrackSelectionChanged.connect (gui_connections, MISSING_INVALIDATOR, boost::bind (&MackieControlProtocol::gui_track_selection_changed, this, _1, true), this);
 
        _instance = this;
 
@@ -123,6 +124,9 @@ MackieControlProtocol::MackieControlProtocol (Session& session)
 MackieControlProtocol::~MackieControlProtocol()
 {
        DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::~MackieControlProtocol\n");
+       
+       drop_connections ();
+       tear_down_gui ();
 
        _active = false;
 
@@ -192,6 +196,17 @@ MackieControlProtocol::next_track()
        }
 }
 
+bool
+MackieControlProtocol::route_is_locked_to_strip (boost::shared_ptr<Route> r) const
+{
+       for (Surfaces::const_iterator si = surfaces.begin(); si != surfaces.end(); ++si) {
+               if ((*si)->route_is_locked_to_strip (r)) {
+                       return true;
+               }
+       }
+       return false;
+}
+
 // predicate for sort call in get_sorted_routes
 struct RouteByRemoteId
 {
@@ -229,13 +244,19 @@ MackieControlProtocol::get_sorted_routes()
 
        for (RouteList::iterator it = routes->begin(); it != routes->end(); ++it) {
 
-               Route & route = **it;
+               boost::shared_ptr<Route> route = *it;
 
-               if (remote_ids.find (route.remote_control_id()) != remote_ids.end()) {
+               if (remote_ids.find (route->remote_control_id()) != remote_ids.end()) {
                        continue;
                }
 
-               if (route.is_hidden() || route.is_master() || route.is_monitor()) {
+               if (route->is_hidden() || route->is_master() || route->is_monitor()) {
+                       continue;
+               }
+
+               /* don't include locked routes */
+
+               if (route_is_locked_to_strip(route)) {
                        continue;
                }
 
@@ -261,7 +282,7 @@ MackieControlProtocol::get_sorted_routes()
                }
 
                sorted.push_back (*it);
-               remote_ids.insert (route.remote_control_id());
+               remote_ids.insert (route->remote_control_id());
        }
 
        sort (sorted.begin(), sorted.end(), RouteByRemoteId());
@@ -275,12 +296,12 @@ MackieControlProtocol::refresh_current_bank()
 }
 
 uint32_t
-MackieControlProtocol::n_strips() const
+MackieControlProtocol::n_strips (bool with_locked_strips) const
 {
        uint32_t strip_count = 0;
 
        for (Surfaces::const_iterator si = surfaces.begin(); si != surfaces.end(); ++si) {
-               strip_count += (*si)->n_strips ();
+               strip_count += (*si)->n_strips (with_locked_strips);
        }
 
        return strip_count;
@@ -296,17 +317,13 @@ MackieControlProtocol::switch_banks (uint32_t initial, bool force)
        }
 
        Sorted sorted = get_sorted_routes();
-       uint32_t strip_cnt = n_strips();
-
-       if (sorted.size() <= strip_cnt && !force) {
-               /* no banking - not enough routes to fill all strips */
-               return;
-       }
-
-       uint32_t delta = sorted.size() - strip_cnt;
+       uint32_t strip_cnt = n_strips (false); // do not include locked strips
+                                              // in this count
 
-       if (delta > 0 && initial > delta) {
-               DEBUG_TRACE (DEBUG::MackieControl, string_compose ("not switching to %1\n", initial));
+       if (sorted.size() <= strip_cnt && _current_initial_bank == 0 && !force) {
+               /* no banking - not enough routes to fill all strips and we're
+                * not at the first one.
+                */
                return;
        }
 
@@ -327,9 +344,9 @@ MackieControlProtocol::switch_banks (uint32_t initial, bool force)
                        vector<boost::shared_ptr<Route> > routes;
                        uint32_t added = 0;
 
-                       DEBUG_TRACE (DEBUG::MackieControl, string_compose ("surface has %1 strips\n", (*si)->n_strips()));
+                       DEBUG_TRACE (DEBUG::MackieControl, string_compose ("surface has %1 unlockedstrips\n", (*si)->n_strips (false)));
 
-                       for (; r != sorted.end() && added < (*si)->n_strips(); ++r, ++added) {
+                       for (; r != sorted.end() && added < (*si)->n_strips (false); ++r, ++added) {
                                routes.push_back (*r);
                        }
 
@@ -341,6 +358,10 @@ MackieControlProtocol::switch_banks (uint32_t initial, bool force)
 
        /* reset this to get the right display of view mode after the switch */
        set_view_mode (_view_mode);
+
+       /* make sure selection is correct */
+       
+       _gui_track_selection_changed (&_last_selected_routes, false);
        
        /* current bank has not been saved */
        session->set_dirty();
@@ -452,7 +473,7 @@ MackieControlProtocol::update_global_led (int id, LedState ls)
 {
        boost::shared_ptr<Surface> surface = surfaces.front();
 
-       if (!surface->type() == mcu) {
+       if (surface->type() != mcu) {
                return;
        }
 
@@ -530,7 +551,7 @@ MackieControlProtocol::set_profile (const string& profile_name)
 }      
 
 void
-MackieControlProtocol::set_device (const string& device_name)
+MackieControlProtocol::set_device (const string& device_name, bool allow_activation)
 {
        map<string,DeviceInfo>::iterator d = DeviceInfo::device_info.find (device_name);
 
@@ -545,9 +566,13 @@ MackieControlProtocol::set_device (const string& device_name)
 
        _device_info = d->second;
 
-       if (_active) {
-               create_surfaces ();
-               switch_banks (0, true);
+       if (allow_activation) {
+               set_active (true);
+       } else {
+               if (_active) {
+                       create_surfaces ();
+                       switch_banks (0, true);
+               }
        }
 }
 
@@ -673,7 +698,7 @@ MackieControlProtocol::set_state (const XMLNode & node, int /*version*/)
        }
 
        if ((prop = node.property (X_("device-name"))) != 0) {
-               set_device (prop->value());
+               set_device (prop->value(), false);
        }
 
        if ((prop = node.property (X_("device-profile"))) != 0) {
@@ -689,40 +714,29 @@ MackieControlProtocol::set_state (const XMLNode & node, int /*version*/)
        return retval;
 }
 
-
-/////////////////////////////////////////////////
-// handlers for Route signals
-// TODO should these be part of RouteSignal?
-// They started off as signal/slot handlers for signals
-// from Route, but they're also used in polling for automation
-/////////////////////////////////////////////////
-
-// TODO handle plugin automation polling
 string 
 MackieControlProtocol::format_bbt_timecode (framepos_t now_frame)
 {
        Timecode::BBT_Time bbt_time;
+
        session->bbt_time (now_frame, bbt_time);
 
-       // According to the Logic docs
-       // digits: 888/88/88/888
-       // BBT mode: Bars/Beats/Subdivisions/Ticks
+       // The Mackie protocol spec is built around a BBT time display of
+       //
+       // digits:     888/88/88/888
+       // semantics:  BBB/bb/ss/ttt
+       //
+       // The third field is "subdivisions" which is a concept found in Logic
+       // but not present in Ardour. Instead Ardour displays a 4 digit tick
+       // count, which we need to spread across the 5 digits of ss/ttt.
+
        ostringstream os;
+
        os << setw(3) << setfill('0') << bbt_time.bars;
        os << setw(2) << setfill('0') << bbt_time.beats;
-
-       // figure out subdivisions per beat
-       const ARDOUR::Meter & meter = session->tempo_map().meter_at (now_frame);
-       int subdiv = 2;
-       if (meter.note_divisor() == 8 && (meter.divisions_per_bar() == 12.0 || meter.divisions_per_bar() == 9.0 || meter.divisions_per_bar() == 6.0)) {
-               subdiv = 3;
-       }
-
-       uint32_t subdivisions = bbt_time.ticks / uint32_t (Timecode::BBT_Time::ticks_per_beat / subdiv);
-       uint32_t ticks = bbt_time.ticks % uint32_t (Timecode::BBT_Time::ticks_per_beat / subdiv);
-
-       os << setw(2) << setfill('0') << subdivisions + 1;
-       os << setw(3) << setfill('0') << ticks;
+       os << ' ';
+       os << setw(1) << setfill('0') << bbt_time.ticks / 1000;
+       os << setw(3) << setfill('0') << bbt_time.ticks % 1000;
 
        return os.str();
 }
@@ -737,10 +751,12 @@ MackieControlProtocol::format_timecode_timecode (framepos_t now_frame)
        // digits: 888/88/88/888
        // Timecode mode: Hours/Minutes/Seconds/Frames
        ostringstream os;
-       os << setw(3) << setfill('0') << timecode.hours;
+       os << ' ';
+       os << setw(2) << setfill('0') << timecode.hours;
        os << setw(2) << setfill('0') << timecode.minutes;
        os << setw(2) << setfill('0') << timecode.seconds;
-       os << setw(3) << setfill('0') << timecode.frames;
+       os << ' ';
+       os << setw(2) << setfill('0') << timecode.frames;
 
        return os.str();
 }
@@ -754,7 +770,7 @@ MackieControlProtocol::update_timecode_display()
 
        boost::shared_ptr<Surface> surface = surfaces.front();
 
-       if (surface->type() != mcu || !_device_info.has_timecode_display()) {
+       if (surface->type() != mcu || !_device_info.has_timecode_display() || !surface->active ()) {
                return;
        }
 
@@ -863,14 +879,25 @@ void
 MackieControlProtocol::notify_transport_state_changed()
 {
        // switch various play and stop buttons on / off
+       update_global_button (Button::Loop, session->get_play_loop());
        update_global_button (Button::Play, session->transport_speed() == 1.0);
        update_global_button (Button::Stop, !session->transport_rolling());
        update_global_button (Button::Rewind, session->transport_speed() < 0.0);
        update_global_button (Button::Ffwd, session->transport_speed() > 1.0);
 
+       notify_metering_state_changed ();
+       
        _transport_previously_rolling = session->transport_rolling();
 }
 
+void 
+MackieControlProtocol::notify_metering_state_changed()
+{
+       for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
+               (*s)->notify_metering_state_changed ();
+       }       
+}
+
 void
 MackieControlProtocol::notify_record_state_changed ()
 {
@@ -1108,6 +1135,18 @@ MackieControlProtocol::midi_input_handler (IOCondition ioc, MIDI::Port* port)
 
        if (ioc & IO_IN) {
 
+               /* Devices using regular JACK MIDI ports will need to have
+                  the x-thread FIFO drained to avoid burning endless CPU.
+
+                  Devices using ipMIDI have port->selectable() as the same
+                  file descriptor that data arrives on, so doing this
+                  for them will simply throw all incoming data away.
+               */
+
+               if (!_device_info.uses_ipmidi()) {
+                       CrossThreadChannel::drain (port->selectable());
+               }
+
                DEBUG_TRACE (DEBUG::MackieControl, string_compose ("data available on %1\n", port->name()));
                framepos_t now = session->engine().frame_time();
                port->parse (now);
@@ -1182,10 +1221,39 @@ MackieControlProtocol::force_special_route_to_strip (boost::shared_ptr<Route> r,
 }
 
 void
-MackieControlProtocol::gui_track_selection_changed (ARDOUR::RouteNotificationListPtr rl)
+MackieControlProtocol::gui_track_selection_changed (ARDOUR::RouteNotificationListPtr rl, bool save_list)
 {
+       _gui_track_selection_changed (rl.get(), save_list);
+}
+
+void
+MackieControlProtocol::_gui_track_selection_changed (ARDOUR::RouteNotificationList* rl, bool save_list)
+{
+
+       /* We need to keep a list of the most recently selected routes around,
+          but we are not allowed to keep shared_ptr<Route> unless we want to
+          handle the complexities of route deletion. So instead, the GUI sends
+          us a notification using weak_ptr<Route>, which we keep a copy
+          of. For efficiency's sake, however, we convert the weak_ptr's into
+          shared_ptr<Route> before passing them to however many surfaces (and
+          thus strips) that we have.
+       */
+
+       StrongRouteNotificationList srl;
+
+       for (ARDOUR::RouteNotificationList::const_iterator i = rl->begin(); i != rl->end(); ++i) {
+               boost::shared_ptr<ARDOUR::Route> r = (*i).lock();
+               if (r) {
+                       srl.push_back (r);
+               }
+       }
+
        for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
-               (*s)->gui_selection_changed (rl);
+               (*s)->gui_selection_changed (srl);
+       }
+       
+       if (save_list) {
+               _last_selected_routes = *rl;
        }
 }
 
@@ -1225,10 +1293,15 @@ MackieControlProtocol::select_range ()
 
        if (!routes.empty()) {
                for (RouteList::iterator r = routes.begin(); r != routes.end(); ++r) {
-                       if (r == routes.begin()) {
-                               SetRouteSelection ((*r)->remote_control_id());
+
+                       if (_modifier_state == MODIFIER_CONTROL) {
+                               ToggleRouteSelection ((*r)->remote_control_id ());
                        } else {
-                               AddRouteToSelection ((*r)->remote_control_id());
+                               if (r == routes.begin()) {
+                                       SetRouteSelection ((*r)->remote_control_id());
+                               } else {
+                                       AddRouteToSelection ((*r)->remote_control_id());
+                               }
                        }
                }
        }