Fix crash when X11 is not available for VST UIs
[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 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 along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 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         , _sort_avg_button (_("Sort by Average Load"))
37         , _sort_max_button (_("Sort by Worst-Case Load"))
38 {
39         _scroller.set_border_width (0);
40         _scroller.set_shadow_type (Gtk::SHADOW_NONE);
41         _scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
42         _scroller.add (_box);
43
44         _reset_button.set_name ("generic button");
45         _sort_avg_button.set_name ("generic button");
46         _sort_max_button.set_name ("generic button");
47
48         _reset_button.signal_clicked.connect (sigc::mem_fun (*this, &PluginDSPLoadWindow::clear_all_stats));
49         _sort_avg_button.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginDSPLoadWindow::sort_by_stats), true));
50         _sort_max_button.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginDSPLoadWindow::sort_by_stats), false));
51
52         add (_scroller);
53         _box.show ();
54         _scroller.show ();
55
56         Gtk::Viewport* viewport = (Gtk::Viewport*) _scroller.get_child();
57         viewport->set_shadow_type(Gtk::SHADOW_NONE);
58         viewport->set_border_width(0);
59
60         _ctrlbox.pack_end (_reset_button, Gtk::PACK_SHRINK, 2);
61         _ctrlbox.pack_end (_sort_avg_button, Gtk::PACK_SHRINK, 2);
62         _ctrlbox.pack_end (_sort_max_button, Gtk::PACK_SHRINK, 2);
63         _ctrlbox.show_all ();
64 }
65
66 PluginDSPLoadWindow::~PluginDSPLoadWindow ()
67 {
68         drop_references ();
69 }
70
71 void
72 PluginDSPLoadWindow::set_session (Session* s)
73 {
74         ArdourWindow::set_session (s);
75         if (!s) {
76                 drop_references ();
77         } else if (is_visible ()) {
78                 refill_processors ();
79         }
80 }
81
82 void
83 PluginDSPLoadWindow::session_going_away ()
84 {
85         ENSURE_GUI_THREAD (*this, &PluginDSPLoadWindow::session_going_away);
86         ArdourWindow::session_going_away ();
87         drop_references ();
88 }
89
90 void
91 PluginDSPLoadWindow::on_show ()
92 {
93         ArdourWindow::on_show ();
94         refill_processors ();
95 }
96
97 void
98 PluginDSPLoadWindow::on_hide ()
99 {
100         ArdourWindow::on_hide ();
101         drop_references ();
102 }
103
104 void
105 PluginDSPLoadWindow::clear_all_stats ()
106 {
107         RouteList routes = _session->get_routelist ();
108         for (RouteList::const_iterator i = routes.begin(); i != routes.end(); ++i) {
109                 (*i)->foreach_processor (sigc::mem_fun (*this, &PluginDSPLoadWindow::clear_processor_stats));
110         }
111 }
112
113 struct DSPLoadSorter
114 {
115         bool _avg;
116         DSPLoadSorter (bool avg) : _avg (avg) {}
117         bool operator() (PluginLoadStatsGui* a, PluginLoadStatsGui* b) {
118                 return _avg ? (a->dsp_avg () < b->dsp_avg ()) : (a->dsp_max () < b->dsp_max ());
119         }
120 };
121
122 void
123 PluginDSPLoadWindow::sort_by_stats (bool avg)
124 {
125         std::list<PluginLoadStatsGui*> pl;
126         std::list<Gtk::Widget*> children = _box.get_children ();
127         for (std::list<Gtk::Widget*>::iterator child = children.begin(); child != children.end(); ++child) {
128                 Gtk::Frame* frame = dynamic_cast<Gtk::Frame*>(*child);
129                 if (!frame) continue;
130                 PluginLoadStatsGui* plsg = dynamic_cast<PluginLoadStatsGui*>(frame->get_child());
131                 if (plsg) {
132                         pl.push_back (plsg);
133                 }
134         }
135         pl.sort (DSPLoadSorter (avg));
136         uint32_t pos = 0;
137         for (std::list<PluginLoadStatsGui*>::iterator i = pl.begin(); i != pl.end(); ++i, ++pos) {
138                 Gtk::Container* p = (*i)->get_parent();
139                 assert (p);
140                 _box.reorder_child (*p, pos);
141         }
142 }
143
144 void
145 PluginDSPLoadWindow::drop_references ()
146 {
147         std::list<Gtk::Widget*> children = _box.get_children ();
148         for (std::list<Gtk::Widget*>::iterator child = children.begin(); child != children.end(); ++child) {
149                 (*child)->hide ();
150                 _box.remove (**child);
151                 if (*child != &_ctrlbox) {
152                         delete *child;
153                 }
154         }
155         _route_connections.drop_connections ();
156         _processor_connections.drop_connections ();
157 }
158
159 void
160 PluginDSPLoadWindow::refill_processors ()
161 {
162         drop_references ();
163         if (!_session || _session->deletion_in_progress()) {
164                 /* may be called from session d'tor, removing monitor-section w/plugin */
165                 return;
166         }
167
168         _session->RouteAdded.connect (
169                         _route_connections, invalidator (*this), boost::bind (&PluginDSPLoadWindow::refill_processors, this), gui_context()
170                         );
171
172         RouteList routes = _session->get_routelist ();
173         for (RouteList::const_iterator i = routes.begin(); i != routes.end(); ++i) {
174
175                 (*i)->foreach_processor (sigc::bind (sigc::mem_fun (*this, &PluginDSPLoadWindow::add_processor_to_display), (*i)->name()));
176
177                 (*i)->processors_changed.connect (
178                                 _route_connections, invalidator (*this), boost::bind (&PluginDSPLoadWindow::refill_processors, this), gui_context()
179                                 );
180
181                 (*i)->DropReferences.connect (
182                                 _route_connections, invalidator (*this), boost::bind (&PluginDSPLoadWindow::refill_processors, this), gui_context()
183                                 );
184         }
185
186         if (_box.get_children().size() == 0) {
187                 _box.add (*Gtk::manage (new Gtk::Label (_("No Plugins"))));
188                 _box.show_all ();
189         } else if (_box.get_children().size() > 1) {
190                 _box.pack_start (_ctrlbox, Gtk::PACK_SHRINK, 2);
191                 _ctrlbox.show ();
192         }
193 }
194
195 void
196 PluginDSPLoadWindow::add_processor_to_display (boost::weak_ptr<Processor> w, std::string const& route_name)
197 {
198         boost::shared_ptr<Processor> p = w.lock ();
199         boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (p);
200         if (!pi || !pi->provides_stats ()) {
201                 return;
202         }
203         p->DropReferences.connect (_processor_connections, MISSING_INVALIDATOR, boost::bind (&PluginDSPLoadWindow::refill_processors, this), gui_context());
204         PluginLoadStatsGui* plsg = new PluginLoadStatsGui (pi);
205         
206         std::string name = route_name + " - " + pi->name();
207         Gtk::Frame* frame = new Gtk::Frame (name.c_str());
208         frame->add (*Gtk::manage (plsg));
209         _box.pack_start (*frame, Gtk::PACK_SHRINK, 2);
210
211         plsg->start_updating ();
212         frame->show_all ();
213 }
214
215 void
216 PluginDSPLoadWindow::clear_processor_stats (boost::weak_ptr<Processor> w)
217 {
218         boost::shared_ptr<Processor> p = w.lock ();
219         boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (p);
220         if (pi) {
221                 pi->clear_stats ();
222         }
223 }