Monitor new signal to rebuild sendlist
[ardour.git] / gtk2_ardour / route_params_ui.cc
1 /*
2  * Copyright (C) 2005-2007 Taybin Rutkin <taybin@taybin.com>
3  * Copyright (C) 2005-2017 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2007-2011 Carl Hetherington <carl@carlh.net>
5  * Copyright (C) 2007-2012 David Robillard <d@drobilla.net>
6  * Copyright (C) 2013-2019 Robin Gareus <robin@gareus.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #include <algorithm>
24 #include <inttypes.h>
25
26 #include <glibmm/threads.h>
27 #include <gtkmm/stock.h>
28
29 #include "ardour/audioengine.h"
30 #include "ardour/audio_track.h"
31 #include "ardour/plugin.h"
32 #include "ardour/plugin_insert.h"
33 #include "ardour/plugin_manager.h"
34 #include "ardour/port_insert.h"
35 #include "ardour/return.h"
36 #include "ardour/route.h"
37 #include "ardour/send.h"
38 #include "ardour/internal_send.h"
39
40 #include "gtkmm2ext/utils.h"
41 #include "gtkmm2ext/window_title.h"
42
43 #include "ardour_ui.h"
44 #include "gui_thread.h"
45 #include "io_selector.h"
46 #include "keyboard.h"
47 #include "mixer_ui.h"
48 #include "mixer_strip.h"
49 #include "port_insert_ui.h"
50 #include "plugin_selector.h"
51 #include "plugin_ui.h"
52 #include "return_ui.h"
53 #include "route_params_ui.h"
54 #include "send_ui.h"
55 #include "timers.h"
56
57 #include "pbd/i18n.h"
58
59 using namespace ARDOUR;
60 using namespace PBD;
61 using namespace Gtk;
62 using namespace Gtkmm2ext;
63
64 RouteParams_UI::RouteParams_UI ()
65         : ArdourWindow (_("Tracks and Busses"))
66         , track_menu(0)
67 {
68         insert_box = 0;
69         _input_iosel = 0;
70         _output_iosel = 0;
71         _active_view = 0;
72         latency_widget = 0;
73
74         using namespace Notebook_Helpers;
75
76         input_frame.set_shadow_type(Gtk::SHADOW_NONE);
77         output_frame.set_shadow_type(Gtk::SHADOW_NONE);
78         latency_frame.set_shadow_type (Gtk::SHADOW_NONE);
79
80         notebook.set_show_tabs (true);
81         notebook.set_show_border (true);
82         notebook.set_name ("RouteParamNotebook");
83
84         // create the tree model
85         route_display_model = ListStore::create(route_display_columns);
86
87         // setup the treeview
88         route_display.set_model(route_display_model);
89         route_display.append_column(_("Tracks/Busses"), route_display_columns.text);
90         route_display.set_name(X_("RouteParamsListDisplay"));
91         route_display.get_selection()->set_mode(Gtk::SELECTION_SINGLE); // default
92         route_display.set_reorderable(false);
93         route_display.set_size_request(75, -1);
94         route_display.set_headers_visible(true);
95         route_display.set_headers_clickable(true);
96
97         dynamic_cast<Gtk::CellRendererText*>(route_display.get_column_cell_renderer(0))->property_ellipsize() = Pango::ELLIPSIZE_START;
98
99         route_select_scroller.add(route_display);
100         route_select_scroller.set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
101
102         route_select_frame.set_name("RouteSelectBaseFrame");
103         route_select_frame.set_shadow_type (Gtk::SHADOW_IN);
104         route_select_frame.add(route_select_scroller);
105
106         list_vpacker.pack_start (route_select_frame, true, true);
107
108         notebook.pages().push_back (TabElem (input_frame, _("Inputs")));
109         notebook.pages().push_back (TabElem (output_frame, _("Outputs")));
110         notebook.pages().push_back (TabElem (redir_hpane, _("Plugins, Inserts & Sends")));
111         notebook.pages().push_back (TabElem (latency_frame, _("Latency")));
112
113         notebook.set_name ("InspectorNotebook");
114
115         title_label.set_name ("RouteParamsTitleLabel");
116         update_title();
117
118         latency_packer.set_spacing (18);
119         delay_label.set_alignment (0, 0.5);
120
121         // changeable area
122         route_param_frame.set_name("RouteParamsBaseFrame");
123         route_param_frame.set_shadow_type (Gtk::SHADOW_IN);
124
125         route_hpacker.pack_start (notebook, true, true);
126
127         route_vpacker.pack_start (title_label, false, false);
128         route_vpacker.pack_start (route_hpacker, true, true);
129
130         list_hpane.add (list_vpacker);
131         list_hpane.add (route_vpacker);
132
133         add (list_hpane);
134
135         set_name ("RouteParamsWindow");
136         set_default_size (620,370);
137         set_wmclass (X_("ardour_route_parameters"), PROGRAM_NAME);
138
139         // events
140         route_display.get_selection()->signal_changed().connect(sigc::mem_fun(*this, &RouteParams_UI::route_selected));
141         route_display.get_column(0)->signal_clicked().connect(sigc::mem_fun(*this, &RouteParams_UI::show_track_menu));
142
143         add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK|Gdk::BUTTON_RELEASE_MASK);
144
145         show_all();
146 }
147
148 RouteParams_UI::~RouteParams_UI ()
149 {
150         delete track_menu;
151 }
152
153 void
154 RouteParams_UI::add_routes (RouteList& routes)
155 {
156         ENSURE_GUI_THREAD (*this, &RouteParams_UI::add_routes, routes)
157
158         for (RouteList::iterator x = routes.begin(); x != routes.end(); ++x) {
159                 boost::shared_ptr<Route> route = (*x);
160
161                 if (route->is_auditioner()) {
162                         return;
163                 }
164
165                 TreeModel::Row row = *(route_display_model->append());
166                 row[route_display_columns.text] = route->name();
167                 row[route_display_columns.route] = route;
168
169                 //route_select_list.rows().back().select ();
170
171                 route->PropertyChanged.connect (*this, invalidator (*this), boost::bind (&RouteParams_UI::route_property_changed, this, _1, boost::weak_ptr<Route>(route)), gui_context());
172                 route->DropReferences.connect (*this, invalidator (*this), boost::bind (&RouteParams_UI::route_removed, this, boost::weak_ptr<Route>(route)), gui_context());
173         }
174 }
175
176
177 void
178 RouteParams_UI::route_property_changed (const PropertyChange& what_changed, boost::weak_ptr<Route> wr)
179 {
180         if (!what_changed.contains (ARDOUR::Properties::name)) {
181                 return;
182         }
183
184         boost::shared_ptr<Route> route (wr.lock());
185
186         if (!route) {
187                 return;
188         }
189
190         ENSURE_GUI_THREAD (*this, &RouteParams_UI::route_name_changed, wr)
191
192         bool found = false ;
193         TreeModel::Children rows = route_display_model->children();
194         for(TreeModel::Children::iterator iter = rows.begin(); iter != rows.end(); ++iter) {
195                 boost::shared_ptr<Route> r =(*iter)[route_display_columns.route];
196                 if (r == route) {
197                         (*iter)[route_display_columns.text] = route->name() ;
198                         found = true ;
199                         break;
200                 }
201         }
202
203         if(!found) {
204                 error << _("route display list item for renamed route not found!") << endmsg;
205         }
206
207         if (route == _route) {
208                 track_input_label.set_text (route->name());
209                 update_title();
210         }
211 }
212
213 void
214 RouteParams_UI::map_frozen()
215 {
216         ENSURE_GUI_THREAD (*this, &RouteParams_UI::map_frozen)
217         boost::shared_ptr<AudioTrack> at = boost::dynamic_pointer_cast<AudioTrack>(_route);
218         if (at && insert_box) {
219                 switch (at->freeze_state()) {
220                         case AudioTrack::Frozen:
221                                 insert_box->set_sensitive (false);
222                                 //hide_redirect_editors (); // TODO hide editor windows
223                                 break;
224                         default:
225                                 insert_box->set_sensitive (true);
226                                 // XXX need some way, maybe, to retoggle redirect editors
227                                 break;
228                 }
229         }
230 }
231
232 PluginSelector*
233 RouteParams_UI::plugin_selector() {
234         return Mixer_UI::instance()->plugin_selector ();
235 }
236
237 void
238 RouteParams_UI::setup_processor_boxes()
239 {
240         if (_session && _route) {
241
242                 // just in case... shouldn't need this
243                 cleanup_processor_boxes();
244
245                 // construct new redirect boxes
246                 insert_box = new ProcessorBox (_session, boost::bind (&RouteParams_UI::plugin_selector, this), _p_selection, 0);
247                 insert_box->set_route (_route);
248
249                 boost::shared_ptr<AudioTrack> at = boost::dynamic_pointer_cast<AudioTrack>(_route);
250                 if (at) {
251                         at->FreezeChange.connect (route_connections, invalidator (*this), boost::bind (&RouteParams_UI::map_frozen, this), gui_context());
252                 }
253                 redir_hpane.add (*insert_box);
254
255                 insert_box->ProcessorSelected.connect (sigc::mem_fun(*this, &RouteParams_UI::redirect_selected));  //note:  this indicates a double-click activation, not just a "selection"
256                 insert_box->ProcessorUnselected.connect (sigc::mem_fun(*this, &RouteParams_UI::redirect_selected));
257
258                 redir_hpane.show_all();
259         }
260 }
261
262 void
263 RouteParams_UI::cleanup_processor_boxes()
264 {
265         if (insert_box) {
266                 redir_hpane.remove(*insert_box);
267                 delete insert_box;
268                 insert_box = 0;
269         }
270 }
271
272 void
273 RouteParams_UI::refresh_latency ()
274 {
275         if (latency_widget) {
276                 latency_widget->refresh();
277
278                 char buf[128];
279                 snprintf (buf, sizeof (buf), _("Latency: %" PRId64 " samples"), _route->signal_latency ());
280                 delay_label.set_text (buf);
281         }
282 }
283
284 void
285 RouteParams_UI::cleanup_latency_frame ()
286 {
287         if (latency_widget) {
288                 latency_frame.remove ();
289                 latency_packer.remove (*latency_widget);
290                 latency_packer.remove (delay_label);
291                 latency_connections.drop_connections ();
292                 latency_click_connection.disconnect ();
293
294                 delete latency_widget;
295                 latency_widget = 0;
296
297         }
298 }
299
300 void
301 RouteParams_UI::setup_latency_frame ()
302 {
303         latency_widget = new LatencyGUI (*(_route->output()), _session->sample_rate(), AudioEngine::instance()->samples_per_cycle());
304
305         refresh_latency ();
306
307         latency_packer.pack_start (*latency_widget, false, false);
308         latency_packer.pack_start (delay_label, false, false);
309
310         _route->signal_latency_updated.connect (latency_connections, invalidator (*this), boost::bind (&RouteParams_UI::refresh_latency, this), gui_context());
311
312         latency_frame.add (latency_packer);
313         latency_frame.show_all ();
314 }
315
316 void
317 RouteParams_UI::setup_io_selector()
318 {
319         cleanup_io_selector();
320
321         // input
322         _input_iosel = new IOSelector (this, _session, _route->input());
323         _input_iosel->setup ();
324         input_frame.add (*_input_iosel);
325         input_frame.show_all();
326
327         // output
328         _output_iosel = new IOSelector (this, _session, _route->output());
329         _output_iosel->setup ();
330         output_frame.add (*_output_iosel);
331         output_frame.show_all();
332 }
333
334 void
335 RouteParams_UI::cleanup_io_selector()
336 {
337         if (_input_iosel) {
338                 _input_iosel->Finished (IOSelector::Cancelled);
339                 input_frame.remove();
340                 delete _input_iosel;
341                 _input_iosel = 0;
342         }
343
344         if (_output_iosel) {
345                 _output_iosel->Finished (IOSelector::Cancelled);
346
347                 output_frame.remove();
348                 delete _output_iosel;
349                 _output_iosel = 0;
350         }
351 }
352
353 void
354 RouteParams_UI::cleanup_view (bool stopupdate)
355 {
356         if (_active_view) {
357                 GenericPluginUI* plugui = 0;
358
359                 if (stopupdate && (plugui = dynamic_cast<GenericPluginUI*>(_active_view)) != 0) {
360                         plugui->stop_updating (0);
361                 }
362
363                 _processor_going_away_connection.disconnect ();
364                 redir_hpane.remove(*_active_view);
365                 delete _active_view;
366                 _active_view = 0;
367         }
368 }
369
370 void
371 RouteParams_UI::route_removed (boost::weak_ptr<Route> wr)
372 {
373         boost::shared_ptr<Route> route (wr.lock());
374
375         if (!route) {
376                 return;
377         }
378
379         ENSURE_GUI_THREAD (*this, invalidator (*this), &RouteParams_UI::route_removed, wr)
380
381         TreeModel::Children rows = route_display_model->children();
382         TreeModel::Children::iterator ri;
383
384         for(TreeModel::Children::iterator iter = rows.begin(); iter != rows.end(); ++iter) {
385                 boost::shared_ptr<Route> r =(*iter)[route_display_columns.route];
386
387                 if (r == route) {
388                         route_display_model->erase(iter);
389                         break;
390                 }
391         }
392
393         if (route == _route) {
394                 cleanup_io_selector();
395                 cleanup_view();
396                 cleanup_processor_boxes();
397
398                 _route.reset ((Route*) 0);
399                 _processor.reset ((Processor*) 0);
400                 update_title();
401         }
402 }
403
404 void
405 RouteParams_UI::set_session (Session *sess)
406 {
407         ArdourWindow::set_session (sess);
408
409         route_display_model->clear();
410
411         if (_session) {
412                 boost::shared_ptr<RouteList> r = _session->get_routes();
413                 add_routes (*r);
414                 _session->RouteAdded.connect (_session_connections, invalidator (*this), boost::bind (&RouteParams_UI::add_routes, this, _1), gui_context());
415                 start_updating ();
416         } else {
417                 stop_updating ();
418         }
419 }
420
421
422 void
423 RouteParams_UI::session_going_away ()
424 {
425         ENSURE_GUI_THREAD (*this, &RouteParams_UI::session_going_away);
426
427         SessionHandlePtr::session_going_away ();
428
429         route_display_model->clear();
430
431         cleanup_io_selector();
432         cleanup_view();
433         cleanup_processor_boxes();
434         cleanup_latency_frame ();
435
436         _route.reset ((Route*) 0);
437         _processor.reset ((Processor*) 0);
438         update_title();
439 }
440
441 void
442 RouteParams_UI::route_selected()
443 {
444         Glib::RefPtr<TreeSelection> selection = route_display.get_selection();
445         TreeModel::iterator iter = selection->get_selected(); // only used with Gtk::SELECTION_SINGLE
446
447         if(iter) {
448                 //If anything is selected
449                 boost::shared_ptr<Route> route = (*iter)[route_display_columns.route] ;
450
451                 if (_route == route) {
452                         // do nothing
453                         return;
454                 }
455
456                 // remove event binding from previously selected
457                 if (_route) {
458                         _route_processors_connection.disconnect ();
459                         cleanup_processor_boxes();
460                         cleanup_view();
461                         cleanup_io_selector();
462                         cleanup_latency_frame ();
463                 }
464
465                 // update the other panes with the correct info
466                 _route = route;
467                 //update_routeinfo (route);
468
469                 setup_io_selector();
470                 setup_processor_boxes();
471                 setup_latency_frame ();
472
473                 route->processors_changed.connect (_route_processors_connection, invalidator (*this), boost::bind (&RouteParams_UI::processors_changed, this, _1), gui_context());
474
475                 track_input_label.set_text (_route->name());
476
477                 update_title();
478
479         } else {
480                 // no selection
481                 if (_route) {
482                         _route_processors_connection.disconnect ();
483
484                         // remove from view
485                         cleanup_io_selector();
486                         cleanup_view();
487                         cleanup_processor_boxes();
488                         cleanup_latency_frame ();
489
490                         _route.reset ((Route*) 0);
491                         _processor.reset ((Processor*) 0);
492                         track_input_label.set_text(_("NO TRACK"));
493                         update_title();
494                 }
495         }
496 }
497
498 void
499 RouteParams_UI::processors_changed (RouteProcessorChange)
500 {
501         cleanup_view();
502
503         _processor.reset ((Processor*) 0);
504
505         //update_title();
506 }
507
508 void
509 RouteParams_UI::show_track_menu()
510 {
511         using namespace Menu_Helpers;
512
513         if (track_menu == 0) {
514                 track_menu = new Menu;
515                 track_menu->set_name ("ArdourContextMenu");
516                 track_menu->items().push_back (MenuElem (_("Add Track or Bus"), sigc::mem_fun (*(ARDOUR_UI::instance()), &ARDOUR_UI::add_route)));
517         }
518         track_menu->popup (1, gtk_get_current_event_time());
519 }
520
521 void
522 RouteParams_UI::redirect_selected (boost::shared_ptr<ARDOUR::Processor> proc)
523 {
524         boost::shared_ptr<Send> send;
525         boost::shared_ptr<Return> retrn;
526         boost::shared_ptr<PluginInsert> plugin_insert;
527         boost::shared_ptr<PortInsert> port_insert;
528
529         if ((boost::dynamic_pointer_cast<InternalSend> (proc)) != 0) {
530                 cleanup_view();
531                 _processor.reset ((Processor*) 0);
532                 update_title();
533                 return;
534         } else if ((send = boost::dynamic_pointer_cast<Send> (proc)) != 0) {
535
536                 SendUI *send_ui = new SendUI (this, send, _session);
537
538                 cleanup_view();
539                 send->DropReferences.connect (_processor_going_away_connection, invalidator (*this), boost::bind (&RouteParams_UI::processor_going_away, this, boost::weak_ptr<Processor>(proc)), gui_context());
540                 _active_view = send_ui;
541
542                 redir_hpane.add (*_active_view);
543                 redir_hpane.show_all();
544
545         } else if ((retrn = boost::dynamic_pointer_cast<Return> (proc)) != 0) {
546
547                 ReturnUI *return_ui = new ReturnUI (this, retrn, _session);
548
549                 cleanup_view();
550                 retrn->DropReferences.connect (_processor_going_away_connection, invalidator (*this), boost::bind (&RouteParams_UI::processor_going_away, this, boost::weak_ptr<Processor>(proc)), gui_context());
551                 _active_view = return_ui;
552
553                 redir_hpane.add (*_active_view);
554                 redir_hpane.show_all();
555
556         } else if ((plugin_insert = boost::dynamic_pointer_cast<PluginInsert> (proc)) != 0) {
557
558                 GenericPluginUI *plugin_ui = new GenericPluginUI (plugin_insert, true);
559
560                 cleanup_view();
561                 plugin_insert->plugin()->DropReferences.connect (_processor_going_away_connection, invalidator (*this), boost::bind (&RouteParams_UI::plugin_going_away, this, PreFader), gui_context());
562                 plugin_ui->start_updating (0);
563                 _active_view = plugin_ui;
564
565                 redir_hpane.add (*_active_view);
566                 redir_hpane.show_all();
567
568         } else if ((port_insert = boost::dynamic_pointer_cast<PortInsert> (proc)) != 0) {
569
570                 PortInsertUI *portinsert_ui = new PortInsertUI (this, _session, port_insert);
571
572                 cleanup_view();
573                 port_insert->DropReferences.connect (_processor_going_away_connection, invalidator (*this), boost::bind (&RouteParams_UI::processor_going_away, this, boost::weak_ptr<Processor> (proc)), gui_context());
574                 _active_view = portinsert_ui;
575
576                 redir_hpane.add (*_active_view);
577                 portinsert_ui->redisplay();
578                 redir_hpane.show_all();
579         }
580
581         _processor = proc;
582         update_title();
583
584 }
585
586 void
587 RouteParams_UI::plugin_going_away (Placement place)
588 {
589         ENSURE_GUI_THREAD (*this, &RouteParams_UI::plugin_going_away, place)
590
591         // delete the current view without calling finish
592
593         if (place == PreFader) {
594                 cleanup_view (false);
595                 _processor.reset ((Processor*) 0);
596         }
597 }
598
599 void
600 RouteParams_UI::processor_going_away (boost::weak_ptr<ARDOUR::Processor> wproc)
601 {
602         boost::shared_ptr<Processor> proc = (wproc.lock());
603
604         if (!proc) {
605                 return;
606         }
607
608         ENSURE_GUI_THREAD (*this, &RouteParams_UI::processor_going_away, wproc)
609
610         printf ("redirect going away\n");
611         // delete the current view without calling finish
612         if (proc == _processor) {
613                 cleanup_view (false);
614                 _processor.reset ((Processor*) 0);
615         }
616 }
617
618 void
619 RouteParams_UI::update_title ()
620 {
621         WindowTitle title (_("Tracks and Busses"));
622
623         if (_route) {
624                 title_label.set_text(_route->name());
625                 title += _route->name();
626                 set_title(title.get_string());
627         } else {
628                 title_label.set_text(_("No Track or Bus Selected"));
629                 title += _("No Track or Bus Selected");
630                 set_title(title.get_string());
631         }
632 }
633
634 void
635 RouteParams_UI::start_updating ()
636 {
637         update_connection = Timers::rapid_connect
638                 (sigc::mem_fun(*this, &RouteParams_UI::update_views));
639 }
640
641 void
642 RouteParams_UI::stop_updating ()
643 {
644         update_connection.disconnect();
645 }
646
647 void
648 RouteParams_UI::update_views ()
649 {
650         SendUI *sui;
651         // TODO: only do it if correct tab is showing
652
653         if ((sui = dynamic_cast<SendUI*> (_active_view)) != 0) {
654                 sui->update ();
655         }
656 }