try just removing all PLATFORM_WINDOWS conditionals in ipmidi code to see if it will...
[ardour.git] / libs / surfaces / mackie / mackie_control_protocol.cc
index 9e1a2bbce73a5a28cfd78a6eecb3b1750cf1d9a8..966a2cbedd669e66b6aaa2b84d7ba6f59d9cc2ee 100644 (file)
@@ -31,6 +31,7 @@
 #include <errno.h>
 
 #include <boost/shared_array.hpp>
+#include <glibmm/miscutils.h>
 
 #include "midi++/types.h"
 #include "midi++/port.h"
 #include "pbd/memento_command.h"
 #include "pbd/convert.h"
 
+#include "ardour/audio_track.h"
 #include "ardour/automation_control.h"
 #include "ardour/async_midi_port.h"
 #include "ardour/dB.h"
 #include "ardour/debug.h"
 #include "ardour/location.h"
 #include "ardour/meter.h"
+#include "ardour/midi_track.h"
 #include "ardour/panner.h"
 #include "ardour/panner_shell.h"
 #include "ardour/route.h"
+#include "ardour/route_group.h"
 #include "ardour/session.h"
 #include "ardour/tempo.h"
 #include "ardour/track.h"
@@ -102,6 +106,7 @@ MackieControlProtocol::MackieControlProtocol (Session& session)
        : ControlProtocol (session, X_("Mackie"))
        , AbstractUI<MackieControlUIRequest> ("mackie")
        , _current_initial_bank (0)
+       , _frame_last (0)
        , _timecode_type (ARDOUR::AnyTime::BBT)
        , _gui (0)
        , _scrub_mode (false)
@@ -122,6 +127,12 @@ MackieControlProtocol::MackieControlProtocol (Session& session)
        DeviceInfo::reload_device_info ();
        DeviceProfile::reload_device_profiles ();
 
+       for (int i = 0; i < 9; i++) {
+               _last_bank[i] = 0;
+       }
+
+       _last_bank[Mixer] = _current_selected_track;
+
        TrackSelectionChanged.connect (gui_connections, MISSING_INVALIDATOR, boost::bind (&MackieControlProtocol::gui_track_selection_changed, this, _1, true), this);
 
        _instance = this;
@@ -282,21 +293,64 @@ MackieControlProtocol::get_sorted_routes()
 
                switch (_view_mode) {
                case Mixer:
+                       if (route->route_group()) {
+                               route->route_group()->set_active (true, this);
+                       }
+                       sorted.push_back (route);
+                       remote_ids.insert (route->remote_control_id());
                        break;
                case AudioTracks:
+                       if (is_audio_track(route)) {
+                               if (route->route_group()) {
+                                       route->route_group()->set_active (true, this);
+                               }
+                               sorted.push_back (route);
+                               remote_ids.insert (route->remote_control_id());
+                       }
                        break;
                case Busses:
+                       if (!is_track(route)) {
+                               if (route->route_group()) {
+                                       route->route_group()->set_active (true, this);
+                               }
+                               sorted.push_back (route);
+                               remote_ids.insert (route->remote_control_id());
+                       }
                        break;
                case MidiTracks:
+                       if (is_midi_track(route)) {
+                               if (route->route_group()) {
+                                       route->route_group()->set_active (true, this);
+                               }
+                               sorted.push_back (route);
+                               remote_ids.insert (route->remote_control_id());
+                       }
                        break;
                case Plugins:
                        break;
-               case Auxes:
+               case Auxes: // for now aux and buss are same
+                       if (!is_track(route)) {
+                               if (route->route_group()) {
+                                       route->route_group()->set_active (true, this);
+                               }
+                               sorted.push_back (route);
+                               remote_ids.insert (route->remote_control_id());
+                       }
+                       break;
+               case Selected: // For example: a group
+                       if (selected(route)) {
+                               /* Selected may be a group in which case we want to
+                                * control each track separately.
+                                */
+                               if (route->route_group()) {
+                                       route->route_group()->set_active (false, this);
+                               }
+                               sorted.push_back (route);
+                               remote_ids.insert (route->remote_control_id());
+                       }
                        break;
                }
 
-               sorted.push_back (*it);
-               remote_ids.insert (route->remote_control_id());
        }
 
        sort (sorted.begin(), sorted.end(), RouteByRemoteId());
@@ -340,7 +394,6 @@ MackieControlProtocol::switch_banks (uint32_t initial, bool force)
                 */
                return;
        }
-       set_flip_mode (Normal);
        _current_initial_bank = initial;
        _current_selected_track = -1;
 
@@ -373,7 +426,7 @@ 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);
+       display_view_mode ();
 
        /* make sure selection is correct */
 
@@ -590,6 +643,7 @@ MackieControlProtocol::device_ready ()
 
        DEBUG_TRACE (DEBUG::MackieControl, string_compose ("device ready init (active=%1)\n", active()));
        update_surfaces ();
+       set_pot_mode (_pot_mode);
 }
 
 // send messages to surface to set controls to correct values
@@ -665,14 +719,10 @@ 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()) {
+               _device_profile = DeviceProfile (profile_name);
                return;
        }
 
@@ -809,7 +859,7 @@ MackieControlProtocol::create_surfaces ()
                        XMLNodeList const& devices = configuration_state->children();
                        for (XMLNodeList::const_iterator d = devices.begin(); d != devices.end(); ++d) {
                                XMLProperty* prop = (*d)->property (X_("name"));
-                               if (prop && prop->value() == device_name) {
+                               if (prop && prop->value() == _device_info.name()) {
                                        this_device = *d;
                                        break;
                                }
@@ -999,7 +1049,17 @@ MackieControlProtocol::set_state (const XMLNode & node, int version)
        }
 
        if ((prop = node.property (X_("device-profile"))) != 0) {
-               set_profile (prop->value());
+               if (prop->value().empty()) {
+                       string default_profile_name;
+
+                       default_profile_name = Glib::get_user_name();
+                       default_profile_name += ' ';
+                       default_profile_name += _device_info.name();
+
+                       set_profile (default_profile_name);
+               } else {
+                       set_profile (prop->value());
+               }
        }
 
        XMLNode* dnode = node.child (X_("Configurations"));
@@ -1084,6 +1144,13 @@ MackieControlProtocol::update_timecode_display()
        // do assignment here so current_frame is fixed
        framepos_t current_frame = session->transport_frame();
        string timecode;
+       // For large jumps in play head possition do full reset
+       int moved = (current_frame - _frame_last) / session->frame_rate ();
+       if (moved) {
+               DEBUG_TRACE (DEBUG::MackieControl, "Timecode reset\n");
+               _timecode_last = string (10, ' ');
+       }
+       _frame_last = current_frame;
 
        switch (_timecode_type) {
        case ARDOUR::AnyTime::BBT:
@@ -1530,10 +1597,18 @@ MackieControlProtocol::clear_ports ()
 void
 MackieControlProtocol::set_view_mode (ViewMode m)
 {
-       Glib::Threads::Mutex::Lock lm (surfaces_lock);
+       _last_bank[_view_mode] = _current_initial_bank;
 
        _view_mode = m;
 
+       switch_banks(_last_bank[_view_mode], true);
+}
+
+void
+MackieControlProtocol::display_view_mode ()
+{
+       Glib::Threads::Mutex::Lock lm (surfaces_lock);
+
        for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
                (*s)->update_view_mode_display ();
        }
@@ -1563,6 +1638,10 @@ MackieControlProtocol::set_flip_mode (FlipMode fm)
 void
 MackieControlProtocol::set_pot_mode (PotMode m)
 {
+       // maybe not in flip mode.
+       if (flip_mode()) {
+               return;
+       }
        _pot_mode = m;
 
        {
@@ -1984,3 +2063,35 @@ MackieControlProtocol::connection_handler (boost::weak_ptr<ARDOUR::Port> wp1, st
                }
        }
 }
+
+bool
+MackieControlProtocol::is_track (boost::shared_ptr<Route> r) const
+{
+       return boost::dynamic_pointer_cast<Track>(r) != 0;
+}
+
+bool
+MackieControlProtocol::is_audio_track (boost::shared_ptr<Route> r) const
+{
+       return boost::dynamic_pointer_cast<AudioTrack>(r) != 0;
+}
+
+bool
+MackieControlProtocol::is_midi_track (boost::shared_ptr<Route> r) const
+{
+       return boost::dynamic_pointer_cast<MidiTrack>(r) != 0;
+}
+
+bool
+MackieControlProtocol::selected (boost::shared_ptr<Route> r) const
+{
+       const RouteNotificationList* rl = &_last_selected_routes;
+
+       for (ARDOUR::RouteNotificationList::const_iterator i = rl->begin(); i != rl->end(); ++i) {
+               boost::shared_ptr<ARDOUR::Route> rt = (*i).lock();
+               if (rt == r) {
+                       return true;
+               }
+       }
+       return false;
+}