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.
34 #include <boost/shared_array.hpp>
36 #include "midi++/types.h"
37 #include "midi++/port.h"
38 #include "midi++/ipmidi_port.h"
39 #include "pbd/pthread_utils.h"
40 #include "pbd/error.h"
41 #include "pbd/memento_command.h"
42 #include "pbd/convert.h"
44 #include "ardour/automation_control.h"
45 #include "ardour/dB.h"
46 #include "ardour/debug.h"
47 #include "ardour/location.h"
48 #include "ardour/meter.h"
49 #include "ardour/panner.h"
50 #include "ardour/panner_shell.h"
51 #include "ardour/route.h"
52 #include "ardour/session.h"
53 #include "ardour/tempo.h"
54 #include "ardour/track.h"
55 #include "ardour/types.h"
56 #include "ardour/audioengine.h"
58 #include "mackie_control_protocol.h"
60 #include "midi_byte_array.h"
61 #include "mackie_control_exception.h"
62 #include "device_profile.h"
63 #include "surface_port.h"
66 #include "control_group.h"
72 using namespace ARDOUR;
74 using namespace Mackie;
80 #include "pbd/abstract_ui.cc" // instantiate template
82 const int MackieControlProtocol::MODIFIER_OPTION = 0x1;
83 const int MackieControlProtocol::MODIFIER_CONTROL = 0x2;
84 const int MackieControlProtocol::MODIFIER_SHIFT = 0x4;
85 const int MackieControlProtocol::MODIFIER_CMDALT = 0x8;
87 MackieControlProtocol* MackieControlProtocol::_instance = 0;
89 bool MackieControlProtocol::probe()
94 MackieControlProtocol::MackieControlProtocol (Session& session)
95 : ControlProtocol (session, X_("Mackie"))
96 , AbstractUI<MackieControlUIRequest> ("mackie")
97 , _current_initial_bank (0)
98 , _timecode_type (ARDOUR::AnyTime::BBT)
99 , _input_bundle (new ARDOUR::Bundle (_("Mackie Control In"), true))
100 , _output_bundle (new ARDOUR::Bundle (_("Mackie Control Out"), false))
103 , _scrub_mode (false)
106 , _current_selected_track (-1)
107 , _modifier_state (0)
108 , _ipmidi_base (MIDI::IPMIDIPort::lowest_ipmidi_port_default)
109 , needs_ipmidi_restart (false)
111 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::MackieControlProtocol\n");
113 DeviceInfo::reload_device_info ();
114 DeviceProfile::reload_device_profiles ();
116 TrackSelectionChanged.connect (gui_connections, MISSING_INVALIDATOR, boost::bind (&MackieControlProtocol::gui_track_selection_changed, this, _1), this);
123 MackieControlProtocol::~MackieControlProtocol()
125 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::~MackieControlProtocol\n");
129 /* stop event loop */
136 catch (exception & e) {
137 cout << "~MackieControlProtocol caught " << e.what() << endl;
140 cout << "~MackieControlProtocol caught unknown" << endl;
143 DEBUG_TRACE (DEBUG::MackieControl, "finished ~MackieControlProtocol::MackieControlProtocol\n");
149 MackieControlProtocol::thread_init ()
151 struct sched_param rtparam;
153 pthread_set_name (X_("MackieControl"));
155 PBD::notify_gui_about_thread_creation (X_("gui"), pthread_self(), X_("MackieControl"), 2048);
156 ARDOUR::SessionEvent::create_per_thread_pool (X_("MackieControl"), 128);
158 memset (&rtparam, 0, sizeof (rtparam));
159 rtparam.sched_priority = 9; /* XXX should be relative to audio (JACK) thread */
161 if (pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam) != 0) {
162 // do we care? not particularly.
167 MackieControlProtocol::midi_connectivity_established ()
169 for (Surfaces::const_iterator si = surfaces.begin(); si != surfaces.end(); ++si) {
174 // go to the previous track.
175 // Assume that get_sorted_routes().size() > route_table.size()
177 MackieControlProtocol::prev_track()
179 if (_current_initial_bank >= 1) {
180 switch_banks (_current_initial_bank - 1);
184 // go to the next track.
185 // Assume that get_sorted_routes().size() > route_table.size()
187 MackieControlProtocol::next_track()
189 Sorted sorted = get_sorted_routes();
190 if (_current_initial_bank + n_strips() < sorted.size()) {
191 switch_banks (_current_initial_bank + 1);
195 // predicate for sort call in get_sorted_routes
196 struct RouteByRemoteId
198 bool operator () (const boost::shared_ptr<Route> & a, const boost::shared_ptr<Route> & b) const
200 return a->remote_control_id() < b->remote_control_id();
203 bool operator () (const Route & a, const Route & b) const
205 return a.remote_control_id() < b.remote_control_id();
208 bool operator () (const Route * a, const Route * b) const
210 return a->remote_control_id() < b->remote_control_id();
214 MackieControlProtocol::Sorted
215 MackieControlProtocol::get_sorted_routes()
220 boost::shared_ptr<RouteList> routes = session->get_routes();
221 set<uint32_t> remote_ids;
223 // routes with remote_id 0 should never be added
224 // TODO verify this with ardour devs
225 // remote_ids.insert (0);
227 // sort in remote_id order, and exclude master, control and hidden routes
228 // and any routes that are already set.
230 for (RouteList::iterator it = routes->begin(); it != routes->end(); ++it) {
232 Route & route = **it;
234 if (remote_ids.find (route.remote_control_id()) != remote_ids.end()) {
238 if (route.is_hidden() || route.is_master() || route.is_monitor()) {
242 switch (_view_mode) {
263 sorted.push_back (*it);
264 remote_ids.insert (route.remote_control_id());
267 sort (sorted.begin(), sorted.end(), RouteByRemoteId());
272 MackieControlProtocol::refresh_current_bank()
274 switch_banks (_current_initial_bank, true);
278 MackieControlProtocol::n_strips() const
280 uint32_t strip_count = 0;
282 for (Surfaces::const_iterator si = surfaces.begin(); si != surfaces.end(); ++si) {
283 strip_count += (*si)->n_strips ();
290 MackieControlProtocol::switch_banks (uint32_t initial, bool force)
292 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("switch banking to start at %1 force ? %2 current = %3\n", initial, force, _current_initial_bank));
294 if (initial == _current_initial_bank && !force) {
298 Sorted sorted = get_sorted_routes();
299 uint32_t strip_cnt = n_strips();
301 if (sorted.size() <= strip_cnt && !force) {
302 /* no banking - not enough routes to fill all strips */
306 uint32_t delta = sorted.size() - strip_cnt;
308 if (delta > 0 && initial > delta) {
309 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("not switching to %1\n", initial));
313 _current_initial_bank = initial;
314 _current_selected_track = -1;
316 // Map current bank of routes onto each surface(+strip)
318 if (_current_initial_bank <= sorted.size()) {
320 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("switch to %1, %2, available routes %3\n", _current_initial_bank, strip_cnt, sorted.size()));
322 // link routes to strips
324 Sorted::iterator r = sorted.begin() + _current_initial_bank;
326 for (Surfaces::iterator si = surfaces.begin(); si != surfaces.end(); ++si) {
327 vector<boost::shared_ptr<Route> > routes;
330 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("surface has %1 strips\n", (*si)->n_strips()));
332 for (; r != sorted.end() && added < (*si)->n_strips(); ++r, ++added) {
333 routes.push_back (*r);
336 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("give surface %1 routes\n", routes.size()));
338 (*si)->map_routes (routes);
342 /* reset this to get the right display of view mode after the switch */
343 set_view_mode (_view_mode);
345 /* current bank has not been saved */
346 session->set_dirty();
350 MackieControlProtocol::set_active (bool yn)
358 /* start event loop */
363 connect_session_signals ();
367 /* set up periodic task for metering and automation
370 Glib::RefPtr<Glib::TimeoutSource> periodic_timeout = Glib::TimeoutSource::create (100); // milliseconds
371 periodic_connection = periodic_timeout->connect (sigc::mem_fun (*this, &MackieControlProtocol::periodic));
372 periodic_timeout->attach (main_loop()->get_context());
386 MackieControlProtocol::periodic ()
392 if (needs_ipmidi_restart) {
399 gettimeofday (&now, 0);
401 now_usecs = (now.tv_sec * 1000000) + now.tv_usec;
403 for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
404 (*s)->periodic (now_usecs);
407 update_timecode_display ();
414 MackieControlProtocol::update_timecode_beats_led()
416 switch (_timecode_type) {
417 case ARDOUR::AnyTime::BBT:
418 update_global_led (Led::Beats, on);
419 update_global_led (Led::Timecode, off);
421 case ARDOUR::AnyTime::Timecode:
422 update_global_led (Led::Timecode, on);
423 update_global_led (Led::Beats, off);
427 os << "Unknown Anytime::Type " << _timecode_type;
428 throw runtime_error (os.str());
433 MackieControlProtocol::update_global_button (int id, LedState ls)
435 boost::shared_ptr<Surface> surface = surfaces.front();
437 if (!surface->type() == mcu) {
441 map<int,Control*>::iterator x = surface->controls_by_device_independent_id.find (id);
442 if (x != surface->controls_by_device_independent_id.end()) {
443 Button * button = dynamic_cast<Button*> (x->second);
444 surface->write (button->set_state (ls));
446 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Button %1 not found\n", id));
451 MackieControlProtocol::update_global_led (int id, LedState ls)
453 boost::shared_ptr<Surface> surface = surfaces.front();
455 if (!surface->type() == mcu) {
459 map<int,Control*>::iterator x = surface->controls_by_device_independent_id.find (id);
460 if (x != surface->controls_by_device_independent_id.end()) {
461 Led * led = dynamic_cast<Led*> (x->second);
462 surface->write (led->set_state (ls));
464 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Led %1 not found\n", id));
468 // send messages to surface to set controls to correct values
470 MackieControlProtocol::update_surfaces()
476 // do the initial bank switch to connect signals
477 // _current_initial_bank is initialised by set_state
478 switch_banks (_current_initial_bank, true);
480 // sometimes the jog wheel is a pot
481 surfaces.front()->blank_jog_ring ();
483 // update global buttons and displays
485 notify_record_state_changed();
486 notify_transport_state_changed();
487 update_timecode_beats_led();
491 MackieControlProtocol::connect_session_signals()
493 // receive routes added
494 session->RouteAdded.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&MackieControlProtocol::notify_route_added, this, _1), this);
495 // receive record state toggled
496 session->RecordStateChanged.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&MackieControlProtocol::notify_record_state_changed, this), this);
497 // receive transport state changed
498 session->TransportStateChange.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&MackieControlProtocol::notify_transport_state_changed, this), this);
499 session->TransportLooped.connect (session_connections, MISSING_INVALIDATOR, boost::bind (&MackieControlProtocol::notify_loop_state_changed, this), this);
500 // receive punch-in and punch-out
501 Config->ParameterChanged.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&MackieControlProtocol::notify_parameter_changed, this, _1), this);
502 session->config.ParameterChanged.connect (session_connections, MISSING_INVALIDATOR, boost::bind (&MackieControlProtocol::notify_parameter_changed, this, _1), this);
503 // receive rude solo changed
504 session->SoloActive.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&MackieControlProtocol::notify_solo_active_changed, this, _1), this);
506 // make sure remote id changed signals reach here
507 // see also notify_route_added
508 Sorted sorted = get_sorted_routes();
510 for (Sorted::iterator it = sorted.begin(); it != sorted.end(); ++it) {
511 (*it)->RemoteControlIDChanged.connect (route_connections, MISSING_INVALIDATOR, boost::bind (&MackieControlProtocol::notify_remote_id_changed, this), this);
516 MackieControlProtocol::set_profile (const string& profile_name)
518 if (profile_name == "default") {
519 /* reset to default */
520 _device_profile = DeviceProfile (profile_name);
523 map<string,DeviceProfile>::iterator d = DeviceProfile::device_profiles.find (profile_name);
525 if (d == DeviceProfile::device_profiles.end()) {
529 _device_profile = d->second;
533 MackieControlProtocol::set_device (const string& device_name)
535 map<string,DeviceInfo>::iterator d = DeviceInfo::device_info.find (device_name);
537 if (d == DeviceInfo::device_info.end()) {
546 _device_info = d->second;
550 switch_banks (0, true);
555 MackieControlProtocol::create_surfaces ()
558 surface_type_t stype = mcu;
561 if (_device_info.extenders() == 0) {
562 device_name = X_("mackie control");
564 device_name = X_("mackie control #1");
567 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Create %1 surfaces\n", 1 + _device_info.extenders()));
569 for (uint32_t n = 0; n < 1 + _device_info.extenders(); ++n) {
571 boost::shared_ptr<Surface> surface (new Surface (*this, device_name, n, stype));
572 surfaces.push_back (surface);
574 /* next device will be an extender */
576 if (_device_info.extenders() < 2) {
577 device_name = X_("mackie control #2");
579 snprintf (buf, sizeof (buf), X_("mackie control #%d"), n+2);
584 if (!_device_info.uses_ipmidi()) {
585 _input_bundle->add_channel (
586 surface->port().input_port().name(),
587 ARDOUR::DataType::MIDI,
588 session->engine().make_port_name_non_relative (surface->port().input_port().name())
591 _output_bundle->add_channel (
592 surface->port().output_port().name(),
593 ARDOUR::DataType::MIDI,
594 session->engine().make_port_name_non_relative (surface->port().output_port().name())
599 MIDI::Port& input_port (surface->port().input_port());
601 if ((fd = input_port.selectable ()) >= 0) {
602 Glib::RefPtr<IOSource> psrc = IOSource::create (fd, IO_IN|IO_HUP|IO_ERR);
604 psrc->connect (sigc::bind (sigc::mem_fun (this, &MackieControlProtocol::midi_input_handler), &input_port));
605 psrc->attach (main_loop()->get_context());
607 // glibmm hack: for now, store only the GSource*
609 port_sources.push_back (psrc->gobj());
610 g_source_ref (psrc->gobj());
616 MackieControlProtocol::close()
620 port_connections.drop_connections ();
621 session_connections.drop_connections ();
622 route_connections.drop_connections ();
623 periodic_connection.disconnect ();
629 MackieControlProtocol::get_state()
631 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::get_state\n");
634 // add name of protocol
635 XMLNode* node = new XMLNode (X_("Protocol"));
636 node->add_property (X_("name"), ARDOUR::ControlProtocol::_name);
639 snprintf (buf, sizeof (buf), "%d", _current_initial_bank);
640 node->add_property (X_("bank"), buf);
642 // ipMIDI base port (possibly not used)
643 snprintf (buf, sizeof (buf), "%d", _ipmidi_base);
644 node->add_property (X_("ipmidi-base"), buf);
646 node->add_property (X_("device-profile"), _device_profile.name());
647 node->add_property (X_("device-name"), _device_info.name());
653 MackieControlProtocol::set_state (const XMLNode & node, int /*version*/)
655 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("MackieControlProtocol::set_state: active %1\n", _active));
658 const XMLProperty* prop;
660 bool active = _active;
662 if ((prop = node.property (X_("ipmidi-base"))) != 0) {
663 set_ipmidi_base (atoi (prop->value()));
666 // fetch current bank
667 if ((prop = node.property (X_("bank"))) != 0) {
668 bank = atoi (prop->value());
671 if ((prop = node.property (X_("active"))) != 0) {
672 active = string_is_affirmative (prop->value());
675 if ((prop = node.property (X_("device-name"))) != 0) {
676 set_device (prop->value());
679 if ((prop = node.property (X_("device-profile"))) != 0) {
680 set_profile (prop->value());
686 switch_banks (bank, true);
693 /////////////////////////////////////////////////
694 // handlers for Route signals
695 // TODO should these be part of RouteSignal?
696 // They started off as signal/slot handlers for signals
697 // from Route, but they're also used in polling for automation
698 /////////////////////////////////////////////////
700 // TODO handle plugin automation polling
702 MackieControlProtocol::format_bbt_timecode (framepos_t now_frame)
704 Timecode::BBT_Time bbt_time;
705 session->bbt_time (now_frame, bbt_time);
707 // According to the Logic docs
708 // digits: 888/88/88/888
709 // BBT mode: Bars/Beats/Subdivisions/Ticks
711 os << setw(3) << setfill('0') << bbt_time.bars;
712 os << setw(2) << setfill('0') << bbt_time.beats;
714 // figure out subdivisions per beat
715 const ARDOUR::Meter & meter = session->tempo_map().meter_at (now_frame);
717 if (meter.note_divisor() == 8 && (meter.divisions_per_bar() == 12.0 || meter.divisions_per_bar() == 9.0 || meter.divisions_per_bar() == 6.0)) {
721 uint32_t subdivisions = bbt_time.ticks / uint32_t (Timecode::BBT_Time::ticks_per_beat / subdiv);
722 uint32_t ticks = bbt_time.ticks % uint32_t (Timecode::BBT_Time::ticks_per_beat / subdiv);
724 os << setw(2) << setfill('0') << subdivisions + 1;
725 os << setw(3) << setfill('0') << ticks;
731 MackieControlProtocol::format_timecode_timecode (framepos_t now_frame)
733 Timecode::Time timecode;
734 session->timecode_time (now_frame, timecode);
736 // According to the Logic docs
737 // digits: 888/88/88/888
738 // Timecode mode: Hours/Minutes/Seconds/Frames
740 os << setw(3) << setfill('0') << timecode.hours;
741 os << setw(2) << setfill('0') << timecode.minutes;
742 os << setw(2) << setfill('0') << timecode.seconds;
743 os << setw(3) << setfill('0') << timecode.frames;
749 MackieControlProtocol::update_timecode_display()
751 if (surfaces.empty()) {
755 boost::shared_ptr<Surface> surface = surfaces.front();
757 if (surface->type() != mcu || !_device_info.has_timecode_display()) {
761 // do assignment here so current_frame is fixed
762 framepos_t current_frame = session->transport_frame();
765 switch (_timecode_type) {
766 case ARDOUR::AnyTime::BBT:
767 timecode = format_bbt_timecode (current_frame);
769 case ARDOUR::AnyTime::Timecode:
770 timecode = format_timecode_timecode (current_frame);
776 // only write the timecode string to the MCU if it's changed
777 // since last time. This is to reduce midi bandwidth used.
778 if (timecode != _timecode_last) {
779 surface->display_timecode (timecode, _timecode_last);
780 _timecode_last = timecode;
784 ///////////////////////////////////////////
786 ///////////////////////////////////////////
788 void MackieControlProtocol::notify_parameter_changed (std::string const & p)
790 if (p == "punch-in") {
791 update_global_button (Button::PunchIn, session->config.get_punch_in());
792 } else if (p == "punch-out") {
793 update_global_button (Button::PunchOut, session->config.get_punch_out());
794 } else if (p == "clicking") {
795 // update_global_button (Button::RelayClick, Config->get_clicking());
797 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("parameter changed: %1\n", p));
801 // RouteList is the set of routes that have just been added
803 MackieControlProtocol::notify_route_added (ARDOUR::RouteList & rl)
805 // currently assigned banks are less than the full set of
806 // strips, so activate the new strip now.
808 refresh_current_bank();
810 // otherwise route added, but current bank needs no updating
812 // make sure remote id changes in the new route are handled
813 typedef ARDOUR::RouteList ARS;
815 for (ARS::iterator it = rl.begin(); it != rl.end(); ++it) {
816 (*it)->RemoteControlIDChanged.connect (route_connections, MISSING_INVALIDATOR, boost::bind (&MackieControlProtocol::notify_remote_id_changed, this), this);
821 MackieControlProtocol::notify_solo_active_changed (bool active)
823 boost::shared_ptr<Surface> surface = surfaces.front();
825 map<int,Control*>::iterator x = surface->controls_by_device_independent_id.find (Led::RudeSolo);
826 if (x != surface->controls_by_device_independent_id.end()) {
827 Led* rude_solo = dynamic_cast<Led*> (x->second);
829 surface->write (rude_solo->set_state (active ? flashing : off));
835 MackieControlProtocol::notify_remote_id_changed()
837 Sorted sorted = get_sorted_routes();
838 uint32_t sz = n_strips();
840 // if a remote id has been moved off the end, we need to shift
841 // the current bank backwards.
843 if (sorted.size() - _current_initial_bank < sz) {
844 // but don't shift backwards past the zeroth channel
845 switch_banks (max((Sorted::size_type) 0, sorted.size() - sz));
847 // Otherwise just refresh the current bank
848 refresh_current_bank();
852 ///////////////////////////////////////////
854 ///////////////////////////////////////////
857 MackieControlProtocol::notify_loop_state_changed()
859 update_global_button (Button::Loop, session->get_play_loop());
863 MackieControlProtocol::notify_transport_state_changed()
865 // switch various play and stop buttons on / off
866 update_global_button (Button::Play, session->transport_speed() == 1.0);
867 update_global_button (Button::Stop, !session->transport_rolling());
868 update_global_button (Button::Rewind, session->transport_speed() < 0.0);
869 update_global_button (Button::Ffwd, session->transport_speed() > 1.0);
871 _transport_previously_rolling = session->transport_rolling();
875 MackieControlProtocol::notify_record_state_changed ()
877 boost::shared_ptr<Surface> surface = surfaces.front();
879 /* rec is a tristate */
881 map<int,Control*>::iterator x = surface->controls_by_device_independent_id.find (Button::Record);
882 if (x != surface->controls_by_device_independent_id.end()) {
883 Button * rec = dynamic_cast<Button*> (x->second);
887 switch (session->record_status()) {
888 case Session::Disabled:
889 DEBUG_TRACE (DEBUG::MackieControl, "record state changed to disabled, LED off\n");
892 case Session::Recording:
893 DEBUG_TRACE (DEBUG::MackieControl, "record state changed to recording, LED on\n");
896 case Session::Enabled:
897 DEBUG_TRACE (DEBUG::MackieControl, "record state changed to enabled, LED flashing\n");
902 surface->write (rec->set_state (ls));
907 list<boost::shared_ptr<ARDOUR::Bundle> >
908 MackieControlProtocol::bundles ()
910 list<boost::shared_ptr<ARDOUR::Bundle> > b;
911 b.push_back (_input_bundle);
912 b.push_back (_output_bundle);
917 MackieControlProtocol::do_request (MackieControlUIRequest* req)
919 if (req->type == CallSlot) {
921 call_slot (MISSING_INVALIDATOR, req->the_slot);
923 } else if (req->type == Quit) {
930 MackieControlProtocol::stop ()
938 MackieControlProtocol::update_led (Surface& surface, Button& button, Mackie::LedState ls)
941 surface.port().write (button.set_state (ls));
946 MackieControlProtocol::build_button_map ()
948 /* this maps our device-independent button codes to the methods that handle them.
951 #define DEFINE_BUTTON_HANDLER(b,p,r) button_map.insert (pair<Button::ID,ButtonHandlers> ((b), ButtonHandlers ((p),(r))));
953 DEFINE_BUTTON_HANDLER (Button::IO, &MackieControlProtocol::io_press, &MackieControlProtocol::io_release);
954 DEFINE_BUTTON_HANDLER (Button::Sends, &MackieControlProtocol::sends_press, &MackieControlProtocol::sends_release);
955 DEFINE_BUTTON_HANDLER (Button::Pan, &MackieControlProtocol::pan_press, &MackieControlProtocol::pan_release);
956 DEFINE_BUTTON_HANDLER (Button::Plugin, &MackieControlProtocol::plugin_press, &MackieControlProtocol::plugin_release);
957 DEFINE_BUTTON_HANDLER (Button::Eq, &MackieControlProtocol::eq_press, &MackieControlProtocol::eq_release);
958 DEFINE_BUTTON_HANDLER (Button::Dyn, &MackieControlProtocol::dyn_press, &MackieControlProtocol::dyn_release);
959 DEFINE_BUTTON_HANDLER (Button::Left, &MackieControlProtocol::left_press, &MackieControlProtocol::left_release);
960 DEFINE_BUTTON_HANDLER (Button::Right, &MackieControlProtocol::right_press, &MackieControlProtocol::right_release);
961 DEFINE_BUTTON_HANDLER (Button::ChannelLeft, &MackieControlProtocol::channel_left_press, &MackieControlProtocol::channel_left_release);
962 DEFINE_BUTTON_HANDLER (Button::ChannelRight, &MackieControlProtocol::channel_right_press, &MackieControlProtocol::channel_right_release);
963 DEFINE_BUTTON_HANDLER (Button::Flip, &MackieControlProtocol::flip_press, &MackieControlProtocol::flip_release);
964 DEFINE_BUTTON_HANDLER (Button::Edit, &MackieControlProtocol::edit_press, &MackieControlProtocol::edit_release);
965 DEFINE_BUTTON_HANDLER (Button::NameValue, &MackieControlProtocol::name_value_press, &MackieControlProtocol::name_value_release);
966 DEFINE_BUTTON_HANDLER (Button::TimecodeBeats, &MackieControlProtocol::timecode_beats_press, &MackieControlProtocol::timecode_beats_release);
967 DEFINE_BUTTON_HANDLER (Button::F1, &MackieControlProtocol::F1_press, &MackieControlProtocol::F1_release);
968 DEFINE_BUTTON_HANDLER (Button::F2, &MackieControlProtocol::F2_press, &MackieControlProtocol::F2_release);
969 DEFINE_BUTTON_HANDLER (Button::F3, &MackieControlProtocol::F3_press, &MackieControlProtocol::F3_release);
970 DEFINE_BUTTON_HANDLER (Button::F4, &MackieControlProtocol::F4_press, &MackieControlProtocol::F4_release);
971 DEFINE_BUTTON_HANDLER (Button::F5, &MackieControlProtocol::F5_press, &MackieControlProtocol::F5_release);
972 DEFINE_BUTTON_HANDLER (Button::F6, &MackieControlProtocol::F6_press, &MackieControlProtocol::F6_release);
973 DEFINE_BUTTON_HANDLER (Button::F7, &MackieControlProtocol::F7_press, &MackieControlProtocol::F7_release);
974 DEFINE_BUTTON_HANDLER (Button::F8, &MackieControlProtocol::F8_press, &MackieControlProtocol::F8_release);
975 DEFINE_BUTTON_HANDLER (Button::F9, &MackieControlProtocol::F9_press, &MackieControlProtocol::F9_release);
976 DEFINE_BUTTON_HANDLER (Button::F10, &MackieControlProtocol::F10_press, &MackieControlProtocol::F10_release);
977 DEFINE_BUTTON_HANDLER (Button::F11, &MackieControlProtocol::F11_press, &MackieControlProtocol::F11_release);
978 DEFINE_BUTTON_HANDLER (Button::F12, &MackieControlProtocol::F12_press, &MackieControlProtocol::F12_release);
979 DEFINE_BUTTON_HANDLER (Button::F13, &MackieControlProtocol::F13_press, &MackieControlProtocol::F13_release);
980 DEFINE_BUTTON_HANDLER (Button::F14, &MackieControlProtocol::F14_press, &MackieControlProtocol::F14_release);
981 DEFINE_BUTTON_HANDLER (Button::F15, &MackieControlProtocol::F15_press, &MackieControlProtocol::F15_release);
982 DEFINE_BUTTON_HANDLER (Button::F16, &MackieControlProtocol::F16_press, &MackieControlProtocol::F16_release);
983 DEFINE_BUTTON_HANDLER (Button::Shift, &MackieControlProtocol::shift_press, &MackieControlProtocol::shift_release);
984 DEFINE_BUTTON_HANDLER (Button::Option, &MackieControlProtocol::option_press, &MackieControlProtocol::option_release);
985 DEFINE_BUTTON_HANDLER (Button::Ctrl, &MackieControlProtocol::control_press, &MackieControlProtocol::control_release);
986 DEFINE_BUTTON_HANDLER (Button::CmdAlt, &MackieControlProtocol::cmd_alt_press, &MackieControlProtocol::cmd_alt_release);
987 DEFINE_BUTTON_HANDLER (Button::On, &MackieControlProtocol::on_press, &MackieControlProtocol::on_release);
988 DEFINE_BUTTON_HANDLER (Button::RecReady, &MackieControlProtocol::rec_ready_press, &MackieControlProtocol::rec_ready_release);
989 DEFINE_BUTTON_HANDLER (Button::Undo, &MackieControlProtocol::undo_press, &MackieControlProtocol::undo_release);
990 DEFINE_BUTTON_HANDLER (Button::Save, &MackieControlProtocol::save_press, &MackieControlProtocol::save_release);
991 DEFINE_BUTTON_HANDLER (Button::Touch, &MackieControlProtocol::touch_press, &MackieControlProtocol::touch_release);
992 DEFINE_BUTTON_HANDLER (Button::Redo, &MackieControlProtocol::redo_press, &MackieControlProtocol::redo_release);
993 DEFINE_BUTTON_HANDLER (Button::Marker, &MackieControlProtocol::marker_press, &MackieControlProtocol::marker_release);
994 DEFINE_BUTTON_HANDLER (Button::Enter, &MackieControlProtocol::enter_press, &MackieControlProtocol::enter_release);
995 DEFINE_BUTTON_HANDLER (Button::Cancel, &MackieControlProtocol::cancel_press, &MackieControlProtocol::cancel_release);
996 DEFINE_BUTTON_HANDLER (Button::Mixer, &MackieControlProtocol::mixer_press, &MackieControlProtocol::mixer_release);
997 DEFINE_BUTTON_HANDLER (Button::FrmLeft, &MackieControlProtocol::frm_left_press, &MackieControlProtocol::frm_left_release);
998 DEFINE_BUTTON_HANDLER (Button::FrmRight, &MackieControlProtocol::frm_right_press, &MackieControlProtocol::frm_right_release);
999 DEFINE_BUTTON_HANDLER (Button::Loop, &MackieControlProtocol::loop_press, &MackieControlProtocol::loop_release);
1000 DEFINE_BUTTON_HANDLER (Button::PunchIn, &MackieControlProtocol::punch_in_press, &MackieControlProtocol::punch_in_release);
1001 DEFINE_BUTTON_HANDLER (Button::PunchOut, &MackieControlProtocol::punch_out_press, &MackieControlProtocol::punch_out_release);
1002 DEFINE_BUTTON_HANDLER (Button::Home, &MackieControlProtocol::home_press, &MackieControlProtocol::home_release);
1003 DEFINE_BUTTON_HANDLER (Button::End, &MackieControlProtocol::end_press, &MackieControlProtocol::end_release);
1004 DEFINE_BUTTON_HANDLER (Button::Rewind, &MackieControlProtocol::rewind_press, &MackieControlProtocol::rewind_release);
1005 DEFINE_BUTTON_HANDLER (Button::Ffwd, &MackieControlProtocol::ffwd_press, &MackieControlProtocol::ffwd_release);
1006 DEFINE_BUTTON_HANDLER (Button::Stop, &MackieControlProtocol::stop_press, &MackieControlProtocol::stop_release);
1007 DEFINE_BUTTON_HANDLER (Button::Play, &MackieControlProtocol::play_press, &MackieControlProtocol::play_release);
1008 DEFINE_BUTTON_HANDLER (Button::Record, &MackieControlProtocol::record_press, &MackieControlProtocol::record_release);
1009 DEFINE_BUTTON_HANDLER (Button::CursorUp, &MackieControlProtocol::cursor_up_press, &MackieControlProtocol::cursor_up_release);
1010 DEFINE_BUTTON_HANDLER (Button::CursorDown, &MackieControlProtocol::cursor_down_press, &MackieControlProtocol::cursor_down_release);
1011 DEFINE_BUTTON_HANDLER (Button::CursorLeft, &MackieControlProtocol::cursor_left_press, &MackieControlProtocol::cursor_left_release);
1012 DEFINE_BUTTON_HANDLER (Button::CursorRight, &MackieControlProtocol::cursor_right_press, &MackieControlProtocol::cursor_right_release);
1013 DEFINE_BUTTON_HANDLER (Button::Zoom, &MackieControlProtocol::zoom_press, &MackieControlProtocol::zoom_release);
1014 DEFINE_BUTTON_HANDLER (Button::Scrub, &MackieControlProtocol::scrub_press, &MackieControlProtocol::scrub_release);
1015 DEFINE_BUTTON_HANDLER (Button::UserA, &MackieControlProtocol::user_a_press, &MackieControlProtocol::user_a_release);
1016 DEFINE_BUTTON_HANDLER (Button::UserB, &MackieControlProtocol::user_b_press, &MackieControlProtocol::user_b_release);
1018 DEFINE_BUTTON_HANDLER (Button::Snapshot, &MackieControlProtocol::snapshot_press, &MackieControlProtocol::snapshot_release);
1019 DEFINE_BUTTON_HANDLER (Button::Read, &MackieControlProtocol::read_press, &MackieControlProtocol::read_release);
1020 DEFINE_BUTTON_HANDLER (Button::Write, &MackieControlProtocol::write_press, &MackieControlProtocol::write_release);
1021 DEFINE_BUTTON_HANDLER (Button::FdrGroup, &MackieControlProtocol::fdrgroup_press, &MackieControlProtocol::fdrgroup_release);
1022 DEFINE_BUTTON_HANDLER (Button::ClearSolo, &MackieControlProtocol::clearsolo_press, &MackieControlProtocol::clearsolo_release);
1023 DEFINE_BUTTON_HANDLER (Button::Track, &MackieControlProtocol::track_press, &MackieControlProtocol::track_release);
1024 DEFINE_BUTTON_HANDLER (Button::Send, &MackieControlProtocol::send_press, &MackieControlProtocol::send_release);
1025 DEFINE_BUTTON_HANDLER (Button::MidiTracks, &MackieControlProtocol::miditracks_press, &MackieControlProtocol::miditracks_release);
1026 DEFINE_BUTTON_HANDLER (Button::Inputs, &MackieControlProtocol::inputs_press, &MackieControlProtocol::inputs_release);
1027 DEFINE_BUTTON_HANDLER (Button::AudioTracks, &MackieControlProtocol::audiotracks_press, &MackieControlProtocol::audiotracks_release);
1028 DEFINE_BUTTON_HANDLER (Button::AudioInstruments, &MackieControlProtocol::audioinstruments_press, &MackieControlProtocol::audioinstruments_release);
1029 DEFINE_BUTTON_HANDLER (Button::Aux, &MackieControlProtocol::aux_press, &MackieControlProtocol::aux_release);
1030 DEFINE_BUTTON_HANDLER (Button::Busses, &MackieControlProtocol::busses_press, &MackieControlProtocol::busses_release);
1031 DEFINE_BUTTON_HANDLER (Button::Outputs, &MackieControlProtocol::outputs_press, &MackieControlProtocol::outputs_release);
1032 DEFINE_BUTTON_HANDLER (Button::User, &MackieControlProtocol::user_press, &MackieControlProtocol::user_release);
1033 DEFINE_BUTTON_HANDLER (Button::Trim, &MackieControlProtocol::trim_press, &MackieControlProtocol::trim_release);
1034 DEFINE_BUTTON_HANDLER (Button::Latch, &MackieControlProtocol::latch_press, &MackieControlProtocol::latch_release);
1035 DEFINE_BUTTON_HANDLER (Button::Grp, &MackieControlProtocol::grp_press, &MackieControlProtocol::grp_release);
1036 DEFINE_BUTTON_HANDLER (Button::Nudge, &MackieControlProtocol::nudge_press, &MackieControlProtocol::nudge_release);
1037 DEFINE_BUTTON_HANDLER (Button::Drop, &MackieControlProtocol::drop_press, &MackieControlProtocol::drop_release);
1038 DEFINE_BUTTON_HANDLER (Button::Replace, &MackieControlProtocol::replace_press, &MackieControlProtocol::replace_release);
1039 DEFINE_BUTTON_HANDLER (Button::Click, &MackieControlProtocol::click_press, &MackieControlProtocol::click_release);
1040 DEFINE_BUTTON_HANDLER (Button::View, &MackieControlProtocol::view_press, &MackieControlProtocol::view_release);
1044 MackieControlProtocol::handle_button_event (Surface& surface, Button& button, ButtonState bs)
1046 if (bs != press && bs != release) {
1047 update_led (surface, button, none);
1051 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Handling %1 for button %2 (%3)\n", (bs == press ? "press" : "release"), button.id(),
1052 Button::id_to_name (button.bid())));
1054 /* check profile first */
1056 string action = _device_profile.get_button_action (button.bid(), _modifier_state);
1058 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Looked up action for button %1 with modifier %2, got [%3]\n",
1059 button.bid(), _modifier_state, action));
1061 if (!action.empty()) {
1062 /* if there is a bound action for this button, and this is a press event,
1063 carry out the action. If its a release event, do nothing since we
1064 don't bind to them at all but don't want any other handling to
1068 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("executing action %1\n", action));
1069 access_action (action);
1074 /* lookup using the device-INDEPENDENT button ID */
1076 ButtonMap::iterator b = button_map.find (button.bid());
1078 if (b != button_map.end()) {
1080 ButtonHandlers& bh (b->second);
1084 surface.write (button.set_state ((this->*(bh.press)) (button)));
1087 surface.write (button.set_state ((this->*(bh.release)) (button)));
1093 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("no button handlers for button ID %1 (device ID %2)\n",
1094 button.bid(), button.id()));
1095 error << string_compose ("no button handlers for button ID %1 (device ID %2)\n",
1096 button.bid(), button.id()) << endmsg;
1101 MackieControlProtocol::midi_input_handler (IOCondition ioc, MIDI::Port* port)
1103 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("something happend on %1\n", port->name()));
1111 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("data available on %1\n", port->name()));
1112 framepos_t now = session->engine().frame_time();
1120 MackieControlProtocol::clear_ports ()
1122 _input_bundle->remove_channels ();
1123 _output_bundle->remove_channels ();
1125 for (PortSources::iterator i = port_sources.begin(); i != port_sources.end(); ++i) {
1126 g_source_destroy (*i);
1127 g_source_unref (*i);
1130 port_sources.clear ();
1134 MackieControlProtocol::set_view_mode (ViewMode m)
1138 for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
1139 (*s)->update_view_mode_display ();
1145 MackieControlProtocol::set_flip_mode (bool yn)
1149 for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
1150 (*s)->update_flip_mode_display ();
1155 MackieControlProtocol::set_master_on_surface_strip (uint32_t surface, uint32_t strip_number)
1157 force_special_route_to_strip (session->master_out(), surface, strip_number);
1161 MackieControlProtocol::set_monitor_on_surface_strip (uint32_t surface, uint32_t strip_number)
1163 force_special_route_to_strip (session->monitor_out(), surface, strip_number);
1167 MackieControlProtocol::force_special_route_to_strip (boost::shared_ptr<Route> r, uint32_t surface, uint32_t strip_number)
1173 for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
1174 if ((*s)->number() == surface) {
1175 Strip* strip = (*s)->nth_strip (strip_number);
1177 strip->set_route (session->master_out());
1178 strip->lock_controls ();
1185 MackieControlProtocol::gui_track_selection_changed (ARDOUR::RouteNotificationListPtr rl)
1187 for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
1188 (*s)->gui_selection_changed (rl);
1193 MackieControlProtocol::transport_frame() const
1195 return session->transport_frame();
1199 MackieControlProtocol::add_down_select_button (int surface, int strip)
1201 _down_select_buttons.insert ((surface<<8)|(strip&0xf));
1205 MackieControlProtocol::remove_down_select_button (int surface, int strip)
1207 DownButtonList::iterator x = find (_down_select_buttons.begin(), _down_select_buttons.end(), (surface<<8)|(strip&0xf));
1208 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("removing surface %1 strip %2 from down select buttons\n", surface, strip));
1209 if (x != _down_select_buttons.end()) {
1210 _down_select_buttons.erase (x);
1212 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("surface %1 strip %2 not found in down select buttons\n",
1218 MackieControlProtocol::select_range ()
1222 pull_route_range (_down_select_buttons, routes);
1224 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("select range: found %1 routes\n", routes.size()));
1226 if (!routes.empty()) {
1227 for (RouteList::iterator r = routes.begin(); r != routes.end(); ++r) {
1228 if (r == routes.begin()) {
1229 SetRouteSelection ((*r)->remote_control_id());
1231 AddRouteToSelection ((*r)->remote_control_id());
1238 MackieControlProtocol::add_down_button (AutomationType a, int surface, int strip)
1240 DownButtonMap::iterator m = _down_buttons.find (a);
1242 if (m == _down_buttons.end()) {
1243 _down_buttons[a] = DownButtonList();
1246 _down_buttons[a].insert ((surface<<8)|(strip&0xf));
1250 MackieControlProtocol::remove_down_button (AutomationType a, int surface, int strip)
1252 DownButtonMap::iterator m = _down_buttons.find (a);
1254 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("removing surface %1 strip %2 from down buttons for %3\n", surface, strip, (int) a));
1256 if (m == _down_buttons.end()) {
1260 DownButtonList& l (m->second);
1261 DownButtonList::iterator x = find (l.begin(), l.end(), (surface<<8)|(strip&0xf));
1266 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("surface %1 strip %2 not found in down buttons for %3\n",
1267 surface, strip, (int) a));
1271 MackieControlProtocol::ControlList
1272 MackieControlProtocol::down_controls (AutomationType p)
1274 ControlList controls;
1277 DownButtonMap::iterator m = _down_buttons.find (p);
1279 if (m == _down_buttons.end()) {
1283 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("looking for down buttons for %1, got %2\n",
1284 p, m->second.size()));
1286 pull_route_range (m->second, routes);
1289 case GainAutomation:
1290 for (RouteList::iterator r = routes.begin(); r != routes.end(); ++r) {
1291 controls.push_back ((*r)->gain_control());
1294 case SoloAutomation:
1295 for (RouteList::iterator r = routes.begin(); r != routes.end(); ++r) {
1296 controls.push_back ((*r)->solo_control());
1299 case MuteAutomation:
1300 for (RouteList::iterator r = routes.begin(); r != routes.end(); ++r) {
1301 controls.push_back ((*r)->mute_control());
1304 case RecEnableAutomation:
1305 for (RouteList::iterator r = routes.begin(); r != routes.end(); ++r) {
1306 boost::shared_ptr<Track> trk = boost::dynamic_pointer_cast<Track> (*r);
1308 controls.push_back (trk->rec_enable_control());
1320 struct ButtonRangeSorter {
1321 bool operator() (const uint32_t& a, const uint32_t& b) {
1322 return (a>>8) < (b>>8) // a.surface < b.surface
1324 ((a>>8) == (b>>8) && (a&0xf) < (b&0xf)); // a.surface == b.surface && a.strip < b.strip
1329 MackieControlProtocol::pull_route_range (DownButtonList& down, RouteList& selected)
1331 ButtonRangeSorter cmp;
1337 list<uint32_t> ldown;
1338 ldown.insert (ldown.end(), down.begin(), down.end());
1341 uint32_t first = ldown.front();
1342 uint32_t last = ldown.back ();
1344 uint32_t first_surface = first>>8;
1345 uint32_t first_strip = first&0xf;
1347 uint32_t last_surface = last>>8;
1348 uint32_t last_strip = last&0xf;
1350 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("PRR %5 in list %1.%2 - %3.%4\n", first_surface, first_strip, last_surface, last_strip,
1353 for (Surfaces::const_iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
1355 if ((*s)->number() >= first_surface && (*s)->number() <= last_surface) {
1360 if ((*s)->number() == first_surface) {
1366 if ((*s)->number() == last_surface) {
1370 ls = (*s)->n_strips ();
1373 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("adding strips for surface %1 (%2 .. %3)\n",
1374 (*s)->number(), fs, ls));
1376 for (uint32_t n = fs; n < ls; ++n) {
1377 boost::shared_ptr<Route> r = (*s)->nth_strip (n)->route();
1379 selected.push_back (r);
1387 MackieControlProtocol::set_ipmidi_base (int16_t portnum)
1389 /* this will not be saved without a session save, so .. */
1391 session->set_dirty ();
1393 _ipmidi_base = portnum;
1395 /* if the current device uses ipMIDI we need
1399 if (_active && _device_info.uses_ipmidi()) {
1400 needs_ipmidi_restart = true;
1405 MackieControlProtocol::ipmidi_restart ()
1410 switch_banks (_current_initial_bank, true);
1411 needs_ipmidi_restart = false;