f90ce88545ecb2f86735e60d7ac6303e2441a2ae
[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                         Port* f = _session->engine().get_port_by_name (*i);
143                         if (!f) {
144                                 return;
145                         }
146
147                         if (s) {
148                                 _io->connect (f, *j, 0);
149                         } else {
150                                 _io->disconnect (f, *j, 0);
151                         }
152                 }
153         }
154 }
155
156 PortMatrixNode::State
157 IOSelector::get_state (ARDOUR::BundleChannel c[2]) const
158 {
159         ARDOUR::Bundle::PortList const & our_ports = c[_ours].bundle->channel_ports (c[_ours].channel);
160         ARDOUR::Bundle::PortList const & other_ports = c[_other].bundle->channel_ports (c[_other].channel);
161
162         if (our_ports.empty() || other_ports.empty()) {
163                 /* we're looking at a bundle with no parts associated with this channel,
164                    so nothing to connect */
165                 return PortMatrixNode::NOT_ASSOCIATED;
166         }
167
168         for (ARDOUR::Bundle::PortList::const_iterator i = our_ports.begin(); i != our_ports.end(); ++i) {
169                 for (ARDOUR::Bundle::PortList::const_iterator j = other_ports.begin(); j != other_ports.end(); ++j) {
170
171                         Port* f = _session->engine().get_port_by_name (*i);
172
173                         /* since we are talking about an IO, our ports should all have an associated Port *,
174                            so the above call should never fail */
175                         assert (f);
176
177                         if (!f->connected_to (*j)) {
178                                 /* if any one thing is not connected, all bets are off */
179                                 return PortMatrixNode::NOT_ASSOCIATED;
180                         }
181                 }
182         }
183
184         return PortMatrixNode::ASSOCIATED;
185 }
186
187 uint32_t
188 IOSelector::n_io_ports () const
189 {
190         if (!_find_inputs_for_io_outputs) {
191                 return _io->n_ports().get (_io->default_type());
192         } else {
193                 return _io->n_ports().get (_io->default_type());
194         }
195 }
196
197 bool
198 IOSelector::list_is_global (int dim) const
199 {
200         return (dim == _other);
201 }
202
203 string
204 IOSelector::disassociation_verb () const
205 {
206         return _("Disconnect");
207 }
208
209 string
210 IOSelector::channel_noun () const
211 {
212         return _("port");
213 }
214
215 IOSelectorWindow::IOSelectorWindow (ARDOUR::Session* session, boost::shared_ptr<ARDOUR::IO> io, bool /*can_cancel*/)
216         : _selector (this, session, io)
217 {
218         set_name ("IOSelectorWindow2");
219         set_title (_("I/O selector"));
220
221         Gtk::Alignment* alignment = manage(new Gtk::Alignment(0.5, 0.5, 0.0, 0.0));
222         alignment->add (_selector);
223         add (*alignment);
224
225         set_position (Gtk::WIN_POS_MOUSE);
226
227         io_name_changed (this);
228
229         show_all ();
230
231         signal_delete_event().connect (sigc::mem_fun (*this, &IOSelectorWindow::wm_delete));
232 }
233
234 bool
235 IOSelectorWindow::wm_delete (GdkEventAny* /*event*/)
236 {
237         _selector.Finished (IOSelector::Accepted);
238         hide ();
239         return true;
240 }
241
242
243 void
244 IOSelectorWindow::on_map ()
245 {
246         _selector.setup_all_ports ();
247         Window::on_map ();
248 }
249
250 void
251 IOSelectorWindow::on_show ()
252 {
253         Gtk::Window::on_show ();
254         pair<uint32_t, uint32_t> const pm_max = _selector.max_size ();
255         resize_window_to_proportion_of_monitor (this, pm_max.first, pm_max.second);
256 }
257
258 void
259 IOSelectorWindow::io_name_changed (void* src)
260 {
261         ENSURE_GUI_THREAD (*this, &IOSelectorWindow::io_name_changed, src)
262
263         string title;
264
265         if (!_selector.find_inputs_for_io_outputs()) {
266                 title = string_compose(_("%1 input"), _selector.io()->name());
267         } else {
268                 title = string_compose(_("%1 output"), _selector.io()->name());
269         }
270
271         set_title (title);
272 }
273
274 PortInsertUI::PortInsertUI (Gtk::Window* parent, ARDOUR::Session* sess, boost::shared_ptr<ARDOUR::PortInsert> pi)
275         : _pi (pi)
276         , latency_button (_("Measure Latency"))
277         , input_selector (parent, sess, pi->input())
278         , output_selector (parent, sess, pi->output())
279 {
280         latency_hbox.pack_start (latency_button, false, false);
281         latency_hbox.pack_start (latency_display, false, false);
282         latency_frame.add (latency_hbox);
283         
284         output_selector.set_min_height_divisor (2);
285         input_selector.set_min_height_divisor (2);
286         
287         pack_start (latency_frame);
288         pack_start (output_selector, true, true);
289         pack_start (input_selector, true, true);
290
291         latency_button.signal_toggled().connect (mem_fun (*this, &PortInsertUI::latency_button_toggled));
292 }
293
294 bool
295 PortInsertUI::check_latency_measurement ()
296 {
297         MTDM* mtdm = _pi->mtdm ();
298
299         if (mtdm->resolve () < 0) {
300                 latency_display.set_text (_("No signal detected"));
301                 return true;
302         }
303
304         if (mtdm->err () > 0.3) {
305                 mtdm->invert ();
306                 mtdm->resolve ();
307         }
308
309         char buf[64];
310         nframes_t sample_rate = AudioEngine::instance()->frame_rate();
311
312         if (sample_rate == 0) {
313                 latency_display.set_text (_("Disconnected from audio engine"));
314                 _pi->stop_latency_detection ();
315                 return false;
316         }
317
318         snprintf (buf, sizeof (buf), "%10.3lf frames %10.3lf ms", mtdm->del (), mtdm->del () * 1000.0f/sample_rate);
319
320         bool solid = true;
321
322         if (mtdm->err () > 0.2) {
323                 strcat (buf, " ??");
324                 solid = false;
325         }
326
327         if (mtdm->inv ()) {
328                 strcat (buf, " (Inv)");
329                 solid = false;
330         }
331
332         if (solid) {
333                 _pi->set_measured_latency ((nframes_t) rint (mtdm->del()));
334                 strcat (buf, " (set)");
335         }
336
337         latency_display.set_text (buf);
338         return true;
339 }
340
341 void
342 PortInsertUI::latency_button_toggled ()
343 {
344         if (latency_button.get_active ()) {
345
346                 _pi->start_latency_detection ();
347                 latency_display.set_text (_("Detecting ..."));
348                 latency_timeout = Glib::signal_timeout().connect (mem_fun (*this, &PortInsertUI::check_latency_measurement), 250);
349
350         } else {
351                 _pi->stop_latency_detection ();
352                 latency_timeout.disconnect ();
353         }
354 }
355
356
357 void
358 PortInsertUI::redisplay ()
359 {
360         input_selector.setup_ports (input_selector.other());
361         output_selector.setup_ports (output_selector.other());
362 }
363
364 void
365 PortInsertUI::finished (IOSelector::Result r)
366 {
367         input_selector.Finished (r);
368         output_selector.Finished (r);
369 }
370
371
372 PortInsertWindow::PortInsertWindow (ARDOUR::Session* sess, boost::shared_ptr<ARDOUR::PortInsert> pi, bool can_cancel)
373         : ArdourDialog ("port insert dialog"),
374           _portinsertui (this, sess, pi),
375           ok_button (can_cancel ? _("OK"): _("Close")),
376           cancel_button (_("Cancel"))
377 {
378
379         set_name ("IOSelectorWindow");
380         string title = _("Port Insert ");
381         title += pi->name();
382         set_title (title);
383
384         ok_button.set_name ("IOSelectorButton");
385         if (!can_cancel) {
386                 ok_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::CLOSE, Gtk::ICON_SIZE_BUTTON)));
387         }
388         cancel_button.set_name ("IOSelectorButton");
389
390         if (can_cancel) {
391                 cancel_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::CANCEL, Gtk::ICON_SIZE_BUTTON)));
392                 get_action_area()->pack_start (cancel_button, false, false);
393         } else {
394                 cancel_button.hide();
395         }
396         get_action_area()->pack_start (ok_button, false, false);
397
398         get_vbox()->pack_start (_portinsertui);
399
400         ok_button.signal_clicked().connect (sigc::mem_fun (*this, &PortInsertWindow::accept));
401         cancel_button.signal_clicked().connect (sigc::mem_fun (*this, &PortInsertWindow::cancel));
402
403         signal_delete_event().connect (sigc::mem_fun (*this, &PortInsertWindow::wm_delete), false);
404
405         pi->DropReferences.connect (going_away_connection, invalidator (*this), boost::bind (&PortInsertWindow::plugin_going_away, this), gui_context());
406 }
407
408 bool
409 PortInsertWindow::wm_delete (GdkEventAny* /*event*/)
410 {
411         accept ();
412         return true;
413 }
414
415 void
416 PortInsertWindow::plugin_going_away ()
417 {
418         ENSURE_GUI_THREAD (*this, &PortInsertWindow::plugin_going_away)
419
420         going_away_connection.disconnect ();
421         delete_when_idle (this);
422 }
423
424 void
425 PortInsertWindow::on_map ()
426 {
427         _portinsertui.redisplay ();
428         Window::on_map ();
429 }
430
431
432 void
433 PortInsertWindow::cancel ()
434 {
435         _portinsertui.finished (IOSelector::Cancelled);
436         hide ();
437 }
438
439 void
440 PortInsertWindow::accept ()
441 {
442         _portinsertui.finished (IOSelector::Accepted);
443         hide ();
444 }
445