Add API to dispatch keyboard events to VST Plugins
[ardour.git] / gtk2_ardour / plugin_dspload_window.cc
1 /*
2  * Copyright (C) 2018 Robin Gareus <robin@gareus.org>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  */
18
19 #include <gtkmm/frame.h>
20 #include <gtkmm/label.h>
21 #include <gtkmm/viewport.h>
22
23 #include "ardour/session.h"
24 #include "gtkmm2ext/gui_thread.h"
25
26 #include "plugin_dspload_ui.h"
27 #include "plugin_dspload_window.h"
28
29 #include "pbd/i18n.h"
30
31 using namespace ARDOUR;
32
33 PluginDSPLoadWindow::PluginDSPLoadWindow ()
34         : ArdourWindow (_("Plugin DSP Load"))
35         , _reset_button (_("Reset All Stats"))
36 {
37         _scroller.set_border_width (0);
38         _scroller.set_shadow_type (Gtk::SHADOW_NONE);
39         _scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
40         _scroller.add (_box);
41
42         _reset_button.set_name ("generic button");
43         _reset_button.signal_clicked.connect (sigc::mem_fun (*this, &PluginDSPLoadWindow::clear_all_stats));
44
45         add (_scroller);
46         _box.show ();
47         _scroller.show ();
48
49         Gtk::Viewport* viewport = (Gtk::Viewport*) _scroller.get_child();
50         viewport->set_shadow_type(Gtk::SHADOW_NONE);
51         viewport->set_border_width(0);
52 }
53
54 PluginDSPLoadWindow::~PluginDSPLoadWindow ()
55 {
56         drop_references ();
57 }
58
59 void
60 PluginDSPLoadWindow::set_session (Session* s)
61 {
62         ArdourWindow::set_session (s);
63         if (!s) {
64                 drop_references ();
65         } else if (is_visible ()) {
66                 refill_processors ();
67         }
68 }
69
70 void
71 PluginDSPLoadWindow::session_going_away ()
72 {
73         ENSURE_GUI_THREAD (*this, &PluginDSPLoadWindow::session_going_away);
74         ArdourWindow::session_going_away ();
75         drop_references ();
76 }
77
78 void
79 PluginDSPLoadWindow::on_show ()
80 {
81         ArdourWindow::on_show ();
82         refill_processors ();
83 }
84
85 void
86 PluginDSPLoadWindow::on_hide ()
87 {
88         ArdourWindow::on_hide ();
89         drop_references ();
90 }
91
92 void
93 PluginDSPLoadWindow::clear_all_stats ()
94 {
95         RouteList routes = _session->get_routelist ();
96         for (RouteList::const_iterator i = routes.begin(); i != routes.end(); ++i) {
97                 (*i)->foreach_processor (sigc::mem_fun (*this, &PluginDSPLoadWindow::clear_processor_stats));
98         }
99 }
100
101 void
102 PluginDSPLoadWindow::drop_references ()
103 {
104         std::list<Gtk::Widget*> children = _box.get_children ();
105         for (std::list<Gtk::Widget*>::iterator child = children.begin(); child != children.end(); ++child) {
106                 (*child)->hide ();
107                 _box.remove (**child);
108                 if (*child != &_reset_button) {
109                         delete *child;
110                 }
111         }
112         _route_connections.drop_connections ();
113         _processor_connections.drop_connections ();
114 }
115
116 void
117 PluginDSPLoadWindow::refill_processors ()
118 {
119         drop_references ();
120         if (!_session || _session->deletion_in_progress()) {
121                 /* may be called from session d'tor, removing monitor-section w/plugin */
122                 return;
123         }
124         RouteList routes = _session->get_routelist ();
125         for (RouteList::const_iterator i = routes.begin(); i != routes.end(); ++i) {
126
127                 (*i)->foreach_processor (sigc::bind (sigc::mem_fun (*this, &PluginDSPLoadWindow::add_processor_to_display), (*i)->name()));
128
129                 (*i)->processors_changed.connect (
130                                 _route_connections, invalidator (*this), boost::bind (&PluginDSPLoadWindow::refill_processors, this), gui_context()
131                                 );
132
133                 (*i)->DropReferences.connect (
134                                 _route_connections, invalidator (*this), boost::bind (&PluginDSPLoadWindow::refill_processors, this), gui_context()
135                                 );
136         }
137
138         if (_box.get_children().size() == 0) {
139                 _box.add (*Gtk::manage (new Gtk::Label (_("No Plugins"))));
140                 _box.show_all ();
141         } else if (_box.get_children().size() > 1) {
142                 _box.pack_start (_reset_button, Gtk::PACK_SHRINK, 2);
143                 _reset_button.show ();
144         }
145 }
146
147 void
148 PluginDSPLoadWindow::add_processor_to_display (boost::weak_ptr<Processor> w, std::string const& route_name)
149 {
150         boost::shared_ptr<Processor> p = w.lock ();
151         boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (p);
152         if (!pi) {
153                 return;
154         }
155         p->DropReferences.connect (_processor_connections, MISSING_INVALIDATOR, boost::bind (&PluginDSPLoadWindow::refill_processors, this), gui_context());
156         PluginLoadStatsGui* plsg = new PluginLoadStatsGui (pi);
157         
158         std::string name = route_name + " - " + pi->name();
159         Gtk::Frame* frame = new Gtk::Frame (name.c_str());
160         frame->add (*Gtk::manage (plsg));
161         _box.pack_start (*frame, Gtk::PACK_SHRINK, 2);
162
163         plsg->start_updating ();
164         frame->show_all ();
165 }
166
167 void
168 PluginDSPLoadWindow::clear_processor_stats (boost::weak_ptr<Processor> w)
169 {
170         boost::shared_ptr<Processor> p = w.lock ();
171         boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (p);
172         if (pi) {
173                 pi->clear_stats ();
174         }
175 }