2 Copyright (C) 2006,2007 John Anderson
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 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 "pbd/pthread_utils.h"
38 #include "pbd/error.h"
39 #include "pbd/memento_command.h"
40 #include "pbd/convert.h"
42 #include "ardour/automation_control.h"
43 #include "ardour/dB.h"
44 #include "ardour/debug.h"
45 #include "ardour/location.h"
46 #include "ardour/meter.h"
47 #include "ardour/panner.h"
48 #include "ardour/panner_shell.h"
49 #include "ardour/route.h"
50 #include "ardour/session.h"
51 #include "ardour/tempo.h"
52 #include "ardour/track.h"
53 #include "ardour/types.h"
54 #include "ardour/audioengine.h"
56 #include "mackie_control_protocol.h"
58 #include "midi_byte_array.h"
59 #include "mackie_control_exception.h"
60 #include "surface_port.h"
63 #include "control_group.h"
69 using namespace ARDOUR;
71 using namespace Mackie;
77 #include "pbd/abstract_ui.cc" // instantiate template
79 #define ui_bind(f, ...) boost::protect (boost::bind (f, __VA_ARGS__))
81 const int MackieControlProtocol::MODIFIER_OPTION = 0x1;
82 const int MackieControlProtocol::MODIFIER_CONTROL = 0x2;
83 const int MackieControlProtocol::MODIFIER_SHIFT = 0x3;
84 const int MackieControlProtocol::MODIFIER_CMDALT = 0x4;
86 MackieControlProtocol* MackieControlProtocol::_instance = 0;
88 bool MackieControlProtocol::probe()
93 MackieControlProtocol::MackieControlProtocol (Session& session)
94 : ControlProtocol (session, X_("Mackie"), this)
95 , AbstractUI<MackieControlUIRequest> ("mackie")
96 , _current_initial_bank (0)
97 , _timecode_type (ARDOUR::AnyTime::BBT)
98 , _input_bundle (new ARDOUR::Bundle (_("Mackie Control In"), true))
99 , _output_bundle (new ARDOUR::Bundle (_("Mackie Control Out"), false))
102 , _scrub_mode (false)
105 , _current_selected_track (-1)
106 , _modifier_state (0)
108 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::MackieControlProtocol\n");
110 DeviceInfo::reload_device_info ();
111 set_device (Config->get_mackie_device_name());
113 AudioEngine::instance()->PortConnectedOrDisconnected.connect (
114 audio_engine_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::port_connected_or_disconnected, this, _2, _4, _5),
118 TrackSelectionChanged.connect (gui_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::gui_track_selection_changed, this, _1), this);
125 MackieControlProtocol::~MackieControlProtocol()
127 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::~MackieControlProtocol\n");
134 catch (exception & e) {
135 cout << "~MackieControlProtocol caught " << e.what() << endl;
138 cout << "~MackieControlProtocol caught unknown" << endl;
141 DEBUG_TRACE (DEBUG::MackieControl, "finished ~MackieControlProtocol::MackieControlProtocol\n");
147 MackieControlProtocol::thread_init ()
149 struct sched_param rtparam;
151 pthread_set_name (X_("MackieControl"));
153 PBD::notify_gui_about_thread_creation (X_("gui"), pthread_self(), X_("MackieControl"), 2048);
154 ARDOUR::SessionEvent::create_per_thread_pool (X_("MackieControl"), 128);
156 memset (&rtparam, 0, sizeof (rtparam));
157 rtparam.sched_priority = 9; /* XXX should be relative to audio (JACK) thread */
159 if (pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam) != 0) {
160 // do we care? not particularly.
164 // go to the previous track.
165 // Assume that get_sorted_routes().size() > route_table.size()
167 MackieControlProtocol::prev_track()
169 if (_current_initial_bank >= 1) {
170 switch_banks (_current_initial_bank - 1);
174 // go to the next track.
175 // Assume that get_sorted_routes().size() > route_table.size()
177 MackieControlProtocol::next_track()
179 Sorted sorted = get_sorted_routes();
180 if (_current_initial_bank + n_strips() < sorted.size()) {
181 switch_banks (_current_initial_bank + 1);
185 // predicate for sort call in get_sorted_routes
186 struct RouteByRemoteId
188 bool operator () (const boost::shared_ptr<Route> & a, const boost::shared_ptr<Route> & b) const
190 return a->remote_control_id() < b->remote_control_id();
193 bool operator () (const Route & a, const Route & b) const
195 return a.remote_control_id() < b.remote_control_id();
198 bool operator () (const Route * a, const Route * b) const
200 return a->remote_control_id() < b->remote_control_id();
204 MackieControlProtocol::Sorted
205 MackieControlProtocol::get_sorted_routes()
210 boost::shared_ptr<RouteList> routes = session->get_routes();
211 set<uint32_t> remote_ids;
213 // routes with remote_id 0 should never be added
214 // TODO verify this with ardour devs
215 // remote_ids.insert (0);
217 // sort in remote_id order, and exclude master, control and hidden routes
218 // and any routes that are already set.
220 for (RouteList::iterator it = routes->begin(); it != routes->end(); ++it) {
222 Route & route = **it;
224 if (remote_ids.find (route.remote_control_id()) != remote_ids.end()) {
228 if (route.is_hidden() || route.is_master() || route.is_monitor()) {
232 switch (_view_mode) {
253 sorted.push_back (*it);
254 remote_ids.insert (route.remote_control_id());
257 sort (sorted.begin(), sorted.end(), RouteByRemoteId());
262 MackieControlProtocol::refresh_current_bank()
264 switch_banks (_current_initial_bank, true);
268 MackieControlProtocol::n_strips() const
270 uint32_t strip_count = 0;
272 for (Surfaces::const_iterator si = surfaces.begin(); si != surfaces.end(); ++si) {
273 strip_count += (*si)->n_strips ();
280 MackieControlProtocol::switch_banks (uint32_t initial, bool force)
282 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("switch banking to start at %1 force ? %2 current = %3\n", initial, force, _current_initial_bank));
284 if (initial == _current_initial_bank && !force) {
288 Sorted sorted = get_sorted_routes();
289 uint32_t strip_cnt = n_strips();
291 if (sorted.size() <= strip_cnt && !force) {
292 /* no banking - not enough routes to fill all strips */
296 uint32_t delta = sorted.size() - strip_cnt;
298 if (delta > 0 && initial > delta) {
299 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("not switching to %1\n", initial));
303 _current_initial_bank = initial;
304 _current_selected_track = -1;
306 for (Surfaces::iterator si = surfaces.begin(); si != surfaces.end(); ++si) {
307 (*si)->drop_routes ();
310 // Map current bank of routes onto each surface(+strip)
312 if (_current_initial_bank <= sorted.size()) {
314 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("switch to %1, %2, available routes %3\n", _current_initial_bank, strip_cnt, sorted.size()));
316 // link routes to strips
318 Sorted::iterator r = sorted.begin() + _current_initial_bank;
320 for (Surfaces::iterator si = surfaces.begin(); si != surfaces.end(); ++si) {
321 vector<boost::shared_ptr<Route> > routes;
324 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("surface has %1 strips\n", (*si)->n_strips()));
326 for (; r != sorted.end() && added < (*si)->n_strips(); ++r, ++added) {
327 routes.push_back (*r);
330 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("give surface %1 routes\n", routes.size()));
332 (*si)->map_routes (routes);
336 /* reset this to get the right display of view mode after the switch */
337 set_view_mode (_view_mode);
339 /* current bank has not been saved */
341 session->set_dirty();
345 MackieControlProtocol::set_active (bool yn)
355 /* start event loop */
360 connect_session_signals ();
364 /* set up periodic task for metering and automation
367 Glib::RefPtr<Glib::TimeoutSource> periodic_timeout = Glib::TimeoutSource::create (100); // milliseconds
368 periodic_connection = periodic_timeout->connect (sigc::mem_fun (*this, &MackieControlProtocol::periodic));
369 periodic_timeout->attach (main_loop()->get_context());
378 catch (exception & e) {
379 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("set_active to false because exception caught: %1\n", e.what()));
388 MackieControlProtocol::periodic ()
396 gettimeofday (&now, 0);
398 now_usecs = (now.tv_sec * 1000000) + now.tv_usec;
400 for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
401 (*s)->periodic (now_usecs);
409 MackieControlProtocol::update_timecode_beats_led()
411 switch (_timecode_type) {
412 case ARDOUR::AnyTime::BBT:
413 update_global_led ("beats", on);
414 update_global_led ("timecode", off);
416 case ARDOUR::AnyTime::Timecode:
417 update_global_led ("timecode", on);
418 update_global_led ("beats", off);
422 os << "Unknown Anytime::Type " << _timecode_type;
423 throw runtime_error (os.str());
428 MackieControlProtocol::update_global_button (const string & name, LedState ls)
430 boost::shared_ptr<Surface> surface = surfaces.front();
432 if (!surface->type() == mcu) {
436 if (surface->controls_by_name.find (name) != surface->controls_by_name.end()) {
437 Button * button = dynamic_cast<Button*> (surface->controls_by_name[name]);
438 surface->write (button->set_state (ls));
440 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Button %1 not found\n", name));
445 MackieControlProtocol::update_global_led (const string & name, LedState ls)
447 boost::shared_ptr<Surface> surface = surfaces.front();
449 if (!surface->type() == mcu) {
453 if (surface->controls_by_name.find (name) != surface->controls_by_name.end()) {
454 Led * led = dynamic_cast<Led*> (surface->controls_by_name[name]);
455 surface->write (led->set_state (ls));
457 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Led %1 not found\n", name));
461 // send messages to surface to set controls to correct values
463 MackieControlProtocol::update_surfaces()
469 // do the initial bank switch to connect signals
470 // _current_initial_bank is initialised by set_state
471 switch_banks (_current_initial_bank, true);
473 // sometimes the jog wheel is a pot
474 surfaces.front()->blank_jog_ring ();
476 // update global buttons and displays
478 notify_record_state_changed();
479 notify_transport_state_changed();
480 update_timecode_beats_led();
484 MackieControlProtocol::connect_session_signals()
486 // receive routes added
487 session->RouteAdded.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_route_added, this, _1), this);
488 // receive record state toggled
489 session->RecordStateChanged.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_record_state_changed, this), this);
490 // receive transport state changed
491 session->TransportStateChange.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_transport_state_changed, this), this);
492 session->TransportLooped.connect (session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_loop_state_changed, this), this);
493 // receive punch-in and punch-out
494 Config->ParameterChanged.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_parameter_changed, this, _1), this);
495 session->config.ParameterChanged.connect (session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_parameter_changed, this, _1), this);
496 // receive rude solo changed
497 session->SoloActive.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_solo_active_changed, this, _1), this);
499 // make sure remote id changed signals reach here
500 // see also notify_route_added
501 Sorted sorted = get_sorted_routes();
503 for (Sorted::iterator it = sorted.begin(); it != sorted.end(); ++it) {
504 (*it)->RemoteControlIDChanged.connect (route_connections, MISSING_INVALIDATOR, ui_bind(&MackieControlProtocol::notify_remote_id_changed, this), this);
509 MackieControlProtocol::set_device (const string& device_name)
511 map<string,DeviceInfo>::iterator d = DeviceInfo::device_info.find (device_name);
513 if (d == DeviceInfo::device_info.end()) {
517 _device_info = d->second;
519 /* store it away in a global location */
521 Config->set_mackie_device_name (device_name);
527 switch_banks (0, true);
532 MackieControlProtocol::create_surfaces ()
535 surface_type_t stype = mcu;
538 if (_device_info.extenders() == 0) {
539 device_name = X_("mackie control");
541 device_name = X_("mackie control #1");
544 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Create %1 surfaces\n", 1 + _device_info.extenders()));
546 for (uint32_t n = 0; n < 1 + _device_info.extenders(); ++n) {
548 boost::shared_ptr<Surface> surface (new Surface (*this, device_name, n, stype));
549 surfaces.push_back (surface);
551 /* next device will be an extender */
553 if (_device_info.extenders() < 2) {
554 device_name = X_("mackie control #2");
556 snprintf (buf, sizeof (buf), X_("mackie control #%d"), n+2);
561 _input_bundle->add_channel (
562 surface->port().input_port().name(),
563 ARDOUR::DataType::MIDI,
564 session->engine().make_port_name_non_relative (surface->port().input_port().name())
567 _output_bundle->add_channel (
568 surface->port().output_port().name(),
569 ARDOUR::DataType::MIDI,
570 session->engine().make_port_name_non_relative (surface->port().output_port().name())
574 MIDI::Port& input_port (surface->port().input_port());
576 if ((fd = input_port.selectable ()) >= 0) {
577 Glib::RefPtr<IOSource> psrc = IOSource::create (fd, IO_IN|IO_HUP|IO_ERR);
579 psrc->connect (sigc::bind (sigc::mem_fun (this, &MackieControlProtocol::midi_input_handler), &input_port));
580 psrc->attach (main_loop()->get_context());
582 // glibmm hack: for now, store only the GSource*
584 port_sources.push_back (psrc->gobj());
585 g_source_ref (psrc->gobj());
591 MackieControlProtocol::close()
595 port_connections.drop_connections ();
596 session_connections.drop_connections ();
597 route_connections.drop_connections ();
598 periodic_connection.disconnect ();
604 MackieControlProtocol::get_state()
606 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::get_state\n");
608 // add name of protocol
609 XMLNode* node = new XMLNode (X_("Protocol"));
610 node->add_property (X_("name"), ARDOUR::ControlProtocol::_name);
614 os << _current_initial_bank;
615 node->add_property (X_("bank"), os.str());
617 for (uint32_t n = 0; n < 16; ++n) {
619 s << string_compose ("f%1-action", n+1);
620 node->add_property (s.str().c_str(), f_action (n));
627 MackieControlProtocol::set_state (const XMLNode & node, int /*version*/)
629 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("MackieControlProtocol::set_state: active %1\n", _active));
632 const XMLProperty* prop;
634 // fetch current bank
635 if ((prop = node.property (X_("bank"))) != 0) {
636 string bank = prop->value();
638 uint32_t new_bank = atoi (bank.c_str());
639 if (_current_initial_bank != new_bank) {
640 switch_banks (new_bank);
645 _f_actions.resize (16);
647 for (uint32_t n = 0; n < 16; ++n) {
649 if ((prop = node.property (string_compose ("f%1-action", n+1))) != 0) {
650 action = prop->value();
653 if (action.empty()) {
654 /* default action if nothing is specified */
655 action = string_compose ("Editor/goto-visual-state-%1", n+1);
658 _f_actions[n] = action;
665 /////////////////////////////////////////////////
666 // handlers for Route signals
667 // TODO should these be part of RouteSignal?
668 // They started off as signal/slot handlers for signals
669 // from Route, but they're also used in polling for automation
670 /////////////////////////////////////////////////
672 // TODO handle plugin automation polling
674 MackieControlProtocol::format_bbt_timecode (framepos_t now_frame)
676 Timecode::BBT_Time bbt_time;
677 session->bbt_time (now_frame, bbt_time);
679 // According to the Logic docs
680 // digits: 888/88/88/888
681 // BBT mode: Bars/Beats/Subdivisions/Ticks
683 os << setw(3) << setfill('0') << bbt_time.bars;
684 os << setw(2) << setfill('0') << bbt_time.beats;
686 // figure out subdivisions per beat
687 const ARDOUR::Meter & meter = session->tempo_map().meter_at (now_frame);
689 if (meter.note_divisor() == 8 && (meter.divisions_per_bar() == 12.0 || meter.divisions_per_bar() == 9.0 || meter.divisions_per_bar() == 6.0)) {
693 uint32_t subdivisions = bbt_time.ticks / uint32_t (Timecode::BBT_Time::ticks_per_beat / subdiv);
694 uint32_t ticks = bbt_time.ticks % uint32_t (Timecode::BBT_Time::ticks_per_beat / subdiv);
696 os << setw(2) << setfill('0') << subdivisions + 1;
697 os << setw(3) << setfill('0') << ticks;
703 MackieControlProtocol::format_timecode_timecode (framepos_t now_frame)
705 Timecode::Time timecode;
706 session->timecode_time (now_frame, timecode);
708 // According to the Logic docs
709 // digits: 888/88/88/888
710 // Timecode mode: Hours/Minutes/Seconds/Frames
712 os << setw(3) << setfill('0') << timecode.hours;
713 os << setw(2) << setfill('0') << timecode.minutes;
714 os << setw(2) << setfill('0') << timecode.seconds;
715 os << setw(3) << setfill('0') << timecode.frames;
721 MackieControlProtocol::update_timecode_display()
723 boost::shared_ptr<Surface> surface = surfaces.front();
725 if (surface->type() != mcu || !surface->has_timecode_display()) {
729 // do assignment here so current_frame is fixed
730 framepos_t current_frame = session->transport_frame();
733 switch (_timecode_type) {
734 case ARDOUR::AnyTime::BBT:
735 timecode = format_bbt_timecode (current_frame);
737 case ARDOUR::AnyTime::Timecode:
738 timecode = format_timecode_timecode (current_frame);
744 // only write the timecode string to the MCU if it's changed
745 // since last time. This is to reduce midi bandwidth used.
746 if (timecode != _timecode_last) {
747 surface->display_timecode (timecode, _timecode_last);
748 _timecode_last = timecode;
752 ///////////////////////////////////////////
754 ///////////////////////////////////////////
756 void MackieControlProtocol::notify_parameter_changed (std::string const & p)
758 if (p == "punch-in") {
759 update_global_button ("punch_in", session->config.get_punch_in());
760 } else if (p == "punch-out") {
761 update_global_button ("punch_out", session->config.get_punch_out());
762 } else if (p == "clicking") {
763 update_global_button ("clicking", Config->get_clicking());
765 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("parameter changed: %1\n", p));
769 // RouteList is the set of routes that have just been added
771 MackieControlProtocol::notify_route_added (ARDOUR::RouteList & rl)
773 // currently assigned banks are less than the full set of
774 // strips, so activate the new strip now.
776 refresh_current_bank();
778 // otherwise route added, but current bank needs no updating
780 // make sure remote id changes in the new route are handled
781 typedef ARDOUR::RouteList ARS;
783 for (ARS::iterator it = rl.begin(); it != rl.end(); ++it) {
784 (*it)->RemoteControlIDChanged.connect (route_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_remote_id_changed, this), this);
789 MackieControlProtocol::notify_solo_active_changed (bool active)
791 boost::shared_ptr<Surface> surface = surfaces.front();
793 Led* rude_solo = dynamic_cast<Led*> (surface->controls_by_name["solo"]);
796 surface->write (rude_solo->set_state (active ? flashing : off));
801 MackieControlProtocol::notify_remote_id_changed()
803 Sorted sorted = get_sorted_routes();
804 uint32_t sz = n_strips();
806 // if a remote id has been moved off the end, we need to shift
807 // the current bank backwards.
809 if (sorted.size() - _current_initial_bank < sz) {
810 // but don't shift backwards past the zeroth channel
811 switch_banks (max((Sorted::size_type) 0, sorted.size() - sz));
813 // Otherwise just refresh the current bank
814 refresh_current_bank();
818 ///////////////////////////////////////////
820 ///////////////////////////////////////////
823 MackieControlProtocol::notify_loop_state_changed()
825 update_global_button ("loop", session->get_play_loop());
829 MackieControlProtocol::notify_transport_state_changed()
831 // switch various play and stop buttons on / off
832 update_global_button ("play", session->transport_speed() == 1.0);
833 update_global_button ("stop", !session->transport_rolling());
834 update_global_button ("rewind", session->transport_speed() < 0.0);
835 update_global_button ("ffwd", session->transport_speed() > 1.0);
837 _transport_previously_rolling = session->transport_rolling();
841 MackieControlProtocol::notify_record_state_changed ()
843 /* rec is a tristate */
846 Button * rec = reinterpret_cast<Button*> (surfaces.front()->controls_by_name["record"]);
850 switch (session->record_status()) {
851 case Session::Disabled:
852 DEBUG_TRACE (DEBUG::MackieControl, "record state changed to disabled, LED off\n");
855 case Session::Recording:
856 DEBUG_TRACE (DEBUG::MackieControl, "record state changed to recording, LED on\n");
859 case Session::Enabled:
860 DEBUG_TRACE (DEBUG::MackieControl, "record state changed to enabled, LED flashing\n");
865 surfaces.front()->write (rec->set_state (ls));
867 DEBUG_TRACE (DEBUG::MackieControl, "record button control not found\n");
871 list<boost::shared_ptr<ARDOUR::Bundle> >
872 MackieControlProtocol::bundles ()
874 list<boost::shared_ptr<ARDOUR::Bundle> > b;
875 b.push_back (_input_bundle);
876 b.push_back (_output_bundle);
881 MackieControlProtocol::port_connected_or_disconnected (string a, string b, bool connected)
883 /* If something is connected to one of our output ports, send MIDI to update the surface
884 to whatever state it should have.
891 for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
892 string const n = AudioEngine::instance()->make_port_name_non_relative ((*s)->port().output_port().name ());
893 if (a == n || b == n) {
901 MackieControlProtocol::do_request (MackieControlUIRequest* req)
903 if (req->type == CallSlot) {
905 call_slot (MISSING_INVALIDATOR, req->the_slot);
907 } else if (req->type == Quit) {
914 MackieControlProtocol::stop ()
921 /** Add a timeout so that a control's in_use flag will be reset some time in the future.
922 * @param in_use_control the control whose in_use flag to reset.
923 * @param touch_control a touch control to emit an event for, or 0.
926 MackieControlProtocol::add_in_use_timeout (Surface& surface, Control& in_use_control, boost::weak_ptr<AutomationControl> touched)
928 Glib::RefPtr<Glib::TimeoutSource> timeout (Glib::TimeoutSource::create (250)); // milliseconds
930 in_use_control.in_use_connection.disconnect ();
931 in_use_control.in_use_connection = timeout->connect (
932 sigc::bind (sigc::mem_fun (*this, &MackieControlProtocol::control_in_use_timeout), &surface, &in_use_control, touched));
934 timeout->attach (main_loop()->get_context());
936 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("timeout queued for surface %1, control %2\n",
937 surface.number(), &in_use_control));}
939 /** Handle timeouts to reset in_use for controls that can't
940 * do this by themselves (e.g. pots, and faders without touch support).
941 * @param in_use_control the control whose in_use flag to reset.
942 * @param touch_control a touch control to emit an event for, or 0.
945 MackieControlProtocol::control_in_use_timeout (Surface* surface, Control* in_use_control, boost::weak_ptr<AutomationControl> wtouched)
947 boost::shared_ptr<AutomationControl> touched (wtouched.lock());
949 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("timeout elapsed for surface %1, control %2\n", surface->number(), in_use_control));
951 in_use_control->set_in_use (false);
954 /* end the touch, and mark the end */
955 touched->stop_touch (session->transport_frame(), true);
958 // only call this method once from the timer
963 MackieControlProtocol::update_led (Surface& surface, Button& button, Mackie::LedState ls)
966 surface.port().write (button.set_state (ls));
971 MackieControlProtocol::build_button_map ()
973 #define DEFINE_BUTTON_HANDLER(b,p,r) button_map.insert (pair<int,ButtonHandlers> ((b), ButtonHandlers ((p),(r))));
975 DEFINE_BUTTON_HANDLER (Button::Io, &MackieControlProtocol::io_press, &MackieControlProtocol::io_release);
976 DEFINE_BUTTON_HANDLER (Button::Sends, &MackieControlProtocol::sends_press, &MackieControlProtocol::sends_release);
977 DEFINE_BUTTON_HANDLER (Button::Pan, &MackieControlProtocol::pan_press, &MackieControlProtocol::pan_release);
978 DEFINE_BUTTON_HANDLER (Button::Plugin, &MackieControlProtocol::plugin_press, &MackieControlProtocol::plugin_release);
979 DEFINE_BUTTON_HANDLER (Button::Eq, &MackieControlProtocol::eq_press, &MackieControlProtocol::eq_release);
980 DEFINE_BUTTON_HANDLER (Button::Dyn, &MackieControlProtocol::dyn_press, &MackieControlProtocol::dyn_release);
981 DEFINE_BUTTON_HANDLER (Button::Left, &MackieControlProtocol::left_press, &MackieControlProtocol::left_release);
982 DEFINE_BUTTON_HANDLER (Button::Right, &MackieControlProtocol::right_press, &MackieControlProtocol::right_release);
983 DEFINE_BUTTON_HANDLER (Button::ChannelLeft, &MackieControlProtocol::channel_left_press, &MackieControlProtocol::channel_left_release);
984 DEFINE_BUTTON_HANDLER (Button::ChannelRight, &MackieControlProtocol::channel_right_press, &MackieControlProtocol::channel_right_release);
985 DEFINE_BUTTON_HANDLER (Button::Flip, &MackieControlProtocol::flip_press, &MackieControlProtocol::flip_release);
986 DEFINE_BUTTON_HANDLER (Button::Edit, &MackieControlProtocol::edit_press, &MackieControlProtocol::edit_release);
987 DEFINE_BUTTON_HANDLER (Button::NameValue, &MackieControlProtocol::name_value_press, &MackieControlProtocol::name_value_release);
988 DEFINE_BUTTON_HANDLER (Button::TimecodeBeats, &MackieControlProtocol::timecode_beats_press, &MackieControlProtocol::timecode_beats_release);
989 DEFINE_BUTTON_HANDLER (Button::F1, &MackieControlProtocol::F1_press, &MackieControlProtocol::F1_release);
990 DEFINE_BUTTON_HANDLER (Button::F2, &MackieControlProtocol::F2_press, &MackieControlProtocol::F2_release);
991 DEFINE_BUTTON_HANDLER (Button::F3, &MackieControlProtocol::F3_press, &MackieControlProtocol::F3_release);
992 DEFINE_BUTTON_HANDLER (Button::F4, &MackieControlProtocol::F4_press, &MackieControlProtocol::F4_release);
993 DEFINE_BUTTON_HANDLER (Button::F5, &MackieControlProtocol::F5_press, &MackieControlProtocol::F5_release);
994 DEFINE_BUTTON_HANDLER (Button::F6, &MackieControlProtocol::F6_press, &MackieControlProtocol::F6_release);
995 DEFINE_BUTTON_HANDLER (Button::F7, &MackieControlProtocol::F7_press, &MackieControlProtocol::F7_release);
996 DEFINE_BUTTON_HANDLER (Button::F8, &MackieControlProtocol::F8_press, &MackieControlProtocol::F8_release);
997 DEFINE_BUTTON_HANDLER (Button::F9, &MackieControlProtocol::F9_press, &MackieControlProtocol::F9_release);
998 DEFINE_BUTTON_HANDLER (Button::F10, &MackieControlProtocol::F10_press, &MackieControlProtocol::F10_release);
999 DEFINE_BUTTON_HANDLER (Button::F11, &MackieControlProtocol::F11_press, &MackieControlProtocol::F11_release);
1000 DEFINE_BUTTON_HANDLER (Button::F12, &MackieControlProtocol::F12_press, &MackieControlProtocol::F12_release);
1001 DEFINE_BUTTON_HANDLER (Button::F13, &MackieControlProtocol::F13_press, &MackieControlProtocol::F13_release);
1002 DEFINE_BUTTON_HANDLER (Button::F14, &MackieControlProtocol::F14_press, &MackieControlProtocol::F14_release);
1003 DEFINE_BUTTON_HANDLER (Button::F15, &MackieControlProtocol::F15_press, &MackieControlProtocol::F15_release);
1004 DEFINE_BUTTON_HANDLER (Button::F16, &MackieControlProtocol::F16_press, &MackieControlProtocol::F16_release);
1005 DEFINE_BUTTON_HANDLER (Button::Shift, &MackieControlProtocol::shift_press, &MackieControlProtocol::shift_release);
1006 DEFINE_BUTTON_HANDLER (Button::Option, &MackieControlProtocol::option_press, &MackieControlProtocol::option_release);
1007 DEFINE_BUTTON_HANDLER (Button::Ctrl, &MackieControlProtocol::control_press, &MackieControlProtocol::control_release);
1008 DEFINE_BUTTON_HANDLER (Button::CmdAlt, &MackieControlProtocol::cmd_alt_press, &MackieControlProtocol::cmd_alt_release);
1009 DEFINE_BUTTON_HANDLER (Button::On, &MackieControlProtocol::on_press, &MackieControlProtocol::on_release);
1010 DEFINE_BUTTON_HANDLER (Button::RecReady, &MackieControlProtocol::rec_ready_press, &MackieControlProtocol::rec_ready_release);
1011 DEFINE_BUTTON_HANDLER (Button::Undo, &MackieControlProtocol::undo_press, &MackieControlProtocol::undo_release);
1012 DEFINE_BUTTON_HANDLER (Button::Save, &MackieControlProtocol::save_press, &MackieControlProtocol::save_release);
1013 DEFINE_BUTTON_HANDLER (Button::Touch, &MackieControlProtocol::touch_press, &MackieControlProtocol::touch_release);
1014 DEFINE_BUTTON_HANDLER (Button::Redo, &MackieControlProtocol::redo_press, &MackieControlProtocol::redo_release);
1015 DEFINE_BUTTON_HANDLER (Button::Marker, &MackieControlProtocol::marker_press, &MackieControlProtocol::marker_release);
1016 DEFINE_BUTTON_HANDLER (Button::Enter, &MackieControlProtocol::enter_press, &MackieControlProtocol::enter_release);
1017 DEFINE_BUTTON_HANDLER (Button::Cancel, &MackieControlProtocol::cancel_press, &MackieControlProtocol::cancel_release);
1018 DEFINE_BUTTON_HANDLER (Button::Mixer, &MackieControlProtocol::mixer_press, &MackieControlProtocol::mixer_release);
1019 DEFINE_BUTTON_HANDLER (Button::FrmLeft, &MackieControlProtocol::frm_left_press, &MackieControlProtocol::frm_left_release);
1020 DEFINE_BUTTON_HANDLER (Button::FrmRight, &MackieControlProtocol::frm_right_press, &MackieControlProtocol::frm_right_release);
1021 DEFINE_BUTTON_HANDLER (Button::Loop, &MackieControlProtocol::loop_press, &MackieControlProtocol::loop_release);
1022 DEFINE_BUTTON_HANDLER (Button::PunchIn, &MackieControlProtocol::punch_in_press, &MackieControlProtocol::punch_in_release);
1023 DEFINE_BUTTON_HANDLER (Button::PunchOut, &MackieControlProtocol::punch_out_press, &MackieControlProtocol::punch_out_release);
1024 DEFINE_BUTTON_HANDLER (Button::Home, &MackieControlProtocol::home_press, &MackieControlProtocol::home_release);
1025 DEFINE_BUTTON_HANDLER (Button::End, &MackieControlProtocol::end_press, &MackieControlProtocol::end_release);
1026 DEFINE_BUTTON_HANDLER (Button::Rewind, &MackieControlProtocol::rewind_press, &MackieControlProtocol::rewind_release);
1027 DEFINE_BUTTON_HANDLER (Button::Ffwd, &MackieControlProtocol::ffwd_press, &MackieControlProtocol::ffwd_release);
1028 DEFINE_BUTTON_HANDLER (Button::Stop, &MackieControlProtocol::stop_press, &MackieControlProtocol::stop_release);
1029 DEFINE_BUTTON_HANDLER (Button::Play, &MackieControlProtocol::play_press, &MackieControlProtocol::play_release);
1030 DEFINE_BUTTON_HANDLER (Button::Record, &MackieControlProtocol::record_press, &MackieControlProtocol::record_release);
1031 DEFINE_BUTTON_HANDLER (Button::CursorUp, &MackieControlProtocol::cursor_up_press, &MackieControlProtocol::cursor_up_release);
1032 DEFINE_BUTTON_HANDLER (Button::CursorDown, &MackieControlProtocol::cursor_down_press, &MackieControlProtocol::cursor_down_release);
1033 DEFINE_BUTTON_HANDLER (Button::CursorLeft, &MackieControlProtocol::cursor_left_press, &MackieControlProtocol::cursor_left_release);
1034 DEFINE_BUTTON_HANDLER (Button::CursorRight, &MackieControlProtocol::cursor_right_press, &MackieControlProtocol::cursor_right_release);
1035 DEFINE_BUTTON_HANDLER (Button::Zoom, &MackieControlProtocol::zoom_press, &MackieControlProtocol::zoom_release);
1036 DEFINE_BUTTON_HANDLER (Button::Scrub, &MackieControlProtocol::scrub_press, &MackieControlProtocol::scrub_release);
1037 DEFINE_BUTTON_HANDLER (Button::UserA, &MackieControlProtocol::user_a_press, &MackieControlProtocol::user_a_release);
1038 DEFINE_BUTTON_HANDLER (Button::UserB, &MackieControlProtocol::user_b_press, &MackieControlProtocol::user_b_release);
1042 MackieControlProtocol::handle_button_event (Surface& surface, Button& button, ButtonState bs)
1044 if (bs != press && bs != release) {
1045 update_led (surface, button, none);
1049 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Handling %1 for button %2\n", (bs == press ? "press" : "release"), button.id()));
1051 ButtonMap::iterator b = button_map.find (button.id());
1053 if (b != button_map.end()) {
1055 ButtonHandlers& bh (b->second);
1059 surface.write (button.set_state ((this->*(bh.press)) (button)));
1061 surface.write (button.set_state ((this->*(bh.release)) (button)));
1067 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("no button handlers for ID %1\n", button.id()));
1072 MackieControlProtocol::midi_input_handler (IOCondition ioc, MIDI::Port* port)
1074 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("something happend on %1\n", port->name()));
1082 CrossThreadChannel::drain (port->selectable());
1084 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("data available on %1\n", port->name()));
1085 framepos_t now = session->engine().frame_time();
1093 MackieControlProtocol::clear_ports ()
1095 _input_bundle->remove_channels ();
1096 _output_bundle->remove_channels ();
1098 for (PortSources::iterator i = port_sources.begin(); i != port_sources.end(); ++i) {
1099 g_source_destroy (*i);
1100 g_source_unref (*i);
1103 port_sources.clear ();
1107 MackieControlProtocol::f_action (uint32_t fn)
1109 if (fn >= _f_actions.size()) {
1113 return _f_actions[fn];
1117 MackieControlProtocol::f_press (uint32_t fn)
1119 string action = f_action (0);
1120 if (!action.empty()) {
1121 access_action (action);
1126 MackieControlProtocol::set_view_mode (ViewMode m)
1130 for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
1131 (*s)->update_view_mode_display ();
1137 MackieControlProtocol::set_flip_mode (bool yn)
1141 for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
1142 (*s)->update_flip_mode_display ();
1147 MackieControlProtocol::set_master_on_surface_strip (uint32_t surface, uint32_t strip_number)
1149 force_special_route_to_strip (session->master_out(), surface, strip_number);
1153 MackieControlProtocol::set_monitor_on_surface_strip (uint32_t surface, uint32_t strip_number)
1155 force_special_route_to_strip (session->monitor_out(), surface, strip_number);
1159 MackieControlProtocol::force_special_route_to_strip (boost::shared_ptr<Route> r, uint32_t surface, uint32_t strip_number)
1165 for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
1166 if ((*s)->number() == surface) {
1167 Strip* strip = (*s)->nth_strip (strip_number);
1169 strip->set_route (session->master_out());
1170 strip->lock_controls ();
1177 MackieControlProtocol::gui_track_selection_changed (ARDOUR::RouteNotificationListPtr rl)
1179 for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
1180 (*s)->gui_selection_changed (rl);
1185 MackieControlProtocol::transport_frame() const
1187 return session->transport_frame();
1191 MackieControlProtocol::add_down_select_button (int surface, int strip)
1193 _down_select_buttons.insert ((surface<<8)|(strip&0xf));
1197 MackieControlProtocol::remove_down_select_button (int surface, int strip)
1199 DownButtonList::iterator x = find (_down_select_buttons.begin(), _down_select_buttons.end(), (surface<<8)|(strip&0xf));
1200 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("removing surface %1 strip %2 from down select buttons\n", surface, strip));
1201 if (x != _down_select_buttons.end()) {
1202 _down_select_buttons.erase (x);
1204 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("surface %1 strip %2 not found in down select buttons\n",
1210 MackieControlProtocol::select_range ()
1214 pull_route_range (_down_select_buttons, routes);
1216 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("select range: found %1 routes\n", routes.size()));
1218 if (!routes.empty()) {
1219 for (RouteList::iterator r = routes.begin(); r != routes.end(); ++r) {
1220 if (r == routes.begin()) {
1221 SetRouteSelection ((*r)->remote_control_id());
1223 AddRouteToSelection ((*r)->remote_control_id());
1230 MackieControlProtocol::add_down_button (AutomationType a, int surface, int strip)
1232 DownButtonMap::iterator m = _down_buttons.find (a);
1234 if (m == _down_buttons.end()) {
1235 _down_buttons[a] = DownButtonList();
1238 _down_buttons[a].insert ((surface<<8)|(strip&0xf));
1242 MackieControlProtocol::remove_down_button (AutomationType a, int surface, int strip)
1244 DownButtonMap::iterator m = _down_buttons.find (a);
1246 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("removing surface %1 strip %2 from down buttons for %3\n", surface, strip, (int) a));
1248 if (m == _down_buttons.end()) {
1252 DownButtonList& l (m->second);
1253 DownButtonList::iterator x = find (l.begin(), l.end(), (surface<<8)|(strip&0xf));
1258 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("surface %1 strip %2 not found in down buttons for %3\n",
1259 surface, strip, (int) a));
1263 MackieControlProtocol::ControlList
1264 MackieControlProtocol::down_controls (AutomationType p)
1266 ControlList controls;
1269 DownButtonMap::iterator m = _down_buttons.find (p);
1271 if (m == _down_buttons.end()) {
1275 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("looking for down buttons for %1, got %2\n",
1276 p, m->second.size()));
1278 pull_route_range (m->second, routes);
1281 case GainAutomation:
1282 for (RouteList::iterator r = routes.begin(); r != routes.end(); ++r) {
1283 controls.push_back ((*r)->gain_control());
1286 case SoloAutomation:
1287 for (RouteList::iterator r = routes.begin(); r != routes.end(); ++r) {
1288 controls.push_back ((*r)->solo_control());
1291 case MuteAutomation:
1292 for (RouteList::iterator r = routes.begin(); r != routes.end(); ++r) {
1293 controls.push_back ((*r)->mute_control());
1296 case RecEnableAutomation:
1297 for (RouteList::iterator r = routes.begin(); r != routes.end(); ++r) {
1298 boost::shared_ptr<Track> trk = boost::dynamic_pointer_cast<Track> (*r);
1300 controls.push_back (trk->rec_enable_control());
1312 struct ButtonRangeSorter {
1313 bool operator() (const uint32_t& a, const uint32_t& b) {
1314 return (a>>8) < (b>>8) // a.surface < b.surface
1316 ((a>>8) == (b>>8) && (a&0xf) < (b&0xf)); // a.surface == b.surface && a.strip < b.strip
1321 MackieControlProtocol::pull_route_range (DownButtonList& down, RouteList& selected)
1323 ButtonRangeSorter cmp;
1329 list<uint32_t> ldown;
1330 ldown.insert (ldown.end(), down.begin(), down.end());
1333 uint32_t first = ldown.front();
1334 uint32_t last = ldown.back ();
1336 uint32_t first_surface = first>>8;
1337 uint32_t first_strip = first&0xf;
1339 uint32_t last_surface = last>>8;
1340 uint32_t last_strip = last&0xf;
1342 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("PRR %5 in list %1.%2 - %3.%4\n", first_surface, first_strip, last_surface, last_strip,
1345 for (Surfaces::const_iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
1347 if ((*s)->number() >= first_surface && (*s)->number() <= last_surface) {
1352 if ((*s)->number() == first_surface) {
1358 if ((*s)->number() == last_surface) {
1362 ls = (*s)->n_strips ();
1365 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("adding strips for surface %1 (%2 .. %3)\n",
1366 (*s)->number(), fs, ls));
1368 for (uint32_t n = fs; n < ls; ++n) {
1369 boost::shared_ptr<Route> r = (*s)->nth_strip (n)->route();
1371 selected.push_back (r);