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/dB.h"
43 #include "ardour/debug.h"
44 #include "ardour/location.h"
45 #include "ardour/meter.h"
46 #include "ardour/panner.h"
47 #include "ardour/panner_shell.h"
48 #include "ardour/route.h"
49 #include "ardour/session.h"
50 #include "ardour/tempo.h"
51 #include "ardour/types.h"
52 #include "ardour/audioengine.h"
54 #include "mackie_control_protocol.h"
56 #include "midi_byte_array.h"
57 #include "mackie_control_exception.h"
58 #include "surface_port.h"
61 #include "control_group.h"
67 using namespace ARDOUR;
69 using namespace Mackie;
75 #include "pbd/abstract_ui.cc" // instantiate template
77 #define ui_bind(f, ...) boost::protect (boost::bind (f, __VA_ARGS__))
79 const int MackieControlProtocol::MODIFIER_OPTION = 0x1;
80 const int MackieControlProtocol::MODIFIER_CONTROL = 0x2;
81 const int MackieControlProtocol::MODIFIER_SHIFT = 0x3;
82 const int MackieControlProtocol::MODIFIER_CMDALT = 0x4;
84 MackieControlProtocol* MackieControlProtocol::_instance = 0;
86 bool MackieControlProtocol::probe()
91 MackieControlProtocol::MackieControlProtocol (Session& session)
92 : ControlProtocol (session, X_("Mackie"), this)
93 , AbstractUI<MackieControlUIRequest> ("mackie")
94 , _current_initial_bank (0)
95 , _timecode_type (ARDOUR::AnyTime::BBT)
96 , _input_bundle (new ARDOUR::Bundle (_("Mackie Control In"), true))
97 , _output_bundle (new ARDOUR::Bundle (_("Mackie Control Out"), false))
100 , _scrub_mode (false)
101 , _flip_mode (Normal)
102 , _view_mode (Global)
103 , _current_selected_track (-1)
105 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::MackieControlProtocol\n");
107 DeviceInfo::reload_device_info ();
109 AudioEngine::instance()->PortConnectedOrDisconnected.connect (
110 audio_engine_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::port_connected_or_disconnected, this, _2, _4, _5),
114 TrackSelectionChanged.connect (gui_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::gui_track_selection_changed, this, _1), this);
121 MackieControlProtocol::~MackieControlProtocol()
123 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::~MackieControlProtocol\n");
130 catch (exception & e) {
131 cout << "~MackieControlProtocol caught " << e.what() << endl;
134 cout << "~MackieControlProtocol caught unknown" << endl;
137 DEBUG_TRACE (DEBUG::MackieControl, "finished ~MackieControlProtocol::MackieControlProtocol\n");
143 MackieControlProtocol::thread_init ()
145 struct sched_param rtparam;
147 pthread_set_name (X_("MackieControl"));
149 PBD::notify_gui_about_thread_creation (X_("gui"), pthread_self(), X_("MackieControl"), 2048);
150 ARDOUR::SessionEvent::create_per_thread_pool (X_("MackieControl"), 128);
152 memset (&rtparam, 0, sizeof (rtparam));
153 rtparam.sched_priority = 9; /* XXX should be relative to audio (JACK) thread */
155 if (pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam) != 0) {
156 // do we care? not particularly.
160 // go to the previous track.
161 // Assume that get_sorted_routes().size() > route_table.size()
163 MackieControlProtocol::prev_track()
165 if (_current_initial_bank >= 1) {
166 switch_banks (_current_initial_bank - 1);
170 // go to the next track.
171 // Assume that get_sorted_routes().size() > route_table.size()
173 MackieControlProtocol::next_track()
175 Sorted sorted = get_sorted_routes();
176 if (_current_initial_bank + n_strips() < sorted.size()) {
177 switch_banks (_current_initial_bank + 1);
181 // predicate for sort call in get_sorted_routes
182 struct RouteByRemoteId
184 bool operator () (const boost::shared_ptr<Route> & a, const boost::shared_ptr<Route> & b) const
186 return a->remote_control_id() < b->remote_control_id();
189 bool operator () (const Route & a, const Route & b) const
191 return a.remote_control_id() < b.remote_control_id();
194 bool operator () (const Route * a, const Route * b) const
196 return a->remote_control_id() < b->remote_control_id();
200 MackieControlProtocol::Sorted
201 MackieControlProtocol::get_sorted_routes()
206 boost::shared_ptr<RouteList> routes = session->get_routes();
207 set<uint32_t> remote_ids;
209 // routes with remote_id 0 should never be added
210 // TODO verify this with ardour devs
211 // remote_ids.insert (0);
213 // sort in remote_id order, and exclude master, control and hidden routes
214 // and any routes that are already set.
216 for (RouteList::iterator it = routes->begin(); it != routes->end(); ++it) {
218 Route & route = **it;
220 if (remote_ids.find (route.remote_control_id()) != remote_ids.end()) {
224 if (route.is_hidden() || route.is_master() || route.is_monitor()) {
228 switch (_view_mode) {
249 sorted.push_back (*it);
250 remote_ids.insert (route.remote_control_id());
253 sort (sorted.begin(), sorted.end(), RouteByRemoteId());
258 MackieControlProtocol::refresh_current_bank()
260 switch_banks (_current_initial_bank, true);
264 MackieControlProtocol::n_strips() const
266 uint32_t strip_count = 0;
268 for (Surfaces::const_iterator si = surfaces.begin(); si != surfaces.end(); ++si) {
269 strip_count += (*si)->n_strips ();
276 MackieControlProtocol::switch_banks (uint32_t initial, bool force)
278 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("switch banking to start at %1 force ? %2 current = %3\n", initial, force, _current_initial_bank));
280 if (initial == _current_initial_bank && !force) {
284 Sorted sorted = get_sorted_routes();
285 uint32_t strip_cnt = n_strips();
287 if (sorted.size() <= strip_cnt && !force) {
288 /* no banking - not enough routes to fill all strips */
292 uint32_t delta = sorted.size() - strip_cnt;
294 if (delta > 0 && initial > delta) {
295 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("not switching to %1\n", initial));
299 _current_initial_bank = initial;
300 _current_selected_track = -1;
302 for (Surfaces::iterator si = surfaces.begin(); si != surfaces.end(); ++si) {
303 (*si)->drop_routes ();
306 // Map current bank of routes onto each surface(+strip)
308 if (_current_initial_bank <= sorted.size()) {
310 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("switch to %1, %2, available routes %3\n", _current_initial_bank, strip_cnt, sorted.size()));
312 // link routes to strips
314 Sorted::iterator r = sorted.begin() + _current_initial_bank;
316 for (Surfaces::iterator si = surfaces.begin(); si != surfaces.end(); ++si) {
317 vector<boost::shared_ptr<Route> > routes;
320 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("surface has %1 strips\n", (*si)->n_strips()));
322 for (; r != sorted.end() && added < (*si)->n_strips(); ++r, ++added) {
323 routes.push_back (*r);
324 cerr << "\t\tadded " << (*r)->name() << endl;
327 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("give surface %1 routes\n", routes.size()));
329 (*si)->map_routes (routes);
333 /* reset this to get the right display of view mode after the switch */
334 set_view_mode (_view_mode);
336 /* current bank has not been saved */
338 session->set_dirty();
342 MackieControlProtocol::set_active (bool yn)
352 /* start event loop */
357 connect_session_signals ();
362 /* set up periodic task for metering and automation
365 Glib::RefPtr<Glib::TimeoutSource> periodic_timeout = Glib::TimeoutSource::create (100); // milliseconds
366 periodic_connection = periodic_timeout->connect (sigc::mem_fun (*this, &MackieControlProtocol::periodic));
367 periodic_timeout->attach (main_loop()->get_context());
376 catch (exception & e) {
377 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("set_active to false because exception caught: %1\n", e.what()));
386 MackieControlProtocol::periodic ()
392 for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
401 MackieControlProtocol::update_timecode_beats_led()
403 switch (_timecode_type) {
404 case ARDOUR::AnyTime::BBT:
405 update_global_led ("beats", on);
406 update_global_led ("timecode", off);
408 case ARDOUR::AnyTime::Timecode:
409 update_global_led ("timecode", on);
410 update_global_led ("beats", off);
414 os << "Unknown Anytime::Type " << _timecode_type;
415 throw runtime_error (os.str());
420 MackieControlProtocol::update_global_button (const string & name, LedState ls)
422 boost::shared_ptr<Surface> surface = surfaces.front();
424 if (!surface->type() == mcu) {
428 if (surface->controls_by_name.find (name) != surface->controls_by_name.end()) {
429 Button * button = dynamic_cast<Button*> (surface->controls_by_name[name]);
430 surface->write (button->set_state (ls));
432 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Button %1 not found\n", name));
437 MackieControlProtocol::update_global_led (const string & name, LedState ls)
439 boost::shared_ptr<Surface> surface = surfaces.front();
441 if (!surface->type() == mcu) {
445 if (surface->controls_by_name.find (name) != surface->controls_by_name.end()) {
446 Led * led = dynamic_cast<Led*> (surface->controls_by_name[name]);
447 surface->write (led->set_state (ls));
449 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Led %1 not found\n", name));
453 // send messages to surface to set controls to correct values
455 MackieControlProtocol::update_surfaces()
461 // do the initial bank switch to connect signals
462 // _current_initial_bank is initialised by set_state
463 switch_banks (_current_initial_bank, true);
465 // sometimes the jog wheel is a pot
466 surfaces.front()->blank_jog_ring ();
468 // update global buttons and displays
470 notify_record_state_changed();
471 notify_transport_state_changed();
472 update_timecode_beats_led();
476 MackieControlProtocol::connect_session_signals()
478 // receive routes added
479 session->RouteAdded.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_route_added, this, _1), this);
480 // receive record state toggled
481 session->RecordStateChanged.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_record_state_changed, this), this);
482 // receive transport state changed
483 session->TransportStateChange.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_transport_state_changed, this), this);
484 session->TransportLooped.connect (session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_loop_state_changed, this), this);
485 // receive punch-in and punch-out
486 Config->ParameterChanged.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_parameter_changed, this, _1), this);
487 session->config.ParameterChanged.connect (session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_parameter_changed, this, _1), this);
488 // receive rude solo changed
489 session->SoloActive.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_solo_active_changed, this, _1), this);
491 // make sure remote id changed signals reach here
492 // see also notify_route_added
493 Sorted sorted = get_sorted_routes();
495 for (Sorted::iterator it = sorted.begin(); it != sorted.end(); ++it) {
496 (*it)->RemoteControlIDChanged.connect (route_connections, MISSING_INVALIDATOR, ui_bind(&MackieControlProtocol::notify_remote_id_changed, this), this);
501 MackieControlProtocol::create_surfaces ()
503 string device_name = "mcu";
504 surface_type_t stype = mcu;
506 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Create %1 surfaces\n",
507 1 + ARDOUR::Config->get_mackie_extenders()));
509 for (uint32_t n = 0; n < 1 + ARDOUR::Config->get_mackie_extenders(); ++n) {
511 boost::shared_ptr<Surface> surface (new Surface (*this, session->engine().jack(), device_name, n, stype));
512 surfaces.push_back (surface);
514 device_name = "mcu_xt";
517 _input_bundle->add_channel (
518 surface->port().input_port().name(),
519 ARDOUR::DataType::MIDI,
520 session->engine().make_port_name_non_relative (surface->port().input_port().name())
523 _output_bundle->add_channel (
524 surface->port().output_port().name(),
525 ARDOUR::DataType::MIDI,
526 session->engine().make_port_name_non_relative (surface->port().output_port().name())
530 MIDI::Port& input_port (surface->port().input_port());
532 if ((fd = input_port.selectable ()) >= 0) {
533 Glib::RefPtr<IOSource> psrc = IOSource::create (fd, IO_IN|IO_HUP|IO_ERR);
535 psrc->connect (sigc::bind (sigc::mem_fun (this, &MackieControlProtocol::midi_input_handler), &input_port));
536 psrc->attach (main_loop()->get_context());
538 // glibmm hack: for now, store only the GSource*
540 port_sources.push_back (psrc->gobj());
541 g_source_ref (psrc->gobj());
547 MackieControlProtocol::close()
551 port_connections.drop_connections ();
552 session_connections.drop_connections ();
553 route_connections.drop_connections ();
554 periodic_connection.disconnect ();
560 MackieControlProtocol::get_state()
562 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::get_state\n");
564 // add name of protocol
565 XMLNode* node = new XMLNode (X_("Protocol"));
566 node->add_property (X_("name"), ARDOUR::ControlProtocol::_name);
568 node->add_property (X_("device"), _device_info.name());
572 os << _current_initial_bank;
573 node->add_property (X_("bank"), os.str());
575 for (uint32_t n = 0; n < 16; ++n) {
577 s << string_compose ("f%1-action", n+1);
578 node->add_property (s.str().c_str(), f_action (n));
585 MackieControlProtocol::set_state (const XMLNode & node, int /*version*/)
587 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("MackieControlProtocol::set_state: active %1\n", _active));
590 const XMLProperty* prop;
592 if ((prop = node.property (X_("device"))) != 0) {
593 load_device_info (prop->value());
596 // fetch current bank
597 if ((prop = node.property (X_("bank"))) != 0) {
598 string bank = prop->value();
600 uint32_t new_bank = atoi (bank.c_str());
601 if (_current_initial_bank != new_bank) {
602 switch_banks (new_bank);
607 _f_actions.resize (16);
609 for (uint32_t n = 0; n < 16; ++n) {
611 if ((prop = node.property (string_compose ("f%1-action", n+1))) != 0) {
612 action = prop->value();
615 if (action.empty()) {
616 /* default action if nothing is specified */
617 action = string_compose ("Editor/goto-visual-state-%1", n+1);
620 _f_actions[n] = action;
627 /////////////////////////////////////////////////
628 // handlers for Route signals
629 // TODO should these be part of RouteSignal?
630 // They started off as signal/slot handlers for signals
631 // from Route, but they're also used in polling for automation
632 /////////////////////////////////////////////////
634 // TODO handle plugin automation polling
636 MackieControlProtocol::format_bbt_timecode (framepos_t now_frame)
638 Timecode::BBT_Time bbt_time;
639 session->bbt_time (now_frame, bbt_time);
641 // According to the Logic docs
642 // digits: 888/88/88/888
643 // BBT mode: Bars/Beats/Subdivisions/Ticks
645 os << setw(3) << setfill('0') << bbt_time.bars;
646 os << setw(2) << setfill('0') << bbt_time.beats;
648 // figure out subdivisions per beat
649 const ARDOUR::Meter & meter = session->tempo_map().meter_at (now_frame);
651 if (meter.note_divisor() == 8 && (meter.divisions_per_bar() == 12.0 || meter.divisions_per_bar() == 9.0 || meter.divisions_per_bar() == 6.0)) {
655 uint32_t subdivisions = bbt_time.ticks / uint32_t (Timecode::BBT_Time::ticks_per_beat / subdiv);
656 uint32_t ticks = bbt_time.ticks % uint32_t (Timecode::BBT_Time::ticks_per_beat / subdiv);
658 os << setw(2) << setfill('0') << subdivisions + 1;
659 os << setw(3) << setfill('0') << ticks;
665 MackieControlProtocol::format_timecode_timecode (framepos_t now_frame)
667 Timecode::Time timecode;
668 session->timecode_time (now_frame, timecode);
670 // According to the Logic docs
671 // digits: 888/88/88/888
672 // Timecode mode: Hours/Minutes/Seconds/Frames
674 os << setw(3) << setfill('0') << timecode.hours;
675 os << setw(2) << setfill('0') << timecode.minutes;
676 os << setw(2) << setfill('0') << timecode.seconds;
677 os << setw(3) << setfill('0') << timecode.frames;
683 MackieControlProtocol::update_timecode_display()
685 boost::shared_ptr<Surface> surface = surfaces.front();
687 if (surface->type() != mcu || !surface->has_timecode_display()) {
691 // do assignment here so current_frame is fixed
692 framepos_t current_frame = session->transport_frame();
695 switch (_timecode_type) {
696 case ARDOUR::AnyTime::BBT:
697 timecode = format_bbt_timecode (current_frame);
699 case ARDOUR::AnyTime::Timecode:
700 timecode = format_timecode_timecode (current_frame);
706 // only write the timecode string to the MCU if it's changed
707 // since last time. This is to reduce midi bandwidth used.
708 if (timecode != _timecode_last) {
709 surface->display_timecode (timecode, _timecode_last);
710 _timecode_last = timecode;
714 ///////////////////////////////////////////
716 ///////////////////////////////////////////
718 void MackieControlProtocol::notify_parameter_changed (std::string const & p)
720 if (p == "punch-in") {
721 update_global_button ("punch_in", session->config.get_punch_in());
722 } else if (p == "punch-out") {
723 update_global_button ("punch_out", session->config.get_punch_out());
724 } else if (p == "clicking") {
725 update_global_button ("clicking", Config->get_clicking());
727 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("parameter changed: %1\n", p));
731 // RouteList is the set of routes that have just been added
733 MackieControlProtocol::notify_route_added (ARDOUR::RouteList & rl)
735 // currently assigned banks are less than the full set of
736 // strips, so activate the new strip now.
738 refresh_current_bank();
740 // otherwise route added, but current bank needs no updating
742 // make sure remote id changes in the new route are handled
743 typedef ARDOUR::RouteList ARS;
745 for (ARS::iterator it = rl.begin(); it != rl.end(); ++it) {
746 (*it)->RemoteControlIDChanged.connect (route_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_remote_id_changed, this), this);
751 MackieControlProtocol::notify_solo_active_changed (bool active)
753 boost::shared_ptr<Surface> surface = surfaces.front();
755 Led* rude_solo = dynamic_cast<Led*> (surface->controls_by_name["solo"]);
758 surface->write (rude_solo->set_state (active ? flashing : off));
763 MackieControlProtocol::notify_remote_id_changed()
765 Sorted sorted = get_sorted_routes();
766 uint32_t sz = n_strips();
768 // if a remote id has been moved off the end, we need to shift
769 // the current bank backwards.
771 if (sorted.size() - _current_initial_bank < sz) {
772 // but don't shift backwards past the zeroth channel
773 switch_banks (max((Sorted::size_type) 0, sorted.size() - sz));
775 // Otherwise just refresh the current bank
776 refresh_current_bank();
780 ///////////////////////////////////////////
782 ///////////////////////////////////////////
785 MackieControlProtocol::notify_loop_state_changed()
787 update_global_button ("loop", session->get_play_loop());
791 MackieControlProtocol::notify_transport_state_changed()
793 // switch various play and stop buttons on / off
794 update_global_button ("play", session->transport_speed() == 1.0);
795 update_global_button ("stop", !session->transport_rolling());
796 update_global_button ("rewind", session->transport_speed() < 0.0);
797 update_global_button ("ffwd", session->transport_speed() > 1.0);
799 _transport_previously_rolling = session->transport_rolling();
803 MackieControlProtocol::notify_record_state_changed ()
805 /* rec is a tristate */
808 Button * rec = reinterpret_cast<Button*> (surfaces.front()->controls_by_name["record"]);
812 switch (session->record_status()) {
813 case Session::Disabled:
814 DEBUG_TRACE (DEBUG::MackieControl, "record state changed to disabled, LED off\n");
817 case Session::Recording:
818 DEBUG_TRACE (DEBUG::MackieControl, "record state changed to recording, LED on\n");
821 case Session::Enabled:
822 DEBUG_TRACE (DEBUG::MackieControl, "record state changed to enabled, LED flashing\n");
827 surfaces.front()->write (rec->set_state (ls));
829 DEBUG_TRACE (DEBUG::MackieControl, "record button control not found\n");
833 list<boost::shared_ptr<ARDOUR::Bundle> >
834 MackieControlProtocol::bundles ()
836 list<boost::shared_ptr<ARDOUR::Bundle> > b;
837 b.push_back (_input_bundle);
838 b.push_back (_output_bundle);
843 MackieControlProtocol::port_connected_or_disconnected (string a, string b, bool connected)
845 /* If something is connected to one of our output ports, send MIDI to update the surface
846 to whatever state it should have.
853 for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
854 string const n = AudioEngine::instance()->make_port_name_non_relative ((*s)->port().output_port().name ());
855 if (a == n || b == n) {
863 MackieControlProtocol::do_request (MackieControlUIRequest* req)
865 if (req->type == CallSlot) {
867 call_slot (MISSING_INVALIDATOR, req->the_slot);
869 } else if (req->type == Quit) {
876 MackieControlProtocol::stop ()
883 /** Add a timeout so that a control's in_use flag will be reset some time in the future.
884 * @param in_use_control the control whose in_use flag to reset.
885 * @param touch_control a touch control to emit an event for, or 0.
888 MackieControlProtocol::add_in_use_timeout (Surface& surface, Control& in_use_control, Control* touch_control)
890 Glib::RefPtr<Glib::TimeoutSource> timeout (Glib::TimeoutSource::create (250)); // milliseconds
892 in_use_control.in_use_connection.disconnect ();
893 in_use_control.in_use_connection = timeout->connect (
894 sigc::bind (sigc::mem_fun (*this, &MackieControlProtocol::control_in_use_timeout), &surface, &in_use_control, touch_control));
895 in_use_control.in_use_touch_control = touch_control;
897 timeout->attach (main_loop()->get_context());
899 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("timeout queued for surface %1, control %2 touch control %3\n",
900 surface.number(), &in_use_control, touch_control));}
902 /** Handle timeouts to reset in_use for controls that can't
903 * do this by themselves (e.g. pots, and faders without touch support).
904 * @param in_use_control the control whose in_use flag to reset.
905 * @param touch_control a touch control to emit an event for, or 0.
908 MackieControlProtocol::control_in_use_timeout (Surface* surface, Control* in_use_control, Control* touch_control)
910 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("timeout elapsed for surface %1, control %2 touch control %3\n",
911 surface->number(), in_use_control, touch_control));
913 in_use_control->set_in_use (false);
916 /* figure out what to do here */
919 // only call this method once from the timer
924 MackieControlProtocol::update_led (Surface& surface, Button& button, Mackie::LedState ls)
927 surface.port().write (button.set_state (ls));
932 MackieControlProtocol::build_button_map ()
934 #define DEFINE_BUTTON_HANDLER(b,p,r) button_map.insert (pair<int,ButtonHandlers> ((b), ButtonHandlers ((p),(r))));
936 DEFINE_BUTTON_HANDLER (Button::Io, &MackieControlProtocol::io_press, &MackieControlProtocol::io_release);
937 DEFINE_BUTTON_HANDLER (Button::Sends, &MackieControlProtocol::sends_press, &MackieControlProtocol::sends_release);
938 DEFINE_BUTTON_HANDLER (Button::Pan, &MackieControlProtocol::pan_press, &MackieControlProtocol::pan_release);
939 DEFINE_BUTTON_HANDLER (Button::Plugin, &MackieControlProtocol::plugin_press, &MackieControlProtocol::plugin_release);
940 DEFINE_BUTTON_HANDLER (Button::Eq, &MackieControlProtocol::eq_press, &MackieControlProtocol::eq_release);
941 DEFINE_BUTTON_HANDLER (Button::Dyn, &MackieControlProtocol::dyn_press, &MackieControlProtocol::dyn_release);
942 DEFINE_BUTTON_HANDLER (Button::Left, &MackieControlProtocol::left_press, &MackieControlProtocol::left_release);
943 DEFINE_BUTTON_HANDLER (Button::Right, &MackieControlProtocol::right_press, &MackieControlProtocol::right_release);
944 DEFINE_BUTTON_HANDLER (Button::ChannelLeft, &MackieControlProtocol::channel_left_press, &MackieControlProtocol::channel_left_release);
945 DEFINE_BUTTON_HANDLER (Button::ChannelRight, &MackieControlProtocol::channel_right_press, &MackieControlProtocol::channel_right_release);
946 DEFINE_BUTTON_HANDLER (Button::Flip, &MackieControlProtocol::flip_press, &MackieControlProtocol::flip_release);
947 DEFINE_BUTTON_HANDLER (Button::Edit, &MackieControlProtocol::edit_press, &MackieControlProtocol::edit_release);
948 DEFINE_BUTTON_HANDLER (Button::NameValue, &MackieControlProtocol::name_value_press, &MackieControlProtocol::name_value_release);
949 DEFINE_BUTTON_HANDLER (Button::TimecodeBeats, &MackieControlProtocol::timecode_beats_press, &MackieControlProtocol::timecode_beats_release);
950 DEFINE_BUTTON_HANDLER (Button::F1, &MackieControlProtocol::F1_press, &MackieControlProtocol::F1_release);
951 DEFINE_BUTTON_HANDLER (Button::F2, &MackieControlProtocol::F2_press, &MackieControlProtocol::F2_release);
952 DEFINE_BUTTON_HANDLER (Button::F3, &MackieControlProtocol::F3_press, &MackieControlProtocol::F3_release);
953 DEFINE_BUTTON_HANDLER (Button::F4, &MackieControlProtocol::F4_press, &MackieControlProtocol::F4_release);
954 DEFINE_BUTTON_HANDLER (Button::F5, &MackieControlProtocol::F5_press, &MackieControlProtocol::F5_release);
955 DEFINE_BUTTON_HANDLER (Button::F6, &MackieControlProtocol::F6_press, &MackieControlProtocol::F6_release);
956 DEFINE_BUTTON_HANDLER (Button::F7, &MackieControlProtocol::F7_press, &MackieControlProtocol::F7_release);
957 DEFINE_BUTTON_HANDLER (Button::F8, &MackieControlProtocol::F8_press, &MackieControlProtocol::F8_release);
958 DEFINE_BUTTON_HANDLER (Button::F9, &MackieControlProtocol::F9_press, &MackieControlProtocol::F9_release);
959 DEFINE_BUTTON_HANDLER (Button::F10, &MackieControlProtocol::F10_press, &MackieControlProtocol::F10_release);
960 DEFINE_BUTTON_HANDLER (Button::F11, &MackieControlProtocol::F11_press, &MackieControlProtocol::F11_release);
961 DEFINE_BUTTON_HANDLER (Button::F12, &MackieControlProtocol::F12_press, &MackieControlProtocol::F12_release);
962 DEFINE_BUTTON_HANDLER (Button::F13, &MackieControlProtocol::F13_press, &MackieControlProtocol::F13_release);
963 DEFINE_BUTTON_HANDLER (Button::F14, &MackieControlProtocol::F14_press, &MackieControlProtocol::F14_release);
964 DEFINE_BUTTON_HANDLER (Button::F15, &MackieControlProtocol::F15_press, &MackieControlProtocol::F15_release);
965 DEFINE_BUTTON_HANDLER (Button::F16, &MackieControlProtocol::F16_press, &MackieControlProtocol::F16_release);
966 DEFINE_BUTTON_HANDLER (Button::Shift, &MackieControlProtocol::shift_press, &MackieControlProtocol::shift_release);
967 DEFINE_BUTTON_HANDLER (Button::Option, &MackieControlProtocol::option_press, &MackieControlProtocol::option_release);
968 DEFINE_BUTTON_HANDLER (Button::Ctrl, &MackieControlProtocol::control_press, &MackieControlProtocol::control_release);
969 DEFINE_BUTTON_HANDLER (Button::CmdAlt, &MackieControlProtocol::cmd_alt_press, &MackieControlProtocol::cmd_alt_release);
970 DEFINE_BUTTON_HANDLER (Button::On, &MackieControlProtocol::on_press, &MackieControlProtocol::on_release);
971 DEFINE_BUTTON_HANDLER (Button::RecReady, &MackieControlProtocol::rec_ready_press, &MackieControlProtocol::rec_ready_release);
972 DEFINE_BUTTON_HANDLER (Button::Undo, &MackieControlProtocol::undo_press, &MackieControlProtocol::undo_release);
973 DEFINE_BUTTON_HANDLER (Button::Save, &MackieControlProtocol::save_press, &MackieControlProtocol::save_release);
974 DEFINE_BUTTON_HANDLER (Button::Touch, &MackieControlProtocol::touch_press, &MackieControlProtocol::touch_release);
975 DEFINE_BUTTON_HANDLER (Button::Redo, &MackieControlProtocol::redo_press, &MackieControlProtocol::redo_release);
976 DEFINE_BUTTON_HANDLER (Button::Marker, &MackieControlProtocol::marker_press, &MackieControlProtocol::marker_release);
977 DEFINE_BUTTON_HANDLER (Button::Enter, &MackieControlProtocol::enter_press, &MackieControlProtocol::enter_release);
978 DEFINE_BUTTON_HANDLER (Button::Cancel, &MackieControlProtocol::cancel_press, &MackieControlProtocol::cancel_release);
979 DEFINE_BUTTON_HANDLER (Button::Mixer, &MackieControlProtocol::mixer_press, &MackieControlProtocol::mixer_release);
980 DEFINE_BUTTON_HANDLER (Button::FrmLeft, &MackieControlProtocol::frm_left_press, &MackieControlProtocol::frm_left_release);
981 DEFINE_BUTTON_HANDLER (Button::FrmRight, &MackieControlProtocol::frm_right_press, &MackieControlProtocol::frm_right_release);
982 DEFINE_BUTTON_HANDLER (Button::Loop, &MackieControlProtocol::loop_press, &MackieControlProtocol::loop_release);
983 DEFINE_BUTTON_HANDLER (Button::PunchIn, &MackieControlProtocol::punch_in_press, &MackieControlProtocol::punch_in_release);
984 DEFINE_BUTTON_HANDLER (Button::PunchOut, &MackieControlProtocol::punch_out_press, &MackieControlProtocol::punch_out_release);
985 DEFINE_BUTTON_HANDLER (Button::Home, &MackieControlProtocol::home_press, &MackieControlProtocol::home_release);
986 DEFINE_BUTTON_HANDLER (Button::End, &MackieControlProtocol::end_press, &MackieControlProtocol::end_release);
987 DEFINE_BUTTON_HANDLER (Button::Rewind, &MackieControlProtocol::rewind_press, &MackieControlProtocol::rewind_release);
988 DEFINE_BUTTON_HANDLER (Button::Ffwd, &MackieControlProtocol::ffwd_press, &MackieControlProtocol::ffwd_release);
989 DEFINE_BUTTON_HANDLER (Button::Stop, &MackieControlProtocol::stop_press, &MackieControlProtocol::stop_release);
990 DEFINE_BUTTON_HANDLER (Button::Play, &MackieControlProtocol::play_press, &MackieControlProtocol::play_release);
991 DEFINE_BUTTON_HANDLER (Button::Record, &MackieControlProtocol::record_press, &MackieControlProtocol::record_release);
992 DEFINE_BUTTON_HANDLER (Button::CursorUp, &MackieControlProtocol::cursor_up_press, &MackieControlProtocol::cursor_up_release);
993 DEFINE_BUTTON_HANDLER (Button::CursorDown, &MackieControlProtocol::cursor_down_press, &MackieControlProtocol::cursor_down_release);
994 DEFINE_BUTTON_HANDLER (Button::CursorLeft, &MackieControlProtocol::cursor_left_press, &MackieControlProtocol::cursor_left_release);
995 DEFINE_BUTTON_HANDLER (Button::CursorRight, &MackieControlProtocol::cursor_right_press, &MackieControlProtocol::cursor_right_release);
996 DEFINE_BUTTON_HANDLER (Button::Zoom, &MackieControlProtocol::zoom_press, &MackieControlProtocol::zoom_release);
997 DEFINE_BUTTON_HANDLER (Button::Scrub, &MackieControlProtocol::scrub_press, &MackieControlProtocol::scrub_release);
998 DEFINE_BUTTON_HANDLER (Button::UserA, &MackieControlProtocol::user_a_press, &MackieControlProtocol::user_a_release);
999 DEFINE_BUTTON_HANDLER (Button::UserB, &MackieControlProtocol::user_b_press, &MackieControlProtocol::user_b_release);
1003 MackieControlProtocol::handle_button_event (Surface& surface, Button& button, ButtonState bs)
1005 if (bs != press && bs != release) {
1006 update_led (surface, button, none);
1010 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Handling %1 for button %2\n", (bs == press ? "press" : "release"), button.id()));
1012 ButtonMap::iterator b = button_map.find (button.id());
1014 if (b != button_map.end()) {
1016 ButtonHandlers& bh (b->second);
1020 surface.write (button.set_state ((this->*(bh.press)) (button)));
1022 surface.write (button.set_state ((this->*(bh.release)) (button)));
1028 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("no button handlers for ID %1\n", button.id()));
1033 MackieControlProtocol::select_track (boost::shared_ptr<Route> r)
1035 if (_modifier_state == MODIFIER_SHIFT) {
1036 r->gain_control()->set_value (0.0);
1038 if (_current_selected_track > 0 && r->remote_control_id() == (uint32_t) _current_selected_track) {
1039 UnselectTrack (); /* EMIT SIGNAL */
1040 _current_selected_track = -1;
1042 SelectByRID (r->remote_control_id()); /* EMIT SIGNAL */
1043 _current_selected_track = r->remote_control_id();;
1049 MackieControlProtocol::midi_input_handler (IOCondition ioc, MIDI::Port* port)
1051 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("something happend on %1\n", port->name()));
1059 CrossThreadChannel::drain (port->selectable());
1061 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("data available on %1\n", port->name()));
1062 framepos_t now = session->engine().frame_time();
1070 MackieControlProtocol::clear_ports ()
1072 for (PortSources::iterator i = port_sources.begin(); i != port_sources.end(); ++i) {
1073 g_source_destroy (*i);
1074 g_source_unref (*i);
1077 port_sources.clear ();
1081 MackieControlProtocol::f_action (uint32_t fn)
1083 if (fn >= _f_actions.size()) {
1087 return _f_actions[fn];
1091 MackieControlProtocol::f_press (uint32_t fn)
1093 string action = f_action (0);
1094 if (!action.empty()) {
1095 access_action (action);
1100 MackieControlProtocol::set_view_mode (ViewMode m)
1104 for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
1105 (*s)->update_view_mode_display ();
1111 MackieControlProtocol::set_flip_mode (FlipMode m)
1115 for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
1116 (*s)->update_flip_mode_display ();
1121 MackieControlProtocol::set_master_on_surface_strip (uint32_t surface, uint32_t strip_number)
1123 force_special_route_to_strip (session->master_out(), surface, strip_number);
1127 MackieControlProtocol::set_monitor_on_surface_strip (uint32_t surface, uint32_t strip_number)
1129 force_special_route_to_strip (session->monitor_out(), surface, strip_number);
1133 MackieControlProtocol::force_special_route_to_strip (boost::shared_ptr<Route> r, uint32_t surface, uint32_t strip_number)
1139 for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
1140 if ((*s)->number() == surface) {
1141 Strip* strip = (*s)->nth_strip (strip_number);
1143 strip->set_route (session->master_out());
1144 strip->lock_route ();
1151 MackieControlProtocol::load_device_info (const string& name)
1153 map<string,DeviceInfo>::iterator i = DeviceInfo::device_info.find (name);
1155 if (i == DeviceInfo::device_info.end()) {
1156 error << string_compose (_("No device info for Mackie Control device \"%1\" - ignored"), name) << endmsg;
1160 _device_info = i->second;
1164 MackieControlProtocol::gui_track_selection_changed (ARDOUR::RouteNotificationListPtr rl)
1166 for (Surfaces::iterator s = surfaces.begin(); s != surfaces.end(); ++s) {
1167 (*s)->gui_selection_changed (rl);