implement relative clock editing (terminated by + or - keys, which adjust the clock...
[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 <gtkmm/messagedialog.h>
21 #include <glibmm/objectbase.h>
22
23 #include <gtkmm2ext/doi.h>
24
25 #include "ardour/port_insert.h"
26 #include "ardour/session.h"
27 #include "ardour/io.h"
28 #include "ardour/audioengine.h"
29 #include "ardour/track.h"
30 #include "ardour/audio_track.h"
31 #include "ardour/midi_track.h"
32 #include "ardour/mtdm.h"
33 #include "ardour/data_type.h"
34 #include "ardour/port.h"
35 #include "ardour/bundle.h"
36
37 #include "io_selector.h"
38 #include "utils.h"
39 #include "gui_thread.h"
40 #include "i18n.h"
41
42 using namespace ARDOUR;
43 using namespace Gtk;
44
45 IOSelector::IOSelector (Gtk::Window* p, ARDOUR::Session* session, boost::shared_ptr<ARDOUR::IO> io)
46         : PortMatrix (p, session, DataType::NIL)
47         , _io (io)
48 {
49         setup_type ();
50
51         /* signal flow from 0 to 1 */
52
53         _find_inputs_for_io_outputs = (_io->direction() == IO::Output);
54
55         if (_find_inputs_for_io_outputs) {
56                 _other = 1;
57                 _ours = 0;
58         } else {
59                 _other = 0;
60                 _ours = 1;
61         }
62
63         _port_group.reset (new PortGroup (io->name()));
64         _ports[_ours].add_group (_port_group);
65
66         io->changed.connect (_io_connection, invalidator (*this), boost::bind (&IOSelector::io_changed_proxy, this), gui_context ());
67
68         setup_all_ports ();
69         init ();
70 }
71
72 void
73 IOSelector::setup_type ()
74 {
75         /* set type according to what's in the IO */
76
77         int N = 0;
78         DataType type_with_ports = DataType::NIL;
79         for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
80                 if (_io->ports().num_ports (*i)) {
81                         type_with_ports = *i;
82                         ++N;
83                 }
84         }
85
86         if (N <= 1) {
87                 set_type (type_with_ports);
88         } else {
89                 set_type (DataType::NIL);
90         }
91 }
92
93 void
94 IOSelector::io_changed_proxy ()
95 {
96         /* The IO's changed signal is emitted from code that holds its route's processor lock,
97            so we can't call setup_all_ports (which results in a call to Route::foreach_processor)
98            without a deadlock unless we break things up with this idle handler.
99         */
100
101         Glib::signal_idle().connect_once (sigc::mem_fun (*this, &IOSelector::io_changed));
102 }
103
104 void
105 IOSelector::io_changed ()
106 {
107         setup_type ();
108         setup_all_ports ();
109 }
110
111 void
112 IOSelector::setup_ports (int dim)
113 {
114         if (!_session) {
115                 return;
116         }
117
118         _ports[dim].suspend_signals ();
119
120         if (dim == _other) {
121
122                 _ports[_other].gather (_session, type(), _find_inputs_for_io_outputs, false);
123
124         } else {
125
126                 _port_group->clear ();
127                 _port_group->add_bundle (_io->bundle (), _io);
128         }
129
130         _ports[dim].resume_signals ();
131 }
132
133 void
134 IOSelector::set_state (ARDOUR::BundleChannel c[2], bool s)
135 {
136         ARDOUR::Bundle::PortList const & our_ports = c[_ours].bundle->channel_ports (c[_ours].channel);
137         ARDOUR::Bundle::PortList const & other_ports = c[_other].bundle->channel_ports (c[_other].channel);
138
139         for (ARDOUR::Bundle::PortList::const_iterator i = our_ports.begin(); i != our_ports.end(); ++i) {
140                 for (ARDOUR::Bundle::PortList::const_iterator j = other_ports.begin(); j != other_ports.end(); ++j) {
141
142                         boost::shared_ptr<Port> f = _session->engine().get_port_by_name (*i);
143                         if (!f) {
144                                 return;
145                         }
146
147                         if (s) {
148                                 if (!f->connected_to (*j)) {
149                                         _io->connect (f, *j, 0);
150                                 }
151                         } else {
152                                 if (f->connected_to (*j)) {
153                                         _io->disconnect (f, *j, 0);
154                                 }
155                         }
156                 }
157         }
158 }
159
160 PortMatrixNode::State
161 IOSelector::get_state (ARDOUR::BundleChannel c[2]) const
162 {
163         if (c[0].bundle->nchannels() == ChanCount::ZERO || c[1].bundle->nchannels() == ChanCount::ZERO) {
164                 return PortMatrixNode::NOT_ASSOCIATED;
165         }
166
167         ARDOUR::Bundle::PortList const & our_ports = c[_ours].bundle->channel_ports (c[_ours].channel);
168         ARDOUR::Bundle::PortList const & other_ports = c[_other].bundle->channel_ports (c[_other].channel);
169
170         if (!_session || our_ports.empty() || other_ports.empty()) {
171                 /* we're looking at a bundle with no parts associated with this channel,
172                    so nothing to connect */
173                 return PortMatrixNode::NOT_ASSOCIATED;
174         }
175
176         for (ARDOUR::Bundle::PortList::const_iterator i = our_ports.begin(); i != our_ports.end(); ++i) {
177                 for (ARDOUR::Bundle::PortList::const_iterator j = other_ports.begin(); j != other_ports.end(); ++j) {
178
179                         boost::shared_ptr<Port> f = _session->engine().get_port_by_name (*i);
180
181                         /* since we are talking about an IO, our ports should all have an associated Port *,
182                            so the above call should never fail */
183                         assert (f);
184
185                         if (!f->connected_to (*j)) {
186                                 /* if any one thing is not connected, all bets are off */
187                                 return PortMatrixNode::NOT_ASSOCIATED;
188                         }
189                 }
190         }
191
192         return PortMatrixNode::ASSOCIATED;
193 }
194
195 uint32_t
196 IOSelector::n_io_ports () const
197 {
198         if (!_find_inputs_for_io_outputs) {
199                 return _io->n_ports().get (_io->default_type());
200         } else {
201                 return _io->n_ports().get (_io->default_type());
202         }
203 }
204
205 bool
206 IOSelector::list_is_global (int dim) const
207 {
208         return (dim == _other);
209 }
210
211 string
212 IOSelector::disassociation_verb () const
213 {
214         return _("Disconnect");
215 }
216
217 string
218 IOSelector::channel_noun () const
219 {
220         return _("port");
221 }
222
223 IOSelectorWindow::IOSelectorWindow (ARDOUR::Session* session, boost::shared_ptr<ARDOUR::IO> io, bool /*can_cancel*/)
224         : ArdourDialog (_("I/O selector"))
225         , _selector (this, session, io)
226 {
227         set_name ("IOSelectorWindow2");
228
229         get_vbox()->pack_start (_selector);
230
231         set_position (Gtk::WIN_POS_MOUSE);
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         hide ();
245         return true;
246 }
247
248
249 void
250 IOSelectorWindow::on_map ()
251 {
252         _selector.setup_all_ports ();
253         Window::on_map ();
254 }
255
256 void
257 IOSelectorWindow::on_show ()
258 {
259         Gtk::Window::on_show ();
260         pair<uint32_t, uint32_t> const pm_max = _selector.max_size ();
261         resize_window_to_proportion_of_monitor (this, pm_max.first, pm_max.second);
262 }
263
264 void
265 IOSelectorWindow::io_name_changed (void*)
266 {
267         ENSURE_GUI_THREAD (*this, &IOSelectorWindow::io_name_changed, src)
268
269         string title;
270
271         if (!_selector.find_inputs_for_io_outputs()) {
272                 title = string_compose(_("%1 input"), _selector.io()->name());
273         } else {
274                 title = string_compose(_("%1 output"), _selector.io()->name());
275         }
276
277         set_title (title);
278 }
279