Fix DSP load sorting with inactive plugins
[ardour.git] / gtk2_ardour / io_selector.cc
1 /*
2  * Copyright (C) 2005-2007 Doug McLain <doug@nostar.net>
3  * Copyright (C) 2005-2016 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2005 Taybin Rutkin <taybin@taybin.com>
5  * Copyright (C) 2006-2012 David Robillard <d@drobilla.net>
6  * Copyright (C) 2007-2012 Carl Hetherington <carl@carlh.net>
7  * Copyright (C) 2008 Hans Baier <hansfbaier@googlemail.com>
8  * Copyright (C) 2013-2015 Robin Gareus <robin@gareus.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License along
21  * with this program; if not, write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24
25 #include <stdint.h>
26
27 #include <glibmm/objectbase.h>
28
29 #include <gtkmm2ext/doi.h>
30
31 #include "ardour/audioengine.h"
32 #include "ardour/bundle.h"
33 #include "ardour/data_type.h"
34 #include "ardour/io.h"
35 #include "ardour/port.h"
36 #include "ardour/session.h"
37
38 #include "io_selector.h"
39 #include "utils.h"
40 #include "gui_thread.h"
41 #include "pbd/i18n.h"
42
43 using namespace ARDOUR;
44 using namespace ARDOUR_UI_UTILS;
45 using namespace Gtk;
46
47 IOSelector::IOSelector (Gtk::Window* p, ARDOUR::Session* session, boost::shared_ptr<ARDOUR::IO> io)
48         : PortMatrix (p, session, DataType::NIL)
49         , _io (io)
50 {
51         setup_type ();
52
53         /* signal flow from 0 to 1 */
54
55         _find_inputs_for_io_outputs = (_io->direction() == IO::Output);
56
57         if (_find_inputs_for_io_outputs) {
58                 _other = 1;
59                 _ours = 0;
60         } else {
61                 _other = 0;
62                 _ours = 1;
63         }
64
65         _port_group.reset (new PortGroup (io->name()));
66         _ports[_ours].add_group (_port_group);
67
68         io->changed.connect (_io_connection, invalidator (*this), boost::bind (&IOSelector::io_changed_proxy, this), gui_context ());
69
70         setup_all_ports ();
71         init ();
72 }
73
74 void
75 IOSelector::setup_type ()
76 {
77         /* set type according to what's in the IO */
78
79         int N = 0;
80         DataType type_with_ports = DataType::NIL;
81         for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
82                 if (_io->ports().num_ports (*i)) {
83                         type_with_ports = *i;
84                         ++N;
85                 }
86         }
87
88         if (N <= 1) {
89                 set_type (type_with_ports);
90         } else {
91                 set_type (DataType::NIL);
92         }
93 }
94
95 void
96 IOSelector::io_changed_proxy ()
97 {
98         /* The IO's changed signal is emitted from code that holds its route's processor lock,
99            so we can't call setup_all_ports (which results in a call to Route::foreach_processor)
100            without a deadlock unless we break things up with this idle handler.
101         */
102
103         Glib::signal_idle().connect_once (sigc::mem_fun (*this, &IOSelector::io_changed));
104 }
105
106 void
107 IOSelector::io_changed ()
108 {
109         setup_type ();
110         setup_all_ports ();
111 }
112
113 void
114 IOSelector::setup_ports (int dim)
115 {
116         if (!_session) {
117                 return;
118         }
119
120         _ports[dim].suspend_signals ();
121
122         if (dim == _other) {
123
124                 _ports[_other].gather (_session, type(), _find_inputs_for_io_outputs, false, show_only_bundles ());
125
126         } else {
127
128                 _port_group->clear ();
129                 _port_group->add_bundle (_io->bundle (), _io);
130         }
131
132         _ports[dim].resume_signals ();
133 }
134
135 void
136 IOSelector::set_state (ARDOUR::BundleChannel c[2], bool s)
137 {
138         ARDOUR::Bundle::PortList const & our_ports = c[_ours].bundle->channel_ports (c[_ours].channel);
139         ARDOUR::Bundle::PortList const & other_ports = c[_other].bundle->channel_ports (c[_other].channel);
140
141         for (ARDOUR::Bundle::PortList::const_iterator i = our_ports.begin(); i != our_ports.end(); ++i) {
142                 for (ARDOUR::Bundle::PortList::const_iterator j = other_ports.begin(); j != other_ports.end(); ++j) {
143
144                         boost::shared_ptr<Port> f = _session->engine().get_port_by_name (*i);
145                         if (!f) {
146                                 return;
147                         }
148
149                         if (s) {
150                                 if (!f->connected_to (*j)) {
151                                         _io->connect (f, *j, 0);
152                                 }
153                         } else {
154                                 if (f->connected_to (*j)) {
155                                         _io->disconnect (f, *j, 0);
156                                 }
157                         }
158                 }
159         }
160 }
161
162 PortMatrixNode::State
163 IOSelector::get_state (ARDOUR::BundleChannel c[2]) const
164 {
165         if (c[0].bundle->nchannels() == ChanCount::ZERO || c[1].bundle->nchannels() == ChanCount::ZERO) {
166                 return PortMatrixNode::NOT_ASSOCIATED;
167         }
168
169         ARDOUR::Bundle::PortList const & our_ports = c[_ours].bundle->channel_ports (c[_ours].channel);
170         ARDOUR::Bundle::PortList const & other_ports = c[_other].bundle->channel_ports (c[_other].channel);
171
172         if (!_session || our_ports.empty() || other_ports.empty()) {
173                 /* we're looking at a bundle with no parts associated with this channel,
174                    so nothing to connect */
175                 return PortMatrixNode::NOT_ASSOCIATED;
176         }
177
178         for (ARDOUR::Bundle::PortList::const_iterator i = our_ports.begin(); i != our_ports.end(); ++i) {
179                 for (ARDOUR::Bundle::PortList::const_iterator j = other_ports.begin(); j != other_ports.end(); ++j) {
180
181                         boost::shared_ptr<Port> f = _session->engine().get_port_by_name (*i);
182
183                         /* since we are talking about an IO, our ports should all have an associated Port *,
184                            so the above call should never fail */
185                         assert (f);
186
187                         if (!f->connected_to (*j)) {
188                                 /* if any one thing is not connected, all bets are off */
189                                 return PortMatrixNode::NOT_ASSOCIATED;
190                         }
191                 }
192         }
193
194         return PortMatrixNode::ASSOCIATED;
195 }
196
197 uint32_t
198 IOSelector::n_io_ports () const
199 {
200         if (!_find_inputs_for_io_outputs) {
201                 return _io->n_ports().get (_io->default_type());
202         } else {
203                 return _io->n_ports().get (_io->default_type());
204         }
205 }
206
207 bool
208 IOSelector::list_is_global (int dim) const
209 {
210         return (dim == _other);
211 }
212
213 std::string
214 IOSelector::disassociation_verb () const
215 {
216         return _("Disconnect");
217 }
218
219 std::string
220 IOSelector::channel_noun () const
221 {
222         return _("port");
223 }
224
225 IOSelectorWindow::IOSelectorWindow (ARDOUR::Session* session, boost::shared_ptr<ARDOUR::IO> io, bool /*can_cancel*/)
226         : ArdourWindow (_("I/O selector"))
227         , _selector (this, session, io)
228 {
229         set_name ("IOSelectorWindow2");
230
231         add (_selector);
232
233         io_name_changed (this);
234
235         show_all ();
236
237         signal_delete_event().connect (sigc::mem_fun (*this, &IOSelectorWindow::wm_delete));
238 }
239
240 bool
241 IOSelectorWindow::wm_delete (GdkEventAny* /*event*/)
242 {
243         _selector.Finished (IOSelector::Accepted);
244         return false;
245 }
246
247
248 void
249 IOSelectorWindow::on_map ()
250 {
251         _selector.setup_all_ports ();
252         Window::on_map ();
253 }
254
255 void
256 IOSelectorWindow::on_show ()
257 {
258         Gtk::Window::on_show ();
259         std::pair<uint32_t, uint32_t> const pm_max = _selector.max_size ();
260         resize_window_to_proportion_of_monitor (this, pm_max.first, pm_max.second);
261 }
262
263 void
264 IOSelectorWindow::io_name_changed (void*)
265 {
266         ENSURE_GUI_THREAD (*this, &IOSelectorWindow::io_name_changed, src)
267
268         std::string title;
269
270         if (!_selector.find_inputs_for_io_outputs()) {
271                 title = string_compose(_("%1 input"), _selector.io()->name());
272         } else {
273                 title = string_compose(_("%1 output"), _selector.io()->name());
274         }
275
276         set_title (title);
277 }
278