Update GPL boilerplate and (C)
[ardour.git] / gtk2_ardour / monitor_selector.cc
1 /*
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>
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 #include <stdint.h>
22
23 #include <glibmm/objectbase.h>
24
25 #include <gtkmm2ext/doi.h>
26
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"
33
34 #include "monitor_selector.h"
35 #include "utils.h"
36 #include "gui_thread.h"
37 #include "pbd/i18n.h"
38
39 using namespace ARDOUR;
40 using namespace ARDOUR_UI_UTILS;
41 using namespace Gtk;
42
43 MonitorSelector::MonitorSelector (Gtk::Window* p, ARDOUR::Session* session, boost::shared_ptr<ARDOUR::IO> io)
44         : PortMatrix (p, session, DataType::AUDIO)
45         , _io (io)
46 {
47         set_type (DataType::AUDIO);
48
49         /* signal flow from 0 to 1 */
50
51         _find_inputs_for_io_outputs = (_io->direction() == IO::Output);
52
53         if (_find_inputs_for_io_outputs) {
54                 _other = 1;
55                 _ours = 0;
56         } else {
57                 _other = 0;
58                 _ours = 1;
59         }
60
61         _port_group.reset (new PortGroup (io->name()));
62         _ports[_ours].add_group (_port_group);
63
64         io->changed.connect (_io_connection, invalidator (*this), boost::bind (&MonitorSelector::io_changed_proxy, this), gui_context ());
65
66         setup_all_ports ();
67         init ();
68 }
69
70 void
71 MonitorSelector::io_changed_proxy ()
72 {
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.
76         */
77
78         Glib::signal_idle().connect_once (sigc::mem_fun (*this, &MonitorSelector::io_changed));
79 }
80
81 void
82 MonitorSelector::io_changed ()
83 {
84         setup_all_ports ();
85 }
86
87 void
88 MonitorSelector::setup_ports (int dim)
89 {
90         if (!_session) {
91                 return;
92         }
93
94         _ports[dim].suspend_signals ();
95
96         if (dim == _other) {
97
98                 _ports[_other].gather (_session, type(), _find_inputs_for_io_outputs, false, show_only_bundles ());
99
100         } else {
101
102                 _port_group->clear ();
103                 _port_group->add_bundle (_io->bundle (), _io);
104         }
105
106         _ports[dim].resume_signals ();
107 }
108
109 void
110 MonitorSelector::set_state (ARDOUR::BundleChannel c[2], bool s)
111 {
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);
114
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) {
117
118                         boost::shared_ptr<Port> f = _session->engine().get_port_by_name (*i);
119                         if (!f) {
120                                 return;
121                         }
122
123                         if (s) {
124                                 if (!f->connected_to (*j)) {
125                                         _io->connect (f, *j, 0);
126                                 }
127                         } else {
128                                 if (f->connected_to (*j)) {
129                                         _io->disconnect (f, *j, 0);
130                                 }
131                         }
132                 }
133         }
134 }
135
136 PortMatrixNode::State
137 MonitorSelector::get_state (ARDOUR::BundleChannel c[2]) const
138 {
139         if (c[0].bundle->nchannels() == ChanCount::ZERO || c[1].bundle->nchannels() == ChanCount::ZERO) {
140                 return PortMatrixNode::NOT_ASSOCIATED;
141         }
142
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);
145
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;
150         }
151
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) {
154
155                         boost::shared_ptr<Port> f = _session->engine().get_port_by_name (*i);
156
157                         /* since we are talking about an IO, our ports should all have an associated Port *,
158                            so the above call should never fail */
159                         assert (f);
160
161                         if (!f->connected_to (*j)) {
162                                 /* if any one thing is not connected, all bets are off */
163                                 return PortMatrixNode::NOT_ASSOCIATED;
164                         }
165                 }
166         }
167
168         return PortMatrixNode::ASSOCIATED;
169 }
170
171 uint32_t
172 MonitorSelector::n_io_ports () const
173 {
174         if (!_find_inputs_for_io_outputs) {
175                 return _io->n_ports().get (_io->default_type());
176         } else {
177                 return _io->n_ports().get (_io->default_type());
178         }
179 }
180
181 bool
182 MonitorSelector::list_is_global (int dim) const
183 {
184         return (dim == _other);
185 }
186
187 std::string
188 MonitorSelector::disassociation_verb () const
189 {
190         return _("Disconnect");
191 }
192
193 std::string
194 MonitorSelector::channel_noun () const
195 {
196         return _("port");
197 }
198
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)
202 {
203         set_name ("IOSelectorWindow2");
204
205         add (_selector);
206
207         io_name_changed (this);
208
209         show_all ();
210
211         signal_delete_event().connect (sigc::mem_fun (*this, &MonitorSelectorWindow::wm_delete));
212 }
213
214 bool
215 MonitorSelectorWindow::wm_delete (GdkEventAny* /*event*/)
216 {
217         _selector.Finished (MonitorSelector::Accepted);
218         return false;
219 }
220
221
222 void
223 MonitorSelectorWindow::on_map ()
224 {
225         _selector.setup_all_ports ();
226         Window::on_map ();
227 }
228
229 void
230 MonitorSelectorWindow::on_show ()
231 {
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);
235 }
236
237 void
238 MonitorSelectorWindow::io_name_changed (void*)
239 {
240         ENSURE_GUI_THREAD (*this, &MonitorSelectorWindow::io_name_changed, src)
241
242         std::string title;
243
244         if (!_selector.find_inputs_for_io_outputs()) {
245                 title = string_compose(_("%1 input"), _selector.io()->name());
246         } else {
247                 title = string_compose(_("%1 output"), _selector.io()->name());
248         }
249
250         set_title (title);
251 }
252