2 * Copyright (C) 2015 Ben Loftis <ben@harrisonconsoles.com>
3 * Copyright (C) 2015 Robin Gareus <robin@gareus.org>
4 * Copyright (C) 2016 Paul Davis <paul@linuxaudiosystems.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <glibmm/objectbase.h>
25 #include <gtkmm2ext/doi.h>
27 #include "ardour/audioengine.h"
28 #include "ardour/bundle.h"
29 #include "ardour/data_type.h"
30 #include "ardour/io.h"
31 #include "ardour/port.h"
32 #include "ardour/session.h"
34 #include "monitor_selector.h"
36 #include "gui_thread.h"
39 using namespace ARDOUR;
40 using namespace ARDOUR_UI_UTILS;
43 MonitorSelector::MonitorSelector (Gtk::Window* p, ARDOUR::Session* session, boost::shared_ptr<ARDOUR::IO> io)
44 : PortMatrix (p, session, DataType::AUDIO)
47 set_type (DataType::AUDIO);
49 /* signal flow from 0 to 1 */
51 _find_inputs_for_io_outputs = (_io->direction() == IO::Output);
53 if (_find_inputs_for_io_outputs) {
61 _port_group.reset (new PortGroup (io->name()));
62 _ports[_ours].add_group (_port_group);
64 io->changed.connect (_io_connection, invalidator (*this), boost::bind (&MonitorSelector::io_changed_proxy, this), gui_context ());
71 MonitorSelector::io_changed_proxy ()
73 /* The IO's changed signal is emitted from code that holds its route's processor lock,
74 so we can't call setup_all_ports (which results in a call to Route::foreach_processor)
75 without a deadlock unless we break things up with this idle handler.
78 Glib::signal_idle().connect_once (sigc::mem_fun (*this, &MonitorSelector::io_changed));
82 MonitorSelector::io_changed ()
88 MonitorSelector::setup_ports (int dim)
94 _ports[dim].suspend_signals ();
98 _ports[_other].gather (_session, type(), _find_inputs_for_io_outputs, false, show_only_bundles ());
102 _port_group->clear ();
103 _port_group->add_bundle (_io->bundle (), _io);
106 _ports[dim].resume_signals ();
110 MonitorSelector::set_state (ARDOUR::BundleChannel c[2], bool s)
112 ARDOUR::Bundle::PortList const & our_ports = c[_ours].bundle->channel_ports (c[_ours].channel);
113 ARDOUR::Bundle::PortList const & other_ports = c[_other].bundle->channel_ports (c[_other].channel);
115 for (ARDOUR::Bundle::PortList::const_iterator i = our_ports.begin(); i != our_ports.end(); ++i) {
116 for (ARDOUR::Bundle::PortList::const_iterator j = other_ports.begin(); j != other_ports.end(); ++j) {
118 boost::shared_ptr<Port> f = _session->engine().get_port_by_name (*i);
124 if (!f->connected_to (*j)) {
125 _io->connect (f, *j, 0);
128 if (f->connected_to (*j)) {
129 _io->disconnect (f, *j, 0);
136 PortMatrixNode::State
137 MonitorSelector::get_state (ARDOUR::BundleChannel c[2]) const
139 if (c[0].bundle->nchannels() == ChanCount::ZERO || c[1].bundle->nchannels() == ChanCount::ZERO) {
140 return PortMatrixNode::NOT_ASSOCIATED;
143 ARDOUR::Bundle::PortList const & our_ports = c[_ours].bundle->channel_ports (c[_ours].channel);
144 ARDOUR::Bundle::PortList const & other_ports = c[_other].bundle->channel_ports (c[_other].channel);
146 if (!_session || our_ports.empty() || other_ports.empty()) {
147 /* we're looking at a bundle with no parts associated with this channel,
148 so nothing to connect */
149 return PortMatrixNode::NOT_ASSOCIATED;
152 for (ARDOUR::Bundle::PortList::const_iterator i = our_ports.begin(); i != our_ports.end(); ++i) {
153 for (ARDOUR::Bundle::PortList::const_iterator j = other_ports.begin(); j != other_ports.end(); ++j) {
155 boost::shared_ptr<Port> f = _session->engine().get_port_by_name (*i);
157 /* since we are talking about an IO, our ports should all have an associated Port *,
158 so the above call should never fail */
161 if (!f->connected_to (*j)) {
162 /* if any one thing is not connected, all bets are off */
163 return PortMatrixNode::NOT_ASSOCIATED;
168 return PortMatrixNode::ASSOCIATED;
172 MonitorSelector::n_io_ports () const
174 if (!_find_inputs_for_io_outputs) {
175 return _io->n_ports().get (_io->default_type());
177 return _io->n_ports().get (_io->default_type());
182 MonitorSelector::list_is_global (int dim) const
184 return (dim == _other);
188 MonitorSelector::disassociation_verb () const
190 return _("Disconnect");
194 MonitorSelector::channel_noun () const
199 MonitorSelectorWindow::MonitorSelectorWindow (ARDOUR::Session* session, boost::shared_ptr<ARDOUR::IO> io, bool /*can_cancel*/)
200 : ArdourWindow (_("Monitor output selector"))
201 , _selector (this, session, io)
203 set_name ("IOSelectorWindow2");
207 io_name_changed (this);
211 signal_delete_event().connect (sigc::mem_fun (*this, &MonitorSelectorWindow::wm_delete));
215 MonitorSelectorWindow::wm_delete (GdkEventAny* /*event*/)
217 _selector.Finished (MonitorSelector::Accepted);
223 MonitorSelectorWindow::on_map ()
225 _selector.setup_all_ports ();
230 MonitorSelectorWindow::on_show ()
232 Gtk::Window::on_show ();
233 std::pair<uint32_t, uint32_t> const pm_max = _selector.max_size ();
234 resize_window_to_proportion_of_monitor (this, pm_max.first, pm_max.second);
238 MonitorSelectorWindow::io_name_changed (void*)
240 ENSURE_GUI_THREAD (*this, &MonitorSelectorWindow::io_name_changed, src)
244 if (!_selector.find_inputs_for_io_outputs()) {
245 title = string_compose(_("%1 input"), _selector.io()->name());
247 title = string_compose(_("%1 output"), _selector.io()->name());