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