enough with umpteen "i18n.h" files. Consolidate on pbd/i18n.h
[ardour.git] / libs / surfaces / mackie / gui.cc
index adaf2df94507f646aff0c6a02e0920696ddb8a9a..06786197e2b0be6a000a03f801326de4c4a37f24 100644 (file)
 #include <gtkmm/scale.h>
 #include <gtkmm/alignment.h>
 
+#include "pbd/error.h"
+#include "pbd/unwind.h"
 #include "pbd/strsplit.h"
+#include "pbd/stacktrace.h"
 
-#include "gtkmm2ext/utils.h"
 #include "gtkmm2ext/actions.h"
+#include "gtkmm2ext/bindings.h"
+#include "gtkmm2ext/gui_thread.h"
+#include "gtkmm2ext/utils.h"
 
+#include "ardour/audioengine.h"
+#include "ardour/port.h"
 #include "ardour/rc_configuration.h"
 
 #include "mackie_control_protocol.h"
 #include "device_info.h"
 #include "gui.h"
+#include "surface.h"
+#include "surface_port.h"
 
-#include "i18n.h"
+#include "pbd/i18n.h"
 
 using namespace std;
-using namespace Mackie;
 using namespace Gtk;
+using namespace ArdourSurface;
+using namespace Mackie;
 
 void*
 MackieControlProtocol::get_gui () const
@@ -51,14 +61,22 @@ MackieControlProtocol::get_gui () const
        if (!_gui) {
                const_cast<MackieControlProtocol*>(this)->build_gui ();
        }
-
+       static_cast<Gtk::Notebook*>(_gui)->show_all();
        return _gui;
 }
 
 void
 MackieControlProtocol::tear_down_gui ()
 {
+       if (_gui) {
+               Gtk::Widget *w = static_cast<Gtk::Widget*>(_gui)->get_parent();
+               if (w) {
+                       w->hide();
+                       delete w;
+               }
+       }
        delete (MackieControlProtocolGUI*) _gui;
+       _gui = 0;
 }
 
 void
@@ -69,98 +87,139 @@ MackieControlProtocol::build_gui ()
 
 MackieControlProtocolGUI::MackieControlProtocolGUI (MackieControlProtocol& p)
        : _cp (p)
+       , table (2, 9)
        , touch_sensitivity_adjustment (0, 0, 9, 1, 4)
        , touch_sensitivity_scale (touch_sensitivity_adjustment)
        , recalibrate_fader_button (_("Recalibrate Faders"))
        , ipmidi_base_port_adjustment (_cp.ipmidi_base(), 0, 32767, 1, 1000)
-       , ipmidi_base_port_spinner (ipmidi_base_port_adjustment)
+       , discover_button (_("Discover Mackie Devices"))
+       , _device_dependent_widget (0)
+       , ignore_active_change (false)
 {
        Gtk::Label* l;
        Gtk::Alignment* align;
+       int row = 0;
 
        set_border_width (12);
 
-       Gtk::Table* table = Gtk::manage (new Gtk::Table (2, 8));
-       table->set_row_spacings (4);
-       table->set_col_spacings (6);
+       table.set_row_spacings (4);
+       table.set_col_spacings (6);
+       table.set_border_width (12);
+       table.set_homogeneous (false);
+
        l = manage (new Gtk::Label (_("Device Type:")));
-       table->attach (*l, 0, 1, 0, 1, AttachOptions(FILL|EXPAND), AttachOptions(0));
-       table->attach (_surface_combo, 1, 2, 0, 1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 20);
+       l->set_alignment (1.0, 0.5);
+       table.attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
+       table.attach (_surface_combo, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
+       row++;
 
        vector<string> surfaces;
-       
+
        for (std::map<std::string,DeviceInfo>::iterator i = DeviceInfo::device_info.begin(); i != DeviceInfo::device_info.end(); ++i) {
                surfaces.push_back (i->first);
        }
        Gtkmm2ext::set_popdown_strings (_surface_combo, surfaces);
-       _surface_combo.set_active_text (p.device_info().name());
        _surface_combo.signal_changed().connect (sigc::mem_fun (*this, &MackieControlProtocolGUI::surface_combo_changed));
 
+       _cp.DeviceChanged.connect (device_change_connection, invalidator (*this), boost::bind (&MackieControlProtocolGUI::device_changed, this), gui_context());
+       _cp.ConnectionChange.connect (connection_change_connection, invalidator (*this), boost::bind (&MackieControlProtocolGUI::connection_handler, this), gui_context());
+
+       ipmidi_base_port_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &MackieControlProtocolGUI::ipmidi_spinner_changed));
+
+       /* device-dependent part */
+
+       device_dependent_row = row;
+
+       if (_device_dependent_widget) {
+               table.remove (*_device_dependent_widget);
+               _device_dependent_widget = 0;
+       }
+
+       _device_dependent_widget = device_dependent_widget ();
+       table.attach (*_device_dependent_widget, 0, 12, row, row+1, AttachOptions(0), AttachOptions(0), 0, 0);
+       row++;
+
+       /* back to the boilerplate */
+
        RadioButtonGroup rb_group = absolute_touch_mode_button.get_group();
        touch_move_mode_button.set_group (rb_group);
 
+       recalibrate_fader_button.signal_clicked().connect (sigc::mem_fun (*this, &MackieControlProtocolGUI::recalibrate_faders));
+       backlight_button.signal_clicked().connect (sigc::mem_fun (*this, &MackieControlProtocolGUI::toggle_backlight));
+
+       touch_sensitivity_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &MackieControlProtocolGUI::touch_sensitive_change));
+       touch_sensitivity_scale.set_update_policy (Gtk::UPDATE_DISCONTINUOUS);
+
        l = manage (new Gtk::Label (_("Button click")));
        l->set_alignment (1.0, 0.5);
-       table->attach (*l, 0, 1, 1, 2, AttachOptions(FILL|EXPAND), AttachOptions (0));
+       table.attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
        align = manage (new Alignment);
        align->set (0.0, 0.5);
        align->add (relay_click_button);
-       table->attach (*align, 1, 2, 1, 2, AttachOptions(FILL|EXPAND), AttachOptions (0));
+       table.attach (*align, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
+       row++;
+
        l = manage (new Gtk::Label (_("Backlight")));
        l->set_alignment (1.0, 0.5);
-       table->attach (*l, 0, 1, 2, 3, AttachOptions(FILL|EXPAND), AttachOptions (0));
+       table.attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
        align = manage (new Alignment);
        align->set (0.0, 0.5);
        align->add (backlight_button);
-       table->attach (*align, 1, 2, 2, 3, AttachOptions(FILL|EXPAND), AttachOptions (0));
+       table.attach (*align, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
+       row++;
+
        l = manage (new Gtk::Label (_("Send Fader Position Only When Touched")));
        l->set_alignment (1.0, 0.5);
-       table->attach (*l, 0, 1, 3, 4, AttachOptions(FILL|EXPAND), AttachOptions (0));
+       table.attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
        align = manage (new Alignment);
        align->set (0.0, 0.5);
        align->add (absolute_touch_mode_button);
-       table->attach (*align, 1, 2, 3, 4, AttachOptions(FILL|EXPAND), AttachOptions (0));
+       table.attach (*align, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
+       row++;
+
        l = manage (new Gtk::Label (_("Send Fader Position When Moved")));
        l->set_alignment (1.0, 0.5);
-       table->attach (*l, 0, 1, 4, 5, AttachOptions(FILL|EXPAND), AttachOptions (0));
+       table.attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
        align = manage (new Alignment);
        align->set (0.0, 0.5);
        align->add (touch_move_mode_button);
-       table->attach (*align, 1, 2, 4, 5, AttachOptions(FILL|EXPAND), AttachOptions (0));
+       table.attach (*align, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
+       row++;
+
        l = manage (new Gtk::Label (_("Fader Touch Sense Sensitivity")));
        l->set_alignment (1.0, 0.5);
-       table->attach (*l, 0, 1, 5, 6, AttachOptions(FILL|EXPAND), AttachOptions (0));
+       table.attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
        touch_sensitivity_scale.property_digits() = 0;
        touch_sensitivity_scale.property_draw_value() = false;
-       table->attach (touch_sensitivity_scale, 1, 2, 5, 6, AttachOptions(FILL|EXPAND), AttachOptions (0));
-       table->attach (recalibrate_fader_button, 1, 2, 6, 7, AttachOptions(FILL|EXPAND), AttachOptions (0));
+       table.attach (touch_sensitivity_scale, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
+       row++;
+       table.attach (recalibrate_fader_button, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
+       row++;
 
-       l = manage (new Gtk::Label (_("ipMIDI Port (lowest)")));
-       l->set_alignment (1.0, 0.5);
-       table->attach (*l, 0, 1, 7, 8, AttachOptions(FILL|EXPAND), AttachOptions (0));
-       table->attach (ipmidi_base_port_spinner, 1, 2, 7, 8, AttachOptions(FILL|EXPAND), AttachOptions (0));
 
-       ipmidi_base_port_spinner.set_sensitive (_cp.device_info().uses_ipmidi());
-       ipmidi_base_port_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &MackieControlProtocolGUI::ipmidi_spinner_changed));
+       table.attach (discover_button, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
+       discover_button.signal_clicked().connect (sigc::mem_fun (*this, &MackieControlProtocolGUI::discover_clicked));
+       row++;
 
        vector<string> profiles;
-       
-       profiles.push_back ("default");
 
        for (std::map<std::string,DeviceProfile>::iterator i = DeviceProfile::device_profiles.begin(); i != DeviceProfile::device_profiles.end(); ++i) {
+               cerr << "add discovered profile " << i->first << endl;
                profiles.push_back (i->first);
        }
        Gtkmm2ext::set_popdown_strings (_profile_combo, profiles);
+       cerr << "set active profile from " << p.device_profile().name() << endl;
        _profile_combo.set_active_text (p.device_profile().name());
        _profile_combo.signal_changed().connect (sigc::mem_fun (*this, &MackieControlProtocolGUI::profile_combo_changed));
 
-       append_page (*table, _("Device Setup"));
-       table->show_all();
+       append_page (table, _("Device Setup"));
+       table.show_all();
 
        /* function key editor */
 
        VBox* fkey_packer = manage (new VBox);
        HBox* profile_packer = manage (new HBox);
+       HBox* observation_packer = manage (new HBox);
 
        l = manage (new Gtk::Label (_("Profile/Settings:")));
        profile_packer->pack_start (*l, false, false);
@@ -168,20 +227,205 @@ MackieControlProtocolGUI::MackieControlProtocolGUI (MackieControlProtocol& p)
        profile_packer->set_spacing (12);
        profile_packer->set_border_width (12);
 
+       l = manage (new Gtk::Label (_("* Button available at the original Mackie MCU PRO or current device if enabled (NOT implemented yet). Device specific name presented.")));
+       observation_packer->pack_start (*l, false, false);
+
        fkey_packer->pack_start (*profile_packer, false, false);
        fkey_packer->pack_start (function_key_scroller, true, true);
+       fkey_packer->pack_start (*observation_packer, false, false);
        fkey_packer->set_spacing (12);
-       function_key_scroller.set_size_request (700,700);
        function_key_scroller.property_shadow_type() = Gtk::SHADOW_NONE;
        function_key_scroller.add (function_key_editor);
        append_page (*fkey_packer, _("Function Keys"));
-       
+
        build_available_action_menu ();
        build_function_key_editor ();
        refresh_function_key_editor ();
        fkey_packer->show_all();
 }
 
+void
+MackieControlProtocolGUI::connection_handler ()
+{
+       /* ignore all changes to combobox active strings here, because we're
+          updating them to match a new ("external") reality - we were called
+          because port connections have changed.
+       */
+
+       PBD::Unwinder<bool> ici (ignore_active_change, true);
+
+       vector<Gtk::ComboBox*>::iterator ic;
+       vector<Gtk::ComboBox*>::iterator oc;
+
+       vector<string> midi_inputs;
+       vector<string> midi_outputs;
+
+       ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsOutput|ARDOUR::IsTerminal), midi_inputs);
+       ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsInput|ARDOUR::IsTerminal), midi_outputs);
+
+       for (ic = input_combos.begin(), oc = output_combos.begin(); ic != input_combos.end() && oc != output_combos.end(); ++ic, ++oc) {
+
+               boost::shared_ptr<Surface> surface = _cp.get_surface_by_raw_pointer ((*ic)->get_data ("surface"));
+
+               if (surface) {
+                       update_port_combos (midi_inputs, midi_outputs, *ic, *oc, surface);
+               }
+       }
+}
+
+void
+MackieControlProtocolGUI::update_port_combos (vector<string> const& midi_inputs, vector<string> const& midi_outputs,
+                                              Gtk::ComboBox* input_combo,
+                                              Gtk::ComboBox* output_combo,
+                                              boost::shared_ptr<Surface> surface)
+{
+       Glib::RefPtr<Gtk::ListStore> input = build_midi_port_list (midi_inputs, true);
+       Glib::RefPtr<Gtk::ListStore> output = build_midi_port_list (midi_outputs, false);
+       bool input_found = false;
+       bool output_found = false;
+       int n;
+
+       input_combo->set_model (input);
+       output_combo->set_model (output);
+
+       Gtk::TreeModel::Children children = input->children();
+       Gtk::TreeModel::Children::iterator i;
+       i = children.begin();
+       ++i; /* skip "Disconnected" */
+
+
+       for (n = 1;  i != children.end(); ++i, ++n) {
+               string port_name = (*i)[midi_port_columns.full_name];
+               if (surface->port().input().connected_to (port_name)) {
+                       input_combo->set_active (n);
+                       input_found = true;
+                       break;
+               }
+       }
+
+       if (!input_found) {
+               input_combo->set_active (0); /* disconnected */
+       }
+
+       children = output->children();
+       i = children.begin();
+       ++i; /* skip "Disconnected" */
+
+       for (n = 1;  i != children.end(); ++i, ++n) {
+               string port_name = (*i)[midi_port_columns.full_name];
+               if (surface->port().output().connected_to (port_name)) {
+                       output_combo->set_active (n);
+                       output_found = true;
+                       break;
+               }
+       }
+
+       if (!output_found) {
+               output_combo->set_active (0); /* disconnected */
+       }
+}
+
+Gtk::Widget*
+MackieControlProtocolGUI::device_dependent_widget ()
+{
+       Gtk::Table* dd_table;
+       Gtk::Label* l;
+       int row = 0;
+
+       uint32_t n_surfaces = 1 + _cp.device_info().extenders();
+
+       if (!_cp.device_info().uses_ipmidi()) {
+               dd_table = Gtk::manage (new Gtk::Table (n_surfaces, 2));
+       } else {
+               dd_table = Gtk::manage (new Gtk::Table (1, 2));
+       }
+
+       dd_table = Gtk::manage (new Gtk::Table (2, n_surfaces));
+       dd_table->set_row_spacings (4);
+       dd_table->set_col_spacings (6);
+       dd_table->set_border_width (12);
+
+       _surface_combo.set_active_text (_cp.device_info().name());
+
+       vector<string> midi_inputs;
+       vector<string> midi_outputs;
+
+       ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsOutput|ARDOUR::IsPhysical), midi_inputs);
+       ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsInput|ARDOUR::IsPhysical), midi_outputs);
+
+       input_combos.clear ();
+       output_combos.clear ();
+
+       if (!_cp.device_info().uses_ipmidi()) {
+
+               for (uint32_t n = 0; n < n_surfaces; ++n) {
+
+                       boost::shared_ptr<Surface> surface = _cp.nth_surface (n);
+
+                       if (!surface) {
+                               PBD::fatal << string_compose (_("programming error: %1\n"), string_compose ("n=%1 surface not found!", n)) << endmsg;
+                               /*NOTREACHED*/
+                       }
+
+                       Gtk::ComboBox* input_combo = manage (new Gtk::ComboBox);
+                       Gtk::ComboBox* output_combo = manage (new Gtk::ComboBox);
+
+                       update_port_combos (midi_inputs, midi_outputs, input_combo, output_combo, surface);
+
+                       input_combo->pack_start (midi_port_columns.short_name);
+                       input_combo->set_data ("surface", surface.get());
+                       input_combos.push_back (input_combo);
+                       output_combo->pack_start (midi_port_columns.short_name);
+                       output_combo->set_data ("surface", surface.get());
+                       output_combos.push_back (output_combo);
+
+                       boost::weak_ptr<Surface> ws (surface);
+                       input_combo->signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &MackieControlProtocolGUI::active_port_changed), input_combo, ws, true));
+                       output_combo->signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &MackieControlProtocolGUI::active_port_changed), output_combo, ws, false));
+
+                       string send_string;
+                       string receive_string;
+
+                       if (n_surfaces > 1) {
+                               if (n == 0) {
+                                       send_string = _("Main surface sends via:");
+                                       receive_string = _("Main surface receives via:");
+                               } else {
+                                       send_string = string_compose (_("Extender %1 sends via:"), n);
+                                       receive_string = string_compose (_("Extender %1 receives via:"), n);
+                               }
+                       } else {
+                               send_string = _("Surface sends via:");
+                               receive_string = _("Surface receives via:");
+                       }
+
+                       l = manage (new Gtk::Label (send_string));
+                       l->set_alignment (1.0, 0.5);
+                       dd_table->attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
+                       dd_table->attach (*input_combo, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
+                       row++;
+
+                       l = manage (new Gtk::Label (receive_string));
+                       l->set_alignment (1.0, 0.5);
+                       dd_table->attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
+                       dd_table->attach (*output_combo, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
+                       row++;
+               }
+
+       } else {
+
+               l = manage (new Gtk::Label (_("ipMIDI Port (lowest)")));
+               l->set_alignment (1.0, 0.5);
+
+               Gtk::SpinButton*  ipmidi_base_port_spinner = manage (new Gtk::SpinButton (ipmidi_base_port_adjustment));
+               dd_table->attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
+               dd_table->attach (*ipmidi_base_port_spinner, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
+               row++;
+       }
+
+       return dd_table;
+}
+
 CellRendererCombo*
 MackieControlProtocolGUI::make_action_renderer (Glib::RefPtr<TreeStore> model, Gtk::TreeModelColumnBase column)
 {
@@ -199,7 +443,7 @@ void
 MackieControlProtocolGUI::build_available_action_menu ()
 {
        /* build a model of all available actions (needs to be tree structured
-        * more) 
+        * more)
         */
 
        available_action_model = TreeStore::create (available_action_columns);
@@ -208,12 +452,13 @@ MackieControlProtocolGUI::build_available_action_menu ()
        vector<string> labels;
        vector<string> tooltips;
        vector<string> keys;
-       vector<AccelKey> bindings;
+       vector<Glib::RefPtr<Gtk::Action> > actions;
+
        typedef std::map<string,TreeIter> NodeMap;
        NodeMap nodes;
        NodeMap::iterator r;
 
-       ActionManager::get_all_actions (labels, paths, tooltips, keys, bindings);
+       Gtkmm2ext::ActionMap::get_all_actions (paths, labels, tooltips, keys, actions);
 
        vector<string>::iterator k;
        vector<string>::iterator p;
@@ -222,6 +467,31 @@ MackieControlProtocolGUI::build_available_action_menu ()
 
        available_action_model->clear ();
 
+       /* Because there are button bindings built in that are not
+          in the key binding map, there needs to be a way to undo
+          a profile edit.
+       */
+       TreeIter rowp;
+       TreeModel::Row parent;
+       rowp = available_action_model->append();
+       parent = *(rowp);
+       parent[available_action_columns.name] = _("Remove Binding");
+
+       /* Key aliasing */
+
+       rowp = available_action_model->append();
+       parent = *(rowp);
+       parent[available_action_columns.name] = _("Shift");
+       rowp = available_action_model->append();
+       parent = *(rowp);
+       parent[available_action_columns.name] = _("Control");
+       rowp = available_action_model->append();
+       parent = *(rowp);
+       parent[available_action_columns.name] = _("Option");
+       rowp = available_action_model->append();
+       parent = *(rowp);
+       parent[available_action_columns.name] = _("CmdAlt");
+
        for (l = labels.begin(), k = keys.begin(), p = paths.begin(), t = tooltips.begin(); l != labels.end(); ++k, ++p, ++t, ++l) {
 
                TreeModel::Row row;
@@ -294,7 +564,7 @@ MackieControlProtocolGUI::build_function_key_editor ()
        col = manage (new TreeViewColumn (_("Plain"), *renderer));
        col->add_attribute (renderer->property_text(), function_key_columns.plain);
        function_key_editor.append_column (*col);
-       
+
        renderer = make_action_renderer (available_action_model, function_key_columns.shift);
        col = manage (new TreeViewColumn (_("Shift"), *renderer));
        col->add_attribute (renderer->property_text(), function_key_columns.shift);
@@ -334,52 +604,87 @@ MackieControlProtocolGUI::refresh_function_key_editor ()
 
        TreeModel::Row row;
        DeviceProfile dp (_cp.device_profile());
+       DeviceInfo di;
 
        for (int n = 0; n < Mackie::Button::FinalGlobalButton; ++n) {
 
                Mackie::Button::ID bid = (Mackie::Button::ID) n;
 
                row = *(function_key_model->append());
-               row[function_key_columns.name] = Mackie::Button::id_to_name (bid);
+               if (di.global_buttons().find (bid) == di.global_buttons().end()) {
+                       row[function_key_columns.name] = Mackie::Button::id_to_name (bid);
+               } else {
+                       row[function_key_columns.name] = di.get_global_button_name (bid) + "*";
+               }
                row[function_key_columns.id] = bid;
 
                Glib::RefPtr<Gtk::Action> act;
                string action;
                const string defstring = "\u2022";
 
-               action = dp.get_button_action (bid, 0);
-               if (action.empty()) {
-                       row[function_key_columns.plain] = defstring;
-               } else {
-                       act = ActionManager::get_action (action.c_str());
-                       if (act) {
-                               row[function_key_columns.plain] = act->get_label();
-                       } else {
+               /* We only allow plain bindings for Fn keys. All others are
+                * reserved for hard-coded actions.
+                */
+
+               if (bid >= Mackie::Button::F1 && bid <= Mackie::Button::F8) {
+
+                       action = dp.get_button_action (bid, 0);
+                       if (action.empty()) {
                                row[function_key_columns.plain] = defstring;
+                       } else {
+                               if (action.find ('/') == string::npos) {
+                                       /* Probably a key alias */
+                                       row[function_key_columns.plain] = action;
+                               } else {
+
+                                       act = ActionManager::get_action (action.c_str());
+                                       if (act) {
+                                               row[function_key_columns.plain] = act->get_label();
+                                       } else {
+                                               row[function_key_columns.plain] = defstring;
+                                       }
+                               }
                        }
                }
 
-               action = dp.get_button_action (bid, MackieControlProtocol::MODIFIER_CONTROL);
-               if (action.empty()) {
-                       row[function_key_columns.control] = defstring;
-               } else {
-                       act = ActionManager::get_action (action.c_str());
-                       if (act) {
-                               row[function_key_columns.control] = act->get_label();
+               /* We only allow plain bindings for Fn keys. All others are
+                * reserved for hard-coded actions.
+                */
+
+               if (bid >= Mackie::Button::F1 && bid <= Mackie::Button::F8) {
+
+                       action = dp.get_button_action (bid, MackieControlProtocol::MODIFIER_SHIFT);
+                       if (action.empty()) {
+                               row[function_key_columns.shift] = defstring;
                        } else {
-                               row[function_key_columns.control] = defstring;
+                               if (action.find ('/') == string::npos) {
+                                       /* Probably a key alias */
+                                       row[function_key_columns.shift] = action;
+                               } else {
+                                       act = ActionManager::get_action (action.c_str());
+                                       if (act) {
+                                               row[function_key_columns.shift] = act->get_label();
+                                       } else {
+                                               row[function_key_columns.shift] = defstring;
+                                       }
+                               }
                        }
                }
 
-               action = dp.get_button_action (bid, MackieControlProtocol::MODIFIER_SHIFT);
+               action = dp.get_button_action (bid, MackieControlProtocol::MODIFIER_CONTROL);
                if (action.empty()) {
-                       row[function_key_columns.shift] = defstring;
+                       row[function_key_columns.control] = defstring;
                } else {
-                       act = ActionManager::get_action (action.c_str());
-                       if (act) {
-                               row[function_key_columns.shift] = act->get_label();
+                       if (action.find ('/') == string::npos) {
+                               /* Probably a key alias */
+                               row[function_key_columns.control] = action;
                        } else {
-                               row[function_key_columns.shift] = defstring;
+                               act = ActionManager::get_action (action.c_str());
+                               if (act) {
+                                       row[function_key_columns.control] = act->get_label();
+                               } else {
+                                       row[function_key_columns.control] = defstring;
+                               }
                        }
                }
 
@@ -387,11 +692,16 @@ MackieControlProtocolGUI::refresh_function_key_editor ()
                if (action.empty()) {
                        row[function_key_columns.option] = defstring;
                } else {
-                       act = ActionManager::get_action (action.c_str());
-                       if (act) {
-                               row[function_key_columns.option] = act->get_label();
+                       if (action.find ('/') == string::npos) {
+                               /* Probably a key alias */
+                               row[function_key_columns.option] = action;
                        } else {
-                               row[function_key_columns.option] = defstring;
+                               act = ActionManager::get_action (action.c_str());
+                               if (act) {
+                                       row[function_key_columns.option] = act->get_label();
+                               } else {
+                                       row[function_key_columns.option] = defstring;
+                               }
                        }
                }
 
@@ -399,11 +709,16 @@ MackieControlProtocolGUI::refresh_function_key_editor ()
                if (action.empty()) {
                        row[function_key_columns.cmdalt] = defstring;
                } else {
-                       act = ActionManager::get_action (action.c_str());
-                       if (act) {
-                               row[function_key_columns.cmdalt] = act->get_label();
+                       if (action.find ('/') == string::npos) {
+                               /* Probably a key alias */
+                               row[function_key_columns.cmdalt] = action;
                        } else {
-                               row[function_key_columns.cmdalt] = defstring;
+                               act = ActionManager::get_action (action.c_str());
+                               if (act) {
+                                       row[function_key_columns.cmdalt] = act->get_label();
+                               } else {
+                                       row[function_key_columns.cmdalt] = defstring;
+                               }
                        }
                }
 
@@ -423,30 +738,39 @@ MackieControlProtocolGUI::refresh_function_key_editor ()
        function_key_editor.set_model (function_key_model);
 }
 
-void 
+void
 MackieControlProtocolGUI::action_changed (const Glib::ustring &sPath, const Glib::ustring &text, TreeModelColumnBase col)
 {
+       // Remove Binding is not in the action map but still valid
+       bool remove (false);
+       if ( text == "Remove Binding") {
+               remove = true;
+       }
        Gtk::TreePath path(sPath);
        Gtk::TreeModel::iterator row = function_key_model->get_iter(path);
 
        if (row) {
 
                std::map<std::string,std::string>::iterator i = action_map.find (text);
-               
+
                if (i == action_map.end()) {
-                       return;
+                       if (!remove) {
+                               return;
+                       }
                }
-
-               cerr << "Changed to " << i->first << " aka " << i->second << endl;
-
                Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (i->second.c_str());
 
-               if (act) {
+               if (act || remove) {
                        /* update visible text, using string supplied by
                           available action model so that it matches and is found
                           within the model.
                        */
-                       (*row).set_value (col.index(), text);
+                       if (remove) {
+                               Glib::ustring dot = "\u2022";
+                               (*row).set_value (col.index(), dot);
+                       } else {
+                               (*row).set_value (col.index(), text);
+                       }
 
                        /* update the current DeviceProfile, using the full
                         * path
@@ -474,7 +798,12 @@ MackieControlProtocolGUI::action_changed (const Glib::ustring &sPath, const Glib
                                modifier = 0;
                        }
 
-                       _cp.device_profile().set_button_action ((*row)[function_key_columns.id], modifier, i->second);
+                       if (remove) {
+                               _cp.device_profile().set_button_action ((*row)[function_key_columns.id], modifier, "");
+                       } else {
+                               _cp.device_profile().set_button_action ((*row)[function_key_columns.id], modifier, i->second);
+                       }
+
                } else {
                        std::cerr << "no such action\n";
                }
@@ -484,13 +813,21 @@ MackieControlProtocolGUI::action_changed (const Glib::ustring &sPath, const Glib
 void
 MackieControlProtocolGUI::surface_combo_changed ()
 {
-       _cp.set_device (_surface_combo.get_active_text());
+       _cp.set_device (_surface_combo.get_active_text(), false);
+}
 
-       /* update ipMIDI field */
+void
+MackieControlProtocolGUI::device_changed ()
+{
+       if (_device_dependent_widget) {
+               table.remove (*_device_dependent_widget);
+               _device_dependent_widget = 0;
+       }
 
-       cerr << "New device called " << _cp.device_info().name() << " with ipMIDI ? " << _cp.device_info().uses_ipmidi() << endl;
+       _device_dependent_widget = device_dependent_widget ();
+       _device_dependent_widget->show_all ();
 
-       ipmidi_base_port_spinner.set_sensitive (_cp.device_info().uses_ipmidi());
+       table.attach (*_device_dependent_widget, 0, 12, device_dependent_row, device_dependent_row+1, AttachOptions(0), AttachOptions(0), 0, 0);
 }
 
 void
@@ -506,6 +843,93 @@ MackieControlProtocolGUI::profile_combo_changed ()
 void
 MackieControlProtocolGUI::ipmidi_spinner_changed ()
 {
-       cerr << "Set IP MIDI base to " << ipmidi_base_port_spinner.get_value() << endl;
-       _cp.set_ipmidi_base ((int16_t) lrintf (ipmidi_base_port_spinner.get_value()));
+       _cp.set_ipmidi_base ((int16_t) lrintf (ipmidi_base_port_adjustment.get_value()));
+}
+
+void
+MackieControlProtocolGUI::discover_clicked ()
+{
+       /* this should help to get things started */
+       _cp.ping_devices ();
+}
+
+void
+MackieControlProtocolGUI::recalibrate_faders ()
+{
+       _cp.recalibrate_faders ();
+}
+
+void
+MackieControlProtocolGUI::toggle_backlight ()
+{
+       _cp.toggle_backlight ();
+}
+
+void
+MackieControlProtocolGUI::touch_sensitive_change ()
+{
+       int sensitivity = (int) touch_sensitivity_adjustment.get_value ();
+       _cp.set_touch_sensitivity (sensitivity);
+}
+
+Glib::RefPtr<Gtk::ListStore>
+MackieControlProtocolGUI::build_midi_port_list (vector<string> const & ports, bool for_input)
+{
+       Glib::RefPtr<Gtk::ListStore> store = ListStore::create (midi_port_columns);
+       TreeModel::Row row;
+
+       row = *store->append ();
+       row[midi_port_columns.full_name] = string();
+       row[midi_port_columns.short_name] = _("Disconnected");
+
+       for (vector<string>::const_iterator p = ports.begin(); p != ports.end(); ++p) {
+               row = *store->append ();
+               row[midi_port_columns.full_name] = *p;
+               std::string pn = ARDOUR::AudioEngine::instance()->get_pretty_name_by_name (*p);
+               if (pn.empty ()) {
+                       pn = (*p).substr ((*p).find (':') + 1);
+               }
+               row[midi_port_columns.short_name] = pn;
+       }
+
+       return store;
+}
+
+void
+MackieControlProtocolGUI::active_port_changed (Gtk::ComboBox* combo, boost::weak_ptr<Surface> ws, bool for_input)
+{
+       if (ignore_active_change) {
+               return;
+       }
+
+       boost::shared_ptr<Surface> surface = ws.lock();
+
+       if (!surface) {
+               return;
+       }
+
+       TreeModel::iterator active = combo->get_active ();
+       string new_port = (*active)[midi_port_columns.full_name];
+
+       if (new_port.empty()) {
+               if (for_input) {
+                       surface->port().input().disconnect_all ();
+               } else {
+                       surface->port().output().disconnect_all ();
+               }
+
+               return;
+       }
+
+       if (for_input) {
+               if (!surface->port().input().connected_to (new_port)) {
+                       surface->port().input().disconnect_all ();
+                       surface->port().input().connect (new_port);
+               }
+       } else {
+               if (!surface->port().output().connected_to (new_port)) {
+                       surface->port().output().disconnect_all ();
+                       surface->port().output().connect (new_port);
+               }
+       }
 }