2e20f9dcc8949436f760e7cea31e464ff2f5d156
[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
125         _session->RouteAdded.connect (
126                         _route_connections, invalidator (*this), boost::bind (&PluginDSPLoadWindow::refill_processors, this), gui_context()
127                         );
128
129         RouteList routes = _session->get_routelist ();
130         for (RouteList::const_iterator i = routes.begin(); i != routes.end(); ++i) {
131
132                 (*i)->foreach_processor (sigc::bind (sigc::mem_fun (*this, &PluginDSPLoadWindow::add_processor_to_display), (*i)->name()));
133
134                 (*i)->processors_changed.connect (
135                                 _route_connections, invalidator (*this), boost::bind (&PluginDSPLoadWindow::refill_processors, this), gui_context()
136                                 );
137
138                 (*i)->DropReferences.connect (
139                                 _route_connections, invalidator (*this), boost::bind (&PluginDSPLoadWindow::refill_processors, this), gui_context()
140                                 );
141         }
142
143         if (_box.get_children().size() == 0) {
144                 _box.add (*Gtk::manage (new Gtk::Label (_("No Plugins"))));
145                 _box.show_all ();
146         } else if (_box.get_children().size() > 1) {
147                 _box.pack_start (_reset_button, Gtk::PACK_SHRINK, 2);
148                 _reset_button.show ();
149         }
150 }
151
152 void
153 PluginDSPLoadWindow::add_processor_to_display (boost::weak_ptr<Processor> w, std::string const& route_name)
154 {
155         boost::shared_ptr<Processor> p = w.lock ();
156         boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (p);
157         if (!pi) {
158                 return;
159         }
160         p->DropReferences.connect (_processor_connections, MISSING_INVALIDATOR, boost::bind (&PluginDSPLoadWindow::refill_processors, this), gui_context());
161         PluginLoadStatsGui* plsg = new PluginLoadStatsGui (pi);
162         
163         std::string name = route_name + " - " + pi->name();
164         Gtk::Frame* frame = new Gtk::Frame (name.c_str());
165         frame->add (*Gtk::manage (plsg));
166         _box.pack_start (*frame, Gtk::PACK_SHRINK, 2);
167
168         plsg->start_updating ();
169         frame->show_all ();
170 }
171
172 void
173 PluginDSPLoadWindow::clear_processor_stats (boost::weak_ptr<Processor> w)
174 {
175         boost::shared_ptr<Processor> p = w.lock ();
176         boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (p);
177         if (pi) {
178                 pi->clear_stats ();
179         }
180 }