Allow port matrix to show both audio and midi ports at the same time, and use that...
[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         /* 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         setup_all_ports ();
65         init ();
66 }
67
68 void
69 IOSelector::setup_ports (int dim)
70 {
71         if (!_session) {
72                 return;
73         }
74         
75         _ports[dim].suspend_signals ();
76
77         if (dim == _other) {
78
79                 _ports[_other].gather (_session, type(), _find_inputs_for_io_outputs, false);
80
81         } else {
82
83                 _port_group->clear ();
84                 _port_group->add_bundle (_io->bundle (), _io);
85         }
86
87         _ports[dim].resume_signals ();
88 }
89
90 void
91 IOSelector::set_state (ARDOUR::BundleChannel c[2], bool s)
92 {
93         ARDOUR::Bundle::PortList const & our_ports = c[_ours].bundle->channel_ports (c[_ours].channel);
94         ARDOUR::Bundle::PortList const & other_ports = c[_other].bundle->channel_ports (c[_other].channel);
95
96         for (ARDOUR::Bundle::PortList::const_iterator i = our_ports.begin(); i != our_ports.end(); ++i) {
97                 for (ARDOUR::Bundle::PortList::const_iterator j = other_ports.begin(); j != other_ports.end(); ++j) {
98
99                         Port* f = _session->engine().get_port_by_name (*i);
100                         if (!f) {
101                                 return;
102                         }
103
104                         if (s) {
105                                 _io->connect (f, *j, 0);
106                         } else {
107                                 _io->disconnect (f, *j, 0);
108                         }
109                 }
110         }
111 }
112
113 PortMatrixNode::State
114 IOSelector::get_state (ARDOUR::BundleChannel c[2]) const
115 {
116         ARDOUR::Bundle::PortList const & our_ports = c[_ours].bundle->channel_ports (c[_ours].channel);
117         ARDOUR::Bundle::PortList const & other_ports = c[_other].bundle->channel_ports (c[_other].channel);
118
119         if (our_ports.empty() || other_ports.empty()) {
120                 /* we're looking at a bundle with no parts associated with this channel,
121                    so nothing to connect */
122                 return PortMatrixNode::NOT_ASSOCIATED;
123         }
124
125         for (ARDOUR::Bundle::PortList::const_iterator i = our_ports.begin(); i != our_ports.end(); ++i) {
126                 for (ARDOUR::Bundle::PortList::const_iterator j = other_ports.begin(); j != other_ports.end(); ++j) {
127
128                         Port* f = _session->engine().get_port_by_name (*i);
129
130                         /* since we are talking about an IO, our ports should all have an associated Port *,
131                            so the above call should never fail */
132                         assert (f);
133
134                         if (!f->connected_to (*j)) {
135                                 /* if any one thing is not connected, all bets are off */
136                                 return PortMatrixNode::NOT_ASSOCIATED;
137                         }
138                 }
139         }
140
141         return PortMatrixNode::ASSOCIATED;
142 }
143
144 uint32_t
145 IOSelector::n_io_ports () const
146 {
147         if (!_find_inputs_for_io_outputs) {
148                 return _io->n_ports().get (_io->default_type());
149         } else {
150                 return _io->n_ports().get (_io->default_type());
151         }
152 }
153
154 bool
155 IOSelector::list_is_global (int dim) const
156 {
157         return (dim == _other);
158 }
159
160 string
161 IOSelector::disassociation_verb () const
162 {
163         return _("Disconnect");
164 }
165
166 string
167 IOSelector::channel_noun () const
168 {
169         return _("port");
170 }
171
172 IOSelectorWindow::IOSelectorWindow (ARDOUR::Session* session, boost::shared_ptr<ARDOUR::IO> io, bool /*can_cancel*/)
173         : _selector (this, session, io)
174 {
175         set_name ("IOSelectorWindow2");
176         set_title (_("I/O selector"));
177
178         Gtk::Alignment* alignment = manage(new Gtk::Alignment(0.5, 0.5, 0.0, 0.0));
179         alignment->add (_selector);
180         add (*alignment);
181
182         set_position (Gtk::WIN_POS_MOUSE);
183
184         io_name_changed (this);
185
186         show_all ();
187
188         signal_delete_event().connect (sigc::mem_fun (*this, &IOSelectorWindow::wm_delete));
189 }
190
191 bool
192 IOSelectorWindow::wm_delete (GdkEventAny* /*event*/)
193 {
194         _selector.Finished (IOSelector::Accepted);
195         hide ();
196         return true;
197 }
198
199
200 void
201 IOSelectorWindow::on_map ()
202 {
203         _selector.setup_all_ports ();
204         Window::on_map ();
205 }
206
207 void
208 IOSelectorWindow::on_show ()
209 {
210         Gtk::Window::on_show ();
211         pair<uint32_t, uint32_t> const pm_max = _selector.max_size ();
212         resize_window_to_proportion_of_monitor (this, pm_max.first, pm_max.second);
213 }
214
215 void
216 IOSelectorWindow::io_name_changed (void* src)
217 {
218         ENSURE_GUI_THREAD (*this, &IOSelectorWindow::io_name_changed, src)
219
220         string title;
221
222         if (!_selector.find_inputs_for_io_outputs()) {
223                 title = string_compose(_("%1 input"), _selector.io()->name());
224         } else {
225                 title = string_compose(_("%1 output"), _selector.io()->name());
226         }
227
228         set_title (title);
229 }
230
231 PortInsertUI::PortInsertUI (Gtk::Window* parent, ARDOUR::Session* sess, boost::shared_ptr<ARDOUR::PortInsert> pi)
232         : _pi (pi)
233         , latency_button (_("Measure Latency"))
234         , input_selector (parent, sess, pi->input())
235         , output_selector (parent, sess, pi->output())
236 {
237         latency_hbox.pack_start (latency_button, false, false);
238         latency_hbox.pack_start (latency_display, false, false);
239         latency_frame.add (latency_hbox);
240         
241         output_selector.set_min_height_divisor (2);
242         input_selector.set_min_height_divisor (2);
243         
244         pack_start (latency_frame);
245         pack_start (output_selector, true, true);
246         pack_start (input_selector, true, true);
247
248         latency_button.signal_toggled().connect (mem_fun (*this, &PortInsertUI::latency_button_toggled));
249 }
250
251 bool
252 PortInsertUI::check_latency_measurement ()
253 {
254         MTDM* mtdm = _pi->mtdm ();
255
256         if (mtdm->resolve () < 0) {
257                 latency_display.set_text (_("No signal detected"));
258                 return true;
259         }
260
261         if (mtdm->err () > 0.3) {
262                 mtdm->invert ();
263                 mtdm->resolve ();
264         }
265
266         char buf[64];
267         nframes_t sample_rate = AudioEngine::instance()->frame_rate();
268
269         if (sample_rate == 0) {
270                 latency_display.set_text (_("Disconnected from audio engine"));
271                 _pi->stop_latency_detection ();
272                 return false;
273         }
274
275         snprintf (buf, sizeof (buf), "%10.3lf frames %10.3lf ms", mtdm->del (), mtdm->del () * 1000.0f/sample_rate);
276
277         bool solid = true;
278
279         if (mtdm->err () > 0.2) {
280                 strcat (buf, " ??");
281                 solid = false;
282         }
283
284         if (mtdm->inv ()) {
285                 strcat (buf, " (Inv)");
286                 solid = false;
287         }
288
289         if (solid) {
290                 _pi->set_measured_latency ((nframes_t) rint (mtdm->del()));
291                 strcat (buf, " (set)");
292         }
293
294         latency_display.set_text (buf);
295         return true;
296 }
297
298 void
299 PortInsertUI::latency_button_toggled ()
300 {
301         if (latency_button.get_active ()) {
302
303                 _pi->start_latency_detection ();
304                 latency_display.set_text (_("Detecting ..."));
305                 latency_timeout = Glib::signal_timeout().connect (mem_fun (*this, &PortInsertUI::check_latency_measurement), 250);
306
307         } else {
308                 _pi->stop_latency_detection ();
309                 latency_timeout.disconnect ();
310         }
311 }
312
313
314 void
315 PortInsertUI::redisplay ()
316 {
317         input_selector.setup_ports (input_selector.other());
318         output_selector.setup_ports (output_selector.other());
319 }
320
321 void
322 PortInsertUI::finished (IOSelector::Result r)
323 {
324         input_selector.Finished (r);
325         output_selector.Finished (r);
326 }
327
328
329 PortInsertWindow::PortInsertWindow (ARDOUR::Session* sess, boost::shared_ptr<ARDOUR::PortInsert> pi, bool can_cancel)
330         : ArdourDialog ("port insert dialog"),
331           _portinsertui (this, sess, pi),
332           ok_button (can_cancel ? _("OK"): _("Close")),
333           cancel_button (_("Cancel"))
334 {
335
336         set_name ("IOSelectorWindow");
337         string title = _("Port Insert ");
338         title += pi->name();
339         set_title (title);
340
341         ok_button.set_name ("IOSelectorButton");
342         if (!can_cancel) {
343                 ok_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::CLOSE, Gtk::ICON_SIZE_BUTTON)));
344         }
345         cancel_button.set_name ("IOSelectorButton");
346
347         if (can_cancel) {
348                 cancel_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::CANCEL, Gtk::ICON_SIZE_BUTTON)));
349                 get_action_area()->pack_start (cancel_button, false, false);
350         } else {
351                 cancel_button.hide();
352         }
353         get_action_area()->pack_start (ok_button, false, false);
354
355         get_vbox()->pack_start (_portinsertui);
356
357         ok_button.signal_clicked().connect (sigc::mem_fun (*this, &PortInsertWindow::accept));
358         cancel_button.signal_clicked().connect (sigc::mem_fun (*this, &PortInsertWindow::cancel));
359
360         signal_delete_event().connect (sigc::mem_fun (*this, &PortInsertWindow::wm_delete), false);
361
362         pi->DropReferences.connect (going_away_connection, invalidator (*this), boost::bind (&PortInsertWindow::plugin_going_away, this), gui_context());
363 }
364
365 bool
366 PortInsertWindow::wm_delete (GdkEventAny* /*event*/)
367 {
368         accept ();
369         return true;
370 }
371
372 void
373 PortInsertWindow::plugin_going_away ()
374 {
375         ENSURE_GUI_THREAD (*this, &PortInsertWindow::plugin_going_away)
376
377         going_away_connection.disconnect ();
378         delete_when_idle (this);
379 }
380
381 void
382 PortInsertWindow::on_map ()
383 {
384         _portinsertui.redisplay ();
385         Window::on_map ();
386 }
387
388
389 void
390 PortInsertWindow::cancel ()
391 {
392         _portinsertui.finished (IOSelector::Cancelled);
393         hide ();
394 }
395
396 void
397 PortInsertWindow::accept ()
398 {
399         _portinsertui.finished (IOSelector::Accepted);
400         hide ();
401 }
402