plug memory leak
[ardour.git] / gtk2_ardour / generic_pluginui.cc
1 /*
2     Copyright (C) 2000 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #ifdef WAF_BUILD
21 #include "gtk2ardour-config.h"
22 #endif
23
24 #include <climits>
25 #include <cerrno>
26 #include <cmath>
27 #include <string>
28 #include <vector>
29
30 #include "pbd/stl_delete.h"
31 #include "pbd/xml++.h"
32 #include "pbd/failed_constructor.h"
33
34 #include <gtkmm2ext/click_box.h>
35 #include <gtkmm2ext/fastmeter.h>
36 #include <gtkmm2ext/barcontroller.h>
37 #include <gtkmm2ext/utils.h>
38 #include <gtkmm2ext/doi.h>
39 #include <gtkmm2ext/slider_controller.h>
40
41 #include "ardour/plugin.h"
42 #include "ardour/plugin_insert.h"
43 #include "ardour/session.h"
44 #ifdef LV2_SUPPORT
45 #include "ardour/lv2_plugin.h"
46 #endif
47
48 #include "ardour_ui.h"
49 #include "prompter.h"
50 #include "plugin_ui.h"
51 #include "gui_thread.h"
52 #include "automation_controller.h"
53
54 #include "i18n.h"
55
56 using namespace std;
57 using namespace ARDOUR;
58 using namespace PBD;
59 using namespace Gtkmm2ext;
60 using namespace Gtk;
61
62 GenericPluginUI::GenericPluginUI (boost::shared_ptr<PluginInsert> pi, bool scrollable)
63         : PlugUIBase (pi)
64         , button_table (initial_button_rows, initial_button_cols)
65         , output_table (initial_output_rows, initial_output_cols)
66         , hAdjustment(0.0, 0.0, 0.0)
67         , vAdjustment(0.0, 0.0, 0.0)
68         , scroller_view(hAdjustment, vAdjustment)
69         , automation_menu (0)
70         , is_scrollable(scrollable)
71 #ifdef LV2_SUPPORT
72         , _fcb(0)
73 #endif
74 {
75         set_name ("PluginEditor");
76         set_border_width (10);
77         //set_homogeneous (false);
78
79         pack_start (main_contents, true, true);
80         settings_box.set_homogeneous (false);
81
82         HBox* constraint_hbox = manage (new HBox);
83         HBox* smaller_hbox = manage (new HBox);
84         smaller_hbox->set_spacing (4);
85         Label* combo_label = manage (new Label (_("<span size=\"large\">Presets</span>")));
86         combo_label->set_use_markup (true);
87
88         latency_button.add (latency_label);
89         latency_button.signal_clicked().connect (sigc::mem_fun (*this, &PlugUIBase::latency_button_clicked));
90         set_latency_label ();
91
92         smaller_hbox->pack_start (latency_button, false, false, 10);
93         smaller_hbox->pack_start (_preset_combo, false, false);
94         smaller_hbox->pack_start (_preset_modified, false, false);
95         smaller_hbox->pack_start (add_button, false, false);
96         smaller_hbox->pack_start (save_button, false, false);
97         smaller_hbox->pack_start (delete_button, false, false);
98         smaller_hbox->pack_start (bypass_button, false, true);
99
100         constraint_hbox->set_spacing (5);
101         constraint_hbox->set_homogeneous (false);
102
103         VBox* v1_box = manage (new VBox);
104         VBox* v2_box = manage (new VBox);
105         pack_end (plugin_analysis_expander, false, false);
106         if (!plugin->get_docs().empty()) {
107                 pack_end (description_expander, false, false);
108         }
109
110         v1_box->pack_start (*smaller_hbox, false, true);
111         v2_box->pack_start (focus_button, false, true);
112
113         main_contents.pack_start (settings_box, false, false);
114
115         constraint_hbox->pack_end (*v2_box, false, false);
116         constraint_hbox->pack_end (*v1_box, false, false);
117
118         main_contents.pack_start (*constraint_hbox, false, false);
119
120         if (is_scrollable ) {
121                 scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
122                 scroller.set_name ("PluginEditor");
123                 scroller_view.set_name("PluginEditor");
124                 scroller_view.add (hpacker);
125                 scroller.add (scroller_view);
126
127                 main_contents.pack_start (scroller, true, true);
128
129         } else {
130                 main_contents.pack_start (hpacker, false, false);
131         }
132
133         pi->ActiveChanged.connect (active_connection, invalidator (*this), boost::bind (&GenericPluginUI::processor_active_changed, this, boost::weak_ptr<Processor>(pi)), gui_context());
134
135         bypass_button.set_active (!pi->active());
136
137         prefheight = 0;
138         build ();
139 }
140
141 GenericPluginUI::~GenericPluginUI ()
142 {
143         if (output_controls.size() > 0) {
144                 screen_update_connection.disconnect();
145         }
146 #ifdef LV2_SUPPORT
147         free(_fcb);
148 #endif
149 }
150
151 // Some functions for calculating the 'similarity' of two plugin
152 // control labels.
153
154 static int get_number(string label) {
155 static const char *digits = "0123456789";
156 int value = -1;
157
158         std::size_t first_digit_pos = label.find_first_of(digits);
159         if (first_digit_pos != string::npos) {
160                 // found some digits: there's a number in there somewhere
161                 stringstream s;
162                 s << label.substr(first_digit_pos);
163                 s >> value;
164         }
165         return value;
166 }
167
168 static int match_or_digit(char c1, char c2) {
169         return c1 == c2 || (isdigit(c1) && isdigit(c2));
170 }       
171
172 static std::size_t matching_chars_at_head(const string s1, const string s2) {
173 std::size_t length, n = 0;
174
175         length = min(s1.length(), s2.length());
176         while (n < length) {
177                 if (!match_or_digit(s1[n], s2[n]))
178                         break;
179                 n++;
180         } 
181         return n;
182 }
183
184 static std::size_t matching_chars_at_tail(const string s1, const string s2) {
185 std::size_t s1pos, s2pos, n = 0;
186
187         s1pos = s1.length();
188         s2pos = s2.length();
189         while (s1pos-- > 0 && s2pos-- > 0) {
190                 if (!match_or_digit(s1[s1pos], s2[s2pos])       )
191                         break;
192                 n++;
193         } 
194         return n;
195 }
196
197 static const guint32 min_controls_per_column = 17, max_controls_per_column = 24;
198 static const float default_similarity_threshold = 0.3;
199
200 void
201 GenericPluginUI::build ()
202 {
203         guint32 i = 0;
204         guint32 x = 0;
205         Frame* frame;
206         Frame* bt_frame;
207         VBox* box;
208         int output_row, output_col;
209         int button_row, button_col;
210         int output_rows, output_cols;
211         int button_rows, button_cols;
212
213         hpacker.set_spacing (10);
214
215         output_rows = initial_output_rows;
216         output_cols = initial_output_cols;
217         button_rows = initial_button_rows;
218         button_cols = initial_button_cols;
219         output_row = 0;
220         button_row = 0;
221         output_col = 0;
222         button_col = 0;
223
224         button_table.set_homogeneous (false);
225         button_table.set_row_spacings (2);
226         button_table.set_col_spacings (2);
227         output_table.set_homogeneous (true);
228         output_table.set_row_spacings (2);
229         output_table.set_col_spacings (2);
230         button_table.set_border_width (5);
231         output_table.set_border_width (5);
232
233         hpacker.set_border_width (10);
234
235         bt_frame = manage (new Frame);
236         bt_frame->set_name ("BaseFrame");
237         bt_frame->set_label (_("Switches"));
238         bt_frame->add (button_table);
239         hpacker.pack_start(*bt_frame, true, true);
240
241         box = manage (new VBox);
242         box->set_border_width (5);
243         box->set_spacing (1);
244
245         frame = manage (new Frame);
246         frame->set_name ("BaseFrame");
247         frame->set_label (_("Controls"));
248         frame->add (*box);
249         hpacker.pack_start(*frame, true, true);
250
251         /* find all ports. build control elements for all appropriate control ports */
252         std::vector<ControlUI *> cui_controls_list;
253
254         for (i = 0; i < plugin->parameter_count(); ++i) {
255
256                 if (plugin->parameter_is_control (i)) {
257
258                         /* Don't show latency control ports */
259
260                         if (plugin->describe_parameter (Evoral::Parameter(PluginAutomation, 0, i)) == X_("latency")) {
261                                 continue;
262                         }
263
264                         if (plugin->describe_parameter (Evoral::Parameter(PluginAutomation, 0, i)) == X_("hidden")) {
265                                 continue;
266                         }
267
268                         ControlUI* cui;
269
270                         boost::shared_ptr<ARDOUR::AutomationControl> c
271                                 = boost::dynamic_pointer_cast<ARDOUR::AutomationControl>(
272                                         insert->control(Evoral::Parameter(PluginAutomation, 0, i)));
273
274                         if ((cui = build_control_ui (i, c)) == 0) {
275                                 error << string_compose(_("Plugin Editor: could not build control element for port %1"), i) << endmsg;
276                                 continue;
277                         }
278
279                         const std::string param_docs = plugin->get_parameter_docs(i);
280                         if (!param_docs.empty()) {
281                                 ARDOUR_UI::instance()->set_tip(cui, param_docs.c_str());
282                         }
283
284                         if (cui->controller || cui->clickbox || cui->combo) {
285                                 // Get all of the controls into a list, so that
286                                 // we can lay them out a bit more nicely later.
287                                 cui_controls_list.push_back(cui);
288                         } else if (cui->button) {
289
290                                 if (!is_scrollable && button_row == button_rows) {
291                                         button_row = 0;
292                                         if (++button_col == button_cols) {
293                                                 button_cols += 2;
294                                                 button_table.resize (button_rows, button_cols);
295                                         }
296                                 }
297
298                                 button_table.attach (*cui, button_col, button_col + 1, button_row, button_row+1,
299                                                      FILL|EXPAND, FILL);
300                                 button_row++;
301
302                         } else if (cui->display) {
303
304                                 output_table.attach (*cui, output_col, output_col + 1, output_row, output_row+1,
305                                                      FILL|EXPAND, FILL);
306
307                                 // TODO: The meters should be divided into multiple rows
308
309                                 if (++output_col == output_cols) {
310                                         output_cols ++;
311                                         output_table.resize (output_rows, output_cols);
312                                 }
313                         }
314                 } 
315         }
316
317 #ifdef LV2_SUPPORT
318         boost::shared_ptr<ARDOUR::LV2Plugin> lv2p = boost::dynamic_pointer_cast<LV2Plugin> (plugin);
319         if (lv2p) {
320                 _fcb = (Gtk::FileChooserButton**) malloc(lv2p->patch_count() * sizeof(Gtk::FileChooserButton*));
321                 for (uint32_t p = 0; p < lv2p->patch_count(); ++p) {
322                         _fcb[p] = manage (new Gtk::FileChooserButton (Gtk::FILE_CHOOSER_ACTION_OPEN));
323                         _fcb[p]->signal_file_set().connect (sigc::bind(sigc::mem_fun (*this, &GenericPluginUI::patch_set_file), p));
324                         lv2p->PatchChanged.connect (*this, invalidator (*this), boost::bind (&GenericPluginUI::patch_changed, this, _1), gui_context());
325                         // when user cancels file selection the FileChooserButton will display "None"
326                         // TODO hack away around this..
327                         if (lv2p->patch_val(p)) {
328                                 _fcb[p]->set_filename(lv2p->patch_val(p));
329                         }
330                         if (lv2p->patch_key(p)) {
331                                 _fcb[p]->set_title(lv2p->patch_key(p));
332                                 Gtk::Label* fcl = manage (new Label (lv2p->patch_key(p)));
333                                 button_table.attach (*fcl, 0, button_cols, button_row, button_row + 1, FILL|EXPAND, FILL);
334                                 ++button_row;
335                         } else {
336                                 _fcb[p]->set_title(_("LV2 Patch"));
337                         }
338
339                         button_table.attach (*_fcb[p], 0, button_cols, button_row, button_row + 1, FILL|EXPAND, FILL);
340                         ++button_row;
341                 }
342         }
343 #endif
344
345         // Iterate over the list of controls to find which adjacent controls
346         // are similar enough to be grouped together.
347         
348         string label, previous_label = "";
349         std::vector<int> numbers_in_labels(cui_controls_list.size());
350         
351         std::vector<float> similarity_scores(cui_controls_list.size());
352         float most_similar = 0.0, least_similar = 1.0;
353         
354         i = 0;
355         for (vector<ControlUI*>::iterator cuip = cui_controls_list.begin(); cuip != cui_controls_list.end(); ++cuip, ++i) {
356                 label = (*cuip)->label.get_text();
357                 numbers_in_labels[i] = get_number(label);
358
359                 if (i > 0) {
360                         // A hand-wavy calculation of how similar this control's
361                         // label is to the previous.
362                         similarity_scores[i] = 
363                                 (float) ( 
364                                         ( matching_chars_at_head(label, previous_label) + 
365                                           matching_chars_at_tail(label, previous_label) +
366                                           1 
367                                         ) 
368                                 ) / (label.length() + previous_label.length());
369                         if (numbers_in_labels[i] >= 0) {
370                                 similarity_scores[i] += (numbers_in_labels[i] == numbers_in_labels[i-1]);
371                         }
372                         least_similar = min(least_similar, similarity_scores[i]);
373                         most_similar  = max(most_similar, similarity_scores[i]);
374                 } else {
375                         similarity_scores[0] = 1.0;
376                 }
377
378                 // cerr << "label: " << label << " sim: " << fixed << setprecision(3) << similarity_scores[i] << " num: " << numbers_in_labels[i] << endl;
379                 previous_label = label;                                
380         }
381
382         
383         // cerr << "most similar: " << most_similar << ", least similar: " << least_similar << endl;
384         float similarity_threshold;
385         
386         if (most_similar > 1.0) {
387                 similarity_threshold = default_similarity_threshold;
388         } else {
389                 similarity_threshold = most_similar - (1 - default_similarity_threshold);
390         }
391         
392         // Now iterate over the list of controls to display them, placing an
393         // HSeparator between controls of less than a certain similarity, and 
394         // starting a new column when necessary.
395         
396         i = 0;
397         for (vector<ControlUI*>::iterator cuip = cui_controls_list.begin(); cuip != cui_controls_list.end(); ++cuip, ++i) {
398
399                 ControlUI* cui = *cuip;
400                 
401                 if (!is_scrollable) {
402                         x++;
403                 }
404                 
405                 if (x > max_controls_per_column || similarity_scores[i] <= similarity_threshold) {
406                         if (x > min_controls_per_column) {
407                                 frame = manage (new Frame);
408                                 frame->set_name ("BaseFrame");
409                                 frame->set_label (_("Controls"));
410                                 box = manage (new VBox);
411                                 box->set_border_width (5);
412                                 box->set_spacing (1);
413                                 frame->add (*box);
414                                 hpacker.pack_start(*frame, true, true);
415                                 x = 0;
416                         } else {
417                                 HSeparator *split = new HSeparator();
418                                 split->set_size_request(-1, 5);
419                                 box->pack_start(*split, false, false, 0);
420                         }
421
422                 }
423                 box->pack_start (*cui, false, false);
424         }
425
426         if (is_scrollable) {
427                 prefheight = 30 * i;
428         }
429
430         if (box->children().empty()) {
431                 hpacker.remove (*frame);
432         }
433
434         if (button_table.children().empty()) {
435                 hpacker.remove (*bt_frame);
436         }
437
438         if (!output_table.children().empty()) {
439                 frame = manage (new Frame);
440                 frame->set_name ("BaseFrame");
441                 frame->set_label(_("Meters"));
442                 frame->add (output_table);
443                 hpacker.pack_end (*frame, true, true);
444         }
445
446         output_update ();
447
448         output_table.show_all ();
449         button_table.show_all ();
450 }
451
452 GenericPluginUI::ControlUI::ControlUI ()
453         : automate_button (X_("")) // force creation of a label
454 {
455         automate_button.set_name ("PluginAutomateButton");
456         ARDOUR_UI::instance()->set_tip (automate_button, _("Automation control"));
457
458         /* XXX translators: use a string here that will be at least as long
459            as the longest automation label (see ::automation_state_changed()
460            below). be sure to include a descender.
461         */
462
463         set_size_request_to_display_given_text (automate_button, _("Mgnual"), 15, 10);
464
465         ignore_change = 0;
466         display = 0;
467         button = 0;
468         clickbox = 0;
469         meterinfo = 0;
470 }
471
472 GenericPluginUI::ControlUI::~ControlUI()
473 {
474         if (meterinfo) {
475                 delete meterinfo->meter;
476                 delete meterinfo;
477         }
478 }
479
480 void
481 GenericPluginUI::automation_state_changed (ControlUI* cui)
482 {
483         /* update button label */
484
485         // don't lock to avoid deadlock because we're triggered by
486         // AutomationControl::Changed() while the automation lock is taken
487         switch (insert->get_parameter_automation_state (cui->parameter()) & (ARDOUR::Off|Play|Touch|Write)) {
488         case ARDOUR::Off:
489                 cui->automate_button.set_label (S_("Automation|Manual"));
490                 break;
491         case Play:
492                 cui->automate_button.set_label (_("Play"));
493                 break;
494         case Write:
495                 cui->automate_button.set_label (_("Write"));
496                 break;
497         case Touch:
498                 cui->automate_button.set_label (_("Touch"));
499                 break;
500         default:
501                 cui->automate_button.set_label (_("???"));
502                 break;
503         }
504 }
505
506
507 bool
508 GenericPluginUI::integer_printer (char buf[32], Adjustment &adj, ControlUI* cui)
509 {
510         float const v = adj.get_value ();
511         
512         if (cui->scale_points) {
513                 Plugin::ScalePoints::const_iterator i = cui->scale_points->begin ();
514                 while (i != cui->scale_points->end() && i->second != v) {
515                         ++i;
516                 }
517
518                 if (i != cui->scale_points->end ()) {
519                         snprintf (buf, 32, "%s", i->first.c_str());
520                         return true;
521                 }
522         }
523                 
524         snprintf (buf, 32, "%.0f", v);
525         return true;
526 }
527
528 bool
529 GenericPluginUI::midinote_printer (char buf[32], Adjustment &adj, ControlUI* cui)
530 {
531         float const v = adj.get_value ();
532
533         if (cui->scale_points) {
534                 Plugin::ScalePoints::const_iterator i = cui->scale_points->begin ();
535                 while (i != cui->scale_points->end() && i->second != v) {
536                         ++i;
537                 }
538
539                 if (i != cui->scale_points->end ()) {
540                         snprintf (buf, 32, "%s", i->first.c_str());
541                         return true;
542                 }
543         }
544         if (v >= 0 && v <= 127) {
545                 int mn = rint(v);
546                 const char notename[12][3] = { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
547                 snprintf (buf, 32, "%s %d", notename[mn%12], (mn/12)-2);
548         } else {
549                 snprintf (buf, 32, "%.0f", v);
550         }
551         return true;
552 }
553
554 void
555 GenericPluginUI::print_parameter (char *buf, uint32_t len, uint32_t param)
556 {
557         plugin->print_parameter (param, buf, len);
558 }
559
560 GenericPluginUI::ControlUI*
561 GenericPluginUI::build_control_ui (guint32 port_index, boost::shared_ptr<AutomationControl> mcontrol)
562 {
563         ControlUI* control_ui = 0;
564
565         Plugin::ParameterDescriptor desc;
566
567         plugin->get_parameter_descriptor (port_index, desc);
568
569         control_ui = manage (new ControlUI ());
570         control_ui->combo = 0;
571         control_ui->control = mcontrol;
572         control_ui->update_pending = false;
573         control_ui->label.set_text (desc.label);
574         control_ui->label.set_alignment (0.0, 0.5);
575         control_ui->label.set_name ("PluginParameterLabel");
576         control_ui->port_index = port_index;
577
578         control_ui->set_spacing (5);
579
580         Gtk::Requisition req (control_ui->automate_button.size_request());
581
582         if (plugin->parameter_is_input(port_index)) {
583
584                 /* See if there any named values for our input value */
585                 control_ui->scale_points = plugin->get_scale_points (port_index);
586
587                 /* If this parameter is an integer, work out the number of distinct values
588                    it can take on (assuming that lower and upper values are allowed).
589                 */
590                 int const steps = desc.integer_step ? (desc.upper - desc.lower + 1) / desc.step : 0;
591
592                 if (control_ui->scale_points && ((steps && int (control_ui->scale_points->size()) == steps) || desc.enumeration)) {
593                         
594                         /* Either:
595                          *   a) There is a label for each possible value of this input, or
596                          *   b) This port is marked as being an enumeration.
597                          */
598
599                         std::vector<std::string> labels;
600                         for (
601                                 ARDOUR::Plugin::ScalePoints::const_iterator i = control_ui->scale_points->begin();
602                                 i != control_ui->scale_points->end();
603                                 ++i) {
604                                 
605                                 labels.push_back(i->first);
606                         }
607
608                         control_ui->combo = new Gtk::ComboBoxText();
609                         set_popdown_strings(*control_ui->combo, labels);
610                         control_ui->combo->signal_changed().connect(
611                                 sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::control_combo_changed),
612                                             control_ui));
613                         mcontrol->Changed.connect(control_connections, invalidator(*this),
614                                                   boost::bind(&GenericPluginUI::ui_parameter_changed,
615                                                               this, control_ui),
616                                                   gui_context());
617                         control_ui->pack_start(control_ui->label, true, true);
618                         control_ui->pack_start(*control_ui->combo, false, true);
619
620                         update_control_display(control_ui);
621
622                         return control_ui;
623                 }
624
625                 if (desc.toggled) {
626
627                         /* Build a button */
628
629                         control_ui->button = manage (new ToggleButton ());
630                         control_ui->button->set_name ("PluginEditorButton");
631                         control_ui->button->set_size_request (20, 20);
632
633                         control_ui->pack_start (control_ui->label, true, true);
634                         control_ui->pack_start (*control_ui->button, false, true);
635                         control_ui->pack_start (control_ui->automate_button, false, false);
636
637                         control_ui->button->signal_clicked().connect (sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::control_port_toggled), control_ui));
638                         control_ui->automate_button.signal_clicked().connect (bind (mem_fun(*this, &GenericPluginUI::astate_clicked), control_ui, (uint32_t) port_index));
639
640                         mcontrol->Changed.connect (control_connections, invalidator (*this), boost::bind (&GenericPluginUI::toggle_parameter_changed, this, control_ui), gui_context());
641                         mcontrol->alist()->automation_state_changed.connect (control_connections, invalidator (*this), boost::bind (&GenericPluginUI::automation_state_changed, this, control_ui), gui_context());
642
643                         if (plugin->get_parameter (port_index) > 0.5){
644                                 control_ui->button->set_active(true);
645                         }
646
647                         automation_state_changed (control_ui);
648
649                         return control_ui;
650                 }
651
652                 /* create the controller */
653
654                 if (mcontrol) {
655                         control_ui->controller = AutomationController::create(insert, mcontrol->parameter(), mcontrol);
656                 }
657
658                 /* XXX this code is not right yet, because it doesn't handle
659                    the absence of bounds in any sensible fashion.
660                 */
661
662                 Adjustment* adj = control_ui->controller->adjustment();
663                 boost::shared_ptr<PluginInsert::PluginControl> pc = boost::dynamic_pointer_cast<PluginInsert::PluginControl> (control_ui->control);
664
665                 adj->set_lower (desc.lower);
666                 adj->set_upper (desc.upper);
667
668                 adj->set_step_increment (desc.step);
669                 adj->set_page_increment (desc.largestep);
670
671                 if (desc.integer_step) {
672                         control_ui->clickbox = new ClickBox (adj, "PluginUIClickBox");
673                         Gtkmm2ext::set_size_request_to_display_given_text (*control_ui->clickbox, "g9999999", 2, 2);
674                         if (desc.midinote) {
675                                 control_ui->clickbox->set_printer (sigc::bind (sigc::mem_fun (*this, &GenericPluginUI::midinote_printer), control_ui));
676                         } else {
677                                 control_ui->clickbox->set_printer (sigc::bind (sigc::mem_fun (*this, &GenericPluginUI::integer_printer), control_ui));
678                         }
679                 } else {
680                         //sigc::slot<void,char*,uint32_t> pslot = sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::print_parameter), (uint32_t) port_index);
681
682                         control_ui->controller->set_size_request (200, req.height);
683                         control_ui->controller->set_name (X_("ProcessorControlSlider"));
684
685                         control_ui->controller->StartGesture.connect (sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::start_touch), control_ui));
686                         control_ui->controller->StopGesture.connect (sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::stop_touch), control_ui));
687
688                 }
689
690                 adj->set_value (plugin->get_parameter (port_index));
691
692                 /* XXX memory leak: SliderController not destroyed by ControlUI
693                    destructor, and manage() reports object hierarchy
694                    ambiguity.
695                 */
696
697                 control_ui->pack_start (control_ui->label, true, true);
698                 if (desc.integer_step) {
699                         control_ui->pack_start (*control_ui->clickbox, false, false);
700                 } else {
701                         control_ui->pack_start (*control_ui->controller, false, false);
702                 }
703
704                 control_ui->pack_start (control_ui->automate_button, false, false);
705                 control_ui->automate_button.signal_clicked().connect (sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::astate_clicked), control_ui, (uint32_t) port_index));
706
707                 automation_state_changed (control_ui);
708
709                 mcontrol->Changed.connect (control_connections, invalidator (*this), boost::bind (&GenericPluginUI::ui_parameter_changed, this, control_ui), gui_context());
710                 mcontrol->alist()->automation_state_changed.connect (control_connections, invalidator (*this), boost::bind (&GenericPluginUI::automation_state_changed, this, control_ui), gui_context());
711
712                 input_controls.push_back (control_ui);
713
714         } else if (plugin->parameter_is_output (port_index)) {
715
716                 control_ui->display = manage (new EventBox);
717                 control_ui->display->set_name ("ParameterValueDisplay");
718
719                 control_ui->display_label = manage (new Label);
720
721                 control_ui->display_label->set_name ("ParameterValueDisplay");
722
723                 control_ui->display->add (*control_ui->display_label);
724                 Gtkmm2ext::set_size_request_to_display_given_text (*control_ui->display, "-888.8g", 2, 6);
725
726                 control_ui->display->show_all ();
727
728                 /* set up a meter */
729                 /* TODO: only make a meter if the port is Hinted for it */
730
731                 MeterInfo * info = new MeterInfo(port_index);
732                 control_ui->meterinfo = info;
733
734                 info->meter = new FastMeter (
735                                 5, 5, FastMeter::Vertical, 0,
736                                 0x0000aaff,
737                                 0x008800ff, 0x008800ff,
738                                 0x00ff00ff, 0x00ff00ff,
739                                 0xcccc00ff, 0xcccc00ff,
740                                 0xffaa00ff, 0xffaa00ff,
741                                 0xff0000ff,
742                                 ARDOUR_UI::config()->get_MeterBackgroundBot(),
743                                 ARDOUR_UI::config()->get_MeterBackgroundTop()
744                                 );
745
746                 info->min_unbound = desc.min_unbound;
747                 info->max_unbound = desc.max_unbound;
748
749                 info->min = desc.lower;
750                 info->max = desc.upper;
751
752                 control_ui->vbox = manage (new VBox);
753                 control_ui->hbox = manage (new HBox);
754
755                 control_ui->hbox->set_spacing(1);
756                 control_ui->vbox->set_spacing(3);
757
758                 control_ui->label.set_angle(90);
759                 control_ui->hbox->pack_start (control_ui->label, false, false);
760                 control_ui->hbox->pack_start (*info->meter, false, false);
761
762                 control_ui->vbox->pack_start (*control_ui->hbox, false, false);
763
764                 control_ui->vbox->pack_start (*control_ui->display, false, false);
765
766                 control_ui->pack_start (*control_ui->vbox);
767
768                 control_ui->meterinfo->meter->show_all();
769                 control_ui->meterinfo->packed = true;
770
771                 output_controls.push_back (control_ui);
772         }
773
774         if (mcontrol) {
775                 mcontrol->Changed.connect (control_connections, invalidator (*this), boost::bind (&GenericPluginUI::ui_parameter_changed, this, control_ui), gui_context());
776         }
777
778         return control_ui;
779 }
780
781 void
782 GenericPluginUI::start_touch (GenericPluginUI::ControlUI* cui)
783 {
784         cui->control->start_touch (cui->control->session().transport_frame());
785 }
786
787 void
788 GenericPluginUI::stop_touch (GenericPluginUI::ControlUI* cui)
789 {
790         cui->control->stop_touch (false, cui->control->session().transport_frame());
791 }
792
793 void
794 GenericPluginUI::astate_clicked (ControlUI* cui, uint32_t /*port*/)
795 {
796         using namespace Menu_Helpers;
797
798         if (automation_menu == 0) {
799                 automation_menu = manage (new Menu);
800                 automation_menu->set_name ("ArdourContextMenu");
801         }
802
803         MenuList& items (automation_menu->items());
804
805         items.clear ();
806         items.push_back (MenuElem (S_("Automation|Manual"),
807                                    sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::set_automation_state), (AutoState) ARDOUR::Off, cui)));
808         items.push_back (MenuElem (_("Play"),
809                                    sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::set_automation_state), (AutoState) Play, cui)));
810         items.push_back (MenuElem (_("Write"),
811                                    sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::set_automation_state), (AutoState) Write, cui)));
812         items.push_back (MenuElem (_("Touch"),
813                                    sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::set_automation_state), (AutoState) Touch, cui)));
814
815         automation_menu->popup (1, gtk_get_current_event_time());
816 }
817
818 void
819 GenericPluginUI::set_automation_state (AutoState state, ControlUI* cui)
820 {
821         insert->set_parameter_automation_state (cui->parameter(), state);
822 }
823
824 void
825 GenericPluginUI::toggle_parameter_changed (ControlUI* cui)
826 {
827         float val = cui->control->get_value();
828
829         if (!cui->ignore_change) {
830                 if (val > 0.5) {
831                         cui->button->set_active (true);
832                         cui->button->set_name ("PluginEditorButton-active");
833                 } else {
834                         cui->button->set_active (false);
835                         cui->button->set_name ("PluginEditorButton");
836                 }
837         }
838 }
839
840 void
841 GenericPluginUI::ui_parameter_changed (ControlUI* cui)
842 {
843         if (!cui->update_pending) {
844                 cui->update_pending = true;
845                 Gtkmm2ext::UI::instance()->call_slot (MISSING_INVALIDATOR, boost::bind (&GenericPluginUI::update_control_display, this, cui));
846         }
847 }
848
849 void
850 GenericPluginUI::update_control_display (ControlUI* cui)
851 {
852         /* XXX how do we handle logarithmic stuff here ? */
853
854         cui->update_pending = false;
855
856         float val = cui->control->get_value();
857
858         cui->ignore_change++;
859
860         if (cui->combo && cui->scale_points) {
861                 for (ARDOUR::Plugin::ScalePoints::iterator it = cui->scale_points->begin(); it != cui->scale_points->end(); ++it) {
862                         if (it->second == val) {
863                                 cui->combo->set_active_text(it->first);
864                                 break;
865                         }
866                 }
867         } else if (cui->button) {
868
869                 if (val > 0.5) {
870                         cui->button->set_active (true);
871                 } else {
872                         cui->button->set_active (false);
873                 }
874         }
875
876         if( cui->controller ) {
877             cui->controller->display_effective_value();
878         }
879
880
881         /*} else {
882                 if (cui->logarithmic) {
883                         val = log(val);
884                 }
885                 if (val != cui->adjustment->get_value()) {
886                         cui->adjustment->set_value (val);
887                 }
888         }*/
889         cui->ignore_change--;
890 }
891
892 void
893 GenericPluginUI::control_port_toggled (ControlUI* cui)
894 {
895         cui->ignore_change++;
896         const bool active = cui->button->get_active();
897         if (active) {
898                 cui->button->set_name ("PluginEditorButton-active");
899         } else {
900                 cui->button->set_name ("PluginEditorButton");
901         }
902         insert->automation_control (cui->parameter())->set_value (active);
903         cui->ignore_change--;
904 }
905
906 void
907 GenericPluginUI::control_combo_changed (ControlUI* cui)
908 {
909         if (!cui->ignore_change && cui->scale_points) {
910                 string value = cui->combo->get_active_text();
911                 insert->automation_control (cui->parameter())->set_value ((*cui->scale_points)[value]);
912         }
913 }
914
915 bool
916 GenericPluginUI::start_updating (GdkEventAny*)
917 {
918         if (output_controls.size() > 0 ) {
919                 screen_update_connection.disconnect();
920                 screen_update_connection = ARDOUR_UI::instance()->SuperRapidScreenUpdate.connect
921                         (sigc::mem_fun(*this, &GenericPluginUI::output_update));
922         }
923         return false;
924 }
925
926 bool
927 GenericPluginUI::stop_updating (GdkEventAny*)
928 {
929         for (vector<ControlUI*>::iterator i = input_controls.begin(); i != input_controls.end(); ++i) {
930                 (*i)->controller->stop_updating ();
931         }
932
933         if (output_controls.size() > 0 ) {
934                 screen_update_connection.disconnect();
935         }
936
937         return false;
938 }
939
940 void
941 GenericPluginUI::output_update ()
942 {
943         for (vector<ControlUI*>::iterator i = output_controls.begin(); i != output_controls.end(); ++i) {
944                 float val = plugin->get_parameter ((*i)->port_index);
945                 char buf[32];
946                 snprintf (buf, sizeof(buf), "%.2f", val);
947                 (*i)->display_label->set_text (buf);
948
949                 /* autoscaling for the meter */
950                 if ((*i)->meterinfo && (*i)->meterinfo->packed) {
951
952                         if (val < (*i)->meterinfo->min) {
953                                 if ((*i)->meterinfo->min_unbound)
954                                         (*i)->meterinfo->min = val;
955                                 else
956                                         val = (*i)->meterinfo->min;
957                         }
958
959                         if (val > (*i)->meterinfo->max) {
960                                 if ((*i)->meterinfo->max_unbound)
961                                         (*i)->meterinfo->max = val;
962                                 else
963                                         val = (*i)->meterinfo->max;
964                         }
965
966                         if ((*i)->meterinfo->max > (*i)->meterinfo->min ) {
967                                 float lval = (val - (*i)->meterinfo->min) / ((*i)->meterinfo->max - (*i)->meterinfo->min) ;
968                                 (*i)->meterinfo->meter->set (lval );
969                         }
970                 }
971         }
972 }
973
974 #ifdef LV2_SUPPORT
975
976 void
977 GenericPluginUI::patch_set_file (uint32_t p)
978 {
979         boost::shared_ptr<ARDOUR::LV2Plugin> lv2p = boost::dynamic_pointer_cast<LV2Plugin> (plugin);
980         lv2p->patch_set(p, _fcb[p]->get_filename ().c_str());
981 }
982
983 void
984 GenericPluginUI::patch_changed (uint32_t p)
985 {
986         boost::shared_ptr<ARDOUR::LV2Plugin> lv2p = boost::dynamic_pointer_cast<LV2Plugin> (plugin);
987         _fcb[p]->set_filename(lv2p->patch_val(p));
988 }
989
990 #endif