2 Copyright (C) 2006,2007 John Anderson
3 Copyright (C) 2012 Paul Davis
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33 #include <boost/shared_array.hpp>
35 #include "midi++/types.h"
36 #include "midi++/port.h"
37 #include "midi++/ipmidi_port.h"
38 #include "pbd/pthread_utils.h"
39 #include "pbd/error.h"
40 #include "pbd/memento_command.h"
41 #include "pbd/convert.h"
43 #include "ardour/audio_track.h"
44 #include "ardour/automation_control.h"
45 #include "ardour/async_midi_port.h"
46 #include "ardour/dB.h"
47 #include "ardour/debug.h"
48 #include "ardour/location.h"
49 #include "ardour/meter.h"
50 #include "ardour/midi_track.h"
51 #include "ardour/panner.h"
52 #include "ardour/panner_shell.h"
53 #include "ardour/route.h"
54 #include "ardour/session.h"
55 #include "ardour/tempo.h"
56 #include "ardour/track.h"
57 #include "ardour/types.h"
58 #include "ardour/audioengine.h"
60 #include "mackie_control_protocol.h"
62 #include "midi_byte_array.h"
63 #include "mackie_control_exception.h"
64 #include "device_profile.h"
65 #include "surface_port.h"
68 #include "control_group.h"
74 using namespace ARDOUR;
78 using namespace ArdourSurface;
79 using namespace Mackie;
83 #include "pbd/abstract_ui.cc" // instantiate template
85 const int MackieControlProtocol::MODIFIER_OPTION = 0x1;
86 const int MackieControlProtocol::MODIFIER_CONTROL = 0x2;
87 const int MackieControlProtocol::MODIFIER_SHIFT = 0x4;
88 const int MackieControlProtocol::MODIFIER_CMDALT = 0x8;
89 const int MackieControlProtocol::MODIFIER_ZOOM = 0x10;
90 const int MackieControlProtocol::MODIFIER_SCRUB = 0x20;
91 const int MackieControlProtocol::MAIN_MODIFIER_MASK = (MackieControlProtocol::MODIFIER_OPTION|
92 MackieControlProtocol::MODIFIER_CONTROL|
93 MackieControlProtocol::MODIFIER_SHIFT|
94 MackieControlProtocol::MODIFIER_CMDALT);
96 MackieControlProtocol* MackieControlProtocol::_instance = 0;
98 bool MackieControlProtocol::probe()
103 MackieControlProtocol::MackieControlProtocol (Session& session)
104 : ControlProtocol (session, X_("Mackie"))
105 , AbstractUI<MackieControlUIRequest> ("mackie")
106 , _current_initial_bank (0)
107 , _timecode_type (ARDOUR::AnyTime::BBT)
109 , _scrub_mode (false)
110 , _flip_mode (Normal)
113 , _current_selected_track (-1)
114 , _modifier_state (0)
115 , _ipmidi_base (MIDI::IPMIDIPort::lowest_ipmidi_port_default)
116 , needs_ipmidi_restart (false)
117 , _metering_active (true)
118 , _initialized (false)
119 , configuration_state (0)
122 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::MackieControlProtocol\n");
124 DeviceInfo::reload_device_info ();
125 DeviceProfile::reload_device_profiles ();
127 TrackSelectionChanged.connect (gui_connections, MISSING_INVALIDATOR, boost::bind (&MackieControlProtocol::gui_track_selection_changed, this, _1, true), this);
134 MackieControlProtocol::~MackieControlProtocol()
136 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::~MackieControlProtocol init\n");
138 for (Surfaces::const_iterator si = surfaces.begin(); si != surfaces.end(); ++si) {
142 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::~MackieControlProtocol drop_connections ()\n");
145 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::~MackieControlProtocol tear_down_gui ()\n");
148 delete configuration_state;
150 /* stop event loop */
151 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::~MackieControlProtocol BaseUI::quit ()\n");
155 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::~MackieControlProtocol close()\n");
158 catch (exception & e) {
159 cout << "~MackieControlProtocol caught " << e.what() << endl;
162 cout << "~MackieControlProtocol caught unknown" << endl;
167 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::~MackieControlProtocol done\n");
171 MackieControlProtocol::thread_init ()
173 struct sched_param rtparam;
175 pthread_set_name (X_("MackieControl"));
177 PBD::notify_gui_about_thread_creation (X_("gui"), pthread_self(), X_("MackieControl"), 2048);
178 ARDOUR::SessionEvent::create_per_thread_pool (X_("MackieControl"), 128);
180 memset (&rtparam, 0, sizeof (rtparam));
181 rtparam.sched_priority = 9; /* XXX should be relative to audio (JACK) thread */
183 if (pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam) != 0) {
184 // do we care? not particularly.
189 MackieControlProtocol::ping_devices ()
191 /* should not be called if surfaces are not connected, but will not
192 * malfunction if it is.
195 for (Surfaces::const_iterator si = surfaces.begin(); si != surfaces.end(); ++si) {
200 // go to the previous track.
201 // Assume that get_sorted_routes().size() > route_table.size()
203 MackieControlProtocol::prev_track()
205 if (_current_initial_bank >= 1) {
206 switch_banks (_current_initial_bank - 1);
210 // go to the next track.
211 // Assume that get_sorted_routes().size() > route_table.size()
213 MackieControlProtocol::next_track()
215 Sorted sorted = get_sorted_routes();
216 if (_current_initial_bank + n_strips() < sorted.size()) {
217 switch_banks (_current_initial_bank + 1);
222 MackieControlProtocol::route_is_locked_to_strip (boost::shared_ptr<Route> r) const
224 for (Surfaces::const_iterator si = surfaces.begin(); si != surfaces.end(); ++si) {
225 if ((*si)->route_is_locked_to_strip (r)) {
232 // predicate for sort call in get_sorted_routes
233 struct RouteByRemoteId
235 bool operator () (const boost::shared_ptr<Route> & a, const boost::shared_ptr<Route> & b) const
237 return a->remote_control_id() < b->remote_control_id();
240 bool operator () (const Route & a, const Route & b) const
242 return a.remote_control_id() < b.remote_control_id();
245 bool operator () (const Route * a, const Route * b) const
247 return a->remote_control_id() < b->remote_control_id();
251 MackieControlProtocol::Sorted
252 MackieControlProtocol::get_sorted_routes()
257 boost::shared_ptr<RouteList> routes = session->get_routes();
258 set<uint32_t> remote_ids;
260 // routes with remote_id 0 should never be added
261 // TODO verify this with ardour devs
262 // remote_ids.insert (0);
264 // sort in remote_id order, and exclude master, control and hidden routes
265 // and any routes that are already set.
267 for (RouteList::iterator it = routes->begin(); it != routes->end(); ++it) {
269 boost::shared_ptr<Route> route = *it;
271 if (remote_ids.find (route->remote_control_id()) != remote_ids.end()) {
275 if (route->is_auditioner() || route->is_master() || route->is_monitor()) {
279 /* don't include locked routes */
281 if (route_is_locked_to_strip(route)) {
285 switch (_view_mode) {
287 sorted.push_back (*it);
288 remote_ids.insert (route->remote_control_id());
291 if (is_audio_track(*it)) {
292 sorted.push_back (*it);
293 remote_ids.insert (route->remote_control_id());
297 if (!is_track(*it)) {
298 sorted.push_back (*it);
299 remote_ids.insert (route->remote_control_id());
303 if (is_midi_track(*it)) {
304 sorted.push_back (*it);
305 remote_ids.insert (route->remote_control_id());
316 sort (sorted.begin(), sorted.end(), RouteByRemoteId());
321 MackieControlProtocol::refresh_current_bank()
323 switch_banks (_current_initial_bank, true);
327 MackieControlProtocol::n_strips (bool with_locked_strips) const
329 uint32_t strip_count = 0;
331 for (Surfaces::const_iterator si = surfaces.begin(); si != surfaces.end(); ++si) {
332 strip_count += (*si)->n_strips (with_locked_strips);
339 MackieControlProtocol::switch_banks (uint32_t initial, bool force)
341 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("switch banking to start at %1 force ? %2 current = %3\n", initial, force, _current_initial_bank));
343 if (initial == _current_initial_bank && !force) {
347 Sorted sorted = get_sorted_routes();
348 uint32_t strip_cnt = n_strips (false); // do not include locked strips
351 if (sorted.size() <= strip_cnt && _current_initial_bank == 0 && !force) {
352 /* no banking - not enough routes to fill all strips and we're
353 * not at the first one.
357 _current_initial_bank = initial;
358 _current_selected_track = -1;
360 // Map current bank of routes onto each surface(+strip)
362 if (_current_initial_bank <= sorted.size()) {
364 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("switch to %1, %2, available routes %3 on %4 surfaces\n",
365 _current_initial_bank, strip_cnt, sorted.size(),
368 // link routes to strips
370 Sorted::iterator r = sorted.begin() + _current_initial_bank;
372 for (Surfaces::iterator si = surfaces.begin(); si != surfaces.end(); ++si) {
373 vector<boost::shared_ptr<Route> > routes;
376 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("surface has %1 unlockedstrips\n", (*si)->n_strips (false)));
378 for (; r != sorted.end() && added < (*si)->n_strips (false); ++r, ++added) {
379 routes.push_back (*r);
382 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("give surface %1 routes\n", routes.size()));
384 (*si)->map_routes (routes);
388 /* reset this to get the right display of view mode after the switch */
389 set_view_mode (_view_mode);
391 /* make sure selection is correct */
393 _gui_track_selection_changed (&_last_selected_routes, false);
395 /* current bank has not been saved */
396 session->set_dirty();
400 MackieControlProtocol::set_active (bool yn)
402 DEBUG_TRACE (DEBUG::MackieControl, string_compose("MackieControlProtocol::set_active init with yn: '%1'\n", yn));
404 if (yn == active()) {
410 /* start event loop */
414 connect_session_signals ();
416 if (!_device_info.name().empty()) {
417 set_device (_device_info.name(), true);
420 /* set up periodic task for metering and automation
423 Glib::RefPtr<Glib::TimeoutSource> periodic_timeout = Glib::TimeoutSource::create (100); // milliseconds
424 periodic_connection = periodic_timeout->connect (sigc::mem_fun (*this, &MackieControlProtocol::periodic));
425 periodic_timeout->attach (main_loop()->get_context());
427 /* a faster periodic task used to display parameter updates */
429 Glib::RefPtr<Glib::TimeoutSource> redisplay_timeout = Glib::TimeoutSource::create (10); // milliseconds
430 redisplay_connection = redisplay_timeout->connect (sigc::mem_fun (*this, &MackieControlProtocol::redisplay));
431 redisplay_timeout->attach (main_loop()->get_context());
440 ControlProtocol::set_active (yn);
442 DEBUG_TRACE (DEBUG::MackieControl, string_compose("MackieControlProtocol::set_active done with yn: '%1'\n", yn));
448 MackieControlProtocol::hui_heartbeat ()
450 Glib::Threads::Mutex::Lock lm (surfaces_lock);
452 for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
453 (*s)->hui_heartbeat ();
460 MackieControlProtocol::periodic ()
466 if (needs_ipmidi_restart) {
475 ARDOUR::microseconds_t now_usecs = ARDOUR::get_microseconds ();
478 Glib::Threads::Mutex::Lock lm (surfaces_lock);
480 for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
481 (*s)->periodic (now_usecs);
485 update_timecode_display ();
491 MackieControlProtocol::redisplay ()
497 if (needs_ipmidi_restart) {
506 ARDOUR::microseconds_t now = ARDOUR::get_microseconds ();
509 Glib::Threads::Mutex::Lock lm (surfaces_lock);
511 for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
512 (*s)->redisplay (now);
520 MackieControlProtocol::update_timecode_beats_led()
522 if (!_device_info.has_timecode_display()) {
526 DEBUG_TRACE (DEBUG::MackieControl, string_compose("MackieControlProtocol::update_timecode_beats_led(): %1\n", _timecode_type));
527 switch (_timecode_type) {
528 case ARDOUR::AnyTime::BBT:
529 update_global_led (Led::Beats, on);
530 update_global_led (Led::Timecode, off);
532 case ARDOUR::AnyTime::Timecode:
533 update_global_led (Led::Timecode, on);
534 update_global_led (Led::Beats, off);
538 os << "Unknown Anytime::Type " << _timecode_type;
539 throw runtime_error (os.str());
544 MackieControlProtocol::update_global_button (int id, LedState ls)
546 boost::shared_ptr<Surface> surface;
549 Glib::Threads::Mutex::Lock lm (surfaces_lock);
551 if (surfaces.empty()) {
555 if (!_device_info.has_global_controls()) {
558 // surface needs to be master surface
559 surface = _master_surface;
562 map<int,Control*>::iterator x = surface->controls_by_device_independent_id.find (id);
563 if (x != surface->controls_by_device_independent_id.end()) {
564 Button * button = dynamic_cast<Button*> (x->second);
565 surface->write (button->set_state (ls));
567 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Button %1 not found\n", id));
572 MackieControlProtocol::update_global_led (int id, LedState ls)
574 Glib::Threads::Mutex::Lock lm (surfaces_lock);
576 if (surfaces.empty()) {
580 if (!_device_info.has_global_controls()) {
583 boost::shared_ptr<Surface> surface = _master_surface;
585 map<int,Control*>::iterator x = surface->controls_by_device_independent_id.find (id);
587 if (x != surface->controls_by_device_independent_id.end()) {
588 Led * led = dynamic_cast<Led*> (x->second);
589 DEBUG_TRACE (DEBUG::MackieControl, "Writing LedState\n");
590 surface->write (led->set_state (ls));
592 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Led %1 not found\n", id));
597 MackieControlProtocol::device_ready ()
599 /* this is not required to be called, but for devices which do
600 * handshaking, it can be called once the device has verified the
604 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("device ready init (active=%1)\n", active()));
608 // send messages to surface to set controls to correct values
610 MackieControlProtocol::update_surfaces()
612 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("MackieControlProtocol::update_surfaces() init (active=%1)\n", active()));
617 // do the initial bank switch to connect signals
618 // _current_initial_bank is initialised by set_state
619 switch_banks (_current_initial_bank, true);
621 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::update_surfaces() finished\n");
625 MackieControlProtocol::initialize()
628 Glib::Threads::Mutex::Lock lm (surfaces_lock);
630 if (surfaces.empty()) {
634 if (!_master_surface->active ()) {
638 // sometimes the jog wheel is a pot
639 if (_device_info.has_jog_wheel()) {
640 _master_surface->blank_jog_ring ();
644 // update global buttons and displays
646 notify_record_state_changed();
647 notify_transport_state_changed();
648 update_timecode_beats_led();
654 MackieControlProtocol::connect_session_signals()
656 // receive routes added
657 session->RouteAdded.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&MackieControlProtocol::notify_route_added, this, _1), this);
658 // receive record state toggled
659 session->RecordStateChanged.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&MackieControlProtocol::notify_record_state_changed, this), this);
660 // receive transport state changed
661 session->TransportStateChange.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&MackieControlProtocol::notify_transport_state_changed, this), this);
662 session->TransportLooped.connect (session_connections, MISSING_INVALIDATOR, boost::bind (&MackieControlProtocol::notify_loop_state_changed, this), this);
663 // receive punch-in and punch-out
664 Config->ParameterChanged.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&MackieControlProtocol::notify_parameter_changed, this, _1), this);
665 session->config.ParameterChanged.connect (session_connections, MISSING_INVALIDATOR, boost::bind (&MackieControlProtocol::notify_parameter_changed, this, _1), this);
666 // receive rude solo changed
667 session->SoloActive.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&MackieControlProtocol::notify_solo_active_changed, this, _1), this);
669 // make sure remote id changed signals reach here
670 // see also notify_route_added
671 Sorted sorted = get_sorted_routes();
673 for (Sorted::iterator it = sorted.begin(); it != sorted.end(); ++it) {
674 (*it)->RemoteControlIDChanged.connect (route_connections, MISSING_INVALIDATOR, boost::bind (&MackieControlProtocol::notify_remote_id_changed, this), this);
679 MackieControlProtocol::set_profile (const string& profile_name)
681 if (profile_name == "default") {
682 /* reset to default */
683 _device_profile = DeviceProfile (profile_name);
686 map<string,DeviceProfile>::iterator d = DeviceProfile::device_profiles.find (profile_name);
688 if (d == DeviceProfile::device_profiles.end()) {
692 _device_profile = d->second;
696 MackieControlProtocol::set_device_info (const string& device_name)
698 map<string,DeviceInfo>::iterator d = DeviceInfo::device_info.find (device_name);
700 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("new device chosen %1\n", device_name));
702 if (d == DeviceInfo::device_info.end()) {
706 _device_info = d->second;
712 MackieControlProtocol::set_device (const string& device_name, bool force)
714 if (device_name == device_info().name() && !force) {
715 /* already using that device, nothing to do */
718 /* get state from the current setup, and make sure it is stored in
719 the configuration_states node so that if we switch back to this device,
720 we will have its state available.
724 Glib::Threads::Mutex::Lock lm (surfaces_lock);
725 if (!surfaces.empty()) {
726 update_configuration_state ();
730 if (set_device_info (device_name)) {
735 port_connection.disconnect ();
736 hui_connection.disconnect ();
738 if (_device_info.device_type() == DeviceInfo::HUI) {
739 Glib::RefPtr<Glib::TimeoutSource> hui_timeout = Glib::TimeoutSource::create (1000); // milliseconds
740 hui_connection = hui_timeout->connect (sigc::mem_fun (*this, &MackieControlProtocol::hui_heartbeat));
741 hui_timeout->attach (main_loop()->get_context());
744 if (!_device_info.uses_ipmidi()) {
745 /* notice that the handler for this will execute in our event
746 loop, not in the thread where the
747 PortConnectedOrDisconnected signal is emitted.
749 ARDOUR::AudioEngine::instance()->PortConnectedOrDisconnected.connect (port_connection, MISSING_INVALIDATOR, boost::bind (&MackieControlProtocol::connection_handler, this, _1, _2, _3, _4, _5), this);
752 if (create_surfaces ()) {
762 ArdourSurface::ipmidi_input_handler (GIOChannel*, GIOCondition condition, void *data)
764 ArdourSurface::MackieControlProtocol::ipMIDIHandler* ipm = static_cast<ArdourSurface::MackieControlProtocol::ipMIDIHandler*>(data);
765 return ipm->mcp->midi_input_handler (Glib::IOCondition (condition), ipm->port);
769 MackieControlProtocol::create_surfaces ()
772 surface_type_t stype = mcu; // type not yet determined
774 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Create %1 surfaces for %2\n", 1 + _device_info.extenders(), _device_info.name()));
776 if (!_device_info.uses_ipmidi()) {
777 _input_bundle.reset (new ARDOUR::Bundle (_("Mackie Control In"), true));
778 _output_bundle.reset (new ARDOUR::Bundle (_("Mackie Control Out"), false));
780 _input_bundle.reset ();
781 _output_bundle.reset ();
784 for (uint32_t n = 0; n < 1 + _device_info.extenders(); ++n) {
785 bool is_master = false;
787 if (n == _device_info.master_position()) {
789 if (_device_info.extenders() == 0) {
790 device_name = _device_info.name();
792 device_name = X_("mackie control");
798 device_name = string_compose (X_("mackie control ext %1"), n+1);
801 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Port Name for surface %1 is %2\n", n, device_name));
803 boost::shared_ptr<Surface> surface;
811 surface.reset (new Surface (*this, device_name, n, stype));
817 _master_surface = surface;
820 if (configuration_state) {
821 XMLNode* this_device = 0;
822 XMLNodeList const& devices = configuration_state->children();
823 for (XMLNodeList::const_iterator d = devices.begin(); d != devices.end(); ++d) {
824 XMLProperty* prop = (*d)->property (X_("name"));
825 if (prop && prop->value() == _device_info.name()) {
831 XMLNode* snode = this_device->child (X_("Surfaces"));
833 surface->set_state (*snode, state_version);
839 Glib::Threads::Mutex::Lock lm (surfaces_lock);
840 surfaces.push_back (surface);
843 if (!_device_info.uses_ipmidi()) {
845 _input_bundle->add_channel (
846 surface->port().input_port().name(),
847 ARDOUR::DataType::MIDI,
848 session->engine().make_port_name_non_relative (surface->port().input_port().name())
851 _output_bundle->add_channel (
852 surface->port().output_port().name(),
853 ARDOUR::DataType::MIDI,
854 session->engine().make_port_name_non_relative (surface->port().output_port().name())
858 MIDI::Port& input_port (surface->port().input_port());
859 AsyncMIDIPort* asp = dynamic_cast<AsyncMIDIPort*> (&input_port);
863 /* async MIDI port */
865 asp->xthread().set_receive_handler (sigc::bind (sigc::mem_fun (this, &MackieControlProtocol::midi_input_handler), &input_port));
866 asp->xthread().attach (main_loop()->get_context());
870 /* ipMIDI port, no IOSource method at this time */
874 if ((fd = input_port.selectable ()) >= 0) {
876 GIOChannel* ioc = g_io_channel_unix_new (fd);
877 surface->input_source = g_io_create_watch (ioc, GIOCondition (G_IO_IN|G_IO_HUP|G_IO_ERR));
879 /* make surface's input source now hold the
880 * only reference on the IO channel
882 g_io_channel_unref (ioc);
884 /* hack up an object so that in the callback from the event loop
885 we have both the MackieControlProtocol and the input port.
887 If we were using C++ for this stuff we wouldn't need this
888 but a nasty, not-fixable bug in the binding between C
889 and C++ makes it necessary to avoid C++ for the IO
893 ipMIDIHandler* ipm = new ipMIDIHandler (); /* we will leak this sizeof(pointer)*2 sized object */
895 ipm->port = &input_port;
897 g_source_set_callback (surface->input_source, (GSourceFunc) ipmidi_input_handler, ipm, NULL);
898 g_source_attach (surface->input_source, main_loop()->get_context()->gobj());
903 if (!_device_info.uses_ipmidi()) {
904 Glib::Threads::Mutex::Lock lm (surfaces_lock);
905 for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
906 (*s)->port().reconnect ();
910 session->BundleAddedOrRemoved ();
912 assert (_master_surface);
918 MackieControlProtocol::close()
920 port_connection.disconnect ();
921 session_connections.drop_connections ();
922 route_connections.drop_connections ();
923 periodic_connection.disconnect ();
928 /** Ensure that the configuration_state XML node contains an up-to-date
929 * copy of the state node the current device. If configuration_state already
930 * contains a state node for the device, it will deleted and replaced.
933 MackieControlProtocol::update_configuration_state ()
935 /* CALLER MUST HOLD SURFACES LOCK */
937 if (!configuration_state) {
938 configuration_state = new XMLNode (X_("Configurations"));
941 XMLNode* devnode = new XMLNode (X_("Configuration"));
942 devnode->add_property (X_("name"), _device_info.name());
944 configuration_state->remove_nodes_and_delete (X_("name"), _device_info.name());
945 configuration_state->add_child_nocopy (*devnode);
947 XMLNode* snode = new XMLNode (X_("Surfaces"));
949 for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
950 snode->add_child_nocopy ((*s)->get_state());
953 devnode->add_child_nocopy (*snode);
957 MackieControlProtocol::get_state()
959 XMLNode& node (ControlProtocol::get_state());
961 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::get_state init\n");
965 snprintf (buf, sizeof (buf), "%d", _current_initial_bank);
966 node.add_property (X_("bank"), buf);
968 // ipMIDI base port (possibly not used)
969 snprintf (buf, sizeof (buf), "%d", _ipmidi_base);
970 node.add_property (X_("ipmidi-base"), buf);
972 node.add_property (X_("device-profile"), _device_profile.name());
973 node.add_property (X_("device-name"), _device_info.name());
976 Glib::Threads::Mutex::Lock lm (surfaces_lock);
977 update_configuration_state ();
980 /* force a copy of the _surfaces_state node, because we want to retain ownership */
981 node.add_child_copy (*configuration_state);
983 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::get_state done\n");
989 MackieControlProtocol::set_state (const XMLNode & node, int version)
991 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("MackieControlProtocol::set_state: active %1\n", active()));
994 const XMLProperty* prop;
997 if (ControlProtocol::set_state (node, version)) {
1001 if ((prop = node.property (X_("ipmidi-base"))) != 0) {
1002 set_ipmidi_base (atoi (prop->value()));
1005 // fetch current bank
1006 if ((prop = node.property (X_("bank"))) != 0) {
1007 bank = atoi (prop->value());
1010 if ((prop = node.property (X_("device-name"))) != 0) {
1011 set_device_info (prop->value());
1014 if ((prop = node.property (X_("device-profile"))) != 0) {
1015 set_profile (prop->value());
1018 XMLNode* dnode = node.child (X_("Configurations"));
1020 delete configuration_state;
1021 configuration_state = 0;
1024 configuration_state = new XMLNode (*dnode);
1025 state_version = version;
1028 switch_banks (bank, true);
1030 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::set_state done\n");
1036 MackieControlProtocol::format_bbt_timecode (framepos_t now_frame)
1038 Timecode::BBT_Time bbt_time;
1040 session->bbt_time (now_frame, bbt_time);
1042 // The Mackie protocol spec is built around a BBT time display of
1044 // digits: 888/88/88/888
1045 // semantics: BBB/bb/ss/ttt
1047 // The third field is "subdivisions" which is a concept found in Logic
1048 // but not present in Ardour. Instead Ardour displays a 4 digit tick
1049 // count, which we need to spread across the 5 digits of ss/ttt.
1053 os << setw(3) << setfill('0') << bbt_time.bars;
1054 os << setw(2) << setfill('0') << bbt_time.beats;
1056 os << setw(1) << setfill('0') << bbt_time.ticks / 1000;
1057 os << setw(3) << setfill('0') << bbt_time.ticks % 1000;
1063 MackieControlProtocol::format_timecode_timecode (framepos_t now_frame)
1065 Timecode::Time timecode;
1066 session->timecode_time (now_frame, timecode);
1068 // According to the Logic docs
1069 // digits: 888/88/88/888
1070 // Timecode mode: Hours/Minutes/Seconds/Frames
1072 os << setw(2) << setfill('0') << timecode.hours;
1074 os << setw(2) << setfill('0') << timecode.minutes;
1075 os << setw(2) << setfill('0') << timecode.seconds;
1077 os << setw(2) << setfill('0') << timecode.frames;
1083 MackieControlProtocol::update_timecode_display()
1085 Glib::Threads::Mutex::Lock lm (surfaces_lock);
1087 if (surfaces.empty()) {
1091 boost::shared_ptr<Surface> surface = _master_surface;
1093 if (surface->type() != mcu || !_device_info.has_timecode_display() || !surface->active ()) {
1097 // do assignment here so current_frame is fixed
1098 framepos_t current_frame = session->transport_frame();
1101 switch (_timecode_type) {
1102 case ARDOUR::AnyTime::BBT:
1103 timecode = format_bbt_timecode (current_frame);
1105 case ARDOUR::AnyTime::Timecode:
1106 timecode = format_timecode_timecode (current_frame);
1112 // only write the timecode string to the MCU if it's changed
1113 // since last time. This is to reduce midi bandwidth used.
1114 if (timecode != _timecode_last) {
1115 surface->display_timecode (timecode, _timecode_last);
1116 _timecode_last = timecode;
1120 ///////////////////////////////////////////
1122 ///////////////////////////////////////////
1124 void MackieControlProtocol::notify_parameter_changed (std::string const & p)
1126 if (p == "punch-in") {
1127 // no such button right now
1128 // update_global_button (Button::PunchIn, session->config.get_punch_in());
1129 } else if (p == "punch-out") {
1130 // no such button right now
1131 // update_global_button (Button::PunchOut, session->config.get_punch_out());
1132 } else if (p == "clicking") {
1133 update_global_button (Button::Click, Config->get_clicking());
1135 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("parameter changed: %1\n", p));
1139 // RouteList is the set of routes that have just been added
1141 MackieControlProtocol::notify_route_added (ARDOUR::RouteList & rl)
1144 Glib::Threads::Mutex::Lock lm (surfaces_lock);
1146 if (surfaces.empty()) {
1151 // currently assigned banks are less than the full set of
1152 // strips, so activate the new strip now.
1154 refresh_current_bank();
1156 // otherwise route added, but current bank needs no updating
1158 // make sure remote id changes in the new route are handled
1159 typedef ARDOUR::RouteList ARS;
1161 for (ARS::iterator it = rl.begin(); it != rl.end(); ++it) {
1162 (*it)->RemoteControlIDChanged.connect (route_connections, MISSING_INVALIDATOR, boost::bind (&MackieControlProtocol::notify_remote_id_changed, this), this);
1167 MackieControlProtocol::notify_solo_active_changed (bool active)
1169 boost::shared_ptr<Surface> surface;
1172 Glib::Threads::Mutex::Lock lm (surfaces_lock);
1174 if (surfaces.empty()) {
1178 surface = _master_surface;
1181 map<int,Control*>::iterator x = surface->controls_by_device_independent_id.find (Led::RudeSolo);
1182 if (x != surface->controls_by_device_independent_id.end()) {
1183 Led* rude_solo = dynamic_cast<Led*> (x->second);
1185 surface->write (rude_solo->set_state (active ? flashing : off));
1191 MackieControlProtocol::notify_remote_id_changed()
1194 Glib::Threads::Mutex::Lock lm (surfaces_lock);
1196 if (surfaces.empty()) {
1201 Sorted sorted = get_sorted_routes();
1202 uint32_t sz = n_strips();
1204 // if a remote id has been moved off the end, we need to shift
1205 // the current bank backwards.
1207 if (sorted.size() - _current_initial_bank < sz) {
1208 // but don't shift backwards past the zeroth channel
1209 switch_banks (max((Sorted::size_type) 0, sorted.size() - sz));
1211 // Otherwise just refresh the current bank
1212 refresh_current_bank();
1216 ///////////////////////////////////////////
1217 // Transport signals
1218 ///////////////////////////////////////////
1221 MackieControlProtocol::notify_loop_state_changed()
1223 update_global_button (Button::Loop, session->get_play_loop());
1227 MackieControlProtocol::notify_transport_state_changed()
1229 if (!_device_info.has_global_controls()) {
1233 // switch various play and stop buttons on / off
1234 update_global_button (Button::Loop, session->get_play_loop());
1235 update_global_button (Button::Play, session->transport_speed() == 1.0);
1236 update_global_button (Button::Stop, session->transport_stopped ());
1237 update_global_button (Button::Rewind, session->transport_speed() < 0.0);
1238 update_global_button (Button::Ffwd, session->transport_speed() > 1.0);
1240 notify_metering_state_changed ();
1244 MackieControlProtocol::notify_metering_state_changed()
1246 Glib::Threads::Mutex::Lock lm (surfaces_lock);
1248 for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
1249 (*s)->notify_metering_state_changed ();
1254 MackieControlProtocol::notify_record_state_changed ()
1256 if (!_device_info.has_global_controls()) {
1260 boost::shared_ptr<Surface> surface;
1263 Glib::Threads::Mutex::Lock lm (surfaces_lock);
1264 if (surfaces.empty()) {
1267 surface = _master_surface;
1270 /* rec is a tristate */
1272 map<int,Control*>::iterator x = surface->controls_by_device_independent_id.find (Button::Record);
1273 if (x != surface->controls_by_device_independent_id.end()) {
1274 Button * rec = dynamic_cast<Button*> (x->second);
1278 switch (session->record_status()) {
1279 case Session::Disabled:
1280 DEBUG_TRACE (DEBUG::MackieControl, "record state changed to disabled, LED off\n");
1283 case Session::Recording:
1284 DEBUG_TRACE (DEBUG::MackieControl, "record state changed to recording, LED on\n");
1287 case Session::Enabled:
1288 DEBUG_TRACE (DEBUG::MackieControl, "record state changed to enabled, LED flashing\n");
1293 surface->write (rec->set_state (ls));
1298 list<boost::shared_ptr<ARDOUR::Bundle> >
1299 MackieControlProtocol::bundles ()
1301 list<boost::shared_ptr<ARDOUR::Bundle> > b;
1303 if (_input_bundle) {
1304 b.push_back (_input_bundle);
1305 b.push_back (_output_bundle);
1312 MackieControlProtocol::do_request (MackieControlUIRequest* req)
1314 if (req->type == CallSlot) {
1316 call_slot (MISSING_INVALIDATOR, req->the_slot);
1318 } else if (req->type == Quit) {
1325 MackieControlProtocol::stop ()
1333 MackieControlProtocol::update_led (Surface& surface, Button& button, Mackie::LedState ls)
1336 surface.port().write (button.set_state (ls));
1341 MackieControlProtocol::build_button_map ()
1343 /* this maps our device-independent button codes to the methods that handle them.
1346 #define DEFINE_BUTTON_HANDLER(b,p,r) button_map.insert (pair<Button::ID,ButtonHandlers> ((b), ButtonHandlers ((p),(r))));
1348 DEFINE_BUTTON_HANDLER (Button::Track, &MackieControlProtocol::track_press, &MackieControlProtocol::track_release);
1349 DEFINE_BUTTON_HANDLER (Button::Send, &MackieControlProtocol::send_press, &MackieControlProtocol::send_release);
1350 DEFINE_BUTTON_HANDLER (Button::Pan, &MackieControlProtocol::pan_press, &MackieControlProtocol::pan_release);
1351 DEFINE_BUTTON_HANDLER (Button::Plugin, &MackieControlProtocol::plugin_press, &MackieControlProtocol::plugin_release);
1352 DEFINE_BUTTON_HANDLER (Button::Eq, &MackieControlProtocol::eq_press, &MackieControlProtocol::eq_release);
1353 DEFINE_BUTTON_HANDLER (Button::Dyn, &MackieControlProtocol::dyn_press, &MackieControlProtocol::dyn_release);
1354 DEFINE_BUTTON_HANDLER (Button::Left, &MackieControlProtocol::left_press, &MackieControlProtocol::left_release);
1355 DEFINE_BUTTON_HANDLER (Button::Right, &MackieControlProtocol::right_press, &MackieControlProtocol::right_release);
1356 DEFINE_BUTTON_HANDLER (Button::ChannelLeft, &MackieControlProtocol::channel_left_press, &MackieControlProtocol::channel_left_release);
1357 DEFINE_BUTTON_HANDLER (Button::ChannelRight, &MackieControlProtocol::channel_right_press, &MackieControlProtocol::channel_right_release);
1358 DEFINE_BUTTON_HANDLER (Button::Flip, &MackieControlProtocol::flip_press, &MackieControlProtocol::flip_release);
1359 DEFINE_BUTTON_HANDLER (Button::View, &MackieControlProtocol::view_press, &MackieControlProtocol::view_release);
1360 DEFINE_BUTTON_HANDLER (Button::NameValue, &MackieControlProtocol::name_value_press, &MackieControlProtocol::name_value_release);
1361 DEFINE_BUTTON_HANDLER (Button::TimecodeBeats, &MackieControlProtocol::timecode_beats_press, &MackieControlProtocol::timecode_beats_release);
1362 DEFINE_BUTTON_HANDLER (Button::F1, &MackieControlProtocol::F1_press, &MackieControlProtocol::F1_release);
1363 DEFINE_BUTTON_HANDLER (Button::F2, &MackieControlProtocol::F2_press, &MackieControlProtocol::F2_release);
1364 DEFINE_BUTTON_HANDLER (Button::F3, &MackieControlProtocol::F3_press, &MackieControlProtocol::F3_release);
1365 DEFINE_BUTTON_HANDLER (Button::F4, &MackieControlProtocol::F4_press, &MackieControlProtocol::F4_release);
1366 DEFINE_BUTTON_HANDLER (Button::F5, &MackieControlProtocol::F5_press, &MackieControlProtocol::F5_release);
1367 DEFINE_BUTTON_HANDLER (Button::F6, &MackieControlProtocol::F6_press, &MackieControlProtocol::F6_release);
1368 DEFINE_BUTTON_HANDLER (Button::F7, &MackieControlProtocol::F7_press, &MackieControlProtocol::F7_release);
1369 DEFINE_BUTTON_HANDLER (Button::F8, &MackieControlProtocol::F8_press, &MackieControlProtocol::F8_release);
1370 DEFINE_BUTTON_HANDLER (Button::MidiTracks, &MackieControlProtocol::miditracks_press, &MackieControlProtocol::miditracks_release);
1371 DEFINE_BUTTON_HANDLER (Button::Inputs, &MackieControlProtocol::inputs_press, &MackieControlProtocol::inputs_release);
1372 DEFINE_BUTTON_HANDLER (Button::AudioTracks, &MackieControlProtocol::audiotracks_press, &MackieControlProtocol::audiotracks_release);
1373 DEFINE_BUTTON_HANDLER (Button::AudioInstruments, &MackieControlProtocol::audioinstruments_press, &MackieControlProtocol::audioinstruments_release);
1374 DEFINE_BUTTON_HANDLER (Button::Aux, &MackieControlProtocol::aux_press, &MackieControlProtocol::aux_release);
1375 DEFINE_BUTTON_HANDLER (Button::Busses, &MackieControlProtocol::busses_press, &MackieControlProtocol::busses_release);
1376 DEFINE_BUTTON_HANDLER (Button::Outputs, &MackieControlProtocol::outputs_press, &MackieControlProtocol::outputs_release);
1377 DEFINE_BUTTON_HANDLER (Button::User, &MackieControlProtocol::user_press, &MackieControlProtocol::user_release);
1378 DEFINE_BUTTON_HANDLER (Button::Shift, &MackieControlProtocol::shift_press, &MackieControlProtocol::shift_release);
1379 DEFINE_BUTTON_HANDLER (Button::Option, &MackieControlProtocol::option_press, &MackieControlProtocol::option_release);
1380 DEFINE_BUTTON_HANDLER (Button::Ctrl, &MackieControlProtocol::control_press, &MackieControlProtocol::control_release);
1381 DEFINE_BUTTON_HANDLER (Button::CmdAlt, &MackieControlProtocol::cmd_alt_press, &MackieControlProtocol::cmd_alt_release);
1382 DEFINE_BUTTON_HANDLER (Button::Read, &MackieControlProtocol::read_press, &MackieControlProtocol::read_release);
1383 DEFINE_BUTTON_HANDLER (Button::Write, &MackieControlProtocol::write_press, &MackieControlProtocol::write_release);
1384 DEFINE_BUTTON_HANDLER (Button::Trim, &MackieControlProtocol::trim_press, &MackieControlProtocol::trim_release);
1385 DEFINE_BUTTON_HANDLER (Button::Touch, &MackieControlProtocol::touch_press, &MackieControlProtocol::touch_release);
1386 DEFINE_BUTTON_HANDLER (Button::Latch, &MackieControlProtocol::latch_press, &MackieControlProtocol::latch_release);
1387 DEFINE_BUTTON_HANDLER (Button::Grp, &MackieControlProtocol::grp_press, &MackieControlProtocol::grp_release);
1388 DEFINE_BUTTON_HANDLER (Button::Save, &MackieControlProtocol::save_press, &MackieControlProtocol::save_release);
1389 DEFINE_BUTTON_HANDLER (Button::Undo, &MackieControlProtocol::undo_press, &MackieControlProtocol::undo_release);
1390 DEFINE_BUTTON_HANDLER (Button::Cancel, &MackieControlProtocol::cancel_press, &MackieControlProtocol::cancel_release);
1391 DEFINE_BUTTON_HANDLER (Button::Enter, &MackieControlProtocol::enter_press, &MackieControlProtocol::enter_release);
1392 DEFINE_BUTTON_HANDLER (Button::Marker, &MackieControlProtocol::marker_press, &MackieControlProtocol::marker_release);
1393 DEFINE_BUTTON_HANDLER (Button::Nudge, &MackieControlProtocol::nudge_press, &MackieControlProtocol::nudge_release);
1394 DEFINE_BUTTON_HANDLER (Button::Loop, &MackieControlProtocol::loop_press, &MackieControlProtocol::loop_release);
1395 DEFINE_BUTTON_HANDLER (Button::Drop, &MackieControlProtocol::drop_press, &MackieControlProtocol::drop_release);
1396 DEFINE_BUTTON_HANDLER (Button::Replace, &MackieControlProtocol::replace_press, &MackieControlProtocol::replace_release);
1397 DEFINE_BUTTON_HANDLER (Button::Click, &MackieControlProtocol::click_press, &MackieControlProtocol::click_release);
1398 DEFINE_BUTTON_HANDLER (Button::ClearSolo, &MackieControlProtocol::clearsolo_press, &MackieControlProtocol::clearsolo_release);
1399 DEFINE_BUTTON_HANDLER (Button::Rewind, &MackieControlProtocol::rewind_press, &MackieControlProtocol::rewind_release);
1400 DEFINE_BUTTON_HANDLER (Button::Ffwd, &MackieControlProtocol::ffwd_press, &MackieControlProtocol::ffwd_release);
1401 DEFINE_BUTTON_HANDLER (Button::Stop, &MackieControlProtocol::stop_press, &MackieControlProtocol::stop_release);
1402 DEFINE_BUTTON_HANDLER (Button::Play, &MackieControlProtocol::play_press, &MackieControlProtocol::play_release);
1403 DEFINE_BUTTON_HANDLER (Button::Record, &MackieControlProtocol::record_press, &MackieControlProtocol::record_release);
1404 DEFINE_BUTTON_HANDLER (Button::CursorUp, &MackieControlProtocol::cursor_up_press, &MackieControlProtocol::cursor_up_release);
1405 DEFINE_BUTTON_HANDLER (Button::CursorDown, &MackieControlProtocol::cursor_down_press, &MackieControlProtocol::cursor_down_release);
1406 DEFINE_BUTTON_HANDLER (Button::CursorLeft, &MackieControlProtocol::cursor_left_press, &MackieControlProtocol::cursor_left_release);
1407 DEFINE_BUTTON_HANDLER (Button::CursorRight, &MackieControlProtocol::cursor_right_press, &MackieControlProtocol::cursor_right_release);
1408 DEFINE_BUTTON_HANDLER (Button::Zoom, &MackieControlProtocol::zoom_press, &MackieControlProtocol::zoom_release);
1409 DEFINE_BUTTON_HANDLER (Button::Scrub, &MackieControlProtocol::scrub_press, &MackieControlProtocol::scrub_release);
1410 DEFINE_BUTTON_HANDLER (Button::UserA, &MackieControlProtocol::user_a_press, &MackieControlProtocol::user_a_release);
1411 DEFINE_BUTTON_HANDLER (Button::UserB, &MackieControlProtocol::user_b_press, &MackieControlProtocol::user_b_release);
1412 DEFINE_BUTTON_HANDLER (Button::MasterFaderTouch, &MackieControlProtocol::master_fader_touch_press, &MackieControlProtocol::master_fader_touch_release);
1417 MackieControlProtocol::handle_button_event (Surface& surface, Button& button, ButtonState bs)
1419 Button::ID button_id = button.bid();
1421 if (bs != press && bs != release) {
1422 update_led (surface, button, none);
1426 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Handling %1 for button %2 (%3)\n", (bs == press ? "press" : "release"), button.id(),
1427 Button::id_to_name (button.bid())));
1429 /* check profile first */
1431 string action = _device_profile.get_button_action (button.bid(), _modifier_state);
1433 if (!action.empty()) {
1435 if (action.find ('/') != string::npos) { /* good chance that this is really an action */
1437 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Looked up action for button %1 with modifier %2, got [%3]\n",
1438 button.bid(), _modifier_state, action));
1440 /* if there is a bound action for this button, and this is a press event,
1441 carry out the action. If its a release event, do nothing since we
1442 don't bind to them at all but don't want any other handling to
1446 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("executing action %1\n", action));
1447 access_action (action);
1454 /* "action" is more likely to be a button name. We use this to
1455 * allow remapping buttons to different (builtin) functionality
1456 * associated with an existing button. This is similar to the
1457 * way that (for example) Nuendo moves the "Shift" function to
1458 * the "Enter" key of the MCU Pro.
1461 int bid = Button::name_to_id (action);
1464 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("apparent button name %1 not found\n", action));
1468 button_id = (Button::ID) bid;
1469 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("handling button %1 as if it was %2 (%3)\n", Button::id_to_name (button.bid()), button_id, Button::id_to_name (button_id)));
1473 /* lookup using the device-INDEPENDENT button ID */
1475 ButtonMap::iterator b = button_map.find (button_id);
1477 if (b != button_map.end()) {
1479 ButtonHandlers& bh (b->second);
1483 surface.write (button.set_state ((this->*(bh.press)) (button)));
1486 surface.write (button.set_state ((this->*(bh.release)) (button)));
1492 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("no button handlers for button ID %1 (device ID %2)\n",
1493 button.bid(), button.id()));
1494 error << string_compose ("no button handlers for button ID %1 (device ID %2)\n",
1495 button.bid(), button.id()) << endmsg;
1500 MackieControlProtocol::midi_input_handler (IOCondition ioc, MIDI::Port* port)
1503 DEBUG_TRACE (DEBUG::MackieControl, "MIDI port closed\n");
1509 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("something happend on %1\n", port->name()));
1511 /* Devices using regular JACK MIDI ports will need to have
1512 the x-thread FIFO drained to avoid burning endless CPU.
1514 Devices using ipMIDI have port->selectable() as the same
1515 file descriptor that data arrives on, so doing this
1516 for them will simply throw all incoming data away.
1519 if (!_device_info.uses_ipmidi()) {
1520 AsyncMIDIPort* asp = dynamic_cast<AsyncMIDIPort*>(port);
1526 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("data available on %1\n", port->name()));
1527 framepos_t now = session->engine().sample_time();
1535 MackieControlProtocol::clear_ports ()
1537 if (_input_bundle) {
1538 _input_bundle->remove_channels ();
1539 _output_bundle->remove_channels ();
1544 MackieControlProtocol::set_view_mode (ViewMode m)
1546 Glib::Threads::Mutex::Lock lm (surfaces_lock);
1550 for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
1551 (*s)->update_view_mode_display ();
1557 MackieControlProtocol::set_flip_mode (FlipMode fm)
1559 if (_flip_mode != fm) {
1561 update_global_button (Button::Flip, off);
1563 update_global_button (Button::Flip, on);
1566 Glib::Threads::Mutex::Lock lm (surfaces_lock);
1570 for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
1571 (*s)->update_flip_mode_display ();
1577 MackieControlProtocol::set_pot_mode (PotMode m)
1579 // maybe not in flip mode.
1586 Glib::Threads::Mutex::Lock lm (surfaces_lock);
1588 for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
1589 (*s)->update_potmode ();
1594 switch (_pot_mode) {
1596 update_global_button (Button::Track, on);
1597 update_global_button (Button::Send, off);
1598 update_global_button (Button::Pan, off);
1601 update_global_button (Button::Track, off);
1602 update_global_button (Button::Send, on);
1603 update_global_button (Button::Pan, off);
1606 update_global_button (Button::Track, off);
1607 update_global_button (Button::Send, off);
1608 update_global_button (Button::Pan, on);
1613 MackieControlProtocol::set_master_on_surface_strip (uint32_t surface, uint32_t strip_number)
1615 force_special_route_to_strip (session->master_out(), surface, strip_number);
1619 MackieControlProtocol::set_monitor_on_surface_strip (uint32_t surface, uint32_t strip_number)
1621 force_special_route_to_strip (session->monitor_out(), surface, strip_number);
1625 MackieControlProtocol::force_special_route_to_strip (boost::shared_ptr<Route> r, uint32_t surface, uint32_t strip_number)
1631 Glib::Threads::Mutex::Lock lm (surfaces_lock);
1633 for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
1634 if ((*s)->number() == surface) {
1635 Strip* strip = (*s)->nth_strip (strip_number);
1637 strip->set_route (session->master_out());
1638 strip->lock_controls ();
1645 MackieControlProtocol::gui_track_selection_changed (ARDOUR::RouteNotificationListPtr rl, bool save_list)
1647 _gui_track_selection_changed (rl.get(), save_list);
1651 MackieControlProtocol::_gui_track_selection_changed (ARDOUR::RouteNotificationList* rl, bool save_list)
1653 /* We need to keep a list of the most recently selected routes around,
1654 but we are not allowed to keep shared_ptr<Route> unless we want to
1655 handle the complexities of route deletion. So instead, the GUI sends
1656 us a notification using weak_ptr<Route>, which we keep a copy
1657 of. For efficiency's sake, however, we convert the weak_ptr's into
1658 shared_ptr<Route> before passing them to however many surfaces (and
1659 thus strips) that we have.
1662 StrongRouteNotificationList srl;
1664 for (ARDOUR::RouteNotificationList::const_iterator i = rl->begin(); i != rl->end(); ++i) {
1665 boost::shared_ptr<ARDOUR::Route> r = (*i).lock();
1672 Glib::Threads::Mutex::Lock lm (surfaces_lock);
1674 for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
1675 (*s)->gui_selection_changed (srl);
1680 _last_selected_routes = *rl;
1685 MackieControlProtocol::transport_frame() const
1687 return session->transport_frame();
1691 MackieControlProtocol::add_down_select_button (int surface, int strip)
1693 _down_select_buttons.insert ((surface<<8)|(strip&0xf));
1697 MackieControlProtocol::remove_down_select_button (int surface, int strip)
1699 DownButtonList::iterator x = find (_down_select_buttons.begin(), _down_select_buttons.end(), (uint32_t) (surface<<8)|(strip&0xf));
1700 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("removing surface %1 strip %2 from down select buttons\n", surface, strip));
1701 if (x != _down_select_buttons.end()) {
1702 _down_select_buttons.erase (x);
1704 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("surface %1 strip %2 not found in down select buttons\n",
1710 MackieControlProtocol::select_range ()
1714 pull_route_range (_down_select_buttons, routes);
1716 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("select range: found %1 routes\n", routes.size()));
1718 if (!routes.empty()) {
1719 for (RouteList::iterator r = routes.begin(); r != routes.end(); ++r) {
1721 if (main_modifier_state() == MODIFIER_CONTROL) {
1722 ToggleRouteSelection ((*r)->remote_control_id ());
1724 if (r == routes.begin()) {
1725 SetRouteSelection ((*r)->remote_control_id());
1727 AddRouteToSelection ((*r)->remote_control_id());
1735 MackieControlProtocol::add_down_button (AutomationType a, int surface, int strip)
1737 DownButtonMap::iterator m = _down_buttons.find (a);
1739 if (m == _down_buttons.end()) {
1740 _down_buttons[a] = DownButtonList();
1743 _down_buttons[a].insert ((surface<<8)|(strip&0xf));
1747 MackieControlProtocol::remove_down_button (AutomationType a, int surface, int strip)
1749 DownButtonMap::iterator m = _down_buttons.find (a);
1751 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("removing surface %1 strip %2 from down buttons for %3\n", surface, strip, (int) a));
1753 if (m == _down_buttons.end()) {
1757 DownButtonList& l (m->second);
1758 DownButtonList::iterator x = find (l.begin(), l.end(), (surface<<8)|(strip&0xf));
1763 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("surface %1 strip %2 not found in down buttons for %3\n",
1764 surface, strip, (int) a));
1768 MackieControlProtocol::ControlList
1769 MackieControlProtocol::down_controls (AutomationType p)
1771 ControlList controls;
1774 DownButtonMap::iterator m = _down_buttons.find (p);
1776 if (m == _down_buttons.end()) {
1780 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("looking for down buttons for %1, got %2\n",
1781 p, m->second.size()));
1783 pull_route_range (m->second, routes);
1786 case GainAutomation:
1787 for (RouteList::iterator r = routes.begin(); r != routes.end(); ++r) {
1788 controls.push_back ((*r)->gain_control());
1791 case SoloAutomation:
1792 for (RouteList::iterator r = routes.begin(); r != routes.end(); ++r) {
1793 controls.push_back ((*r)->solo_control());
1796 case MuteAutomation:
1797 for (RouteList::iterator r = routes.begin(); r != routes.end(); ++r) {
1798 controls.push_back ((*r)->mute_control());
1801 case RecEnableAutomation:
1802 for (RouteList::iterator r = routes.begin(); r != routes.end(); ++r) {
1803 boost::shared_ptr<Track> trk = boost::dynamic_pointer_cast<Track> (*r);
1805 controls.push_back (trk->rec_enable_control());
1817 struct ButtonRangeSorter {
1818 bool operator() (const uint32_t& a, const uint32_t& b) {
1819 return (a>>8) < (b>>8) // a.surface < b.surface
1821 ((a>>8) == (b>>8) && (a&0xf) < (b&0xf)); // a.surface == b.surface && a.strip < b.strip
1826 MackieControlProtocol::pull_route_range (DownButtonList& down, RouteList& selected)
1828 ButtonRangeSorter cmp;
1834 list<uint32_t> ldown;
1835 ldown.insert (ldown.end(), down.begin(), down.end());
1838 uint32_t first = ldown.front();
1839 uint32_t last = ldown.back ();
1841 uint32_t first_surface = first>>8;
1842 uint32_t first_strip = first&0xf;
1844 uint32_t last_surface = last>>8;
1845 uint32_t last_strip = last&0xf;
1847 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("PRR %5 in list %1.%2 - %3.%4\n", first_surface, first_strip, last_surface, last_strip,
1850 Glib::Threads::Mutex::Lock lm (surfaces_lock);
1852 for (Surfaces::const_iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
1854 if ((*s)->number() >= first_surface && (*s)->number() <= last_surface) {
1859 if ((*s)->number() == first_surface) {
1865 if ((*s)->number() == last_surface) {
1869 ls = (*s)->n_strips ();
1872 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("adding strips for surface %1 (%2 .. %3)\n",
1873 (*s)->number(), fs, ls));
1875 for (uint32_t n = fs; n < ls; ++n) {
1876 boost::shared_ptr<Route> r = (*s)->nth_strip (n)->route();
1878 selected.push_back (r);
1886 MackieControlProtocol::set_ipmidi_base (int16_t portnum)
1888 /* this will not be saved without a session save, so .. */
1890 session->set_dirty ();
1892 _ipmidi_base = portnum;
1894 /* if the current device uses ipMIDI we need
1898 if (active() && _device_info.uses_ipmidi()) {
1899 needs_ipmidi_restart = true;
1904 MackieControlProtocol::ipmidi_restart ()
1907 if (create_surfaces ()) {
1910 switch_banks (_current_initial_bank, true);
1911 needs_ipmidi_restart = false;
1916 MackieControlProtocol::clear_surfaces ()
1921 Glib::Threads::Mutex::Lock lm (surfaces_lock);
1922 _master_surface.reset ();
1928 MackieControlProtocol::set_touch_sensitivity (int sensitivity)
1930 sensitivity = min (9, sensitivity);
1931 sensitivity = max (0, sensitivity);
1933 Glib::Threads::Mutex::Lock lm (surfaces_lock);
1935 for (Surfaces::const_iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
1936 (*s)->set_touch_sensitivity (sensitivity);
1941 MackieControlProtocol::recalibrate_faders ()
1943 Glib::Threads::Mutex::Lock lm (surfaces_lock);
1945 for (Surfaces::const_iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
1946 (*s)->recalibrate_faders ();
1951 MackieControlProtocol::toggle_backlight ()
1953 Glib::Threads::Mutex::Lock lm (surfaces_lock);
1955 for (Surfaces::const_iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
1956 (*s)->toggle_backlight ();
1960 boost::shared_ptr<Surface>
1961 MackieControlProtocol::get_surface_by_raw_pointer (void* ptr) const
1963 Glib::Threads::Mutex::Lock lm (surfaces_lock);
1965 for (Surfaces::const_iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
1966 if ((*s).get() == (Surface*) ptr) {
1971 return boost::shared_ptr<Surface> ();
1974 boost::shared_ptr<Surface>
1975 MackieControlProtocol::nth_surface (uint32_t n) const
1977 Glib::Threads::Mutex::Lock lm (surfaces_lock);
1979 for (Surfaces::const_iterator s = surfaces.begin(); s != surfaces.end(); ++s, --n) {
1985 return boost::shared_ptr<Surface> ();
1989 MackieControlProtocol::connection_handler (boost::weak_ptr<ARDOUR::Port> wp1, std::string name1, boost::weak_ptr<ARDOUR::Port> wp2, std::string name2, bool yn)
1993 Glib::Threads::Mutex::Lock lm (surfaces_lock);
1997 for (Surfaces::const_iterator s = scopy.begin(); s != scopy.end(); ++s) {
1998 if ((*s)->connection_handler (wp1, name1, wp2, name2, yn)) {
1999 ConnectionChange (*s);
2006 MackieControlProtocol::is_track (boost::shared_ptr<Route> r) const
2008 return boost::dynamic_pointer_cast<Track>(r) != 0;
2012 MackieControlProtocol::is_audio_track (boost::shared_ptr<Route> r) const
2014 return boost::dynamic_pointer_cast<AudioTrack>(r) != 0;
2018 MackieControlProtocol::is_midi_track (boost::shared_ptr<Route> r) const
2020 return boost::dynamic_pointer_cast<MidiTrack>(r) != 0;