Display hardware IO individually in the port matrices unless we are in _show_only_bun...
[ardour.git] / gtk2_ardour / io_selector.cc
1 /*
2     Copyright (C) 2002-2007 Paul Davis
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
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <glibmm/objectbase.h>
21
22 #include <gtkmm2ext/doi.h>
23
24 #include "ardour/port_insert.h"
25 #include "ardour/session.h"
26 #include "ardour/io.h"
27 #include "ardour/audioengine.h"
28 #include "ardour/track.h"
29 #include "ardour/audio_track.h"
30 #include "ardour/midi_track.h"
31 #include "ardour/mtdm.h"
32 #include "ardour/data_type.h"
33 #include "ardour/port.h"
34 #include "ardour/bundle.h"
35
36 #include "io_selector.h"
37 #include "utils.h"
38 #include "gui_thread.h"
39 #include "i18n.h"
40
41 using namespace ARDOUR;
42 using namespace Gtk;
43
44 IOSelector::IOSelector (Gtk::Window* p, ARDOUR::Session* session, boost::shared_ptr<ARDOUR::IO> io)
45         : PortMatrix (p, session, DataType::NIL)
46         , _io (io)
47 {
48         setup_type ();
49
50         /* signal flow from 0 to 1 */
51
52         _find_inputs_for_io_outputs = (_io->direction() == IO::Output);
53
54         if (_find_inputs_for_io_outputs) {
55                 _other = 1;
56                 _ours = 0;
57         } else {
58                 _other = 0;
59                 _ours = 1;
60         }
61
62         _port_group.reset (new PortGroup (io->name()));
63         _ports[_ours].add_group (_port_group);
64
65         io->changed.connect (_io_connection, invalidator (*this), boost::bind (&IOSelector::io_changed_proxy, this), gui_context ());
66
67         setup_all_ports ();
68         init ();
69 }
70
71 void
72 IOSelector::setup_type ()
73 {
74         /* set type according to what's in the IO */
75
76         int N = 0;
77         DataType type_with_ports = DataType::NIL;
78         for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
79                 if (_io->ports().num_ports (*i)) {
80                         type_with_ports = *i;
81                         ++N;
82                 }
83         }
84
85         if (N <= 1) {
86                 set_type (type_with_ports);
87         } else {
88                 set_type (DataType::NIL);
89         }
90 }
91
92 void
93 IOSelector::io_changed_proxy ()
94 {
95         /* The IO's changed signal is emitted from code that holds its route's processor lock,
96            so we can't call setup_all_ports (which results in a call to Route::foreach_processor)
97            without a deadlock unless we break things up with this idle handler.
98         */
99
100         Glib::signal_idle().connect_once (sigc::mem_fun (*this, &IOSelector::io_changed));
101 }
102
103 void
104 IOSelector::io_changed ()
105 {
106         setup_type ();
107         setup_all_ports ();
108 }
109
110 void
111 IOSelector::setup_ports (int dim)
112 {
113         if (!_session) {
114                 return;
115         }
116
117         _ports[dim].suspend_signals ();
118
119         if (dim == _other) {
120
121                 _ports[_other].gather (_session, type(), _find_inputs_for_io_outputs, false, show_only_bundles ());
122
123         } else {
124
125                 _port_group->clear ();
126                 _port_group->add_bundle (_io->bundle (), _io);
127         }
128
129         _ports[dim].resume_signals ();
130 }
131
132 void
133 IOSelector::set_state (ARDOUR::BundleChannel c[2], bool s)
134 {
135         ARDOUR::Bundle::PortList const & our_ports = c[_ours].bundle->channel_ports (c[_ours].channel);
136         ARDOUR::Bundle::PortList const & other_ports = c[_other].bundle->channel_ports (c[_other].channel);
137
138         for (ARDOUR::Bundle::PortList::const_iterator i = our_ports.begin(); i != our_ports.end(); ++i) {
139                 for (ARDOUR::Bundle::PortList::const_iterator j = other_ports.begin(); j != other_ports.end(); ++j) {
140
141                         boost::shared_ptr<Port> f = _session->engine().get_port_by_name (*i);
142                         if (!f) {
143                                 return;
144                         }
145
146                         if (s) {
147                                 if (!f->connected_to (*j)) {
148                                         _io->connect (f, *j, 0);
149                                 }
150                         } else {
151                                 if (f->connected_to (*j)) {
152                                         _io->disconnect (f, *j, 0);
153                                 }
154                         }
155                 }
156         }
157 }
158
159 PortMatrixNode::State
160 IOSelector::get_state (ARDOUR::BundleChannel c[2]) const
161 {
162         if (c[0].bundle->nchannels() == ChanCount::ZERO || c[1].bundle->nchannels() == ChanCount::ZERO) {
163                 return PortMatrixNode::NOT_ASSOCIATED;
164         }
165
166         ARDOUR::Bundle::PortList const & our_ports = c[_ours].bundle->channel_ports (c[_ours].channel);
167         ARDOUR::Bundle::PortList const & other_ports = c[_other].bundle->channel_ports (c[_other].channel);
168
169         if (!_session || our_ports.empty() || other_ports.empty()) {
170                 /* we're looking at a bundle with no parts associated with this channel,
171                    so nothing to connect */
172                 return PortMatrixNode::NOT_ASSOCIATED;
173         }
174
175         for (ARDOUR::Bundle::PortList::const_iterator i = our_ports.begin(); i != our_ports.end(); ++i) {
176                 for (ARDOUR::Bundle::PortList::const_iterator j = other_ports.begin(); j != other_ports.end(); ++j) {
177
178                         boost::shared_ptr<Port> f = _session->engine().get_port_by_name (*i);
179
180                         /* since we are talking about an IO, our ports should all have an associated Port *,
181                            so the above call should never fail */
182                         assert (f);
183
184                         if (!f->connected_to (*j)) {
185                                 /* if any one thing is not connected, all bets are off */
186                                 return PortMatrixNode::NOT_ASSOCIATED;
187                         }
188                 }
189         }
190
191         return PortMatrixNode::ASSOCIATED;
192 }
193
194 uint32_t
195 IOSelector::n_io_ports () const
196 {
197         if (!_find_inputs_for_io_outputs) {
198                 return _io->n_ports().get (_io->default_type());
199         } else {
200                 return _io->n_ports().get (_io->default_type());
201         }
202 }
203
204 bool
205 IOSelector::list_is_global (int dim) const
206 {
207         return (dim == _other);
208 }
209
210 string
211 IOSelector::disassociation_verb () const
212 {
213         return _("Disconnect");
214 }
215
216 string
217 IOSelector::channel_noun () const
218 {
219         return _("port");
220 }
221
222 IOSelectorWindow::IOSelectorWindow (ARDOUR::Session* session, boost::shared_ptr<ARDOUR::IO> io, bool /*can_cancel*/)
223         : ArdourWindow (_("I/O selector"))
224         , _selector (this, session, io)
225 {
226         set_name ("IOSelectorWindow2");
227
228         add (_selector);
229
230         set_position (Gtk::WIN_POS_MOUSE);
231
232         io_name_changed (this);
233
234         show_all ();
235
236         signal_delete_event().connect (sigc::mem_fun (*this, &IOSelectorWindow::wm_delete));
237 }
238
239 bool
240 IOSelectorWindow::wm_delete (GdkEventAny* /*event*/)
241 {
242         _selector.Finished (IOSelector::Accepted);
243         hide ();
244         return true;
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         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         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