large part of rev 5374 from 2.x, fwd port to 3.0
[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
29 #include "pbd/stl_delete.h"
30 #include "pbd/xml++.h"
31 #include "pbd/failed_constructor.h"
32
33 #include <gtkmm2ext/click_box.h>
34 #include <gtkmm2ext/fastmeter.h>
35 #include <gtkmm2ext/barcontroller.h>
36 #include <gtkmm2ext/utils.h>
37 #include <gtkmm2ext/doi.h>
38 #include <gtkmm2ext/slider_controller.h>
39
40 #include "midi++/manager.h"
41
42 #include "ardour/plugin.h"
43 #include "ardour/plugin_insert.h"
44 #include "ardour/ladspa_plugin.h"
45 #ifdef HAVE_SLV2
46 #include "ardour/lv2_plugin.h"
47 #endif
48 #include "ardour/session.h"
49
50 #include <lrdf.h>
51
52 #include "ardour_ui.h"
53 #include "prompter.h"
54 #include "plugin_ui.h"
55 #include "utils.h"
56 #include "gui_thread.h"
57 #include "automation_controller.h"
58
59 #include "i18n.h"
60
61 using namespace std;
62 using namespace ARDOUR;
63 using namespace PBD;
64 using namespace Gtkmm2ext;
65 using namespace Gtk;
66
67 GenericPluginUI::GenericPluginUI (boost::shared_ptr<PluginInsert> pi, bool scrollable)
68         : PlugUIBase (pi),
69           button_table (initial_button_rows, initial_button_cols),
70           output_table (initial_output_rows, initial_output_cols),
71           hAdjustment(0.0, 0.0, 0.0),
72           vAdjustment(0.0, 0.0, 0.0),
73           scroller_view(hAdjustment, vAdjustment),
74           automation_menu (0),
75           is_scrollable(scrollable)
76 {
77         set_name ("PluginEditor");
78         set_border_width (10);
79         //set_homogeneous (false);
80
81         pack_start(main_contents);
82         settings_box.set_homogeneous (false);
83
84         HBox* constraint_hbox = manage (new HBox);
85         HBox* smaller_hbox = manage (new HBox);
86         Label* combo_label = manage (new Label (_("<span size=\"large\">Presets</span>")));
87         combo_label->set_use_markup (true);
88
89         latency_button.add (latency_label);
90         latency_button.signal_clicked().connect (sigc::mem_fun (*this, &PlugUIBase::latency_button_clicked));
91         set_latency_label ();
92
93         smaller_hbox->pack_start (latency_button, false, false, 10);
94         smaller_hbox->pack_start (preset_combo, false, false);
95         smaller_hbox->pack_start (save_button, false, false);
96         smaller_hbox->pack_start (bypass_button, false, true);
97
98         constraint_hbox->set_spacing (5);
99         constraint_hbox->set_homogeneous (false);
100
101         VBox* v1_box = manage (new VBox);
102         VBox* v2_box = manage (new VBox);
103         pack_end(plugin_analysis_expander);
104
105         v1_box->pack_start (*smaller_hbox, false, true);
106         v2_box->pack_start (focus_button, false, true);
107
108         main_contents.pack_start (settings_box, false, false);
109
110         constraint_hbox->pack_end (*v2_box, false, false);
111         constraint_hbox->pack_end (*v1_box, false, false);
112
113         main_contents.pack_start (*constraint_hbox, false, false);
114
115         if ( is_scrollable ) {
116                 scroller.set_policy (Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
117                 scroller.set_name ("PluginEditor");
118                 scroller_view.set_name("PluginEditor");
119                 scroller_view.add (hpacker);
120                 scroller.add (scroller_view);
121
122                 main_contents.pack_start (scroller, true, true);
123
124         }
125         else {
126                 main_contents.pack_start (hpacker, false, false);
127         }
128
129         pi->ActiveChanged.connect (active_connection, invalidator (*this), boost::bind (&GenericPluginUI::processor_active_changed, this, boost::weak_ptr<Processor>(pi)), gui_context());
130
131         bypass_button.set_active (!pi->active());
132
133         build ();
134 }
135
136 GenericPluginUI::~GenericPluginUI ()
137 {
138         if (output_controls.size() > 0) {
139                 screen_update_connection.disconnect();
140         }
141 }
142
143 void
144 GenericPluginUI::build ()
145
146 {
147         guint32 i = 0;
148         guint32 x = 0;
149         Frame* frame;
150         Frame* bt_frame;
151         VBox* box;
152         int output_row, output_col;
153         int button_row, button_col;
154         int output_rows, output_cols;
155         int button_rows, button_cols;
156
157         prefheight = 30;
158         hpacker.set_spacing (10);
159
160         output_rows = initial_output_rows;
161         output_cols = initial_output_cols;
162         button_rows = initial_button_rows;
163         button_cols = initial_button_cols;
164         output_row = 0;
165         button_row = 0;
166         output_col = 0;
167         button_col = 0;
168
169         button_table.set_homogeneous (false);
170         button_table.set_row_spacings (2);
171         button_table.set_col_spacings (2);
172         output_table.set_homogeneous (true);
173         output_table.set_row_spacings (2);
174         output_table.set_col_spacings (2);
175         button_table.set_border_width (5);
176         output_table.set_border_width (5);
177
178         hpacker.set_border_width (10);
179
180         bt_frame = manage (new Frame);
181         bt_frame->set_name ("BaseFrame");
182         bt_frame->add (button_table);
183         hpacker.pack_start(*bt_frame, true, true);
184
185         box = manage (new VBox);
186         box->set_border_width (5);
187         box->set_spacing (1);
188
189         frame = manage (new Frame);
190         frame->set_name ("BaseFrame");
191         frame->set_label (_("Controls"));
192         frame->add (*box);
193         hpacker.pack_start(*frame, true, true);
194
195         /* find all ports. build control elements for all appropriate control ports */
196
197         for (i = 0; i < plugin->parameter_count(); ++i) {
198
199                 if (plugin->parameter_is_control (i)) {
200
201                         /* Don't show latency control ports */
202
203                         if (plugin->describe_parameter (Evoral::Parameter(PluginAutomation, 0, i)) == X_("latency")) {
204                                 continue;
205                         }
206
207                         ControlUI* cui;
208
209                         /* if we are scrollable, just use one long column */
210
211                         if (!is_scrollable) {
212                                 if (x++ > 20){
213                                         frame = manage (new Frame);
214                                         frame->set_name ("BaseFrame");
215                                         box = manage (new VBox);
216
217                                         box->set_border_width (5);
218                                         box->set_spacing (1);
219
220                                         frame->add (*box);
221                                         hpacker.pack_start(*frame,true,true);
222
223                                         x = 1;
224                                 }
225                         }
226
227                         boost::shared_ptr<ARDOUR::AutomationControl> c
228                                 = boost::dynamic_pointer_cast<ARDOUR::AutomationControl>(
229                                         insert->control(Evoral::Parameter(PluginAutomation, 0, i)));
230
231                         if ((cui = build_control_ui (i, c)) == 0) {
232                                 error << string_compose(_("Plugin Editor: could not build control element for port %1"), i) << endmsg;
233                                 continue;
234                         }
235
236                         if (cui->controller || cui->clickbox || cui->combo) {
237
238                                 box->pack_start (*cui, false, false);
239
240                         } else if (cui->button) {
241
242                                 if (button_row == button_rows) {
243                                         button_row = 0;
244                                         if (++button_col == button_cols) {
245                                                 button_cols += 2;
246                                                 button_table.resize (button_rows, button_cols);
247                                         }
248                                 }
249
250                                 button_table.attach (*cui, button_col, button_col + 1, button_row, button_row+1,
251                                                      FILL|EXPAND, FILL);
252                                 button_row++;
253
254                         } else if (cui->display) {
255
256                                 output_table.attach (*cui, output_col, output_col + 1, output_row, output_row+1,
257                                                      FILL|EXPAND, FILL);
258
259                                 // TODO: The meters should be divided into multiple rows
260
261                                 if (++output_col == output_cols) {
262                                         output_cols ++;
263                                         output_table.resize (output_rows, output_cols);
264                                 }
265
266                                 /* old code, which divides meters into
267                                  * columns first, rows later. New code divides into one row
268
269                                 if (output_row == output_rows) {
270                                         output_row = 0;
271                                         if (++output_col == output_cols) {
272                                                 output_cols += 2;
273                                                 output_table.resize (output_rows, output_cols);
274                                         }
275                                 }
276
277                                 output_table.attach (*cui, output_col, output_col + 1, output_row, output_row+1,
278                                                      FILL|EXPAND, FILL);
279
280                                 output_row++;
281                                 */
282                         }
283
284                         /* HACK: ideally the preferred height would be queried from
285                            the complete hpacker, but I can't seem to get that
286                            information in time, so this is an estimation
287                         */
288
289                         prefheight += 30;
290
291                 }
292         }
293
294         if (box->children().empty()) {
295                 hpacker.remove (*frame);
296         }
297
298         if (button_table.children().empty()) {
299                 hpacker.remove (*bt_frame);
300         }
301
302         if (!output_table.children().empty()) {
303                 frame = manage (new Frame);
304                 frame->set_name ("BaseFrame");
305                 frame->add (output_table);
306                 hpacker.pack_end (*frame, true, true);
307         }
308
309         output_update ();
310
311         output_table.show_all ();
312         button_table.show_all ();
313 }
314
315 GenericPluginUI::ControlUI::ControlUI ()
316         : automate_button (X_("")) // force creation of a label
317 {
318         automate_button.set_name ("PluginAutomateButton");
319         ARDOUR_UI::instance()->set_tip (automate_button, _("Automation control"));
320
321         /* XXX translators: use a string here that will be at least as long
322            as the longest automation label (see ::automation_state_changed()
323            below). be sure to include a descender.
324         */
325
326         set_size_request_to_display_given_text (automate_button, _("Mgnual"), 15, 10);
327
328         ignore_change = 0;
329         display = 0;
330         button = 0;
331         clickbox = 0;
332         meterinfo = 0;
333 }
334
335 GenericPluginUI::ControlUI::~ControlUI()
336 {
337         if (meterinfo) {
338                 delete meterinfo->meter;
339                 delete meterinfo;
340         }
341 }
342
343 void
344 GenericPluginUI::automation_state_changed (ControlUI* cui)
345 {
346         /* update button label */
347
348         // don't lock to avoid deadlock because we're triggered by
349         // AutomationControl::Changed() while the automation lock is taken
350         switch (insert->get_parameter_automation_state (cui->parameter())
351                         & (Off|Play|Touch|Write)) {
352         case Off:
353                 cui->automate_button.set_label (_("Manual"));
354                 break;
355         case Play:
356                 cui->automate_button.set_label (_("Play"));
357                 break;
358         case Write:
359                 cui->automate_button.set_label (_("Write"));
360                 break;
361         case Touch:
362                 cui->automate_button.set_label (_("Touch"));
363                 break;
364         default:
365                 cui->automate_button.set_label (_("???"));
366                 break;
367         }
368 }
369
370
371 static void integer_printer (char buf[32], Adjustment &adj, void */*arg*/)
372 {
373         snprintf (buf, 32, "%.0f", adj.get_value());
374 }
375
376 void
377 GenericPluginUI::print_parameter (char *buf, uint32_t len, uint32_t param)
378 {
379         plugin->print_parameter (param, buf, len);
380 }
381
382 GenericPluginUI::ControlUI*
383 GenericPluginUI::build_control_ui (guint32 port_index, boost::shared_ptr<AutomationControl> mcontrol)
384 {
385         ControlUI* control_ui = 0;
386         if (!mcontrol) {
387                 return control_ui;
388         }
389
390         Plugin::ParameterDescriptor desc;
391
392         plugin->get_parameter_descriptor (port_index, desc);
393
394         control_ui = manage (new ControlUI ());
395         control_ui->combo = 0;
396         control_ui->combo_map = 0;
397         control_ui->control = mcontrol;
398         control_ui->update_pending = false;
399         control_ui->label.set_text (desc.label);
400         control_ui->label.set_alignment (0.0, 0.5);
401         control_ui->label.set_name ("PluginParameterLabel");
402
403         control_ui->set_spacing (5);
404
405         Gtk::Requisition req (control_ui->automate_button.size_request());
406
407         if (plugin->parameter_is_input (port_index)) {
408
409                 boost::shared_ptr<LadspaPlugin> lp;
410 #ifdef HAVE_SLV2
411                 boost::shared_ptr<LV2Plugin> lv2p;
412 #endif
413                 if ((lp = boost::dynamic_pointer_cast<LadspaPlugin>(plugin)) != 0) {
414
415                         // FIXME: not all plugins have a numeric unique ID
416                         uint32_t id = atol (lp->unique_id().c_str());
417                         lrdf_defaults* defaults = lrdf_get_scale_values(id, port_index);
418
419                         if (defaults && defaults->count > 0)    {
420
421                                 control_ui->combo = new Gtk::ComboBoxText;
422                                 //control_ui->combo->set_value_in_list(true, false);
423                                 set_popdown_strings (*control_ui->combo, setup_scale_values(port_index, control_ui));
424                                 control_ui->combo->signal_changed().connect (sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::control_combo_changed), control_ui));
425                                 mcontrol->Changed.connect (control_connections, invalidator (*this), boost::bind (&GenericPluginUI::parameter_changed, this, control_ui), gui_context());
426                                 control_ui->pack_start(control_ui->label, true, true);
427                                 control_ui->pack_start(*control_ui->combo, false, true);
428
429                                 update_control_display(control_ui);
430
431                                 lrdf_free_setting_values(defaults);
432                                 return control_ui;
433                         }
434
435 #ifdef HAVE_SLV2
436                 } else if ((lv2p = boost::dynamic_pointer_cast<LV2Plugin>(plugin)) != 0) {
437
438                         SLV2Port port = lv2p->slv2_port(port_index);
439                         SLV2ScalePoints points = slv2_port_get_scale_points(lv2p->slv2_plugin(), port);
440
441                         if (points) {
442                                 control_ui->combo = new Gtk::ComboBoxText;
443                                 //control_ui->combo->set_value_in_list(true, false);
444                                 set_popdown_strings (*control_ui->combo, setup_scale_values(port_index, control_ui));
445                                 control_ui->combo->signal_changed().connect (sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::control_combo_changed), control_ui));
446                                 mcontrol->Changed.connect (control_connections, invalidator (*this), boost::bind (&GenericPluginUI::parameter_changed, this, control_ui), gui_context());
447                                 control_ui->pack_start(control_ui->label, true, true);
448                                 control_ui->pack_start(*control_ui->combo, false, true);
449
450                                 update_control_display(control_ui);
451
452                                 slv2_scale_points_free(points);
453                                 return control_ui;
454                         }
455 #endif
456                 }
457
458                 if (desc.toggled) {
459
460                         /* Build a button */
461
462                         control_ui->button = manage (new ToggleButton ());
463                         control_ui->button->set_name ("PluginEditorButton");
464                         control_ui->button->set_size_request (20, 20);
465
466                         control_ui->pack_start (control_ui->label, true, true);
467                         control_ui->pack_start (*control_ui->button, false, true);
468                         // control_ui->pack_start (control_ui->automate_button, false, false);
469
470                         control_ui->button->signal_clicked().connect (sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::control_port_toggled), control_ui));
471                         mcontrol->Changed.connect (control_connections, invalidator (*this), boost::bind (&GenericPluginUI::toggle_parameter_changed, this, control_ui), gui_context());
472
473                         if (plugin->get_parameter (port_index) > 0.5){
474                                 control_ui->button->set_active(true);
475                         }
476
477                         return control_ui;
478                 }
479
480                 /* create the controller */
481
482                 control_ui->controller = AutomationController::create(insert, mcontrol->parameter(), mcontrol);
483                 /* XXX this code is not right yet, because it doesn't handle
484                    the absence of bounds in any sensible fashion.
485                 */
486
487 //#if 0
488                 control_ui->controller->adjustment()->set_lower (desc.lower);
489                 control_ui->controller->adjustment()->set_upper (desc.upper);
490
491                 control_ui->logarithmic = false; // just disable it for now
492                 /*
493                 control_ui->logarithmic = desc.logarithmic;
494                 if (control_ui->logarithmic) {
495                         if (control_ui->controller->adjustment()->get_lower() == 0.0) {
496                                 control_ui->controller->adjustment()->set_lower (control_ui->controller->adjustment()->get_upper()/10000);
497                         }
498                         control_ui->controller->adjustment()->set_upper (log(control_ui->controller->adjustment()->get_upper()));
499                         control_ui->controller->adjustment()->set_lower (log(control_ui->controller->adjustment()->get_lower()));
500                 }*/
501
502
503                 control_ui->controller->adjustment()->set_step_increment (desc.step);
504                 control_ui->controller->adjustment()->set_page_increment (desc.largestep);
505 //#endif
506
507                 if (desc.integer_step) {
508                         control_ui->clickbox = new ClickBox (control_ui->controller->adjustment(), "PluginUIClickBox");
509                         Gtkmm2ext::set_size_request_to_display_given_text (*control_ui->clickbox, "g9999999", 2, 2);
510                         control_ui->clickbox->set_print_func (integer_printer, 0);
511                 } else {
512                         //sigc::slot<void,char*,uint32_t> pslot = sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::print_parameter), (uint32_t) port_index);
513
514                         control_ui->controller->set_size_request (200, req.height);
515                         control_ui->controller->set_name (X_("PluginSlider"));
516                         control_ui->controller->set_style (BarController::LeftToRight);
517                         control_ui->controller->set_use_parent (true);
518                         control_ui->controller->set_logarithmic (control_ui->logarithmic);
519
520                         control_ui->controller->StartGesture.connect (sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::start_touch), control_ui));
521                         control_ui->controller->StopGesture.connect (sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::stop_touch), control_ui));
522
523                 }
524
525                 if (control_ui->logarithmic) {
526                         control_ui->controller->adjustment()->set_value(log(plugin->get_parameter(port_index)));
527                 } else{
528                         control_ui->controller->adjustment()->set_value(plugin->get_parameter(port_index));
529                 }
530
531                 /* XXX memory leak: SliderController not destroyed by ControlUI
532                    destructor, and manage() reports object hierarchy
533                    ambiguity.
534                 */
535
536                 control_ui->pack_start (control_ui->label, true, true);
537                 if (desc.integer_step) {
538                         control_ui->pack_start (*control_ui->clickbox, false, false);
539                 } else {
540                         control_ui->pack_start (*control_ui->controller, false, false);
541                 }
542
543                 control_ui->pack_start (control_ui->automate_button, false, false);
544                 control_ui->automate_button.signal_clicked().connect (sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::astate_clicked), control_ui, (uint32_t) port_index));
545
546                 automation_state_changed (control_ui);
547
548                 mcontrol->Changed.connect (control_connections, invalidator (*this), boost::bind (&GenericPluginUI::parameter_changed, this, control_ui), gui_context());
549                 mcontrol->alist()->automation_state_changed.connect (control_connections, invalidator (*this), boost::bind (&GenericPluginUI::automation_state_changed, this, control_ui), gui_context());
550
551                 input_controls.push_back (control_ui);
552
553         } else if (plugin->parameter_is_output (port_index)) {
554
555                 control_ui->display = manage (new EventBox);
556                 control_ui->display->set_name ("ParameterValueDisplay");
557
558                 control_ui->display_label = manage (new Label);
559
560                 control_ui->display_label->set_name ("ParameterValueDisplay");
561
562                 control_ui->display->add (*control_ui->display_label);
563                 Gtkmm2ext::set_size_request_to_display_given_text (*control_ui->display, "-99,99", 2, 2);
564
565                 control_ui->display->show_all ();
566
567                 /* set up a meter */
568                 /* TODO: only make a meter if the port is Hinted for it */
569
570                 MeterInfo * info = new MeterInfo(port_index);
571                 control_ui->meterinfo = info;
572
573                 info->meter = new FastMeter (5, 5, FastMeter::Vertical);
574
575                 info->min_unbound = desc.min_unbound;
576                 info->max_unbound = desc.max_unbound;
577
578                 info->min = desc.lower;
579                 info->max = desc.upper;
580
581                 control_ui->vbox = manage (new VBox);
582                 control_ui->hbox = manage (new HBox);
583
584                 control_ui->label.set_angle(90);
585                 control_ui->hbox->pack_start (control_ui->label, false, false);
586                 control_ui->hbox->pack_start (*info->meter, false, false);
587
588                 control_ui->vbox->pack_start (*control_ui->hbox, false, false);
589
590                 control_ui->vbox->pack_start (*control_ui->display, false, false);
591
592                 control_ui->pack_start (*control_ui->vbox);
593
594                 control_ui->meterinfo->meter->show_all();
595                 control_ui->meterinfo->packed = true;
596
597                 output_controls.push_back (control_ui);
598         }
599
600         mcontrol->Changed.connect (control_connections, invalidator (*this), boost::bind (&GenericPluginUI::parameter_changed, this, control_ui), gui_context());
601
602         return control_ui;
603 }
604
605 void
606 GenericPluginUI::start_touch (GenericPluginUI::ControlUI* cui)
607 {
608         cui->control->start_touch (cui->control->session().transport_frame());
609 }
610
611 void
612 GenericPluginUI::stop_touch (GenericPluginUI::ControlUI* cui)
613 {
614         cui->control->stop_touch (false, cui->control->session().transport_frame());
615 }
616
617 void
618 GenericPluginUI::astate_clicked (ControlUI* cui, uint32_t /*port*/)
619 {
620         using namespace Menu_Helpers;
621
622         if (automation_menu == 0) {
623                 automation_menu = manage (new Menu);
624                 automation_menu->set_name ("ArdourContextMenu");
625         }
626
627         MenuList& items (automation_menu->items());
628
629         items.clear ();
630         items.push_back (MenuElem (_("Manual"),
631                                    sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::set_automation_state), (AutoState) Off, cui)));
632         items.push_back (MenuElem (_("Play"),
633                                    sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::set_automation_state), (AutoState) Play, cui)));
634         items.push_back (MenuElem (_("Write"),
635                                    sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::set_automation_state), (AutoState) Write, cui)));
636         items.push_back (MenuElem (_("Touch"),
637                                    sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::set_automation_state), (AutoState) Touch, cui)));
638
639         automation_menu->popup (1, gtk_get_current_event_time());
640 }
641
642 void
643 GenericPluginUI::set_automation_state (AutoState state, ControlUI* cui)
644 {
645         insert->set_parameter_automation_state (cui->parameter(), state);
646 }
647
648 void
649 GenericPluginUI::toggle_parameter_changed (ControlUI* cui)
650 {
651         float val = cui->control->get_value();
652
653         if (!cui->ignore_change) {
654                 if (val > 0.5) {
655                         cui->button->set_active (true);
656                 } else {
657                         cui->button->set_active (false);
658                 }
659         }
660 }
661
662 void
663 GenericPluginUI::parameter_changed (ControlUI* cui)
664 {
665         if (!cui->update_pending) {
666                 cui->update_pending = true;
667                 Gtkmm2ext::UI::instance()->call_slot (MISSING_INVALIDATOR, boost::bind (&GenericPluginUI::update_control_display, this, cui));
668         }
669 }
670
671 void
672 GenericPluginUI::update_control_display (ControlUI* cui)
673 {
674         /* XXX how do we handle logarithmic stuff here ? */
675
676         cui->update_pending = false;
677
678         float val = cui->control->get_value();
679
680         cui->ignore_change++;
681
682         if (cui->combo) {
683                 std::map<string,float>::iterator it;
684                 for (it = cui->combo_map->begin(); it != cui->combo_map->end(); ++it) {
685                         if (it->second == val) {
686                                 cui->combo->set_active_text(it->first);
687                                 break;
688                         }
689                 }
690         } else if (cui->button) {
691
692                 if (val > 0.5) {
693                         cui->button->set_active (true);
694                 } else {
695                         cui->button->set_active (false);
696                 }
697         }
698
699         if( cui->controller ) {
700             cui->controller->display_effective_value();
701         }
702
703
704         /*} else {
705                 if (cui->logarithmic) {
706                         val = log(val);
707                 }
708                 if (val != cui->adjustment->get_value()) {
709                         cui->adjustment->set_value (val);
710                 }
711         }*/
712         cui->ignore_change--;
713 }
714
715 void
716 GenericPluginUI::control_port_toggled (ControlUI* cui)
717 {
718         cui->ignore_change++;
719         insert->automation_control (cui->parameter())->set_value (cui->button->get_active());
720         cui->ignore_change--;
721 }
722
723 void
724 GenericPluginUI::control_combo_changed (ControlUI* cui)
725 {
726         if (!cui->ignore_change) {
727                 string value = cui->combo->get_active_text();
728                 std::map<string,float> mapping = *cui->combo_map;
729                 insert->automation_control(cui->parameter())->set_value(mapping[value]);
730         }
731 }
732
733 void
734 GenericPluginUI::processor_active_changed (boost::weak_ptr<Processor> weak_processor)
735 {
736         ENSURE_GUI_THREAD (*this, &GenericPluginUI::processor_active_changed, weak_processor)
737
738         boost::shared_ptr<Processor> processor = weak_processor.lock();
739
740         bypass_button.set_active (!processor || !processor->active());
741 }
742
743 bool
744 GenericPluginUI::start_updating (GdkEventAny*)
745 {
746         if (output_controls.size() > 0 ) {
747                 screen_update_connection.disconnect();
748                 screen_update_connection = ARDOUR_UI::instance()->RapidScreenUpdate.connect
749                         (sigc::mem_fun(*this, &GenericPluginUI::output_update));
750         }
751         return false;
752 }
753
754 bool
755 GenericPluginUI::stop_updating (GdkEventAny*)
756 {
757         for (vector<ControlUI*>::iterator i = input_controls.begin(); i != input_controls.end(); ++i) {
758                 (*i)->controller->stop_updating ();
759         }
760         
761         if (output_controls.size() > 0 ) {
762                 screen_update_connection.disconnect();
763         }
764         
765         return false;
766 }
767
768 void
769 GenericPluginUI::output_update ()
770 {
771         for (vector<ControlUI*>::iterator i = output_controls.begin(); i != output_controls.end(); ++i) {
772                 float val = plugin->get_parameter ((*i)->parameter().id());
773                 char buf[32];
774                 snprintf (buf, sizeof(buf), "%.2f", val);
775                 (*i)->display_label->set_text (buf);
776
777                 /* autoscaling for the meter */
778                 if ((*i)->meterinfo && (*i)->meterinfo->packed) {
779
780                         if (val < (*i)->meterinfo->min) {
781                                 if ((*i)->meterinfo->min_unbound)
782                                         (*i)->meterinfo->min = val;
783                                 else
784                                         val = (*i)->meterinfo->min;
785                         }
786
787                         if (val > (*i)->meterinfo->max) {
788                                 if ((*i)->meterinfo->max_unbound)
789                                         (*i)->meterinfo->max = val;
790                                 else
791                                         val = (*i)->meterinfo->max;
792                         }
793
794                         if ((*i)->meterinfo->max > (*i)->meterinfo->min ) {
795                                 float lval = (val - (*i)->meterinfo->min) / ((*i)->meterinfo->max - (*i)->meterinfo->min) ;
796                                 (*i)->meterinfo->meter->set (lval );
797                         }
798                 }
799         }
800 }
801
802 vector<string>
803 GenericPluginUI::setup_scale_values(guint32 port_index, ControlUI* cui)
804 {
805         vector<string> enums;
806         boost::shared_ptr<LadspaPlugin> lp;
807 #ifdef HAVE_SLV2
808         boost::shared_ptr<LV2Plugin> lv2p;
809 #endif
810
811         if ((lp = boost::dynamic_pointer_cast<LadspaPlugin>(plugin)) != 0) {
812                 // all LADPSA plugins have a numeric unique ID
813                 uint32_t id = atol (lp->unique_id().c_str());
814
815                 cui->combo_map = new std::map<string, float>;
816                 lrdf_defaults* defaults = lrdf_get_scale_values(id, port_index);
817                 if (defaults)   {
818                         for (uint32_t i = 0; i < defaults->count; ++i) {
819                                 enums.push_back(defaults->items[i].label);
820                                 pair<string, float> newpair;
821                                 newpair.first = defaults->items[i].label;
822                                 newpair.second = defaults->items[i].value;
823                                 cui->combo_map->insert(newpair);
824                         }
825
826                         lrdf_free_setting_values(defaults);
827                 }
828
829 #ifdef HAVE_SLV2
830         } else if ((lv2p = boost::dynamic_pointer_cast<LV2Plugin>(plugin)) != 0) {
831
832                 SLV2Port port = lv2p->slv2_port(port_index);
833                 SLV2ScalePoints points = slv2_port_get_scale_points(lv2p->slv2_plugin(), port);
834                 cui->combo_map = new std::map<string, float>;
835
836                 for (unsigned i=0; i < slv2_scale_points_size(points); ++i) {
837                         SLV2ScalePoint p = slv2_scale_points_get_at(points, i);
838                         SLV2Value label = slv2_scale_point_get_label(p);
839                         SLV2Value value = slv2_scale_point_get_value(p);
840                         if (label && (slv2_value_is_float(value) || slv2_value_is_int(value))) {
841                                 enums.push_back(slv2_value_as_string(label));
842                                 pair<string, float> newpair;
843                                 newpair.first = slv2_value_as_string(label);
844                                 newpair.second = slv2_value_as_float(value);
845                                 cui->combo_map->insert(newpair);
846                         }
847                 }
848
849                 slv2_scale_points_free(points);
850 #endif
851         }
852
853
854         return enums;
855 }
856
857
858