the big Route structure refactor. !!!! THIS WILL ***NOT LOAD*** PRIOR 3.0 or 2.X...
[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/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 (ARDOUR::Session& session, boost::shared_ptr<ARDOUR::IO> io)
45         : PortMatrix (session, io->default_type())
46         , _io (io)
47 {
48         /* signal flow from 0 to 1 */
49
50         _find_inputs_for_io_outputs = (_io->direction() == IO::Output);
51
52         if (_find_inputs_for_io_outputs) {
53                 _other = 1;
54                 _ours = 0;
55         } else {
56                 _other = 0;
57                 _ours = 1;
58         }
59
60         _port_group.reset (new PortGroup (""));
61         _ports[_ours].add_group (_port_group);
62         
63         setup_all_ports ();
64 }
65
66 void
67 IOSelector::setup_ports (int dim)
68 {
69         _ports[dim].suspend_signals ();
70
71         if (dim == _other) {
72
73                 _ports[_other].gather (_session, _find_inputs_for_io_outputs);
74                 
75         } else {
76
77                 _port_group->clear ();
78                 _port_group->add_bundle (_io->bundle ());
79         }
80
81         _ports[dim].resume_signals ();
82 }
83
84 void
85 IOSelector::set_state (ARDOUR::BundleChannel c[2], bool s)
86 {
87         ARDOUR::Bundle::PortList const & our_ports = c[_ours].bundle->channel_ports (c[_ours].channel);
88         ARDOUR::Bundle::PortList const & other_ports = c[_other].bundle->channel_ports (c[_other].channel);
89
90         for (ARDOUR::Bundle::PortList::const_iterator i = our_ports.begin(); i != our_ports.end(); ++i) {
91                 for (ARDOUR::Bundle::PortList::const_iterator j = other_ports.begin(); j != other_ports.end(); ++j) {
92
93                         Port* f = _session.engine().get_port_by_name (*i);
94                         if (!f) {
95                                 return;
96                         }
97
98                         if (s) {
99                                 _io->connect (f, *j, 0);
100                         } else {
101                                 _io->disconnect (f, *j, 0);
102                         }
103                 }
104         }
105 }
106
107 PortMatrixNode::State
108 IOSelector::get_state (ARDOUR::BundleChannel c[2]) const
109 {
110         ARDOUR::Bundle::PortList const & our_ports = c[_ours].bundle->channel_ports (c[_ours].channel);
111         ARDOUR::Bundle::PortList const & other_ports = c[_other].bundle->channel_ports (c[_other].channel);
112
113         if (our_ports.empty() || other_ports.empty()) {
114                 /* we're looking at a bundle with no parts associated with this channel,
115                    so nothing to connect */
116                 return PortMatrixNode::UNKNOWN;
117         }
118
119         for (ARDOUR::Bundle::PortList::const_iterator i = our_ports.begin(); i != our_ports.end(); ++i) {
120                 for (ARDOUR::Bundle::PortList::const_iterator j = other_ports.begin(); j != other_ports.end(); ++j) {
121
122                         Port* f = _session.engine().get_port_by_name (*i);
123
124                         /* since we are talking about an IO, our ports should all have an associated Port *,
125                            so the above call should never fail */
126                         assert (f);
127                         
128                         if (!f->connected_to (*j)) {
129                                 /* if any one thing is not connected, all bets are off */
130                                 return PortMatrixNode::NOT_ASSOCIATED;
131                         }
132                 }
133         }
134
135         return PortMatrixNode::ASSOCIATED;
136 }
137
138 uint32_t
139 IOSelector::n_io_ports () const
140 {
141         if (!_find_inputs_for_io_outputs) {
142                 return _io->n_ports().get (_io->default_type());
143         } else {
144                 return _io->n_ports().get (_io->default_type());
145         }
146 }
147
148 void
149 IOSelector::add_channel (boost::shared_ptr<ARDOUR::Bundle> b)
150 {
151         /* we ignore the bundle parameter, as we know what it is that we're adding to */
152         
153         // The IO selector only works for single typed IOs
154         const ARDOUR::DataType t = _io->default_type ();
155
156         try {
157                 _io->add_port ("", this);
158         }
159         
160         catch (AudioEngine::PortRegistrationFailure& err) {
161                 MessageDialog msg (_("There are no more JACK ports available."));
162                 msg.run ();
163         }
164 }
165
166 void
167 IOSelector::remove_channel (ARDOUR::BundleChannel bc)
168 {
169         Port* f = _session.engine().get_port_by_name (bc.bundle->channel_ports(bc.channel)[0]);
170         if (!f) {
171                 return;
172         }
173         
174         _io->remove_port (f, this);
175 }
176
177 bool
178 IOSelector::list_is_global (int dim) const
179 {
180         return (dim == _other);
181 }
182
183 IOSelectorWindow::IOSelectorWindow (ARDOUR::Session& session, boost::shared_ptr<ARDOUR::IO> io, bool can_cancel)
184         : ArdourDialog ("I/O selector")
185         , _selector (session, io)
186         , add_button (_("Add Port"))
187         , disconnect_button (_("Disconnect All"))
188         , ok_button (can_cancel ? _("OK"): _("Close"))
189         , cancel_button (_("Cancel"))
190         , rescan_button (_("Rescan"))
191
192 {
193         /* XXX: what's this for? */
194         add_events (Gdk::KEY_PRESS_MASK | Gdk::KEY_RELEASE_MASK);
195         
196         set_name ("IOSelectorWindow2");
197
198         /* Disconnect All button */
199         disconnect_button.set_name ("IOSelectorButton");
200         disconnect_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::DISCONNECT, Gtk::ICON_SIZE_BUTTON)));
201         disconnect_button.signal_clicked().connect (sigc::mem_fun (_selector, &IOSelector::disassociate_all));
202         get_action_area()->pack_start (disconnect_button, false, false);
203
204         /* Add Port button */
205         add_button.set_name ("IOSelectorButton");
206         add_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::ADD, Gtk::ICON_SIZE_BUTTON)));
207         get_action_area()->pack_start (add_button, false, false);
208         add_button.signal_clicked().connect (sigc::bind (sigc::mem_fun (_selector, &IOSelector::add_channel), boost::shared_ptr<Bundle> ()));
209
210         /* Rescan button */
211         rescan_button.set_name ("IOSelectorButton");
212         rescan_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::REFRESH, Gtk::ICON_SIZE_BUTTON)));
213         rescan_button.signal_clicked().connect (sigc::mem_fun (*this, &IOSelectorWindow::rescan));
214         get_action_area()->pack_start (rescan_button, false, false);
215
216         io->PortCountChanged.connect (sigc::hide (mem_fun (*this, &IOSelectorWindow::ports_changed)));
217
218         /* Cancel button */
219         if (can_cancel) {
220                 cancel_button.set_name ("IOSelectorButton");
221                 cancel_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::CANCEL, Gtk::ICON_SIZE_BUTTON)));
222                 get_action_area()->pack_start (cancel_button, false, false);
223         } else {
224                 cancel_button.hide();
225         }
226         cancel_button.signal_clicked().connect (mem_fun(*this, &IOSelectorWindow::cancel));
227
228         /* OK button */
229         ok_button.set_name ("IOSelectorButton");
230         if (!can_cancel) {
231                 ok_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::CLOSE, Gtk::ICON_SIZE_BUTTON)));
232         }
233         ok_button.signal_clicked().connect (mem_fun(*this, &IOSelectorWindow::accept));
234         get_action_area()->pack_start (ok_button, false, false);
235
236         get_vbox()->set_spacing (8);
237
238         get_vbox()->pack_start (_selector, true, true);
239
240         set_position (Gtk::WIN_POS_MOUSE);
241
242         io_name_changed (this);
243         ports_changed ();
244
245         show_all ();
246
247         signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), this));
248 }
249
250 void
251 IOSelectorWindow::ports_changed ()
252 {
253         /* XXX make this insensitive based on port connectivity, not
254            port counts.
255         */
256
257         add_button.set_sensitive (true);
258 }
259
260 void
261 IOSelectorWindow::rescan ()
262 {
263         _selector.setup_ports (_selector.other());
264 }
265
266 void
267 IOSelectorWindow::cancel ()
268 {
269         _selector.Finished (IOSelector::Cancelled);
270         hide ();
271 }
272
273 void
274 IOSelectorWindow::accept ()
275 {
276         _selector.Finished (IOSelector::Accepted);
277         hide ();
278 }
279
280 void
281 IOSelectorWindow::on_map ()
282 {
283         _selector.setup_all_ports ();
284         Window::on_map ();
285 }
286
287 void
288 IOSelectorWindow::io_name_changed (void* src)
289 {
290         ENSURE_GUI_THREAD(bind (mem_fun(*this, &IOSelectorWindow::io_name_changed), src));
291         
292         string title;
293
294         if (!_selector.find_inputs_for_io_outputs()) {
295                 title = string_compose(_("%1 input"), _selector.io()->name());
296         } else {
297                 title = string_compose(_("%1 output"), _selector.io()->name());
298         }
299
300         set_title (title);
301 }
302
303 PortInsertUI::PortInsertUI (ARDOUR::Session& sess, boost::shared_ptr<ARDOUR::PortInsert> pi)
304         : input_selector (sess, pi->input())
305         , output_selector (sess, pi->output())
306 {
307         output_selector.set_min_height_divisor (2);
308         input_selector.set_min_height_divisor (2);
309         
310         pack_start (output_selector, true, true);
311         pack_start (input_selector, true, true);
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 (sess, pi),
332           ok_button (can_cancel ? _("OK"): _("Close")),
333           cancel_button (_("Cancel")),
334           rescan_button (_("Rescan"))
335 {
336
337         set_name ("IOSelectorWindow");
338         string title = _("ardour: ");
339         title += pi->name();
340         set_title (title);
341         
342         ok_button.set_name ("IOSelectorButton");
343         if (!can_cancel) {
344                 ok_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::CLOSE, Gtk::ICON_SIZE_BUTTON)));
345         }
346         cancel_button.set_name ("IOSelectorButton");
347         rescan_button.set_name ("IOSelectorButton");
348         rescan_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::REFRESH, Gtk::ICON_SIZE_BUTTON)));
349
350         get_action_area()->pack_start (rescan_button, false, false);
351         if (can_cancel) {
352                 cancel_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::CANCEL, Gtk::ICON_SIZE_BUTTON)));
353                 get_action_area()->pack_start (cancel_button, false, false);
354         } else {
355                 cancel_button.hide();
356         }
357         get_action_area()->pack_start (ok_button, false, false);
358
359         get_vbox()->pack_start (_portinsertui);
360
361         ok_button.signal_clicked().connect (mem_fun (*this, &PortInsertWindow::accept));
362         cancel_button.signal_clicked().connect (mem_fun (*this, &PortInsertWindow::cancel));
363
364         signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), reinterpret_cast<Window *> (this))); 
365
366         going_away_connection = pi->GoingAway.connect (mem_fun (*this, &PortInsertWindow::plugin_going_away));
367 }
368
369 void
370 PortInsertWindow::plugin_going_away ()
371 {
372         ENSURE_GUI_THREAD (mem_fun (*this, &PortInsertWindow::plugin_going_away));
373         
374         going_away_connection.disconnect ();
375         delete_when_idle (this);
376 }
377
378 void
379 PortInsertWindow::on_map ()
380 {
381         _portinsertui.redisplay ();
382         Window::on_map ();
383 }
384
385
386 void
387 PortInsertWindow::cancel ()
388 {
389         _portinsertui.finished (IOSelector::Cancelled);
390         hide ();
391 }
392
393 void
394 PortInsertWindow::accept ()
395 {
396         _portinsertui.finished (IOSelector::Accepted);
397         hide ();
398 }
399